The AutoLisp Tutorial - DCL
Dialog Control Language - Part 4
Part 4 - PopUp_List
Let's build a working DCL file showing us exactly how to handle popup list.
We will build a DCL file containing two pop_up list plus an Okay and Cancel button. The selected items will be displayed on the screen after the user presses the Okay button.
Layout thoughts: I will place the popup_list in a row, (side by side). Then I'll put the Okay and Cancel buttons in a row at the bottom of the dialog box. So...I'll need something like this:
: column {
: boxed_row {
: popup_list {
// Put code for popup_list 1 here
}
: popup_list {
// Put code for popup_list 2
here
}
}
: boxed_row {
: button {
// Put
code for the Okay button here
}
: button {
// Put
code for the Cancel button here
}
}
}
Let's copy in the code for the header and all of the controls above from the "Controls" section of this tutorial. I'll show them in red. Notice the key names and labels had to be changed.
SAMPLE4 : dialog {
label = "Sample Dialog Box Routine
- Part 4";
: column {
: boxed_row {
:
popup_list {
key
= "mylist1";
label = "Select Item";
fixed_width_font = true;
width = 30;
value = "";
}
:
popup_list {
key
= "mylist2";
label = "Select Item";
fixed_width_font = true;
width = 30;
value = "";
}
}
: boxed_row {
: button {
key = "accept";
label = " Okay ";
is_default = true;
}
: button {
key = "cancel";
label = " Cancel ";
is_default = false;
is_cancel = true;
}
}
}
}
Right click and copy the above. Open NotePad and paste it. Save the file as SAMPLE4.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:SAMPLE4()
;;;--- Load the dcl file
(setq dcl_id (load_dialog "SAMPLE4.dcl"))
;;;--- Load the dialog definition if it is not already loaded
(if (not (new_dialog "SAMPLE4" dcl_id) )
(exit))
;;;--- If an action event occurs, do this function
(action_tile "accept" "(setq ddiag 2)(saveVars)(done_dialog)")
(action_tile "cancel" "(setq ddiag 1)(done_dialog)")
;;;--- Display the dialog box
(start_dialog)
;;;--- Unload the dialog box
(unload_dialog dcl_id)
;;;--- If the user pressed the Cancel button
(if(= ddiag 1)
(princ "\n Sample4 cancelled!")
)
;;;--- If the user pressed the Okay button
(if(= ddiag 2)
(progn
(princ "\n The user pressed Okay!")
)
)
;;;--- Suppress the last echo for a clean exit
(princ)
)
Right click and copy the above. Open NotePad and paste it. Save the file as SAMPLE4.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 "sample4") and press enter
You should see this
C:Sample4
Command:
Now type Sample4 and press enter. If everything went according to plan you should see this on your screen:
Looks good but, there is nothing to choose. Let's add some data to the popup list boxes. We will need two list. We will call the list for the first popup list box myList1 and the second myList2.
(setq myList1(list "Electrical" "Structural" "Plumbing" "Foundation"))
(setq myList2(list "Plastic" "Steel" "Aluminum" "Concrete"))
Alrighty then, we have our list built. All we have to do is put them in the dialog box. We will use the start_list, add_list, and end_list functions. Start_list tells DCL which popup list box we are going to edit. The identification is made by using the popup list box KEY. The first popup list box has a key of "mylist1" and the second has a key of "mylist2". So the start_list function would look like this:
(start_list "mylist1" 3) ; The 3 means we want to delete the old contents and start new.
Next we use the add_list function to tell DCL which list to put in the popup list box. We use the mapcar function to apply add_list to each member in the list. Our list for the first popup list box is named myList1. So...
(mapcar 'add_list myList1)
Finally we use the end_list function to tell DCL to display the new contents because we are through editing the popup list box.
(end_list)
To look at it all together:
(start_list "mylist1"
3)
(mapcar 'add_list myList1)
(end_list)
(start_list "mylist2"
3)
(mapcar 'add_list myList2)
(end_list)
Let's add all of this to the AutoLisp program and see what it looks like.
(defun C:SAMPLE4()
(setq myList1(list "Electrical" "Structural" "Plumbing" "Foundation"))
(setq myList2(list "Plastic" "Steel" "Aluminum" "Concrete"))
;;;--- Load the dcl file
(setq dcl_id (load_dialog "SAMPLE4.dcl"))
;;;--- Load the dialog definition if it is not already loaded
(if (not (new_dialog "SAMPLE4" dcl_id) ) (exit))
(start_list "mylist1" 3)
(mapcar 'add_list myList1)
(end_list)
(start_list "mylist2"
3)
(mapcar 'add_list myList2)
(end_list)
;;;--- If an action event occurs, do this
function
(action_tile "accept" "(setq ddiag 2)(saveVars)(done_dialog)")
(action_tile "cancel" "(setq ddiag 1)(done_dialog)")
;;;--- Display the dialog box
(start_dialog)
;;;--- Unload the dialog box
(unload_dialog dcl_id)
;;;--- If the user pressed the Cancel button
(if(= ddiag 1)
(princ "\n Sample4 cancelled!")
)
;;;--- If the user pressed the Okay button
(if(= ddiag 2)
(progn
(princ "\n The user pressed Okay!")
)
)
;;;--- Suppress the last echo for a clean exit
(princ)
)
Notice the location of the red lines. We create the list before loading the dialog box. We add the list to the dialog box after it is loaded with new_dialog and before the action_tile statements. This is the order you should use.
Looking good so far. We need to add the SaveVars function to save the selected items from the popup list boxes when the Okay button is pressed. Look at the blue text in the Sample4.lsp program above.
Let's steal the saveVars routine from the popup list box control on the "Saving data from the dialog box" page of this tutorial and modify it. I'll show the modifications in red.
(defun saveVars()
;;;--- Get the selected item from the first list
(setq sStr1(get_tile "mylist1"))
;;;--- Make sure something was selected...
(if(= sStr1
"")>
(setq myItem1
nil)
(setq myItem1
(nth (atoi sStr1) myList1))
)
)
Wow! That was easy. Wait a minute, we have two pop_up list boxes. We will have to create a function out of this or simply copy this and do it twice. For now, let's just do it twice.
Our program would now look like this:
(defun saveVars()
;;;--- Get the selected item from the first list
(setq sStr1(get_tile "mylist1"))
;;;--- Make sure something was selected...
(if(= sStr1
"")>
(setq myItem1
"Nothing")
(setq myItem1
(nth (atoi sStr1) myList1))
)
;;;--- Get the selected item from the second list
(setq sStr2(get_tile "mylist2"))
;;;--- Make sure something was selected...
(if(= sStr2
"")>
(setq myItem2
"Nothing")
(setq myItem2
(nth (atoi sStr2) myList2))
)
)
(defun C:SAMPLE4()
(setq myList1(list "Electrical"
"Structural" "Plumbing" "Foundation"))
(setq myList2(list "Plastic" "Steel" "Aluminum" "Concrete"))
;;;--- Load the dcl file
(setq dcl_id (load_dialog "SAMPLE4.dcl"))
;;;--- Load the dialog definition if it is not already loaded
(if (not (new_dialog "SAMPLE4" dcl_id) ) (exit))
(start_list "mylist1"
4)
(mapcar 'add_list myList1)
(end_list)
(start_list "mylist2"
4)
(mapcar 'add_list myList2)
(end_list)
;;;--- If an action event occurs, do this
function
(action_tile "accept" "(setq ddiag 2)(saveVars)(done_dialog)")
(action_tile "cancel" "(setq ddiag 1)(done_dialog)")
;;;--- Display the dialog box
(start_dialog)
;;;--- Unload the dialog box
(unload_dialog dcl_id)
;;;--- If the user pressed the Cancel button
(if(= ddiag 1)
(princ "\n Sample4 cancelled!")
)
;;;--- If the user pressed the Okay button
(if(= ddiag 2)
(progn
(princ "\n The user pressed
Okay!")
)
)
;;;--- Suppress the last echo for a clean exit
(princ)
)
Last item. We need to replace the line in the program: (princ "\n The user pressed Okay!") with something to modify and display the selected items. Let's do something simple. We will tell the user what was selected out of each popup list..
;;;--- If the user
pressed the Okay button
(if(= ddiag 2)
(progn
;;;--- Inform the user of his selection from the first list
(princ (strcat "\n You chose " myItem1 "
from the first popup list box."))
;;;--- Inform the user of his selections from the second list
(princ
(strcat "\n You chose " myItem2 " from the second popup list box."))
)
)
Add the above to the file, save it and test it out. Everything working okay?
When you get your program tested and everything is working, move the blue line above, [ (defun C:SAMPLE4() ] 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.