From c0a6a139cf9e53bcf1ae3d1867b02c10ab2b5754 Mon Sep 17 00:00:00 2001 From: Seth Hillbrand Date: Mon, 23 Feb 2026 08:53:11 -0800 Subject: [PATCH] Add $INSUNITS header variable to DXF export The DXF exporter was missing the $INSUNITS header variable, which tells CAD software what physical units the drawing coordinates represent. Without it, other programs have no reliable way to determine the coordinate scale. Add $INSUNITS to the HEADER section alongside the existing $MEASUREMENT variable. When exporting in millimeters, $INSUNITS is set to 4; when exporting in inches, it is set to 1. Fixes https://gitlab.com/kicad/code/kicad/-/issues/23181 --- common/plotters/DXF_plotter.cpp | 8 +++++++- include/plotters/plotter_dxf.h | 11 +++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/common/plotters/DXF_plotter.cpp b/common/plotters/DXF_plotter.cpp index 34e2588360..0e3242ae73 100644 --- a/common/plotters/DXF_plotter.cpp +++ b/common/plotters/DXF_plotter.cpp @@ -701,12 +701,14 @@ void DXF_PLOTTER::SetUnits( DXF_UNITS aUnit ) case DXF_UNITS::MM: m_unitScalingFactor = 0.00254; m_measurementDirective = 1; + m_insUnits = 4; break; case DXF_UNITS::INCH: default: m_unitScalingFactor = 0.0001; m_measurementDirective = 0; + m_insUnits = 1; } } @@ -779,6 +781,10 @@ bool DXF_PLOTTER::StartPlot( const wxString& aPageNumber ) "$MEASUREMENT\n" " 70\n" "{}\n" + " 9\n" + "$INSUNITS\n" + " 70\n" + "{}\n" " 0\n" "ENDSEC\n" " 0\n" @@ -873,7 +879,7 @@ bool DXF_PLOTTER::StartPlot( const wxString& aPageNumber ) "-0.2\n" " 0\n" "ENDTAB\n", - GetMeasurementDirective() ); + GetMeasurementDirective(), GetInsUnits() ); // Text styles table // Defines 4 text styles, one for each bold/italic combination diff --git a/include/plotters/plotter_dxf.h b/include/plotters/plotter_dxf.h index e5f851f0c2..34017011ea 100644 --- a/include/plotters/plotter_dxf.h +++ b/include/plotters/plotter_dxf.h @@ -476,6 +476,16 @@ public: return m_measurementDirective; } + /** + * Get the correct value for the $INSUNITS header variable given the current units. + * + * @return the $INSUNITS value (1 = inches, 4 = millimeters) + */ + unsigned int GetInsUnits() const + { + return m_insUnits; + } + // Finds the nearest legacy color to the given RGB values (aR, aG, aB). int FindNearestLegacyColor( int aR, int aG, int aB ); @@ -493,4 +503,5 @@ protected: DXF_UNITS m_plotUnits; double m_unitScalingFactor; unsigned int m_measurementDirective; + unsigned int m_insUnits; };