Splines to Plines30 Nov, 2003 By: Tony Hotchkiss
AutoLISP Solutions: AutoLISP converts splines with ease.
Eric Proulx requested an AutoLISP program to convert splines to polylines. The SPLINE-TO-PLINE.LSP routine converts splines to either lightweight polylines or 3D polylines, depending on the spline. If the spline is 2D (planar) and the z-coordinates are zero, the routine creates a lightweight polyline. Otherwise the spline becomes a 3D polyline. In fact, there is no direct correlation between splines and polylines, so SPLINE-TO-PLINE.LSP generates a list of points along the spline and uses the points to create polylines that have only line segments. In order to retain accuracy, you can select the number of line segments. The default is 100 segments.
Figure 1 shows a variety of splines in different orientations, some planar and some 3D with the grip markers for the splines. Figure 2 shows the conversion to polylines, with grip markers.
Figure 1. Splines in various orientations with grip marks.
Figure 2. Converted polylines, 2D and 3D, with grip marks.
The default number of segments (100) was used for all of the objects. After conversion, the splines are deleted and the polylines are on the same layers that the splines were on.
HOW TO USE SPLINE-TO-PLINE.LSP
Download SPLINE-TO-PLINE.LSP from Cadalyst's CAD Tips site and save it in AutoCAD's Support directory. From the AutoCAD Tools menu, choose Load Applications, or enter Appload at the Command prompt. In the Load/Unload Applications dialog box, select the SPLINE-TO-PLINE.LSP file from the support directory where you installed it, then click Close. After you load the program, you are prompted to enter S2P to start.
When the program starts, it prompts you to select objects. If you don't select any objects, it prompts you to try again. If you select objects other than splines, the program ignores them. If no splines are in the selected set of objects, the program ends. When you select one or more splines, the program prompts you to:
Enter Number of segments <100>:
where 100 is the default that you accept by pressing <Enter>. It then converts the splines to polylines (lightweight or 3D) on the layers of the original splines. At first glance, it looks as though nothing happens because the new objects look the same as the originals. To verify that the conversion occurred, select any converted objects and list them to ascertain their types.
Get the CodeDownload the code for this and all articles in Cadalyst's CAD Tips site. Or get the routines directly from author Tony Hotchkiss at CAD/CAM Technologies, 42 15th St., Buffalo, NY 14213, 716.883.8790, e-mail firstname.lastname@example.org or visit his web site.
For this routine, I didn't include my usual error handler and system variable functions because no system variables are affected during execution. SPLINE-TO-PLINE.LSP has four functions, along with the (c:s2p) function to create the shortcut name S2P and the prompt to let you know how to start the routine.
The (spline-to-pline) function gets a list of splines to convert with a call to (get-spline), establishes the number of segments for the polylines, and then calls (convert-spline) in a repeat loop to convert each selected spline in turn to the appropriate type of polyline. The (spline-to-pline) function also sets up the global variables *thisdrawing* and *modelspace* for use in ActiveX functions later in the program.
The (get-spline) function tests that no selection sets exist in the drawing, then adds the selection set ss1 with the (vla-add) method. The (vla-clear) method initializes the selection set. This is similar to using the old AutoLISP (ssadd) function with no arguments to construct a new selection set with no members. A (while) loop gets the members of the selection set-the splines-using the (vla-selectionscreen) method. A (repeat) loop within the (while) loop tests that the selected object is a spline before adding it to a list of spline objects. (get-spline) returns the list of splines to the (spline-to-pline) function.
If the list is not empty, each spline in the list is sent to the (convert-spline) function together with the number of polyline segments to be used to create polylines. (convert-spline) has some interesting functions that return parametric data about AutoCAD curve objects.
You can use AutoCAD to convert splines to polylines. Use the Divide command to create a set of point objects along the splines, and then use the Pline command with the set of points obtained. The equivalent programming algorithm is to use the ActiveX functions (vlax-curve-getstartparam), (vlax-curve-getendparam), and (vlax-curve-getpointatparam) to collect a list of any number of points along a curve. There's no need to create point objects, and you can use the point list to create the required polyline objects using the (vla-addlightweightpolyline) and (vla-add3dpoly) methods.
This is the basis of the (convert-spline) function, which generates lists of 2D and 3D points to make either 2D (lightweight) or 3D polylines via a call to the (add-polyline) function. This function takes two arguments: the point list, and the name of the appropriate method to add lightweight or 3D polylines, respectively.
The program adds the polylines to the drawing on the same layer as the splines were on, and then deletes the splines.
As an educator, my hope is that these programming notes encourage you to read the code and use any parts of it to assist you in writing your own programs. The difficulty in writing code in Visual LISP using ActiveX techniques is that the documentation that comes with AutoCAD pertains to VBA (Visual BASIC for Applications), and we are supposed to transpose the methods to Visual LISP by adding the VLA- to the VBA methods and properties. However, not all of the methods are available this way, and sometimes you just use the (vlax-) functions instead. Let's hope that Visual LISP is developed along with VBA and that it gets the documentation that it deserves.
As usual, good programming, and keep those requests coming in.
About the Author: Tony Hotchkiss
For Mold Designers! Cadalyst has an area of our site focused on technologies and resources specific to the mold design professional. Sponsored by Siemens NX. Visit the Equipped Mold Designer here!
For Architects! Cadalyst has an area of our site focused on technologies and resources specific to the building design professional. Sponsored by HP. Visit the Equipped Architect here!