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

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

shivengarg8426
Explorer Explorer
3,373 Views
7 Replies
Message 1 of 8

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

shivengarg8426
Explorer
Explorer

Hi, I KNOW THIS IS FOR VBA FOR AUTOCAD BUT I NEED HELP.

I am using python and using both pyautocad and comtypes.client which is quite similar to VBA . 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. 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??

0 Likes
Accepted solutions (1)
3,374 Views
7 Replies
Replies (7)
Message 2 of 8

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

 

if you’re using a current version of Autocad, dump pyautocad and get AxApp24.py and AxApp24.pyi from here https://github.com/CEXT-Dan/PyRx

it converts all the python types to COM types for you

 

modify getApp at the bottom if you need to create an instance… the project is meant to run in process, but it should work fine out of process

 

def getApp() -> IAcadApplication:
    id = AcadApplication.CLSID
    return win32com.client.GetActiveObject(id)
Python for AutoCAD, Python wrappers for ARX https://github.com/CEXT-Dan/PyRx
Message 3 of 8

Ed__Jobe
Mentor
Mentor

Welcome to the forums. See this post to learn how to post your code.

Ed


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
How to post your code.

EESignature

0 Likes
Message 4 of 8

shivengarg8426
Explorer
Explorer

I am using autocad 2013, which version can I use for AxApp24? I think I also have autocad 2017 on another computer

0 Likes
Message 5 of 8

daniel_cadext
Advisor
Advisor

yeah, sorry, those versions are a bit old, the wrappers are acad2021 or later

If you look at the top of AxApp24.py,  there's utility functions that may be useful for converting python types to COM

 

Python for AutoCAD, Python wrappers for ARX https://github.com/CEXT-Dan/PyRx
Message 6 of 8

shivengarg8426
Explorer
Explorer
Accepted solution

I actually did it using semdcommand instead. Here is the code I used:

# 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:
if entity.Layer in target_layers:
handle_string += '(handent "' + entity.Handle + '")\n'
handle_string += '\n\n'
acad.ActiveDocument.SendCommand(handle_string)

# 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'
handle_string += '\n\n'
acad.ActiveDocument.SendCommand(handle_string)

full code can be found here: https://github.com/shiven2001/pyautocad/blob/main/copy%20objects%20between%20multiple%20drawings.py

Message 7 of 8

contactEUNB5
Community Visitor
Community Visitor

Good Morning Daniel,

 

I was searching for a solution for pyautocad, and found this blog.

 

I visited the PyRx blog (https://pyarx.blogspot.com/) and the Pipy org webpage (https://pypi.org/project/cad-pyrx/) : I installed using pip as showed. The installation worked correctly and I have the library on my pc, with all files in it.

 

But, the problem arises for me at this step :

Use APPLOAD command or the startup suite to load PyRx in CAD application, example:

_APPLOAD
%localappdata%\Programs\Python\Python312\Lib\site-packages\pyrx\RxLoaderZ25.0.zrx

When I launch appload from autocad (I am using AutoCAD 2025), I cannot charge any .zrx file - the manager only allows for these extensions :

.arx

.crx

.Isp

.dvb

.dbx

.vlx

.fas

 

If you know how I can fix this, your help would be very much appreciated.

 

Thanks a lot, and hope to hear from you soon,

 

Patrice

0 Likes
Message 8 of 8

daniel_cadext
Advisor
Advisor

Hi,

Try replacing RxLoaderZ25.0.zrx with RxLoader25.0.arx 

 

for reference

RxLoader24.0.arx = 2021

RxLoader24.1.arx = 2022

RxLoader24.2.arx = 2023

RxLoader24.3.arx = 2024

RxLoader25.0.arx = 2025

Python for AutoCAD, Python wrappers for ARX https://github.com/CEXT-Dan/PyRx
0 Likes