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.
object.AddSpline(PointsArray, StartTangent, EndTangent)
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.
A 3D vector specifying the tangency of the spline curve at the first point.
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))
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.
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:
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
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.