pyautocad给出了不可知的错误

时间:2018-02-16 12:15:57

标签: python python-3.6 autocad

  

OSError:[WinError -2147221005]无效的类字符串

完全追溯

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

当我尝试从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)

在谷歌上几个小时后,我决定在这里想到 可能是什么原因?

1 个答案:

答案 0 :(得分:1)

发生此特定错误是因为您的程序无法正确打开AutoCAD。但是当我自己打开AutoCAD并运行您的代码时,会出现以下错误:

_ctypes.COMError:(-2147467262,'不兼容的接口',(无,无,无,0,无))

根据我的经验,COMError经常由于与所用程序的连接不良而出现。我将win32com与pyautocad结合起来解决了此类问题。对于这种特殊情况,应该是这样的:

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