Tutorials

Units

Reading and setting the units of a drawing, which live in its HEADER section as variables.

Insertion units

$INSUNITS is a whole number naming the unit of the drawing, which AutoCAD uses to scale blocks, images and xrefs inserted from a drawing of another unit. Among its values: 0 unitless, 1 inches, 2 feet, 4 millimeters, 5 centimeters, 6 meters (AutoCAD’s INSUNITS reference lists them all). A drawing from Create starts in millimeters.

C++
DxfIO::Header::Variable *units = variable(drawing, "$INSUNITS");
std::println("$INSUNITS was {}", std::get<std::int64_t>(units->Values.front().Value));
units->Values.front().Value = std::int64_t{6};
text
$INSUNITS was 4

How lengths are shown

$LUNITS is the format AutoCAD shows lengths in: 1 scientific, 2 decimal, 3 engineering, 4 architectural, 5 fractional (AutoCAD’s LUNITS reference). $LUPREC is how many decimal places. Neither changes a coordinate in the file.

C++
variable(drawing, "$LUNITS")->Values.front().Value = std::int64_t{2};
variable(drawing, "$LUPREC")->Values.front().Value = std::int64_t{2};

Older releases

$INSUNITS came with R2000: a drawing made for R12 has none, and saving to R12 leaves it out.

C++
DxfIO::Document old = DxfIO::Create(DxfIO::Version::R12);
std::println("an R12 drawing has $INSUNITS: {}", variable(old, "$INSUNITS") != nullptr);
text
an R12 drawing has $INSUNITS: false

Save

C++
const auto saved = DxfIO::SaveFile(drawing, "units.dxf");
if (!saved)
{
  std::println("{}", saved.error().What);
  return 1;
}