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
Solved! Go to Solution.
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
Solved! Go to Solution.
Solved by steven-g. Go to 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.
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.
Thankyou @steven-g ,
After installing the full version, It worked perfectly. Thankyou so much. I was stuck in this from a very long time.
Thankyou @steven-g ,
After installing the full version, It worked perfectly. Thankyou so much. I was stuck in this from a very long time.
I am also facing the same error and I am using Autocad 2023 student version.
I am also facing the same error and I am using Autocad 2023 student version.
@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
@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.