3

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.

RBuntu
  • 907
  • 10
  • 22
  • You have to import the COMError name by itself or use it namespaced. Try either `from comtypes import COMError` or `except comtypes.COMError` (not sure where it comes from). – Two-Bit Alchemist Aug 22 '16 at 17:00
  • I tried all 3 combinations of those and all of them give me the same COMError. Not sure why the script isn't recognizing it – RBuntu Aug 22 '16 at 18:15
  • Even when I put a blank exception to "except" everything, it still gives me the COMError – RBuntu Aug 22 '16 at 18:24
  • As I said in the other post, usually wait few seconds before begining manipulation, I found that there is a method to check acad availability but found it too complexe, so I choosed the "wait function" solution – ubugnu Aug 23 '16 at 17:30
  • For me, the issue isn't the startup, since the software should already be open when the script is run. The issue for me is when there is a pending command (for example a move or resize command awaiting coordinates), I get the COMError. I can always notify the user to hit "esc" in the AutoCAD window before running the script if necessary, but I would rather try and have my python script do that so that the script is as automated as possible. – RBuntu Aug 23 '16 at 19:12

1 Answers1

6

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)")
IamSierraCharlie
  • 504
  • 5
  • 10
  • `ce.args[1]` is the `ce.text` property. You can reference it like `if ce.text == 'Call ...'` or unpack `ce.args` as `hresult, text, details = ce.args` and then `if text == 'Call ...'`. – Nuno André Aug 08 '19 at 22:54