Back

AutoCAD: Dialog Fun Facts

Game Changer

As I mentioned in my last article Free Samples – The Halloween Edition it was a GAME CHANGER when Autodesk in 1992 released AutoCAD r12. This popular DOS based CAD product not only introduced a whole new graphical user interface (GUI) as a facelift, but it also gave to the developers the ability to customize their own GUI using AutoLISP. This is also a huge win for the typical AutoCAD user. Now instead of blindly responding to prompts at the command line, dialog boxes would appear on the screen for user input. Since after multiple decades later the same programmable dialog box function continues to be implemented in the current version of AutoCAD, let’s explore the many fun facts available for AutoCAD users to take full advantage of this feature.

Fun Fact #1

The first fun fact I would like to point out is that customizing your own dialog boxes in AutoCAD is not complicated at all. There are only a few very simple lines of code that is needed to make a custom dialog box appear in AutoCAD.

First of all, custom dialog boxes in AutoCAD are defined in a separate dialog control language (dcl) file with a .dcl file extension. Since a dcl file just contains text, any text editor like the Windows built-in Notepad program can be used to create and edit a dcl file. Each dialog box must be given a unique name which is case sensitive. For example, the text content for a dcl file named MyDclMin.dcl can just have this single line of code:

MyDclMin : dialog {ok_only;}

In this example, MyDclMin is the unique name I’m using for this custom dialog box. Though not required, the dcl file name in this case matches with the custom dialog name. The ok_only tile is already predefined in each and every version of AutoCAD since r12.

Next, there’s just a couple of lines of AutoLISP code needed to invoke the MyDclMin dialog box. At the AutoCAD command prompt enter the following line of code exactly as shown followed by pressing the Enter key on the keyboard:

(setq dcl-id (load_dialog "MyDclMin.dcl"))

Note: This assumes MyDclMin.dcl is saved in one of the folders listed under AutoCAD’s Options command, Files tab and Support File Search Path (see Figure 1).

Figure 1

This first line of code uses the load_dialog function to load the dcl file MyDclMin.dcl (file name is not case sensitive).

Then, enter this line of code at the AutoCAD command prompt followed again by pressing the Enter key to execute it:

(new_dialog "MyDclMin" dcl-id)(start_dialog)

The new_dialog function locates the unique dialog name MyDclMin (this is case sensitive) from within the dcl file and the start_dialog function launches the dialog onto the screen. Note: In this line of code make sure that there are no spaces between the close and open parenthesis. The MyDclMin dialog box appears as shown (see Figure 2).

Figure 2

Notice that a custom dialog box does not require a title nor any other messages to be displayed but at a minimum a single button that closes or dismisses it from the screen. In this example there are two ways to close the dialog box: (1) click the OK button and (2) press the ESC key on the keyboard.

After the dialog is dismissed the AutoLISP function to remove the dialog from AutoCAD’s memory is:

(unload_dialog dcl-id)

That’s all there is to it. Congratulations, you’ve just successfully completed your first custom dialog box! It’s as simple as that. Of course, the above lines of code assumes that there are no errors in locating the dcl file and loading the dialog named MyDclMin.

Fun Fact #2

The second fun fact I would like to point out is that the ok_only tile that’s predefined in AutoCAD is designed to help developers quickly generate custom dialog boxes without having to create code from scratch. Why reinvent the wheel when you don’t have to? But to understand how this all works, it’s important to dig a little deeper and find out what lines of code actually makes up the ok_only tile. Then you can create your own custom tile for your dialog boxes.

The predefined lines of code can be found inside a file called base.dcl which is typically installed under Windows user login profile and per the AutoCAD version:
"C:\Users\windows-login-profile\AppData\Roaming\Autodesk\AutoCAD Version ##\Release ##.#\enu\Support\base.dcl"

Since the base.dcl file is a text file just like any other dcl file, it can again be opened for viewing or editing using the Windows built-in Notepad program. Doing a search within base.dcl for ok_only reveals the following lines of code (see Figure 3):

Figure 3

The code here predefines some basic attributes for the ok_only tile such as: (1) this is a column with a fixed width, (2) the alignment is to be centered and (3) the dialog can be dismissed when the ESC key is pressed. This also shows that the ok_only tile references another tile called the ok_button. Doing a search for the ok_button within the base.dcl file shows the following lines of code (see Figure 4):

Figure 4

Here, again there are more attributes assigned to this tile such as: (1) the button’s label, (2) the key AutoLISP references for action and (3) the button is automatically selected when the Enter key is pressed. This again shows that the ok_button tile references another tile called the retirement_button. Doing a search for the retirement_button within the base.dcl file shows the following lines of code (see Figure 5):

Figure 5

We finally reached the source of the code that makes up the entire predefined ok_only tile as a button with the following attributes: (1) a fixed width, (2) the maximum width is 8 and (3) is center aligned.

When looking through the rest of the contents of base.dcl you’ll discover many additional ok tiles that all follow similar coding patterns such as:

ok_cancel
ok_cancel_help
ok_cancel_help_info

Now let us see how the dialog appears when these three though different but similar predefined tiles are used in lieu of ok_only. For this example, I’m going to name the dcl file MyDcl.dcl. The content this time consists of the following:

MyDcl1 : dialog {
      label = "Title:
Ok Cancel";
      ok_cancel;
}
MyDcl2 : dialog {
     label = "Title:
Ok Cancel Help";
     ok_cancel_help;
}
MyDcl3 : dialog {
     label = "Title:
Ok Cancel Help Info";
     ok_cancel_help_info;
}

Notice that I’ve made the following additions and or changes this time: (1) the single dcl file contains not one but three different dialog names, (2) under each dialog name now shows a label to define the title that appears on the dialog box and (3) each dialog box definition has now expanded from a single line to multiple lines. Now let’s execute the following line of code at the AutoCAD command prompt to load it:

(setq dcl-id (load_dialog "MyDcl.dcl"))

Like before, this assumes that the MyDcl.dcl file is saved in a folder that is in the AutoCAD Support File Search Path. Then similar to the previous example, enter the following line of code to launch the dialog onto the screen:

(new_dialog "MyDcl1" dcl-id)(start_dialog)

Figure 6

The custom dialog that appears this time comes from the dialog named MyDcl1. This includes a unique title at the top of the dialog labelled as “Title: Ok Cancel” and an additional Cancel button. To close the dialog, you can click either the OK or the Cancel button and the ESC key on the keyboard.

To invoke the second dialog named MyDcl2 you don’t have to execute the load_dialog line again since we have not unloaded the MyDcl.dcl file from AutoCAD’s memory. So, all that’s needed to bring this up on the screen is this single line of code:

(new_dialog "MyDcl2" dcl-id)(start_dialog)

Figure 7

For MyDcl2 in addition to the difference in the title which now shows “Ok Cancel Help”, a third button labelled as Help is shown. But unlike the OK and the Cancel buttons, the Help button does nothing when clicked. The reason for this is because there’s no predefined action assigned to the Help button in either the dcl or the AutoLISP code.

Finally, a similar line of code can be entered to bring up the MyDcl3 dialog:

(new_dialog "MyDcl3" dcl-id)(start_dialog)

Figure 8

Just like the previous dialog, MyDcl3 shows a Help button. But this time there’s a fourth button labelled as Info which again has no action assigned. Only the OK and Cancel buttons when clicked perform the action of closing or dismissing the dialog box.

This demonstrates how simple it is to use predefined tiles in custom dialog boxes to show different buttons. Also, this should give you an idea as to the code required to create your own button. In the next section I’ll show an example of a dialog box using a custom ok button.

Fun Fact #3

The third fun fact I would like to point out is that there’s actually another more flexible method of working with dcl files. When dialog boxes were first introduced in 1992 Autodesk presented the dcl files as if they needed to be permanently stored as separate files in order for them to function with their accompanied lisp programs (see Figure 9).

Figure 9

This methodology comes with the following shortcomings:

(1) the dcl file can be accidentally deleted

(2) the dcl file could be accidentally left behind during lisp file transfers

(3) the dcl like the lsp file must be located in a folder that the AutoCAD program can find

But what I’ve learned early on is that this is not really necessary. Instead, we can use AutoLISP functions to do the following:

(1) create a dcl file in a designated folder

(2) open and write the lines of code needed to define a custom dialog box

(3) delete the dcl file after the dialog is dismissed

So, for this final example using entirely AutoLISP functions, I’m going to create a dcl file called MyDclBox.dcl which contains all the lines of code needed to make a dialog box named MyDclBox appear on the screen. Let’s start off with entering this AutoLISP code at the AutoCAD command prompt to designate the “temporary drawing path” as the location to create MyDclBox.dcl file:

(setq dcl-file (strcat(getvar"tempprefix")"MyDclBox.dcl"))

Next, the AutoLISP open function can be used to start writing content into the dcl file by entering the following at the command prompt:

(setq fw (open dcl-file "w"))

This is then followed by the next series of lines of code using the write-line function to create all the code needed for the MyDclBox dialog:

(write-line "MyDclBox : dialog {" fw)
(write-line "label = \"Title: My Message\";" fw)
(write-line ": button {" fw)
(write-line "label = \"My Okay\";" fw)
(write-line "key = \"accept\";" fw)
(write-line "is_cancel = true;" fw)
(write-line "is_default = true; " fw)
(write-line "}" fw)
(write-line "}" fw)


Notice that some of the quotation symbols within the dcl code are now preceded with a back slash. This is required so that an actual quotation mark is written as content into the dcl file.

Lastly, the following AutoLISP close function is executed to finish the file writing process:

(setq fw (close fw))

The MyDclBox.dcl file now contains the lines of code as shown (see Figure 10).

Figure 10

In addition to the custom dialog name and title, as I mentioned earlier, I’ve now included code to define my very own custom button with the following attributes: (1) the label is “My Okay”, (2) the dialog can be dismissed by pressing the Esc key and (3) the dialog can be closed by pressing the Enter key.

Next, like in previous examples here are the lines of AutoLISP code needed to load and invoke the MyDclBox dialog box at the AutoCAD command prompt:

(setq dcl-id (load_dialog dcl-file))
(new_dialog "MyDclBox" dcl-id)(start_dialog)

Figure 11

In addition to having a custom “My Okay” button the MyDclBox also includes a unique title at the top of the dialog labelled as "Title: My Message". Since attributes are defined in the My Okay button, clicking on it or pressing the ESC key on the keyboard closes the dialog box.

After the dialog box is dismissed run the AutoLISP function to remove it from AutoCAD’s memory:

(unload_dialog dcl-id)

Finally, the AutoLISP generated dcl file can be erased from the temporary drawing path location using the following function:

(vl-file-delete dcl-file)

Note: In order for AutoLISP functions that start with the letters “vl” to execute successfully this may need to be executed first:

(vl-load-com)

Now you’ve learned the simple steps of using AutoLISP functions to create the dcl file on the fly and then delete it from storage after the dialog box is closed.

Game On

Autodesk has made customizing AutoCAD dialog boxes literally available at your fingertips. With just a few lines of code you can immediately see graphically on the screen the works of your labor. Now that you’ve learned how easy it is to customize and invoke your own dialog boxes within AutoCAD, I hope your reaction is the same as mind. It is now GAME ON!

Appears in these Categories

Back