
Back to AutoLisp Tutorial Homepage
D I A L O G
C O N T R O L
L A N G U A G E
D I A L O G
C O N T R O L
L A N G U A G E
D I A L O G
C O N T R O L
L A N G U A G E
D I A L O G
C O N T R O L
L A N G U A G E
D I A L O G
C O N T R O L
L A N G U A G E
D I A L O G
C O N T R O L
L A N G U A G E
D I A L O G
C O N T R O |
|
The AutoLisp Tutorial - DCL Dialog Control Language - Part 7 Now it is time to take all of the parts and pieces and put them together. Let's build a working DCL file that will let us see most of the things we've learned and yet not get to deep into autolisp. Lets draw a polygon or circle, on a selected layer with the option to save the settings last used for defaults next time we run the program. If polygon is chosen, ask for the number of sides. So....we will need a list_box to hold all of the available layer names. We will need a radio_column to select a circle or polylgon. We will need an popup_list to hold the number of sides for the polygon. We will need a toggle to check the "Save Settings" option. And last, we will need an Okay and Cancel button. Layout thoughts: The list will need to have several layers showing so the user won't have to scroll forever. So, it will probably be the tallest item on the dialog box. I'll put it in a column by itself. I'll try to fit the rest of the items in a column beside it. Then I'll put the Okay and Cancel buttons at the bottom in a row. So...I'll need something like this: : column { // Put
code for radio button 1 here [ Circle ] // Put
code for radio button 2 here [ Polygon ] // Put code
for popup_list here [ Number of Sides ] // Put code
for toggle here [ Save Settings ] // Put code
for list here [ Layer Names ] // Put code for button 1
here [ Okay ] // Put code for button 2
here [ Cancel ] Let's copy in the code for the header and all of the controls above. I'll show them in red. Notice the key names and labels had to be changed. SAMPLE7 : dialog { } Right click and copy the above. Open NotePad and paste it. Save the file as SAMPLE.DCL Be sure to change the "Save as Type" drop down box to "All Files" before saving it or it will put a ".txt" extension on the file name. Save this file somewhere in the AutoCAD search path. Next we will get a copy of the AutoLisp model and revise it. All new code is shown in red. (defun C:SAMPLE7() ;;;--- Unload the dialog box Right click and copy the above. Open NotePad and paste it. Save the file as SAMPLE7.LSP Be sure to change the "Save as Type" drop down box to "All Files" before saving it or it will put a ".txt" extension on the file name. Save this file somewhere in the AutoCAD search path. Let's load the program and see what the DCL file looks like. On the command line type this: Command: (load "sample7") and press enter You should see this C:Sample7 Now type Sample and press enter. If everything went according to plan you should see this on your screen:
Notice there are no items in either list. We haven't added that part yet. Also notice the Draw Circle is selected. We did that in the DCL file. We set the value of that radio_button to "1". We did not set the value of the toggle, so the default is unchecked. If you push the Cancel button the program will exit normally. If you press the Okay button an error will occur because we have not defined the saveVars routine. We have two things left to do. First we need to build the list for the popup_list control and the list_box control. Then we need to add the list to the dialog box. Instead of building a function to get all of the layer names into a list, let's just build the list manually to keep things simple. Okay...you talked me into it. I'll do it both ways and you can use the one you want. I'm going to use the short version for this tutorial. Long way: ;;;--- Set up a list to hold the layer names Short Way: (setq layerList(list "0" "DIM" "HIDDEN" "STR" "TX" "WELD")) We also need a list for the polgon's number of sides: (setq numSides(list "4" "6" "8" "12" "16")) We need to add these lines to our autolisp program. Add it just below the new_dialog call and above the action_tile statements. We now need to upload the list into the dialog box. So put these lines just below the lines you just added. ;;;--- Add the layer names to the dialog box ;;;--- Add the number of sides to the dialog box When you are done it should look like this: (defun C:SAMPLE7() (setq layerList(list "0" "DIM" "HIDDEN" "STR" "TX" "WELD")) (setq numSides(list "4" "6" "8" "12" "16")) ;;;--- Add the layer names to
the dialog box ;;;--- Add the number of sides
to the dialog box ;;;--- If an action event
occurs, do this function ;;;--- Unload the dialog box Save, Load, and Run. You should see this:
Next, let's build the SaveVars routine. We will do this by starting with the saveVars routine in the AutoLisp Model, then copy and paste from the "Save Data from Dialog Box" section of this tutorial. We will then modify the names to match the key names in the DCL file. I also changed a variable name to numStr to be used later to set the default settings. More on that later. I'll show you the changes I made in red: (defun saveVars() (setq radios(get_tile
"radios")) Note: Since we did not specify a default value for the list_box, layerName will be set to nil if the user does not select a layer from the list before hitting the Okay button. Save, Load, and Run. Check the values after pressing the Okay button by typing an exclamation point and then the variable name on the command line. Examples: To get the layer name type !layerName and press enter. To get the value of the toggle type !saveSet and press enter. Everything working okay? Alrighty then...let's move on. The only work left on the dialog box is to disable the "Number of Sides" popup_list when a circle is selected. Let's add two action_tiles on both of the radio_buttons. ;;;--- If an action event
occurs, do this function This will send a parameter of 1 to the toggleRadio function if the Circle is selected. It will send a parameter of 2 to the toggleRadio function if the Polygon is selected. What the hell is a toggleRadio function? We have to create it and put it in our AutoLisp program. Like this: (defun toggleRadio(a) ;if circle is selected ;else Since our default for the radio buttons is to draw a circle, the numSides popup_list should be disabled before the dialog box starts. So just before the action_tile statements we need to add this line: (mode_tile "numsides" 1) Copy, Save, and Run. You should see this:
The numSides popup_list is disabled when the circle is selected. Enabled when polygon is selected. Cool. Here is the AutoLisp program after the lines above were added: (defun saveVars() ;;;--- Unload the dialog box Now the only thing remaining is to do something when the user presses the okay button. We have all of the dialog box data stored in variable names....
So now all we have to do is write the AutoLisp code inside the "Okay button was pressed" IF statement. (Where the blue line is above.) Now we will replace the blue line above: (princ "\n \n ...SAMPLE Complete!") with new code. First, let's go ahead and change the layer. (setq oldLay(getvar
"clayer")) That was easy. Now, Let's draw the selected entity (if(= radios "drawcir") Add the above to your program and save it. Test it out. Everything working okay? And finally all we have left is the code for the default settings. Since everything in the dialog box is a string, why don't we save all of the data as strings. Lucky for us, AutoDesk included 15 setvars for us to store data in. USERR1 thru USERR5 is used to store real numbers. USERI1 thru USERI5 is used to store integers. USERS1 thru USERS5 are used to store strings. We will use the system variables USERS1 thru USERS4 to save our data since we are saving the data as strings. All of these setvars are stored inside the drawing file. They will be there everytime you open your drawing. How convenient! So let's get to it. The first variable to store is RADIOS and it is already a string. So.... (setvar "USERS1" radios) The second variable to store is NUMSIDES and it is an integer representing the number of sides the polygon should have. I want to store the index of the selected item in the list not the actual number of sides. We saved the index as a variable named NUMSTR when we ran the saveVars routine. So... (setvar "USERS2" numStr) The third variable to save is SAVESET. It is stored as an integer. We will have to convert it to a string to save it. (setvar "USERS3" (itoa saveSet)) The fourth and final variable to save is LAYERNAME. It is stored as a string. I want to save the index of the selected item. We saved that earlier with the saveVars routine in a variable named sSTR. (setvar "USERS4" sSTR)
The last thing we have to do is check for the default settings and load them if they exist. (defun saveVars() (setq radios(get_tile "radios")) ;;; Build the list for the dialog box
;;;--- Only disable the numSides popup_list if a
circle is being drawn ;;;--- Unload the dialog box ;;;---
See what needs to be drawn ;;;--- Else
draw a polygon When you get your program tested and everything is working, move the blue line above, [ (defun C:SAMPLE7() ] all the way to the top of the file. This will make all of your variables local and will reset them all to nil when the program ends. That's it. We're done. All questions/complaints/suggestions should be sent to JefferyPSanders.com Last Updated January 28th, 2006 Copyright 2002-2008 JefferyPSanders.com. All rights reserved. |