In this blog post, I am going to discuss one of the most important methods in pyautocad i.e. Add(). This method opens up doors for creating a number of document-based objects as per AutoCAD’s Object model. For a more comprehensive documentation on pyautocad I refer to my other pyautocad examples. In previous articles I covered the Intersect()-method in pyautocad, as well as AutoCAD raster image objects, 3D mesh objects, object deletion with pyautocad, pyautocad for basic AutoCAD operations etc.
Basic use of Add()-method in pyautocad
The pyautocad Add()-method creates member objects and adds them to our document. In this particular section, I will discuss the very basic syntax that can be used to create objects in this way. This is true for the following objects types/groups:
The syntax to create these objects is very simple:
object.Add(Name) # pyautocad Add()-method
For instance, if we want to create a new Layer, I will use the following syntax:
The same concept works for all of the other object types contained by above object type list.
Applying the pyautocad Add()-method for creating a block
Sometimes we need to work with multiple objects, treating them as a single unit. In such cases, we use blocks.
The pyautocad syntax for creating a block in AutoCAD is as follows.
After creating a block we can save the same in a variable and add different geometries to that using the methods I discussed in my previous blog posts.
b1 = acad.doc.Blocks.Add(ip, "Test_block_1") l1 = b1.AddLine(APoint(100, 100, 0), APoint(350, 350, 0)) c1 = b1.AddCircle(APoint(200, 250, 0), 150)
Now the AutoCAD block is created as a part of the document.
But still, it is not visible in the model space yet. To use the block I must insert the newly created block into the model space. I can do so by using the pyautocad InsertBlock()-method.
The pyautocad syntax for applying the InsertBlock()-method is as shown below:
object.InsertBlock(InsertionPoint, Name , Xscale , Yscale , ZScale , Rotation , Password) e.g. acad.model.InsertBlock(APoint(250, 500, 0), "Test_block_1", 1, 1, 1, 0)
I can see that the block has been successfully inserted into the model space.
For further blog posts covering AutoCAD automatization please check my other blog posts related to pyautocad and pywin32. Please leave any questions that you might have as a comment below. Feel free to contact me for any technical assistance. You can do so by using our contact form.
Civil engineer interested in automation in core subjects such as civil, mechanical and electrical, using IT skills comprising cloud computing, devops, programming languages and databases along with the technical skills gained while working as a civil engineer since past 3 years.