使用pyautocad/comtypes在AutoCAD中将对象从一个图纸复制到另一个图纸
我正在尝试从多个AutoCAD图纸中复制特定图层的对象到一个AutoCAD图纸中(基本上就是想把它们合并在一起)。但是我遇到了一个错误。我导入了pyautocad和comtypes.client这两个库。这是我的代码:
# Copy model space of other drawings
for drawing in drawingslist[1:]:
drawing.Activate()
main_drawing = acad.ActiveDocument
print(drawing)
print(main_drawing)
#Select all entities in the drawing
source_model_space = main_drawing.ModelSpace
destination_model_space = destination_drawing.ModelSpace
objs = []
for obj in source_model_space:
if obj.Layer in target_layers:
objs.append(obj)
retObjects = main_drawing.CopyObjects(objs)
# Close the drawing
我在这行代码retObjects = main_drawing.CopyObjects(objs)出错了,提示说objs是一个“无效的对象数组”,不能用于CopyObjects方法……我该怎么解决这个问题呢?
2 个回答
0
COM包装器需要这个作为变体/安全数组,试着把对象列表转换成安全数组,下面是一个例子:
win32com.client.VARIANT(pythoncom.VT_ARRAY | pythoncom.VT_DISPATCH, objs)
0
我实际上是通过使用sendcommand来实现的:
# if you get the best interface, you can investigate its properties with 'dir()'
m = comtypes.client.GetBestInterface(source_model_space)
handle_string = 'COPYBASE\n'
handle_string += '0,0,0\n'
for entity in m:
time.sleep(0.1)
if entity.Layer in target_layers:
time.sleep(0.1)
handle_string += '(handent "' + entity.Handle + '")\n'
handle_string += '\n'
acad.ActiveDocument.SendCommand(handle_string)
time.sleep(1)
# Paste the objects at the same location in the target drawing
acad.ActiveDocument = destination_drawing
handle_string = 'PASTECLIP\n'
handle_string += '0,0,0\n'
acad.ActiveDocument.SendCommand(handle_string)
time.sleep(1)