Solid objects in AutoCAD using pyautocad (Python)

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.

1. Setting up environment

As usual, we will set up our environment to integrate python with AutoCAD by importing the pyautocad library.

from pyautocad import Autocad, APoint


acad = Autocad(create_if_not_exists=True)

2. About solid 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.

3. Writing code for solid objects using pyautocad

In this blog, we will talk about some basic solid objects i.e. box, cone, cylinder, elliptical cone, elliptical cylinder, sphere, torus & 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)  

4. Drawing the solid objects

Using the code given in point 3, we will draw the objects on the AutoCAD template.

Figure 1.2: Solid objects from the top view (2D Wireframe)
Figure 1.2: Solid objects from the top right corner (2D Wireframe)

As we can observe from figures 1.1 & 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.

Figure 1.3: Solid objects from the top right corner (Realistic)

As we can see from figure 1.3, the objects look more like 3D objects & realistic than in figures 1.1 & 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.

Figure 2.1: Properties of the torus
Figure 2.2: Properties of the torus

For more information on AutoCAD itself, you can also review the Autodesk documentation.

Leave a Reply

2 thoughts on “Solid objects in AutoCAD using pyautocad (Python)

  1. Mr. Sawant,

    Thank you for this blog post and the previous one about 2D objects. Can you tell me more bout the environment you are using? Python 2 or 3? What version of pyautocad? I only ask because I’m getting errors when trying to use the Copy() method and Move() method on circle and line objects.

Leave a Reply

Your email address will not be published. Required fields are marked *

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

Close

Meta