0% found this document useful (0 votes)
240 views22 pages

The Autolisp Tutorial - DCL: Dialog Control Language - Part 5

The document discusses building a dialog control language (DCL) file to demonstrate how to handle radio buttons. It describes adding four radio buttons in a column and Okay and Cancel buttons in a row. The AutoLisp code is shown to load the DCL file, handle button clicks, save the selected radio button, and display the choice when Okay is clicked. Modifications are made to improve the layout and display of the selected item.

Uploaded by

abl_ph7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
240 views22 pages

The Autolisp Tutorial - DCL: Dialog Control Language - Part 5

The document discusses building a dialog control language (DCL) file to demonstrate how to handle radio buttons. It describes adding four radio buttons in a column and Okay and Cancel buttons in a row. The AutoLisp code is shown to load the DCL file, handle button clicks, save the selected radio button, and display the choice when Okay is clicked. Modifications are made to improve the layout and display of the selected item.

Uploaded by

abl_ph7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 22

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.
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 from disk into memory
(if(not(setq dcl_id (load_dialog "SAMPLE5.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 "SAMPLE5" dcl_id))
(progn
(alert "The SAMPLE5 definition 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 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:

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:

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:

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")))
chosen
(setq choice2(atoi(get_tile "but2")))
(setq choice3(atoi(get_tile "but3")))
(setq choice4(atoi(get_tile "but4")))

; 0 = not chosen

1=

; 0 = not chosen
; 0 = not chosen
; 0 = not chosen

1 = chosen
1 = chosen
1 = chosen

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")))
chosen
(setq choice2(atoi(get_tile "but2")))
(setq choice3(atoi(get_tile "but3")))
(setq choice4(atoi(get_tile "but4")))
)

; 0 = not chosen

1=

; 0 = not chosen
; 0 = not chosen
; 0 = not chosen

1 = chosen
1 = 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")))
chosen
(setq choice2(atoi(get_tile "but2")))
(setq choice3(atoi(get_tile "but3")))
(setq choice4(atoi(get_tile "but4")))

; 0 = not chosen

1=

; 0 = not chosen
; 0 = not chosen
; 0 = not chosen

1 = chosen
1 = chosen
1 = chosen

)
(defun C:SAMPLE5()
;;;--- Load the dcl file from disk into memory
(if(not(setq dcl_id (load_dialog "SAMPLE5.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 "SAMPLE5" dcl_id))
(progn
(alert "The DCL definition 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 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)
)

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?

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.

El Tutorial AutoLisp - DCL


Dilogo Lenguaje de control - Parte 5
Parte 5 - Radio Buttons
Vamos a construir un archivo de DCL de trabajo que nos muestra exactamente
cmo manejar los botones de radio.
La primera cosa que usted necesita saber acerca de un botn de radio es lo
estpidos que son. Ellos no tienen cerebro. Ellos no saben lo que los otros
botones de radio estn haciendo. Puede layout seis botones de seleccin y
seleccione uno de ellos como si fueran alterna. Esa no es la forma en que usamos
los botones de radio. Se supone que ser inteligente. Ellos deben saber qu hacer
si se selecciona un botn de radio alrededor de ellos. Deben entregarse fuera
porque se supone slo un botn de opcin en un grupo a comprobar. Ah es
donde radio_column y radio_row vienen a jugar. Son los cerebros de los botones
de radio. Miran todos los botones en su fila o columna para asegurarse de que
slo uno est activada.
Vamos a construir un archivo que contiene DCL 4 radio_buttons ms un botn
bien y Cancelar. El elemento seleccionado se mostrar en la pantalla despus de
que el usuario pulsa el botn Aceptar.

Pensamientos de colocacin: voy a colocar los radio_buttons en una


columna, (apiladas una encima de la otra). Entonces voy a poner el bien y

cancelar los botones en una fila en la parte inferior del cuadro de dilogo.As
que ... voy a necesitar algo como esto:
: Columna {
: radio_column {
// cdigo para radio_column
Ponga aqu
: radio_column { // cdigo para poner
radio_button 1 aqu
}
: radio_button { //
cdigo para radio_button Ponga aqu 2
}
:
radio_button { // cdigo para radio_button Ponga aqu
3
}
: radio_button { // Dicho cdigo para
radio_button 4 aqu
}
}
: {boxed_row
:
botn {

// cdigo para poner el botn Bueno aqu


}
: botn {
// cdigo para poner el botn Cancelar aqu
}
}
}

Vamos a copiar en el cdigo de la cabecera y todos los controles arriba del


" Controles "de este tutorial. Voy a mostrarles en rojo. Observe los nombres clave
y etiquetas tuvieron que ser cambiado.

Sample5: Dilogo {
label = "Muestra el cuadro de dilogo de
rutina - Parte 5";
: {columna
: radio_column {
key = " mychoice ";
: radio_button {
key = " But1 ";
label = " Manzanas ";
}
{: radio_button
= clave " but2 ";
label = " Naranjas ";
}
: radio_button {
key = " but3 ";
label = " Bananas ";
}
: radio_button {
key = " but4 ";
label = " Limones ";
}
}
: boxed_row {
: botn {
clave =
"aceptar";
label = "Est
bien";
is_default =
true;
}
: botn
{
key = "cancelar";
label = "Cancelar";
is_default =
false;
is_cancel = true;
}
}
}

}
Haga clic derecho y copiar la anterior. Abra el Bloc de notas y
pegarlo. Guarde el archivo como SAMPLE5.DCL Asegrese de cambiar la
opcin "Guardar como tipo" cuadro desplegable a "todos los archivos" antes
de guardarlo o se ponga una extensin ".txt" en el nombre del
archivo. Guarde este archivo en algn lugar de la ruta de bsqueda de
AutoCAD.

A continuacin vamos a obtener una copia del modelo AutoLisp y


revisarlo. Todos nuevo cdigo se muestra en rojo.
(Defun C: sample5 ()
;;; --- Cargue el archivo dcl del
disco en la memoria
(si (no (setq dcl_id (load_dialog
" sample5 .dcl ")))
(progn
(alerta "El archivo DCL
no se pudo cargar ! ")
(salida)
)
;;; --- Si
no, el archivo DCL se carg
(progn
;;; --- Cargue la
definicin dentro del archivo DCL
(si (no (new_dialog
" sample5 "dcl_id))

(progn
( alertar "El sample5 definicin no se pudo cargar!
")
(salida)
)
;;; --- Si no, la definicin se carg
(progn
;;; --- Si se produce un evento de
accin, hacer esta funcin
(action_tile "aceptar"
" (saveVars) (done_dialog 2) ")
(action_tile
"cancelar" "(done_dialog 1)")
;;; --- Muestra el
cuadro de dilogo
(setq ddiag

(start_dialog))
;;; --- Descargue el cuadro de
dilogo
( dcl_id unload_dialog)
;;; --Si el usuario pulsa el botn Cancelar
(si (= ddiag
1)
(princ "\ n sample5 cancelado! ")
)
;;; --- Si el usuario pulsa el botn
Aceptar
(si ( = ddiag 2)
(progn
(princ "\ n El usuario pulsa Okay!")
)
)
)
)
)
)
;;; --- reprimir
el ltimo eco para una salida limpia
(princ) )

Haga clic derecho y copiar la anterior. Abra el Bloc de notas y


pegarlo. Guarde el archivo como SAMPLE5.LSP Asegrese de cambiar la
opcin "Guardar como tipo" cuadro desplegable a "todos los archivos" antes
de guardarlo o se ponga una extensin ".txt" en el nombre del
archivo. Guarde este archivo en algn lugar de la ruta de bsqueda de
AutoCAD.

Vamos a cargar el programa y ver lo que el archivo DCL se parece. En la lnea


de comandos, escriba lo siguiente:
Comando: (load "sample5") y pulse Enter
Usted debe ver esta
C: sample5
comandos:
Ahora escriba sample5 y pulse enter. Si todo sali de acuerdo al plan que usted
debe ver esto en la pantalla:

Eso no parece muy bueno no? Vamos a cambiar el boxed_row en


un boxed_column en nuestro archivo DCL. (Vase el texto azul en el archivo
DCL arriba) Realice los cambios a continuacin, guarde el archivo
Sample5.DCL. No hay necesidad de cargar el programa AutoLISP de nuevo, est
cargada. Slo tiene que ejecutar el programa sample5 nuevo. Ahora debera tener
este aspecto:

Todava no se ve bien. Es nuestro sello "Muestra el cuadro de dilogo de rutina


- Parte 5" que est causando el problema. Vamos a acortarlo a "SDBR - Parte 5"
y tratar de nuevo:

Se ve mejor!

Verse bien hasta el momento. Tenemos que aadir los SaveVars funcin para
guardar los elementos seleccionados de la radio_column cuando se pulsa el botn
Aceptar. Mira el texto azul en el programa Sample5.lsp arriba.
Vamos a robar la rutina saveVars del control radio_column en el " Guardar datos
desde el dilogo cuadro"pgina de este tutorial y modificarlo. Te voy a mostrar
las modificaciones en rojo.
Podemos hacerlo de dos maneras diferentes. Podemos comprobar cada
radio_button para averiguar cul es el o podemos comprobar toda la columna de
radio_buttons al obtener el valor de la radio_column.

Primer mtodo: Comprobacin del Radio_Column:


(SaveVars defun ()
;;; --- Obtener la clave de la eleccin hecha
;;; [Regresa " But1 "" but2 "" but3 "o" but4 "lo que se ha
seleccionado.]
(Setq miOpcion (get_tile " mychoice "))
)
Segundo mtodo: Comprobacin cada Radio_Button:
(SaveVars defun ()
;;; --- Obtener el valor de cada elemento
(Choice1 setq (atoi (get_tile " But1 "))) ; 0 = No 1 = elegidos
elegidos
(eleccin2 setq (atoi (get_tile " but2 "))) ; 0 = No 1 = elegidos
elegidos
(choice3 setq (atoi (get_tile " but3 "))) ; 0 = No 1 = elegidos
elegidos
(choice4 setq (atoi (get_tile " but4 "))) ; 0 = No 1 = elegidos
elegidos
)

As que ... Cul usamos? Para este tutorial, vamos a usar ambos. Por que no?

(SaveVars defun ()
;;; --- Obtener la clave de la eleccin hecha
;;; [Regresa " But1 "" but2 "" but3 "o" but4 "lo que se ha
seleccionado.]
(Setq miOpcion (get_tile " mychoice "))
;;; --- Obtener el valor de cada elemento
(Choice1 setq (atoi (get_tile " But1 "))) ; 0 = No 1 = elegidos
elegidos
(eleccin2 setq (atoi (get_tile " but2 "))) ; 0 = No 1 = elegidos

elegidos
(choice3 setq (atoi (get_tile " but3 ")))
elegidos
(choice4 setq (atoi (get_tile " but4 ")))
elegidos

; 0 = No 1 = elegidos
; 0 = No 1 = elegidos

Aadir este al programa original Sample5.lsp y deberamos tener algo que se


parece a esto:
(SaveVars defun ()
;;; --- Obtener la clave de la eleccin hecha
;;; [Regresa "But1" "but2" "but3" o "but4" lo que se ha
seleccionado.]
(MiOpcion setq (get_tile "mychoice"))
;;; --- Obtener el valor de cada elemento
(Choice1 setq (atoi (get_tile "But1"))) ; 0 = No 1 = elegidos
elegidos
(eleccin2 setq (atoi (get_tile "but2"))) ; 0 = No 1 = elegidos
elegidos
(choice3 setq (atoi (get_tile "but3"))) ; 0 = No 1 = elegidos
elegidos
(choice4 setq (atoi (get_tile "but4"))) ; 0 = No 1 = elegidos
elegidos
)
(Defun C: sample5 ()
;;; --- Cargue el archivo dcl del
disco en la memoria
(si (no (setq dcl_id (load_dialog
"SAMPLE5.dcl")))
(progn
(alerta "El archivo DCL no
se pudo cargar ! ")
(salida)
)
;;; --- Si no,
el archivo DCL se carg
(progn
;;; --- Cargue la
definicin dentro del archivo DCL
(si (no (new_dialog
"sample5" dcl_id))

(progn
( alertar "La definicin DCL no se pudo cargar!")
(salida)
)
;;; --- Si no, la definicin se carg
(progn
;;; --- Si se produce un evento de
accin, hacer esta funcin
(action_tile "aceptar"
"(saveVars) (done_dialog 2)")
(action_tile
"cancelar" "(done_dialog 1)")
;;; --- Muestra el
cuadro de dilogo
(setq ddiag
(start_dialog))
;;; --- Descargue el cuadro de
dilogo
( dcl_id unload_dialog)
;;; --Si el usuario pulsa el botn Cancelar
(si (= ddiag
1)
(princ "\ n sample5 signi

llen! ")
)
;;; --- Si el usuario pulsa el botn
Aceptar
(si (= ddiag 2)
(progn (princ
"\ n El usuario pulsa Okay!")
)
)
)
)
)
)
;;; --- reprimir el ltimo
eco para una salida limpia
(princ) )

ltimo artculo. Tenemos que sustituir la lnea en el programa: (princ "\ n


El usuario presionamos bien!") con algo para mostrar el elemento
seleccionado.
;;; --- Si el usuario pulsa el botn Aceptar
(si (= ddiag 2)
(progn
;;; --- Informe al usuario de su seleccin a travs de
los datos radio_column
(princ "\ n Utilizando datos Radio_column ... Usted
eligi")
(cond
((= MyChoice "But1") (princ "Manzanas!"))
( (= miOpcion "but2") (princ "Naranjas!"))
((= miOpcion "but3") (princ "Bananas!"))
((= miOpcion "but4") (princ "Limones!"))
)
;;; --- Informe al usuario de su seleccin a travs de
los datos radio_buttons
(princ "\ n Utilizando datos Radio_buttons ... Usted
eligi")
(cond
((= Choice1 1) (princ "Manzanas!"))
((= Choice2 1) (princ "Naranjas!"))
((= Choice3 1) (princ "Bananas!"))
((= Choice4 1) (princ "Limones!"))
)
)
)

Aadir la anterior para el archivo de AutoLISP, guardarlo y probarlo. Todo


trabajo bien?

Cuando usted consigue su programa de prueba y todo est funcionando, mueva


la lnea azul arriba, [ (defun C: sample5 () ]. hasta llegar a la parte superior
del archivo Esto har que todas las variables locales y se restablecer a todos a nil
cuando el programa termina.
Eso es. Hemos terminado.

You might also like