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:
- Dictionaries
- DimStyles
- Documents
- Groups
- Layers
- Layouts
- Linetypes
- Materials
- PopupMenus
- RegisteredApplications
- SelectionSets
- TextStyles
- Toolbars
- Views
- Viewports
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:
acad.doc.Layers.Add(layer_name)
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.
object.Add(Insertion_Point, Block_Name)
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.
Concluding remarks
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.
10 comments
is there a acad.model.AddLeader() method in pyautocad module?
i am working on a drawing which needs a leader line using pyautocad module .
Hi Kiran. I have not tested it yet, maybe Tanmay has. Let us await his answer here. Most of the documentation that we provide on this blog is based on trial and error, especially if it is related to pyautocad. But we can also use other modules, not just pyautocad.
Hi kiran,
Thanks for your question.
I have not tried this yet, and since I am out of station and not carrying system, I’ll share some documentao with you, so that you can give it a try.
object.AddLeader(PointsArray, Annotation, Type)
object
Type: Block, ModelSpace, PaperSpace
The objects this method applies to.
PointsArray
Access: Input-only
Type: Variant (array of doubles)
The array of 3D WCS coordinates specifying the leader. You must provide at least two points to define the leader. The third point is optional.
Annotation
Access: Input-only
Type: BlockReference, MText, Tolerance
The object that should be attached to the leader. The value can also be NULL to not attach an object.
Type
Access: Input-only
Type: AcLeaderType enum
acLineNoArrow
acLineWithArrow
acSplineNoArrow
acSplineWithArrow
Have a great day!
Hi Tanmay, I’ve been trying to make this AddLeader method work, but I’m still struggling. Haven’t you tested it yet? It would really help me a lot.
See example below:
import pyautocad
acad = pyautocad.Autocad()
start_point = pyautocad.APoint(0, 0, 0)
vertices = [pyautocad.APoint(2, 2, 0), pyautocad.APoint(4, 0, 0), pyautocad.APoint(6, 2, 0)]
leader = acad.model.AddLeader(start_point, vertices)
Hi Gabriel, Do you find a way to draw leaders in AutoCAD by using python?
Hi, I am pretty new to pyautocad module. I was wondering why i cannot find the “Add” method in the pyautocad official documentation.
https://pyautocad.readthedocs.io/en/latest/index.html
Thanks for your post it will help me in my task. I want to automate block insertion task in ACADE
regards
You don`t write Add(), instead you use add method specific to object you want to add, like AddCircle, AddLine
Hi Linnart,
Me, like Gabriel above, also have some problems with the AddLeader method. The example doesn’t work for me and the documentation Tanmay sent is in VBA I think. Would you mind helping out with the kind of arguments that are expected from pyautcad for this method? I’d really appreciate it.
Hello Linnart,
I am now trying to find the method to instruct python to create a revision cloud, but this topic is still to be discussed as there is no solution in internet.
May i know is there any comment to deal with it?
Thank you so much.