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:UsersMONSTRAppDataLocalProgramsPythonPython36libsite-packagespyautocadapi.py", line 111, in iter_objects
block = self.doc.ActiveLayout.Block
File "C:UsersMONSTRAppDataLocalProgramsPythonPython36libsite-packagespyautocadapi.py", line 74, in doc
return self.app.ActiveDocument
File "C:UsersMONSTRAppDataLocalProgramsPythonPython36libsite-packagespyautocadapi.py", line 67, in app
self._app = comtypes.client.CreateObject('AutoCAD.Application', dynamic=True)
File "C:UsersMONSTRAppDataLocalProgramsPythonPython36libsite-packagescomtypesclient__init__.py", line 227, in CreateObject
clsid = comtypes.GUID.from_progid(progid)
File "C:UsersMONSTRAppDataLocalProgramsPythonPython36libsite-packagescomtypesGUID.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
当我尝试从此处编译此代码
时,我会遇到此错误from pyautocad import Autocad, APoint
acad = Autocad()
acad.prompt("Hello, Autocad from Pythonn")
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)
在Google上几个小时后,我决定在这里想知道
原因是什么?
发生特定错误,因为您的程序无法正确打开自动加速;但是,当我自己打开AutoCAD然后运行您的代码时,它会出现以下错误:
_ctypes.COMError: (-2147467262, 'No compatible interface', (None, None, None, 0, None))
根据我的经验,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