PYCAD 正弦波路径动态显示,定时器用法
本帖最后由 枫叶棋语 于 2023-7-25 11:23 编辑#变换函数
class SinCurve:
def __init__(self, doc, basept: Point3d, step):
self.doc = doc
self.ObjectIds: List = []
self.basept = basept
self.step = step
self.A = 100
self.x = 0
self.ColorIndex = 1
def route(self):
v = Vector3d(
self.x,
self.A * math.sin(self.x * math.pi / 180),
0,
)
self.x += self.step
if self.x >= 360 * 10:
self.x = 0
return self.basept + v
def __call__(self):
with acdoc(self.doc):
with dbtrans(self.doc) as tr:
btr = tr.opencurrspace()
self.len = len(self.ObjectIds)
if self.len > 0:
for objid in self.ObjectIds.copy():
ent: Entity = tr.getobject(objid, OpenMode.ForWrite)
ent.Transparency = Transparency(
alpha=int(ent.Transparency.Alpha * 0.9)
)
ent.Radius *= 0.9
if ent.Transparency.Alpha < 10:
self.ObjectIds.remove(objid)
tr.erase(ent)
else:
tr.flush(ent)
cir: Entity = Circle()
cir.Radius = 10
cir.Center = self.route()
cir.ColorIndex = self.ColorIndex
cir.Transparency = Transparency(alpha=255)
prinf(cir.Transparency.Alpha)
tr.addentity(btr, cir)
self.ObjectIds.append(cir.ObjectId)
self.ColorIndex += 1
if self.ColorIndex > 10:
self.ColorIndex = 1
#定时器
class AutoChange:
from time import time
import System.Windows.Forms as Winforms
def __init__(self, instance):
self.T0 = 0
self.T1 = 0
self.instance = instance
self.stopflag = False
self.duration = 10
def callback(self, sender, event):
try:
self.instance.__call__()
Winforms.Cursor.Position = Winforms.Cursor.Position
if self.stopflag:
self.autostop()
except Exception as ex:
prinf(ex.Message)
self.stop()
except System.Exception as ex:
prinf(ex.Message)
self.stop()
def set_timer(self, inter_time: float):
# 时间单位为毫秒
self.timer = Winforms.Timer()
self.timer.Interval = inter_time
self.timer.Tick += System.EventHandler(self.callback)
def start(self):
self.T0 = time()
self.timer.Start()
def stop(self):
self.timer.Stop()
self.timer.Dispose()
def autostop(self):
# 时间单位为秒
if self.T0 != 0:
self.T1 = time()
if self.T1 - self.T0 > self.duration:
self.stop()
@Command()
def ts01(doc: Document):
# global T0,T1,timer
res = getpoint()
if not res.ok:
return
center = res.value
sincurve = SinCurve(doc=doc, basept=center, step=10)
auto_change = AutoChange(sincurve)
auto_change.set_timer(50)
auto_change.duration = 100
auto_change.start()
auto_change.stopflag = True
QQ群:582889822 枫哥越来越骚了
页:
[1]