AutoLISP for BeginnersLet us learn AutoLISP in plain English. You will be guided slowly to create your own AutoLISP program! 1. Your First AutoLISP Program: Zoom to Origin 2. Using AutoLISP Variable and Asking User Input 3. Using AutoLISP Program to label point coordinate 4. Labeling Coordinate with Easting and Northing in AutoCAD 5. AutoLISP Exercise : Create Regular Polygon by Defining Area 6. AutoLISP exercise: creating program to label coordinate 7. AutoLISP Exercise: Using Block and Conditional If 8. AutoLISP tutorial: system variable and conditional COND 9. Adding New Line in Multiline text 10.AutoLISP tutorial: Working with layers 11.Creating layer and styles with AutoLISP 12.Modifying objects: working with selection 13.Filter selection with selection set 14.Saving, using and managing your AutoLISP program 15.How to load AutoLISP program 16.Automatically execute command when open/create new file 17.Set default system variables in acaddoc.lsp 18.Adding AIA standard layers with LISP 19.How to: Create polygon in isometric drawing Your First AutoLISP Program: Zoom to Origin By Edwin Prakoso | Last updated: February 10, 2011 0 0 0 0 AutoLISP has been a popular customization for AutoCAD. Many people use it to extend AutoCAD capabilities, do things that vanilla AutoCAD can‟t. AutoLISP can also be useful to automate several process that usually need you use several tools, but with AutoLISP you may only need a few clicks. I‟m not good with AutoLISP, but if you are interested to learn it with we, you can keep reading this tutorial. If you are an AutoLISP guru, I will be happy if you correct any mistakes or if you suggest better solution. So let us start to learn slowly, by creating simple program. You can also refer to AfraLISP for more AutoLISP tutorial. It‟s an excellent resource! Creating AutoLISP Application An AutoLISP program can be created in notepad. It is a plain text, you only have to save it with file extension .lsp. However, AutoCAD itself has provided a Visual LISP editor. There are many functionalities you can use here, more useful than notepad. Let us use visual lisp editor instead of notepad. You can access visual lisp editor from manage tab, applications panel. AutoCAD will open visual lisp window. This window might not look fancy, and and the icons remind me of good old Windows 3.1 icons. However, it has many specific AutoLISP programming tools that can help us. Click new or access file>new to create a new AutoLISP program. AutoLISP Structure Before we start, let us see the common program structure below. (defun c:YourProgramCommand () WhateverYouWantAutoCADtoDo (princ) ) Define a Function (defun ()) AutoLISP will start with (defun c:ProgramCommand ()).5398>: 2000 The red fonts are the command we input during the zoom process. Because we want to zoom to origin. Your First AutoLISP Program: Zoom to Origin We are going to create our very first program. Most programmer will put the close parenthesis below. This program will zoom to the drawing origin. to see how it works. Defun stands for define for a function. This is what we do manually: 1. AutoCAD will run your program when you type ZO then enter at command line.0. we type 0. no matter which part of drawing we currently see. Examine what we do when we use the command. 3. parallel to open parenthesis. 2. or [All/Center/Dynamic/Extents/Previous/Scale/Window/Object] <real time>: _c Specify center point: 0. Command: „_zoom Specify corner of window. That is the tricky part. enter a scale factor (nX or nXP). Then AutoCAD will ask us which method you want to use. This will be easier for us to find the parenthesis pair. as long as they can find it. we type _zoom to activate the zoom tool. AutoCAD tool that can do this is zoom to center.0 Enter magnification or height <1753. So make sure it will be EASY FOR YOU to find it. . Most programming language will not care where is the close parenthesis. If you find this: (defun c:ZO ()) It means that we are defining ZO as a command. First. Then AutoCAD will ask us the center point. We will do a simple exercise here. you define whatever you want AutoCAD to do. Inside the parenthesis. We type _c to use zoom center. With 0. Adding (princ) will end the program gracefully. If you use imperial. you can use (princ) or not using it.4. This time. Each input in quote. click load active edit window. The last thing AutoCAD will ask is the magnification.0 [enter] 2000 [enter]. do not copy and then paste it. Type it. Now in your Visual LISP editor. Now save this program. I use 2000. So we type _zoom [enter] _c [enter] 0. you may want to change it to smaller number. Type the distance you want to see on your screen. and 1000 to the right. It means AutoCAD will show 1000 to the left. .0″ “2000″) (princ) ) You know what‟s written in red means right? Command will load internal AutoCAD command. It will not work. but without it. Does it work? Congratulations! You have just created your first program! We will modify this program further. Load and Run Your Program In visual LISP editor. and try to type ZO then [enter]. (defun c:ZO () (command “_ZOOM” “_C” “0. Now move to AutoCAD window. then you give the parameters. it will also work. try to type like below. So save it and don‟t loose it. we press enter. Each time we give the input.0 at the screen center. 2011 0 0 0 0 We have started the AutoLISP tutorial by creating a very simple program: zoom to origin. So I changed the name to Zoom to Point (ZP). That program works. Remember to type ZP then [enter] to run it after this. or notepad ++ to create AutoLISP program. Because we will enable users to pick their own point. but it is very limited to zoom to 0. However. you basically can use notepad.0. Visual LISP Editor Again. Visual LISP editor can be very handy to find errors when our application become more complex. zoom to origin may not appropriate name anymore. We will add more functionalities so users can pick other point to zoom to.Using AutoLISP Variable and Asking User Input By Edwin Prakoso | Last updated: February 14. .0 coordinate only. I will not cover about it.If you want to learn more about it. then our program will be like this. the program name and user variables have to be unique and not using AutoCAD system variables. we haven‟t use it. which is very good. Change the 0.0 coordinate to this code: (setq ZPT (getpoint)) Now load the application again. but there is no instruction what should we do. you can read visual lisp tutorial on AfraLISP. In our previous sample. We both know that the program want us to click a point. we add these following lines before our command. Using Variables In programming. (setq ZPT '(0 0)) (setq MRAT 2000) Now let‟s put it all together in our program. they will be confused! So let‟s fix the code. and focus more on how you can write the routines. then try to activate it again. we use variables to save our values. (command "_ZOOM" "_C" "0. Now our program become like this.0" "2000") Let‟s say we save the coordinate to ZPT (zoom point) and save the magnification to MRAT (magnification ratio). doesn‟t it? But there is one thing left. To set the ZPT value and MRAT value. It works. Does it work? Asking for User Input Now we are going to ask the user to define their point. (defun c:ZP () (setq ZPT '(0 0)) (setq MRAT 2000) (command "_ZOOM" "_C" ZPT MRAT) (princ) ) Try to type it in your visual LISP editor and try it. at least now. So how to tell AutoCAD the variables value? You can assign the value to variables using setq function. (command "_ZOOM" "_C" ZPT MRAT) Remember. You can use any other names that can be easily recognized. If we give the program to other people. Load the program. . and run it. I added some comments like below. Do not load your program for now. If you have run it before. and will not be processed. modify it to this. Now AutoCAD doesn‟t keep the variable in your computer memory after the program has finished running. type !ZPT. Now you can see how the pretty colors can easily distinguish the codes right? See what the colors mean in this AfraLISP tutorial. close AutoCAD and open it again. You may want to leave the value as global variable if you do want to use it in other program. Type ZP then [enter] to test if your program hasn‟t loaded yet. what it does. and create or open a new drawing. (defun c:ZP (/ ZPT MRAT) Now load your program. who create it. You can give comments to your AutoLISP program using semicolon character. Giving Comments Most AutoLISP program have comments. it should mention nil. type !ZPT or !MRAT to see the variable value. It should say: Unknown command “ZP”. Nice… isn‟t it? Defining Local Variables Let us move to AutoCAD. But if you are not. After you run it. it is a good idea to set it as a local variable. It probably doesn‟t matter because we only have one AutoLISP program running and only has 2 variables. The idea is to give information when other people open the program. Press F1 for help. The exclamation mark in front of the variable name will show you the variable value. If it‟s not. It means that AutoCAD remember the value and use your computer resource. Now they should say nil. . But when you already have dozens of AutoLISP program running with many variables. If you haven‟t run the program. In defun line. Close your drawing. Let us complete it so it would looks like a real program. In AutoCAD command line. it will show the last coordinate you picked. it may affect your machine‟s performance. and you can also add some information what the codes in a line does. Or it doesn‟t have a value. Open Visual LISP editor.(setq ZPT (getpoint " Pick a point or type coordinate: ") Load and try again. Comments will be ignored by AutoCAD. variable . user input.This program will quickly zoom to a specific point .Created by: Edwin Prakoso .If you have a problem with the code. try to copy the completed code below.this will ask for user input (setq MRAT 2000) (command "_ZOOM" "_C" ZPT MRAT) .Zoom to Point . this will run the zoom command (princ) ) Filed Under: AutoLISP Tagged With: comments.com (defun c:ZP (/ ZPT MRAT) (setq ZPT (getpoint " Pick a point or type coordinate: ")) . .website: http://cad-notes. and paste in your Visual LISP editor. Download the LISP file 2. There are several ways to load AutoLISP program. If you move the UCS. How to use the program? 1. But what if you want to label the coordinate from UCS? Because I‟m currently learning AutoLISP. It is nice that you can create your own block. create your own block shapes. it will still use global coordinate. But there is a limitation. 2013 3 0 0 4 I have posted an AutoCAD tip how you can create your own label coordinate in AutoCAD using block attributes. then usually we do use global coordinate. 3. then I decided to take it as a challenge to create a program to do that. It makes sense. . because if we want to label our coordinate.Using AutoLISP Program to label point coordinate By Edwin Prakoso | Last updated: June 8. I wrote the code to run when I type LB then [enter]. but this is the easiest way. Load the LISP program. and customize it to look anything you want to. You should be able to use it after you load the program. It will only recognize the point position from global coordinate. You can download the file in link you‟ll find below this post. You need to click twice: the point you want to label and label location. So if it‟s too large. . The program will continue to ask you for points until you press [esc] or [enter]. . " y ". Y. LB. . download this file. change your style. There are two files in the zip file. 6. . Here is the code. LBZ. Thank you to Shawki! . too small. 1. and Z) Enjoy the LISP. 7. I realize that Shawki abo zeed have published similar code in labeling coordinate tips.lsp is for Labeling Coordinate (X. 5. . So you don‟t need to reactivate it after you have labeled one point.4. be patience! We will get there. or you want to customize it.lsp is for Labeling Coordinate (X and Y only) 2. " z)) (command "_LEADER" p textloc "" ptcoord "") ) ) And if you want to simply download and use it. It looks it has more features. change it in UNITS settings. If you are following AutoLISP tutorial in CAD Notes. I decide to make it this way because mostly we want to create several labels at once. If you want to change the format to decimal or architecture format. you may want to try his code too. It will use leader command. It is also use file UNITS settings.com Limitation ---------Will use current leader style and current units setting (defun c:lb (/ p x y ptcoord textloc) (while (setq p (getpoint " Pick Point: ")) (setq textloc (getpoint " Pick Label Location: ")) (setq x (rtos (car p))) (setq y (rtos (cadr p))) (setq z (rtos (caddr p))) (setq ptcoord (strcat x ". If this one doesn‟t work fine. . and share it to your friends! Notes: After I wrote this. Automatic coordinate labeling Edwin Prakoso http://cad-notes. . and so on. So I made minor adjustment to the code. then save it as “lb. . You can open notepad.Labeling Coordinate with Easting and Northing in AutoCAD By Edwin Prakoso | Last updated: January 23. copy and paste the code below. Or replace East with E or X=. and elevation instead of just the coordinate text. E. I had some questions if it can show N. If you want to show only Easting and Northing. This automatic labeling will create three lines of texts instead of just one line of xyz coordinate. you can delete the marked lines.lsp” (type it with double quote when saving in notepad). 2013 0 0 0 0 After I provided AutoLISP program to label coordinate automatically. Try this AutoLISP exercise to create labeling coordinate program here. .*you may delete this line (command "_LEADER" p textloc "" x y z "") . . then modify lines marked * (defun c:lb (/ p x y z ptcoord textloc) (while (setq p (getpoint " Pick Point: ")) (setq textloc (getpoint " Pick Label Location: ")) (setq x (rtos (car p))) (setq y (rtos (cadr p))) (setq z (rtos (caddr p))) . . I hope this is useful. " z)) . . Automatic coordinate labeling Edwin Prakoso http://cad-notes. . .com Limitation ---------Will use current leader style and current units setting If you don't want to show elevation.*you may delete this line (setq x (strcat "East " x)) (setq y (strcat "North " y)) (setq z (strcat "Elev. .*you may delete z ) ) Want to create this program by your own? You can also adjust it to your needs. . .You can read our tip on how to load AutoLISP program. 2011 1 0 0 0 I hope you are having fun with our AutoLISP exercises. Type in AutoCAD the line above.AutoLISP Exercise : Create Regular Polygon by Defining Area By Edwin Prakoso | Last updated: February 10. This time we will learn about asking for more user input. Interesting isn‟t it? writing Calculation in AutoLISP Writing calculation in AutoLISP is quite different. Yes you can use AutoLISP command in AutoCAD. If we want to calculate 2+5. then using the input in mathematical equation. Last time. The calculation result result will be used to draw an object. we were introduced to use AutoLISP variables and asking users for input. then we write: (+ 2 5) Let us try it in AutoCAD command line. . You need to type it exactly like above. Our challenge now is to create a program that can draw a regular polygon by defining number of sides and the area. But should not be difficult to understand. . (setq a 2) (setq b 5) (+ a b) What we did is set variable a value to 2. b to 5. Drawing Polygon Method How can we draw polygon by defining the area? I did some searches and find this formula. Not so difficult. We create it using circumscribed method. (setq c (+ a b) This one means c = a + b. then calculate a+b. source: Math Open Reference page. Now let us see the polygon formula. What is apothem? See image below. Type each line then press [enter]. isn‟t it? Refer to other calculation function in AfraLISP site here. If we know the area and number of sides. How can we draw a polygon when we know the apothem and number of sides? Using polygon tool of course.Now let‟s try another one. we can calculate the apothem. 3. 2. (setq apt (sqrt (/ (/ a (/ (sin(/ pi n)) (cos(/ pi n)))) n))) Asking For User Input . The bad news is AutoLISP doesn‟t have tan function. division as (/ a b). sine of an angle as (sin ang). But there is work around. If you want to try writing the equation by yourself. multiplication as (* a b). It can be a good exercise to get familiar with mathematical function in AutoLISP. 4. With a. The complete equation can be written. b. go ahead. and ang to keep (pi/n). Tangen is sin/cos. You can find the equation below later. cosine of an angle as (cos ang). (setq ang (/ pi n)) (setq apt (sqrt (/ (/ a (/ (sin ang) (cos ang))) n))) Or you can write in a single line. 1. so we will just use it. which looks more confusing. square root can be written as (sqrt a). 5. Tan (pi/N) can be written as: (setq ang (/ pi N)) (/ (sin ang) (cos ang)) Pi is built in variable.Calculating Apothem Length We need to change the formula to get the apothem like below: Functions in Our Formula We will use these function in our formula. Besides the sequence. I use variable a for area and n for number of sides. I also use apt to keep the equation value (the apothem length). you should be familiar with the command. and ang are variables. 2 Requires an integer value. so it is a real number.com (defun c:pba (/ a n apt ptloc) (setq n (getint " Number of Polygon Sides: ")) (setq a (getreal " Expected Polygon Area: ")) (setq ang (/ pi n)) . type (GETINT) type integer number. We can use GETREAL for this purpose. area. You need to ask the user the number of polygon sides. 3. You don‟t accept 2. What will AutoCAD say? Command: (getint) 3. This LISP will create regular polygon by defining polygon area and number of sides . 2. 3. You already use GETPOINT to ask for a point right? Let us try. 1. If you have problem. don‟t you? We can use GETINT to ask for integer. It should return the number you entered. Then you calculate the value.Let‟s say 3. Number of sides is an integer. 4. Now we need three user input: number of sides. I strongly suggest you to try writing the program first. Or they can type the coordinate.2. This time type a decimal number. . Created by Edwin Prakoso . the expected polygon area. Using the right user input function will also reduce probability of errors. Now we have complete data. what we will put in our program. and center point. Below is the complete code I created. Website: http://cad-notes.5 as number of sides. In AutoCAD command line. Try again. 2. you can copy the code below. Finally you can write the polygon command. Area can have decimal numbers.We already use getpoint to ask user to pick a point. You can check and compare the code later. Writing the Complete Code Now we can write the complete code. 1. You need to ask one more input: the polygon center. We are going to create a program to place coordinate label for a point automatically. So you should already familiar with this code: (defun c:\cnlabel () (setq p (getpoint " Pick Point to Label: ")) .(setq apt (sqrt (/ (/ a (/ (sin ang) (cos ang))) n))) . We already learned how to ask for user input for a point. It will require us to define two points: point to label. See the program and try it first if you want. Let us see what are we going to create. Next. we will continue our AutoLISP tutorial. asking user to click text location .calculating apothem for circumscribed polygon (setq ptloc (getpoint " Pick Location: ")) (command "_POLYGON" n ptloc "C" apt) ) How are you doing so far? I would like to know what do you think after you have done the exercises. we are going to create an AutoLISP program to label coordinate. 2011 0 0 0 0 In this exercise. and where we want to place the label. asking user to click point to label and save it (setq textloc (getpoint " Pick Text Location")) . Program Basic We are going to use leader tool to label it. This time we are going to use lists and strings. AutoLISP exercise: creating program to label coordinate By Edwin Prakoso | Last updated: February 9. We have learned how to use mathematic equation in AutoLISP to calculate a value. This time we will work with strings. After you load it. Again. 3. we can press [enter]. 5. Leader will ask for another input for the next line. Next. In AutoLISP program we can use point coordinate we get from user. We are going to use text location (textloc variable) as second point. We don‟t want to add next line. we need to place first point. we simulate it using “”. The program can be used.(command "_leader" p textloc "" p "") and use the input ) . we can press [enter]. 1. Then it will ask next point. To finish it. but would like to use Multileader? Simple. type cnlabel to activate the tool. activate label tool In visual LISP editor. 4. This line below will do what is described above. We can‟t control the text appearance. use this line: (command "_mleader" p textloc p) Try to use it first if you want to know how mleader works. If we only want one segment. Using Leader tool Let‟s see how the leader tool works. . we simply use “”. The first point will be the point we label. (command "_leader" p textloc "" p "") What if you don‟t want to use leader. but the result is not good enough. 2. After we activate leader tool. the label will be shown similar like below. It will ask for another point for the leader. pay attention on each step what you should do. To simulate [enter] in AutoLISP. you can copy or type it (I suggest you to type it for your exercise) then click load active edit window. we type the text. Unless you work in 3D. caddr will get the third value from the list. we continue the line like this. and z value we can use car. we can do this: (car p) To save the value to x. we don‟t need to use z value. car will pick the first value from the list. so the value should be still stored even after we run the program. Converting Real to String .See how many decimal numbers it has? Getting Value from The List Let us try the AutoLISP program. cadr. we can write lines as below: (setq x (car p)) (setq y (cadr p)) (setq z (caddr p)) When we work on 2D drawings. (setq x (car p)) cadr will get the second value from the list. We haven‟t define p and textloc as local variables.0) The value is a list. So to get x value. type !p to see the p value. To separate all the values. and caddr. y. To get the x.782 34. It should return something like this: (268. so you can delete the last line.178 0. Now after you run it once. You may change the text inside the double quotes. Now let‟s put it all together: (defun c:cnlabel () (setq p (getpoint " Pick Point to Label: ")) (setq textloc (getpoint " Pick Text Location")) (setq x (rtos (car p))) (setq y (rtos (cadr p))) (setq ptcoord (strcat "x=" x ". but they are still in real number. Just like when we place a leader or even a simple line. like x=….We already get the x and y value. y = 50 I can create it by combining x and y like this: (setq ptcoord (strcat “x= ” x “. We can convert them to string using rtos function. We can add more texts to those strings. y=… then we need to convert them to string. N=…. But in strings. we use strcat to add more text to the variable. In calculating real or integer. el=… etc. . ” “y= “ y)) Don‟t forget to save it to a variable! I use ptcoord. we can use mathematic function like + or –. " "y=" y)) (command "_leader" p textloc "" ptcoord "") (princ) ) Tips: Seeing Dynamic Lines from Previous Point One annoying thing about this program is we can‟t see dynamic line when we place the second point. Let us add it to our code. You may want to use E=…. (setq x (rtos (car p))) (setq y (rtos (cadr p))) Now x and y are strings. Let say I want it to look like this: x = 100. If we want to add more text. and I believe many more things can be done. . we can make this program to behave the same. The idea is we can keep using it and don‟t have to reactivate it when we still want to use it. but now this will do. Easting and elevation in multiple lines. More Tips: Adding a Loop to Keep the Program Active When we use line tool. " "y=" y)) (command "_leader" p textloc "" ptcoord "") (princ) ) . I would like to know what program have you made until this point? Please share with us! Next.start while (setq p (getpoint " Pick Point to Label: ")) (setq textloc (getpoint p " Pick Text Location")) (setq x (rtos (car p))) (setq y (rtos (cadr p))) (setq ptcoord (strcat "x=" x ". Now try it. The variable is p. we are going to modify it a bit further: You will create AutoLISP program to label coordinate showing Northing. so now the line become: (setq textloc (getpoint p ” Pick Text Location”)) Add it and try it again.end while ) We will cover more about loop. I‟m amazed how many thing I can do with a basic knowledge of AutoLISP. To do this. the tool will be still active until we press [esc] or [enter]. we can add a loop with (while). So the complete program will be like this. we simply add the variable of the first point when we ask the second point.To add the dynamic line. Because when labeling coordinate we usually need to label several points. Some other tools also have the same behavior. (defun c:cnlabel (/ p x y ptcoord textloc) (while . We will create a program to label elevation.AutoLISP Exercise: Using Block and Conditional If By Edwin Prakoso | Last updated: February 9. We can have more geometries without having to draw them and also can place the text to block attributes. We had a good time when creating label for coordinate. But this time we will also use block. Before We Start: Preparing the Block File . 2011 0 0 0 0 This time we are going to work with AutoCAD block. Using block has several advantages. Now we are going to create a similar AutoLISP program. See what I‟m talking about? We are going to use that. it will open insert dialog box. Now we need to set the location as AutoCAD support file search path. This is the block we are going to use as elevation label. That will not work with our AutoLISP program.Before we start. try to type –INSERT then [enter]. AutoLISP will only work if we give input in command line or without dialog box. You may have different location if you want to. I save it to D:\acadlib. Add the folder location here. we need to add – (dash) in front of the command. To prevent AutoCAD to load the dialog box. Command We will Use Easy enough to guess: we are going to use INSERT to insert our block. Save the file to your library folder. . you need to download this AutoCAD file. Open AutoCAD option. If you try to type INSERT then [enter] in AutoCAD. We don‟t want that. When you need to place it to a drawing with 1:100 scale. And we can use it for other LISP application. We will do it later in this AutoLISP tutorial. In my country. many people like to use + 0. Like in previous tutorial. you can see it works nicely. AutoCAD will load the file from that location. then it will search and load DWG name with the same name as the block. Because we don‟t want the program to ask user for scale every time they use it. then we will use conditional IF. The rest is easy. The next thing is change the label value when it‟s at 0 elevation. Then insert the DWG as block. don‟t we? So now we will add one more variable. We will add an if conditional to change the value. That is why we define the file location in default search path. things to do if false)) I added conditional IF twice: (if (= cnglobalscale nil) (setq cnglobalscale (getreal " Set Global Scale for CN Annotation <1/1>: ")) . So AutoCAD will still recognize the variable after the application ends. Add Ability to Change Scale If you examine the block.Insert block will insert a block with the name we defined in our AutoLISP program. The structure of conditional IF is (IF (statement) (things to do if true) (else. So our basic program can be like this: (defun c:cnannolevel (/ labelposition y) (setq labelposition (getpoint " Pick Label Position: ")) (setq y (cadr labelposition)) (setq y (rtos y)) (command "_-insert" "annolevel" labelposition 1 1 0 y) ) More Consideration If you already tried it. If it can‟t find it. But you may want to consider to allow AutoCAD users to change the block scale.00 when it‟s at zero elevation. then you need to scale it after placing it. This time we will let the variable become global variable. you will soon know that it was created in full scale 1:1. we can get the elevation value from the coordinate list and insert it to the block attribute. we use conditional IF. Instead of using (setq y (rtos y)) .) (if (= cnglobalscale nil) (setq cnglobalscale 1) ) The code in plain English 1. Change Text in Zero Elevation As I mentioned before. Just like before. Because the user don’t provide value. then the scale still NIL.We assume that they want to use default scale 1:1. we only ask for scale one time. That’s why we provide 1/1 in the bracket. when the global scale is NIL. Remember. we here would like to have + 0. 2. it will use full scale. The second conditional will check if the user don’t give any input and simply press *enter+. If it’s true it will ask the user for input. The first conditional will check if the globalscale is not set (the value is NIL).00 than just 0 at zero level. So we need to provide them a way to change the scale manually later. Now the program become like this: (defun c:cnannolevel (/ labelposition y) (if (= cnglobalscale nil) (setq cnglobalscale (getreal " Set Global Scale for CN Annotation <1/1>: ")) ) (if (= cnglobalscale nil) (setq cnglobalscale 1) ) (setq labelposition (getpoint " Pick Label Position: ")) (setq y (cadr labelposition)) (setq y (rtos y)) (command "_-insert" "annolevel" labelposition cnglobalscale cnglobalscale 0 y) ) (defun c:cnannoscale () (setq cnglobalscale (getreal " Set Global Scale for CN Annotation: ")) ) I added one more function at the bottom so users can change the scale anytime. This is common in AutoCAD tools to offer user to use default value. This is simple. We tell user that if they don’t give any value. it will use the original value. But the problem is the label can‟t be updated automatically. this will change text when in zero elevation (command "_-insert" "annolevel" labelposition cnglobalscale cnglobalscale 0 y) (princ) ) (defun c:cnannoscale () .00") (setq y (rtos y))) The code will check if Y value is zero. . and replace it with + 0. If it‟s not.We use (if (= y 0) (setq y "%%p 0. We will cover how to update the value later when we cover entity selection. Final Code After we added those features. this another function defined to enable user to change scale later (setq cnglobalscale (getreal " Set Global Scale for CN Annotation: ")) ) This program works nice.com annotation utilities .00") (setq y (rtos y))) . by: Edwin Prakoso (defun c:cnannolevel (/ labelposition y) (if (= cnglobalscale nil) . then this is the complete code: . this will check if user already has defined the drawing scale (setq cnglobalscale (getreal " Set Global Scale for CN Annotation <1/1>: ")) ) (if (= cnglobalscale nil) . CAD-Notes. this will check if the user choose default value (setq cnglobalscale 1) ) (setq labelposition (getpoint " Pick Label Position: ")) (setq y (cadr labelposition)) (if (= y 0) (setq y "%%p 0.00. Not like using fields. 2011 0 0 0 0 In the last AutoLISP tutorial. we have tried to place block and change the attribute value. We can use our reusable content to make AutLISP programming less complex. Have fun! AutoLISP tutorial: system variable and conditional COND By Edwin Prakoso | Last updated: January 23. This time we will try to use another conditional. and automatically place label at the end of line. one of the most important thing in AutoLISP. We will also touch AutoCAD system variables. and we can maintain our drawing standard in AutoCAD. using COND. Using AutoCAD block in AutoLISP is a great benefit. . We also use conditional IF. The program we will make in this AutoLISP tutorial will draw grid line.This is how our program will work. The basic of using COND is: (cond ((condition a) (things to do when condition a is met) ((condition b) (things to do when condition b is met) ((condition c) (things to do when condition c is met) and so on… ) Let‟s see what can we do with COND in our case. do B. COND will only do things when the condition is met. you can tell AutoCAD to do A when condition is met. When using IF. Or if you completely new to AutoLISP. you need to download this file first. each file is a grid label for each direction. So if you haven‟t read it or want to refresh your memory. follow the tutorial from beginning here. Using Conditional COND COND is a bit different with IF. read the AutoLISP tutorial here. Or else. The zip file has 4 AutoCAD DWG files.Before you start. We will use conditional COND to choose which AutoCAD block we should use.change block name when meet the condition ((AND (= pt1x pt2x) (< pt1y pt2y)) (setq gridblockname "vgridtop")) . This AutoLISP tutorial requires you to understand what we did previously. (cond ((AND (= pt1x pt2x) (> pt1y pt2y)) (setq gridblockname "vgridbottom")) . there are two conditions are met: 1. if you feel the variable name is ridiculously long. Y2 is the 2nd point picked by user. Y1 is the first picked point. 2. To achieve that. we don‟t have to define it because we already set it as default block name. we need to draw line with ortho mode is on. we set the block name to HGRIDRIGHT. It will check the drawing scale. We define the three conditions in our program. This behavior is controlled by system variable ORTHOMODE. For right direction. Changing AutoCAD system variable Another thing we need to do is change an AutoCAD system variable temporarily. and X2. We need to save the current ortho mode first to a temporary variable. We will use this block when we draw grid line to the right. OK. we need to use block VGRIDBOTTOM. we set ORTHOMODE system variable to 1. (setvar "orthomode" CurrentOrthomode) The complete AutoLISP program Now we have everything we need. X1 and X2 are the same. 2. . (setq CurrentOrthomode (getvar "orthomode")) (setvar "orthomode" 1) The first line will get the system variable and save it to variable CurrentOrthomode. We want to create the grid horizontal or vertical. We need to change it to 1 to make ortho mode active. We can get the system variable and save it to user variable in AutoLISP using GETVAR. We don‟t want the program to change our AutoCAD behavior and we need to change option every time we use the program. Refer to previous tutorial to see how it works. We can do it in AutoLISP using SETVAR. But before we change it. we can restore the system variable to the original value. After our AutoLISP program ends. you can choose your own name :) The second line. When we draw a grid to bottom.((AND (> pt1x pt2x) (= pt1y pt2y)) (setq gridblockname "hgridleft")) ) First. it is a good thing to restore the system variable after the program ends. It will save the current orthomode and change orthomode to 1. But when we draw it to bottom. Y2 is larger than Y1. X1. The AutoLISP program will run like this: 1. 5. end if ) Error trapping To complete this AutoLISP program. It will ask user to place two points for the grid. We change an AutoCAD system variable: ORTHOMODE. this will check if the user choose default value (setq cnglobalscale 1) ) . It will check which it should use. 6. it is a good thing that you create an error trapping. It will draw grid line and insert the block. The variable will not be restored when you press esc key! . when an error occur and the program ends prematurely.change grid name when meet the condition ((AND (= pt1x pt2x) (< pt1y pt2y)) (setq gridblockname "vgridtop")) ((AND (> pt1x pt2x) (= pt1y pt2y)) (setq gridblockname "hgridleft")) ) (setq gridnumber (getstring " Enter Grid Number: ")) (command "_line" pt1 pt2 "") (command "_-insert" gridblockname pt2 cnglobalscale cnglobalscale 0 gridnumber) (setvar "orthomode" CurrentOrthomode) (princ) ) (defun c:cnannoscale () . We do set it back to original value. The complete AutoLISP code is like this: (defun c:cngrid (/ pt1) (if (= cnglobalscale nil) (c:cnannoscale) ) .set default grid name (cond ((AND (= pt1x pt2x) (> pt1y pt2y)) (setq gridblockname "vgridbottom")) . the system variable is not restored. The problem is. this another function defined to enable user to change scale later (setq cnglobalscale (getreal " Set Global Scale for CN Annotation <1/1>: ")) (if (= cnglobalscale nil) . 4. It will restore the orthomode. end if (setq CurrentOrthomode (getvar "orthomode")) (setvar "orthomode" 1) (setq pt1 (getpoint " Pick Grid Start Point: ")) (setq pt2 (getpoint pt1 "nPick Grid Last Point: ")) (setq pt1x (car pt1))(setq pt1y (cadr pt1)) (setq pt2x (car pt2))(setq pt2y (cadr pt2)) (setq gridblockname "hgridright") .3. . But using “” in AutoLISP to simulate pressing enter. The problem is it uses multiline text. MLEADER is neat. When we use “” AutoCAD thinks we want to end the command. But you may want to use multileader instead of leader in your AutoLISP program. Adding New Line in Multiline text By Edwin Prakoso | Last updated: April 28. When working with multiline text. 2011 0 0 0 0 We created an AutoLISP program to create leader to label coordinate before. and you can have more control and flexibility with it. But not adding new line. Many problem can happen if you don‟t set error trapping in an AutoLISP program! You can read it in AfraLISP about error trapping here. we press [enter] when we want to add another text line.This is what happen in this selection issue and the missing dialog box. It will be very useful for surveyors who use vanilla AutoCAD. it will not work. not single line text in leader. To solve this.asking user to click text location (setq x (rtos (car p))) (setq y (rtos (cadr p))) (setq ptcoordN (strcat "N=" y)) (setq ptcoordE (strcat "E=" x)) (setq ptcoordN (strcat ptcoordN (chr 10) ptcoordE)) (command "mleader" p textloc ptcoordN) . chr 10 will add new line (or line feed) to our variable. we need to use ANSI character to add a new line.ativate label command and use the input (setvar "LUPREC" oldprec) (princ) ) ) I don‟t know if there is other solution for this. start while loop (setq p (getpoint " Pick Point to Label:")) . That AutoLISP code will combine N coordinate. asking user to click point to label and save it (setq textloc (getpoint p " Pick Text Location:")) . If you know the other way.I posted a reply in the comment section. new line. The 3rd line will combine them both. then add prefix. We add that character to our string variable: (setq ptcoordN (strcat "N=" y)) (setq ptcoordE (strcat "E=" x)) (setq ptcoordN (strcat ptcoordN (chr 10) ptcoordE)) The first and second line will get x and y value. but just in case you miss it I write it as a post. The complete AutoLISP code will be: (defun c:cnlabel (/ p x y ptcoordN ptcoordE textloc oldprec) (setq oldprec (getvar "LUPREC")) (setvar "LUPREC" 4) (while . Let‟s take an example. please share it here! Filed Under: AutoLISP . then place E coordinate there. In system variable and COND conditional.AutoLISP tutorial: Working with layers By Edwin Prakoso | Last updated: April 21. but also to maintain our standard. . This will ensure the users (or you) to use the same block for every drawing. 2011 0 0 0 0 An AutoLISP program doesn‟t have to be difficult. We can create simple LISP program and get many benefits from it. Let‟s explore the possibility to use it to control our drawing standard. Now let‟s see how to control the other properties you need to control: layers and styles. It‟s not just about speed up the drawing creation. we discussed how we can use block to create grids. We did it before here. 4. (setvar "CLAYER" "LAYERNAME") Sample: creating text Now let‟s try an example. Changing the current layer We can change the current layer using setvar.Working with layers Layers are the most important thing to manage our drawings. CADD manager has a short article about the concept. change layer to annotation layer . Get current layer and save it to a variable. Set current layer to a different layer. You may want to consider ask for height and rotation angle. 2. annotation layer. We ask for required data for creating text. But it’s not necessary in this sample. We want to create a LISP program to create text. In this sample. and use a default layer. If you‟re not familiar with layer standard. This is the code after we put it all together: (defun c:cntext () (setq oldlayer (getvar "CLAYER")) . Current layer variable is controlled by CLAYER. Set the current layer to previous layer. This is what we do: 1. get current layer (setvar "CLAYER" "A-Anno") . In this sample we ask for text content and position. 3. create the text (setvar "CLAYER" oldlayer) .then do this if exist (if flag (progn .restore active layer ) Check for existing layer It should works.end statement group .(setq textcontent (getstring " Type Text: ")) (setq textloc (getpoint " Text Position: ")) (command "_TEXT" textloc "" "" textcontent) . But we still have a problem. Let‟s see the complete code below: (defun c:cntext () (setq flag (tblsearch "LAYER" "A-Anno")) . .looking for A-Anno layer . error: AutoCAD variable setting rejected: “clayer” “layername” We need to check if the layer exist or not first. So we need to add (progn GROUP OF STATEMENTS) to run several statements.grouping statement (setq oldlayer (getvar "CLAYER")) (setvar "CLAYER" "A-Anno") (setq textcontent (getstring " Type Text: ")) (setq textloc (getpoint " Text Position: ")) (command "_TEXT" textloc "" "" textcontent) (setvar "CLAYER" oldlayer) ) . What if the layer doesn‟t exist? AutoCAD returns error warning. We can do it by using this code: (setq flag (tblsearch "LAYER" "A-Anno")) Then add conditional IF like this: (if flag (PUT WHAT YOU WANT AUTOCAD TO DO HERE) (ELSE DO THIS) ) We have discussed about using conditional IF in AutoLISP before. The problem is IF only allows you to run one statement. else .") ) . end if ) Now AutoCAD will try to find the layer first before executing our command. Ask the user to create it first. then it will give a warning. If it doesn‟t exist. But what we did before is simply change it. By changing current layer or style in the program. Create it first to maintain standard. If the layer exists. then it will run our program. For example. 2011 0 0 0 0 We learned how we can work with layers when we create objects in our AutoLISP program. If it doesn‟t find it in name list. we place it on annotation layer. This is a good way to be implemented with your company standard.and give warning if it doesn't exist (alert "No A-Anno layer. it will make sure our objects to use specific properties. . other alternatives to deal with non-exist layers. credit: this article was written based on this AfraLISP tutorial. we want when we place the grid label. That‟s not cool. Creating layer and styles with AutoLISP By Edwin Prakoso | Last updated: May 27.. it simply gives a warning. We will see in the next tutorial. There are several possibilities to handle this situation. So we can write it as a LISP program below: (command "_layer" "m" "NEWLAYER" "") . So we press [enter] to end the command.The more logical thing to do is. That‟s how we use it with AutoLISP. -Layer is the command. You will see the list of available options. Type –layer on command prompt. We use LAYER command. Use – (dash) to load the command without dialog box. Remember. when it doesn‟t find it. Command: -layer Current layer: “0″ Enter an option [?/Make/Set/New/Rename/ON/OFF/Color/Ltype/LWeight/TRansparency/MATerial/Plot/Fre eze/Thaw/LOck/Unlock/stAte/Description/rEconcile]: m Enter name for new layer (becomes the current layer) <0>: newlayer Enter an option [?/Make/Set/New/Rename/ON/OFF/Color/Ltype/LWeight/TRansparency/MATerial/Plot/Fre eze/Thaw/LOck/Unlock/stAte/Description/rEconcile]: The bold text with red color is what we‟re going to use in AutoLISP. M will create new one. you can check the command we can use at command line first. it creates a new one. it will ask you for it’s name and then make it current. Creating a new layer Creating new one is easy. This command will keep asking you to enter an option. The good thing about it is.") ) .grouping statement (setq oldlayer (getvar "CLAYER")) (setvar "CLAYER" "A-Anno") (setq textcontent (getstring " Type Text: ")) (setq textloc (getpoint " Text Position: ")) (command "_TEXT" textloc "" "" textcontent) (setvar "CLAYER" oldlayer) ) . end if ) With much simpler code like this defun c:newla () (setq oldlayer (getvar "CLAYER")) . Create it first to maintain standard. . We can replace this code we made before: (defun c:cntext () (setq flag (tblsearch "LAYER" "A-Anno")) .looking for existing .get the current first (command "_layer" "m" "newtest" "") .and give warning if it doesn't exist (alert "No A-Anno layer. So we don‟t have to add code to check if it already exist.Easy. It also can create new style and use it as current. right? This will create new layer named NEWLAYER (if there is no newlayer in the list) and make it current. if there is existing layer with that name. Let examine in command line how the sequence is.end statement group .then do this if exist (if flag (progn . it will accept it and set make it current.create and change current (setq textcontent (getstring " Type Text: ")) (setq textloc (getpoint " Text Position: ")) (command "_TEXT" textloc "" "" textcontent) (setvar "CLAYER" oldlayer) ) Working with text styles and dimension styles So what about text styles and dimension styles? Text Styles What about text styles? We can use style command.else . Specify full font name or font filename (TTF or SHX) <txt>: arial Specify height of text or [Annotative] <0. There are 7 options AutoCAD will ask you. It ignores the other questions and accept default values.5 and width factor 0. Or if it doesn’t exist. 2. and AutoCAD will ask one more question. Another example. we need to use conditional IF. You can use this line to create text styles (command "-style" "newstyle" "arial" "" "" "" "" "") That code will create a new text style with name newstyle and using arial.5 0.ttf as it‟s font. It asks us to confirm if we want to redefine the existing.Command: -STYLE Enter name of text style or [?] <Standard>: newstyle New style.0000>: Specify obliquing angle <0>: Display text backwards? [Yes/No] <No>: Display text upside-down? [Yes/No] <No>: “newstyle” is now the current text style. We need to use save option in dimstyle to create a new one.85 0 "N" "N") * Note: if you use shx font. Dimension Styles Unfortunately when working with dimstyle. SHX fonts support vertical alignment. The problem is when we use save dimstyle.0000>: Specify width factor <1. this will create a style with height 2. 1. it will ask one more question. Let‟s see this sample. if you want to make it vertical. you need to press [enter] one more time.then do this if exist (if flag (command "-dimstyle" "R" "NEWDIMSTYLE") (command "-dimstyle" "S" "NEWDIMSTYLE" "") . we need to use restore to change the current dimension style. and AutoCAD find it already exist. (command "-style" "newstyle" "arial" 2. So we use this code: (defun c:tst () (setq flag (tblsearch "dimstyle" "newdimstyle")) .looking for dimstyle .85. etc. end if ) Changing the properties You already know how to create a text style and set it‟s properties. because they‟re asked when you create a new one. 2011 0 0 0 0 We covered several topics on how we can draw more efficiently using AutoLISP. Now it‟s time to move on. specific linetype. we will cover how we can change the properties of layers and dimenion styles. etc) AutoCAD will ask us to select object. The most important thing about editing objects is to select them. copy. . As we know. We are going to learn how we can modify existing objects in our drawing. lineweight. Modifying objects: working with selection By Edwin Prakoso | Last updated: June 21. Next.). So we can create it with red color. After we activate an AutoCAD modification command (like move. AutoCAD will continue to ask you to select object. Then you can add it to move command. (defun c:mleft () (setq sel1 (ssget)) (command "move" sel1 "" "0. Sample 1: Move to left This sample program will allow you to select object and move them 5 unit to left. Try to use CHROP to understand what we‟re doing below.Saving selection to a variable The function that allow us to do this is SSGET.0" "-5.0. If you don‟t add it. Then you can choose which properties you want to set to ByLayer. is having the objects‟ properties overridden. Sample 2: Reset properties to ByLayer One thing that can annoy us when trying to apply CAD standard. (defun c:rst () (setq sel1 (ssget)) (command "CHPROP" sel1 "" "COLOR" "BYLAYER" . Set to other than ByLayer.0") ) SSGET will save your selection to sel1 variable.0. You can change object properties by using CHPROP. Remember. you need to add “” to end selection. For window and crossing polygon selection. Or limit the method for user to use."LWEIGHT" "BYLAYER" "LTYPE" "BYLAYER" "") ) Using other selection mode Sometimes we need to select objects without asking users to give input. all objects in the drawing. setq sel1 (ssget "l")) setq sel1 (ssget "p")) setq sel1 (ssget "x")) to select last object‟s created. Other code that we can use are: setq sel1 (ssget "c" '(0 0) '(10 10)) to select object inside and touching rectangular window (crossing polygon).10. we can also ask for user‟s input. to select objects in previous selection.0 to 10. . If you‟re not familiar with the selection mode above. Using the code above will let users to use AutoCAD default mode. read using AutoCAD selection below. You can use these code to select object from within the program: (setq sel1 (ssget "w" '(0 0) '(10 10))) That code will select all objects inside rectangular window from 0. For example: (setq pt1 (getpoint " Pick first point: ")) (setq pt2 (getpoint " Pick other corner point: ")) (setq sel1 (ssget "c" pt1 pt2)) We will expand this selection using filter. We can define which kind of object we want to select. See all LISP function that you can use to expand your LISP program. Selection sets.If you have a simple example that we can use in this exercise. This time we will extend the object selection by using selection filter. We can define objects with specific properties to select. I will add your code here. Here are the list how you can select objects in AutoCAD. and you will be credited. 2. please let me know. Using AutoCAD selection. Filter selection with selection set By Edwin Prakoso | Last updated: June 28. Good explanation by Kenny Ramage on AfraLISP. 2012 0 0 0 0 In the last tutorial. Using filter in AutoLISP is very similar with using AutoCAD filter. we learned how to use object selection in AutoLISP. AutoLISP function list. Further readings: 1. . 3. END PROGRAM With that simple code. SET ACTIVE LAYER TO PREVIOUS (command "CHPROP" sel1 "" . we can use this code: (setq sel1 (ssget "x" '((0 . we can create a program to run and select dimensions in drawing. "DIMENSION")))) The complete code become: (defun c:dimla (/ sel1 CLAYER ) (setq sel1 (ssget "X" '((0 . .For example.CREATE NEW LAYER (setvar "CLAYER" OLDLAYER) . but can help you to maintain drawing standard. right? You can also modify it to allow user to check the selection visually before move dimensions to other layer. To filter the selection.GET CURRENT LAYER (command "_layer" "m" "ANNO-DIMENSION" "") . Very useful. "DIMENSION")))) . This kind of program probably very simple. We will create this program in this tutorial. CHANGE DIMENSION LAYER TO NEW LAYER "LAYER" "ANNO-DIMENSION" "") ). and move it to annotation layer. As we did before. we can select all object using this line. SELECT ALL DIMENSION (setq OLDLAYER (getvar "CLAYER")) . (setq sel1 (ssget "x")) The Object filter Now we want to select all dimensions in our drawing. you can quickly find and move all dimensions to desired layer. "DIMENSION") . This code will only allows you to select dimensions. I won‟t cover it further here. read more about conditional operators of selection filter there. See the complete dxf associative code here. Saving. "ANNO-DIMENSION") - We use DXF codes with selection filters in AutoLISP. This code will only allows you to select objects on layer ANNO-DIMENSION. DXF code One more thing that you might want to know is the DXF code. using and managing your AutoLISP program By Edwin Prakoso | Last updated: August 5. Go ahead. (0 .Using DXF code 0 allows you to define object to select by objects type. 2011 0 0 0 1 .More about selection filter Afralisp covers this material in more advanced topic here. Using DXF code 8 allows you to define object to select by their layers. because they have detailed explanation about it. (8 . Or notepad will work. You should be able to use the programs. you‟ll find mostly the program is given to you written in code. In CADTutor forum. (defun c:cnlabel (/ p x y ptcoord textloc) (while . But don‟t use Microsoft Word or other Word Processing program.Andres Rodriguez – Fotolia. We know that we can find many AutoLISP program on internet now. Saving an AutoLISP program You may find people giving you LISP program written in code like this. Now it‟s time to manage them in AutoCAD.com We covered several basic AutoLISP tutorial already. but also for you who want to simply use AutoLISP program. You need to copy the code to your clipboard (by selecting them and pressing ctrl + c). " "y=" y)) (command "_leader" p textloc "" ptcoord "") (princ) ) .start while (setq p (getpoint " Pick Point to Label: ")) (setq textloc (getpoint p " Pick Text Location")) (setq x (rtos (car p))) (setq y (rtos (cadr p))) (setq ptcoord (strcat "x=" x ".end while ) You can use visual lisp editor to save it as a program. This is not only for you who want to learn AutoLISP. . Of course. then you will want AutoCAD automatically load it every time it starts. If you use Visual LISP editor.Paste your code there. . Go to AutoCAD ribbon> manage tab> applications panel.lsp” (with double quote to force notepad save it as it is). Click load application. Type “YourProgramName. then by default it will save your code as . hint: AutoCAD veterans use APPLOAD in command line In load/unload applications dialog. To load it automatically. Loading AutoLISP Program Now you have your LISP program. Before you can use it. change the blue text with your program name. browse and find your AutoLISP file you saved before. Save your file to a location that allow people easily access it. Save it as LISP program. But if you use notepad. add the LISP program to startup suite.lsp extension when saving your file. Select it and click load. then you must define the . Automatically load AutoLISP program when AutoCAD start If you use your LISP program repeatedly.lsp file. So you don‟t have to load it in every new AutoCAD session. you need to load it to AutoCAD. Just in case one day you don‟t want a LISP program to load automatically anymore. The less cool way to do it is by clicking contents button below startup suite briefcase. you know where to remove it now. right? Using AutoLISP program So how you can use the program? . Here you can add or remove LISP from startup suite.The cool way to do it is by dragging the file to startup suite. Again, if you copy it from internet, you can see on top of the program like this. DEFUN is defining the function. In this sample, the function can be loaded by typing DIMLA in command line then press [enter]. As simple as that. What you should do next, depends on your AutoLISP program. Placing AutoLISP in AutoCAD Interface If you‟re a command line freaks, then you can stop here. However, not all of us like using command line. You may want to have it on your ribbon (well uh, or toolbar/menu if you use classic interface). In ribbon panel/toolbar/menu Type CUI then press [enter] to load Customize User Interface dialog. Or click User Interface in customization panel. If you‟re not familiar how to create a command here, read this tutorial first. You have to make a command, change the command name and macro. The macro should be ^C^CDIMLA. Or whatever command you want to activate using this macro. ^C^C means you press esc twice to cancel all running command. After you‟ve done, drag the command to ribbon panel, toolbar, or menu as you like. In tool palettes What about tool palettes? Can we place the command there? Sure you can. You can use palettes to activate AutoLISP command too. The process is similar with adding action recorder command in this tutorial. What we learned Now you know how to save a LISP code, load it to AutoCAD, and use it. You also know how to use AutoLISP command from ribbon/toolbar. And even using from tool palette. So how do you use AutoLISP? Do you load it automatically? And do you use command line or place it to ribbon? How to load AutoLISP program By Edwin Prakoso | Last updated: October 14, 2011 0 0 0 0 Do you want to load an AutoLISP program? Here is a basic guide how to do it. Or you can type APPLOAD then press [enter]. you can load it by using load application in manage tab. . Double-clicking the file will also load the application. This method will load your application in current session only. You can simply drag and drop the application to startup suite icon as shown below. It means that when you close AutoCAD. then restart it. You will see load/unload applications dialog opened. you can add it to startup suite. You have to load it again. To add application to startup suite If you want AutoCAD to automatically load it every time you start AutoCAD. then you can consider to load it automatically in every AutoCAD session. You can do it by adding it to startup suite. Find your AutoLISP program then click load.To load application If you have an AutoLISP program. the application is no longer loaded. If you use the application frequently. To add it to startup suite is easy. yes. find your application and click open. If you notice the add button here. it‟s the other method to add application to startup suite. . In opened dialog box. click application you want to remove then click remove. click contents button below briefcase icon.To remove application from startup suite To remove it from startup suite. Drag and drop thing to add application is cool. Click it. There are more methods like described in AfraLISP here.There are some more advance technique. but I find this is the most convenient way for people who don‟t know much about customization like me. if you are interested :) . you will learn how to load AutoLISP program using acaddoc. It means we may send or plot files that use old value. You may use text editor or visual LISP editor to do it.lsp What is ACADDOC. We have to update it manually. I would recommend you to do the later.lsp. Creating acaddoc. you will also learn how to make AutoCAD automatically update datalink when we save or plot our files. . In this post. and optionally put your LISP to startup suite. it‟s not updated automatically. Getting started with acaddoc. AutoCAD will execute it every time we create or open a drawing file. The difference is.Automatically execute command when open/create new file By Edwin Prakoso | Last updated: October 25. We will define our own plot and save command. 2011 0 0 0 0 Last week we covered about loading AutoLISP file.LSP? In short. You may choose any existing path. it‟s also an AutoLISP file. or create your own support path. You learned how to load. If you want further explanation. In this post. We also had another post about excel datalink. Startup suite will load LISP automatically every time you start AutoCAD. and you can also save all your LISP file there. Jimmy Bergmark explain about ACADDOC. Save it to a support file path. The problem with datalink is.lsp Create a new LISP file.LSP in details here. Press F1 for help. Undefine and define PLOT function in acaddoc.lsp and zoomextend. We just undefine plot command. Loading AutoLISP command is something quite basic we can do with acaddoc. Undefine and redefine We can undefine AutoCAD command using UNDEFINE. Next. AutoCAD will not recognize it anymore.Loading AutoLISP program in acaddoc. . Try to type plot in command line and see how AutoCAD will response.lsp You can load AutoLISP program using this line: (LOAD "MYLISP") This sample below will load cnlabel. we will try to define our own command here. After we undefine PLOT. Like we discuss before. type UNDEFINE [enter] then PLOT [enter]. Command: plot Unknown command “PLOT”. we will change how AutoCAD command work. First.lsp. we need to undefine it.lsp In the next step. we will change plot and save command.lsp. In AutoCAD command line. Our code become like this: .lsp. isn‟t it? Regenerating model. then you will not see the dialog box.PLOT [enter]. This line below will undefine existing PLOT command (command "undefine" "plot") . You will need to setup the plot using command line. then define new command. You will see after I create a new file.Now try type . Plot command should work. This line below will define new PLOT command (defun c:PLOT ( ) (command "_DATALINKUPDATE" K) (initdia) (command ". our code will undefine plot. it updates datalink first.plot") ) We use INITDIA to load AutoCAD plot dialog. After we finished. The new plot command should update datalink before start to plot. Save this AutoLISP file. First we undefine PLOT. The dot means we use AutoCAD built in command. Then when we use PLOT command.undefine Enter command name: plot Command: Command: . then activate plot.lsp Now let‟s work with acaddoc. PLOT and . Notice the dot before PLOT command. AutoCAD menu utilities loaded. The last one use AutoCAD built-in plot command. Nice.lsp. Create or open a new file then activate plot. The first one will use plot we define in acaddoc.PLOT will give different result. You can activate PLOT command back using REDEFINE [enter] PLOT [enter] Defining function in acaddoc. If we don‟t use INITDIA before plot. But we haven‟t finished. We need to make sure datalinks in our files are updated before we send them. We need to modify save and saveas command.plot Update datalink when saving file Now we don‟t have to worry when we plot files. It surely will use most updated value from excel files. Discuss it here if you have problem. You can modify them as your exercise.lsp can be useful? Do you think you can make use of acaddoc.lsp? Do you prefer to load AutoLISP from startup suite or this method? And what command you want to modify? What do you want it to do? Share it here. the others might find it useful too! .Command: PLOT _DATALINKUPDATE Select an option [Update data link/Write data link] <Update data link>: Command: . It‟s very similar with plot. How do you think acaddoc. And they become regular questions. we are going to focus to the last one. You can load AutoLISP file automatically. What can we do with acaddoc.lsp will execute commands defined in it.lsp By Edwin Prakoso | Last updated: January 23. every time AutoCAD open a file.lsp in AutoLISP tutorial. You can run commands before you start working with your file. 4.Set default system variables in acaddoc. 3. You can set system variable before you start working. You can define new command. 2012 0 0 0 0 We covered about acaddoc.lsp? Now if you know you can run commands automatically when you open a file. we redefine PLOT command to update datalink before AutoCAD starts to plot. . I found that there are many users confused why their AutoCAD doesn‟t work as usual. 2. These are some samples of changed system variables. what would you do? 1. In this article. In the tutorial. Acaddoc. We can‟t remember all the variables. AutoCAD default noun-verb selection becomes verb-noun selection. I used to be agree with it. And more… There is a debate why they changed. If you want to learn how to use Visual LISP editor. It’s because FILEDIA system variable has changed. You have to type file path and file name from command line. Single document interface is activated. Most people think that it was because routines in AutoLISP or other 3rd party applications. File dialog box is missing. if you have specific system variable that changes often. It’s because PICKFIRST system variable has changed. We can force to set the variables in acaddoc.lsp using notepad or visual LISP editor.lsp. I prefer to backup my system variables and restore them when I have problem. Just type these code: (SETVAR "PICKFIRST" 1) Feel free to add more lines or change system variables and values as you preferred. What system variables do you find change often? Do you find system variables in your AutoCAD change often? What are they? . until I saw this problem also happens in AutoCAD LT. 1. right? However.lsp Harold Reuvers reminds me that we can set those variables in acaddoc. 2. 4. It’s controlled by SDI system variable. So it remains a mystery to me.Common unexpected system variables change These are several common system variables that change regularly. All you need is to create an acaddoc. 3. see this basic AutoLISP tutorial. this is a good solution. ISO 12567.Adding AIA standard layers with LISP By Edwin Prakoso | Last updated: September 28. There are several standard layers available like BS 1192. . There are four LISPs. I would suggest you to keep your standard layers in AutoCAD template and standards file. 2012 0 0 0 1 Standard layers is one popular topic when we talk about CAD standards. AIA. before you run AIALAYERSDEMO. If you asked me. and one ctb plot style. you will get error when running the LISP program. one linetype. There is a bundle of AutoLISP programs that you can download and run to build the layers. it would take times to add them in your template manually. You must load demo. You can run it. it will create the layers and you can save your template.lin linetype. You can download the AutoLISP program in this cadalyst tip. if you‟ve never created standard the layers before. If you don‟t. However. It might take a while until all the layers are created.You can use AIA Monochrome. See this guide if you don’t know how to load them. you can load the AutoLISP programs. 3. AutoComplete should help you. Save the files in your LISP folder.ctb as your plot style. right click then select extract all… from contextual menu. If you use AutoCAD 2012 or later. now you have a good tool to start using it! . You need to extract it first. 1. 2. 4. you can run the AIA layers tools by typing the commands. After you load them. you can select the zip file. If you are planning to adopt AIA standard layers. In Windows explorer. After you extracted the file. How to load and run the AutoLISP program You will download a zip file. As you can see below. what if you need to draw polygon in isometric drawing? Lee Mac has an AutoLISP program to do this. you can download the “isopoly” program here. you can quickly draw polygon in your isometric drawing. It‟s really helpful. Using Isopoly To use isopoly. If you are interested. However. It‟s not a 3D drawing. you need to load the program to AutoCAD first. Type 1 then [enter] again to accept the value. After you load the program. Refer to Lee‟s guide to load AutoLISP program here.How to: Create polygon in isometric drawing By Edwin Prakoso | Last updated: November 25. . 2013 0 1 0 1 We can create isometric drawing by changing the snap style. Type SNAPSTYL then [enter]. change SNAPSTYL system variable to 1. it‟s a 2D drawing. Lee also includes a short code to allow you quickly change SNAPSTYL using command line. Lee has many great free AutoLISP program. read this guide saving: using and managing your AutoLISP program.You can cycle between isoplane by pressing F5 or type ISOPLANE on command line. If you never visit his site. Find it at the bottom of the download page. If you are not familiar how to save that code to an AutoLISP program. check all these programs that you can download. .