AutoCAD comes with a large set of APIs (Application
Programming Interfaces) to build a custom environment using
languages like LISP, VBA or C++. In AutoCAD's Release 12 we
suddenly found another little programming language hidden in the
product. It is said to be on the release list since version 1.4
and follows Autodesk's tradition: "Well, we could've made it
easy, but why not make it customizable?"
DIESEL was used by AutoCAD programmers to add little checkmarks to menu labels and to supress a "cancel" in front of a command. Some people also used DIESEL to add the drawing name to the status bar. Following the Windows releases, even this use of DIESEL was gone. So it could fade away in history...
But then Autodesk released AutoCAD LT and - after some initial confusion about AutoLISP LT - we learned that DIESEL is the only programming language left in LT. Suddenly it makes sense to spend some time playing with DIESEL. This article will teach you how to use DIESEL, when to use it and why.
Professional LISP and ADS programmers probably think DIESEL is not worth to think about. Before you turn the page, first answer this little quiz: "Show me the shortest routine to extract the third item from a list of comma-delimited keywords." Here's my solution, which is nothing more than a call to DIESEL:
(defun 3rd (string) (menucmd (strcat "m=$(index,2,\"" string "\")")) )
You see, DIESEL is useful even if you don't have any reason to deal with LT. Now if you knew this before, don't read further... Otherwise: here is some DIESEL for LISPers.
You don't do LISP and you don't do menu macros. OK, maybe
DIESEL really isn't for you. But do you want an AutoCAD object
which displays as a text, but changes its contents automatically
depending on the current environment? E.g. do you want to have a
text string automatically display the name and path of the
current drawing file, or the date it was last edited?
No problem, if you installed the Bonus/Express tools for AutoCAD Release 14 or beyond, look at the RText command. This creates an RText object which acts like I just reported. And what do you do to define the variant text string? You enter a DIESEL expression.
DIESEL was written by John Walker for Autodesk, Inc. John and Autodesk put the program and its source code in the public domain. You can download the complete source code from John Walker's site at www.fourmilab.ch/diesel. This allows you to embed the DIESEL interpreter into your own programs, whether AutoCAD-related or not. If you come across other implementations of DIESEL, let me know at firstname.lastname@example.org.
So far three reasons for professionals who already 'know everything' to read on. Let's now have a look at what DIESEL really is and what you can do with it.
The basic idea behind DIESEL is very simple: "There's
nothing but strings".
The DIESEL interpreter takes a string, processes it, and returns a string. Initially the DIESEL interpreter returns every character from its input stream. But this would be boring. Therefore the DIESEL interpreter accepts special character sequences ("function calls") in the input stream and replaces them with the function result.
There is no interactive version of DIESEL, i.e. you can't type a string, pass it to the DIESEL interpreter and print the result. But it's fairly easy to build one.
If you use full-scale AutoCAD, type the following lines directly at the command line:
(defun c:DIESEL (/ s) (while (/= "" (setq s (getstring "\nDIESEL: " T))) (princ (menucmd (strcat "m=" s))) ) (princ) )
This will implement a new command with a name of DIESEL (what else). It will prompt you for a string, pass this to the DIESEL interpreter, and report the result.
If you use AutoCAD LT and prefer menus, add the following as one line to your menu file, e.g. following the _time command:
[DIESEL]*^C^C_SETENV DIESEL;\_SETENV DIESEL;$M=$(EVAL,"$(GETENV,DIESEL)");
The next time you start LT, the menu will recompile, and you will find a DIESEL command in the appropiate pull-down. Click on "DIESEL". The command will prompt you for a string, pass this to the DIESEL interpreter, and report the result.
If you use AutoCAD LT 97 and prefer toolbars, create a DIESEL toolbar button. To do so, right-click on any tool, select New and enter DIESEL as the toolbar name. The DIESEL toolbar opens but has no tools in it. Select Customize, and drag a blank button from the Custom category to the new toolbar. Under Name and Help enter "DIESEL Test", as Macro enter:
*^C^C_SETENV DIESEL;\_SETENV DIESEL;$M=$(EVAL,"$(GETENV,DIESEL)")
Close all toolbar dialogs. LT will recompile the menu and now you have a DIESEL Test command button. Again it will prompt you for a string, pass this to the DIESEL interpreter, and report the result.
In newer versions of AutoCAD LT you use the same macro, only the way to create the button is a bit different. Use Tools>Customize>Toolbars. Use the Toolbars tab to create the DIESEL toolbar, use the Commands tab to drag a user defined button to this new toolbar, then use the Properties tab to enter name, help string and macro.
Let's start experimenting with our custom-made DIESEL command.
DIESEL just passed the basic test for any programming language. It prints the words "Hello,world!". But that's not a proof, you say? Maybe it did not interpret the input line? OK, test #2:
DIESEL: "Hello,world!" Hello,world!
You see? It really does something. It suppresses the
quotes surrounding our test string. Since everything in DIESEL is
a string, sometimes you need to explicitely say "this is a
string, and this is not!" As with every other programming
language, a pair of quotes indicates "this is a
string!". The DIESEL interpreter accepts this as an input
string, while the returned result is the string without quotes,
as you would expect.
So, how do you put a quote into a string? Simply, use two quotes to represent one - plus a surrounding pair of quotes to tell DIESEL "this is just a string!":
DIESEL: """Hello,world!""" "Hello,world!"
As said before, passing strings through the DIESEL interpreter is not that much fun. Thus Autodesk added a number of useful functions to DIESEL. We may calculate the length of the "Hello,world!" string:
DIESEL: $(strlen,"Hello,world!") 12
Function calls in DIESEL are prefixed by a dollar sign and
surrounded by parentheses. A comma acts as the delimiter between
the function name and its arguments. Arguments run until the next
comma or closing parenthesis. You don't have to add quotes to the
arguments unless they include commas, like in this example. If
you don't add the quotes here, the comma between
"Hello" and "world" ends the strlen argument,
which therefore would return 5.
Using quotes in DIESEL functions is much more difficult than it seems. We therefore change to a simpler test string. More on quotes.
DIESEL: $(strlen,aDIESELteststring) 17
Any character that is not part of a function call - and not a quote - is simply returned. Thus you can easily concatenate words, commands names and options in any way you need it.
DIESEL: SNAP;$(strlen,aDIESELteststring).0 SNAP;17.0
Always remember that DIESEL only takes strings and returns strings. You can calculate with strings, however, as long as they look like numbers:
DIESEL: $(+,1,1) 2
Math functions in DIESEL are +, -, * and /. The DIESEL functions fix, rtos, angtos and edtime take a string, interpret it as a real number, and return it formatted as integer, length, angle or date/time.
Like with any other programming language you can "nest" function calls, i.e. use one function call to calculate the argument of an outer function call.
DIESEL: $(+,$(strlen,aDIESELteststring),$(strlen,anotherstring)) 30
As soon as you start writing DIESEL expressions which nest function calls, it becomes of interest when which function is evaluated and how. Both AutoCAD and AutoCAD LT can report the evaluation sequence and results. Simply set the system variable MACROTRACE to 1. Here's a printout of this last evaluation:
DIESEL: $(+,$(strlen,aDIESELteststring),$(strlen,anotherstring)) Eval: $(+, $(strlen,aDIESELteststring), $(strlen,anotherstring)) Eval: $(strlen,aDIESELteststring) ===> 17 Eval: $(strlen,anotherstring) ===> 13 ===> 30
If you create DIESEL programs, always turn the macro trace on. This will easily point you to eventual errors.
DIESEL would not be a 'real' programming language if it would
not allow you to use some kind of variables to store and retrieve
data. DIESEL variables can only contain strings - as you would
DIESEL does not know any 'internal' variables, but it uses AutoCAD's variables. To retrieve the value of an AutoCAD variable, you use the getvar function:
DIESEL: $(getvar,viewctr) 261.70086705,176.04582323,0
Point-type variables become a comma-delimited string, integer-type and real-type variables become strings. The index function is handy for finding the x or y coordinate of a point, like in this example:
DIESEL: $(rtos,$(index,1,$(getvar,viewctr)),2,3) Eval: $(RTOS, $(index,1,$(getvar,viewctr)), 2, 3) Eval: $(INDEX, 1, $(getvar,viewctr)) Eval: $(GETVAR, viewctr) ===> 261.70086705,176.04582323,0 ===> 176.04582323 ===> 176.046 176.046
To save a variable value, you simply call the setvar
command or AutoLISP function. AutoCAD Release 12 introduced the
USERS* string-type variables for interfacing with DIESEL. AutoCAD
LT missed these variables, but you can use the setenv
command and the getenv DIESEL function as a replacement. See our initial LT macro for an example.
A typical problem when using Variables in DIESEL is when the variables are set or read. Critical to this is The Macro Evaluation Sequence.
The LT macro also shows how to use the eval DIESEL function. The entered string is saved to the DIESEL environment variable, which is part of the ACLT.INI file resp. the Windows registry. Next we retrieve the string using getenv. This however only gives us the string as entered. We need to explicitely force DIESEL to evaluate the string returned by getenv.
To create conditional macros, DIESEL includes the if
and nth functions, where nth is similar to other
programming language's case statement. Conditions also use
strings: the string "0" equals false, the string
"1" or anything other than "0" equals true.
To write expressions you may use the =, <, >, !=, <=, >=, and eq functions. To combine conditions use the and, or, and xor functions. There is no not function, but you can easily use the if function for this task:
DIESEL: $(if,$(getvar,tilemode),,Tilemode is not set) Tilemode is not set
Unlike other programming languages, DIESEL has no commands to
repeat certain actions.
If you're working in LISP, this is of course no problem, as LISP itself has enough control structures to create loops.
If you're working in AutoCAD LT, you can use the automatic repetition of menu macros to implement loops. If your menu macro starts with an asterics (*), AutoCAD will repeat it forever (or until you or the macro forces a break). Using variables, you can build loops which run for a predefined number of times or even loop through the characters in a string. A very complex and fine example of this technique can be found in Robert Freeman's Text on Arc macro.
For several uses for DIESEL see:
I'm sure you will find others. The main DIESEL applications are:
The main source for DIESEL is John Walker's site at www.fourmilab.ch/diesel. Here you'll not only find details about the language, you also can download the complete C language source code to embed DIESEL into your own programs.
For your convenience I have added a reference of all DIESEL commands.
For further information about DIESEL and it's uses, read:
Dietmar Rudolph heads CR/LF Corp. in Essen, Germany. He is a CAD consultant, application developer and well-respected AutoCAD expert. In his spare time he has authored several CAD books, some hundred magazine articles and various CAD manuals. Dietmar has localized various Autodesk products and other programs for use in the German language markets.
This is a shareware document. What is a shareware document? Did this article help you earn some money? Then give something back! Help creating shareware documents by donating something to the author:
Contact CR/LF GmbH
© 2000-2011 by CR/LF GmbH, Essen/Germany. All rights reserved.
No part of this document may be reproduced or published without written consent by CR/LF GmbH.
Last modification: 01.04.2011