Programatically setting DWGUNITS

Programatically setting DWGUNITS

Anonymous
Not applicable
3,955 Views
22 Replies
Message 1 of 23

Programatically setting DWGUNITS

Anonymous
Not applicable

Hi Folks, does anyone know how to programatically read and set DWGUNITS for the current drawing?

 

Environment:  Objectarx, C#, Map3D 2018

 

// Get the Database Object
doc.Database.DwgUnits =  // DOES NOT EXIST

Thank you

 

0 Likes
3,956 Views
22 Replies
Replies (22)
Message 2 of 23

_gile
Consultant
Consultant

Hi,

 

As far as know DWGUNITS is a command used to set multiple system variables as INSUNITS, LUNITS, LUPREC, ...



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 3 of 23

Anonymous
Not applicable

gile, thank you but that does not apply to Map3D.   Here is the command and options in Map3D for DWGUNITS.  I am trying to Programatically set DWGUNITS.

 

Command: -DWGUNITS


Drawing units:
1. Inches
2. Feet
3. Millimeters
4. Centimeters
5. Decimeters
6. Meters

 

This is what I am trying to do:

 

// Get the Database Object
doc.Database.DwgUnits =  // DOES NOT EXIST

 

0 Likes
Message 4 of 23

Ed__Jobe
Mentor
Mentor

Giles is correct. I checked with tech support some time ago and there is no api to do what you want. Notice that you entered DWGUNITS ad the "command" prompt. It is just a command that does a bunch of stuff behind the scene.

Ed


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
How to post your code.

EESignature

0 Likes
Message 5 of 23

Anonymous
Not applicable

Ed thank you for your input!  I learned something new.  How do I then set the drawing unit to a different value programmatically?  Basically I need to change it from Inches to Feet before I import a file using ReadDwgFile.

0 Likes
Message 6 of 23

_gile
Consultant
Consultant

If INSUNITS is not correctly set in both source and target drawings, you should set the INSUNUTSDEFSOURCE and/or the INSUNITSDEFTARGET system variables.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 7 of 23

Anonymous
Not applicable

Thank you for the info Gilles!  I checked source/target drawings.  I do not have rights to modify the file I am importing. (source)  I only have rights to modify the new file that I will be importing into. (target).  I checked the source file and it has INSUNITS set to 1.   The new file has INSUNITS set to zero.  Would I set the new file INSUNITSDEFTARGET to 2 (feet)?

0 Likes
Message 8 of 23

Anonymous
Not applicable

Quick update:  I set INSUNITS to Feet.

Then went to the command line BUT DWGUNITS is STILL set to Inches:

 

 

BEFORE doc.Database.Insunits = Inches

doc.Database.Insunits = UnitsValue.Feet;

AFTER doc.Database.Insunits = Feet

AutoCAD Text Window:

Command: INSUNITS

Enter new value for INSUNITS <2>: *Cancel*

Command: -DWGUNITS

Drawing units:
1. Inches
2. Feet
3. Millimeters
4. Centimeters
5. Decimeters
6. Meters
Unit for length <1>: *Cancel*

As you can see, I set INSUNITS to feet but DWGUNITS continues set to Inches.

 

0 Likes
Message 9 of 23

_gile
Consultant
Consultant

INSUNITSDEFSOURCE and INSUNITSDEFTARGET have to be used if INSUNITS is equal to 0 respectively in the source and in the target file.

In the cas you describe you do not have to set INSUNITSDEFSOURCE as INSUNITS is equal to 1, and you should set INSUNITS to 2 in the taget file so that you won't have to set INSUNITSDEFTARGET anymore.

If this does not work (due to some issue with Civil 3d), you can use the Database.Insert() overload method which take a Matrix3d as argument passing it the following scaling matrix.

Matrix3d.Scaling(1.0 / 12.0, Point3d.Origin);


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 10 of 23

Anonymous
Not applicable

Gilles, how can I change DWGUNITS from 1 (inches) to 2 (feet)?  Changing INSUNITS did not alter DWGUNITS.  Thank you.

0 Likes
Message 11 of 23

_gile
Consultant
Consultant

As said in the first replies, you have to call the -DWGUNITS command.

But if the issue is related to insertion, this is managed by the INSUNITS system variable, read this topic.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 12 of 23

dgorsman
Consultant
Consultant

-DWGUNITS command also adds some AEC content to the file, which I suspect is where the units definition is stored.  It's also the source of rogue AEC data that some have spent a *lot* of time trying to eliminate. 

----------------------------------
If you are going to fly by the seat of your pants, expect friction burns.
"I don't know" is the beginning of knowledge, not the end.


0 Likes
Message 13 of 23

_gile
Consultant
Consultant

@dgorsman wrote:

-DWGUNITS command also adds some AEC content to the file, which I suspect is where the units definition is stored.  It's also the source of rogue AEC data that some have spent a *lot* of time trying to eliminate. 


Yes, running -DWGUNITS in Vanilla AutoCAD creates a new AEC_VARS dictionary in the NOD which contains an AecDbVarsDwgSetup object.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 14 of 23

Anonymous
Not applicable

Ok I have an update.  I found an error in AutoCAD Text Window that could be related to the whole problem:

 

Unit-scaling inserted database...Duplicate definition of block _ClosedBlank ignored.

 

I did a search on this an it mentions that (Just had Gilles had mentioned) INSUNITS should match.  I checked the source and destination and both have it set to 0.  These are the settings for both files:

 

INSUNITS   0

INSUNITSDEFTARGET 1

INSUNITSDEFSOURCE 1

MEASUREINIT 0

MEASUREMENT 0

 

 

 

 

0 Likes
Message 15 of 23

_gile
Consultant
Consultant

@Anonymous wrote:

Ok I have an update.  I found an error in AutoCAD Text Window that could be related to the whole problem:

 

Unit-scaling inserted database...Duplicate definition of block _ClosedBlank ignored.


This has nothing to do with your scaling issue. This is an information message which just means that the source file contains a block definiton name "_ClosedBlank" which already exists in the target block table that it will be ignored.

 


@Anonymous wrote:

I did a search on this an it mentions that (Just had Gilles had mentioned) INSUNITS should match.  I checked the source and destination and both have it set to 0.  These are the settings for both files:

 

INSUNITS   0

INSUNITSDEFTARGET 1

INSUNITSDEFSOURCE 1

MEASUREINIT 0

MEASUREMENT 0


As said iupper and in the provided links to the AutoCAD documentation, the INSUNITS system variable is used to automatically scale the inserted files (as external references or block references) if both values in source and target files are different from 0 (No units).

The INSUNITSDEFSOURCE and INSUNITSDEFTARGET are to be used (in the target file) to emulate the INSUNITS values in respectiveley source and target files.

So, if INSUNITS equals 0 in source file, you can set, in the target file, the INSUNITSDEFSOURCE to the expected value for the source file, this won't change the source file INSUNITS system variable value but let the insertion process act if it was.

If INSUNITS equals 0 in the target file, you could also set the INSUNITSDEFTARGET to the expected value, but it should be better to directly set the INSUNITS value.

 

It looks like you're missing some AutoCAD basics which are absolutely required if you want to be able to customize AutoCAD by programming.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 16 of 23

Anonymous
Not applicable

Setting these to any different combination possible did not help:

 

INSUNITS 

INSUNITSDEFTARGET

INSUNITSDEFSOURCE

0 Likes
Message 17 of 23

andkal
Collaborator
Collaborator

Here is a walkaround via lisp routine how to get "DWGUNITS" number, although it works in a very nasty way. You can also see how to set it with lisp code and deal with popping warning about scaling objects.

(defun c:GET_DWGUNITS ( /  logdirpath1 list2 list1 string1 dwgunts infile1)
;;;    Andkal, v1.01 - 23.03.2019
;;;    Function gets "drawing units" number
;;;    1. Inches
;;;    2. Feet
;;;    3. Millimeters
;;;    4. Centimeters
;;;    5. Decimeters
;;;    6. Meters
    ;-----------------------------------------
    ;setting variables
    ;-----------------------------------------
    (setq
        old_logfilepath (getvar "logfilepath")
        logdirpath1 (strcat (getvar "MYDOCUMENTSPREFIX") "\\TEMP1LOG")
    );setq
    (vl-mkdir logdirpath1)
    (setq old_cmdecho (getvar "cmdecho"))
    (setvar "cmdecho" 1)
    (command "_logfilepath" logdirpath1)
    (command "_logfilemode" 1);Specifies whether the contents of the command history are written to a log file. 
    ;-----------------------------------------
    ;we temporarily change units to read command prompt and save it to log file
    ;-----------------------------------------
    (command "_-dwgunits" 
            ;"'graphscr"
            2    ;units for length
            ""    ;Linear display format
            ""    ;Linear display precision
            ""    ;Scale objects from other drawings upon insert? [Yes/No]
            ""    ;Match INSUNITS to drawing units? [Yes/No]
    );command
    (if (= 1 (getvar "cmdactive") );if warning appears - command types "no"
       (command "no")    ;Scale objects in current drawing to reflect change in units? [Yes/No]
    );if
    ;-----------------------------------------
    (graphscr)
    (setvar "cmdecho" 0)
    (command "_logfilemode" 0)
    ;-----------------------------------------
    ;reading current dwgunits "number" from a log file
    ;-----------------------------------------
    (setq list2 (list) )
    (setq infile1 (open (getvar "LogFileName") "r"))
    (while (setq string1 (read-line infile1) )
        (if (not (null string1))
            (setq list1 (append list1 (list string1) ) );makes a list of strings of each line of this file
        ) ; if
    ) ;while
    (close infile1)
    (setq
            dwgunts (nth 10 list1); current dwgunits are stored in 10th element of the list between < > signs
            dwgunts (substr dwgunts (+ 2 (vl-string-position (ascii "<") dwgunts) ) (strlen dwgunts) )
            dwgunts (vl-list->string (reverse (vl-string->list dwgunts)))
            dwgunts (substr dwgunts (+ 2 (vl-string-position (ascii ">") dwgunts) ) (strlen dwgunts) )
            dwgunts (atoi (vl-list->string (reverse (vl-string->list dwgunts))))
            ;dwgunts (atoi dwgunts)
    );setq
    ;-----------------------------------------
    ;restoring dwgunits and systemvariables
    ;-----------------------------------------
    (command "_-dwgunits" 
            ;"'graphscr"
            dwgunts    ;units for length
            ""    ;Linear display format
            ""    ;Linear display precision
            ""    ;Scale objects from other drawings upon insert? [Yes/No]
            ""    ;Match INSUNITS to drawing units? [Yes/No]
    );command
    (if (= 1 (getvar "cmdactive") );if warning appears - command types "no"
       (command "no")    ;Scale objects in current drawing to reflect change in units? [Yes/No]
    );if
    (graphscr)
    ;-----------------------------------------
    ;restoring systemvariables
    ;-----------------------------------------
    (vl-file-delete (getvar "LogFileName") );deleting log file
    (command "_logfilepath" old_logfilepath)
    (setvar "cmdecho" old_cmdecho)
    ;-----------------------------------------
    (princ "\n------------\n")
    (princ dwgunts)
    (princ) 
);defun

• www.autolisps.blogspot.com - Productivity plugins for Autocad and Autocad MEP
• Autodesk AppStore
0 Likes
Message 18 of 23

andkal
Collaborator
Collaborator

To late to edit post.... So one more time:

(defun c:GET_DWGUNITS ( /  logdirpath1 dwgunts infile1 )
;;;    Andkal, v1.02 - 23.03.2019
;;;    Function gets "drawing units" number
;;;    1. Inches
;;;    2. Feet
;;;    3. Millimeters
;;;    4. Centimeters
;;;    5. Decimetersw
;;;    6. Meters
    ;-----------------------------------------
    ;setting variables
    ;-----------------------------------------
    (setq
        old_logfilepath (getvar "logfilepath")
        logdirpath1 (strcat (getvar "MYDOCUMENTSPREFIX") "\\TEMP2LOG")
        old_cmdecho (getvar "cmdecho")
        old_logfilemode (getvar "logfilemode")
    );setq
    (vl-mkdir logdirpath1)
    (setvar "logfilepath" logdirpath1)
    (setvar "cmdecho" 1)
    (setvar "logfilemode" 1);Specifies whether the contents of the command history are written to a log file. 
    ;-----------------------------------------
    ;we temporarily change units to read command prompt and save it to log file
    ;-----------------------------------------
    (command "_-dwgunits" 
            ;"'graphscr"
            2    ;units for length
;            ""    ;Drawing unit display format
            ""    ;Linear display format
            ""    ;Linear display precision
            ""    ;Scale objects from other drawings upon insert? [Yes/No]    -<SPRAWDZIC CZY DOBRZE BEDZIE WSTAWIAL JAK ZMIENIE NA "NIE"
            ""    ;Match INSUNITS to drawing units? [Yes/No]
    );command
    (if (= 1 (getvar "cmdactive") );if warning appears - command types "no"
       (command "_no")    ;Scale objects in current drawing to reflect change in units? [Yes/No]
    );if
    ;-----------------------------------------
    ;(command "_textscr");turns off textscr (toggle since 2014)
    (graphscr)
    (setvar "cmdecho" 0)
    (command "logfilemode" 0)
    ;-----------------------------------------
    ;reading current dwgunits "number" from a log file
    ;-----------------------------------------
    (setq infile1 (open (getvar "LogFileName") "r") dwgunts "")
    (while (and (read-line infile1) (= (vl-string-position (ascii "<") dwgunts) nil) )
        (setq dwgunts (strcat dwgunts (read-line infile1) ) )
    ) ;while
    (close infile1)
    (setq
            dwgunts (substr dwgunts (+ 2 (vl-string-position (ascii "<") dwgunts) ) (strlen dwgunts) )
            dwgunts (vl-list->string (reverse (vl-string->list dwgunts)))
            dwgunts (substr dwgunts (+ 2 (vl-string-position (ascii ">") dwgunts) ) (strlen dwgunts) )
            dwgunts (atoi (vl-list->string (reverse (vl-string->list dwgunts))))
    );setq
    ;-----------------------------------------
    ;restoring dwgunits and system variables
    ;-----------------------------------------
    (command "_-dwgunits" 
            ;"'graphscr"
            dwgunts    ;units for length
;            ""    ;Drawing unit display format
            ""    ;Linear display format
            ""    ;Linear display precision
            ""    ;Scale objects from other drawings upon insert? [Yes/No]
            ""    ;Match INSUNITS to drawing units? [Yes/No]
    );command
    (if (= 1 (getvar "cmdactive") );if warning appears - command types "no"
       (command "_no")    ;Scale objects in current drawing to reflect change in units? [Yes/No]
    );if
    ;(command "_textscr");turns off textscr (toggle since 2014)
    (graphscr)
    ;-----------------------------------------
    ;restoring systemvariables
    ;-----------------------------------------
    (command "logfilepath" old_logfilepath)
    (setvar "cmdecho" old_cmdecho)
    (setvar "logfilemode" old_logfilemode)
    ;-----------------------------------------
    ;deleting files
    ;-----------------------------------------
    (vl-file-delete (getvar "LogFileName") );deleting log file
    (setq obj1 (vlax-create-object "Scripting.FileSystemObject"))
    (vlax-invoke obj1 "DeleteFolder" logdirpath1 :vlax-true)
    (vlax-release-object obj1)
    ;-----------------------------------------
    (princ "\n------------\n")
    (princ dwgunts)
    (princ) 
);defun

• www.autolisps.blogspot.com - Productivity plugins for Autocad and Autocad MEP
• Autodesk AppStore
0 Likes
Message 19 of 23

andkal
Collaborator
Collaborator

shorter version

(defun c:GET_DWGUNITS ( /  logdirpath1 dwgunts infile1 )
;;;        Andkal, v1.03 - 23.03.2019
;;;        Function gets drawing units value
;;;        1. Inches    2. Feet    3. Millimeters    4. Centimeters    5. Decimetersw    6. Meters

    ;-----------------------------------------
    ;setting variables
    ;-----------------------------------------
    (setq old_logfilepath (getvar "logfilepath")
        logdirpath1 (strcat (getvar "MYDOCUMENTSPREFIX") "\\TEMP2LOG")
        old_cmdecho (getvar "cmdecho")
        old_logfilemode (getvar "logfilemode")
    );setq
    (vl-mkdir logdirpath1)
    (setvar "logfilepath" logdirpath1)
    (setvar "cmdecho" 1)
    (setvar "logfilemode" 1);Specifies whether the contents of the command history are written to a log file. 
    ;-----------------------------------------
    (command "_-dwgunits" 
            ;"'graphscr"
            ""    ;units for length
            ""    ;Linear display format
            ""    ;Linear display precision
            ""    ;Scale objects from other drawings upon insert? [Yes/No]
            ""    ;Match INSUNITS to drawing units? [Yes/No]
    );command
    (graphscr)
    (setvar "cmdecho" 0)
    (setvar "logfilemode" 0)
    ;-----------------------------------------
    ;reading current dwgunits "number" from a log file
    ;-----------------------------------------
    (setq infile1 (open (getvar "LogFileName") "r") dwgunts "")
    (while (and (read-line infile1) (= (vl-string-position (ascii "<") dwgunts) nil) )
        (setq dwgunts (strcat dwgunts (read-line infile1) ) )
    ) ;while
    (close infile1)
    (setq
            dwgunts (substr dwgunts (+ 2 (vl-string-position (ascii "<") dwgunts) ) (strlen dwgunts) )
            dwgunts (vl-list->string (reverse (vl-string->list dwgunts)))
            dwgunts (substr dwgunts (+ 2 (vl-string-position (ascii ">") dwgunts) ) (strlen dwgunts) )
            dwgunts (atoi (vl-list->string (reverse (vl-string->list dwgunts))))
    );setq
    ;-----------------------------------------
    ;restoring systemvariables
    ;-----------------------------------------
    (setvar "logfilepath" old_logfilepath)
    (setvar "cmdecho" old_cmdecho)
    (setvar "logfilemode" old_logfilemode)
    ;-----------------------------------------
    ;deleting files
    ;-----------------------------------------
    (vl-file-delete (getvar "LogFileName") );deleting log file
    (setq infile1 (vlax-create-object "Scripting.FileSystemObject"))
    (vlax-invoke infile1 "DeleteFolder" logdirpath1 :vlax-true)
    (vlax-release-object infile1)
    ;-----------------------------------------
    (princ "\n------------\n")
    (princ dwgunts)
    (princ) 
);defun

• www.autolisps.blogspot.com - Productivity plugins for Autocad and Autocad MEP
• Autodesk AppStore
0 Likes
Message 20 of 23

sizy458
Advocate
Advocate

Forgot variable 'USERS1'.

 

 

So how i do for variable INSUNITS and INSUNITSDEFSOURCE ,INSUNITSDEFTARGET without passed by API acedSetVar ?

0 Likes