Drawing AutoCAD splines with pyautocad

In our previous blogs, we learned about different pyautocad object utilities. In continuation with the same, we are going to discuss a new object called spline in this blog.

Setting up the AutoCAD template and environment using pyautocad

First of all, we will import the necessary libraries and create an AutoCAD template for drawing a spline.

#Importing pyautocad library:
from pyautocad import Autocad, APoint, aDouble

#Setting "create_if_not_exists"  to "True" will open & create AutoCAD template if not already:
acad = Autocad(create_if_not_exists=True)

To create a AutoCAD spline object with pyautocad we need to pass the points which we want to connect using the spline.

Hence, we need to use a method called “aDouble”, which we have used previously while creating polylines in one of our previous blogs.

p1 = aDouble(0, 0, 0, 42, 25, 0, 100, -15, 0, 155, 45, 0)

As you can see, I have passed 4 points in the form of (x, y, z) format.

Now, to attach these points with spline, we need to pass these points along with tangent points to the AddSpline command.

Syntax:

object.AddSpline(PointsArray, StartTangent, EndTangent)

PointsArray:

An array of 3D coordinates defining the spline curve. At least two points (six elements) are required for constructing a Spline object. The array size must be in multiples of three.

StartTangent:

A 3D vector specifying the tangency of the spline curve at the first point.

EndTangent:

A 3D vector specifying the tangency of the spline curve at the last point.

Let’s draw a spline now.

sp1 = acad.model.AddSpline(p1, APoint(2, 2, 0), APoint(50, 75, 0))
Figure 1: Spline and its properties

Python properties of a AutoCAD spline object in pyautocad

To utilize the objects for various purposes, we need to know the properties of the objects in depth.

Hence, we will start going through some of the properties of the spline.

We can use, closed & closed2 methods to check whether the line is closed in 2D space or 3D space respectively.

print(sp1.Closed)
print(sp1.Closed2)

O/p :

False
False

We can also find control points, with the help of which the spline curve is sketched.

Technically, the control point is a member of a set of points used to determine the shape of a spline curve.

Let’s check the output for the same first.

print(sp1.ControlPoints)
print("Number of control points: " + str(sp1.NumberOfControlPoints))

O/p:

(0.0, 0.0, 0.0, 11.52051310585697, 11.52051310585697, 0.0, 50.05475183782982, 47.655203788491555, 0.0, 103.9749708814854, -53.20316811306732, 0.0, 139.950225035047, 
22.425337552570483, 0.0, 155.0, 45.0, 0.0)
Number of control points: 6

To make this more understandable, we will connect these control points and see the results.

Figure 2.1: Representation of Control Points of a Spline

To get the degree of the spline’s polynomial representation we can use Degree or Degree2 which works for 2D & 3D respectively.

print(sp1.Degree)
print(sp1.Degree2)

O/p:
3
3

We can also find the start and end tangents of the spline:

print(sp1.StartTangent)
print(sp1.EndTangent)

O/p:
(0.7071067811865475, 0.7071067811865475, 0.0)
(0.5547001962252291, 0.8320502943378436, 0.0)

To print the fit points attaching which the spline has been drawn using the FitPoints property:

print(sp1.FitPoints)
print("Number of fit points: " + str(sp1.NumberOfFitPoints))

O/p:
(0.0, 0.0, 0.0, 42.0, 25.0, 0.0, 100.0, -15.0, 0.0, 155.0, 45.0, 0.0)
Number of fit points: 4

So fit points are basically the points which we have passed to draw the spline attaching those specific points.

We can also set tolerance for the fit points using the FitTolerance property:

sp1.FitTolerance = 15

If we set the fit point tolerance as mentioned above, we will get a curve within that tolerance limit.

Let’s draw the curve and check its properties:

Figure 2.2: Spline with fit point tolerance set to 15
print(sp1.Closed)
print(sp1.Closed2)
print("Control Points:")
print(sp1.ControlPoints)
print(sp1.Degree)
print(sp1.Degree2)
print(sp1.StartTangent)
print(sp1.EndTangent)
print(sp1.FitPoints)
print(sp1.FitTolerance)

O/p:

False
False
Control Points:
(0.0, 0.0, 0.0, 9.987921367545106, 9.987921367545106, 0.0, 45.132353712458624, 32.12396931297328, 0.0, 102.3441977662021, -22.046253272951006, 0.0, 141.67123369082907, 25.006850536243608, 0.0, 155.0, 45.0, 0.0)
3
3
(0.7071067811865475, 0.7071067811865475, 0.0)
(0.5547001962252291, 0.8320502943378436, 0.0)
(0.0, 0.0, 0.0, 42.0, 25.0, 0.0, 100.0, -15.0, 0.0, 155.0, 45.0, 0.0)
15.0

Other properties of AutoCAD spline object

Along with the above-mentioned properties, we can also find some other properties with the attribute names listed below:

print(sp1.IsPeriodic)
print(sp1.IsPlanar)
print(sp1.IsRational)

O/p:

False
True
False

You May Also Like

Leave a Reply

3 comments

Madhan says:

Hi Tanmay,
I appreciate all the great work you are doing. This is really fantastic tutorial to start with.
I am a python programmer started working on pyautocad. From the spline instructions I need some details to calculate tangent vector for start and end points. I assume this is angle w.r.t to x, y axis to end the curve. Could you please some relevant links to understand it.

Thanks,
Madhan

Hi Mr. Madhan,

Thanks a lot for your feedback. If we use the “ControlPoints” method against the spline object, and use them to construct a polyline, we will get to see the tangent vector on our template. So basically, control points represent the traversal of tangent lines.

Else to fetch the start and end tangent points, we can simply use those methods as given in this blog.

Thanks once again!

Ian says:

Hi Tammay,

Is it possible to draw a spline using pyautocad without specifying start and end tangents? You don’t have to specify them when using graphical input in AutoCAD.

Thanks
Ian

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.