Time to read: about 9 minutes.
This article was was featured on the front cover of CADalyst Magazine in 1987.
The CAD Cookbook
In this department interesting problems submitted by our readers are tackled by an independent consultant.
CADalyst is proud to open the CAD Cookbook, a new department to provide a limited consulting service to our subscribers. Your letters are welcome. Your part is to inspire us by describing a single AutoCAD problem or requirement we can help you with. If it has general application and can be solved with the moderate consulting budget of this department, we may select your request and discuss it with you as necessary. Then we will tackle the problem with macros, menus, LISP or whatever seems most suitable; we’ll check out our solution with you and publish both that and your response when completed. The service is free to subscribers and the principle selection criteria will be that the solution will be useful to many readers.
Our initial chef is Mike Slinn, the principal contributor to our ongoing LISP Tutorial. Other consultants are invited to let us know of their availability in case readers’enthusiasm exceeds his part-time capacity.
AutoCAD allows us any line type providing it consists of dots or dashes. We need a more versatile line type command with the capacity to include inserted letters, symbols and crosslines. Some desirable features would include:
- User selectable spacing between elements;
- Access to standard editing (
- User selectable crossline weight, different from base line;
- Automatic element spacing when turning at sharp angles.
I am sure that a large number of AutoCAD users would welcome such an enriched linetype command.
Yours very truly,
Sr. Electrical Engineer
Fenco Lavalin Corp.
This request for a composite line type has been on the AutoCAD wishlist for more than two years. Many users need to include letters or symbols as an integral identification of a line. The simplest example is perhaps the line with crossticks, either singly or in groups. Another frequent need is for code lettering: two or three text characters within a break in the line, repeated at regular intervals. The ability to include simple symbols or drawings is also useful. These need to be combined with lines that may be dashed or dotted like regular AutoCAD linetypes.
We saw the general requirement to be this: after an initial solid line segment anchoring the beginning of a composite line, to repeat a sequence of elements in the direction of the second point until it cannot be repeated again, and then to complete it with a solid line from the end of the last complete repetition to the endpoint of the composite line segment. Each repeating sequence should be allowed to consist of (in any order) a reasonable number of dashes (line segments of specified length), gaps (spaces of specified length), text strings (placed in an appropriate length of gap) and AutoCAD Blocks or Shapes for symbols. We thought it necessary to provide an online definition capacity (similar to block definition and unlike the offline coding required for
Shapes and text fonts). The user interface should parallel the ordinary
Line command as nearly as possible.
Linetype definition does not provide options other than dots and parallel dashes. Because we have no access to the internal line drawing routines, our solution falls short of being a true AutoCAD line. It was not too difficult to imitate the point-to-point construction of an AutoCAD line, but standard editing eluded us. As an aid to editing, however, it seemed useful as an option to make each completed line segment into a block. It can then be removed by a single
Erase Last. Although our routine is coded for AutoCAD v2.18 or above, the block-creating technique is only possible with AutoCAD v2.5. An interesting AutoLISP technique forms the heart of CLINE, involving a new programming idiom and the
(Setq J (List 'Command "text" "c" 'K L 'M N)) (Eval J)
In this example, an AutoCAD command (
Text) is set up as a list
J and when
J is later evaluated, the command is performed. The text string (a single character
N) is centered at insertion point
L, and angle
M. Because the text height
L is not quoted, its current value at the time of the
SetQ command will be stored in the list
M are quoted, their values are not referenced until the
(Eval J) statement is executed. CLINE may therefore reuse this command list variable
J to place the same text string in many positions. The repeating sequence of lines, gaps, text and/or blocks that make up a composite line is created by making a list of variables like
J. At each repeat of the sequence, the list elements are evaluated in turn and the included commands create the composite line entities.
The initial version brought the following request for improvements from our client. Our responses are included in green.
I received the composite line routine for evaluation from Mike Slinn. I spent some time playing with it over the weekend. I couldn’t expect a faultless performance and ran into a few surprises [these were attributed to their use of an early "patched" AutoCAD v2.18]. What I consider desirable modifications and additional features includes the following:
- Automatic text orientation – the first version wrote upside-down when going to the left. Implemented.
- The default for closing a polygon should be <No> – it was originally
- It should be possible to define an unbroken line with symbols (eg. cross-ticks) superimposed. This can be done by creating a unit block with centered symbol and insertion point
- When a segment ends without sufficient space for another symbol repeat, an additional line segment is drawn from the end of the last completed repeat to the end. This produces a pen, lift and a little dot on the line. Plot optimization level 4 should eliminate this. Ideally the two end dashes would be equalized and the middle shifted to balance the segment
- To Break a segment, the block has to be exploded and the individual line and symbol entities edited appropriately – this procedure is too long. LISP commands to mimic the normal line edit commands are possible but were not attempted. The strategy would be to generate replacement segments as required.
Orthomode would be useful. Possible for AutoCAD v2.5 only. Implemented – and the routine detects your version number automatically.
- Rubberbanding would be useful. As above-implemented for AutoCAD v2.5 only.
- Line definitions need to be saved for future use. Possible but not implemented as an integral part of the routine. The quite adequate work-around technique is to create menu macros to run the definition routine with the desired parameters; for example: [SWLine]CCCN;0.5;T;SW;0.2;D;1; X; which defines a line with SW’s spaced between one unit dashes. Incidentally, the line definition call would perhaps more naturally have been named
CDis often used in DOS shell calls and would clash.
- Line definition parameters should be able to be displayed for verification. Not implemented. Use of menu macros should make this unnecessary.
- How can I change one of these linetypes to another? Not implemented here. A strategy might be to make the LISP definition of the code that defines the line into the value of an attribute of the block containing each segment
I would like to thank Mike Slinn and CADalyst, for giving me the opportunity to be part of tbis joint effort. I think that Mike has created a usable tool which, through normal evolution, should become very useful.
How to Use CLINE
CLINE.LSP to your AutoCAD directory. Prior to starting AutoCAD v2.18 or v2.5, make sure the AutoLISP Stack and Heap are set correctly by typing:
SET LISPSTACK=5000 SET LISPHEAP=40000
Then start AutoCAD as usual. Once inside the drawing editor, type:
(load "CLINE") This loads the routine, which will prompt you to define a composite line and then use the definition immediately. You should first see:
Composite line drawing routine (C) Copyright 1987 Michael Slinn Define composite line: Minimum end line segment length? <0.50000>:
Your response sets the minimum length of the line segments at beginning and end of a segment of composite line. For now, press the space or return key for the default. It then cues you for the components of the repeating sequence:
Just type one letter followed by Enter to respond to the prompt, eg. type T and press Enter to insert text. The program will ask:
Text string? Respond with a short text string, such as
SW and press Enter. It then prompts for the text height, offering a default. From your response it will define both the text height and the gap that will include the text string. Again the program will ask:
Next? Type D and press Enter for a dash; you will be prompted for the dash length with a default of twice the minimum segment length specified earlier. Override this by entering
2 and pressing Enter. At the
Next? prompt, terminate the definition by typing
X and pressing Enter.
If you have AutoCAD v2.5 or later, CLINE will prompt you with an additional question:
CLINE segments to form blocks? <Yes>
Accept this default. If you do not, or if you are using AutoCAD V2.18, the routine will generate composite lines whose entities remain separate. [Users of AutoCAD v2.18 may ignore the next three paragraphs.]
If AutoCAD v2.5 users press Enter to assent to the default, each composite line segment (the portion of the line between each pair of points, no matter how complex) will be drawn, transformed into a block and then re-inserted at the same location. This allows you later to
Rotate the line as a unit.
Blocks need names; several composite lines of many segments will require many block names. The program requests a prefix character string (up to 27 characters long; the default is
CLINEBLK), and an initial block sequence number from 1-9999 (default 1), to create a first block name (default
CLINEBLK1). Horrible things will happen if the, block name already exists. CLINE has no way of checking to see if the block name has already been used. Pick a unique block name!
As we draw each new line segment, the numeric suffix is increased by one. Should a block suffix reach 9999 (“Max Block Usage”), a new block name is requested. The last letter of the block name prefix is altered automatically and the block name suffix reset to 1 so that you may accept the default values offered without causing an error. Caution: once you leave the drawing editor, CLINE forgets the current block name suffix and prefix. The default name must be changed next, session when CLINE is first used, or again horrible things will happen with block name duplication. If you have prudently Listed and logged the last generated block name, you may continue the sequence with the same prefix and the next numeric suffix. You will now use the line definition for the first time.
CLINE behaves much like the
Line command. It requests the starting point of the line with “From point:” and subsequent segments by “To point:”. Any point specification is valid, including digitizing a point, entering relative or absolute coordinates from the keyboard, or using OSnap, but not including AutoLISP commands such as
(GetPoint). With AutoCAD v2.5 or later you will see a rubberband line, and
Ortho will work.
The actual AutoCAD commands issued by CLINE are echoed onto the screen, which gives you an idea of what is going on but slows down the program. CLINE will crash when command echoing is turned off, due to an undocumented bug in AutoCAD v2.52. Continue drawing lines until you have completed a first sequence. Upon pressing the spacebar, Enter or Ctrl-C CLINE asks:
Close the polygon? <No> An N, space or Enter will cause the program to end. Typing a Y causes one extra composite line segment to be generated which forms a closed polygon.
After the first use on initial load, you may reuse the definition and generate more of the same composite lines at any time by typing
CL. New definitions are possible by typing
More complex CLINEs may be defined by using more components. Blocks may be part of the composite line type definition. Make sure that blocks are of unit size (the X-dimension of the symbol should equal one drawing unit), and have their insertion point in the center of the block. They may be scaled when used (your menu macros defining CLINEs might need variables if you frequently work at different scales) and the blocks will be placed in a suitable scaled gap. Make cross-ticks as unit blocks with a horizontal line 1.2 units long through the center (0.6 units either side), and line or polyline diagonal ticks of whatever widths as required. A Shape could be used by inserting it in a block. Dots are made from very short dashes, dotted lines with short dashes and gaps. AutoCAD
Linetypes can be used for the current layer, with discretion: symbols and text will also be drawn with whatever
Linetype is chosen.
Although the composite lines may appear to draw slowly, compare the rate at which they are drawn to the speed with which you can draw them any other way! Note that once they are constructed, subsequent regenerations will proceed much faster.
CAD Cookbook routines are available on diskette from CADalyst for US$10.00. Write “CAD Cookbook”, CADalyst, 282-810 West Broadway, Vancouver BC, Canada V5Z 4C9 and request CLINE in IBM 360K or Victor 620K format. Profits go to the routine’s author.