DimAligned object in AutoCAD using Python

In this article I introduce DimAligned object in AutoCAD with Python. In general, adding dimensions manually is a very tedious task that I can avoid if I use Python automatization instead.

For this tutorial I am using the pyautocad module in Python. I can also use the pywin32 and win32com modules in Python for this. These modules provide another way performing AutoCAD automatization in Python. Do check out my previous blog posts for more information about these modules.

Adding DimAligned object to AutoCAD drawings

As the name of the article suggests I focus on dimensions for linear entities. I can use the DimAligned object for this.

I can add dimensions with the AddDimAligned method. The first thing to remember is that a DimAligned object requires 3 parameters:

  • ExtLine1Point (first endpoint)
  • ExtLine2Point (second endpoint)
  • TextPosition (text for dimensions)

In this case, I have two geometries present in my model space. The left one is made up of lines and the right one is a polyline. You can see this in below screenshot.

In light of the fact that I can fetch the start and end points of a AutoCAD Line objects, I can iterate over the AcDbLine object, passing those points as start and end points for the Dimensions.

I am using the midpoint formula, with a factor of 100 for the offset, for the text position. See code below.

for l in acad.iter_objects_fast(object_name_or_list="AcDbLine"):
    acad.model.AddDimAligned(APoint(l.StartPoint), APoint(l.EndPoint), APoint((l.StartPoint[0]+l.EndPoint[0]+100)/2, (l.StartPoint[1]+l.EndPoint[1]+100)/2, 0))

On the other hand, the polyline object returns an array of doubles for the coordinates of each of its nodes. In this case, I can iterate over that array and pass those points as start and end points in each iteration.

for i in acad.iter_objects_fast(object_name_or_list="AcDbPolyline"):
    nodes = len(i.Coordinates)/2
    j=0
    while j <= nodes+1:
        acad.model.AddDimAligned(APoint(i.Coordinates[j], i.Coordinates[j+1], 0),    APoint(i.Coordinates[j+2], i.Coordinates[j+3], 0),    APoint((i.Coordinates[j]+i.Coordinates[j+2]+100)/2,     (i.Coordinates[j+1]+i.Coordinates[j+3]+100)/2, 0))
             j+=2

Of course, this is a very basic approach to adding dimensions to any linear geometry. Nevertheless, let me now implement this and see the results.

Evidently, as you can see in above screenshot, I can say that I have added the dimensions to the linear geometries.

Properties of the DimAligned object in AutoCAD

Now that I have added the dimensions to the figures I want to introduce some of its important properties. I cannot explain each of them in this article itself since there are around 115 properties altogether, many of them working for different dimension aspects.

But firstly, I show some basic properties in the Python code below.

# general properties
print(l.ObjectName)

print(l.Rotation) # rotation angle of object in radians

print(l.LinearScaleFactor) 
# specfies global scale factor for linear dimensioning  measurements

print(l.StyleName)

l.DecimalSeparator = "."
print(l.DecimalSeparator)


O/p:

AcDbAlignedDimension
0.0
1.0
ISO-25
.

Because there are multiple dimension lines present inside my drawing I am selecting one of them to apply these properties. As well as there are ArrowHead properties with which I can set the arrowhead size by passing an integer value.

# arrow head properties
l.ArrowheadSize = 15
print(l.ArrowheadSize)
print(l.Arrowhead1Type)
print(l.Arrowhead2Type)

O/p:
15.0
0
0

As I have shown in the code above, I can get and set the Arrowhead size and type. For the Arrowhead type, there are multiple options. The default is acArrowDefault (0). The other types are listed below:

  • acArrowDefault
  • acArrowDot
  • acArrowDotSmall
  • acArrowDotBlank
  • acArrowOrigin
  • acArrowOrigin2
  • acArrowOpen
  • acArrowOpen90
  • acArrowOpen30
  • acArrowClosed
  • acArrowSmall
  • acArrowNone
  • acArrowOblique
  • acArrowBoxFilled
  • acArrowBoxBlank
  • acArrowClosedBlank
  • acArrowDatumFilled
  • acArrowDatumBlank
  • acArrowIntegral
  • acArrowArchTick
  • acArrowUserDefined Read-only

Similarly, I have some dimension line properties as given below.

l.DimensionLineColor = 200
print("Dimension line color on the basis of 0-256 color index: " + str(l.DimensionLineColor))
    
print("The dimension line extends beyond the extension line when oblique strokes are drawn instead of arrowheads: " + str(l.DimensionLineExtend))

print("The dimension line type is: " + l.DimensionLineType)l.DimensionLineWeight = 100    

print("Dimension lineweight: " + str(l.DimensionLineWeight))
    
print("Dimension text direction: ", end="" )
    
print(l.DimTxtDirection) 
# False: Left to right reading style
# True: Right to Left reading style 

print(l.Fit)

O/p:


Dimension line color on the basis of 0-256 color index: 200
The dimension line extends beyond the extension line when oblique strokes are drawn instead of arrowheads: 0.0
The dimension line type is: ByBlock
Dimension lineweight: 100
Dimension text direction: False
3

The Fit property has 4 possible values as listed below:

  • acTextAndArrows(0): To place both the text and arrowheads inside the extension lines.
  • acArrowsOnly(1): To place only the arrowheads inside the extension lines. The text is placed outside the arrowheads.
  • acTextOnly(2): To place only the text inside the extension lines. The arrowheads are placed outside the extension lines.
  • acBestFit(3): This is the default option that places the text and arrowheads in the best fit location given the space available. When enough space is available for text and arrowheads, this option places both between the extension lines.

There are a few other properties such as text properties, and tolerance properties. Let me add a few from each of those properties to explain.

#Text properties
l.TextHeight = 20
print(l.TextHeight)
print(l.TextInside) 

#Tolerance properties
print(l.ToleranceDisplay)
print(l.TolerancePrecision)

O/p:
20.0
False
0
2

Here, the TextInside property has a boolean value to specify if the text is to be added to the extension line forcefully (true) or only if there is space (false) accordingly.

In order to specify tolerances in the dimension text I have the ToleranceDisplay property which also returns a boolean. ToleranceDisplay returns False by default, i.e. if tolerances have not been specified.

In general, the tolerance precision has 4 possible values:

  • acTolNone: 0 (Default)
  • acTolSymmetrical: 1
  • acTolDeviation: 2
  • acTolLimits: 3
  • acTilBasic: 4

Indeed, as I also already mentioned myself there are many more properties than I have specified above, but these are a few to show how to use them. Below is a list of the methods of the DimAligned AutoCAD object class.

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

Concluding remarks

Finally, I can say that I have covered the DimAligned object from AutoCAD and the way I can automate the same with Python. In the same way, there are other dimension objects, which I will cover in my future blog posts. In case of some more technical information or consultation, feel free to contact me using our contact form. Also, drop a comment in our comment box for any doubts.

References to related content

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

Leave a Reply

Leave a Reply

Your email address will not be published.

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

Close

Meta