jpsheader.jpg (29354 bytes)

Back to DCL Tutorial Homepage

Back to AutoLisp Homepage

Back to JefferyPSanders.com

DownLoad Sample4.LSP

DownLoad Sample4.DCL

 

 

 

 

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

 

 

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 from disk into memory
  (if(not(setq dcl_id (load_dialog "SAMPLE4.dcl")))
    (progn
      (alert "The DCL file could not be loaded!")
      (exit)
    )

    ;;;--- Else, the DCL file was loaded
    (progn

      ;;;--- Load the definition inside the DCL file
      (if (not(new_dialog "SAMPLE4" dcl_id))

        (progn
        (alert "The SAMPLE4.DCL file could not be loaded!")
          (exit)
        )
       
        ;;;--- Else, the definition was loaded
        (progn 

          ;;;--- If an action event occurs, do this function
          (action_tile "accept" "(saveVars)(done_dialog 2)")
          (action_tile "cancel" "(done_dialog 1)")

          ;;;--- Display the dialog box
          (setq ddiag(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:

 

DCL_PART401.jpg (16441 bytes)


   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 from disk into memory
  (if(not(setq dcl_id (load_dialog "SAMPLE4.dcl")))
    (progn
      (alert "The DCL file could not be loaded!")
      (exit)
    )

    ;;;--- Else, the DCL file was loaded
    (progn

      ;;;--- Load the definition inside the DCL file
      (if (not(new_dialog "SAMPLE4" dcl_id))

        (progn
        (alert "The SAMPLE4.DCL file could not be loaded!")
          (exit)
        )
       
        ;;;--- Else, the definition was loaded
        (progn 

         
(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" "(saveVars)(done_dialog 2)")
          (action_tile "cancel" "(done_dialog 1)")

          ;;;--- Display the dialog box
          (setq ddiag(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))
  )

)

  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 from disk into memory
  (if(not(setq dcl_id (load_dialog "SAMPLE4.dcl")))
    (progn
      (alert "The DCL file could not be loaded!")
      (exit)
    )

    ;;;--- Else, the DCL file was loaded
    (progn

      ;;;--- Load the definition inside the DCL file
      (if (not(new_dialog "SAMPLE4" dcl_id))

        (progn
        (alert "The SAMPLE4.DCL file could not be loaded!")
          (exit)
        )
       
        ;;;--- Else, the definition was loaded
        (progn 

         
(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" "(saveVars)(done_dialog 2)")
          (action_tile "cancel" "(done_dialog 1)")

          ;;;--- Display the dialog box
          (setq ddiag(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?

DCL_PART402.jpg (16668 bytes)

DCL_PART403.jpg (14025 bytes)


 

  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.

Back


AutoLisp DCL Tutorial Home

AutoLisp Home

Home

All questions/complaints/suggestions should be sent to JefferyPSanders.com

Last Updated May 1st, 2013

Copyright 2002-2013 JefferyPSanders.com. All rights reserved.