Resolving problems with units. Post 8 in series “AutoCAD Cleanup”

Scenario: Some of your files were created in another country. The units are set to Architectural when you received the files. All objects’ LTSCALEs are set to 1. Some layers in both files use the HIDDEN linetype. You referenced these files to the files created and worked on in the US and experience the following problems:

For AutoCAD 2005 or earlier:

  • No matter what LTSCALE you set either lines from the US files or lines from the foreign files do not scale correctly.

For AutoCAD 2006 or later:

  • No matter what LTSCALE you set, either lines from the US files or lines from the foreign files do not scale correctly.
  • When you reference foreign file in AutoCAD 2006 or newer, in addition to the problems with the LTSCALE, the whole foreign XREF file doesn’t scale correctly and is not located in the right place even though you attached it with the scale=1 and insertion point at 0,0,0.

Here is what happened and how to correct this.

There are several variables in AutoCAD that control units.

MEASUREINIT (0 English; 1 Metric) sets the initial drawing units as English or metric. MEASUREINIT controls which units, hatch pattern and linetype definition files a new drawing uses if not started from a particular template. MEASUREINIT is saved in the registry and the original value is determined by the geographic location of the installation.

MEASUREMENT is stored in the drawing and always overrides the MEASUREINIT setting.

When the foreign file was created, it took the local (metric) value of Measureinit and saved it in the file as the value for MEASUREMENT. At that time the operator may have changed the units to Architectural, but did not change other unit variables. In the course of his work, the metric linetype definition for HIDDEN line from the ACADISO.lin was loaded and used in the drawing.

The US drawing was started with MEASUREMENT set for English units and HIDDEN line definition from ACAD.lin (English).

The linetype names are identical in ACAD.lin and ACADISO.lin, but definitions are different. After the definitions are loaded into the file, it is not clear what .lin file they came from.

To correct the linetype issue you need to re-load linetype definitions from the ACAD.lin file into the foreign file. While you are at it, set the MEASUREMENT=0.

The second problem is illustrated by the units dialog boxes

For AutoCAD 2005 and earlier it says:

Units to scale drag-and-drop content.

For AutoCAD 2006 and later it says:

Units to scale inserted content.

units1
units2

These values are controlled by the INSUNITS variable, which is saved in the drawing. The foreign drawing had it set to something other than inches, but the file did not have any symptoms until AutoCAD2006. Before 2006 this variable only affected drag-and-drop operation.

Possible values of INSUNITS are: 0-Unspecified (unitless); 1-Inches; 2-Feet; 3-Miles; 4-Millimeters; 5-Centimeters; 6-Meters; 7-Kilometers; 8-Microinches; 9-Mils; 10-Yards; 11-Angstroms; 12-Nanometers; 13-Microns; 14-Decimeters; 15-decameters; 16-Hectometers; 17-Gigameters; 18-Astronomical Units; 19-Light Years; 20-Parsecs

The only remaining question is what will happen when INSUNITS in either file is set to 0? (unitless).This situation is controlled by INSUNITSDEFSOURCE and INSUNITSDEFTARGET. They can be set in the Options-> User Preferences. The values are similar to INSUNITS.

The following script will fix all unit related the problems

-UNITS 4 64 1 0 0 n
 INSUNITS 1
 MEASUREMENT 0
 EXPERT 3
 LINETYPE LOAD * ACAD.LIN
 blank line
 EXPERT 0
 -PURGE LT *ISO* N
 QSAVE
 blank line

Lines 1-3 simply set unit variables. Line 4 forces the linetype command to work in text mode without issuing prompts for user intervention (it needs EXPERT 3 mode for that, which I reset back to default EXPERT 0 after I am done with the LINETYPE command). After loading all linetypes from ACAD.lin, I purge only the linetypes with a name containing “ISO”. (I could have purged all unused linetypes or even all unused definitions.)

Using scripts to run LISP or VBA routines and setting variables. Post 7 in series “AutoCAD Cleanup”

Run LISP routines or VBA macros

You can batch process multiple files using scripts which call LISP or VBA macros.

Example: the following script runs a lisp routine to delete layer filters. It loads the Layerfilterdelete.lsp file, then deletes layer filters using a command LFD defined in the routine.

(LOAD “C:\\AU-CLEANUP\\EXERCISES\\SCRIPTING\\LAYERFILTERDELETE.LSP”)
 LFD
 QSAVE
 blank line

Layerfilterdelete.lsp was downloaded from AcadX.com, author: R. Robert Bell.

(DEFUN C:LAYERFILTERSDELETE () (VL-LOAD-COM) (VL-CATCH-ALL-APPLY ‘(LAMBDA () (VLA-REMOVE (VLA-GETEXTENSIONDICTIONARY (VLA-GET-LAYERS (VLA-GET-ACTIVEDOCUMENT (VLAX-GET-ACAD-OBJECT)))) “ACAD_LAYERFILTERS”))) (PRINC “\NALL LAYER FILTERS HAVE BEEN DELETED.”) (PRINC)) (DEFUN C:LFD () (C:LAYERFILTERSDELETE))

If a routine expects a user response, you need to script the response also.

Set up Variables

You can use scripts to set up variables. Here is an example where you set some variables you need for your environment. Keep in mind that not all variables are saved in files, some are saved in the registry, and some cannot be saved at all.

ATTMODE 1
 ATTDIA 1
 DIMASSOC 2
 FILLETRAD 0
 FILLMODE 1
 MIRRTEXT 0
 PDMODE 0
 VISRETAIN 1
 XEDIT 0
 QSAVE
 blank line

Dealing with CTB and STB types of DWG. Post 6 in series “AutoCAD Cleanup”

DWG files come in two flavors. One uses traditional CTB (color based plot tables), the other uses STB (style based plot tables). Plot style is a general property that can be assigned to an object or a layer in the STB “flavored” DWG file. An object can also inherit this property from a layer much like color, linetype or lineweight when set to ByLayer.

In DWG file of CTB flavor, the plot style property is set to ByColor and cannot be changed.

If PSTYLEPOLICY (a variable saved in the registry) is set to 1, new files (unless created from templates) will be CTB flavor of DWG and will use CTB for plotting; when it is set to 0, new files will be STB flavor of DWG and will use STB for plotting.

When you need to convert DWG files from STB flavor to CTB flavor, you need to strip the plot style property from the objects and layer definitions. This can be done easily using the following script. DWG files of CTB flavor will not be affected by this script.

CONVERTPSTYLES
 QSAVE
 Blank Line

When you need to convert from CTB to STB, you need to add that property to the objects and layer definitions.

Before converting a CTB flavor of DWG file to STB flavor of DWG file, you first need to have a STB table which you will be able to use with your former CTB DWG file.

To accomplish this, type CONVERTCTB at the command line and select the CTB to be converted to STB. IN this example, I selected ACAD.CTB to be converted to ACAD2.STB

You can then use the following script to batch convert CTB DWG files to use ACAD2.STB. DWG files of STB flavor will not be affected by this script.

CONVERTPSTYLES
 ACAD2.STB
 QSAVE
 Blank Line

Note: Files that were using STB, when converted to CTB usually plot best with the Monochrome.ctb pen table. For color and grayscale output, use true colors.

Replace an XREF with another XREF. Post 5 in series “AutoCAD Cleanup”

If you are using XREFs for borders, you can use script to replace a current border with a different one. This example assumes that

  • the layer G-Xref exists (but if not, you can script making it
  • the border needs to be overlayed in the paperspace
  • old and new border have the same position in respect to 0,0,0

The following script replaces the XREF border CD_SHEET_E_VERT in paperspace. You may use it to replace XREF borders from a consultant with your company border.

The new border BR08-4851.DWG is overlaid on G-XREF layer and its full path is then converted to relative path (we will talk more about full and relative paths in future posts)

TILEMODE 0
 -XREF D CD_SHEET_E_VERT
 -LAYER S G-XREF
 blank line
 -XREF O P:\4000\4851\DRAWINGS\REF\ARCH\BR08-4851.DWG
 0,0,0 1 1 0
 -XREF P “BR08-4851″
 ..\..\REF\ARCH\BR08-4851.DWG
 QSAVE
 blank line

Force Objects Properties to ByLayer. Post 4 in series “AutoCAD Cleanup”

In AutoCAD 2008, there is a new cleaning tool: SETBYLAYER (menu: Modify->Change to ByLayer). Before using this command in a script, let’s take a look at its settings. Select it from the menu or type SETBYLAYER from the command line, then type “s” to get to the setbylayer settings dialog box. Here you can easily select which properties will be changed. But dialog boxes cannot be used in scripts, so we will use the SETBYLAYERMODE variable to select the properties that we want to change.

setbylayer1The default value of the SETBYLAYERMODE variable is 127. It is NOT saved in the drawing. The picture on the left shows value corresponding to each property. In addition:
+32 Changes ByBlock to ByLayer
+64 Includes blocks when changing ByBlock to ByLayer

To select several properties, add these values. For example, to change just color and lineweight to ByLayer including all objects in blocks and ByBlock objects, set SETBYLAYERMODE=1+4+32+64=101.

The following script will change only color and lineweight of all objects (including set to ByBlock and nested in the blocks) on unlocked layers in modelspace and layout. Note that there are two spaces between “ALL” and first “Y” and one space between first and second “Y”

SETBYLAYERMODE 101
 TILEMODE 1
 SETBYLAYER ALL Y Y
 TILEMODE 0
 SETBYLAYER ALL Y Y
 QSAVE
 Blank Line

Scripting and using ScriptPro. Post 3 in series “AutoCAD Cleanup”

It is very tedious and boring when you need to repeat the same group of operations on many files. To speed up such tasks you can use AutoCAD scripting and a free utility ScriptPro; It allows you to run the same script on many files in a batch mode. It can be installed as a part of Migration Tools for AutoCAD 2004- 2006 or as a separate tool for AutoCAD 2007-2009. After installationy you can find it in the Autodesk Program Group

scriptpro1

ScriptPro runs outside of AutoCAD. It starts a session of AutoCAD for every file on the list, runs the script on it and then closes that session of AutoCAD and starts on the next file.

Cleanup tasks are most likely candidates for scripting because similar problems are most likely exist in many files of the same dataset.

If you can do something in AutoCAD using just your keyboard, you can script it. A script is just a text file with a .SCR extension; it tells AutoCAD what to do just as if an operator types the commands at the command line.

Let’s create a script for purging, auditing and repairing a file.

  1. Open any drawing. Type –purge at the command line. You need to pre-pend “-“ to many AutoCAD commands to force them into the command line mode instead of dialog box. Then just follow the prompts at the command line to purge everything.
  2. Start Notepad. You can start typing commands as you would enter them in AutoCAD. I’m a bad typist, I so I simply copy my “conversation” with AutoCAD from the command window and paste it into Notepad. Then I delete AutoCAD’s responses and leave only my commands.
  3. A couple of things that you need to know about scripting syntax: every space and every new line is interpreted like pressing the ENTER key in AutoCAD, so be careful with extra spaces and lines. To make my scripts more readable, I write one command per line, no spaces at the end of the line, and finish my scripts with one empty line.
  4. Audit checks the file for corruption, and then repairs what it can. It checks the whole file but repairs only the objects in the current space. Most of the time corrupted elements reside in model space. AUDIT also diagnoses loaded XREFs and, if errors are found, may report them as fixed. But it cannot write changes back to XREFs, so next time it will report them again. It is better to repair XREFs first or use RECOVERALL on the files with reported problems

Here is our first script line-by-line. There are no spaces at the end of each line and no empty lines except where noted.

line #
script line
comment
1 -PURGE ALL * N-PURGE ALL * N Purges all elements with any name, does not ask for confirmation (repeat at least 2 times)
2 -PURGE RE * N This line will delete all unused registered applications (not available from dialog box interface and)
3 TILEMODE 1 Switches to model space
4 AUDIT Y Audits and fixes corrupted elements
5 TILEMODE 0 Switches to layout
6 AUDIT Y Audits and fixes corrupted elements
7 QSAVE Saves the changes
8
blank Line
Blank line (if prefer, you can use a space after QSAVE instead of empty line)

Now save the text file with .scr extension and run the script on an AutoCAD file to test it (Tools->Run Script).

To use a script in batch mode on multiple files, I added QSAVE at the end. If I do not, the script will still run, but results will not be saved.

In the several following posts we will look at the scripts for various tasks

Scripting Tips

  • Use short scripts as “building blocks” for longer scripts. This way you will need to open and close you are going to process. Check if ScriptPro has timed out on any files after the processing is completed. the file only once. Huge timesaver.
  • Check for the unexpected. You may encounter locked layers, corrupted files that cannot be repaired by AUDIT, proxy elements for which you do not have object enablers, etc. To verify that your scripts actually worked, examine the log file after batch processing.
  • Check your old scripts when/if using with newer version of AutoCAD. Commands can change.
    Before starting batch processing, make sure that no one is working on or planning to open any files