Search code examples
pythonpython-3.6autocad

pyautocad gives ungooglable error


OSError: [WinError -2147221005] Invalid class string

full traceback

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:/Users/MONSTR/Desktop/Ванжые/Yusuf bey/GUI/test1.py", line 10, in <module>
    for text in acad.iter_objects('Text'):
  File "C:\Users\MONSTR\AppData\Local\Programs\Python\Python36\lib\site-packages\pyautocad\api.py", line 111, in iter_objects
    block = self.doc.ActiveLayout.Block
  File "C:\Users\MONSTR\AppData\Local\Programs\Python\Python36\lib\site-packages\pyautocad\api.py", line 74, in doc
    return self.app.ActiveDocument
  File "C:\Users\MONSTR\AppData\Local\Programs\Python\Python36\lib\site-packages\pyautocad\api.py", line 67, in app
    self._app = comtypes.client.CreateObject('AutoCAD.Application', dynamic=True)
  File "C:\Users\MONSTR\AppData\Local\Programs\Python\Python36\lib\site-packages\comtypes\client\__init__.py", line 227, in CreateObject
    clsid = comtypes.GUID.from_progid(progid)
  File "C:\Users\MONSTR\AppData\Local\Programs\Python\Python36\lib\site-packages\comtypes\GUID.py", line 78, in from_progid
    _CLSIDFromProgID(str(progid), byref(inst))
  File "_ctypes/callproc.c", line 918, in GetResult
OSError: [WinError -2147221005] Invalid class string

I get this error when I am trying to compile this code from here

from pyautocad import Autocad, APoint

acad = Autocad()
acad.prompt("Hello, Autocad from Python\n")
print(acad.doc.Name)

p1 = APoint(0, 0)
p2 = APoint(50, 25)
for i in range(5):
    text = acad.model.AddText('Hi %s!' % i, p1, 2.5)
    acad.model.AddLine(p1, p2)
    acad.model.AddCircle(p1, 10)
    p1.y += 10

dp = APoint(10, 0)
for text in acad.iter_objects('Text'):
    print('text: %s at: %s' % (text.TextString, text.InsertionPoint))
    text.InsertionPoint = APoint(text.InsertionPoint) + dp

for obj in acad.iter_objects(['Circle', 'Line']):
    print(obj.ObjectName)

After few hours on google, I decided to wonder here
What can be the reason ?


Solution

  • That specific error occurs because your program can't open AutoCAD properly; but when I open AutoCAD by myself and then run your code, it appears the following error:

    _ctypes.COMError: (-2147467262, 'No compatible interface', (None, None, None, 0, None))
    

    In my experience, COMError often appears due to a poor connection with the used program. I solve that kind of problem by combining win32com with pyautocad. For this particular case, it would be something like this:

    from pyautocad import Autocad, APoint
    import win32com.client
    
    AutoCAD = win32com.client.Dispatch("AutoCAD.Application")
    acad = Autocad(create_if_not_exists = False)
    
    p1 = APoint(0, 0)
    p2 = APoint(50, 25)
    
    for i in range(5):
        text = acad.model.AddText('Hi %s!' % i, p1, 2.5)
        acad.model.AddLine(p1, p2)
        acad.model.AddCircle(p1, 10)
        p1.y += 10
    
    dp = APoint(10, 0)
    for text in acad.iter_objects(['Hi']):
        print('text: %s at: %s' % (text.TextString, text.InsertionPoint))
        text.InsertionPoint = APoint(text.InsertionPoint) + dp
    
    for line in acad.iter_objects(dont_cast = True):
        print(line.ObjectName)
    
    AutoCAD.Visible = True