Sample lisp programs
It uses functional programming paradigm for programming; i. Statements are written in fully parenthesized prefix notation. For e. It has automatic garbage collection. Hence the programmer need not have to explicitly free the dynamically allocated memory.
It has a strong support for recursion. That's why, it is widely used in Artificial Intelligence projects. Lisp has many dialects; but most common dialects are Common Lisp and Scheme. Three basic building blocks of Lisp programs 1. These functions are advanced solutions to the standard procedures for adding and removing dictionary data. Upon executing the function, the routine runs through two examples using the vlax-ldata-xxx functions.
It then lists the data contained in the key and finishes by removing the key, but it does not remove the dictionary entry.
It then proceeds to remove the key and finally deletes the Circle object. Similar to the second section of LispData1. The "LDT" key value is not set on a selected object until you run the routine once. Afterwards, you can use the routine to change the value associated with key "LDT" for that object. Please note that this description supplements the extensive comments contained in the Regarx.
Furthermore, it supersedes the comments in the source code on the function arguments which are incorrect. The regarx. It also provides a thorough explanation of the information necessary to implement this mechanism since the wrong information in any location of the registry can completely disable AutoCAD and possibly your operating system.
Please back up your registry before running any commands that edit and change your registry entries. The vlax-reg-app function is more general and can be used to register any application. At the end of the file, you will find sample code that passes correct parameters to the register-VL-app function. This sample file provides an example of what can be accomplished with Visual LISP registry functions.
The dump-registered-apps function prints out a registry database subtree for every application that is registered for demand loading in your AutoCAD Release 14 configuration. One particular expression worth mentioning in the registry-tree-dump function called by dump-registered-apps is the second occurrence of the foreach function which contains a recursive call to registry-tree-dump. For each application that is registered for demand loading, there are three subkeys: Commands, Loader, and Name.
This particular foreach expression applies the registry-tree-dump function to the string concatenation of each these three subkeys with the key for each of the registered applications to build and print out its subtree. It is also important to notice that the vl-acad-defun expressions appear outside of the defined functions. If they were inside a non-command line function no "c:" , it would be impossible to call either of the two functions in sym-exp.
Therefore, there are two rules to follow for explicitly exporting functions from an ARX routine without using a command line function to evaluate vl-acad-defun expressions :. Code is included at the end of the sample file which can be used to test these functions from the AutoCAD command line. This program utilizes Editor Reactors to update a "time-stamp" text entity each time the user completes an instance of the PLOT command. Between the user pressing OK to begin the plotting procedure and AutoCAD spooling to the plotter, the Editor Reactor fires off the callback function which adds or updates a time-stamp text entity at a base point of 0,0,0.
The result is that until you run the Unregister-Timestamp function, the associated ARX application will automatically be launched the next time you start AutoCAD regardless of the drawing file that you open. Furthermore, if you open the drawing in which you registered the Time-Stamp routine without the associated ARX loaded, you will receive a notification dialog informing you that the required ARX application is missing.
To return to other commands for demand loading of vlide. There is a workaround contained in this sample file which solves an "Object Open for Read" error which you may receive in connection with associating a callback function with the PLOT command to modify a graphical entity. The workaround is to add an additional graphical object to the drawing after creating the object which the callback function modifies. Upon loading this sample file, it uses ActiveX function calls to draw a line.
It then passes the line as a VLA Object along with a 4 x 4 transformation matrix to the vla-TransformBy function in order to rotate the line by 90 degrees. The file provides a total of five different matrices which can be used to rotate, move, or scale the line. To switch to a different matrix, change the variable called in the vla-TransformBy expression.
The use of matrices can be an excellent shortcut for manipulating graphical objects. Once created, the code for a matrix can easily be reapplied in other functions. The first three columns of the matrix specify scaling and rotation.
The fourth column is a translation vector. The functions that use a matrix of this type treat a point as a column vector of dimension 4.
You will never find the z- coordinate first or the x-coordinate last. We obtained the coordinates from the drawing and then stored them in a variable. We did mention a third LISP function we could use, getpoint. Programmers prefer getpoint because it is more efficient than the Id-LastPoint combo we used above. As before, we use the setq function to store the value of the coordinates in variable xyz. The getpoint function waits for you to pick a point on the screen.
We could just as easily have written anything, like:. No prompt. Just a silent BricsCAD waiting patiently for the right thing to happen … and the user puzzled at why nothing is happening.
A lack of communication, you might say. To place text in the drawing, we can use only the command function in conjunction with the Text command. I suppose the MText command might work, but for one line of text, the Text command is excellent. The Text command is, however, trickier than the Id command. It has a minimum of four prompts that your LISP routine must answer:. Start point: a pair of numbers, specifically an x,y-coordinate. Height of text: a number to make the text legible. Rotation angle of text: a number, probably 0 degrees.
Text: the string, in our case the x,y,z-coordinates. Change this number to something convenient for your drawings. BricsCAD runs through the Text command, inserting the responses for its prompts, then placing the coordinates as text. There you have it: a full-fledged LISP program. Well, not quite. In the next section, you find out how to save the code as a. What you have now is the algorithm — the core of every computer program that performs the actual work. What is lacking is most of a user interface — the part that makes it easier for any user to employ the program.
How many can you think of? Go right ahead, Mr. Grabowski: Show me how to add them in. But, wait a minute! If you are familiar with programming, then you know how quickly a simple program fills up with feature-bloat. While all those added features sound desirable, they may make the program less desirable.
Take a second look at the wish list above. Check off features important to you, and then cross out those you could live without. To give the program a name, surround the code with the defun function, and give it a name, as follows:. When the c: prefix is missing, however, then you have to run the program like a LISP function, complete with the parentheses, as follows:. Input variables — feed data to LISP routines; the names of input variables appear before the slash.
Local variables — used only within programs; the names of local variables appear after the slash. In this program, xyz is the name of the variable that is used strictly within the program. If variables are not declared local, they become global.
The benefit to declaring variables as local is that BricsCAD automatically frees up the memory used by the variable when the LISP program ends; the drawback is that the value is lost, making debugging harder.
For this reason, otherwise-local variables are kept global until the program is debugged. And the closing parenthesis balances the opening parenthesis at the beginning of the program.
By saving the program to a file on disk, you avoid retyping the code with each new BricsCAD session. You do this, as follows:. I indented the code in the middle to make it stand out from the defun line and the closing parenthesis.
This is standard among programmers; the indents make it easier to read code. Save the file with the name label. Assuming you saved label. Adding label. Add the name of the program:.
Start BricsCAD and it should load label automatically. There are two solutions. Or steal the value stored in system variable LUPrec — the precision specified by the user through the Units command — under the not necessarily true assumption that the user wants consistent units.
The code to do this is as follows:. That was the easy part. The tough part is applying the precision to the x,y,z-coordinates, which takes three steps: 1 pick apart the coordinate triplet; 2 apply the precision factor; and 3 join together the coordinates. The exclamation mark forces BricsCAD to print the value of variable xyz, which holds the x,y,z-coordinates.
Your results will differ, depending on where you picked. LISP has several functions for picking apart a list. Here you use the car and cdr functions, and combinations thereof. The car function extracts the first item the x-coordinate from a list. Try it now: : car xyz 6. You use variable PtX to store the x-coordinate, PtY for the y-coordinate, and so on.
0コメント