Community
AutoCAD LT Forum
Welcome to Autodesk’s AutoCAD LT Forums. Share your knowledge, ask questions, and explore popular AutoCAD LT topics.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Unable to get AutoCAD Object using Python to import DWG file

6 REPLIES 6
SOLVED
Reply
Message 1 of 7
Anonymous
6269 Views, 6 Replies

Unable to get AutoCAD Object using Python to import DWG file

Anonymous
Not applicable

I am unable to get the object of AutoCAD in Python. I have to import DWG files using Python and have to render and perform operations on them.

 

In the initial step itself, while checking to get the Active Object if available or creating new AutoCAD object if not available, I am getting below error. I have AutoCAD installed and also opened in my system. Please suggest what I am doing wrong and what this error actually means. Do AutoCAD requires any configurations, environment variables setup or before getting any object do any path importing is required in Python.

 

import os
import comtypes.client
from comtypes import COMError
from comtypes.client import CreateObject, GetActiveObject

def main():
    try: #Get AutoCAD running instance
        acad = GetActiveObject("AutoCAD.Application.20")
        state = True
    except(OSError,COMError): #If autocad isn't running, open it
        acad = CreateObject("AutoCAD.Application.20",dynamic=True)
        state = False
    
    if state: #If you have only 1 opened drawing
        doc = acad.Documents.Items(0)
    else:
        filepath = "old plan pass.dwg" #Replace it with the actual drawing path
        doc = acad.Documents.Open(filepath)

    #Our example command is to draw a line from (0,0) to (5,5)
    command_str = '._line 0,0 5,5 ' #Notice that the last SPACE is equivalent to hiting ENTER
    #You should separate the command's arguments also with SPACE

    #Send the command to the drawing
    doc.SendCommand(command_str)

#Execution Part
if __name__ == '__main__':
    main()

Gettings this Error.

 

---------------------------------------------------------------------------
OSError                                   Traceback (most recent call last)
<ipython-input-12-10d674ebc1da> in main()
      8     try: #Get AutoCAD running instance
----> 9         acad = GetActiveObject("AutoCAD.Application.20")
     10         state = True

~\Anaconda3\envs\py36\lib\site-packages\comtypes\client\__init__.py in GetActiveObject(progid, interface, dynamic)
    172     """
--> 173     clsid = comtypes.GUID.from_progid(progid)
    174     if dynamic:

~\Anaconda3\envs\py36\lib\site-packages\comtypes\GUID.py in from_progid(cls, progid)
     77             inst = cls()
---> 78             _CLSIDFromProgID(str(progid), byref(inst))
     79             return inst

_ctypes/callproc.c in GetResult()

OSError: [WinError -2147221005] Invalid class string

During handling of the above exception, another exception occurred:

OSError                                   Traceback (most recent call last)
<ipython-input-12-10d674ebc1da> in <module>
     28 #Execution Part
     29 if __name__ == '__main__':
---> 30     main()

<ipython-input-12-10d674ebc1da> in main()
     10         state = True
     11     except(OSError,COMError): #If autocad isn't running, open it
---> 12         acad = CreateObject("AutoCAD.Application.20",dynamic=True)
     13         state = False
     14 

~\Anaconda3\envs\py36\lib\site-packages\comtypes\client\__init__.py in CreateObject(progid, clsctx, machine, interface, dynamic, pServerInfo)
    225     You can also later request to receive events with GetEvents().
    226     """
--> 227     clsid = comtypes.GUID.from_progid(progid)
    228     logger.debug("%s -> %s", progid, clsid)
    229     if dynamic:

~\Anaconda3\envs\py36\lib\site-packages\comtypes\GUID.py in from_progid(cls, progid)
     76                 return cls(progid)
     77             inst = cls()
---> 78             _CLSIDFromProgID(str(progid), byref(inst))
     79             return inst
     80         else:

_ctypes/callproc.c in GetResult()

OSError: [WinError -2147221005] Invalid class string
0 Likes

Unable to get AutoCAD Object using Python to import DWG file

I am unable to get the object of AutoCAD in Python. I have to import DWG files using Python and have to render and perform operations on them.

 

In the initial step itself, while checking to get the Active Object if available or creating new AutoCAD object if not available, I am getting below error. I have AutoCAD installed and also opened in my system. Please suggest what I am doing wrong and what this error actually means. Do AutoCAD requires any configurations, environment variables setup or before getting any object do any path importing is required in Python.

 

import os
import comtypes.client
from comtypes import COMError
from comtypes.client import CreateObject, GetActiveObject

def main():
    try: #Get AutoCAD running instance
        acad = GetActiveObject("AutoCAD.Application.20")
        state = True
    except(OSError,COMError): #If autocad isn't running, open it
        acad = CreateObject("AutoCAD.Application.20",dynamic=True)
        state = False
    
    if state: #If you have only 1 opened drawing
        doc = acad.Documents.Items(0)
    else:
        filepath = "old plan pass.dwg" #Replace it with the actual drawing path
        doc = acad.Documents.Open(filepath)

    #Our example command is to draw a line from (0,0) to (5,5)
    command_str = '._line 0,0 5,5 ' #Notice that the last SPACE is equivalent to hiting ENTER
    #You should separate the command's arguments also with SPACE

    #Send the command to the drawing
    doc.SendCommand(command_str)

#Execution Part
if __name__ == '__main__':
    main()

Gettings this Error.

 

---------------------------------------------------------------------------
OSError                                   Traceback (most recent call last)
<ipython-input-12-10d674ebc1da> in main()
      8     try: #Get AutoCAD running instance
----> 9         acad = GetActiveObject("AutoCAD.Application.20")
     10         state = True

~\Anaconda3\envs\py36\lib\site-packages\comtypes\client\__init__.py in GetActiveObject(progid, interface, dynamic)
    172     """
--> 173     clsid = comtypes.GUID.from_progid(progid)
    174     if dynamic:

~\Anaconda3\envs\py36\lib\site-packages\comtypes\GUID.py in from_progid(cls, progid)
     77             inst = cls()
---> 78             _CLSIDFromProgID(str(progid), byref(inst))
     79             return inst

_ctypes/callproc.c in GetResult()

OSError: [WinError -2147221005] Invalid class string

During handling of the above exception, another exception occurred:

OSError                                   Traceback (most recent call last)
<ipython-input-12-10d674ebc1da> in <module>
     28 #Execution Part
     29 if __name__ == '__main__':
---> 30     main()

<ipython-input-12-10d674ebc1da> in main()
     10         state = True
     11     except(OSError,COMError): #If autocad isn't running, open it
---> 12         acad = CreateObject("AutoCAD.Application.20",dynamic=True)
     13         state = False
     14 

~\Anaconda3\envs\py36\lib\site-packages\comtypes\client\__init__.py in CreateObject(progid, clsctx, machine, interface, dynamic, pServerInfo)
    225     You can also later request to receive events with GetEvents().
    226     """
--> 227     clsid = comtypes.GUID.from_progid(progid)
    228     logger.debug("%s -> %s", progid, clsid)
    229     if dynamic:

~\Anaconda3\envs\py36\lib\site-packages\comtypes\GUID.py in from_progid(cls, progid)
     76                 return cls(progid)
     77             inst = cls()
---> 78             _CLSIDFromProgID(str(progid), byref(inst))
     79             return inst
     80         else:

_ctypes/callproc.c in GetResult()

OSError: [WinError -2147221005] Invalid class string
6 REPLIES 6
Message 2 of 7
pendean
in reply to: Anonymous

pendean
Community Legend
Community Legend
you are in the LT forum, not the AUTOCAD forum: likely the reason if you are not familiar with what software you are using.

0 Likes

you are in the LT forum, not the AUTOCAD forum: likely the reason if you are not familiar with what software you are using.

Message 3 of 7
Anonymous
in reply to: pendean

Anonymous
Not applicable
Sorry, I didn't get your answer. I am using the AutoCAD 2020 LT trial version as of now.
0 Likes

Sorry, I didn't get your answer. I am using the AutoCAD 2020 LT trial version as of now.
Message 4 of 7
steven-g
in reply to: Anonymous

steven-g
Mentor
Mentor
Accepted solution

LT doesn't have the option to be accessed by external applications. No Python, VBA, .NET etc. There is no Lisp or any of the other developer tools, those are only available if you are using the full version of Autocad.

0 Likes

LT doesn't have the option to be accessed by external applications. No Python, VBA, .NET etc. There is no Lisp or any of the other developer tools, those are only available if you are using the full version of Autocad.

Message 5 of 7
Anonymous
in reply to: steven-g

Anonymous
Not applicable

Thankyou @steven-g ,

 

After installing the full version, It worked perfectly. Thankyou so much. I was stuck in this from a very long time.

 

 

0 Likes

Thankyou @steven-g ,

 

After installing the full version, It worked perfectly. Thankyou so much. I was stuck in this from a very long time.

 

 

Message 6 of 7
pranav_jadhav_btech2021
in reply to: Anonymous

pranav_jadhav_btech2021
Observer
Observer

I am also facing the same error and I am using Autocad 2023 student version.

 

0 Likes

I am also facing the same error and I am using Autocad 2023 student version.

 

Message 7 of 7

pendean
Community Legend
Community Legend

@pranav_jadhav_btech2021 wrote:

I am also facing the same error and I am using Autocad 2023 student version.

 


you are in the LT forum, not the AUTOCAD forum: if you know for sure you are not using the LT version, then the correct forum to ask your question is here https://forums.autodesk.com/t5/autocad-forum/bd-p/706 

0 Likes


@pranav_jadhav_btech2021 wrote:

I am also facing the same error and I am using Autocad 2023 student version.

 


you are in the LT forum, not the AUTOCAD forum: if you know for sure you are not using the LT version, then the correct forum to ask your question is here https://forums.autodesk.com/t5/autocad-forum/bd-p/706 

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Technology Administrators


Autodesk Design & Make Report