Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
AutoCAD automation via python and win32com: read all attributes from BlockReferences
import win32com.client
acad = win32com.client.Dispatch("AutoCAD.Application")
doc = acad.ActiveDocument # Document object
# iterate trough all objects (entities) in the currently opened drawing
# and if its a BlockReference, display its attributes and some other things.
for entity in acad.ActiveDocument.ModelSpace:
name = entity.EntityName
if name == 'AcDbBlockReference':
HasAttributes = entity.HasAttributes
if HasAttributes:
print(entity.Name)
print(entity.Layer)
print(entity.ObjectID)
for attrib in entity.GetAttributes():
print(" {}: {}".format(attrib.TagString, attrib.TextString))
# update text
attrib.TextString = 'modified with python'
attrib.Update()
@LiaungYip

This comment has been minimized.

Copy link

@LiaungYip LiaungYip commented Aug 9, 2015

You are a prince. Thank you.

@johnjat

This comment has been minimized.

Copy link

@johnjat johnjat commented Nov 2, 2016

How do i add the win32 module in my python

@AndresRomero2332

This comment has been minimized.

Copy link

@AndresRomero2332 AndresRomero2332 commented Oct 15, 2019

Hi I don't know how to create blocks with atributes, maybe someone here could help me please??

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment