pyautocad中批量更新实体属性

问题描述 投票:0回答:1

我正在制作一个 pyqt 小部件工具,它应该使用 AutoCAD 中的数据进行操作。 基本上,它允许用户选择颜色并将其填充到 AutoCAD 中选定的填充对象中。它实际上可以直接在 AutoCAD 中完成,但有时打开自定义调色板并手动选择颜色是一项耗时的任务。

我当前的工具基于

pyautocad
pyqt
模块。这是它的样子:

打开小部件时,用户可以选择舱口,然后选择小部件中的类型,然后按

Fill type
按钮,最后我们得到重新绘制的舱口。

这就是 Python 代码中的样子:

red, green, blue = 146, 32, 6
acad = Autocad() # initializing AutoCAD connection
list_to_upd = []
curr_sset = acad.doc.PickfirstSelectionSet # get a list of selected entities
new_color = None
for obj in curr_sset:
    if obj.ObjectName == 'AcDbHatch': # check if entity is hatch type
        if not new_color:
            tcolor = obj.TrueColor
            tcolor.SetRGB(red, green, blue)
            new_color = tcolor
        obj.TrueColor = new_color # set a new background color for hatch

它工作正常,但只适用于少量舱口。当所选特征的数量较多时,它的工作时间会明显更长。例如,即使选择了 50 个影线,也可能需要 10-15 秒来填充新颜色。

我一直在寻找解决方案。模块的作者讲述了使用缓存,但我不知道如何在我的代码中实现它。还尝试使用多处理,但看起来无法使用 ActiveX 对象。还认为有一个解决方案可以像 LISP 或常规命令一样传递提示,但到目前为止我还不熟悉 AutoCAD 语法。有什么想法吗?

python lisp autocad
1个回答
0
投票

我找到了 autolisp 命令的替代方案。工作速度超快,没有延迟。

但是我使用的是

win32com.client
模块

acad = win32com.client.Dispatch("AutoCAD.Application")
doc = acad.ActiveDocument  
if len(acad.ActiveDocument.PickfirstSelectionSet): # check if there is a selection of entities
    doc.SendCommand('(setq p1 (cadr (ssgetfirst)))\n')
    doc.SendCommand('(command "_.chprop" p1 "" "_C" "_truecolor" "{},{},{}" "")\n'.format(red, green, blue))
    doc.SendCommand('(sssetfirst nil p1)\n') # use it if you need to highlight a previous selection
© www.soinside.com 2019 - 2024. All rights reserved.