Wednesday, September 26, 2007

Perform Loop Operations in AutoCAD LT using DIESEL

It's well known fact that you can not perform loop operations using DIESEL as you do in AutoLISP or other powerful programming languages. Isn't it really frustrating to have given up so many interesting tasks due to the absence of loop structures inside DIESEL? Let's give a try to stretch the LT limits a little bit and see how to mimic the loop operations using DIESEL.

The following macro illustrates how to use DIESEL to perfrom a loop operation. This macro calculates the factorial of a given number by iterating a required number of times. You can use the same logic to create various loop structures as per your requirement.

*^C^C$M=$(if,$(<=,$(getvar,USERI2),$(getvar,USERI1)),setvar;USERI3;$M=$(if,$(=,$(getvar,USERI2),1),1,$(*,$(getvar,USERI2),$(getvar,USERI3)));setvar;USERI2;$(+,$(getvar,USERI2),1),_modemacro;"The factorial of "$(getvar,USERI1)" is "$(getvar,USERI3);^C)

Before using the above macro, you need to run a simple macro to set up the user variables and the status bar. You need to supply the required number at the USERI1 prompt.

^C^C_modemacro;.;setvar;useri1;\;setvar;useri2;1;setvar;useri3;0;

Isn't it quite simple to do an iteration using DIESEL? Basically a loop macro has the following componetns.

  • *^C^C in the beginning to have continuous operations
  • A conditional statement using a counter variable to continuously check against the loop upper limit.
  • If the condition is not met, do the required operations and continue.
  • If the condition is met, then terminate the macro using ^C at the end.
Since I have used USERIx system variables, you can not calculate factorials for numbers greater than 7 using the above macro (due to integer limit). If you need to have silent mathematical operations, you could better try with SETENV and GETENV instead of SETVAR and GETVAR. Try to experiment with this and come up with new interesting macros. Best of luck for your efforts...!!

1 comment:

Idrees Tirur said...

Dear Haris
My name is Idrees I am working in Abu dhabi as electical Design and draftsman
Could u contact to me this email ID
idreestirur@gmail.com