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/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...
 
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 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 56 of file base_units.h.

◆ INDETERMINATE_ACTION

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

Definition at line 48 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 47 of file base_units.h.

Function Documentation

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

307 {
308  double dtmp = 0;
309 
310  // Acquire the 'right' decimal point separator
311  const struct lconv* lc = localeconv();
312 
313  wxChar decimal_point = lc->decimal_point[0];
314  wxString buf( aTextValue.Strip( wxString::both ) );
315 
316  // Convert any entered decimal point separators to the 'right' one
317  buf.Replace( wxT( "." ), wxString( decimal_point, 1 ) );
318  buf.Replace( wxT( "," ), wxString( decimal_point, 1 ) );
319 
320  // Find the end of the numeric part
321  unsigned brk_point = 0;
322 
323  while( brk_point < buf.Len() )
324  {
325  wxChar ch = buf[brk_point];
326 
327  if( !( (ch >= '0' && ch <= '9') || (ch == decimal_point) || (ch == '-') || (ch == '+') ) )
328  break;
329 
330  ++brk_point;
331  }
332 
333  // Extract the numeric part
334  buf.Left( brk_point ).ToDouble( &dtmp );
335 
336  // Check the optional unit designator (2 ch significant)
337  wxString unit( buf.Mid( brk_point ).Strip( wxString::leading ).Left( 2 ).Lower() );
338 
339  if( aUnits == EDA_UNITS::MILLIMETRES || aUnits == EDA_UNITS::MILS
340  || aUnits == EDA_UNITS::INCHES )
341  {
342  if( unit == wxT( "mm" ) )
343  {
344  aUnits = EDA_UNITS::MILLIMETRES;
345  }
346  else if( unit == wxT( "mi" ) || unit == wxT( "th" ) )
347  {
348  aUnits = EDA_UNITS::MILS;
349  }
350  else if( unit == wxT( "in" ) || unit == wxT( "\"" ) )
351  {
352  aUnits = EDA_UNITS::INCHES;
353  }
354  else if( unit == "oz" ) // 1 oz = 1.37 mils
355  {
356  aUnits = EDA_UNITS::MILS;
357  dtmp *= 1.37;
358  }
359  }
360  else if( aUnits == EDA_UNITS::DEGREES )
361  {
362  if( unit == wxT( "ra" ) ) // Radians
363  {
364  dtmp *= 180.0f / M_PI;
365  }
366  }
367 
368  switch( aType )
369  {
371  dtmp = From_User_Unit( aUnits, dtmp );
373 
374  case EDA_DATA_TYPE::AREA:
375  dtmp = From_User_Unit( aUnits, dtmp );
377 
379  dtmp = From_User_Unit( aUnits, dtmp );
380  }
381 
382  return dtmp;
383 }
#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:281

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

Referenced by 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_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_PREVIEW_3D_MODEL::onMouseWheelOffset(), PANEL_PREVIEW_3D_MODEL::onMouseWheelRot(), PANEL_PREVIEW_3D_MODEL::onMouseWheelScale(), rotationFromString(), FP_TEXT_GRID_TABLE::SetValue(), DIALOG_CREATE_ARRAY::TransferDataFromWindow(), DIALOG_IMPORT_GFX::TransferDataFromWindow(), DIALOG_PAD_PRIMITIVES_TRANSFORM::Transform(), PANEL_PREVIEW_3D_MODEL::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 386 of file base_units.cpp.

387 {
388  wxString buf( aTextValue.Strip( wxString::both ) );
389  unsigned brk_point = 0;
390 
391  while( brk_point < buf.Len() )
392  {
393  wxChar c = buf[brk_point];
394 
395  if( !( (c >= '0' && c <='9') || (c == '.') || (c == ',') || (c == '-') || (c == '+') ) )
396  break;
397 
398  ++brk_point;
399  }
400 
401  // Check the unit designator (2 ch significant)
402  wxString unit( buf.Mid( brk_point ).Strip( wxString::leading ).Left( 2 ).Lower() );
403 
404  if( unit == wxT( "mm" ) )
405  aUnits = EDA_UNITS::MILLIMETRES;
406  else if( unit == wxT( "mi" ) || unit == wxT( "th" ) ) // "mils" or "thou"
407  aUnits = EDA_UNITS::MILS;
408  else if( unit == wxT( "in" ) || unit == wxT( "\"" ) )
409  aUnits = EDA_UNITS::INCHES;
410  else if( unit == wxT( "de" ) || unit == wxT( "ra" ) ) // "deg" or "rad"
411  aUnits = EDA_UNITS::DEGREES;
412 }

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

520 {
521  char temp[50];
522  int len;
523 
524  len = snprintf( temp, sizeof(temp), "%.10g", aAngle / 10.0 );
525 
526  return std::string( temp, len );
527 }

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

485 {
486  char buf[50];
487  double engUnits = aValue;
488  int len;
489 
490  engUnits /= IU_PER_MM;
491 
492  if( engUnits != 0.0 && fabs( engUnits ) <= 0.0001 )
493  {
494  len = snprintf( buf, sizeof(buf), "%.10f", engUnits );
495 
496  // Make sure snprintf() didn't fail and the locale numeric separator is correct.
497  wxCHECK( len >= 0 && len < 50 && strchr( buf, ',' ) == NULL, std::string( "" ) );
498 
499  while( --len > 0 && buf[len] == '0' )
500  buf[len] = '\0';
501 
502  if( buf[len] == '.' )
503  buf[len] = '\0';
504  else
505  ++len;
506  }
507  else
508  {
509  len = snprintf( buf, sizeof(buf), "%.10g", engUnits );
510 
511  // Make sure snprintf() didn't fail and the locale numeric separator is correct.
512  wxCHECK( len >= 0 && len < 50 && strchr( buf, ',' ) == NULL , std::string( "" ) );
513  }
514 
515  return std::string( buf, len );
516 }
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 530 of file base_units.cpp.

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

References FormatInternalUnits().

◆ FormatInternalUnits() [3/4]

std::string FormatInternalUnits ( const wxSize &  aSize)

Definition at line 542 of file base_units.cpp.

543 {
544  return FormatInternalUnits( aSize.GetWidth() ) + " " + FormatInternalUnits( aSize.GetHeight() );
545 }
std::string FormatInternalUnits(int aValue)
Function FormatInternalUnits converts aValue from internal units to a string appropriate for writing ...
Definition: base_units.cpp:484

References FormatInternalUnits().

◆ FormatInternalUnits() [4/4]

std::string FormatInternalUnits ( const VECTOR2I aPoint)

Definition at line 536 of file base_units.cpp.

537 {
538  return FormatInternalUnits( aPoint.x ) + " " + FormatInternalUnits( aPoint.y );
539 }
std::string FormatInternalUnits(int aValue)
Function FormatInternalUnits converts aValue from internal units to a string appropriate for writing ...
Definition: base_units.cpp:484

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

282 {
283  switch( aUnits )
284  {
286  return MM_TO_IU( aValue );
287 
288  case EDA_UNITS::MILS:
289  return MILS_TO_IU( aValue );
290 
291  case EDA_UNITS::INCHES:
292  return IN_TO_IU( aValue );
293 
294  case EDA_UNITS::DEGREES:
295  // Convert to "decidegrees"
296  return aValue * 10;
297 
298  default:
299  case EDA_UNITS::UNSCALED:
300  case EDA_UNITS::PERCENT:
301  return aValue;
302  }
303 }

References DEGREES, INCHES, MILLIMETRES, MILS, 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().

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

424 {
425  switch( aUnit )
426  {
428  switch( aType )
429  {
430  default:
431  wxASSERT( 0 );
434  return _( "mm" );
435  case EDA_DATA_TYPE::AREA:
436  return _( "sq. mm" );
438  return _( "cu. mm" );
439  }
440 
441  case EDA_UNITS::MILS:
442  switch( aType )
443  {
444  default:
445  wxASSERT( 0 );
448  return _( "mils" );
449  case EDA_DATA_TYPE::AREA:
450  return _( "sq. mils" );
452  return _( "cu. mils" );
453  }
454 
455  case EDA_UNITS::INCHES:
456  switch( aType )
457  {
458  default:
459  wxASSERT( 0 );
462  return _( "in" );
463  case EDA_DATA_TYPE::AREA:
464  return _( "sq. in" );
466  return _( "cu. in" );
467  }
468 
469  case EDA_UNITS::PERCENT:
470  return _( "%" );
471 
472  case EDA_UNITS::UNSCALED:
473  return wxEmptyString;
474 
475  case EDA_UNITS::DEGREES:
476  return _( "deg" );
477 
478  default:
479  return wxT( "??" );
480  }
481 }
#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)

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_PREVIEW_3D_MODEL::formatOffsetValue(), PANEL_PREVIEW_3D_MODEL::formatRotationValue(), FP_TEXT_GRID_TABLE::FP_TEXT_GRID_TABLE(), PCB_DIMENSION_BASE::GetMsgPanelInfo(), MessageTextFromValue(), UNIT_BINDER::SetDataType(), UNIT_BINDER::SetUnits(), UNIT_BINDER::SetValue(), UNIT_BINDER::UNIT_BINDER(), and PCB_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 119 of file base_units.cpp.

121 {
122  wxString text;
123  const wxChar* format;
124  double value = aValue;
125 
126  switch( aType )
127  {
129  value = To_User_Unit( aUnits, value );
130  // Fall through to continue computation
132 
133  case EDA_DATA_TYPE::AREA:
134  value = To_User_Unit( aUnits, value );
135  // Fall through to continue computation
137 
139  value = To_User_Unit( aUnits, value );
140  }
141 
142  switch( aUnits )
143  {
144  default:
146 #if defined( EESCHEMA )
147  format = wxT( "%.2f" );
148 #else
149  format = wxT( "%.4f" );
150 #endif
151  break;
152 
153  case EDA_UNITS::MILS:
154 #if defined( EESCHEMA )
155  format = wxT( "%.0f" );
156 #else
157  format = wxT( "%.2f" );
158 #endif
159  break;
160 
161  case EDA_UNITS::INCHES:
162 #if defined( EESCHEMA )
163  format = wxT( "%.3f" );
164 #else
165  format = wxT( "%.4f" );
166 #endif
167  break;
168 
169  case EDA_UNITS::DEGREES:
170  // 3 digits in mantissa should be good for rotation in degree
171  format = wxT( "%.3f" );
172  break;
173 
174  case EDA_UNITS::UNSCALED:
175  format = wxT( "%.0f" );
176  break;
177  }
178 
179  text.Printf( format, value );
180 
181  if( aAddUnitLabel )
182  {
183  text += " ";
184  text += GetAbbreviatedUnitsLabel( aUnits, aType );
185  }
186 
187  return text;
188 }
#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:423
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:68

References AREA, DEGREES, DISTANCE, GetAbbreviatedUnitsLabel(), INCHES, KI_FALLTHROUGH, MILLIMETRES, MILS, text, 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 103 of file base_units.cpp.

105 {
106  return MessageTextFromValue( aUnits, double( aValue ), aAddUnitLabel, aType );
107 }
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:103

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(), DRAWING_TOOL::DrawBoardCharacteristics(), DRC_ENGINE::EvalRules(), formatCoord(), DIALOG_NET_INSPECTOR::formatLength(), LIB_CIRCLE::GetMsgPanelInfo(), LIB_ARC::GetMsgPanelInfo(), LIB_RECTANGLE::GetMsgPanelInfo(), SCH_PIN::GetMsgPanelInfo(), LIB_POLYLINE::GetMsgPanelInfo(), PL_EDITOR_LAYOUT::GetMsgPanelInfo(), LIB_TEXT::GetMsgPanelInfo(), LIB_BEZIER::GetMsgPanelInfo(), PCB_TEXT::GetMsgPanelInfo(), PCB_TARGET::GetMsgPanelInfo(), DS_DRAW_ITEM_BASE::GetMsgPanelInfo(), ZONE::GetMsgPanelInfo(), LIB_FIELD::GetMsgPanelInfo(), SCH_BITMAP::GetMsgPanelInfo(), NETINFO_ITEM::GetMsgPanelInfo(), LIB_PIN::GetMsgPanelInfo(), PCB_TRACK::GetMsgPanelInfo(), FP_TEXT::GetMsgPanelInfo(), PCB_DIMENSION_BASE::GetMsgPanelInfo(), SCH_TEXT::GetMsgPanelInfo(), PCB_SHAPE::GetMsgPanelInfo(), PCB_VIA::GetMsgPanelInfo(), PCB_DIM_ALIGNED::GetMsgPanelInfo(), PCB_DIM_LEADER::GetMsgPanelInfo(), PAD::GetMsgPanelInfo(), LIB_CIRCLE::GetSelectMenuText(), LIB_RECTANGLE::GetSelectMenuText(), LIB_ARC::GetSelectMenuText(), DS_DRAW_ITEM_LINE::GetSelectMenuText(), PCB_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_TO_HOLE::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(), BOARD_NETLIST_UPDATER::updateCopperZoneNets(), 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 111 of file base_units.cpp.

113 {
114  return MessageTextFromValue( aUnits, double( aValue ), aAddUnitLabel, aType );
115 }
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:103

References MessageTextFromValue().

◆ Mils2mm()

int Mils2mm ( double  x)

Convert mils to mm.

Definition at line 62 of file base_units.cpp.

63 {
64  return KiROUND( x * 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:70

References KiROUND().

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

◆ Mm2mils()

int Mm2mils ( double  x)

Convert mm to mils.

Definition at line 56 of file base_units.cpp.

57 {
58  return KiROUND( x * 1000. / 25.4 );
59 }
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:70

References KiROUND().

Referenced by SCH_SEXPR_PARSER::parsePAGE_INFO(), and PCB_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 203 of file base_units.cpp.

205 {
206  double value_to_print = aValue;
207 
208  switch( aType )
209  {
211  value_to_print = To_User_Unit( aUnits, value_to_print );
213 
214  case EDA_DATA_TYPE::AREA:
215  value_to_print = To_User_Unit( aUnits, value_to_print );
217 
219  value_to_print = To_User_Unit( aUnits, value_to_print );
220  }
221 
222 
223  char buf[50];
224  int len;
225 
226  if( value_to_print != 0.0 && fabs( value_to_print ) <= 0.0001 )
227  {
228  len = sprintf( buf, "%.10f", value_to_print );
229 
230  while( --len > 0 && buf[len] == '0' )
231  buf[len] = '\0';
232 
233  if( buf[len]=='.' || buf[len]==',' )
234  buf[len] = '\0';
235  else
236  ++len;
237  }
238  else
239  {
240  if( aUnits == EDA_UNITS::MILS )
241  len = sprintf( buf, "%.7g", value_to_print );
242  else
243  len = sprintf( buf, "%.10g", value_to_print );
244  }
245 
246  wxString stringValue( buf, wxConvUTF8 );
247 
248  if( aAddUnitSymbol )
249  {
250  switch( aUnits )
251  {
253  stringValue += wxT( " mm" );
254  break;
255 
256  case EDA_UNITS::DEGREES:
257  stringValue += wxT( " deg" );
258  break;
259 
260  case EDA_UNITS::MILS:
261  stringValue += wxT( " mils" );
262  break;
263 
264  case EDA_UNITS::INCHES:
265  stringValue += wxT( " in" );
266  break;
267 
268  case EDA_UNITS::PERCENT:
269  stringValue += wxT( "%" );
270  break;
271 
272  case EDA_UNITS::UNSCALED:
273  break;
274  }
275  }
276 
277  return stringValue;
278 }
#define KI_FALLTHROUGH
The KI_FALLTHROUGH macro is to be used when switch statement cases should purposely fallthrough from ...
Definition: macros.h:83
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:68

References AREA, DEGREES, DISTANCE, INCHES, KI_FALLTHROUGH, MILLIMETRES, MILS, PERCENT, 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(), PANEL_SETUP_BOARD_STACKUP::computeBoardThickness(), MICROWAVE_TOOL::createFootprint(), MICROWAVE_TOOL::createMicrowaveInductor(), PANEL_SETUP_BOARD_STACKUP::createRowData(), DIALOG_TRACK_VIA_PROPERTIES::DIALOG_TRACK_VIA_PROPERTIES(), DRAWING_TOOL::DrawBoardCharacteristics(), DRAWING_TOOL::DrawSpecificationStackup(), 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(), 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(), 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 68 of file base_units.cpp.

69 {
70  switch( aUnit )
71  {
73  return IU_TO_MM( aValue );
74 
75  case EDA_UNITS::MILS:
76  return IU_TO_MILS( aValue );
77 
78  case EDA_UNITS::INCHES:
79  return IU_TO_IN( aValue );
80 
81  case EDA_UNITS::DEGREES:
82  return aValue / 10.0f;
83 
84  default:
85  return aValue;
86  }
87 }

References DEGREES, INCHES, MILLIMETRES, and MILS.

Referenced by ComboBoxUnits(), PROPERTIES_FRAME::CopyPrmsFromPanelToGeneral(), PROPERTIES_FRAME::CopyPrmsFromPanelToItem(), KIGFX::PREVIEW::DimensionLabel(), PL_EDITOR_FRAME::DisplayGridMsg(), PCB_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 415 of file base_units.cpp.

416 {
417  double value = DoubleValueFromString( aUnits, aTextValue, aType );
418 
419  return KiROUND<double, long long int>( value );
420 }
double DoubleValueFromString(EDA_UNITS aUnits, const wxString &aTextValue, EDA_DATA_TYPE aType)
Function DoubleValueFromString converts aTextValue to a double.
Definition: base_units.cpp:306

References DoubleValueFromString().

Referenced by GRID_MENU::BuildChoiceList(), PIN_TABLE_DATA_MODEL::compare(), PANEL_SETUP_BOARD_STACKUP::computeBoardThickness(), 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(), 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(), and DIALOG_GRID_SETTINGS::TransferDataToWindow().