In our previous blog posts, we learned about some basic geometrical objects such as the AutoCAD polyline, AutoCAD circle, AutoCAD arc, etc. Moving forward, we will discuss how to deal with the solid objects from AutoCAD using pyautocad in this blog post.
Initializing AutoCAD drawing and pyautocad script
from pyautocad import Autocad, APoint acad = Autocad(create_if_not_exists=True)
Understanding the basics of solid AutoCAD objects
In AutoCAD, we have different solid objects, e.g. box, cone, cylinder, sphere, etc. and each one of them has object-specific methods to create those individual objects.
But unlike the basic geometrical objects such as lines, polylines, circles, arcs, etc., to find the properties of the objects, we have a common set of methods that shall be used for all sorts of solid objects.
To make this clearer, we will start exploring solid objects.
Writing Python code for solid AutoCAD objects using pyautocad
In this blog post I will talk about some basic solid objects i.e. box, cone, cylinder, elliptical cone, elliptical cylinder, sphere, torus and wedge.
Each individual of the above-mentioned objects has its own methods to create those objects according to their respective geometrical properties.
I will mention the parameters to be passed with each of the commands that we will use for respective objects:
# Box: (Origin/Center, Length, Width, Height) box = acad.model.AddBox(APoint(0, 0, 0), 1000, 1200, 750) # Cone: (Center, Base radius, Height) cone = acad.model.AddCone(APoint(2000, 0, 0), 750, 800) # Cylinder: (Center, Radius, Height) cyl = acad.model.AddCylinder(APoint(3200, 0, 0), 350, 1250) # Elliptical Cone: (Center, MajorRadius, MinorRadius, Height) econe = acad.model.AddEllipticalCone(APoint(4000, 500 , 0), 450, 225, 1275.62) # EllipticalCylinder: (Center, MajorRadius, MinorRadius, Height) ecyl = acad.model.AddEllipticalCylinder(APoint(1500, 2000 , 0), 750, 400, 950) # Sphere: (Center, Radius) sph = acad.model.AddSphere(APoint(2500, 3500, 0), 250) # Torus: (Center, TorusRadius, TubeRadius) tor = acad.model.AddTorus(APoint(1000, 4000, 0), 500, 100) # Wedge: (Center, Length, Width, Height) wed = acad.model.AddWedge(APoint(2000, 5000, 0), 1000, 1200, 750)
Drawing solit AutoCAD objects
Using the code given in point 3 I will draw the objects on the AutoCAD template.
As we can observe from Figures 1.1 and 1.2 the objects have been created.
But if we look closer in the top left corner of the template, we are using 2D wireframe by default, because of which the objects are not appearing like 3D objects.
We will set the visual style from 2D wireframe to the “realistic” style.
As we can see from Figure 1.3, the objects look more like 3D objects & realistic than in Figures 1.1 and 1.2.
5. Properties of solid objects
It is because the solid objects are a collection of polylines resulting in a solid object, the solid object has a similar set of properties that can be fetched using similar methods.
For example, we have methods to find the radius of gyration, the moment of inertia, volume, etc. and each object type possesses these properties.
Let us implement these methods against the box we have created and fetch the properties.
print("Volume of box: " + str(box.Volume)) print("Centroid of box: " + str(box.Centroid)) print("Moment of Inertia of box: " + str(box.MomentOfInertia)) print("Product of inertia of box: " + str(box.ProductOfInertia)) print("Principal directions of box: " + str(box.PrincipalDirections)) print("Principal moments of box: " + str(box.PrincipalMoments)) print("Radius of giration of box: " + str(box.RadiiOfGyration))
O/p: Volume of box: 900000000.0 Centroid of box: (0.0, 0.0, 0.0) Moment of Inertia of box: (150187500000000.0, 117187500000000.0, 183000000000000.0) Product of inertia of box: (-0.0, -0.0, -0.0) Principal directions of box: (1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0) Principal moments of box: (150187500000000.0, 117187500000000.0, 183000000000000.0) Radius of gyration of box: (408.5033659592048, 360.8439182435161, 450.92497528228944)
To make sure this works for complex objects, we will use these commands against Torus.
O/p: Volume of Torus: 98696044.01089358 Centroid of Torus: (1000.0, 4000.0, 0.0) Moment of Inertia of Torus: (1592090559950727.0, 111649899787323.36, 1703246979517996.0) Product of inertia of Torus: (-394784176043574.3, -0.0, -0.0) Principal directions of Torus: (1.0, 0.0, -0.0, -0.0, 1.0, 0.0, 0.0, 0.0, 1.0) Principal moments of Torus: (12953855776429.75, 12953855776429.781, 25414231332805.0) Radius of giration of Torus: (4016.372741666291, 1063.6023693091324, 4154.214727237869)
If we calculate the volume for torus using V=(πr^2)(2πR), we will get the same value as 98721640.0
where, r = Minor radius = 100 & R = Major radius = 500.
Else, if we use an inbuild AutoCAD command “MASSPROP” against torus, we will get the same results. Let’s check the same.
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.