[英]How to change the properties of an AutoCAD object programmatically in Python
I want to automatically process a bunch of AutoCAD drawings using Python.我想使用 Python 自动处理一堆 AutoCAD 图纸。 To that end I need to change the properties of the drawing entities programmatically.为此,我需要以编程方式更改绘图实体的属性。 I've been struggling for a while, to no avail.我已经挣扎了一段时间,没有结果。
This is the code I'm using to read the .dxf
and open the .dwg
files:这是我用来读取.dxf
和打开.dwg
文件的代码:
import win32com.client
import dxfgrabber
import os
folder = r'C:\path\to\my\folder'
filename = 'my_file'
dwg_path = os.path.join(folder, filename + '.dwg')
dxf_path = os.path.join(folder, filename + '.dxf')
acad = win32com.client.dynamic.Dispatch("AutoCAD.Application")
doc = acad.Documents.Open(dwg_path)
acad.Visible = True
dxf = dxfgrabber.readfile(dxf_path)
Then I iterate over the objects placed in a layer called FirstLayer
and select one of them:然后我遍历放置在名为FirstLayer
的层中的对象并选择其中一个:
item = [obj for obj in dxf.entities if obj.layer == 'FirstLayer'][0]
This particular entity is a text object:这个特定的实体是一个文本对象:
In [1122]: type(item)
Out[1122]: dxfgrabber.dxfentities.Text
In [1123]: item.insert
Out[1123]: (4022.763956904745, 3518.371877135191, 0.0)
In [1124]: item.layer
Out[1124]: 'FirstLayer'
In [1125]: item.handle
Out[1125]: '298'
My goal is to change properties such as color
, layer
, etc. This is one of my attempts to move the text object to a different layer called SecondLayer
:我的目标是更改color
、 layer
等属性。这是我将文本对象移动到名为SecondLayer
的不同图层的SecondLayer
:
doc.SendCommand(f'CHPROP {item.insert[0]},{item.insert[1]} LA\n SecondLayer\n ')
I guess the problem is that the object cannot be selected through the coordinates of the insertion point.我猜问题是无法通过插入点的坐标选择对象。 I also tried (unsuccessfully) to select the object by its handle using the following script:我还尝试(未成功)使用以下脚本通过句柄选择对象:
_CHPROP (handent 298) _LA SecondLayer
Any ideas on how to work around this?关于如何解决这个问题的任何想法?
EDIT编辑
I came up with the following solution before @Lee Mac posted his excellent answer:在@Lee Mac 发布他的出色回答之前,我想出了以下解决方案:
doc.SendCommand(f'CHPROP (handent "{item.handle}") \n_LA SecondLayer\n ')
Upon issuing the CHPROP
command, the subsequent object selection prompt will either require you to supply one or more entity names (which may be obtained by converting a handle using the AutoLISP handent
function), or supplying a selection set (which may be obtained using the AutoLISP ssget
function).一旦发布CHPROP
命令,后续的对象选择提示将或者需要以提供一个或多个实体的名称(其可以通过将使用的AutoLISP手柄来获得handent
,或提供选择集(其可以使用来获得功能) AutoLISP ssget
函数)。
You were very close with your use of handent
, however entity handles in AutoCAD are represented by hexadecimal strings and so you will need to supply the handent
function with a string argument surrounded by double-quotes, eg:您对handent
的使用非常接近,但是 AutoCAD 中的实体句柄由十六进制字符串表示,因此您需要为handent
函数提供一个由双引号括起来的字符串参数,例如:
(handent "298")
If the supplied handle is valid, handent
will then return an entity name pointer:如果提供的句柄有效, handent
将返回一个实体名称指针:
_$ (handent "3B8")
<Entity name: 7ffff706880>
However, since CHPROP
accepts a selection set argument, you needn't iterate over every entity, but instead simply supply CHPROP
with a filter selection set, eg:但是,由于CHPROP
接受选择集参数,因此您无需遍历每个实体,而只需为CHPROP
提供过滤器选择集,例如:
doc.SendCommand(f'CHPROP (ssget "_x" (list (cons 8 "FirstLayer"))) LA\n SecondLayer\n ')
The handle in DXF files is stored as HEX string, maybe AutoCAD requires an integer value, but I am not an AutoCAD expert (@LeeMac). DXF 文件中的句柄存储为十六进制字符串,也许 AutoCAD 需要一个整数值,但我不是 AutoCAD 专家 (@LeeMac)。 Convert HEX string to int in Python by int('298', 16)
.在 Python 中通过int('298', 16)
将 HEX 字符串转换为 int。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.