Too many COMError, during the execution(ActiveX)

Too many COMError, during the execution(ActiveX)

Beka_Tchigladze
Advisor Advisor
1,957 Views
8 Replies
Message 1 of 9

Too many COMError, during the execution(ActiveX)

Beka_Tchigladze
Advisor
Advisor

Hello, I wrote some routine in python to modify the leader content

everything is ok, script works fine but python raises too many COMError's at the end of the day I except this errors and jut neglect it because actually code works but, what causes this errors so frequently?

I have to click several times to get a result when it raises this Error

 

this is an error content I just wrap it with exception during the screencast

Traceback (most recent call last):
File "C:\Users\ASUS\Desktop\test.py", line 14, in <module>
leader = acad.get_selection()
File "C:\Users\ASUS\AppData\Local\Programs\Python\Python310\lib\site-packages\pyautocad\api.py", line 175, in get_selection
selection = self.doc.SelectionSets.Add('SS1')
File "C:\Users\ASUS\AppData\Local\Programs\Python\Python310\lib\site-packages\pyautocad\api.py", line 74, in doc
return self.app.ActiveDocument
File "C:\Users\ASUS\AppData\Local\Programs\Python\Python310\lib\site-packages\comtypes\client\lazybind.py", line 168, in __getattr__
return self._comobj._invoke(descr.memid, descr.invkind, 0)
File "C:\Users\ASUS\AppData\Local\Programs\Python\Python310\lib\site-packages\comtypes\automation.py", line 745, in _invoke
self.__com_Invoke(memid, riid_null, lcid, invkind,
_ctypes.COMError: (-2147418111, 'Call was rejected by callee.', (None, None, None, 0, None))
[Finished in 639ms]

orutine.gif

 

 

from pyautocad import Autocad, APoint
import re, sys, keyboard
from comtypes import COMError

acad = Autocad()
counter = 144
tempNum = 144
letters = ['"A"','"B"','"C"']

while True:
	try:
		if keyboard.is_pressed('esc'):
			sys.exit()
		leader = acad.get_selection()
		for i in leader:
			text = re.search(r'(.*)(5.})(\d+)(.*)(([a-z]\.)("[A-Z]"\Z))',i.textString)
			phase = letters[(counter-1)%3]
			newString = text.group(1) + text.group(2) + str(counter) + text.group(4) + text.group(6) + phase
			i.textString = newString
			tempNum += 1 
	except COMError:
		counter = tempNum
		continue
	except AttributeError:
		counter = tempNum
		continue
	counter += 1
	print(counter)

 

 

Beka Tchigladze
Road/Highway Technical Designer

AEC Collection/C3D 2025
WIN 11 64 PRO
Predator Helios 18, Intel Core i9 14900HX, 32 GB
NVIDIA RTX 4080, 12 GB
YouTube--->Linkedin
0 Likes
Accepted solutions (1)
1,958 Views
8 Replies
Replies (8)
Message 2 of 9

Beka_Tchigladze
Advisor
Advisor

Hello, Could someone move this post to HERE?

Beka Tchigladze
Road/Highway Technical Designer

AEC Collection/C3D 2025
WIN 11 64 PRO
Predator Helios 18, Intel Core i9 14900HX, 32 GB
NVIDIA RTX 4080, 12 GB
YouTube--->Linkedin
0 Likes
Message 3 of 9

Ed__Jobe
Mentor
Mentor

I have never used python, but my guess is that it has to do with:

 

 

selection = self.doc.SelectionSets.Add('SS1')

 

 

SelectionSets are stored in a collection. If you try to add one with a name that already exists, you will get an error. You should check if the name exists before trying to add it. From the code you are posting, it looks like your call to the get_Selection method always tries to use SS1 as the name. It's handling the error, but not preventing it. The following sub is an example of handling the error:

 

 

Public Function AddSelectionSet(SetName As String) As AcadSelectionSet
' This routine does the error trapping neccessary for when you want to create a
' selectin set. It takes the proposed name and either adds it to the selectionsets
' collection or sets it.
    On Error Resume Next
    Set AddSelectionSet = ThisDrawing.SelectionSets.Add(SetName)
    If Err.Number <> 0 Then
        Set AddSelectionSet = ThisDrawing.SelectionSets.Item(SetName)
        AddSelectionSet.Clear
    End If
End Function

 

 

Ed


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
How to post your code.

EESignature

0 Likes
Message 4 of 9

Beka_Tchigladze
Advisor
Advisor

before I will try that method you proposed, very frequently I get this error same time when i execute, even before I make any click in autocad.

Beka Tchigladze
Road/Highway Technical Designer

AEC Collection/C3D 2025
WIN 11 64 PRO
Predator Helios 18, Intel Core i9 14900HX, 32 GB
NVIDIA RTX 4080, 12 GB
YouTube--->Linkedin
0 Likes
Message 5 of 9

Ed__Jobe
Mentor
Mentor

I don't know what kind of development environment you are using. Can you set a breakpoint and see what line gives the error?

Ed


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
How to post your code.

EESignature

0 Likes
Message 6 of 9

Beka_Tchigladze
Advisor
Advisor

I am already tired to struggling with all this stupid module, it actually works but it stacks at some point I don't know why I also tried about selections according to you advice, but it is the same, works for some times but raise different errors later. I put break points but it is different problem sometimes it says that there is not Leader.textString method sometimes it stacks with selection, but the process is same I am very carefull with selecting objects.

I canceled stupid pyautocad module and tried out with win32com.client directly but the problem is same.

import win32com.client
import pythoncom
import re, sys, keyboard


acad = win32com.client.Dispatch("AutoCAD.Application")
doc = acad.ActiveDocument
mp = doc.ModelSpace

def APoint(x, y, z=0):
    """Coordinate points are converted to floating point numbers""" 
    return win32com.client.VARIANT(pythoncom.VT_ARRAY | pythoncom.VT_R8, (x, y, z))

def vtobj(obj):
    """ is converted to an object array """ 
    return win32com.client.VARIANT(pythoncom.VT_ARRAY | pythoncom.VT_DISPATCH, obj)

def vtFloat(list):
    """The list is converted to a floating point number""" 
    return win32com.client.VARIANT(pythoncom.VT_ARRAY | pythoncom.VT_R8, list)
    
def vtInt(list):
    """list is converted to integer """ 
    return win32com.client.VARIANT(pythoncom.VT_ARRAY | pythoncom.VT_I2, list)

def vtVariant(list):
    """The list is converted to a variant """ 
    return win32com.client.VARIANT(pythoncom.VT_ARRAY | pythoncom.VT_VARIANT, list)

counter = 35
tempNum = 35
letters = ['"A"','"B"','"C"']


while True:
	if keyboard.is_pressed('esc'):
		sys.exit()

	try:
		doc.SelectionSets.Item("SS1").Delete()
	except:
		print("Delete selection failed")
	
	stl = doc.SelectionSets.Add("SS1")
	stl.SelectOnScreen()
	leader = stl[0]
	

	text = re.search(r'(.*)(\d.})(\d+)(.*)(([a-z]\.)("[A-Z]"\Z))',leader.textString)
	phase = letters[(counter-1)%3]
	newString = text.group(1) + text.group(2) + str(counter) + text.group(4) + text.group(6) + phase
	leader.textString = newString
	counter+= 1 

 

 

 

Beka Tchigladze
Road/Highway Technical Designer

AEC Collection/C3D 2025
WIN 11 64 PRO
Predator Helios 18, Intel Core i9 14900HX, 32 GB
NVIDIA RTX 4080, 12 GB
YouTube--->Linkedin
0 Likes
Message 7 of 9

Ed__Jobe
Mentor
Mentor

Is there a reason you're using python and not vba or .NET?

Ed


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
How to post your code.

EESignature

Message 8 of 9

Beka_Tchigladze
Advisor
Advisor

@Ed__Jobe 

Unfortunatly I am very begginer in programming and I just know some python but it has many limitations it is very useful in some tasks like file managment and etc But not that good. I will move on .NET in a few month. Thank you for you support in any case

Beka Tchigladze
Road/Highway Technical Designer

AEC Collection/C3D 2025
WIN 11 64 PRO
Predator Helios 18, Intel Core i9 14900HX, 32 GB
NVIDIA RTX 4080, 12 GB
YouTube--->Linkedin
0 Likes
Message 9 of 9

Ed__Jobe
Mentor
Mentor
Accepted solution

I'm not sure what logic you are using to change the text, but here is an example of how to select and edit the TextString of a mleader.

Sub EL()
    Dim L As AcadMLeader
    Dim ent As AcadEntity
    Dim pp As Variant
    
    On Error GoTo Err_Control
    
    ThisDrawing.Utility.GetEntity ent, pp, "Select a MLeader: "
    While Not IsEmpty(ent)
        If TypeOf ent Is AcadMLeader Then
            Set L = ent
            'You can use regex here to edit the TextString property
            ThisDrawing.Utility.Prompt L.TextString & vbCrLf & "Command: "
            ThisDrawing.Utility.GetEntity ent, pp, "Select a MLeader: "
        End If
    Wend
    
    
Exit_Here:
    Exit Sub
Err_Control:
    Select Case Err.Number
    Case Is = -2147352567
        ' Nothing was selected.
        Err.Clear
        Resume Exit_Here
    Case Else
        MsgBox Err.Number & ", " & Err.Description, , "EditLeader"
        Err.Clear
        Resume Exit_Here
    End Select
    
End Sub

Ed


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
How to post your code.

EESignature