明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索

[【PyCAD】] Pycad来袭,你还在等什么

    [复制链接]
发表于 2019-12-17 08:45 | 显示全部楼层

牛逼,非常牛逼。
 楼主| 发表于 2019-12-22 21:39 | 显示全部楼层
本帖最后由 雪山飞狐_lzh 于 2019-12-30 20:38 编辑

例程贴
1、hello world
  1. @command()
  2. def helloworld(doc):
  3.     #显示hello world!
  4.     print('hello world!')
  5.     #打开事务
  6.     with dbtrans(doc) as tr:
  7.         #打开当前空间
  8.         btr = tr.opencurrspace()
  9.         #向当前空间添加直线
  10.         tr.addentity(btr, acdb.Line(acge.Point3d(0,0,0), acge.Point3d(10,10,0)))
复制代码

2、模拟直线命令
  1. @command()
  2. def myline2(doc):
  3.     #模拟直线命令
  4.     with dbtrans(doc) as tr:
  5.         btr = tr.opencurrspace()
  6.         lines, pts = [], []
  7.         while True:
  8.             n = len(pts)
  9.             if n == 0:
  10.                 #获取起点
  11.                 respt = edx.getpoint('\n指定第一个点:')
  12.                 if not respt.ok(): return
  13.                 pts.append(respt.value)
  14.             else:
  15.                 if n > 2:
  16.                     opts = aced.PromptPointOptions(
  17.                         '\n指定下一点或[闭合(C)/放弃(U)]', 'C U')
  18.                 else:
  19.                     opts = aced.PromptPointOptions(
  20.                         '\n指定下一点或[放弃(U)]', 'U')
  21.                 #获取下一点
  22.                 opts.AllowNone = True
  23.                 opts.BasePoint = pts[-1]
  24.                 opts.UseBasePoint = True
  25.                 opts.UseDashedLine = False
  26.                 respt = edx.getpoint(opts)
  27.                 if respt.ok():
  28.                     #如果正确输入点就添加一条直线
  29.                     pt = respt.value
  30.                     if n > 0:
  31.                         line = acdb.Line(pts[-1], pt)
  32.                         tr.addentity(btr, line)
  33.                         tr.flush(line)
  34.                         lines.append(line)
  35.                         pts.append(pt)
  36.                 elif respt.keyword('C'):
  37.                     #如果输入关键字C则闭合并退出
  38.                     line = acdb.Line(pts[-1], pts[0])
  39.                     tr.addentity(btr, line)
  40.                     break
  41.                 elif respt.keyword('U'):
  42.                     #如果输入关键字U则回退
  43.                     del pts[-1]
  44.                     if n > 1:
  45.                         lines[-1].Erase()
  46.                         tr.flush(line)
  47.                         del lines[-1]
  48.                 else: break
复制代码

3、linq去重,留下同心圆中直径最小的
  1. @command(flags=acrx.CommandFlags.UsePickSet)
  2. def linqtest(doc):
  3.     #linq去重,留下同心圆中直径最小的
  4.     #选择圆
  5.     ss = edx.ssget(filters=(0, "circle"))
  6.     if not ss.ok(): return
  7.     with dbtrans(doc) as tr:
  8.         #流程:按圆心分组->每组排序后->跳过第一个元素
  9.         cirs = \
  10.             ss.Cast[acdb.ObjectId]()\
  11.             .Select(lambda i: tr.getobject(i))\
  12.             .GroupBy(lambda c: c.Center)\
  13.             .SelectMany(lambda cs: cs.OrderBy(lambda c: c.Radius).Skip(1))
  14.         tr.erase(*cirs)
复制代码

4、获取曲线自交点
  1. @command()
  2. def ccitest2(doc):
  3.     #获取曲线自交点
  4.     #获取曲线
  5.     opts = aced.PromptEntityOptions('\n请选择曲线:')
  6.     opts.SetRejectMessage('错误类型!')
  7.     opts.AddAllowedClass(acdb.Curve, False)
  8.     res = edx.entsel(opts)
  9.     if not res.ok(): return
  10.     with dbtrans(doc) as tr:
  11.         pl = tr.getobject(res[0])  #type: acdb.Curve
  12.         curve3d = pl.GetGeCurve()
  13.         #用cci类判断自相交
  14.         cci = acge.CurveCurveIntersector3d(curve3d, curve3d, acge.Vector3d.ZAxis)
  15.         for k in range(cci.NumberOfIntersectionPoints):
  16.             #显示自相交点
  17.             pars = cci.GetIntersectionParameters(k)
  18.             print(curve3d.EvaluatePoint(pars[0]))
复制代码

5、两曲线间最短距离
  1. @command()
  2. def mindict(doc):
  3.     #两曲线间最短距离
  4.     #获取曲线
  5.     opts = aced.PromptEntityOptions('\n请选择曲线:')
  6.     opts.SetRejectMessage('错误类型!')
  7.     opts.AddAllowedClass(acdb.Curve, False)
  8.     res = edx.entsel(opts)
  9.     if not res.ok(): return
  10.     res2 = edx.entsel(opts)
  11.     if not res2.ok(): return
  12.     with dbtrans(doc) as tr:
  13.         curves = (tr.getobject(res.ObjectId).GetGeCurve(), tr.getobject(res2.ObjectId).GetGeCurve())
  14.         pocs = curves[0].GetClosestPointTo(curves[1])
  15.         line = acdb.Line(pocs[0].Point, pocs[1].Point)
  16.         tr.addentity(tr.opencurrspace(), line)
复制代码
发表于 2019-12-22 22:34 | 显示全部楼层
这就厉害了,顶起来
 楼主| 发表于 2019-12-30 23:08 | 显示全部楼层
修正版本0.9.279.6d
短时间不会大版本更新了
等过年把调试器做完,准备升1.0:)
发表于 2020-1-5 13:35 | 显示全部楼层
怎么加载了NFox.Pycad.Acad.dll没有PYE命令,,
 楼主| 发表于 2020-1-6 00:37 | 显示全部楼层
cad版本多少?
pycad的两个版本都试过吗
发表于 2020-1-9 16:04 | 显示全部楼层
顶顶顶;
lisp调试太麻烦,不知道错误虫子在哪?
如果能用py代替lsp,真是大快人心,这样每周都可节省不少时间了!
谢谢版主
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2024-5-2 06:09 , Processed in 0.470404 second(s), 17 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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