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 <eda_units.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...
 
double To_User_Unit (EDA_UNITS aUnit, double aValue)
 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 aAddUnitLabel=true, 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 aAddUnitLabel=true, EDA_DATA_TYPE aType=EDA_DATA_TYPE::DISTANCE)
 Convert a value to a string using double notation. More...
 
wxString MessageTextFromValue (EDA_UNITS aUnits, long long int aValue, bool aAddUnitLabel=true, EDA_DATA_TYPE aType=EDA_DATA_TYPE::DISTANCE)
 
wxString StringFromValue (EDA_UNITS aUnit, double aValue, bool aAddUnitSymbol=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)
 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, 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, 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)
 Function FetchUnitsFromString writes any unit info found in the string to aUnits. More...
 
wxString GetAbbreviatedUnitsLabel (EDA_UNITS aUnit, 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.

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

Definition at line 461 of file base_units.cpp.

462 {
463  wxString text;
464 
465  text.Printf( wxT( "%.3f" ), aAngle / 10.0 );
466  StripTrailingZeros( text, 1 );
467 
468  return text;
469 }
void StripTrailingZeros(wxString &aStringValue, unsigned aTrailingZeroAllowed)
Remove trailing zeros from a string containing a converted float number.
Definition: string.cpp:830

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

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

Referenced by GBR_TO_PCB_EXPORTER::export_non_copper_item(), GBR_TO_PCB_EXPORTER::export_via(), PCB_IO::format(), BOARD_STACKUP::FormatBoardStackup(), PCB_IO::formatSetup(), 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,
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.
Returns
A double representing that value in internal units

Definition at line 338 of file base_units.cpp.

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

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

Referenced by PCB_UNIT_RESOLVER::Convert(), 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(), FP_TEXT_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 
)

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

Definition at line 422 of file base_units.cpp.

423 {
424  wxString buf( aTextValue.Strip( wxString::both ) );
425  unsigned brk_point = 0;
426 
427  while( brk_point < buf.Len() )
428  {
429  wxChar c = buf[brk_point];
430 
431  if( !( (c >= '0' && c <='9') || (c == '.') || (c == ',') || (c == '-') || (c == '+') ) )
432  break;
433 
434  ++brk_point;
435  }
436 
437  // Check the unit designator (2 ch significant)
438  wxString unit( buf.Mid( brk_point ).Strip( wxString::leading ).Left( 2 ).Lower() );
439 
440  if( unit == wxT( "mm" ) )
441  aUnits = EDA_UNITS::MILLIMETRES;
442  else if( unit == wxT( "mi" ) || unit == wxT( "th" ) ) // "mils" or "thou"
443  aUnits = EDA_UNITS::MILS;
444  else if( unit == wxT( "in" ) || unit == wxT( "\"" ) )
445  aUnits = EDA_UNITS::INCHES;
446  else if( unit == wxT( "de" ) || unit == wxT( "ra" ) ) // "deg" or "rad"
447  aUnits = EDA_UNITS::DEGREES;
448 }

References DEGREES, INCHES, MILLIMETRES, and MILS.

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

569 {
570  char temp[50];
571  int len;
572 
573  len = snprintf( temp, sizeof(temp), "%.10g", aAngle / 10.0 );
574 
575  return std::string( temp, len );
576 }

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

534 {
535  char buf[50];
536  double engUnits = aValue;
537  int len;
538 
539  engUnits /= IU_PER_MM;
540 
541  if( engUnits != 0.0 && fabs( engUnits ) <= 0.0001 )
542  {
543  len = snprintf( buf, sizeof(buf), "%.10f", engUnits );
544 
545  // Make sure snprintf() didn't fail and the locale numeric separator is correct.
546  wxCHECK( len >= 0 && len < 50 && strchr( buf, ',' ) == NULL, std::string( "" ) );
547 
548  while( --len > 0 && buf[len] == '0' )
549  buf[len] = '\0';
550 
551  if( buf[len] == '.' )
552  buf[len] = '\0';
553  else
554  ++len;
555  }
556  else
557  {
558  len = snprintf( buf, sizeof(buf), "%.10g", engUnits );
559 
560  // Make sure snprintf() didn't fail and the locale numeric separator is correct.
561  wxCHECK( len >= 0 && len < 50 && strchr( buf, ',' ) == NULL , std::string( "" ) );
562  }
563 
564  return std::string( buf, len );
565 }
static constexpr double IU_PER_MM
Mock up a conversion function.
#define NULL

References IU_PER_MM, and NULL.

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

580 {
581  return FormatInternalUnits( aPoint.x ) + " " + FormatInternalUnits( aPoint.y );
582 }
std::string FormatInternalUnits(int aValue)
Function FormatInternalUnits converts aValue from internal units to a string appropriate for writing ...
Definition: base_units.cpp:533

References FormatInternalUnits().

◆ FormatInternalUnits() [3/4]

std::string FormatInternalUnits ( const wxSize &  aSize)

Definition at line 591 of file base_units.cpp.

592 {
593  return FormatInternalUnits( aSize.GetWidth() ) + " " + FormatInternalUnits( aSize.GetHeight() );
594 }
std::string FormatInternalUnits(int aValue)
Function FormatInternalUnits converts aValue from internal units to a string appropriate for writing ...
Definition: base_units.cpp:533

References FormatInternalUnits().

◆ FormatInternalUnits() [4/4]

std::string FormatInternalUnits ( const VECTOR2I aPoint)

Definition at line 585 of file base_units.cpp.

586 {
587  return FormatInternalUnits( aPoint.x ) + " " + FormatInternalUnits( aPoint.y );
588 }
std::string FormatInternalUnits(int aValue)
Function FormatInternalUnits converts aValue from internal units to a string appropriate for writing ...
Definition: base_units.cpp:533

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

◆ From_User_Unit()

double From_User_Unit ( 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 313 of file base_units.cpp.

314 {
315  switch( aUnits )
316  {
318  return MM_TO_IU( aValue );
319 
320  case EDA_UNITS::MILS:
321  return MILS_TO_IU( aValue );
322 
323  case EDA_UNITS::INCHES:
324  return IN_TO_IU( aValue );
325 
326  case EDA_UNITS::DEGREES:
327  // Convert to "decidegrees"
328  return aValue * 10;
329 
330  default:
331  case EDA_UNITS::UNSCALED:
332  case EDA_UNITS::PERCENT:
333  return aValue;
334  }
335 }

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

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

◆ GetAbbreviatedUnitsLabel()

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

Get the units string for a given units type.

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

Definition at line 472 of file base_units.cpp.

473 {
474  switch( aUnit )
475  {
477  switch( aType )
478  {
479  default:
480  wxASSERT( 0 );
483  return _( "mm" );
484  case EDA_DATA_TYPE::AREA:
485  return _( "sq. mm" );
487  return _( "cu. mm" );
488  }
489 
490  case EDA_UNITS::MILS:
491  switch( aType )
492  {
493  default:
494  wxASSERT( 0 );
497  return _( "mils" );
498  case EDA_DATA_TYPE::AREA:
499  return _( "sq. mils" );
501  return _( "cu. mils" );
502  }
503 
504  case EDA_UNITS::INCHES:
505  switch( aType )
506  {
507  default:
508  wxASSERT( 0 );
511  return _( "in" );
512  case EDA_DATA_TYPE::AREA:
513  return _( "sq. in" );
515  return _( "cu. in" );
516  }
517 
518  case EDA_UNITS::PERCENT:
519  return _( "%" );
520 
521  case EDA_UNITS::UNSCALED:
522  return wxEmptyString;
523 
524  case EDA_UNITS::DEGREES:
525  return _( "deg" );
526 
527  default:
528  return wxT( "??" );
529  }
530 }
#define KI_FALLTHROUGH
The KI_FALLTHROUGH macro is to be used when switch statement cases should purposely fallthrough from ...
Definition: macros.h:83
#define _(s)
Definition: 3d_actions.cpp:33

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

Referenced by DIALOG_PLOT::applyPlotSettings(), UNIT_BINDER::ChangeValue(), ComboBoxUnits(), DIALOG_TRACK_VIA_PROPERTIES::DIALOG_TRACK_VIA_PROPERTIES(), KIGFX::PREVIEW::DimensionLabel(), PANEL_PREV_3D::formatOffsetValue(), PANEL_PREV_3D::formatRotationValue(), FP_TEXT_GRID_TABLE::FP_TEXT_GRID_TABLE(), DIMENSION_BASE::GetMsgPanelInfo(), MessageTextFromValue(), UNIT_BINDER::SetDataType(), UNIT_BINDER::SetUnits(), UNIT_BINDER::SetValue(), UNIT_BINDER::UNIT_BINDER(), and DIMENSION_BASE::updateText().

◆ MessageTextFromValue() [1/3]

wxString MessageTextFromValue ( EDA_UNITS  aUnits,
double  aValue,
bool  aAddUnitLabel = true,
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.
aAddUnitLabelIf true, adds the unit label to the end of the string
aTypeType of the unit being used (e.g. distance, area, etc.)
Returns
The converted string for display in user interface elements.

Definition at line 141 of file base_units.cpp.

143 {
144  wxString text;
145  const wxChar* format;
146  double value = aValue;
147 
148  switch( aType )
149  {
151  value = To_User_Unit( aUnits, value );
152  // Fall through to continue computation
154 
155  case EDA_DATA_TYPE::AREA:
156  value = To_User_Unit( aUnits, value );
157  // Fall through to continue computation
159 
161  value = To_User_Unit( aUnits, value );
162  }
163 
164  switch( aUnits )
165  {
166  default:
168 #if defined( EESCHEMA )
169  format = wxT( "%.2f" );
170 #else
171  format = wxT( "%.4f" );
172 #endif
173  break;
174 
175  case EDA_UNITS::MILS:
176 #if defined( EESCHEMA )
177  format = wxT( "%.0f" );
178 #else
179  format = wxT( "%.2f" );
180 #endif
181  break;
182 
183  case EDA_UNITS::INCHES:
184 #if defined( EESCHEMA )
185  format = wxT( "%.3f" );
186 #else
187  format = wxT( "%.4f" );
188 #endif
189  break;
190 
191  case EDA_UNITS::DEGREES:
192  // 3 digits in mantissa should be good for rotation in degree
193  format = wxT( "%.3f" );
194  break;
195 
196  case EDA_UNITS::UNSCALED:
197  format = wxT( "%.0f" );
198  break;
199  }
200 
201  text.Printf( format, value );
202 
203  if( aAddUnitLabel )
204  {
205  text += " ";
206  text += GetAbbreviatedUnitsLabel( aUnits, aType );
207  }
208 
209  return text;
210 }
#define KI_FALLTHROUGH
The KI_FALLTHROUGH macro is to be used when switch statement cases should purposely fallthrough from ...
Definition: macros.h:83
wxString GetAbbreviatedUnitsLabel(EDA_UNITS aUnit, EDA_DATA_TYPE aType)
Get the units string for a given units type.
Definition: base_units.cpp:472
double To_User_Unit(EDA_UNITS aUnit, double aValue)
Function To_User_Unit convert aValue in internal units to the appropriate user units defined by aUnit...
Definition: base_units.cpp:91

References AREA, DEGREES, DISTANCE, GetAbbreviatedUnitsLabel(), INCHES, KI_FALLTHROUGH, MILLIMETRES, MILS, To_User_Unit(), UNSCALED, and VOLUME.

◆ MessageTextFromValue() [2/3]

wxString MessageTextFromValue ( EDA_UNITS  aUnits,
int  aValue,
bool  aAddUnitLabel,
EDA_DATA_TYPE  aType 
)

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

127 {
128  return MessageTextFromValue( aUnits, double( aValue ), aAddUnitLabel, aType );
129 }
wxString MessageTextFromValue(EDA_UNITS aUnits, int aValue, bool aAddUnitLabel, EDA_DATA_TYPE aType)
Convert a value to a string using double notation.
Definition: base_units.cpp:125

References MessageTextFromValue().

Referenced by GRID_MENU::BuildChoiceList(), DRC_TEST_PROVIDER_MATCHED_LENGTH::checkLengthViolations(), DRC_TEST_PROVIDER_HOLE_SIZE::checkPad(), DRC_TEST_PROVIDER_MATCHED_LENGTH::checkSkewViolations(), DRC_TEST_PROVIDER_HOLE_SIZE::checkVia(), DIALOG_BOARD_REANNOTATE::CoordTowxString(), GERBVIEW_FRAME::DisplayGridMsg(), EDA_DRAW_FRAME::DisplayGridMsg(), PCB_BASE_FRAME::DisplayGridMsg(), DIALOG_PAD_PROPERTIES::displayPrimitivesList(), DRC_ENGINE::EvalRules(), formatCoord(), DIALOG_NET_INSPECTOR::formatLength(), LIB_CIRCLE::GetMsgPanelInfo(), LIB_RECTANGLE::GetMsgPanelInfo(), SCH_PIN::GetMsgPanelInfo(), LIB_ARC::GetMsgPanelInfo(), LIB_POLYLINE::GetMsgPanelInfo(), LIB_TEXT::GetMsgPanelInfo(), PL_EDITOR_LAYOUT::GetMsgPanelInfo(), LIB_BEZIER::GetMsgPanelInfo(), PCB_TEXT::GetMsgPanelInfo(), DS_DRAW_ITEM_BASE::GetMsgPanelInfo(), PCB_TARGET::GetMsgPanelInfo(), ZONE::GetMsgPanelInfo(), SCH_BITMAP::GetMsgPanelInfo(), NETINFO_ITEM::GetMsgPanelInfo(), LIB_FIELD::GetMsgPanelInfo(), TRACK::GetMsgPanelInfo(), FP_TEXT::GetMsgPanelInfo(), LIB_PIN::GetMsgPanelInfo(), DIMENSION_BASE::GetMsgPanelInfo(), PCB_SHAPE::GetMsgPanelInfo(), SCH_TEXT::GetMsgPanelInfo(), ALIGNED_DIMENSION::GetMsgPanelInfo(), VIA::GetMsgPanelInfo(), LEADER::GetMsgPanelInfo(), PAD::GetMsgPanelInfo(), LIB_CIRCLE::GetSelectMenuText(), LIB_RECTANGLE::GetSelectMenuText(), LIB_ARC::GetSelectMenuText(), DS_DRAW_ITEM_LINE::GetSelectMenuText(), TRACK::GetSelectMenuText(), SCH_LINE::GetSelectMenuText(), DS_DRAW_ITEM_RECT::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_SILK_TO_MASK::Run(), DRC_TEST_PROVIDER_SILK_CLEARANCE::Run(), test::DRC_TEST_PROVIDER_DIFF_PAIR_COUPLING::Run(), DRC_TEST_PROVIDER_MATCHED_LENGTH::runInternal(), DIALOG_BOARD_STATISTICS::saveReportClicked(), RC_ITEM::ShowCoord(), DRC_TEST_PROVIDER_EDGE_CLEARANCE::testAgainstEdge(), DRC_TEST_PROVIDER_COURTYARD_CLEARANCE::testCourtyardClearances(), DRC_TEST_PROVIDER_HOLE_CLEARANCE::testHoleAgainstHole(), DRC_TEST_PROVIDER_COPPER_CLEARANCE::testItemAgainstZones(), DRC_TEST_PROVIDER_COPPER_CLEARANCE::testPadAgainstItem(), DRC_TEST_PROVIDER_COPPER_CLEARANCE::testTrackAgainstItem(), 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(), GERBVIEW_FRAME::UpdateStatusBar(), SCH_BASE_FRAME::UpdateStatusBar(), PCB_BASE_FRAME::UpdateStatusBar(), and DIALOG_BOARD_STATISTICS::updateWidets().

◆ MessageTextFromValue() [3/3]

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

Definition at line 133 of file base_units.cpp.

135 {
136  return MessageTextFromValue( aUnits, double( aValue ), aAddUnitLabel, aType );
137 }
wxString MessageTextFromValue(EDA_UNITS aUnits, int aValue, bool aAddUnitLabel, EDA_DATA_TYPE aType)
Convert a value to a string using double notation.
Definition: base_units.cpp:125

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  aUnits,
double  aValue,
bool  aAddUnitSymbol,
EDA_DATA_TYPE  aType 
)

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
Returns
A wxString object containing value and optionally the symbol unit (like 2.000 mm)

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

227 {
228  double value_to_print = aValue;
229 
230  switch( aType )
231  {
233  value_to_print = To_User_Unit( aUnits, value_to_print );
235 
236  case EDA_DATA_TYPE::AREA:
237  value_to_print = To_User_Unit( aUnits, value_to_print );
239 
241  value_to_print = To_User_Unit( aUnits, value_to_print );
242  }
243 
244 
245 #if defined( EESCHEMA )
246  wxString stringValue = wxString::Format( wxT( "%.3f" ), value_to_print );
247 
248  // Strip trailing zeros. However, keep at least 3 digits in mantissa for readability.
249  StripTrailingZeros( stringValue, 3 );
250 
251 #else
252 
253  char buf[50];
254  int len;
255 
256  if( value_to_print != 0.0 && fabs( value_to_print ) <= 0.0001 )
257  {
258  len = sprintf( buf, "%.10f", value_to_print );
259 
260  while( --len > 0 && buf[len] == '0' )
261  buf[len] = '\0';
262 
263  if( buf[len]=='.' || buf[len]==',' )
264  buf[len] = '\0';
265  else
266  ++len;
267  }
268  else
269  {
270  if( aUnits == EDA_UNITS::MILS )
271  len = sprintf( buf, "%.7g", value_to_print );
272  else
273  len = sprintf( buf, "%.10g", value_to_print );
274  }
275 
276  wxString stringValue( buf, wxConvUTF8 );
277 
278 #endif
279 
280  if( aAddUnitSymbol )
281  {
282  switch( aUnits )
283  {
285  stringValue += wxT( " mm" );
286  break;
287 
288  case EDA_UNITS::DEGREES:
289  stringValue += wxT( " deg" );
290  break;
291 
292  case EDA_UNITS::MILS:
293  stringValue += wxT( " mils" );
294  break;
295 
296  case EDA_UNITS::INCHES:
297  stringValue += wxT( " in" );
298  break;
299 
300  case EDA_UNITS::PERCENT:
301  stringValue += wxT( "%" );
302  break;
303 
304  case EDA_UNITS::UNSCALED:
305  break;
306  }
307  }
308 
309  return stringValue;
310 }
#define KI_FALLTHROUGH
The KI_FALLTHROUGH macro is to be used when switch statement cases should purposely fallthrough from ...
Definition: macros.h:83
void StripTrailingZeros(wxString &aStringValue, unsigned aTrailingZeroAllowed)
Remove trailing zeros from a string containing a converted float number.
Definition: string.cpp:830
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
double To_User_Unit(EDA_UNITS aUnit, double aValue)
Function To_User_Unit convert aValue in internal units to the appropriate user units defined by aUnit...
Definition: base_units.cpp:91

References AREA, DEGREES, DISTANCE, Format(), INCHES, KI_FALLTHROUGH, MILLIMETRES, MILS, 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(), 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(), FP_TEXT_GRID_TABLE::GetValue(), PIN_TABLE_DATA_MODEL::GetValue(), FIELDS_GRID_TABLE< SCH_FIELD >::GetValue(), DIALOG_PLOT::init_Dialog(), DIALOG_PAD_PROPERTIES::initValues(), BOARD_INSPECTION_TOOL::InspectConstraints(), FOOTPRINT_EDITOR_SETTINGS::MigrateFromLegacy(), PCBNEW_SETTINGS::MigrateFromLegacy(), PANEL_SETUP_BOARD_STACKUP::onUpdateThicknessValue(), BOARD_INSPECTION_TOOL::reportClearance(), BOARD_INSPECTION_TOOL::reportZoneConnection(), UNIT_BINDER::SetDoubleValue(), UNIT_BINDER::SetValue(), EDA_POSITION_CTRL::SetValue(), PANEL_SETUP_BOARD_STACKUP::synchronizeWithBoard(), DIALOG_GRID_SETTINGS::TransferDataFromWindow(), PANEL_SETUP_CONSTRAINTS::TransferDataToWindow(), DIALOG_PAD_PRIMITIVE_POLY_PROPS::TransferDataToWindow(), PANEL_SETUP_TRACKS_AND_VIAS::Validate(), and UNIT_BINDER::Validate().

◆ To_User_Unit()

double To_User_Unit ( 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 91 of file base_units.cpp.

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

References DEGREES, INCHES, MILLIMETRES, and MILS.

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

◆ ValueFromString()

long long int ValueFromString ( 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
aUnitsThe units of aTextValue.
aTextValueA reference to a wxString object containing the string to convert.
Returns
The string from Value, according to units (inch, mm ...) for display,

Definition at line 451 of file base_units.cpp.

452 {
453  double value = DoubleValueFromString( aUnits, aTextValue, aType );
454  return KiROUND<double, long long int>( value );
455 }
double DoubleValueFromString(EDA_UNITS aUnits, const wxString &aTextValue, EDA_DATA_TYPE aType)
Function DoubleValueFromString converts aTextValue to a double.
Definition: base_units.cpp:338

References DoubleValueFromString().

Referenced by GRID_MENU::BuildChoiceList(), PIN_TABLE_DATA_MODEL::compare(), MICROWAVE_TOOL::createFootprint(), MICROWAVE_TOOL::createMicrowaveInductor(), PANEL_FP_EDITOR_DEFAULTS::getGridValue(), PANEL_SETUP_TEXT_AND_GRAPHICS::getGridValue(), DIALOG_ANNOTATE::GetStartNumber(), EDA_POSITION_CTRL::GetValue(), UNIT_BINDER::GetValue(), FOOTPRINT_PREVIEW_PANEL::New(), DIALOG_PAD_PRIMITIVE_POLY_PROPS::onCellChanging(), PANEL_SETUP_BOARD_STACKUP::onThicknessChange(), PANEL_SETUP_BOARD_STACKUP::onUpdateThicknessValue(), COMMON_TOOLS::Reset(), FP_TEXT_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::Validate().