I'm currently working with AutoCAD in Python and recently attempted to establish a connection between Python and AutoCAD. I wrote a simple script to test the connection:
from pyautocad import Autocad
def main():
# Create an instance of AutoCAD
acad = Autocad()
# Check if there's an active document
print(acad.doc.Name)
if __name__ == "__main__":
main()
However, I encountered the following error message:
Traceback (most recent call last):
File "C:\Users\engli\PycharmProjects\pythonProject\Architecture\create a line.py", line 12, in <module>
main()
File "C:\Users\engli\PycharmProjects\pythonProject\Architecture\create a line.py", line 8, in main
print(acad.doc.Name)
^^^^^^^^
File "C:\Users\engli\PycharmProjects\pythonProject\.venv\Lib\site-packages\pyautocad\api.py", line 74, in doc
return self.app.ActiveDocument
^^^^^^^^
File "C:\Users\engli\PycharmProjects\pythonProject\.venv\Lib\site-packages\pyautocad\api.py", line 63, in app
self._app = comtypes.client.GetActiveObject('AutoCAD.Application', dynamic=True)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\engli\PycharmProjects\pythonProject\.venv\Lib\site-packages\comtypes\client\__init__.py", line 178, in GetActiveObject
clsid = GUID.from_progid(progid)
^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\engli\PycharmProjects\pythonProject\.venv\Lib\site-packages\comtypes\GUID.py", line 74, in from_progid
_CLSIDFromProgID(str(progid), byref(inst))
File "_ctypes/callproc.c", line 1008, in GetResult
OSError: [WinError -2147221005] Invalid class string
The error message Invalid class string suggests that the COM object for AutoCAD cannot be found or accessed. I'm unsure how to resolve this issue. Any help would be greatly appreciated.