明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 486|回复: 1

[【PyCAD】] PYCAD 正弦波路径动态显示,定时器用法

  [复制链接]
发表于 2023-7-25 10:41 | 显示全部楼层 |阅读模式
本帖最后由 枫叶棋语 于 2023-7-25 11:23 编辑
  1. #变换函数
  2. class SinCurve:
  3.     def __init__(self, doc, basept: Point3d, step):
  4.         self.doc = doc
  5.         self.ObjectIds: List[ObjectId] = []
  6.         self.basept = basept
  7.         self.step = step
  8.         self.A = 100
  9.         self.x = 0
  10.         self.ColorIndex = 1

  11.     def route(self):
  12.         v = Vector3d(
  13.             self.x,
  14.             self.A * math.sin(self.x * math.pi / 180),
  15.             0,
  16.         )
  17.         self.x += self.step
  18.         if self.x >= 360 * 10:
  19.             self.x = 0
  20.         return self.basept + v

  21.     def __call__(self):
  22.         with acdoc(self.doc):
  23.             with dbtrans(self.doc) as tr:
  24.                 btr = tr.opencurrspace()
  25.                 self.len = len(self.ObjectIds)
  26.                 if self.len > 0:
  27.                     for objid in self.ObjectIds.copy():
  28.                         ent: Entity = tr.getobject(objid, OpenMode.ForWrite)
  29.                         ent.Transparency = Transparency(
  30.                             alpha=int(ent.Transparency.Alpha * 0.9)
  31.                         )
  32.                         ent.Radius *= 0.9
  33.                         if ent.Transparency.Alpha < 10:
  34.                             self.ObjectIds.remove(objid)
  35.                             tr.erase(ent)
  36.                         else:
  37.                             tr.flush(ent)

  38.                 cir: Entity = Circle()
  39.                 cir.Radius = 10
  40.                 cir.Center = self.route()
  41.                 cir.ColorIndex = self.ColorIndex
  42.                 cir.Transparency = Transparency(alpha=255)
  43.                 prinf(cir.Transparency.Alpha)
  44.                 tr.addentity(btr, cir)
  45.                 self.ObjectIds.append(cir.ObjectId)
  46.                 self.ColorIndex += 1
  47.                 if self.ColorIndex > 10:
  48.                     self.ColorIndex = 1

  49. #定时器
  50. class AutoChange:
  51.     from time import time
  52.     import System.Windows.Forms as Winforms

  53.     def __init__(self, instance):
  54.         self.T0 = 0
  55.         self.T1 = 0
  56.         self.instance = instance
  57.         self.stopflag = False
  58.         self.duration = 10

  59.     def callback(self, sender, event):
  60.         try:
  61.             self.instance.__call__()
  62.             Winforms.Cursor.Position = Winforms.Cursor.Position
  63.             if self.stopflag:
  64.                 self.autostop()

  65.         except Exception as ex:
  66.             prinf(ex.Message)
  67.             self.stop()

  68.         except System.Exception as ex:
  69.             prinf(ex.Message)
  70.             self.stop()

  71.     def set_timer(self, inter_time: float):
  72.         # 时间单位为毫秒
  73.         self.timer = Winforms.Timer()
  74.         self.timer.Interval = inter_time
  75.         self.timer.Tick += System.EventHandler(self.callback)

  76.     def start(self):
  77.         self.T0 = time()
  78.         self.timer.Start()

  79.     def stop(self):
  80.         self.timer.Stop()
  81.         self.timer.Dispose()

  82.     def autostop(self):
  83.         # 时间单位为秒
  84.         if self.T0 != 0:
  85.             self.T1 = time()
  86.             if self.T1 - self.T0 > self.duration:
  87.                 self.stop()


  88. @Command()
  89. def ts01(doc: Document):
  90.     # global T0,T1,timer
  91.     res = getpoint()
  92.     if not res.ok:
  93.         return
  94.     center = res.value
  95.     sincurve = SinCurve(doc=doc, basept=center, step=10)
  96.     auto_change = AutoChange(sincurve)
  97.     auto_change.set_timer(50)
  98.     auto_change.duration = 100
  99.     auto_change.start()
  100.     auto_change.stopflag = True
复制代码



QQ群:582889822

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

x

评分

参与人数 1明经币 +1 收起 理由
Bao_lai + 1 很给力!

查看全部评分

发表于 2023-7-25 15:20 | 显示全部楼层
枫哥越来越骚了
回复 支持 1 反对 0

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2024-4-28 23:11 , Processed in 0.274557 second(s), 25 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表