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...
 
void 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 wxPoint &aPoint)
 
std::string FormatInternalUnits (const EDA_IU_SCALE &aIuScale, const wxSize &aSize)
 
std::string FormatInternalUnits (const EDA_IU_SCALE &aIuScale, const VECTOR2I &aPoint)
 
constexpr int Mils2IU (const EDA_IU_SCALE &aIuScale, int mils)
 

Function Documentation

◆ FetchUnitsFromString()

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

Writes any unit info found in the string to aUnits.

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}

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 131 of file eda_units.cpp.

132{
133 std::string temp = fmt::format( "{:.10g}", aAngle.AsDegrees() );
134
135 return temp;
136}
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/4]

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

Definition at line 173 of file eda_units.cpp.

175{
176 return FormatInternalUnits( aIuScale, aPoint.x ) + " "
177 + FormatInternalUnits( aIuScale, aPoint.y );
178}
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:139

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

◆ FormatInternalUnits() [2/4]

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

Definition at line 165 of file eda_units.cpp.

167{
168 return FormatInternalUnits( aIuScale, aPoint.x ) + " "
169 + FormatInternalUnits( aIuScale, aPoint.y );
170}

References FormatInternalUnits().

◆ FormatInternalUnits() [3/4]

std::string EDA_UNIT_UTILS::FormatInternalUnits ( const EDA_IU_SCALE aIuScale,
const wxSize &  aSize 
)

Definition at line 181 of file eda_units.cpp.

182{
183 return FormatInternalUnits( aIuScale, aSize.GetWidth() ) + " "
184 + FormatInternalUnits( aIuScale, aSize.GetHeight() );
185}

References FormatInternalUnits().

◆ FormatInternalUnits() [4/4]

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 139 of file eda_units.cpp.

140{
141 std::string buf;
142 double engUnits = aValue;
143
144 engUnits /= aIuScale.IU_PER_MM;
145
146 if( engUnits != 0.0 && fabs( engUnits ) <= 0.0001 )
147 {
148 buf = fmt::format( "{:.10f}", engUnits );
149
150 // remove trailing zeros
151 while( !buf.empty() && buf[buf.size() - 1] == '0' )
152 {
153 buf.pop_back();
154 }
155 }
156 else
157 {
158 buf = fmt::format( "{:.10g}", engUnits );
159 }
160
161 return buf;
162}
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 125 of file eda_units.cpp.

126{
127 return GetText( aUnits, aType ).Trim( false );
128}
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:98

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 98 of file eda_units.cpp.

99{
100 wxString label;
101
102 switch( aUnits )
103 {
104 case EDA_UNITS::MILLIMETRES: label = wxT( " mm" ); break;
105 case EDA_UNITS::DEGREES: label = wxT( "°" ); break;
106 case EDA_UNITS::MILS: label = wxT( " mils" ); break;
107 case EDA_UNITS::INCHES: label = wxT( " in" ); break;
108 case EDA_UNITS::PERCENT: label = wxT( "%" ); break;
109 case EDA_UNITS::UNSCALED: break;
110 default: UNIMPLEMENTED_FOR( "Unknown units" ); break;
111 }
112
113 switch( aType )
114 {
115 case EDA_DATA_TYPE::VOLUME: label += wxT( "³" ); break;
116 case EDA_DATA_TYPE::AREA: label += wxT( "²" ); break;
117 case EDA_DATA_TYPE::DISTANCE: break;
118 default: UNIMPLEMENTED_FOR( "Unknown measurement" ); break;
119 }
120
121 return label;
122}
#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(), GetLabel(), SCH_SHEET::GetMsgPanelInfo(), SCH_SYMBOL::GetRef(), TEXT_SEARCH_HANDLER::GetResultCell(), SCH_SHEET::GetSelectMenuText(), 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().