The AutoLisp Tutorial - DCL

Dialog Control Language - Part 1


  Part 1 - Buttons  


   Let's build a working DCL file showing us exactly how to handle buttons.

 We will build a DCL file containing three buttons plus a Close [ Cancel ]   button.  Each of the three buttons will display a message when pressed.


  Layout thoughts:   I will place the buttons in a column, (stacked on top of each other).  Then I'll put the Close button at the bottom on a row of it's own.  So...I'll need something like this:

: column {
  : boxed_column {
    : button {
      // Put code for button 1 here
    } 
    : button {  ]
      // Put code for button 2 here 
    } 
    : button {  ]
      // Put code for button 3 here 
    } 
  } 
  : boxed_row {
    : button {

       // Put code for the Close button here
    }
  }  
}


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.

SAMPLE1 : dialog {
          label = "Sample Dialog Box Routine - Part 1";

          : column {
            : boxed_column {
              : button {
                key = "but1";
                label = "Button 1";
                is_default = false;

              }
              : button {
                key = "but2";
                label = "Button 2";
                is_default = false;

              }
              : button {
                key = "but3";
                label = "Button 3";
                is_default = false;

              }
            }
            : boxed_row {
              : button {
                key = "cancel";
                label = "Close";
                is_default = true;
                is_cancel = true;

              }
            }  
          }

}

Right click and copy the above. Open NotePad and paste it.  Save the file as SAMPLE1.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:SAMPLE1()

  ;;;--- Load the dcl file
  (setq dcl_id (load_dialog "SAMPLE1.dcl"))

  ;;;--- Load the dialog definition if it is not already loaded
  (if (not (new_dialog "SAMPLE1" dcl_id) ) (exit))

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

  ;;;--- Display the dialog box
  (start_dialog)

  ;;;--- Unload the dialog box
  (unload_dialog dcl_id)

  ;;;--- Suppress the last echo for a clean exit
  (princ)

)

  I removed several lines from the model.  I took out the part that checked to see if we hit the Cancel or Okay buttons.  We don't need either in this program.   I also removed the action tile for the okay button and removed "(setq ddiag 1)" from the cancel button.

Right click and copy the above. Open NotePad and paste it.  Save the file as SAMPLE1.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 "sample1") and press enter

  You should see this

C:Sample1
Command:

  Now type Sample1 and press enter.  If everything went according to plan you should see this on your screen:

 

DCL_PART101.jpg (13107 bytes)


   The buttons do not work yet.  Except for the close button.  It should work fine.  We need to add the function to print three different messages when the user presses each button.   Let's send a parameter to the function to decide which message to display.   If the parameter equals 1 we will print the first message.  If 2 then print the second message.  If 3 print the third message.  Something like this:

(defun doButton(a)
  (cond
    ((= a 1)(alert "Button 1 was pressed!"))
    ((= a 2)(alert "Button 2 was pressed!"))
    ((= a 3)(alert "Button 3 was pressed!"))
  )
)

  Now we need to add the action calls for each of the buttons:

  (action_tile "but1" "(doButton 1)")
  (action_tile "but2" "(doButton 2)")

  (action_tile "but3" "(doButton 3)")

  This will send a parameter of 1,2, or 3 to the doButton function depending on which button is pressed.


  Let's add the doButton function and the action calls to the autolisp program.  It should look like this:

(defun doButton(a)
  (cond
    ((= a 1)(alert "Button 1 was pressed!"))
    ((= a 2)(alert "Button 2 was pressed!"))
    ((= a 3)(alert "Button 3 was pressed!"))
  )
)


(defun C:SAMPLE1()

  ;;;--- Load the dcl file
  (setq dcl_id (load_dialog "SAMPLE1.dcl"))

  ;;;--- Load the dialog definition if it is not already loaded
  (if (not (new_dialog "SAMPLE1" dcl_id) ) (exit))

  ;;;--- If an action event occurs, do this function
  (action_tile "but1" "(doButton 1)")
  (action_tile "but2" "(doButton 2)")
  (action_tile "but3" "(doButton 3)")

  (action_tile "cancel" "(done_dialog)")

  ;;;--- Display the dialog box
  (start_dialog)

  ;;;--- Unload the dialog box
  (unload_dialog dcl_id)

  ;;;--- Suppress the last echo for a clean exit
  (princ)

)


Save it and test it out.   Everything working okay?

DCL_PART102.jpg (4432 bytes)    DCL_PART103.jpg (4453 bytes)   DCL_PART104.jpg (4437 bytes)


 

  When you get your program tested and everything is working, move the blue line above, [ (defun C:SAMPLE1() ] 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 Tutorial Home