KiCad PCB EDA Suite
EDA_UNIT_UTILS Namespace Reference

Namespaces

namespace  UI
 

Functions

bool IsImperialUnit (EDA_UNITS aUnit)
 
bool IsMetricUnit (EDA_UNITS aUnit)
 
int Mm2mils (double aVal)
 Convert mm to mils. More...
 
int Mils2mm (double aVal)
 Convert mils to mm. More...
 
bool FetchUnitsFromString (const wxString &aTextValue, EDA_UNITS &aUnits)
 Writes any unit info found in the string to aUnits. More...
 
wxString GetText (EDA_UNITS aUnits, EDA_DATA_TYPE aType=EDA_DATA_TYPE::DISTANCE)
 Get the units string for a given units type. More...
 
wxString GetLabel (EDA_UNITS aUnits, EDA_DATA_TYPE aType=EDA_DATA_TYPE::DISTANCE)
 Get the units string for a given units type. More...
 
std::string FormatAngle (const EDA_ANGLE &aAngle)
 Converts aAngle from board units to a string appropriate for writing to file. More...
 
std::string FormatInternalUnits (const EDA_IU_SCALE &aIuScale, int aValue)
 Converts aValue from internal units to a string appropriate for writing to file. More...
 
std::string FormatInternalUnits (const EDA_IU_SCALE &aIuScale, const VECTOR2I &aPoint)
 
constexpr int Mils2IU (const EDA_IU_SCALE &aIuScale, int mils)
 

Function Documentation

◆ FetchUnitsFromString()

bool EDA_UNIT_UTILS::FetchUnitsFromString ( const wxString &  aTextValue,
EDA_UNITS aUnits 
)

Writes any unit info found in the string to aUnits.

Returns
true - when unit was found, false - when unit could not be determined

Definition at line 68 of file eda_units.cpp.

69{
70 wxString buf( aTextValue.Strip( wxString::both ) );
71 unsigned brk_point = 0;
72
73 while( brk_point < buf.Len() )
74 {
75 wxChar c = buf[brk_point];
76
77 if( !( ( c >= '0' && c <= '9' ) || ( c == '.' ) || ( c == ',' ) || ( c == '-' )
78 || ( c == '+' ) ) )
79 break;
80
81 ++brk_point;
82 }
83
84 // Check the unit designator (2 ch significant)
85 wxString unit( buf.Mid( brk_point ).Strip( wxString::leading ).Left( 2 ).Lower() );
86
87 if( unit == wxT( "mm" ) )
89 else if( unit == wxT( "mi" ) || unit == wxT( "th" ) ) // "mils" or "thou"
90 aUnits = EDA_UNITS::MILS;
91 else if( unit == wxT( "in" ) || unit == wxT( "\"" ) )
92 aUnits = EDA_UNITS::INCHES;
93 else if( unit == wxT( "de" ) || unit == wxT( "ra" ) ) // "deg" or "rad"
94 aUnits = EDA_UNITS::DEGREES;
95 else
96 return false;
97 return true;
98}

References DEGREES, INCHES, MILLIMETRES, and MILS.

Referenced by PCB_PARSER::parseDIMENSION().

◆ FormatAngle()

std::string EDA_UNIT_UTILS::FormatAngle ( const EDA_ANGLE aAngle)

Converts aAngle from board units to a string appropriate for writing to file.

This should only be used for writing to files as it ignores locale

Note
Internal angles for board items can be either degrees or tenths of degree on how KiCad is built.
Parameters
aAngleA angle value to convert.
Returns
std::string object containing the converted angle.

Definition at line 134 of file eda_units.cpp.

135{
136 std::string temp = fmt::format( "{:.10g}", aAngle.AsDegrees() );
137
138 return temp;
139}
double AsDegrees() const
Definition: eda_angle.h:149

References EDA_ANGLE::AsDegrees().

Referenced by DIALOG_PAD_PROPERTIES::displayPrimitivesList(), PCB_PLUGIN::format(), PCB_PLUGIN::formatRenderCache(), SCH_SEXPR_PLUGIN::saveField(), SCH_SEXPR_PLUGIN_CACHE::savePin(), SCH_SEXPR_PLUGIN::saveSheet(), SCH_SEXPR_PLUGIN::saveSymbol(), SCH_SEXPR_PLUGIN::saveText(), SCH_SEXPR_PLUGIN_CACHE::saveTextBox(), and SCH_SEXPR_PLUGIN::saveTextBox().

◆ FormatInternalUnits() [1/2]

std::string EDA_UNIT_UTILS::FormatInternalUnits ( const EDA_IU_SCALE aIuScale,
const VECTOR2I aPoint 
)

Definition at line 175 of file eda_units.cpp.

177{
178 return FormatInternalUnits( aIuScale, aPoint.x ) + " "
179 + FormatInternalUnits( aIuScale, aPoint.y );
180}
std::string FormatInternalUnits(const EDA_IU_SCALE &aIuScale, int aValue)
Converts aValue from internal units to a string appropriate for writing to file.
Definition: eda_units.cpp:142

References FormatInternalUnits(), VECTOR2< T >::x, and VECTOR2< T >::y.

◆ FormatInternalUnits() [2/2]

std::string EDA_UNIT_UTILS::FormatInternalUnits ( const EDA_IU_SCALE aIuScale,
int  aValue 
)

Converts aValue from internal units to a string appropriate for writing to file.

This should only be used for writing to files as it ignores locale

Note
Internal units for board items can be either deci-mils or nanometers depending on how KiCad is built.
Parameters
aValueA coordinate value to convert.
Returns
A std::string object containing the converted value.

Definition at line 142 of file eda_units.cpp.

143{
144 std::string buf;
145 double engUnits = aValue;
146
147 engUnits /= aIuScale.IU_PER_MM;
148
149 if( engUnits != 0.0 && fabs( engUnits ) <= 0.0001 )
150 {
151 buf = fmt::format( "{:.10f}", engUnits );
152
153 // remove trailing zeros
154 while( !buf.empty() && buf[buf.size() - 1] == '0' )
155 {
156 buf.pop_back();
157 }
158
159 // if the value was really small
160 // we may have just stripped all the zeros after the decimal
161 if( buf[buf.size() - 1] == '.' )
162 {
163 buf.pop_back();
164 }
165 }
166 else
167 {
168 buf = fmt::format( "{:.10g}", engUnits );
169 }
170
171 return buf;
172}
const double IU_PER_MM
Definition: base_units.h:77

References EDA_IU_SCALE::IU_PER_MM.

Referenced by BOOST_AUTO_TEST_CASE(), PCB_PLUGIN::format(), EDA_TEXT::Format(), STROKE_PARAMS::Format(), formatArc(), formatBezier(), BOARD_STACKUP::FormatBoardStackup(), formatCircle(), PCB_PLUGIN::formatGeneral(), FormatInternalUnits(), formatPoly(), PCB_PLUGIN::formatPolyPts(), formatRect(), PCB_PLUGIN::formatSetup(), SCH_SEXPR_PLUGIN::saveBitmap(), SCH_SEXPR_PLUGIN::saveBusEntry(), SCH_SEXPR_PLUGIN_CACHE::saveField(), SCH_SEXPR_PLUGIN::saveField(), SCH_SEXPR_PLUGIN::saveJunction(), SCH_SEXPR_PLUGIN::saveLine(), SCH_SEXPR_PLUGIN::saveNoConnect(), SCH_SEXPR_PLUGIN_CACHE::savePin(), SCH_SEXPR_PLUGIN::saveSheet(), SCH_SEXPR_PLUGIN_CACHE::SaveSymbol(), SCH_SEXPR_PLUGIN::saveSymbol(), SCH_SEXPR_PLUGIN_CACHE::saveText(), SCH_SEXPR_PLUGIN::saveText(), SCH_SEXPR_PLUGIN_CACHE::saveTextBox(), and SCH_SEXPR_PLUGIN::saveTextBox().

◆ GetLabel()

wxString EDA_UNIT_UTILS::GetLabel ( EDA_UNITS  aUnits,
EDA_DATA_TYPE  aType = EDA_DATA_TYPE::DISTANCE 
)

Get the units string for a given units type.

This version is for setting a wxStaticText label.

Parameters
aUnitsThe units requested.
aTypeDISTANCE, AREA, or VOLUME
Returns
The human readable units string.

Definition at line 128 of file eda_units.cpp.

129{
130 return GetText( aUnits, aType ).Trim( false );
131}
wxString GetText(EDA_UNITS aUnits, EDA_DATA_TYPE aType=EDA_DATA_TYPE::DISTANCE)
Get the units string for a given units type.
Definition: eda_units.cpp:101

References GetText().

Referenced by UNIT_BINDER::ChangeValue(), DIALOG_TRACK_VIA_PROPERTIES::DIALOG_TRACK_VIA_PROPERTIES(), WX_COLLAPSIBLE_PANE_HEADER::DoGetBestClientSize(), PCB_DIMENSION_BASE::GetMsgPanelInfo(), UNIT_BINDER::onKillFocus(), WX_COLLAPSIBLE_PANE_HEADER::onPaint(), DIALOG_TRACK_VIA_PROPERTIES::onUnitsChanged(), TOOLS_HOLDER::PopTool(), UNIT_BINDER::SetDataType(), UNIT_BINDER::SetUnits(), UNIT_BINDER::SetValue(), and UNIT_BINDER::UNIT_BINDER().

◆ GetText()

wxString EDA_UNIT_UTILS::GetText ( EDA_UNITS  aUnits,
EDA_DATA_TYPE  aType = EDA_DATA_TYPE::DISTANCE 
)

Get the units string for a given units type.

This version is for appending to a value string.

Parameters
aUnitsThe units requested.
aTypeDISTANCE, AREA, or VOLUME
Returns
The human readable units string with appropriate separators.

Definition at line 101 of file eda_units.cpp.

102{
103 wxString label;
104
105 switch( aUnits )
106 {
107 case EDA_UNITS::MILLIMETRES: label = wxT( " mm" ); break;
108 case EDA_UNITS::DEGREES: label = wxT( "°" ); break;
109 case EDA_UNITS::MILS: label = wxT( " mils" ); break;
110 case EDA_UNITS::INCHES: label = wxT( " in" ); break;
111 case EDA_UNITS::PERCENT: label = wxT( "%" ); break;
112 case EDA_UNITS::UNSCALED: break;
113 default: UNIMPLEMENTED_FOR( wxS( "Unknown units" ) ); break;
114 }
115
116 switch( aType )
117 {
118 case EDA_DATA_TYPE::VOLUME: label += wxT( "³" ); break;
119 case EDA_DATA_TYPE::AREA: label += wxT( "²" ); break;
120 case EDA_DATA_TYPE::DISTANCE: break;
121 default: UNIMPLEMENTED_FOR( wxS( "Unknown measurement" ) ); break;
122 }
123
124 return label;
125}
#define UNIMPLEMENTED_FOR(type)
Definition: macros.h:120

References AREA, DEGREES, DISTANCE, INCHES, MILLIMETRES, MILS, PERCENT, UNIMPLEMENTED_FOR, UNSCALED, and VOLUME.

Referenced by SCH_SYMBOL::AddSheetPathReferenceEntryIfMissing(), ComboBoxUnits(), KIGFX::PREVIEW::DimensionLabel(), SCH_LINE::FindWireSegmentNetNameRecursive(), PANEL_PREVIEW_3D_MODEL::formatOffsetValue(), PANEL_PREVIEW_3D_MODEL::formatRotationValue(), FP_TEXT_GRID_TABLE::FP_TEXT_GRID_TABLE(), SCH_SHEET::GetItemDescription(), GetLabel(), SCH_SHEET::GetMsgPanelInfo(), SCH_SYMBOL::GetRef(), TEXT_SEARCH_HANDLER::GetResultCell(), DIALOG_MIGRATE_BUSES::loadGraphData(), NETLIST_EXPORTER_XML::makeSymbols(), EDA_UNIT_UTILS::UI::MessageTextFromValue(), PANEL_SETUP_BOARD_STACKUP::onAdjustDielectricThickness(), SCH_SHEET::operator<(), SCH_LEGACY_PLUGIN_CACHE::SaveSymbol(), SCH_SHEET::SetFields(), EDA_UNIT_UTILS::UI::StringFromValue(), DIALOG_SHEET_PIN_PROPERTIES::TransferDataToWindow(), and PCB_DIMENSION_BASE::updateText().

◆ IsImperialUnit()

bool EDA_UNIT_UTILS::IsImperialUnit ( EDA_UNITS  aUnit)

Definition at line 29 of file eda_units.cpp.

30{
31 switch( aUnit )
32 {
34 case EDA_UNITS::MILS:
35 return true;
36
37 default:
38 return false;
39 }
40}

References INCHES, and MILS.

Referenced by getTickFormatForScale(), EDA_DRAW_FRAME::GetUnitPair(), COMMON_TOOLS::SetLastUnits(), COMMON_TOOLS::SwitchUnits(), COMMON_TOOLS::ToggleUnits(), and PCB_EDIT_FRAME::UpdateViaSizeSelectBox().

◆ IsMetricUnit()

bool EDA_UNIT_UTILS::IsMetricUnit ( EDA_UNITS  aUnit)

Definition at line 43 of file eda_units.cpp.

44{
45 switch( aUnit )
46 {
48 return true;
49
50 default:
51 return false;
52 }
53}

References MILLIMETRES.

Referenced by COMMON_TOOLS::SetLastUnits(), and COMMON_TOOLS::SwitchUnits().

◆ Mils2IU()

constexpr int EDA_UNIT_UTILS::Mils2IU ( const EDA_IU_SCALE aIuScale,
int  mils 
)
inlineconstexpr

◆ Mils2mm()

int EDA_UNIT_UTILS::Mils2mm ( double  aVal)

Convert mils to mm.

Definition at line 62 of file eda_units.cpp.

63{
64 return KiROUND( aVal * 25.4 / 1000. );
65}
constexpr ret_type KiROUND(fp_type v)
Round a floating point number to an integer using "round halfway cases away from zero".
Definition: util.h:85

References KiROUND().

Referenced by DIALOG_PRINT_GENERIC::initPrintData(), PCB_PARSER::parsePAGE_INFO(), PL_EDITOR_FRAME::ToPrinter(), and DIALOG_PRINT_USING_PRINTER::TransferDataToWindow().

◆ Mm2mils()

int EDA_UNIT_UTILS::Mm2mils ( double  aVal)

Convert mm to mils.

Definition at line 56 of file eda_units.cpp.

57{
58 return KiROUND( aVal * 1000. / 25.4 );
59}

References KiROUND().

Referenced by PCB_PARSER::parsePAGE_INFO(), and SCH_SEXPR_PARSER::parsePAGE_INFO().