Working with helices in AutoCAD using pyautocad (Python)

In our previous blogs posts we learned about different AutoCAD objects such as lines, polylines, arcs, ellipses, etc. In continuation with the same, we are now going to talk about helices. It has certain limitations as compared to other objects. Let’s discuss those in detail.

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. Creating a helix

Unlike other AutoCAD objects such as AutoCAD lines, AutoCAD polylines, AutoCAD arcs, AutoCAD ellipses, etc. we do not have any method provided by pyautocad to draw a helix.

So, we can only fetch properties of the pre-created helix.

Let’s draw a helix first.

Figure 1.1: Helix & its properties (2D view)
Figure 1.2: Helix & its properties (3D view)

As you can see from Figures 1.1 & 1.2, I have created a helix with the below-mentioned properties:

Top Radius: 250 units

Bottom Radius: 200 units

Center: (2000, 1500)

Height: 100 units

3. Storing the helix object in a variable

As we cannot create a helix using pyautocad, we do not have a variable assigned to the helix which has been created on the AutoCAD template.

In this case, we can utilize a wonderful method provided by pyautocad, i.e. iter_objects.

With this, we can iterate through all the previously created objects from the AutoCAD template.

As per pyautocad, the type of a helix object is “AcDbHelix”, which can be found out using the “ObjectName” property against the created drawing.

So, through a combination of this iter_objects method and filtering out the helix with its “ObjectName” (which is “AcDbHelix”) we can store it in a variable while implementing “for loop”, eventually using it to fetch the properties of the helix.

Let us implement the loop now, to filter out helix from the template:

def helix():
    obj = acad.iter_objects(limit=None, block=acad.doc.Layouts.item(2).Block)
    for obj in obj:
        print("Type of object: " + obj.ObjectName)
        if obj.ObjectName == "AcDbHelix":
...

Syntax for “iter_objects”:

iter_objects(object_name_or_list=None, block=None, limit=None, dont_cast=False)

Parameters:

object_name_or_list – part of object type name, or list of it

block – Autocad template, default – ActiveDocument.ActiveLayout.Block

limit – max number of objects to return, default infinite

To make things work a little faster by iterating only through the “AcDbHelix” object, we can pass the parameter “object_name_or_list” as:

def helix():
    obj = acad.iter_objects(object_name_or_list= "AcDbHelix", limit=None, block=acad.doc.Layouts.item(2).Block)
    for obj in obj:
...

This will store the helix object in the variable obj and we can fetch properties of the helix by applying different methods against that variable.

4. Helix properties

Let’s fetch properties of our helix now:

print("Top radius of helix: " + str(round(obj.TopRadius,2)))
print("Base radius of helix: " + str(round(obj.BaseRadius,2)))
print("Helix constrain: " + str(round(obj.constrain,2)))
print("Height of helix: " + str(round(obj.Height,2)))
print("Center point of helix: ")
print(obj.Position)
print("Total length of helix: " + str(round(obj.TotalLength,2)))
print("Number fo turns helix took to complete: " + str(round(obj.Turns,2)))
print("Slope of turns: " + str(round(obj.TurnSlope,2)))
print("Height of single turn: " + str(round(obj.TurnHeight,2)))
print("Twist of the helix: " +  str(round(obj.Twist,2)))
O/p:

Type of object: AcDbHelix
Top radius of helix: 250.0
Base radius of helix: 200.0
Helix constrain: 1
Height of helix: 100.0
Center point of helix:
(2000.0, 1500.0, 0.0)
Total length of helix: 4242.63
Number fo turns helix took to complete: 3.0
Slope of turns: 0.46
Height of single turn: 33.33
Twist of the helix: 0

5. Editing the newly created helix object

Although we cannot create a helix using pyautocad, we can definitely edit the previously created helix.

Let’s change the center point of the helix.

We can use the Move method to move objects from one point to another.

Syntax:

object.Move(<current location>, <new location>)
obj.Move(APoint(obj.Position), APoint(1500, 1000))

Now, we will change some other properties too and, let’s see what happens.

obj.TopRadius = 450
obj.BaseRadius = 700
obj.Height = 500
obj.Twist = 1

Let’s run the code now and print the properties again.

Figure 2.1: Helix & its properties, after editing (2D view)
Figure 2.2: Helix & its properties, after editing (3D view)
O/p:

Top radius of helix: 450.0
Base radius of helix: 700.0
Helix constrain: 1
Height of helix: 500.0
Center point of helix:
(1500.0, 1000.0, 0.0)
Total length of helix: 10853.13
Number fo turns helix took to complete: 3.0
Slope of turns: -0.46
Height of single turn: 166.67
Twist of the helix: 1

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

Leave a Reply

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