Thursday, April 17, 2025

Setting AutoCAD drawing UNITS using AutoLISP

Program to configure AutoCAD drawing UNITS for Survey Plan Drawing

 One important ritual you must perform whenever you are starting a new drawing in AutoCAD, is to setup the drawing UNITS according to your drawing needs or specification.

For survey drawings, you will make not less than ten clicks to complete the process. So, I decided to automate this process using AutoLISP.


The UNITS settings are available via the following AutoCAD system variable commands:-

  • LUNITS -  system variable that controls the linear/length drawing units
  • LUPREC -  system variable that controls the linear/length precision
  • AUNITS -  system variable that controls the angles drawing units
  • AUPREC -  system variable that controls the angles precision
  • ANGBASE -  system variable that controls the angle orientation
  • ANGDIR -  system variable that controls the angle direction to be either Clock-clockwise or Counter-clockwise
  • INSUNITS - system variable that controls the insertion scaling

The picture above show what each variable is found.

To get the default values you want to use, use the getvar function in AutoLISP to confirm the values you want to set. The values can be checked as follow:-

(getvar "LUNITS")
(getvar "LUPREC")
(getvar "AUNITS")
(getvar "AUPREC")
(getvar "ANGDIR")
(getvar "ANGBASE")
(getvar "INSUNITS")

The setvar function in AutoLISP to set the drawing units in AutoCAD. For example, running this expression below will set the linear/length drawing units to Decimal units.

(setvar "LUNITS" 2)

The Unit format is as follow: 1=Scientific, 2=Decimal, 3=Engineering, 4=Architectural, 5=Fractional

To set the linear/length precision,the expression is like this below which will set the precision to four decimal places: (0.0001). To change the decimal place, change the value 4 to a new number.

(setvar "LUPREC" 4)

Similar concept is applicable to the other units system variables above. For AUNITS, it requires an integer between 0 and 4 and AUPREC requires an integer between 0 and 8.

(setvar "AUNITS" 1)
(setvar "AUPREC" 1)

ANGDIR requires 0 or 1 only. ANGBASE can be set to any number but in this case, I will use 270 to set it to NORTH.

(setvar "ANGDIR" 1)
(setvar "ANGBASE" 270)

For the insertion scaling, I will 6 for meters.

(setvar "INSUNITS" 6)


Putting it altogether:-


;;;Program to configure AutoCAD drawing UNITS for Survey Plan Drawing

;;;Step 1: Manually configure the settings you wanted

;;;Step 2: Determine the default setting using the GETVAR SYSVariable

(defun C:GETDEFAULTUNITS(/ linear-units linear-units-precision angular-units angular-units-precision angule-dir base-angle insertion-scale)
	(setq linear-units (getvar "LUNITS") ; 2
	      linear-units-precision (getvar "LUPREC") ; 2
	      angular-units (getvar "AUNITS") ; 1
	      angular-units-precision (getvar "AUPREC") ; 1
	      angule-dir (getvar "ANGDIR") ; 1
	      base-angle (getvar "ANGBASE") ; 270
	      insertion-scale (getvar "INSUNITS") ; 6
	  ) ; end setq
	
	(princ)
) ; end defun



;;; Step 3: Configure the units using the values obtained above
(defun C:SETUNITS(/)

	(setvar "LUNITS" linear-units)
	(setvar "LUPREC" linear-units-precision)
	(setvar "AUNITS" angular-units)
	(setvar "AUPREC" angular-units-precision)
	(setvar "ANGDIR" angule-dir)
	(setvar "ANGBASE" base-angle)
	(setvar "INSUNITS" insertion-scale)

	(princ)
); end defun



That is it!

No comments:

Post a Comment