AutoCAD Block object in Python

In this article I elaborate on Block objects in AutoCAD and how working with them can be automatized using Python.

Although for this practical example I am using the pyautocad library I can also use pythoncom. I have explained how to use communication modules in Python in previous blog posts. More precisely, I am specifically referring to pythoncom and win32com.

Creating AutoCAD Block objects in Python

Firstly, let me explain why AutoCAD Block objects are so significant.

Whenever I want to use certain objects repeatedly, I create a block for those objects. That one block object can contain multiple other objects with e.g. different geometries, attributes, etc.

Once I create a AutoCAD Block object with the pyautocad module in Python, it gets saved in the Document database. I have explained how this Document object works in one of my previous articles.

I can save multiple AutoCAD Block objects in the Document database. Likewise, I can do the same for Blocks collectively in the form of a Blocks Collection object.

Creating instances with InsertBlock()-method in pyautocad

Now that I have created blocks I want to use them in my drawing. This is when I use the InsertBlock method. This method creates an AutoCAD Block object instance for the block that I pass as an input parameter value.

The Block that is present in my drawing by now is known as the BlockReference object.

Let me write the code to explain this considering all the things above.

from pyautocad import Autocad, APoint, aDouble
acad = Autocad(create_if_not_exists=True)


# flow
# Document > BlocksCollection > Block > BlockReference

# insertion point for block
ip = APoint(0, 0, 0)


# adding block to documents block collection
b1 = acad.doc.Blocks.Add(ip, "Test_block_1")

# adding geometries to block
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))


# adding block instance to drawing (creating a reference to the block)
block_ref1 = acad.model.InsertBlock(APoint(50, 50, 0), "Test_block_1", 1, 1, 1, 0)
Block Reference object created using Block from Block Collections

AutoCAD Block and BlockReference object properties

As I have now created both AutoCAD Block and AutoCAD BlockReference objects I will now walk you through the various different properties of these two object types.

Firstly, let me talk about some of the important properties of the AutoCAD Block object. While using blocks I have different requirements. For example, I have to decide whether or not my block has to be explodable, whether it should be dynamic or static, and whether I want to keep it flexible to scale or not, etc.

Below is the code showing how I can use all such properties using the pyautocad module in Python.

# properties

print("Object Name: " + b1.ObjectName)
print("Name of Block: " + b1.Name)
print("Native units of measures for the Block: ", end="")
print(b1.Units)
print("Is scaling allowed for the Block ? ", end="")
print(b1.BlockScaling)
print("Is the Block explodable ? ", end="")
print(b1.Explodable)
print("Is the Block dynamic ? ", end="")
print(b1.IsDynamicBlock)

O/p:

Object Name: AcDbBlockTableRecord
Name of Block: Test_block_1
Native units of measures for the Block: 0
Is scaling allowed for the Block ? 0
Is the Block explodable ? True
Is the Block dynamic ? False

In above code the BlockScaling property is returning a “0”. This represents a type of scaling from the list of types given below:

  • acAny: 0
  • acUniform: 1

On the other hand, I have BlockReference properties. Check the code displayed below.

# properties of BlockReference
print("Object Name: " + block_ref1.ObjectName)
print("Block Name: " + block_ref1.Name)
print("Original Block Name: " + block_ref1.EffectiveName)

print("Entity Transparency: ", end="")
print(block_ref1.EntityTransparency)

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

print("Insertion Point: ", end="")
print(block_ref1.InsertionPoint)

print("Insert units saved with Blocks: ", end="")
print(block_ref1.InsUnits)

print("Conversion factor between Block units and drawing units: ", end="")
print(block_ref1.InsUnitsFactor)

print("Is the Block dynamic ? ", end="")
print(block_ref1.IsDynamicBlock)

print("Layer: " + block_ref1.Layer)

print("Line type: " + block_ref1.Linetype)

print("Line type scale: ")
print(block_ref1.LinetypeScale)

print("Line weight: ")
print(block_ref1.Lineweight)

print("Rotation angle for the block: ")
print(block_ref1.Lineweight)

#Scale factors
print("X Scale factor of block: ", end="")
print(block_ref1.XEffectiveScaleFactor)

print("X Scale factor for block or external reference (xref): ", end="")
print(block_ref1.XScaleFactor)

print("Y Scale factor of block: ", end="")
print(block_ref1.YEffectiveScaleFactor)

print("Y Scale factor for block or external reference (xref): ", end="")
print(block_ref1.YScaleFactor)

print("Z Scale factor of block: ", end="")
print(block_ref1.ZEffectiveScaleFactor)

print("Z Scale factor for block or external reference (xref): ", end="")
print(block_ref1.ZScaleFactor)

O/p:

Object Name: AcDbBlockReference
Block Name: Test_block_1
Original Block Name: Test_block_1
Entity Transparency: ByLayer
Does the Block contain any Attributes: False
Insertion Point: (50.0, 50.0, 0.0)
Insert units saved with Blocks: Unitless
Conversion factor between Block units and drawing units: 1.0
Is the Block dynamic ? False
Layer: 0
Line type: ByLayer
Line type scale:
1.0
Line weight: 
-1
Rotation angle for the block:
-1
X Scale factor of block: 1.0
X Scale factor for block or external reference (xref): 1.0
Y Scale factor of block: 1.0
Y Scale factor for block or external reference (xref): 1.0
Z Scale factor of block: 1.0
Z Scale factor for block or external reference (xref): 1.0

Block (Reference) methods in AutoCAD using Python

This section is just to introduce two of the important methods for Block objects:

  • InsertBlock
  • Delete

As I have already mentioned in the first section I need the BlockCollections object from Document (object/database) in order to be able to insert a Block.

acad.doc.Blocks.Add(Insertion point, Block name)

To delete a AutoCAD Block object I can simply take the respective AutoCAD Block instance and apply the Delete method on the same.

block.Delete()

Coming to the BlockReference object there are a few methods I have to mention:

  • ConvertToAnonymousBlock
  • ConvertToStaticBlock
  • GetConstantAttributes
  • GetDynamicBlockProperties
  • ResetBlock

The ConvertToAnonymousBlock method converts a Dynamic block to Anonymous Block.

block_ref1.ConvertToAnonymousBlock 

On the other hand, the ConvertToStaticBlock method converts a Dynamic block to a named block. This takes a name as a parameter.

block_ref1.ConvertToStaticBlock("static_block1")

To fetch the constant attributes from a Block or external reference I can use the GetConstantAttributes method. This method returns an array of AutoCAD Attribute objects that are constant for the BlockReference object.

block_ref1.GetConstantAttributes

Also, If I want to fetch an array of dynamic properties assigned to a block I can use the following code:

block_ref1.GetDynamicBlockProperties

Lastly, to reset any dynamic block to the default state I can use the ResetBlock method.

block_ref1.ResetBlock

Besides that, the BlockReference object has all those same methods available as I use with any other AutoCAD objects such as Line, Layer, Circle, Raster, etc. Some of those important methods are listed below:

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

Do check out my previous blog posts to understand how to implement those methods.

Example of an AutoCAD Attributed Block

Coming to an end, I would like to share an example of an Attributed Block. An attributed block is the one containing different attributes which further contains metadata of that Block or entities inside the same.

I will explain more about the Attribute objects and the AttributeReference objects separately in the future.

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))

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")

br = acad.model.InsertBlock(APoint(50, 50, 0), "Attributed_Block_1", 1, 1, 1, 0)
AutoCAD Block, enhanced attribute editor

Evidently, as it contains attributes if I use the HasAttribute property method for this Block reference, I will get a True value.

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

O/p:
Does the Block contain any Attributes: True

Concluding remarks

To sum up, I can conclude that using the above methods I can automatize different tasks related to AutoCAD Block and AutoCAD BlockReference objects.

Also, in case of any queries do leave your question in the comment section below. As well as you can contact me using our contact form for any kind of technical consultation. Please check out my other articles for understanding the usage and significance of the pyautocad and pywin32 Python modules.

References to related content

I, together with other SCDA members, have built a series of blog posts documenting AutoCAD, pyautocad and pywin32 for AutoCAD automatization with Python. Here is a list of some related content:

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