More At Last (lastn.lsp)When AutoCAD offers you the 'Select objects:' prompt, there are a number of ways you can respond. You can pick one or more objects individually; select a group of them with a WINDOW or CROSSING; use the PREVIOUS option to select the previously selected objects; or the LAST option for the last object created. This month's program provides another useful object selection method. When invoked it will select the last n number of objects in the drawing where n is an integer input by the user.The ListingThis program has a large description section; in fact the description is larger than the program itself. Many newcomers to programming tend to underestimate the importance of comments in a program and tend to underdocument them. This program, although quite short, is very versatile and the description needs to be quite detailed so as to provide the user with a good working knowledge of how to use it to its full potential.The program is written in two parts; the LASTN function, commonly referred to as a user-defined function; and the C:LASTN function, which is the main program. The C:LASTN function is the one invoked by the user and requests an integer from the user. It then calls the LASTN function with the integer as an argument which in turn gets the integer number of objects and returns them to the C:LASTN function. The LASTN function starts with the DEFUN statement which gives it its name, defines one argument request, INT2, and localizes two variables - SS2 and SSL2. Argument requests are variable names left of the forward slash in the list of variables after the DEFUN statement. This means that when the function is called, there must also be an argument for each argument request defined in the list. The values of the arguments are stored in the names of the argument requests which then act as local variables. In this case there is one argument, an integer, whose value will be stored in variable INT2. The next line uses SSADD with no arguments to create the selection set SS2 which does not have objects. This is required as SSADD will be called later with an object and selection set as arguments - the selection set must already exist in order for SSADD to work. This is followed by the REPEAT function which will run the code nested in it INT2 times. The code in the REPEAT starts with an IF function which uses the ENTLAST function as its test-expression to check that there is an object in the drawing. If ENTLAST returned a value, ie - an object name, the IF's then-expression is run. A PROGN is used to group two expressions into a single then-expression. There is no else-expression for this IF. This grouped then-expression starts with the SSADD function with two arguments - the name of the last undeleted object as returned by the ENTLAST function and the selection set SS2. This object is deleted from the drawing by the following ENTDEL function. It is worth noting that although the object has been deleted, its name is still in the selection set and exists in the 'background' so to speak. It won't be fully deleted until the drawing is exited and until then can be recovered. This is the mechanism by which commands such as OOPS and UNDO can bring back deleted objects. If variable INT2 was greater than 1, the REPEAT function will loop and there will be a new 'last' object which is placed into the selection set and then deleted. This continues until REPEAT has looped the required number of times. The variable SSL2 is then set to 1 less than the number of objects in selection set SS2. A WHILE loop is then started which will undelete the objects in the selection set. This works by getting the name of the SSL2'th object name in the selection set and passing it back to the ENTDEL function. The ENTDEL function acts as a toggle. If the object has not been deleted, ENTDEL will delete it; if it has been deleted, ENTDEL will undelete it. In this case all the objects had been deleted and are now being undeleted. The reason SSL2 was originally set to I less than the number of objects in the selection set is because of the way the SSNAME function counts - 0 gets the first object, I the second and so on. In each iteration, SSL2 is decremented by one so as to progressively undelete all the objects in the selection set. Once the value in SSL2 falls below zero, the program will drop out of the WHILE loop. The selection set SS2 is then returned to the waiting function simply by quoting it at the end of the function. In this case, the waiting function is C:LASTN which is the main program. C:LASTN starts with the DEFUN statement where it gets its name and one variable, COUNT2, is localized. The next line will prompt the user for the number of objects to select and store that number in variable IT2. This is followed by an IF statement which checks if the program has been called transparently. It does this by getting the value of the CMDACTIVE setvar which will be '1' if it there is already a command active and '0' if there is not. CMDACTlVE does not consider an AutoLISP program to be a command. If no command is active, then the SELECT command is called using the COMMAND function. The LASTN function, with its integer argument, is called which returns the selection set it builds to the waiting SELECT command. The select command is then terminated with two double-quote marks. The PRINC function then prevents nil from being printed to the command line. The program will then terminate and the objects selected will be accessible to future commands with the PREVIOUS option. If a command is active, this means that the program has been called transparently In this case the LASTN function is called with the integer obtained earlier as its argument and the selection set it builds is returned to C:LASTN. As this selection set is the last value in C:LASTN (there being no PRINC function following), it is returned to AutoCAD and the waiting command which should be expecting a selection set. Usage TipsYou may have noticed that the C:LASTN function was defined last. This is a courtesy to new users so that when the program is loaded with (load "LASTN"), C:LASTN is printed to the command line providing them with a clue as to how to invoke it.As mentioned in the description for the program, the LASTN function can be used independently of C:LASTN by other programs. C:LASTN merely provides a user interface for LASTN which does the real work. It is also possible to incorporate LASTN into other programs that you write. LASTN is a reasonably-good generic function which may be used by other
programs. Once you have built up a library of such generic functions, AutoLISP
writing will be much easier since you can quickly build up a program with
a number of tried and proven functions.
|
©1996-2001 ZOTO Technologies