AutoLISP Solutions: Smart pipe symbols1 Aug, 2002 By: Tony Hotchkiss
PVSYMBOLS.LSP inserts pipe symbols with automatic trim.
Robert Davis and Michael D'Attilio separately requested a routine to insert piping symbols with automatic trim. Many routines in textbooks and recent tips and tricks in Cadalyst insert piping symbols. However, these routines do not automatically trim the lines into which you insert the symbols. The solution presented here includes an AutoLISP file LSP), a dialog box file (DCL), a symbols library file (SLB), and a drawing file (DWG) that contains some symbols. This solution works by drawing lines in AutoCAD (figure 1) and then inserting symbols, as shown in figure 2. I am not an expert in piping symbols, so figure 2 does not make sense in an engineering application. However, it illustrates how the lines are trimmed when you insert the symbols.
How to Use PVSYMBOLS
Download the four files from Cadalyst's CAD Tips site and save them in the AutoCAD Support directory. The Cadalyst Web site also explains how to load AutoLISP programs, so follow those instructions to load the PVSYMBOL.LSP file. A prompt tells you to enter PVS to start the program.
|Figure 1. First, draw your lines in AutoCAD.|
When you start the program, the dialog box in figure 3 appears. This initial dialog box shows the Air Line symbol. Notice that the selection Insert with Trim is checked. You can select any symbol from the symbols list and insert the symbol with or without trimming. When you select a symbol from the list, its picture appears at the top of the dialog box.
If you press the OK button with the Insert with Trim choice, the routine prompts you to:
Select a line:
If you don't select a line, this prompt appears:
You must select a line, try again:
When you select a line, the routine automatically inserts the chosen symbol and trims the line appropriately. The dialog box displays again, and the last symbol used is shown as the selected one. To insert several instances of the same symbol, simply press the spacebar to repeat the insertion. To terminate symbol insertion, select the Cancel button in the dialog box.
|Figure 2. Pvsymbols inserts your selected symbols and trims the lines automatically.|
Insertion base point:
and the routine places the selected symbol on the drawing at the point you specify. The dialog box reappears, again showing the last symbol used.
Note that the symbols are all exactly one unit in length and that their base (insertion) points are all at the left end. This is what allows the program to automatically trim lines, as explained in the programming notes. When you select a line, the routine automatically inserts your chosen symbol and trims the line.
The program requires Visual LISP, so it works in any version of AutoCAD from 2000 upward. The program starts with my error manager and system variable management functions.
|Figure 3. Pvsymbols dialog box.|
The routine uses the selected point on the line and the start point of the line to calculate the angle of inclination of the line. The angle is represented in radians for the purpose of calling the AutoCAD command Break, and the break distance is exactly one unit. AutoCAD breaks the line before it inserts the symbol.
|Figure 4. When you select a symbol in the box at right, it appears in the preview box.|
The routine converts the insertion angle to degrees before it uses the Insert command to place the symbol using its block name, the insertion point, and the angle of rotation. (doit) also calls the functions (setv) and (rsetv) to change the object snap mode to 512, representing the "nearest" object snap, while AutoCAD inserts the symbol.
If you select the no-trim option, (doit) simply uses the Insert command after it establishes the insertion base point with the standard (getpoint) function. After inserting the symbol, (doit) calls the dialog box driver function Psymbol. The dialog box continues to appear until you select the Cancel button. I used this somewhat crude approach to repeat the appearance of the dialog box instead of the more standard method of hiding the dialog box by surrounding it with a (while) loop and monitoring the done-dialog status argument. I was able to retain the last symbol used by calling (psymbol) using the block, or symbol, name as an argument.
Using Your Own Symbols
sYou can use your own symbol libraries with PVSYMBOLS.LSP, but you must adhere to these few simple rules to make the programming as painless as possible:
- The symbol block names are the same as the slide names.
- The symbol blocks are all contained in a single drawing, PVSYMBOLS.DWG, that is inserted into the current drawing if it has not been inserted previously.
- The slides are placed in a slide library file named PVSYMBOLS.SLB. You can construct slide library files (SLB) from slide files (SLD) by using the Slidelib utility program supplied in the AutoCAD support directory.
- In the (init) function, replace my list of blocks with yours. Also replace my list of symbol names with yours. The symbol names must appear in the same order in both lists. Be sure to include the quotation marks.
The box below shows the (init) function that contains the two lists along with the Visual LISP function (vlposition), which ensures that the symbol name matches the symbol slide for showing its image in the dialog box.
|Code for (init) Function|
|(defun init (bnam / block-list symbol-list pos)|
|(setq block-list (list "al" "ball" "bfly" "check" "checks""diaph" "gate" "globe" "locks" "needle" "plug" "pressr" "qc" "qo" "sol") ;_ list|
|) ;_ setq|
|(setq symbol-list (list "Air Line" "Ball" "Butterfly" "Check" "Check with Spring" "Diaphragm" "Gate" "Globe" "Locks Shield" "Needle" "Plug" "Pressure Reducing" "Quick Close" "Quick Open" "Solenoid")|
|) ;_ setq|
|(mapcar ‘add_list symbol-list)|
|(setq pos (vl-position bnam block-list))|
|(show-image (itoa pos))|
|) ;_ init|
Autodesk Technical Evangelist Lynn Allen guides you through a different AutoCAD feature in every edition of her popular "Circles and Lines" tutorial series. For even more AutoCAD how-to, check out Lynn's quick tips in the Cadalyst Video Gallery. Subscribe to Cadalyst's free Tips & Tools Weekly e-newsletter and we'll notify you every time a new video tip is published. All exclusively from Cadalyst!