 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

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)

#Wedge: (Center, Length, Width, Height)

## 4. Drawing the solid objects

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

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.

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

1. Patrick J. Phillips says:

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.

1. Tanmay Sawant says:

Hello Mr. Phillips,

Thanks a lot for your question. The python version I am using is 3.9.7, and the pyautocad version is 0.2.0. Talking about copy and move commands, we have posted a blog regarding the same. This is the link : https://www.supplychaindataanalytics.com/operations-with-autocad-objects-using-pyautocad-python/

Please check this out. Have a great day!
Thank you

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