AutoCAD Attribute object in Python

In my previous article I discussed AutoCAD Block objects and other related objects related to the AutoCAD Block class. In that article I also briefly introduced Block atrributes. Now, in this article, I mainly focus on the AutoCAD Attribute class.

I am using the pyautocad module for this article, but I can also use communication modules such as pythoncom, and win32com.

What is an AutoCAD Attribute object?

The AutoCAD Attribute object is basically a meta-data that describes the characteristics of an AutoCAD object. For example, it can describe the characteristics of AutoCAD Block objects.AttributeReference is the object class containing text that characterizes some AutoCAD block object.

As I have already explained in one of my previous articles covering AutoCAD Block objects the AutoCAD Block is an element of the BlockCollection class. To use AutoCAD Block object I have to create an instance of a specific block in my drawing. The resulting object is a BlockReference instance.

Similarly, an AutoCAD AttributeRefrence object is an instance of the Attribute class.

In daily working activities such as e.g. tagging entities, creating drawing labels and annotations, notations for a particular object, viewport setting adjustments, etc. I will need attributed AutoCAD Block objects.

Therefore, understanding the various AutoCAD Attribute related commands is of great importance.

AutoCAD Attribute object properties

In this section I will discuss some of the important properties of Attribute objects. Besides that, I would like to point out that all the properties that are apply to Attribute objects apply to AttributeReference objects too.

I have used the same example for this practice as I have already used in my previous AutoCAD Block article. I recommend that you check out that blog post for comprehensive understanding of the AutoCAD Block class.

from pickle import TRUE
from pyautocad import Autocad, APoint, aDouble

acad = Autocad(create_if_not_exists=True)

ip = APoint(0, 0, 0)
b1 = acad.doc.Blocks.Add(ip, "Attributed_Block_1")

pl = b1.AddPolyline(aDouble(0, 0, 0, 10000, 0, 0, 10000, 5000, 0, 0, 5000, 0, 0, 0, 0))
l = b1.AddLine(APoint(0, 250, 0), APoint(10000, 250, 0))
l = b1.AddLine(APoint(5000, 250, 0), APoint(5000, 0, 0))

#0, 1, 2, 3, 4, 5, 6 .... 10
a1 = b1.AddAttribute(50, 0, "DATE", aDouble(200, 100, 0), "DATE", "Date: 17/07/2022")
a2 = b1.AddAttribute(50, 0, "DWG", aDouble(5200, 100, 0), "DWG", "Drawing Name: Drawing 1")
a2.MTextAttribute=True

br = acad.model.InsertBlock(APoint(50, 50, 0), "Attributed_Block_1", 1, 1, 1, 0)

print("Does the Block contain any Attributes: ", end="")
print(br.HasAttributes)

Now, I can see that a reference to the AutoCAD Attributed Block has been created in my document.

Below I demonstrate the properties of the Attribute object and the AttributeRefrence object that I created.

#General Properties
print("Attribute alignment: ", end="") 
print(a1.Alignment)
print("Layer of attribute: " + a1.Layer)
print("Is the direction of text backward? " + str(a1.Backward))
print("Is the attribute reference constant ? " + str(a1.Constant))
print("Entity transparency value: ", end="")
print(a1.EntityTransparency)
print("Field length of the attribute: ", end="")
print(a1.FieldLength)
print("Text height: ", end="")
print(a1.Height)
print("Attribute insertion point: ", end="")
print(a1.InsertionPoint)
print("Is attribute reference invisible: " + str(a1.Invisible))
print("Can the attribute or attribute reference be moved relative to geometry in the block ? " + str(a1.LockPosition))
print("Object name: " + a1.ObjectName)
print("Oblique angle of the object: ", end="")
print(a1.ObliqueAngle)

print("Is the attribute preset? " + str(a1.Preset))
# apreset attribute sets the attribute to its default, or preset, value when the user inserts the block.

print("Rotation of object: ", end="")
print(a1.Rotation)
print("Scale factor for the object: ", end="")
print(a1.ScaleFactor)
print("Style name of the attribute object: " + a1.StyleName)
print("Is the attribute set for verification: " + str(a1.Verify))


O/p:
Attribute alignment: 0
Layer of attribute: 0
Is the direction of text backward? False
Is the attribute reference constant ? False
Entity transparency value: ByLayer
Field length of the attribute: 0
Text height: 50.0
Attribute insertion point: (200.0, 100.0, 0.0)
Is attribute reference invisible: False
Can the attribute or attribute reference be moved relative to geometry in the block ? False
Object name: AcDbAttributeDefinition
Oblique angle of the object: 0.0
Is the attribute preset? False
Rotation of object: 0.0
Scale factor for the object: 1.0
Style name of the attribute object: Standard
Is the attribute set for verification: False

Similarly, there are some other properties that define the attribute name, attribute content, and type of text. And so on. See the code and program output below.

# multiline text / text properties
if(a2.MTextAttribute==True):
    print("Attribute content: " + a2.MTextAttributeContent)
    print("Boundary width of multiline text: ", end="")
    print(a2.MTextBoundaryWidth)
    print("Multiline text direction: ", end="")
    print(a2.MTextDrawingDirection)


print("Prompt string of an attribute: " + a1.PromptString)
print("Tag string of the attribute: " + a1.TagString)
print("Text string of the attribute: " + a1.TextString)
print("Alignment point of the text: ", end="")
print(a1.TextAlignmentPoint)
print("Attribute text generation flag: ", end="")
print(a1.TextGenerationFlag)


O/p:

Attribute content: Drawing Name: Drawing 1
Boundary width of multiline text: 0.0
Multiline text direction: 5
Prompt string of an attribute: DATE
Tag string of the attribute: DATE
Text string of the attribute: Date: 17/07/2022
Alignment point of the text: (0.0, 0.0, 0.0)
Attribute text generation flag: 0

As I show in above code there is a property for multiline text that returns the value of text direction. It has five possible return values in the form of integers. Those options are as follows:

  • acBottomToTop: 1
  • acByStyle: 2
  • acLeftToRight: 3
  • acRightToLeft: 4
  • acTopToBottom: 5

Methods of the AutoCAD Attribute class

Some methods of the Attribute and AttributeReference class are similar to other methods of other AutoCAD object classes. I list some of the important methods below:

  • ArrayPolar
  • ArrayRectangular
  • Copy
  • Delete
  • GetBoundingBox
  • IntersectWith
  • Mirror
  • Mirror3D
  • Move
  • Rotate
  • Rotate3D
  • ScaleEntity
  • Update
  • UpdateMTextAttribute

Concluding remarks

I demonstrated the use of the AutoCAD Attribute, AttributeReference, and AttriburtedBlock classes. Obviously, the most important thing is implementing this information in our day-to-day life to automatize repetitive and costly tasks in an effort to increase productivity. Considering the same, feel free to use our contact form to book a session with me for any kind of technical guidance. Besides that, leave any feedback, doubts or questions in the comment section below.

References to related content

I have already established a rather comprehensive documentation on pyautocad, AutoCAD, pythoncom, and pywin32. Please see a list of some related content below:

You May Also Like

Leave a Reply

7 comments

Caesar says:

Hello Sawant,
Can I use Pyautocad to read the Coordinates of the existing polylines in CAD drawing?

Yes you can, please go through our previous articles, that will help you.

Hello, how can I extract the attribute from existing blocks?

thanks

TAIEB says:

Hello,
Thank you for this tutorial,
Please, i have write this code with Win32com block attribute modify, i need him in pyautocad if possible.
Thanks.

import win32com.client
acad = win32com.client.GetActiveObject(“AutoCAD.Application”)
acad.visible=1
dess = acad.ActiveDocument
mo = dess.Utility
Point = mo.GetPoint()

def M_Block():
for entity in acad.ActiveDocument.ModelSpace:
name = entity.EntityName
if name == ‘AcDbBlockReference’:
HasAttributes = entity.HasAttributes
if HasAttributes:

for attrib in entity.GetAttributes():
if attrib.TagString==”W”:
attrib.TextString=str(1.00)+”m”
if attrib.TagString==”H”:
attrib.TextString=str(2.50)+”m”

attrib.Update()

M_Block()

Whats wrong with win32com? I already told you to contact us via contact form for quotation if you have an individual problem of personal interest

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.