KiCad PCB EDA Suite
EDA_UNIT_UTILS::UI Namespace Reference

Functions

double ToUserUnit (const EDA_IU_SCALE &aIuScale, EDA_UNITS aUnit, double aValue)
 Function To_User_Unit convert aValue in internal units to the appropriate user units defined by aUnit. More...
 
wxString StringFromValue (const EDA_IU_SCALE &aIuScale, EDA_UNITS aUnits, double aValue, bool aAddUnitsText=false, EDA_DATA_TYPE aType=EDA_DATA_TYPE::DISTANCE)
 Returns the string from aValue according to aUnits (inch, mm ...) for display. More...
 
wxString MessageTextFromValue (const EDA_IU_SCALE &aIuScale, EDA_UNITS aUnits, double aValue, bool aAddUnitsText=true, EDA_DATA_TYPE aType=EDA_DATA_TYPE::DISTANCE)
 A helper to convert the double length aValue to a string in inches, millimeters, or unscaled units. More...
 
wxString MessageTextFromValue (const EDA_IU_SCALE &aIuScale, EDA_UNITS aUnits, int aValue, bool aAddUnitLabel=true, EDA_DATA_TYPE aType=EDA_DATA_TYPE::DISTANCE)
 Convert a value to a string using double notation. More...
 
wxString MessageTextFromValue (const EDA_IU_SCALE &aIuScale, EDA_UNITS aUnits, long long int aValue, bool aAddUnitLabel=true, EDA_DATA_TYPE aType=EDA_DATA_TYPE::DISTANCE)
 
wxString MessageTextFromValue (EDA_ANGLE aValue, bool aAddUnitLabel=true)
 
double FromUserUnit (const EDA_IU_SCALE &aIuScale, EDA_UNITS aUnit, double aValue)
 Return in internal units the value "val" given in a real unit such as "in", "mm" or "deg". More...
 
double DoubleValueFromString (const EDA_IU_SCALE &aIuScale, EDA_UNITS aUnits, const wxString &aTextValue, EDA_DATA_TYPE aType=EDA_DATA_TYPE::DISTANCE)
 Function DoubleValueFromString converts aTextValue to a double. More...
 
double DoubleValueFromString (const wxString &aTextValue)
 
long long int ValueFromString (const EDA_IU_SCALE &aIuScale, EDA_UNITS aUnits, const wxString &aTextValue, EDA_DATA_TYPE aType=EDA_DATA_TYPE::DISTANCE)
 Function ValueFromString converts aTextValue in aUnits to internal units used by the application. More...
 
long long int ValueFromString (const wxString &aTextValue)
 

Function Documentation

◆ DoubleValueFromString() [1/2]

double EDA_UNIT_UTILS::UI::DoubleValueFromString ( const EDA_IU_SCALE aIuScale,
EDA_UNITS  aUnits,
const wxString &  aTextValue,
EDA_DATA_TYPE  aType = EDA_DATA_TYPE::DISTANCE 
)

Function DoubleValueFromString converts aTextValue to a double.

Warning
This utilizes the current locale and will break if decimal formats differ
Parameters
aIuScaleThe internal units scale for the current frame/app.
aUnitsThe units of aTextValue.
aTextValueA reference to a wxString object containing the string to convert.
Returns
A double representing that value in internal units

Definition at line 449 of file eda_units.cpp.

451{
452 double dtmp = 0;
453
454 // Acquire the 'right' decimal point separator
455 const struct lconv* lc = localeconv();
456
457 wxChar decimal_point = lc->decimal_point[0];
458 wxString buf( aTextValue.Strip( wxString::both ) );
459
460 // Convert any entered decimal point separators to the 'right' one
461 buf.Replace( wxT( "." ), wxString( decimal_point, 1 ) );
462 buf.Replace( wxT( "," ), wxString( decimal_point, 1 ) );
463
464 // Find the end of the numeric part
465 unsigned brk_point = 0;
466
467 while( brk_point < buf.Len() )
468 {
469 wxChar ch = buf[brk_point];
470
471 if( !( (ch >= '0' && ch <= '9') || (ch == decimal_point) || (ch == '-') || (ch == '+') ) )
472 break;
473
474 ++brk_point;
475 }
476
477 // Extract the numeric part
478 buf.Left( brk_point ).ToDouble( &dtmp );
479
480 // Check the optional unit designator (2 ch significant)
481 wxString unit( buf.Mid( brk_point ).Strip( wxString::leading ).Left( 2 ).Lower() );
482
483 if( aUnits == EDA_UNITS::MILLIMETRES
484 || aUnits == EDA_UNITS::MILS
485 || aUnits == EDA_UNITS::INCHES )
486 {
487 if( unit == wxT( "mm" ) )
488 {
489 aUnits = EDA_UNITS::MILLIMETRES;
490 }
491 else if( unit == wxT( "mi" ) || unit == wxT( "th" ) )
492 {
493 aUnits = EDA_UNITS::MILS;
494 }
495 else if( unit == wxT( "in" ) || unit == wxT( "\"" ) )
496 {
497 aUnits = EDA_UNITS::INCHES;
498 }
499 else if( unit == "oz" ) // 1 oz = 1.37 mils
500 {
501 aUnits = EDA_UNITS::MILS;
502 dtmp *= 1.37;
503 }
504 }
505 else if( aUnits == EDA_UNITS::DEGREES )
506 {
507 if( unit == wxT( "ra" ) ) // Radians
508 dtmp *= 180.0f / M_PI;
509 }
510
511 switch( aType )
512 {
514 dtmp = FromUserUnit( aIuScale, aUnits, dtmp );
516
518 dtmp = FromUserUnit( aIuScale, aUnits, dtmp );
520
522 dtmp = FromUserUnit( aIuScale, aUnits, dtmp );
523 }
524
525 return dtmp;
526}
#define KI_FALLTHROUGH
The KI_FALLTHROUGH macro is to be used when switch statement cases should purposely fallthrough from ...
Definition: macros.h:83
double FromUserUnit(const EDA_IU_SCALE &aIuScale, EDA_UNITS aUnit, double aValue)
Return in internal units the value "val" given in a real unit such as "in", "mm" or "deg".
Definition: eda_units.cpp:389

References AREA, DEGREES, DISTANCE, FromUserUnit(), INCHES, KI_FALLTHROUGH, MILLIMETRES, MILS, and VOLUME.

Referenced by UNITS_PROVIDER::AngleValueFromString(), GRID_MENU::BuildChoiceList(), PCB_UNIT_RESOLVER::Convert(), PROPERTIES_FRAME::CopyPrmsFromPanelToItem(), PANEL_PREVIEW_3D_MODEL::doIncrementOffset(), PANEL_PREVIEW_3D_MODEL::doIncrementRotation(), PANEL_PREVIEW_3D_MODEL::doIncrementScale(), LIBEVAL::COMPILER::generateUCode(), UNIT_BINDER::GetDoubleValue(), DIALOG_BOARD_REANNOTATE::GetParameters(), DIALOG_EXPORT_STEP::GetXOrg(), DIALOG_EXPORT_IDF3::GetXRef(), DIALOG_EXPORT_3DFILE::GetXRef(), DIALOG_EXPORT_STEP::GetYOrg(), DIALOG_EXPORT_IDF3::GetYRef(), DIALOG_EXPORT_3DFILE::GetYRef(), FOOTPRINT_PREVIEW_PANEL::New(), PANEL_PREVIEW_3D_MODEL::onMouseWheelOffset(), PANEL_PREVIEW_3D_MODEL::onMouseWheelRot(), PANEL_PREVIEW_3D_MODEL::onMouseWheelScale(), COMMON_TOOLS::Reset(), rotationFromString(), PANEL_3D_RAYTRACING_OPTIONS::TransferDataFromWindow(), DIALOG_CREATE_ARRAY::TransferDataFromWindow(), DIALOG_IMPORT_GFX::TransferDataFromWindow(), DIALOG_PAD_PRIMITIVES_TRANSFORM::Transform(), PANEL_PREVIEW_3D_MODEL::updateOrientation(), ValueFromString(), UNITS_PROVIDER::ValueFromString(), and DIALOG_IMPORT_GFX::~DIALOG_IMPORT_GFX().

◆ DoubleValueFromString() [2/2]

double EDA_UNIT_UTILS::UI::DoubleValueFromString ( const wxString &  aTextValue)

Definition at line 412 of file eda_units.cpp.

413{
414 double dtmp = 0;
415
416 // Acquire the 'right' decimal point separator
417 const struct lconv* lc = localeconv();
418
419 wxChar decimal_point = lc->decimal_point[0];
420 wxString buf( aTextValue.Strip( wxString::both ) );
421
422 // Convert any entered decimal point separators to the 'right' one
423 buf.Replace( wxT( "." ), wxString( decimal_point, 1 ) );
424 buf.Replace( wxT( "," ), wxString( decimal_point, 1 ) );
425
426 // Find the end of the numeric part
427 unsigned brk_point = 0;
428
429 while( brk_point < buf.Len() )
430 {
431 wxChar ch = buf[brk_point];
432
433 if( !( ( ch >= '0' && ch <= '9' ) || ( ch == decimal_point ) || ( ch == '-' )
434 || ( ch == '+' ) ) )
435 {
436 break;
437 }
438
439 ++brk_point;
440 }
441
442 // Extract the numeric part
443 buf.Left( brk_point ).ToDouble( &dtmp );
444
445 return dtmp;
446}

◆ FromUserUnit()

double EDA_UNIT_UTILS::UI::FromUserUnit ( const EDA_IU_SCALE aIuScale,
EDA_UNITS  aUnit,
double  aValue 
)

Return in internal units the value "val" given in a real unit such as "in", "mm" or "deg".

Definition at line 389 of file eda_units.cpp.

391{
392 switch( aUnits )
393 {
395 return MM_TO_IU( aValue, aIuScale );
396
397 case EDA_UNITS::MILS:
398 return MILS_TO_IU( aValue, aIuScale );
399
401 return IN_TO_IU( aValue, aIuScale );
402
403 default:
407 return aValue;
408 }
409}
#define IN_TO_IU(x, scale)
Definition: eda_units.cpp:191
#define MM_TO_IU(x, scale)
Definition: eda_units.cpp:190
#define MILS_TO_IU(x, scale)
Definition: eda_units.cpp:192

References DEGREES, IN_TO_IU, INCHES, MILLIMETRES, MILS, MILS_TO_IU, MM_TO_IU, PERCENT, and UNSCALED.

Referenced by PROPERTIES_FRAME::CopyPrmsFromGeneralToPanel(), PROPERTIES_FRAME::CopyPrmsFromItemToPanel(), DoubleValueFromString(), DRAWING_TOOL::DrawBoardCharacteristics(), FOOTPRINT_EDITOR_SETTINGS::MigrateFromLegacy(), PCBNEW_SETTINGS::MigrateFromLegacy(), UNIT_BINDER::setPrecision(), and UNIT_BINDER::Validate().

◆ MessageTextFromValue() [1/4]

wxString EDA_UNIT_UTILS::UI::MessageTextFromValue ( const EDA_IU_SCALE aIuScale,
EDA_UNITS  aUnits,
double  aValue,
bool  aAddUnitsText = true,
EDA_DATA_TYPE  aType = EDA_DATA_TYPE::DISTANCE 
)

A helper to convert the double length aValue to a string in inches, millimeters, or unscaled units.

Should be used only to display a coordinate in status, but not in dialogs, files, etc., because the mantissa of the number displayed has 4 digits max for readability. The actual internal value could need up to 8 digits to preserve precision.

Parameters
aUnitsUnits (INCHES, MILLIMETRE ..)
aValueThe double value to convert.
aAddUnitsTextIf true, adds the unit label to the end of the string
aTypeDISTANCE, AREA, or VOLUME
Returns
The converted string for display in user interface elements.

Definition at line 319 of file eda_units.cpp.

322{
323 wxString text;
324 const wxChar* format;
325 double value = aValue;
326
327 switch( aType )
328 {
330 value = ToUserUnit( aIuScale, aUnits, value );
331 // Fall through to continue computation
333
335 value = ToUserUnit( aIuScale, aUnits, value );
336 // Fall through to continue computation
338
340 value = ToUserUnit( aIuScale, aUnits, value );
341 }
342
343 switch( aUnits )
344 {
345 default:
347#if defined( EESCHEMA )
348 format = wxT( "%.2f" );
349#else
350 format = wxT( "%.4f" );
351#endif
352 break;
353
354 case EDA_UNITS::MILS:
355#if defined( EESCHEMA )
356 format = wxT( "%.0f" );
357#else
358 format = wxT( "%.2f" );
359#endif
360 break;
361
363#if defined( EESCHEMA )
364 format = wxT( "%.3f" );
365#else
366 format = wxT( "%.4f" );
367#endif
368 break;
369
371 // 3 digits in mantissa should be good for rotation in degree
372 format = wxT( "%.3f" );
373 break;
374
376 format = wxT( "%.0f" );
377 break;
378 }
379
380 text.Printf( format, value );
381
382 if( aAddUnitsText )
383 text += EDA_UNIT_UTILS::GetText( aUnits, aType );
384
385 return text;
386}
double ToUserUnit(const EDA_IU_SCALE &aIuScale, EDA_UNITS aUnit, double aValue)
Function To_User_Unit convert aValue in internal units to the appropriate user units defined by aUnit...
Definition: eda_units.cpp:194
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 AREA, DEGREES, DISTANCE, EDA_UNIT_UTILS::GetText(), INCHES, KI_FALLTHROUGH, MILLIMETRES, MILS, text, ToUserUnit(), UNSCALED, and VOLUME.

Referenced by GRID_MENU::BuildChoiceList(), DIALOG_PAD_PROPERTIES::displayPrimitivesList(), EDIT_TOOL::DragArcTrack(), DRC_ENGINE::EvalRules(), formatCoord(), DS_DRAW_ITEM_BASE::GetMsgPanelInfo(), MessageTextFromValue(), UNITS_PROVIDER::MessageTextFromValue(), EDA_SHAPE::ShapeGetMsgPanelInfo(), PNS::DP_MEANDER_PLACER::TuningInfo(), PNS::MEANDER_PLACER::TuningInfo(), and PNS::MEANDER_SKEW_PLACER::TuningInfo().

◆ MessageTextFromValue() [2/4]

wxString EDA_UNIT_UTILS::UI::MessageTextFromValue ( const EDA_IU_SCALE aIuScale,
EDA_UNITS  aUnits,
int  aValue,
bool  aAddUnitLabel = true,
EDA_DATA_TYPE  aType = EDA_DATA_TYPE::DISTANCE 
)

Convert a value to a string using double notation.

For readability, the mantissa has 0, 1, 3 or 4 digits, depending on units for unit = inch the mantissa has 3 digits (Eeschema) or 4 digits for unit = mil the mantissa has 0 digits (Eeschema) or 1 digits for unit = mm the mantissa has 3 digits (Eeschema) or 4 digits Should be used only to display info in status, but not in dialogs, because 4 digits only could truncate the actual value

Definition at line 290 of file eda_units.cpp.

294{
295 return MessageTextFromValue( aIuScale, aUnits, double( aValue ), aAddUnitLabel, aType );
296}
wxString MessageTextFromValue(const EDA_IU_SCALE &aIuScale, EDA_UNITS aUnits, double aValue, bool aAddUnitsText=true, EDA_DATA_TYPE aType=EDA_DATA_TYPE::DISTANCE)
A helper to convert the double length aValue to a string in inches, millimeters, or unscaled units.
Definition: eda_units.cpp:319

References MessageTextFromValue().

◆ MessageTextFromValue() [3/4]

wxString EDA_UNIT_UTILS::UI::MessageTextFromValue ( const EDA_IU_SCALE aIuScale,
EDA_UNITS  aUnits,
long long int  aValue,
bool  aAddUnitLabel = true,
EDA_DATA_TYPE  aType = EDA_DATA_TYPE::DISTANCE 
)

Definition at line 300 of file eda_units.cpp.

304{
305 return MessageTextFromValue( aIuScale, aUnits, double( aValue ), aAddUnitLabel, aType );
306}

References MessageTextFromValue().

◆ MessageTextFromValue() [4/4]

wxString EDA_UNIT_UTILS::UI::MessageTextFromValue ( EDA_ANGLE  aValue,
bool  aAddUnitLabel = true 
)

Definition at line 309 of file eda_units.cpp.

310{
311 if( aAddUnitLabel )
312 return wxString::Format( wxT( "%.1f°" ), aValue.AsDegrees() );
313 else
314 return wxString::Format( wxT( "%.1f" ), aValue.AsDegrees() );
315}
double AsDegrees() const
Definition: eda_angle.h:149
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, const CPTREE &aTree)
Output a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:200

References EDA_ANGLE::AsDegrees(), and Format().

◆ StringFromValue()

wxString EDA_UNIT_UTILS::UI::StringFromValue ( const EDA_IU_SCALE aIuScale,
EDA_UNITS  aUnits,
double  aValue,
bool  aAddUnitsText = false,
EDA_DATA_TYPE  aType = EDA_DATA_TYPE::DISTANCE 
)

Returns the string from aValue according to aUnits (inch, mm ...) for display.

Convert a value to a string using double notation.

For readability, if the mantissa has 3 or more digits then any trailing 0's are removed. This function should be used to display values in dialogs because a value entered in mm (for instance 2.0 mm) could need up to 8 digits mantissa to preserve precision.

Parameters
aUnitsUnits (INCHES, MILLIMETRE ..)
aValueValue in internal units
aAddUnitsTextAdd units text with appropriate separators
aTypeDISTANCE, AREA, or VOLUME
Returns
A wxString object containing value and optionally the symbol unit (like 2.000 mm)

For readability, the mantissa has 3 or more digits, the trailing 0 are removed if the mantissa has more than 3 digits and some trailing 0 This function should be used to display values in dialogs because a value entered in mm (for instance 2.0 mm) could need up to 8 digits mantissa if displayed in inch to avoid truncation or rounding made just by the printf function. otherwise the actual value is rounded when read from dialog and converted in internal units, and therefore modified.

Definition at line 229 of file eda_units.cpp.

232{
233 double value_to_print = aValue;
234
235 switch( aType )
236 {
238 value_to_print = ToUserUnit( aIuScale, aUnits, value_to_print );
240
242 value_to_print = ToUserUnit( aIuScale, aUnits, value_to_print );
244
246 value_to_print = ToUserUnit( aIuScale, aUnits, value_to_print );
247 }
248
249 char buf[50];
250
251 if( value_to_print != 0.0 && fabs( value_to_print ) <= 0.0001 )
252 {
253 int len = snprintf( buf, sizeof( buf ) - 1, "%.10f", value_to_print );
254
255 while( --len > 0 && buf[len] == '0' )
256 buf[len] = '\0';
257
258 if( len >= 0 && ( buf[len] == '.' || buf[len] == ',' ) )
259 buf[len] = '\0';
260 }
261 else
262 {
263 snprintf( buf, sizeof( buf ) - 1, "%.10g", value_to_print );
264 }
265
266 wxString stringValue( buf, wxConvUTF8 );
267
268 if( aAddUnitsText )
269 stringValue += EDA_UNIT_UTILS::GetText( aUnits, aType );
270
271 return stringValue;
272}

References AREA, DISTANCE, EDA_UNIT_UTILS::GetText(), KI_FALLTHROUGH, ToUserUnit(), and VOLUME.

Referenced by BuildStackupReport(), UNIT_BINDER::ChangeDoubleValue(), UNIT_BINDER::ChangeValue(), DRAWING_TOOL::DrawBoardCharacteristics(), DRAWING_TOOL::DrawSpecificationStackup(), PANEL_SETUP_FORMATTING::ImportSettingsFrom(), DIALOG_PLOT::init_Dialog(), EAGLE_PLUGIN::loadClasses(), PANEL_3D_RAYTRACING_OPTIONS::loadSettings(), FOOTPRINT_EDITOR_SETTINGS::MigrateFromLegacy(), PCBNEW_SETTINGS::MigrateFromLegacy(), UNIT_BINDER::SetDoubleValue(), UNIT_BINDER::SetValue(), UNITS_PROVIDER::StringFromValue(), and UNIT_BINDER::Validate().

◆ ToUserUnit()

double EDA_UNIT_UTILS::UI::ToUserUnit ( const EDA_IU_SCALE aIuScale,
EDA_UNITS  aUnit,
double  aValue 
)

Function To_User_Unit convert aValue in internal units to the appropriate user units defined by aUnit.

Returns
The converted value, in double
Parameters
aUnitThe units to convert aValue to.
aValueThe value in internal units to convert.

Definition at line 194 of file eda_units.cpp.

196{
197 switch( aUnit )
198 {
200 return IU_TO_MM( aValue, aIuScale );
201
202 case EDA_UNITS::MILS:
203 return IU_TO_MILS( aValue, aIuScale );
204
206 return IU_TO_IN( aValue, aIuScale );
207
209 return aValue;
210
211 default:
212 return aValue;
213 }
214}
#define IU_TO_IN(x, scale)
Definition: eda_units.cpp:188
#define IU_TO_MILS(x, scale)
Definition: eda_units.cpp:189
#define IU_TO_MM(x, scale)
Definition: eda_units.cpp:187

References DEGREES, INCHES, IU_TO_IN, IU_TO_MILS, IU_TO_MM, MILLIMETRES, and MILS.

Referenced by ComboBoxUnits(), PROPERTIES_FRAME::CopyPrmsFromPanelToGeneral(), PROPERTIES_FRAME::CopyPrmsFromPanelToItem(), KIGFX::PREVIEW::DimensionLabel(), PL_EDITOR_FRAME::DisplayGridMsg(), GERBER_DRAW_ITEM::GetMsgPanelInfo(), PCB_DIMENSION_BASE::GetValueText(), MessageTextFromValue(), UNIT_BINDER::setPrecision(), StringFromValue(), and PL_EDITOR_FRAME::UpdateStatusBar().

◆ ValueFromString() [1/2]

long long int EDA_UNIT_UTILS::UI::ValueFromString ( const EDA_IU_SCALE aIuScale,
EDA_UNITS  aUnits,
const wxString &  aTextValue,
EDA_DATA_TYPE  aType = EDA_DATA_TYPE::DISTANCE 
)

Function ValueFromString converts aTextValue in aUnits to internal units used by the application.

Warning
This utilizes the current locale and will break if decimal formats differ
Parameters
aIuScaleThe internal units scale for the current frame/app.
aUnitsThe units of aTextValue.
aTextValueA reference to a wxString object containing the string to convert.
Returns
A long long int representing that value in internal units

Definition at line 529 of file eda_units.cpp.

531{
532 double value = DoubleValueFromString( aIuScale, aUnits, aTextValue, aType );
533
534 return KiROUND<double, long long int>( value );
535}
double DoubleValueFromString(const EDA_IU_SCALE &aIuScale, EDA_UNITS aUnits, const wxString &aTextValue, EDA_DATA_TYPE aType=EDA_DATA_TYPE::DISTANCE)
Function DoubleValueFromString converts aTextValue to a double.
Definition: eda_units.cpp:449

References DoubleValueFromString().

Referenced by DIALOG_ANNOTATE::GetStartNumber(), UNIT_BINDER::GetValue(), and PANEL_EESCHEMA_ANNOTATION_OPTIONS::TransferDataFromWindow().

◆ ValueFromString() [2/2]

long long int EDA_UNIT_UTILS::UI::ValueFromString ( const wxString &  aTextValue)

Definition at line 538 of file eda_units.cpp.

539{
540 double value = DoubleValueFromString( aTextValue );
541
542 return KiROUND<double, long long int>( value );
543}

References DoubleValueFromString().