Thursday, August 23, 2007

Auto Increment Numbering Macro for AutoCAD LT

Have you ever tried automating tasks in AutoCAD LT? If so, you will find it really difficult due to lack of programming support inside it. Often this leads you to experiment with DIESEL to achieve the results you normally achieve in full version of AutoCAD. Here is a tricky macro to automate the process of increment numbering in AutoCAD LT.

*^c^c_text;\;;$M=$(+,$(getvar,USERR1),$(getvar,USERR2));setvar;USERR1;$M=$(+,$(getvar,USERR1),$(getvar,USERR2));

Configure the macro in a button (or wherever you are comfortable). Click on the button once and click on the required locations continuously to keep the incremented numbers. use USERR1 system variable to set the starting number and USERR2 for the increment. For example, suppose you need to start numbering from 1.001 and continue like 1.002,1.003,1.004........, set USERR1 to 1 and USERR2 to .001. This will enable you to do the numbering as stated before.

The above macro is restricted to work only when the text height in the current text style is 0. If you have the habit of assigning specific text height to a text style, then you should better use the following macro.
Quote:

*^c^c_text;\;$M=$(+,$(getvar,USERR1),$(getvar,USERR2));setvar;USERR1;$M=$(+,$(getvar,USERR1),$(getvar,USERR2));

I just removed only one semicolumn from the second macro after the 'Text' command. It's becuase the 'Text' command skips one step (Text height) if the current text style has already got text height assigned. Hope AutoCAD LT people find this tip very useful. Needless to say, it will give you the same result in AutoCAD full version.

*EDIT*

Thanks to a very good suggestion from Russ for adding a prefix, I ended up adding both prefix and suffix to the numbers.

*^c^c_text;\;;$M=$(getvar,USERS1)$M=$(+,$(getvar,USERR1),$(getvar,USERR2))$M=$(getvar,USERS2);setvar;USERR1;$M=$(+,$(getvar,USERR1),$(getvar,USERR2));

The macro uses USERS1 system variable to hold the prefix and USERS2 for the suffix. Thanks to comments from an anonymous visitor, I realised that USERSx system variables are not supported in AutoCAD LT. So I replaced the macro with a new one using (getenv,variable) function in place of (getvar,USERSx).


*^c^c_text;\;;$M=$(getenv,StrPrefix)$M=$(+,$(getvar,USERR1),$(getvar,USERR2))$M=$(getenv,StrSuffix);setvar;USERR1;$M=$(+,$(getvar,USERR1),$(getvar,USERR2));

You need to set two environment variables (I used StrPreifx for prefix and StrSuffix for suffix) using SETENV command which, I beleive, is available in AutoCAD LT. Don't forget to respect the text height of the current text style as mentioned eariler. Again, I don't have AutoCAD LT to test this macro. I would be really thankful if somebody could test it on LT and post a feedback on it.

*EDIT*

15 comments:

Teri said...

Nice Blog :)

Anonymous said...

Nice work.

Don't suppose you could modify this to include the thirds user variable to hold some prefix text so that you could autonumber something like: W100, W101, W102.....

Russ

har!s said...

I have modified the macro to include a suffix and prefix. Thanks for the suggestion.

har!s

Anonymous said...

Does LT allow the use of the USERSx variables?

har!s said...

To be frank, I have never seen AutoCAD LT in my life :-). Since it supported USERIx and USERRx, I guessed it would also support USERSx. Now my guess seems to be wrong. I may have to use SETENV and GETENV instead of USERSx variables. Thanks a lot for the suggestion.

Anonymous said...

At last, I found a command that's working , and I can confirm it works with AutoCad LT 2006. Even with the prefix and suffix.
But now, my question is : How do I set the variables StrPrefix or StrSuffix to nothing? Cause I don't always need a prefix but once it has been set, I can change the value but not turning it to nothing (no value). Thanks for answering.
Sorry if this question is stupid, I'm just beginning with the programmation.
Emmanuel from France.

har!s said...

Hi Emmanuel,
Thanks for testing and providing a feedback on that. For resetting the environment variables, you will have to use SETENV command, which I believe is available in AutoCAD LT (It's not available in vanilla AutoCAD). Try something like this

^C^C_Setenv;strPrefix;.;;strSuffix;.;

That's based on an assumption. You will have to try and rectify that.

Anonymous said...

thanks for your quick answer.
The SETENV command does exist on AutoCad LT.
The command line given works but only to set the variables to "." (dot) That means that the Prefix and suffix, will be a dot. for example : .1. .2. .3. ...etc
And I would like to set the variables to nothing, just to have 1, 2, 3 ... (only the numbers).
it would be nice if it was possible to set "" for example but it take it as text that means that the increment will be ""1"" ""2"" ...
The question is : what is the code or caracter to set the variable to nothing (that means no value)?
Meanwhile, the caracter "space" ends any command, so it doesn't fit.
Thanks for answering.
Emmanuel

har!s said...

Emmanuel,
Are you sure that setting strPrefix or strSuffix to "" (double quotes without space) stores that as it is in the environment variable? For me, when I tried to set it up with the follwing AutoLISP code, it cleared the variable.

(Setenv "strPrefix" "")

I don't quite understand why the equivalent commands don't work in AutoCAD LT.

Russ said...

Has anyone made this work with LT 2009? It seems to be broke since the upgrade.

Russ

Jim Robb said...

I have also had problems resetting the strPrefix & strSuffix the "" doesn't change the setting and " " puts a space in. Any other ideas Har!s?
PS its a great macro I love it.

har!s said...

Hi Jim,
Thanks for your comments. Unfortunately, I don't have AutoCAD LT available with me to experiment on this issue. Most of my LT macros were being done based on assumptions.

Anonymous said...

great, It's working fine.
I need extract attributes of block using diesel because I am using Autocad LT, do you have ideas, or a good way to start?

Casey Williams said...

can you put an example up with the start value of 1 and an increment of 5? is there a way to be prompted for increment and start value?

Anonymous said...

Hi,
thanks for the job, but setvar does not exist on LT...