AutoCAD Menu Streamlining and Updating or When does 8 = 37?

I had the opportunity to update some AutoCAD toolbars recently and looking through other’s code is always a good time to either enhance something or learn from it. I chose to enhance the code and streamline it for future expansion.

The code itself is classic AutoCAD toolbar stuff. Lots of semicolons, back slashes, command line commands and options, and unfortunately, lots of hardcoded values associated with each scale. The tools worked, mostly, but trying to extend them or re-use them for new tools was problematic at best and hair-wrenching at times. As I looked through the toolbar code and built a table of the values for various scales, an opportunity to use some good old diesel code presented itself.

Let me give you a little background on the tools: The menu consisted of a series of toolbars, each one intended to be used for a particular drawing scale, providing a common method of inserting tags, dimensions, text, symbols, etc. The toolbars were wrapped up in flyouts, so that when a scale change was required, you simply picked it from the flyout and the correct associated tools were exposed and ready for use.

Anyway, as I started to build the tables to better understand the code and define patterns so that I could migrate this functionality for a Meter based metric project, I began to see how painstaking this process was going to become. To resolve the issues and simplify the maintenance, I changed all the hardcoded values into variables that could be multiplied by the current dimscale value. In doing so, I was able to create 10 new metric based toolbars from one master toolbar that only required two search and replace operations. Rather than copying each toolbar 10 times and then searching and replacing 22 different values for each scale, I reduced the effort to three operations, copy, search & replace, saveas.

Here is the tip: When does 8=37?

When you replace hard coded values with flexible diesel code!

Example code: $M=$(*,-0.0015875,$(getvar,dimscale))

Don’t use hardcoded values in your menu macros, rather generate a value based on a variable and use diesel to retrieve it. So when does 8=37?

When you build a diesel macro to retrieve a value multiplied by the dimscale variable. The best thing about using diesel is it is evaluated so the 37 characters are not counted against your character limit. Only the number of characters represented in the value returned by the function is counted against the character limit. And variables make your tools more flexible. Instead of creating 40 new toolbars for the new metric tool menu, I was able to create 4 instead….now back to migrating legacy code with you!!!

Other Modemacro goodies

The recent LT Unlimited blog article by Kate Morrical titled: “Customize the Status Bar with MODEMACRO” reminded me of some customization I recently wrote. I received a call from a remote architect who needed some assistance with Linetypes. She was concerned that when switching from model to paperspace her linetypes weren’t always displaying correctly.

I explained that she should be adjusting her ltscale variables in order to see the linetypes when in modelspace. She wished for an “automatic solution”. So I wrote a quick macro, using VBA, to automatically set the correct lt and psltscale values based on the current value of dimscale. I tied the macro to the AcadDocument_LayoutSwitched event so that whenever she switched layouts, the macro would update the values.

To provide graphic feedback that this system was working I chose to use the MODEMACRO system variable. Here is the macro in action with visual feedback occuring on the status bar.

Here is the code from the vba macro that sets the MODEMACRO variable. You can see from the code snippet below, that I am displaying the current value of the MEASUREMENT, DIMSCALE, LTSCALE, and PSLTSCALE variables.

ThisDrawing.SetVariable “modemacro”, “WB_STATUS- UNITS: $(IF, $(=,
$(getvar,MEASUREMENT),0),IMPERIAL,METRIC) SCALE=$(GETVAR,DIMSCALE)
LTS=$(GETVAR,LTSCALE) PSLTS=$(GETVAR,PSLTSCALE)”