Shiven Garg
Shiven Garg

Reputation: 35

Autocad using pyautocad / comtypes to copy objects from one drawing to another

I am trying to copy objects from specific layers from multiple autocad drawings to one autocad drawing (basically trying to combine them). But I am getting an error. I imported both pyautocad and comtypes.client. This is my code:

  # 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

I am getting an error at retObjects = main_drawing.CopyObjects(objs) saying that the objs is a 'Invalid object array' for CopyObjects method.... How to fix??

Upvotes: 1

Views: 312

Answers (2)

Shiven Garg
Shiven Garg

Reputation: 35

I actually managed to do it using 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)

Upvotes: 0

danielm103
danielm103

Reputation: 141

COM wrappers want this as a variant/Safe Array, try converting the obj list to a safe array, example:

win32com.client.VARIANT(pythoncom.VT_ARRAY | pythoncom.VT_DISPATCH, objs)

Upvotes: 0

Related Questions