KiCad PCB EDA Suite
base_units.h File Reference

Implementation of conversion functions that require both schematic and board internal units. More...

#include <string>
#include <common.h>
#include <convert_to_biu.h>
#include <math/util.h>
#include <math/vector2d.h>

Go to the source code of this file.

Macros

#define INDETERMINATE_STATE   _( "-- mixed values --" )
 Used for holding indeterminate values, such as with multiple selections holding different values or controls which do not wish to set a value. More...
 
#define INDETERMINATE_ACTION   _( "-- leave unchanged --" )
 
#define ALLOW_BOLD_THICKNESS   IsBold()
 

Functions

int Mm2mils (double x)
 Convert mm to mils. More...
 
int Mils2mm (double x)
 Convert mils to mm. More...
 
std::string Double2Str (double aValue)
 Helper function Double2Str to print a float number without using scientific notation and no trailing 0 We want to avoid scientific notation in S-expr files (not easy to read) for floating numbers. More...
 
void StripTrailingZeros (wxString &aStringValue, unsigned aTrailingZeroAllowed=1)
 Function StripTrailingZeros Remove trailing 0 from a string containing a converted float number. More...
 
double To_User_Unit (EDA_UNITS aUnit, double aValue, bool aUseMils=false)
 Function To_User_Unit convert aValue in internal units to the appropriate user units defined by aUnit. More...
 
wxString AngleToStringDegrees (double aAngle)
 Function AngleToStringDegrees is a helper to convert the double aAngle (in internal unit) to a string in degrees. More...
 
wxString MessageTextFromValue (EDA_UNITS aUnits, double aValue, bool aUseMils=false, EDA_DATA_TYPE aType=EDA_DATA_TYPE::DISTANCE)
 Function MessageTextFromValue is a helper to convert the double length aValue to a string in inches, millimeters, or unscaled units. More...
 
wxString MessageTextFromValue (EDA_UNITS aUnits, int aValue, bool aUseMils=false, EDA_DATA_TYPE aType=EDA_DATA_TYPE::DISTANCE)
 
wxString MessageTextFromValue (EDA_UNITS aUnits, long long int aValue, bool aUseMils=false, EDA_DATA_TYPE aType=EDA_DATA_TYPE::DISTANCE)
 
wxString StringFromValue (EDA_UNITS aUnit, double aValue, bool aAddUnitSymbol=false, bool aUseMils=false, EDA_DATA_TYPE aType=EDA_DATA_TYPE::DISTANCE)
 Function StringFromValue returns the string from aValue according to units (inch, mm ...) for display, and the initial unit for value. More...
 
double From_User_Unit (EDA_UNITS aUnit, double aValue, bool aUseMils=false)
 Return in internal units the value "val" given in a real unit such as "in", "mm" or "deg". More...
 
double DoubleValueFromString (EDA_UNITS aUnits, const wxString &aTextValue, bool aUseMils=false, EDA_DATA_TYPE aType=EDA_DATA_TYPE::DISTANCE)
 Function DoubleValueFromString converts aTextValue to a double. More...
 
long long int ValueFromString (EDA_UNITS aUnits, const wxString &aTextValue, bool aUseMils=false, EDA_DATA_TYPE aType=EDA_DATA_TYPE::DISTANCE)
 Function ValueFromString converts aTextValue in aUnits to internal units used by the application. More...
 
void FetchUnitsFromString (const wxString &aTextValue, EDA_UNITS &aUnits, bool &aUseMils)
 Function FetchUnitsFromString writes any unit info found in the string to aUnits and aUseMils. More...
 
wxString GetAbbreviatedUnitsLabel (EDA_UNITS aUnit, bool aUseMils=false, EDA_DATA_TYPE aType=EDA_DATA_TYPE::DISTANCE)
 Get the units string for a given units type. More...
 
std::string FormatInternalUnits (int aValue)
 Function FormatInternalUnits converts aValue from internal units to a string appropriate for writing to file. More...
 
std::string FormatAngle (double aAngle)
 Function FormatAngle converts aAngle from board units to a string appropriate for writing to file. More...
 
std::string FormatInternalUnits (const wxPoint &aPoint)
 
std::string FormatInternalUnits (const wxSize &aSize)
 
std::string FormatInternalUnits (const VECTOR2I &aPoint)
 

Detailed Description

Implementation of conversion functions that require both schematic and board internal units.

Author
Wayne Stambaugh stamb.nosp@m.augh.nosp@m.w@ver.nosp@m.izon.nosp@m..net

Definition in file base_units.h.

Macro Definition Documentation

◆ ALLOW_BOLD_THICKNESS

#define ALLOW_BOLD_THICKNESS   IsBold()

Definition at line 57 of file base_units.h.

◆ INDETERMINATE_ACTION

#define INDETERMINATE_ACTION   _( "-- leave unchanged --" )

Definition at line 49 of file base_units.h.

◆ INDETERMINATE_STATE

#define INDETERMINATE_STATE   _( "-- mixed values --" )

Used for holding indeterminate values, such as with multiple selections holding different values or controls which do not wish to set a value.

Definition at line 48 of file base_units.h.

Function Documentation

◆ AngleToStringDegrees()

wxString AngleToStringDegrees ( double  aAngle)

Function AngleToStringDegrees is a helper to convert the double aAngle (in internal unit) to a string in degrees.

Definition at line 484 of file base_units.cpp.

485 {
486  wxString text;
487 
488  text.Printf( wxT( "%.3f" ), aAngle/10.0 );
489  StripTrailingZeros( text, 1 );
490 
491  return text;
492 }
void StripTrailingZeros(wxString &aStringValue, unsigned aTrailingZeroAllowed)
Function StripTrailingZeros Remove trailing 0 from a string containing a converted float number.
Definition: base_units.cpp:201

References StripTrailingZeros().

Referenced by PANEL_EDIT_OPTIONS::TransferDataToWindow().

◆ Double2Str()

std::string Double2Str ( double  aValue)

Helper function Double2Str to print a float number without using scientific notation and no trailing 0 We want to avoid scientific notation in S-expr files (not easy to read) for floating numbers.

So we cannot always just use the g or the f format to print a fp number this helper function uses the f format when needed, or g when f is not well working and then removes trailing 0

Definition at line 62 of file base_units.cpp.

63 {
64  char buf[50];
65  int len;
66 
67  if( aValue != 0.0 && fabs( aValue ) <= 0.0001 )
68  {
69  // For these small values, %f works fine,
70  // and %g gives an exponent
71  len = sprintf( buf, "%.16f", aValue );
72 
73  while( --len > 0 && buf[len] == '0' )
74  buf[len] = '\0';
75 
76  if( buf[len] == '.' )
77  buf[len] = '\0';
78  else
79  ++len;
80  }
81  else
82  {
83  // For these values, %g works fine, and sometimes %f
84  // gives a bad value (try aValue = 1.222222222222, with %.16f format!)
85  len = sprintf( buf, "%.16g", aValue );
86  }
87 
88  return std::string( buf, len );
89 }

Referenced by GBR_TO_PCB_EXPORTER::export_non_copper_item(), GBR_TO_PCB_EXPORTER::export_via(), PCB_IO::format(), BOARD_STACKUP::FormatBoardStackup(), formatStroke(), SCH_SEXPR_PLUGIN::saveJunction(), GBR_TO_PCB_EXPORTER::writeCopperLineItem(), GBR_TO_PCB_EXPORTER::writePcbPolygon(), and GBR_TO_PCB_EXPORTER::writePcbZoneItem().

◆ DoubleValueFromString()

double DoubleValueFromString ( EDA_UNITS  aUnits,
const wxString &  aTextValue,
bool  aUseMils = false,
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
aUnitsThe units of aTextValue.
aTextValueA reference to a wxString object containing the string to convert.
aUseMilsIndicates mils should be used for imperial units (inches).
Returns
A double representing that value in internal units

Definition at line 346 of file base_units.cpp.

348 {
349  double dtmp = 0;
350 
351  // Acquire the 'right' decimal point separator
352  const struct lconv* lc = localeconv();
353 
354  wxChar decimal_point = lc->decimal_point[0];
355  wxString buf( aTextValue.Strip( wxString::both ) );
356 
357  // Convert the period in decimal point
358  buf.Replace( wxT( "." ), wxString( decimal_point, 1 ) );
359 
360  // Find the end of the numeric part
361  unsigned brk_point = 0;
362 
363  while( brk_point < buf.Len() )
364  {
365  wxChar ch = buf[brk_point];
366 
367  if( !( (ch >= '0' && ch <='9') || (ch == decimal_point) || (ch == '-') || (ch == '+') ) )
368  {
369  break;
370  }
371 
372  ++brk_point;
373  }
374 
375  // Extract the numeric part
376  buf.Left( brk_point );
377 
378  buf.ToDouble( &dtmp );
379 
380  // Check the optional unit designator (2 ch significant)
381  wxString unit( buf.Mid( brk_point ).Strip( wxString::leading ).Left( 2 ).Lower() );
382 
383  if( aUnits == EDA_UNITS::INCHES || aUnits == EDA_UNITS::MILLIMETRES )
384  {
385  if( unit == wxT( "in" ) || unit == wxT( "\"" ) )
386  {
387  aUnits = EDA_UNITS::INCHES;
388  aUseMils = false;
389  }
390  else if( unit == wxT( "mm" ) )
391  {
392  aUnits = EDA_UNITS::MILLIMETRES;
393  }
394  else if( unit == wxT( "mi" ) || unit == wxT( "th" ) ) // "mils" or "thou"
395  {
396  aUnits = EDA_UNITS::INCHES;
397  aUseMils = true;
398  }
399  else if( unit == "oz" ) // 1 oz = 1.37 mils
400  {
401  aUnits = EDA_UNITS::INCHES;
402  aUseMils = true;
403  dtmp *= 1.37;
404  }
405  }
406  else if( aUnits == EDA_UNITS::DEGREES )
407  {
408  if( unit == wxT( "ra" ) ) // Radians
409  {
410  dtmp *= 180.0f / M_PI;
411  }
412  }
413 
414  switch( aType )
415  {
417  dtmp = From_User_Unit( aUnits, dtmp, aUseMils );
419 
420  case EDA_DATA_TYPE::AREA:
421  dtmp = From_User_Unit( aUnits, dtmp, aUseMils );
423 
425  dtmp = From_User_Unit( aUnits, dtmp, aUseMils );
426  }
427 
428  return dtmp;
429 }
double From_User_Unit(EDA_UNITS aUnits, double aValue, bool aUseMils)
Return in internal units the value "val" given in a real unit such as "in", "mm" or "deg".
Definition: base_units.cpp:321
#define KI_FALLTHROUGH
The KI_FALLTHROUGH macro is to be used when switch statement cases should purposely fallthrough from ...
Definition: macros.h:88

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

Referenced by PCB_UNIT_RESOLVER::Convert(), PROPERTIES_FRAME::CopyPrmsFromPanelToGeneral(), PROPERTIES_FRAME::CopyPrmsFromPanelToItem(), PANEL_PREV_3D::doIncrementOffset(), PANEL_PREV_3D::doIncrementRotation(), PANEL_PREV_3D::doIncrementScale(), LIBEVAL::COMPILER::generateUCode(), UNIT_BINDER::GetDoubleValue(), DIALOG_BOARD_REANNOTATE::GetParameters(), DIALOG_IMPORT_GFX::getPCBdefaultLineWidthMM(), DIALOG_EXPORT_STEP::GetXOrg(), DIALOG_EXPORT_IDF3::GetXRef(), DIALOG_EXPORT_3DFILE::GetXRef(), DIALOG_EXPORT_STEP::GetYOrg(), DIALOG_EXPORT_IDF3::GetYRef(), DIALOG_EXPORT_3DFILE::GetYRef(), PANEL_PREV_3D::onMouseWheelOffset(), PANEL_PREV_3D::onMouseWheelRot(), PANEL_PREV_3D::onMouseWheelScale(), rotationFromString(), TEXT_MOD_GRID_TABLE::SetValue(), DIALOG_CREATE_ARRAY::TransferDataFromWindow(), DIALOG_IMPORT_GFX::TransferDataFromWindow(), DIALOG_PAD_PRIMITIVES_TRANSFORM::Transform(), PANEL_PREV_3D::updateOrientation(), DIALOG_IMPORT_GFX::updatePcbImportOffsets_mm(), and ValueFromString().

◆ FetchUnitsFromString()

void FetchUnitsFromString ( const wxString &  aTextValue,
EDA_UNITS aUnits,
bool &  aUseMils 
)

Function FetchUnitsFromString writes any unit info found in the string to aUnits and aUseMils.

Definition at line 432 of file base_units.cpp.

433 {
434  wxString buf( aTextValue.Strip( wxString::both ) );
435  unsigned brk_point = 0;
436 
437  while( brk_point < buf.Len() )
438  {
439  wxChar c = buf[brk_point];
440 
441  if( !( (c >= '0' && c <='9') || (c == '.') || (c == ',') || (c == '-') || (c == '+') ) )
442  break;
443 
444  ++brk_point;
445  }
446 
447  // Check the unit designator (2 ch significant)
448  wxString unit( buf.Mid( brk_point ).Strip( wxString::leading ).Left( 2 ).Lower() );
449 
450  if( unit == wxT( "in" ) || unit == wxT( "\"" ) )
451  {
452  aUnits = EDA_UNITS::INCHES;
453  aUseMils = false;
454  }
455  else if( unit == wxT( "mm" ) )
456  {
457  aUnits = EDA_UNITS::MILLIMETRES;
458  }
459  else if( unit == wxT( "mi" ) || unit == wxT( "th" ) ) // "mils" or "thou"
460  {
461  aUnits = EDA_UNITS::INCHES;
462  aUseMils = true;
463  }
464  else if( unit == wxT( "de" ) || unit == wxT( "ra" ) ) // "deg" or "rad"
465  {
466  aUnits = EDA_UNITS::DEGREES;
467  }
468 }

References DEGREES, INCHES, and MILLIMETRES.

Referenced by PCB_PARSER::parseDIMENSION().

◆ FormatAngle()

std::string FormatAngle ( double  aAngle)

Function FormatAngle converts aAngle from board units to a string appropriate for writing to file.

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
A std::string object containing the converted angle.

Definition at line 589 of file base_units.cpp.

590 {
591  char temp[50];
592  int len;
593 
594  len = snprintf( temp, sizeof(temp), "%.10g", aAngle / 10.0 );
595 
596  return std::string( temp, len );
597 }

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

◆ FormatInternalUnits() [1/4]

std::string FormatInternalUnits ( int  aValue)

Function FormatInternalUnits converts aValue from internal units to a string appropriate for writing to file.

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 560 of file base_units.cpp.

561 {
562  char buf[50];
563  double engUnits = aValue;
564  int len;
565 
566  engUnits /= IU_PER_MM;
567 
568  if( engUnits != 0.0 && fabs( engUnits ) <= 0.0001 )
569  {
570  len = snprintf( buf, sizeof(buf), "%.10f", engUnits );
571 
572  while( --len > 0 && buf[len] == '0' )
573  buf[len] = '\0';
574 
575  if( buf[len] == '.' )
576  buf[len] = '\0';
577  else
578  ++len;
579  }
580  else
581  {
582  len = snprintf( buf, sizeof(buf), "%.10g", engUnits );
583  }
584 
585  return std::string( buf, len );
586 }
static constexpr double IU_PER_MM
Mock up a conversion function.

References IU_PER_MM.

Referenced by BOOST_AUTO_TEST_CASE(), PCB_IO::format(), EDA_TEXT::Format(), BOARD_STACKUP::FormatBoardStackup(), PCB_IO::formatGeneral(), FormatInternalUnits(), PCB_IO::formatSetup(), formatStroke(), SCH_SEXPR_PLUGIN_CACHE::saveArc(), SCH_SEXPR_PLUGIN_CACHE::saveBezier(), SCH_SEXPR_PLUGIN::saveBitmap(), SCH_SEXPR_PLUGIN::saveBusEntry(), SCH_SEXPR_PLUGIN_CACHE::saveCircle(), SCH_SEXPR_PLUGIN::saveField(), SCH_SEXPR_PLUGIN_CACHE::saveField(), SCH_SEXPR_PLUGIN::saveJunction(), SCH_SEXPR_PLUGIN::saveLine(), SCH_SEXPR_PLUGIN::saveNoConnect(), SCH_SEXPR_PLUGIN_CACHE::savePin(), SCH_SEXPR_PLUGIN_CACHE::savePolyLine(), SCH_SEXPR_PLUGIN_CACHE::saveRectangle(), SCH_SEXPR_PLUGIN::saveSheet(), SCH_SEXPR_PLUGIN::saveSymbol(), SCH_SEXPR_PLUGIN_CACHE::SaveSymbol(), SCH_SEXPR_PLUGIN::saveText(), and SCH_SEXPR_PLUGIN_CACHE::saveText().

◆ FormatInternalUnits() [2/4]

std::string FormatInternalUnits ( const wxPoint aPoint)

Definition at line 600 of file base_units.cpp.

601 {
602  return FormatInternalUnits( aPoint.x ) + " " + FormatInternalUnits( aPoint.y );
603 }
std::string FormatInternalUnits(int aValue)
Function FormatInternalUnits converts aValue from internal units to a string appropriate for writing ...
Definition: base_units.cpp:560

References FormatInternalUnits(), wxPoint::x, and wxPoint::y.

◆ FormatInternalUnits() [3/4]

std::string FormatInternalUnits ( const wxSize &  aSize)

Definition at line 612 of file base_units.cpp.

613 {
614  return FormatInternalUnits( aSize.GetWidth() ) + " " + FormatInternalUnits( aSize.GetHeight() );
615 }
std::string FormatInternalUnits(int aValue)
Function FormatInternalUnits converts aValue from internal units to a string appropriate for writing ...
Definition: base_units.cpp:560

References FormatInternalUnits().

◆ FormatInternalUnits() [4/4]

std::string FormatInternalUnits ( const VECTOR2I aPoint)

Definition at line 606 of file base_units.cpp.

607 {
608  return FormatInternalUnits( aPoint.x ) + " " + FormatInternalUnits( aPoint.y );
609 }
std::string FormatInternalUnits(int aValue)
Function FormatInternalUnits converts aValue from internal units to a string appropriate for writing ...
Definition: base_units.cpp:560

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

◆ From_User_Unit()

double From_User_Unit ( EDA_UNITS  aUnit,
double  aValue,
bool  aUseMils = false 
)

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

Definition at line 321 of file base_units.cpp.

322 {
323  switch( aUnits )
324  {
326  return MM_TO_IU( aValue );
327 
328  case EDA_UNITS::INCHES:
329  if( aUseMils )
330  return MILS_TO_IU( aValue );
331  else
332  return IN_TO_IU( aValue );
333 
334  case EDA_UNITS::DEGREES:
335  // Convert to "decidegrees"
336  return aValue * 10;
337 
338  default:
339  case EDA_UNITS::UNSCALED:
340  case EDA_UNITS::PERCENT:
341  return aValue;
342  }
343 }

References DEGREES, INCHES, MILLIMETRES, PERCENT, and UNSCALED.

Referenced by DoubleValueFromString(), FOOTPRINT_EDITOR_SETTINGS::MigrateFromLegacy(), PCBNEW_SETTINGS::MigrateFromLegacy(), and UNIT_BINDER::Validate().

◆ GetAbbreviatedUnitsLabel()

wxString GetAbbreviatedUnitsLabel ( EDA_UNITS  aUnit,
bool  aUseMils = false,
EDA_DATA_TYPE  aType = EDA_DATA_TYPE::DISTANCE 
)

Get the units string for a given units type.

Parameters
aUnits- The units requested.
aUseMils- Use mils for the unit
aType- The data type of the unit (e.g. distance, area, etc.)
Returns
The human readable units string.

Definition at line 495 of file base_units.cpp.

496 {
497  switch( aUnit )
498  {
499  case EDA_UNITS::INCHES:
500  if( aUseMils )
501  {
502  switch( aType )
503  {
504  default:
505  wxASSERT( 0 );
508  return _( "mils" );
509  case EDA_DATA_TYPE::AREA:
510  return _( "sq. mils" );
512  return _( "cu. mils" );
513  }
514  }
515  else
516  {
517  switch( aType )
518  {
519  default:
520  wxASSERT( 0 );
523  return _( "in" );
524  case EDA_DATA_TYPE::AREA:
525  return _( "sq. in" );
527  return _( "cu. in" );
528  }
529  }
530 
532  switch( aType )
533  {
534  default:
535  wxASSERT( 0 );
538  return _( "mm" );
539  case EDA_DATA_TYPE::AREA:
540  return _( "sq. mm" );
542  return _( "cu. mm" );
543  }
544 
545  case EDA_UNITS::PERCENT:
546  return _( "%" );
547 
548  case EDA_UNITS::UNSCALED:
549  return wxEmptyString;
550 
551  case EDA_UNITS::DEGREES:
552  return _( "deg" );
553 
554  default:
555  return wxT( "??" );
556  }
557 }
#define KI_FALLTHROUGH
The KI_FALLTHROUGH macro is to be used when switch statement cases should purposely fallthrough from ...
Definition: macros.h:88
#define _(s)
Definition: 3d_actions.cpp:33

References _, AREA, DEGREES, DISTANCE, INCHES, KI_FALLTHROUGH, MILLIMETRES, PERCENT, UNSCALED, and VOLUME.

Referenced by DIALOG_PLOT::applyPlotSettings(), UNIT_BINDER::ChangeValue(), DIALOG_TRACK_VIA_PROPERTIES::DIALOG_TRACK_VIA_PROPERTIES(), PANEL_PREV_3D::formatOffsetValue(), formatPreviewDimension(), PANEL_PREV_3D::formatRotationValue(), MessageTextFromValue(), DIALOG_BOARD_STATISTICS::saveReportClicked(), UNIT_BINDER::SetDataType(), UNIT_BINDER::SetUnits(), UNIT_BINDER::SetValue(), TEXT_MOD_GRID_TABLE::TEXT_MOD_GRID_TABLE(), UNIT_BINDER::UNIT_BINDER(), DIMENSION::updateText(), and DIALOG_BOARD_STATISTICS::updateWidets().

◆ MessageTextFromValue() [1/3]

wxString MessageTextFromValue ( EDA_UNITS  aUnits,
double  aValue,
bool  aUseMils = false,
EDA_DATA_TYPE  aType = EDA_DATA_TYPE::DISTANCE 
)

Function MessageTextFromValue is 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 be printed.

Use StringFromValue() instead where precision matters.

Parameters
aUnitsThe units to show the value in. The unit string is added to the message text.
aValueThe double value to convert.
aUseMilsConvert inch values to mils if true.
aTypeType of the unit being used (e.g. distance, area, etc.)
Returns
The converted string for display in user interface elements.

Definition at line 139 of file base_units.cpp.

140 {
141  wxString text;
142  const wxChar* format;
143  double value = aValue;
144 
145  switch( aType )
146  {
148  value = To_User_Unit( aUnits, value, aUseMils );
149  // Fall through to continue computation
151 
152  case EDA_DATA_TYPE::AREA:
153  value = To_User_Unit( aUnits, value, aUseMils );
154  // Fall through to continue computation
156 
158  value = To_User_Unit( aUnits, value, aUseMils );
159  }
160 
161  if( aUnits == EDA_UNITS::INCHES )
162  {
163  if( aUseMils )
164  {
165 #if defined( EESCHEMA )
166  format = wxT( "%.0f" );
167 #else
168  format = wxT( "%.1f" );
169 #endif
170  }
171  else
172  {
173 #if defined( EESCHEMA )
174  format = wxT( "%.3f" );
175 #else
176  format = wxT( "%.4f" );
177 #endif
178  }
179  }
180  else
181  {
182 #if defined( EESCHEMA )
183  format = wxT( "%.4f" );
184 #else
185  format = wxT( "%.3f" );
186 #endif
187  }
188 
189  text.Printf( format, value );
190  text += " ";
191 
192  text += GetAbbreviatedUnitsLabel( aUnits, aUseMils, aType );
193 
194  return text;
195 }
double To_User_Unit(EDA_UNITS aUnit, double aValue, bool aUseMils)
Function To_User_Unit convert aValue in internal units to the appropriate user units defined by aUnit...
Definition: base_units.cpp:92
#define KI_FALLTHROUGH
The KI_FALLTHROUGH macro is to be used when switch statement cases should purposely fallthrough from ...
Definition: macros.h:88
wxString GetAbbreviatedUnitsLabel(EDA_UNITS aUnit, bool aUseMils, EDA_DATA_TYPE aType)
Get the units string for a given units type.
Definition: base_units.cpp:495

References AREA, DISTANCE, GetAbbreviatedUnitsLabel(), INCHES, KI_FALLTHROUGH, To_User_Unit(), and VOLUME.

◆ MessageTextFromValue() [2/3]

wxString MessageTextFromValue ( EDA_UNITS  aUnits,
int  aValue,
bool  aUseMils = false,
EDA_DATA_TYPE  aType = EDA_DATA_TYPE::DISTANCE 
)

Definition at line 124 of file base_units.cpp.

125 {
126  return MessageTextFromValue( aUnits, double( aValue ), aUseMils );
127 }
wxString MessageTextFromValue(EDA_UNITS aUnits, int aValue, bool aUseMils, EDA_DATA_TYPE aType)
Definition: base_units.cpp:124

References MessageTextFromValue().

Referenced by DRC_TEST_PROVIDER_HOLE_SIZE::checkPad(), DRC_TEST_PROVIDER_HOLE_SIZE::checkVia(), DIALOG_BOARD_REANNOTATE::CoordTowxString(), DIALOG_PAD_PROPERTIES::displayPrimitivesList(), DRC_TEST_PROVIDER_HOLE_CLEARANCE::doPadToPadHoleDrc(), DRC_TEST_PROVIDER_COPPER_CLEARANCE::doPadToPadsDrc(), DRC_TEST_PROVIDER_COPPER_CLEARANCE::doTrackDrc(), DRC_ENGINE::EvalRulesForItems(), formatCoord(), DIALOG_SELECT_NET_FROM_LIST::formatLength(), LIB_CIRCLE::GetMsgPanelInfo(), LIB_RECTANGLE::GetMsgPanelInfo(), LIB_TEXT::GetMsgPanelInfo(), SCH_PIN::GetMsgPanelInfo(), LIB_ARC::GetMsgPanelInfo(), LIB_POLYLINE::GetMsgPanelInfo(), LIB_BEZIER::GetMsgPanelInfo(), TEXTE_PCB::GetMsgPanelInfo(), WS_DRAW_ITEM_BASE::GetMsgPanelInfo(), ZONE_CONTAINER::GetMsgPanelInfo(), SCH_BITMAP::GetMsgPanelInfo(), LIB_FIELD::GetMsgPanelInfo(), TRACK::GetMsgPanelInfo(), TEXTE_MODULE::GetMsgPanelInfo(), LIB_PIN::GetMsgPanelInfo(), NETINFO_ITEM::GetMsgPanelInfo(), DRAWSEGMENT::GetMsgPanelInfo(), SCH_TEXT::GetMsgPanelInfo(), VIA::GetMsgPanelInfo(), D_PAD::GetMsgPanelInfo(), LIB_RECTANGLE::GetSelectMenuText(), LIB_CIRCLE::GetSelectMenuText(), PCB_TARGET::GetSelectMenuText(), LIB_POLYLINE::GetSelectMenuText(), LIB_ARC::GetSelectMenuText(), WS_DRAW_ITEM_LINE::GetSelectMenuText(), WS_DRAW_ITEM_POLYPOLYGONS::GetSelectMenuText(), SCH_LINE::GetSelectMenuText(), TRACK::GetSelectMenuText(), WS_DRAW_ITEM_RECT::GetSelectMenuText(), WS_DRAW_ITEM_TEXT::GetSelectMenuText(), WS_DRAW_ITEM_BITMAP::GetSelectMenuText(), VIA::GetSelectMenuText(), DIALOG_BOARD_REANNOTATE::MakeSampleText(), MessageTextFromValue(), DRC_TEST_PROVIDER_VIA_DIAMETER::Run(), DRC_TEST_PROVIDER_TRACK_WIDTH::Run(), DRC_TEST_PROVIDER_ANNULUS::Run(), DRC_TEST_PROVIDER_EDGE_CLEARANCE::Run(), test::DRC_TEST_PROVIDER_SILK_TO_PAD::Run(), DIALOG_BOARD_STATISTICS::saveReportClicked(), RC_ITEM::ShowCoord(), DRC_TEST_PROVIDER_COPPER_CLEARANCE::testCopperDrawItem(), DRC_TEST_PROVIDER_HOLE_CLEARANCE::testHoles2Holes(), DRC_TEST_PROVIDER_COPPER_CLEARANCE::testZones(), PNS::MEANDER_SKEW_PLACER::TuningInfo(), PNS::MEANDER_PLACER::TuningInfo(), PNS::DP_MEANDER_PLACER::TuningInfo(), VIA_SIZE_MENU::update(), TRACK_WIDTH_MENU::update(), DIFF_PAIR_MENU::update(), DIALOG_BOARD_STATISTICS::updateDrillGrid(), and DIALOG_BOARD_STATISTICS::updateWidets().

◆ MessageTextFromValue() [3/3]

wxString MessageTextFromValue ( EDA_UNITS  aUnits,
long long int  aValue,
bool  aUseMils = false,
EDA_DATA_TYPE  aType = EDA_DATA_TYPE::DISTANCE 
)

Definition at line 131 of file base_units.cpp.

133 {
134  return MessageTextFromValue( aUnits, double( aValue ), aUseMils );
135 }
wxString MessageTextFromValue(EDA_UNITS aUnits, int aValue, bool aUseMils, EDA_DATA_TYPE aType)
Definition: base_units.cpp:124

References MessageTextFromValue().

◆ Mils2mm()

int Mils2mm ( double  x)
inline

Convert mils to mm.

Definition at line 65 of file base_units.h.

65 { return KiROUND( x * 25.4 / 1000. ); }
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:68

References KiROUND().

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

◆ Mm2mils()

int Mm2mils ( double  x)
inline

Convert mm to mils.

Definition at line 62 of file base_units.h.

62 { return KiROUND( x * 1000./25.4 ); }
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:68

References KiROUND().

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

◆ StringFromValue()

wxString StringFromValue ( EDA_UNITS  aUnit,
double  aValue,
bool  aAddUnitSymbol = false,
bool  aUseMils = false,
EDA_DATA_TYPE  aType = EDA_DATA_TYPE::DISTANCE 
)

Function StringFromValue returns the string from aValue according to units (inch, mm ...) for display, and the initial unit for value.

For readability, the mantissa has 3 or more digits (max 8 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.

Parameters
aUnit= display units (INCHES, MILLIMETRE ..)
aValue= value in Internal_Unit
aAddUnitSymbol= true to add symbol unit to the string value
aUseMilsIndicates mils should be used for imperial units (inches).
Returns
A wxString object containing value and optionally the symbol unit (like 2.000 mm)

Definition at line 233 of file base_units.cpp.

235 {
236  double value_to_print = aValue;
237 
238  switch( aType )
239  {
241  value_to_print = To_User_Unit( aUnits, value_to_print, aUseMils );
243 
244  case EDA_DATA_TYPE::AREA:
245  value_to_print = To_User_Unit( aUnits, value_to_print, aUseMils );
247 
249  value_to_print = To_User_Unit( aUnits, value_to_print, aUseMils );
250  }
251 
252 
253 #if defined( EESCHEMA )
254  wxString stringValue = wxString::Format( wxT( "%.3f" ), value_to_print );
255 
256  // Strip trailing zeros. However, keep at least 3 digits in mantissa
257  // For readability
258  StripTrailingZeros( stringValue, 3 );
259 
260 #else
261 
262  char buf[50];
263  int len;
264 
265  if( value_to_print != 0.0 && fabs( value_to_print ) <= 0.0001 )
266  {
267  len = sprintf( buf, "%.10f", value_to_print );
268 
269  while( --len > 0 && buf[len] == '0' )
270  buf[len] = '\0';
271 
272  if( buf[len]=='.' || buf[len]==',' )
273  buf[len] = '\0';
274  else
275  ++len;
276  }
277  else
278  {
279  if( aUnits == EDA_UNITS::INCHES && aUseMils )
280  len = sprintf( buf, "%.7g", value_to_print );
281  else
282  len = sprintf( buf, "%.10g", value_to_print );
283  }
284 
285  wxString stringValue( buf, wxConvUTF8 );
286 
287 #endif
288 
289  if( aAddUnitSymbol )
290  {
291  switch( aUnits )
292  {
293  case EDA_UNITS::INCHES:
294  if( aUseMils )
295  stringValue += wxT( " mils" );
296  else
297  stringValue += wxT( " in" );
298  break;
299 
301  stringValue += wxT( " mm" );
302  break;
303 
304  case EDA_UNITS::DEGREES:
305  stringValue += wxT( " deg" );
306  break;
307 
308  case EDA_UNITS::PERCENT:
309  stringValue += wxT( "%" );
310  break;
311 
312  case EDA_UNITS::UNSCALED:
313  break;
314  }
315  }
316 
317  return stringValue;
318 }
double To_User_Unit(EDA_UNITS aUnit, double aValue, bool aUseMils)
Function To_User_Unit convert aValue in internal units to the appropriate user units defined by aUnit...
Definition: base_units.cpp:92
#define KI_FALLTHROUGH
The KI_FALLTHROUGH macro is to be used when switch statement cases should purposely fallthrough from ...
Definition: macros.h:88
void StripTrailingZeros(wxString &aStringValue, unsigned aTrailingZeroAllowed)
Function StripTrailingZeros Remove trailing 0 from a string containing a converted float number.
Definition: base_units.cpp:201
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, CPTREE &aTree)
Function Format outputs a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:201

References AREA, DEGREES, DISTANCE, Format(), INCHES, KI_FALLTHROUGH, MILLIMETRES, PERCENT, StripTrailingZeros(), To_User_Unit(), UNSCALED, and VOLUME.

Referenced by PANEL_SETUP_TRACKS_AND_VIAS::AppendDiffPairs(), PANEL_SETUP_TRACKS_AND_VIAS::AppendTrackWidth(), PANEL_SETUP_TRACKS_AND_VIAS::AppendViaSize(), DIALOG_PLOT::applyPlotSettings(), BuildStackupReport(), UNIT_BINDER::ChangeValue(), ConvertOutlineToPolygon(), MICROWAVE_TOOL::createFootprint(), MICROWAVE_TOOL::createMicrowaveInductor(), PANEL_SETUP_BOARD_STACKUP::createRowData(), DIALOG_TRACK_VIA_PROPERTIES::DIALOG_TRACK_VIA_PROPERTIES(), SCH_PIN::GetMsgPanelInfo(), LIB_PIN::GetMsgPanelInfo(), TEXT_MOD_GRID_TABLE::GetValue(), PIN_TABLE_DATA_MODEL::GetValue(), FIELDS_GRID_TABLE< SCH_FIELD >::GetValue(), DIALOG_PLOT::init_Dialog(), DIALOG_PAD_PROPERTIES::initValues(), FOOTPRINT_EDITOR_SETTINGS::MigrateFromLegacy(), PCBNEW_SETTINGS::MigrateFromLegacy(), PANEL_SETUP_BOARD_STACKUP::onCalculateDielectricThickness(), PANEL_SETUP_BOARD_STACKUP::onUpdateThicknessValue(), DIALOG_PAD_PROPERTIES::padValuesOK(), PCB_INSPECTION_TOOL::reportCopperClearance(), PCB_INSPECTION_TOOL::reportZoneConnection(), UNIT_BINDER::SetDoubleValue(), UNIT_BINDER::SetValue(), EDA_POSITION_CTRL::SetValue(), PANEL_SETUP_BOARD_STACKUP::synchronizeWithBoard(), PANEL_SETUP_BOARD_STACKUP::transferDataFromUIToStackup(), DIALOG_GRID_SETTINGS::TransferDataFromWindow(), PANEL_SETUP_FEATURE_CONSTRAINTS::TransferDataToWindow(), DIALOG_PAD_PRIMITIVE_POLY_PROPS::TransferDataToWindow(), UNIT_BINDER::Validate(), and PANEL_SETUP_TRACKS_AND_VIAS::validateData().

◆ StripTrailingZeros()

void StripTrailingZeros ( wxString &  aStringValue,
unsigned  aTrailingZeroAllowed = 1 
)

Function StripTrailingZeros Remove trailing 0 from a string containing a converted float number.

The trailing 0 are removed if the mantissa has more than aTrailingZeroAllowed digits and some trailing 0

Definition at line 201 of file base_units.cpp.

202 {
203  struct lconv * lc = localeconv();
204  char sep = lc->decimal_point[0];
205  unsigned sep_pos = aStringValue.Find( sep );
206 
207  if( sep_pos > 0 )
208  {
209  // We want to keep at least aTrailingZeroAllowed digits after the separator
210  unsigned min_len = sep_pos + aTrailingZeroAllowed + 1;
211 
212  while( aStringValue.Len() > min_len )
213  {
214  if( aStringValue.Last() == '0' )
215  aStringValue.RemoveLast();
216  else
217  break;
218  }
219  }
220 }

Referenced by AngleToStringDegrees(), and StringFromValue().

◆ To_User_Unit()

double To_User_Unit ( EDA_UNITS  aUnit,
double  aValue,
bool  aUseMils = false 
)

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.
aUseMilsIndicates mils should be used for imperial units (inches).

Definition at line 92 of file base_units.cpp.

93 {
94  switch( aUnit )
95  {
97  return IU_TO_MM( aValue );
98 
99  case EDA_UNITS::INCHES:
100  if( aUseMils )
101  return IU_TO_MILS( aValue );
102  else
103  return IU_TO_IN( aValue );
104 
105  case EDA_UNITS::DEGREES:
106  return aValue / 10.0f;
107 
108  default:
109  return aValue;
110  }
111 }

References DEGREES, INCHES, and MILLIMETRES.

Referenced by GRID_MENU::BuildChoiceList(), PL_EDITOR_FRAME::DisplayGridMsg(), GERBVIEW_FRAME::DisplayGridMsg(), EDA_DRAW_FRAME::DisplayGridMsg(), PCB_BASE_FRAME::DisplayGridMsg(), formatPreviewDimension(), DIMENSION::GetValueText(), MessageTextFromValue(), StringFromValue(), PL_EDITOR_FRAME::UpdateStatusBar(), SCH_BASE_FRAME::UpdateStatusBar(), PCB_BASE_FRAME::UpdateStatusBar(), GERBVIEW_FRAME::UpdateStatusBar(), PCB_EDIT_FRAME::UpdateTrackWidthSelectBox(), and PCB_EDIT_FRAME::UpdateViaSizeSelectBox().

◆ ValueFromString()

long long int ValueFromString ( EDA_UNITS  aUnits,
const wxString &  aTextValue,
bool  aUseMils = false,
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
aUnitsThe units of aTextValue.
aTextValueA reference to a wxString object containing the string to convert.
aUseMilsIndicates mils should be used for imperial units (inches).
Returns
The string from Value, according to units (inch, mm ...) for display,

Definition at line 471 of file base_units.cpp.

473 {
474  double value = DoubleValueFromString( aUnits, aTextValue, aUseMils, aType );
475  return KiROUND<double, long long int>( value );
476 }
double DoubleValueFromString(EDA_UNITS aUnits, const wxString &aTextValue, bool aUseMils, EDA_DATA_TYPE aType)
Function DoubleValueFromString converts aTextValue to a double.
Definition: base_units.cpp:346

References DoubleValueFromString().

Referenced by GRID_MENU::BuildChoiceList(), PIN_TABLE_DATA_MODEL::compare(), MICROWAVE_TOOL::createFootprint(), MICROWAVE_TOOL::createMicrowaveInductor(), PANEL_MODEDIT_DEFAULTS::getGridValue(), PANEL_SETUP_TEXT_AND_GRAPHICS::getGridValue(), PANEL_SETUP_BOARD_STACKUP::GetPcbThickness(), DIALOG_ANNOTATE::GetStartNumber(), UNIT_BINDER::GetValue(), EDA_POSITION_CTRL::GetValue(), FOOTPRINT_PREVIEW_PANEL::New(), DIALOG_PAD_PRIMITIVE_POLY_PROPS::onCellChanging(), PANEL_SETUP_BOARD_STACKUP::onThicknessChange(), PANEL_SETUP_BOARD_STACKUP::onUpdateThicknessValue(), COMMON_TOOLS::Reset(), TEXT_MOD_GRID_TABLE::SetValue(), FIELDS_GRID_TABLE< SCH_FIELD >::SetValue(), PIN_TABLE_DATA_MODEL::SetValue(), PANEL_SETUP_BOARD_STACKUP::transferDataFromUIToStackup(), PANEL_SETUP_TRACKS_AND_VIAS::TransferDataFromWindow(), DIALOG_GRID_SETTINGS::TransferDataToWindow(), and PANEL_SETUP_TRACKS_AND_VIAS::validateData().