I am trying to use pyautocad to perform simple tasks such as drawing and retrieving properties of drawn objects. I am able to draw, but simple code I found in online tutorials seems to fail when it comes to retrieving object properties such as text contents, insertion points etc. I understood that pyautocad is a pretty old package and I was wondering if the issues I am facing are due to Autocad/Python versions or just errors in my code.
I read the docs for pyautocad and searched for an answer, but couldn't solve my problem and I am hoping someone here can help.
Note that I can't (would not like to) use neither IronPython nor comtypes to send commands directly to Autocad and I am looking for a solution using pyautocad.
My end goal is to be able to prompt the user to select various circles, lines and polylines in an open drawing and then retrieve the coordinates of these polylines. The problem is that I am stuck in the super simple script below, which throws an error, even though it seems to work on several youtube videos and blog posts online:
from pyautocad import Autocad, APoint
acad = Autocad(create_if_not_exists=True)
acad.prompt("Hello from Python\n")
print(acad.doc.Name)
for obj in acad.iter_objects(['Circle', 'Line']):
print(obj.ObjectName)
This is the error message:
Traceback (most recent call last):
File "C:\tests.py", line 21, in <module>
for obj in acad.iter_objects(['Circle', 'Line']):
File "C:\Users\usr\anaconda3\lib\site-packages\pyautocad\api.py", line 128, in iter_objects
item = self.best_interface(item)
File "C:\Users\usr\anaconda3\lib\site-packages\pyautocad\api.py", line 156, in best_interface
return comtypes.client.GetBestInterface(obj)
File "C:\Users\usr\anaconda3\lib\site-packages\comtypes\client\__init__.py", line 112, in GetBestInterface
interface = getattr(mod, itf_name)
AttributeError: module 'comtypes.gen.AXDBLib' has no attribute 'IAcadCircle'
I would appreciate your help!
Solved! Go to Solution.
Solved by pendean. Go to Solution.
Hi, b.isufi
try this, change the "dont_cast" parameter to "True", (defect value "False) in the "iter_objects()" method.
from pyautocad import Autocad, APoint
acad = Autocad(create_if_not_exists=True)
acad.prompt("Hello from Python\n")
print(acad.doc.Name)
for obj in acad.iter_objects(['Circle', 'Line'], dont_cast=True):
print(obj.ObjectName)
If this doesn't work, try with the "iter_objects_fast()".
If you are interested in pyautocad, I suggest you to check my channel on YT: My channel.
Cheers.
I faced a similar error, but dont_cast=True fixed it (at least for me) .
@kevinaxeltagliaferri : Thanks for the great help, that seems to be the solution!
Can't find what you're looking for? Ask the community or share your knowledge.