Fun With Polylines (pjoin.lsp)Many AutoLISP programs are used to 'cut through' AutoCAD's command hierarchy and this month's program, PJOIN.LSP, is one of them.A few months ago I got a call from a friend who was trying to write a program that would take a group of contiguous lines and/ or polylines, convert them if necessary and then join them into a single polyline. A day and several phone calls later he had a program working. While his program did the basic job that he required, I thought that a slightly more polished version would be useful. This one would filter out objects that could not be converted into a polyline, as well as reporting the number of segments in the newly created polyline. This would flag to the user that not all the objects selected were joined, as would occur if there was a mismatch in the endpoints of some of the objects. So 1 sat down and hacked out the program below. The ProgramAfter the program description, the program's name is defined and variables are localised. The current status of the Cmdecho status is saved and is then set to zero to prevent echoing of the program's command sequences to screen.The Undo command is then called to group the program into One Undo item. Note the period and underscore preceding "UNDO" and the underscore preceding "GROUP". The period ensures the command will work if it has been undefined and the underscore will enable the program to work on non-English versions of AutoCAD without translation. If you are using Release 11 or an even earlier version of AutoCAD, leave out the underscore, as it is not supported on these early releases. Since the SSGET function has only a generic "Select objects:" prompt, it is preceded by a PROMPT function which will display a more detailed prompt to give the user a better idea of what is expected. The SSGET uses a Boolean filter list so that only lines, arcs and polylines are passed to the selection set in variable SS2. An IF function then checks if any objects were selected by testing for a value in SS2. This is needed, since it is possible to reach this point without selecting anything. If objects have been selected, the IF's then-expression will run. This consists of most of the rest of the program, nested in a PROGN function. This starts by extracting the name of the first object in selection set SS2 and stores it in variable OBJN2. The next line uses some nested code to extract the object type and stores it in variable OTYP2. An IF function then checks whether the object is a polyline and, if so, its then-expression is run. This is the COMMAND function which runs the PEDIT command which, in turn, joins all the objects in selection set SS2 to the first object whose name is stored in OBJN2. If the object is not a polyline, the IF's else~expression is run. This is the PEDIT command again but has a "Y" which is the response to the Pedit's prompt asking to convert the object to a polyline. In order to count the number of segments in the newly created polyline, we start by storing its name in variable OBJN3. Since it is the most recently created object, the Entlast function can be used to select it. Polylines are complex objects containing subentities (or subobjects) and, by counting the subentities, we can determine the number of segments in the polyline. Counter CTR2 is initialised to -2 and a While loop will step through each subentity in the polyline until the end, each time incrementing the value of CTR2 by one. To step through the polyline, the name of each subentity, stored in OBJN3, is used as an argument to the Entnext function. This function then gets the next subentity name, resetting OBJN2 to the new name. The reason for initialising CTR2 to -2 is that each subentity is a vertex (segment endpoint) and then we have the Sequend subentity, resulting in two steps more than there are segments. The value in CTR2 is then used in a prompt to inform the user of the number of segments in the polyline. This ends the then-expression that was started when variable SS2 was checked for a value. If no objects had been selected, this IF's else-expression, which is simply a prompt stating that no objects were selected, would have run. The If is then closed. The program then winds
up with the Undo group being closed, the Cmdecho setvar restored to its
original value and the PRINC function called to prevent the value of Cmdecho
being printed to the command line.
|
©1996-2001 ZOTO Technologies