Tidy Circles (AutoLISP Solutions AutoCAD Tutorial)1 May, 2007 By: Tony Hotchkiss
Programs trim all entities outside of a circle and convert circles to polylines.
Naveen Rayalla e-mailed a request to delete all entities touching and outside of a circle in AutoCAD. To trim all entities outside of a circle using the regular AutoCAD Trim command, the user first selects the Trim tool, selects the circle as a cutting edge or boundary and then makes a series of fences or selects individual entities, depending on how many entities are to be trimmed. When there are many entities, such as those shown below, the process can be time consuming.
A drawing ready to be trimmed.
Our AutoLISP Solution is a CIRC-TRIM.LSP, a routine that trims everything outside of a circle when the user simply selects the circle. Because the solution is rather short, I have included a bonus AutoLISP Solution to convert circles to polylines.
In my job as professor of technology at Buffalo State College, I use the VALCAM CAD-CAM system that I wrote in AutoLISP to work inside AutoCAD. The CNC code generator uses polylines for machining, so the students convert all of the entities to be machined into polylines. AutoCAD doesn't convert circles into polylines, and it's tedious to draw arcs instead of circles and then convert those to polylines. The solution CIR-TO-PLINE.LSP does the job automatically for any selected circles.
Get the Code
Download the CIRC-TRIM.LSP and CIR-TO-PLINE.LSP files from Cadalyst's CAD Tips site and save them in AutoCAD's Support directory. Use the Appload facility by selecting Tools/Load Application, and then select CIRC-TRIM.LSP for trimming outside of a circle and CIR-TO-PLINE.LSP to convert circles to polylines.
How to Use the CIRC-TRIM.LSP Code
To start the program, enter CT and you see the prompt Select the circle. The figure below shows a circle being selected. A typical result is shown in the next figures (2 and 3) in which the entities outside of the circle have been trimmed with no further user interaction.
A circle is selected.
A typical result showing entities after trimming.
How to Use CIR-TO-PLINE.LSP
To start the program, enter CTP and you see the prompt Select the circle(s) to be modified. After you select any number of circles by any selection method, the circles are automatically converted to polylines and the polylines are placed on the same layer as the selected circles. The drawing won't change in appearance, but you can verify that the objects are polylines by using the AutoCAD List command.
CIRC-TRIM.LSP starts with my usual error handler and system variable-management functions. If an error occurs during the program for any reason, such as a cancellation by the user, any system variables that have been changed are reset to their previous values by calling the RESETTING function.
The main function is CIRC-TRIM that begins with some standard code to initialize the program to recognize Visual LISP functions and sets global variables for the current drawing, model space and utility functions. There are only two lines of code that follow the initialization:
(vla-GetEntity *utility* 'Cirobject 'PickedPoint "\nSelect the circle")
(do-trim Cirobject PickedPoint)
The GetEntity method creates the selected object name Cirobject as well as the picked point, and these two objects form the arguments passed to the DO-TRIM function, shown here:
(defun do-trim (Cirobj PickPoint)
(setq cenptobj (vla-get-Center Cirobj)
radinc (/ (vla-get-Radius Cirobj) num)
rad (+ (vla-get-Radius Cirobj) radinc)
cen (vlax-safearray->list (vlax-variant-value cenptobj))
anginc (/ pi (/ num 2))
p0 (vlax-safearray->list PickPoint)
) ;_ end of setq
(setq p1 (polar cen ang rad)
ang (+ ang anginc)
p2 (polar cen ang rad)
) ;_ end of setq
(vl-cmdf "TRIM" p0 "" "F" p1 p2 "" "")
) ;_ end of repeat
) ;_ end of do-trim
The DO-TRIM function works by trimming objects just outside of the selected circle using a repeat loop to make a sequence of calls to the Trim command. A number of segments (num = 100) is used to successively trim using a fence between two points that are incremented around the outside of the selected circle. I chose 100 steps to get a good combination of accuracy and speed of execution.
Any objects that are entirely outside of the circles aren't affected by the trimming as is the case with the AutoCAD Trim command. You may choose to delete (erase) any unwanted entities.
CIR-TO-PLINE also starts with the error handler and other system variable managers. The main function CIRC->PLINE uses the SelectOnScreen method to give the greatest flexibility in selecting objects. A repeat loop then calls DO-MOD for each of the circles selected. It checks to make sure that the object is a circle before any conversion takes place. DO-MOD calls the CONVERT function, shown here:
(defun convert (pt1 pt2 lyrname)
(setq xylist nil)
(setq plst (list pt1 pt2 pt1))
(mapcar '(lambda (xy)
(setq xylist (append xylist (list (car xy) (cadr xy))))
) ;_ end of lambda
) ;_ end of mapcar
(setq polyobj (make-LWpolyline xylist)) ; polyline object
(vla-SetBulge polyobj 0 1)
(vla-SetBulge polyobj 1 1)
(vla-put-Layer polyobj lyrname)
) ;_ end of convert
CONVERT uses two points on the diagonal of the circle to create a list of three points, repeating the first point so that the resulting polyline is effectively closed. After the polyline is added by a call to MAKE-LWPOLYLINE, the SetBulge method is applied with a bulge of 1 denoting 180 degrees included angle. Finally the newly added polyline is put on the same layer as the circle from which it was made. The circle is then deleted, leaving the polyline in its place.
I have used the function MAKE-LWPOLYLINE in other routines (including the Spurgear routine), so I was able to reuse the code here as follows:
(defun make-LWpolyline (ptlst)
(setq arr (vlax-make-safearray 5 (cons 0 (- (length ptlst) 1))))
(vlax-safearray-fill arr ptlst)
(setq invpline (vla-AddLightweightPolyline
) ;_ end of vla-AddLightweightPolyline
) ;_ end of setq
) ;_ end of make-LWpolyline
MAKE-LWPOLYLINE uses the AddLightweightPolyline method as shown after converting a list of 2D points to a variant containing an array.
As always, I look forward to receiving your comments and requests for AutoLISP Solutions. Contact me using the links below.