About Unit Conversion (AutoLISP)


Values that represent distances, volumes, or other forms of measurement can be converted from one real-world unit to another.

The acad.unt file defines various conversions between real-world units such as miles to kilometers, Fahrenheit to Celsius, and so on. The function cvunit takes a value expressed in one system of units and returns the equivalent value in another system. The two systems of units are specified by strings containing expressions of units defined in acad.unt. The cvunit function does not convert measurements of the same type. For example, it does not convert inches into grams.

The first time cvunit converts to or from a unit during a drawing editor session, it must look up the string that specifies the unit in acad.unt. If your routine has many values to convert from one system of units to another, it is more efficient to convert the value 1.0 by a single call to cvunit and then use the returned value as a scale factor in subsequent conversions. This works for all units defined in acad.unt except temperature scales, which involve an offset as well as a scale factor.

The following example code converts a value in inches to centimeters:

(cvunit 1.0 "inch" "cm")

The following example code converts a value from Fahrenheit to Celsius:

(cvunit 32 "fahrenheit" "celsius")

The value returned after converting from Fahrenheit to Celsius is not exactly 0.0, you can use rtos to control the precision of the value returned by cvunit. If you still need a real value, you can convert the string returned by rtos with atof.

(setq temp (cvunit 32 "fahrenheit" "celsius"))

(setq temp (rtos temp 2 2))

(setq temp (atof temp))