AutoLISP Solutions: Design pipe flanges1 Oct, 2002 By: Tony Hotchkiss
AutoLISP routine draws both front and side views.
Stacy Mitchell requests an AutoLISP routine to draw pipe flanges with both end and side views. Depending on the view required, the input information includes flange thickness, flange inside and outside diameters, bolt circle diameter, number of holes, hole diameter, neck diameter, and the flange projection. Figure 1 shows typical plan and side views for the type of flange under consideration.
The AutoLISP solution is flange.lsp, which also has a dialog control language file called flange.dcl. The dialog control box lets you select either the plan view or the side view. The routine presents data entry boxes relative to the chosen view, as shown in figures 2 and 3. Data entry boxes are grayed out when they are not required.
How to use flange.lsp
Download the flange.lsp and flange.dcl files from Cadalyst's CAD Tips site and save both files in the AutoCAD support directory. Select Load Applications from the AutoCAD Tools menu. In the Load Applications dialog box, select the flange.lsp file from the AutoCAD support directory and then select Close.
|Figure 1. Plan and side views of a typical flange.|
At the AutoCAD prompt, enter FLD to start the program. When you enter FLD for the first time, a dialog box similar to figure 2 appears with a plan view shown with default data in the data entry boxes. Select either the plan or side view and fill in the appropriate data in the available boxes. When you select OK, the routine prompts:
If you selected the plan view, AutoCAD automatically draws that view. If you picked side view, AutoCAD draws the side view and also prompts you for a rotation angle.
|Figure 2. Data entry box for flange plan view.|
You can supply the rotation angle by snapping to a convenient object snap or polar tracking position. Use the rotation angle to change the side view to horizontal, vertical, or any other orientation.
When you place flanges in the same drawing session, the flange program remembers the last set of data that you enter, so you can make a plan view and a side view of the same flange without re-entering the data.
The dialog box displays a reasonably complex pair of images for the plan and side views of the flange, but there are no slides or slide libraries anywhere. Instead, I programmed vector images in the dialog box. Unfortunately, AutoLISP allows only single straight lines to be drawn there. Drawing vectors can be quite time-consuming for the programmer, especially when making circles, so I created a function, (image-circle), that draws circles effectively in only fifteen lines of code. The function requires a center point, a radius, a number of vectors, and a color as input. The number of vectors depends on the resolution you want to display. For the largest circle drawn for the outside diameter of the flange plan view, 30 straight-line vectors are enough to make a very convincing circle. See the code box on the next page for the (image-circle) function.
|Figure 3. Data entry box for flange side view.|
Using the (image-circle) function to draw a set of circles to represent a bolt circle is easy. It requires a repeat loop of only two lines of code, which is part of the function (draw-plan-image). The trick to drawing vectors in dialog boxes is to delineate the image in integers that represent the width and height rather than in real units. This means that the integers must be supplied to the coordinates of the (vector_image) functions. I used the standard (fix) function in my functions to draw the plan and side images.
I reduced the need to call the (command) function when drawing the actual flanges using the parameters set by the user. I made three functions-(draw-circle), (draw-line), and (draw-rect)-to automatically create (entmake) lists for circles, lines, and rectangles, respectively. You can extend this technique to create any AutoCAD entity more efficiently than calling the (command) function. You can also designate any layer name on the fly, and AutoCAD creates that layer if it doesn't exist.
I tested drawing circles using my (draw-circle) function vs. using the (command) function, and under the most favorable conditions, the (command) function took four to five times longer. Times got worse with CMDECHO on and object snaps active. Neither of these factors affects the speed for (entmake), so I used (entmake) instead of (command) for speed. If you use functions such as (draw-circle), programming for (entmake) becomes easy.
All the programs in AutoLISP Solutions should work, and they do when I test them on my systems. Occasionally, I goof up, such as making a global variable become local, as I did with the piping symbols program in August 2002.
Sometimes, the programs don't work for you because I write them for AutoCAD 2000 or later. Updates to AutoLISP in the most recent AutoCAD versions make programming much easier, so it's even less likely that I'll write code that works for earlier versions of AutoCAD.
As always, good programming. Contact me with any questions and keep those requests coming in.
In her easy-to-follow, friendly style, long-time Cadalyst contributing editor and Autodesk Technical Evangelist Lynn Allen guides you through a new feature or time-saving trick in every episode of her popular AutoCAD video tips. Subscribe to the free Cadalyst Video Picks newsletter and we'll notify you every time a new video tip is published. All exclusively from Cadalyst!