Message 1 of 7
Not applicable
06-03-2019
03:18 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
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.
