The AutoLisp Tutorial - DCL

Dialog Control Language - Part 5


  Part 5 - Radio Buttons  


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

  The first thing you need to know about a radio button is how stupid they are.   They have no brains.  They do not know what the other radio buttons are doing.   You can layout six radio buttons and select everyone of them like they were toggles.  That's not the way we use radio buttons.  They are supposed to be smart.  They should know what to do if a radio button around them is selected.   They should turn themselves off because only one radio button in a group is supposed to be checked.  That's where radio_column and radio_row come in to play.   They are the brains for the radio buttons.  They watch all the buttons in their row or column to make sure only one is turned on.  Okay..moving on.

   We will build a DCL file containing 4 radio_buttons plus an Okay and Cancel button.   The selected item will be displayed on the screen after the user presses the Okay button.


  Layout thoughts:   I will place the radio_buttons in a column, (stacked on top of each other).  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 {
  : radio_column {
    // Put code for radio_column here
    : radio_column {
      // Put code for radio_button 1 here
    } 
    : radio_button {
      // Put code for radio_button 2 here
    } 
    : radio_button {
      // Put code for radio_button 3 here
    } 
    : radio_button {
      // Put code for radio_button 4 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.

SAMPLE5 : dialog {
          label = "Sample Dialog Box Routine - Part 5";

          : column {
            : radio_column {
              key = "mychoice";
              : radio_button {
                key = "but1";
                label = "Apples";
              }
              : radio_button {
                key = "but2";
                label = "Oranges";
              }
              : radio_button {
                key = "but3";
                label = "Bananas";
              }
              : radio_button {
                key = "but4";
                label = "Lemons";
              }
            }
            : 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 SAMPLE5.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:SAMPLE5()

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

  ;;;--- Load the dialog definition if it is not already loaded
  (if (not (new_dialog "SAMPLE5" 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 Sample5 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 SAMPLE5.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 "sample5") and press enter

  You should see this

C:Sample5
Command:

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

 

DCL_PART501.jpg (9475 bytes)

   That doesn't look very good does it?  Let's change the boxed_row into a boxed_column in our DCL file.  (See the blue text in the DCL file above) Make the changes then Save the Sample5.DCL file.   No need to load the autolisp program again, it's loaded.  Just run the Sample5 program again.  Now it should look like this:

DCL_PART502.jpg (11149 bytes)

   It still doesn't look right.  It's our label "Sample Dialog Box Routine - Part 5" that is causing the problem.  Let's shorten it to "SDBR - Part 5" and try it again:

DCL_PART503.jpg (7309 bytes)

Looks better! 


   Looking good so far.  We need to add the SaveVars function to save the selected items from the radio_column  when the Okay button is pressed.  Look at the blue text in the Sample5.lsp program above.

  Let's steal the saveVars routine from the radio_column control on the "Saving data from the dialog box" page of this tutorial and modify it.  I'll show the modifications in red.

  We can do this two different ways.  We can check each radio_button to find out which one is on or we can check the entire column of radio_buttons by getting the value of the radio_column. 

First method:  Checking the Radio_Column:

(defun saveVars()

  ;;;--- Get the key of the choice made
  ;;;    [ returns "but1" "but2" "but3" or "but4" whichever is selected.]

  (setq myChoice(get_tile "mychoice"))

)

Second method: Checking each Radio_Button:

(defun saveVars()

;;;--- Get the value of each item
  (setq choice1(atoi(get_tile "but1"))) 
// 0 = not chosen    1 = chosen  
  (setq choice2(atoi(get_tile "but2"))) 
// 0 = not chosen    1 = chosen
  (setq choice3(atoi(get_tile "but3"))) 
// 0 = not chosen    1 = chosen
  (setq choice4(atoi(get_tile "but4"))) 
// 0 = not chosen    1 = chosen

)

  Wow! That was easy.  So...Which one do we use?  For this tutorial, let's use both.  Why not? 

 


(defun saveVars()

  ;;;--- Get the key of the choice made
  ;;;    [ returns "but1" "but2" "but3" or "but4" whichever is selected.]

  (setq myChoice(get_tile "mychoice"))

  ;;;--- Get the value of each item
  (setq choice1(atoi(get_tile "but1"))) 
// 0 = not chosen    1 = chosen  
  (setq choice2(atoi(get_tile "but2"))) 
// 0 = not chosen    1 = chosen
  (setq choice3(atoi(get_tile "but3"))) 
// 0 = not chosen    1 = chosen
  (setq choice4(atoi(get_tile "but4"))) 
// 0 = not chosen    1 = chosen

)


Add this to the original Sample5.lsp program and we should have something that looks like this:

(defun saveVars()

  ;;;--- Get the key of the choice made
  ;;;    [ returns "but1" "but2" "but3" or "but4" whichever is selected.]

  (setq myChoice(get_tile "mychoice"))

  ;;;--- Get the value of each item
  (setq choice1(atoi(get_tile "but1"))) 
// 0 = not chosen    1 = chosen  
  (setq choice2(atoi(get_tile "but2"))) 
// 0 = not chosen    1 = chosen
  (setq choice3(atoi(get_tile "but3"))) 
// 0 = not chosen    1 = chosen
  (setq choice4(atoi(get_tile "but4"))) 
// 0 = not chosen    1 = chosen

)

(defun C:SAMPLE5()

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

  ;;;--- Load the dialog definition if it is not already loaded
  (if (not (new_dialog "SAMPLE5" 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 Sample5 cance
lled!")
  )

  ;;;--- 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 display the selected item.  

  ;;;--- If the user pressed the Okay button
  (if(= ddiag 2)
    (progn

      ;;;--- Inform the user of his selection using the radio_column data
      (princ "\n Using Radio_column data...You chose ")
      (cond
        ((= myChoice "but1")(princ "Apples!"))
        ((= myChoice "but2")(princ "Oranges!"))
        ((= myChoice "but3")(princ "Bananas!"))
        ((= myChoice "but4")(princ "Lemons!"))
      )

      ;;;--- Inform the user of his selection using the radio_buttons data
      (princ "\n Using Radio_buttons data...You chose ")
      (cond
        ((= Choice1 1)(princ "Apples!"))
        ((= Choice2 1)(princ "Oranges!"))
        ((= Choice3 1)(princ "Bananas!"))
        ((= Choice4 1)(princ "Lemons!"))
      )

    )
  )



Add the above to the autolisp file, save it and test it out.   Everything working okay?

DCL_PART504.jpg (7175 bytes)

DCL_PART505.jpg (12555 bytes)


 

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