在python中除了COMError之外有困难。下面是我打算在AutoCAD中做一些事情的方法。
def populate_drawing(self):
nMeasurementsFinal = Feature_recognition.determine_OD_dims(Feature_recognition(), "C:\\Users\\buntroh\\Documents\\Rotoworks\\122508.csv")
while True:
try:
for nObject in self.acad.iter_objects(None, None, None, False):
if hasattr(nObject, 'TextString'):
try:
nObject.TextString = nMeasurementsFinal[nObject.TextString]
except KeyError as e:
continue
except COMError:
self.acad.doc.SendCommand("Chr(3)")
break
COMError异常是因为无论何时在运行脚本之前在AutoCAD中选择了某些内容,它都会返回COMError。然而,即使有例外,我仍然得到:
COMError: (-2147418111, 'Call was rejected by callee.', (None, None, None, 0, None))
当没有选择任何内容并且脚本不应该处理COMError异常时,我得到:
NameError: global name 'COMError' is not defined
不确定该怎么做。我导入了 comtypes ,所以我不确定为什么COMError未定义。
答案 0 :(得分:2)
同意以上评论之一,您需要
from comtypes import COMError
然后,您的异常可能是这样的,并假设出现错误: COMError:(-2147418111,'通话被被叫方拒绝。',(无,无,无,0,无))
except COMError as ce:
target_error = ce.args # this is a tuple
if target_error[1] == 'Call was rejected by callee.':
self.acad.doc.SendCommand("Chr(3)")