明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 602|回复: 3

Pycad ,使用定时器实现旋转圆

[复制链接]
发表于 2023-7-21 20:08 | 显示全部楼层 |阅读模式
本帖最后由 枫叶棋语 于 2023-7-21 20:43 编辑
  1. from math import pi
  2. from time import time
  3. import System.Windows.Forms as Winforms
  4. def Polar(center: Point3d, angle: float, distance: float):
  5.     import math
  6.     v1 = Vector3d(distance, 0, 0)
  7.     v1 = v1.RotateBy(angle * math.pi / 180, Vector3d.ZAxis)  # 将角度转换为弧度
  8.     return center + v1

  9. def muti_polar (cir: Entity,center:Point3d,angle: float):
  10.     import math
  11.     matrix =Matrix3d.Rotation(angle=angle*180/math.pi,axis=Vector3d.ZAxis,center=center)
  12.     cir.TransformBy(matrix)


  13. @Command(lock= False)
  14. def ts01(doc:Document):
  15.     res = getpoint()
  16.     if not res.ok:
  17.         return
  18.     center = res.value
  19.     angle_list = [0, 120,240]
  20.     pts = [Polar(center,x,100) for x in angle_list]
  21.     cir_ids = []
  22.     color =1
  23.     with dbtrans(doc) as tr:
  24.         btr = tr.opencurrspace()
  25.         for x in pts :
  26.             cir:Entity =Circle()
  27.             cir.Center =x
  28.             cir.Radius =20
  29.             cir.ColorIndex =color
  30.             color +=1
  31.             tr.addentity(btr,cir)
  32.             tr.flush(cir)
  33.             cir_ids.append(cir.ObjectId)
  34.     T0 =time()
  35.     timer =Winforms.Timer()     
  36.     def on_timer_tick(sender,event):
  37.         doc:Document =Application.DocumentManager.MdiActiveDocument
  38.         with doc.LockDocument():
  39.             with dbtrans (doc)as tr:
  40.                 try:
  41.                     T1 =time()
  42.                     if T1 -T0 >20:#5秒后结束定时器
  43.                         timer.Stop()
  44.                         timer.Dispose()
  45.                     for objid in cir_ids:
  46.                         cir = tr.getobject(objid,OpenMode.ForWrite)
  47.                         muti_polar(cir,center,10)
  48.                         tr.flush(cir)
  49.                 except Exception as ex:
  50.                     prinf (ex.Message)
  51.                     timer.Stop()
  52.                     timer.Dispose()

  53.     timer =Winforms.Timer()     
  54.     timer.Interval =50
  55.     timer.Tick += System.EventHandler(on_timer_tick)
  56.     timer.Start()


复制代码

发表于 2023-7-24 14:16 | 显示全部楼层
枫哥搞个动图看看呀
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2024-4-28 18:42 , Processed in 0.401361 second(s), 22 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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