Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Excepting a COMError in Python

Having difficulty excepting a COMError in python. Below is the method I call to do some stuff in 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

The COMError exception is because whenever something is selected in AutoCAD before the script is run, it returns a COMError. However, even with the exception, I still get:

COMError: (-2147418111, 'Call was rejected by callee.', (None, None, None, 0, None))

When nothing is selected and the script shouldn't have to handle the COMError exception, I get:

NameError: global name 'COMError' is not defined

Not sure what to do. I have comtypes imported so I'm not sure why COMError is undefined.

like image 685
RBuntu Avatar asked Feb 26 '26 17:02

RBuntu


1 Answers

Agree with one of the comments above, you need

from comtypes import COMError

then your exception perhaps something like this assuming the error: COMError: (-2147418111, 'Call was rejected by callee.', (None, None, None, 0, None))

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)")
like image 183
IamSierraCharlie Avatar answered Mar 01 '26 06:03

IamSierraCharlie



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!