KiCad PCB EDA Suite
base_units.cpp File Reference

Code to handle objects that require both schematic and board internal units. More...

#include <base_struct.h>
#include <base_units.h>
#include <common.h>
#include <math/util.h>
#include <macros.h>
#include <title_block.h>
#include "libeval/numeric_evaluator.h"

Go to the source code of this file.

Functions

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, bool aUseMils)
 Function To_User_Unit convert aValue in internal units to the appropriate user units defined by aUnit. More...
 
wxString MessageTextFromValue (EDA_UNITS aUnits, int aValue, bool aUseMils, EDA_DATA_TYPE aType)
 
wxString MessageTextFromValue (EDA_UNITS aUnits, long long int aValue, bool aUseMils, EDA_DATA_TYPE aType)
 
wxString MessageTextFromValue (EDA_UNITS aUnits, double aValue, bool aUseMils, EDA_DATA_TYPE aType)
 Function MessageTextFromValue is a helper to convert the double length aValue to a string in inches, millimeters, or unscaled units. More...
 
void StripTrailingZeros (wxString &aStringValue, unsigned aTrailingZeroAllowed)
 Function StripTrailingZeros Remove trailing 0 from a string containing a converted float number. More...
 
wxString StringFromValue (EDA_UNITS aUnits, double aValue, bool aAddUnitSymbol, bool aUseMils)
 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 aUnits, double aValue, bool aUseMils)
 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)
 Function DoubleValueFromString converts aTextValue to a double. 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...
 
long long int ValueFromString (EDA_UNITS aUnits, const wxString &aTextValue, bool aUseMils)
 Function ValueFromString converts aTextValue in aUnits to internal units used by the application. 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 GetAbbreviatedUnitsLabel (EDA_UNITS aUnit, bool aUseMils, EDA_DATA_TYPE aType)
 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 VECTOR2I &aPoint)
 
std::string FormatInternalUnits (const wxSize &aSize)
 

Detailed Description

Code to handle objects 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
Note
This file is an ugly hack to solve the problem of formatting the base units for either schematics or boards in objects that are include in both domains. At some point in the future. This code should be rolled back into the appropriate object and build with the correct internal unit formatting depending on the application.

Definition in file base_units.cpp.

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

457 {
458  wxString text;
459 
460  text.Printf( wxT( "%.3f" ), aAngle/10.0 );
461  StripTrailingZeros( text, 1 );
462 
463  return text;
464 }
void StripTrailingZeros(wxString &aStringValue, unsigned aTrailingZeroAllowed)
Function StripTrailingZeros Remove trailing 0 from a string containing a converted float number.
Definition: base_units.cpp:202

References StripTrailingZeros().

Referenced by PANEL_PCBNEW_SETTINGS::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 65 of file base_units.cpp.

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

Referenced by GBR_TO_PCB_EXPORTER::export_flashed_copper_item(), PCB_IO::format(), BOARD_STACKUP::FormatBoardStackup(), PCB_IO::formatSetup(), GBR_TO_PCB_EXPORTER::writeCopperLineItem(), GBR_TO_PCB_EXPORTER::writePcbLineItem(), GBR_TO_PCB_EXPORTER::writePcbPolygonItem(), and GBR_TO_PCB_EXPORTER::writePcbZoneItem().

◆ DoubleValueFromString()

double DoubleValueFromString ( EDA_UNITS  aUnits,
const wxString &  aTextValue,
bool  aUseMils = false 
)

Function DoubleValueFromString converts aTextValue to a double.

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

332 {
333  double value;
334  double dtmp = 0;
335 
336  // Acquire the 'right' decimal point separator
337  const struct lconv* lc = localeconv();
338 
339  wxChar decimal_point = lc->decimal_point[0];
340  wxString buf( aTextValue.Strip( wxString::both ) );
341 
342  // Convert the period in decimal point
343  buf.Replace( wxT( "." ), wxString( decimal_point, 1 ) );
344 
345  // Find the end of the numeric part
346  unsigned brk_point = 0;
347 
348  while( brk_point < buf.Len() )
349  {
350  wxChar ch = buf[brk_point];
351 
352  if( !( (ch >= '0' && ch <='9') || (ch == decimal_point) || (ch == '-') || (ch == '+') ) )
353  {
354  break;
355  }
356 
357  ++brk_point;
358  }
359 
360  // Extract the numeric part
361  buf.Left( brk_point );
362 
363  buf.ToDouble( &dtmp );
364 
365  // Check the optional unit designator (2 ch significant)
366  wxString unit( buf.Mid( brk_point ).Strip( wxString::leading ).Left( 2 ).Lower() );
367 
368  if( aUnits == EDA_UNITS::INCHES || aUnits == EDA_UNITS::MILLIMETRES )
369  {
370  if( unit == wxT( "in" ) || unit == wxT( "\"" ) )
371  {
372  aUnits = EDA_UNITS::INCHES;
373  aUseMils = false;
374  }
375  else if( unit == wxT( "mm" ) )
376  {
377  aUnits = EDA_UNITS::MILLIMETRES;
378  }
379  else if( unit == wxT( "mi" ) || unit == wxT( "th" ) ) // "mils" or "thou"
380  {
381  aUnits = EDA_UNITS::INCHES;
382  aUseMils = true;
383  }
384  else if( unit == "oz" ) // 1 oz = 1.37 mils
385  {
386  aUnits = EDA_UNITS::INCHES;
387  aUseMils = true;
388  dtmp *= 1.37;
389  }
390  }
391  else if( aUnits == EDA_UNITS::DEGREES )
392  {
393  if( unit == wxT( "ra" ) ) // Radians
394  {
395  dtmp *= 180.0f / M_PI;
396  }
397  }
398 
399  value = From_User_Unit( aUnits, dtmp, aUseMils );
400 
401  return value;
402 }
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:306

References DEGREES, From_User_Unit(), INCHES, and MILLIMETRES.

Referenced by PANEL_PREV_3D::doIncrementOffset(), PANEL_PREV_3D::doIncrementRotation(), PANEL_PREV_3D::doIncrementScale(), UNIT_BINDER::GetDoubleValue(), 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_IMPORT_GFX::TransferDataFromWindow(), DIALOG_CREATE_ARRAY::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 405 of file base_units.cpp.

406 {
407  wxString buf( aTextValue.Strip( wxString::both ) );
408  unsigned brk_point = 0;
409 
410  while( brk_point < buf.Len() )
411  {
412  wxChar c = buf[brk_point];
413 
414  if( !( (c >= '0' && c <='9') || (c == '.') || (c == ',') || (c == '-') || (c == '+') ) )
415  break;
416 
417  ++brk_point;
418  }
419 
420  // Check the unit designator (2 ch significant)
421  wxString unit( buf.Mid( brk_point ).Strip( wxString::leading ).Left( 2 ).Lower() );
422 
423  if( unit == wxT( "in" ) || unit == wxT( "\"" ) )
424  {
425  aUnits = EDA_UNITS::INCHES;
426  aUseMils = false;
427  }
428  else if( unit == wxT( "mm" ) )
429  {
430  aUnits = EDA_UNITS::MILLIMETRES;
431  }
432  else if( unit == wxT( "mi" ) || unit == wxT( "th" ) ) // "mils" or "thou"
433  {
434  aUnits = EDA_UNITS::INCHES;
435  aUseMils = true;
436  }
437  else if( unit == wxT( "de" ) || unit == wxT( "ra" ) ) // "deg" or "rad"
438  {
439  aUnits = EDA_UNITS::DEGREES;
440  }
441 }

References DEGREES, INCHES, and MILLIMETRES.

Referenced by DIALOG_TEXT_PROPERTIES::OnDimensionTextChange(), and 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 552 of file base_units.cpp.

553 {
554  char temp[50];
555  int len;
556 
557  len = snprintf( temp, sizeof(temp), "%.10g", aAngle / 10.0 );
558 
559  return std::string( temp, len );
560 }

Referenced by DIALOG_PAD_PROPERTIES::displayPrimitivesList(), PCB_IO::format(), SCH_SEXPR_PLUGIN_CACHE::savePin(), 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 523 of file base_units.cpp.

524 {
525  char buf[50];
526  double engUnits = aValue;
527  int len;
528 
529  engUnits /= IU_PER_MM;
530 
531  if( engUnits != 0.0 && fabs( engUnits ) <= 0.0001 )
532  {
533  len = snprintf( buf, sizeof(buf), "%.10f", engUnits );
534 
535  while( --len > 0 && buf[len] == '0' )
536  buf[len] = '\0';
537 
538  if( buf[len] == '.' )
539  buf[len] = '\0';
540  else
541  ++len;
542  }
543  else
544  {
545  len = snprintf( buf, sizeof(buf), "%.10g", engUnits );
546  }
547 
548  return std::string( buf, len );
549 }

Referenced by BOOST_AUTO_TEST_CASE(), NETCLASS::Format(), PCB_IO::format(), EDA_TEXT::Format(), BOARD_STACKUP::FormatBoardStackup(), PCB_IO::formatDefaults(), PCB_IO::formatGeneral(), FormatInternalUnits(), PCB_IO::formatSetup(), formatStroke(), SCH_SEXPR_PLUGIN_CACHE::saveArc(), SCH_SEXPR_PLUGIN_CACHE::saveBezier(), SCH_SEXPR_PLUGIN::saveBusEntry(), SCH_SEXPR_PLUGIN_CACHE::saveCircle(), 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_CACHE::SaveSymbol(), SCH_SEXPR_PLUGIN::saveText(), and SCH_SEXPR_PLUGIN_CACHE::saveText().

◆ FormatInternalUnits() [2/4]

std::string FormatInternalUnits ( const wxPoint aPoint)

Definition at line 563 of file base_units.cpp.

564 {
565  return FormatInternalUnits( aPoint.x ) + " " + FormatInternalUnits( aPoint.y );
566 }
std::string FormatInternalUnits(int aValue)
Function FormatInternalUnits converts aValue from internal units to a string appropriate for writing ...
Definition: base_units.cpp:523

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

◆ FormatInternalUnits() [3/4]

std::string FormatInternalUnits ( const VECTOR2I aPoint)

Definition at line 569 of file base_units.cpp.

570 {
571  return FormatInternalUnits( aPoint.x ) + " " + FormatInternalUnits( aPoint.y );
572 }
std::string FormatInternalUnits(int aValue)
Function FormatInternalUnits converts aValue from internal units to a string appropriate for writing ...
Definition: base_units.cpp:523

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

◆ FormatInternalUnits() [4/4]

std::string FormatInternalUnits ( const wxSize &  aSize)

Definition at line 575 of file base_units.cpp.

576 {
577  return FormatInternalUnits( aSize.GetWidth() ) + " " + FormatInternalUnits( aSize.GetHeight() );
578 }
std::string FormatInternalUnits(int aValue)
Function FormatInternalUnits converts aValue from internal units to a string appropriate for writing ...
Definition: base_units.cpp:523

References FormatInternalUnits().

◆ From_User_Unit()

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

307 {
308  switch( aUnits )
309  {
311  return MM_TO_IU( aValue );
312 
313  case EDA_UNITS::INCHES:
314  if( aUseMils )
315  return MILS_TO_IU( aValue );
316  else
317  return IN_TO_IU( aValue );
318 
319  case EDA_UNITS::DEGREES:
320  // Convert to "decidegrees"
321  return aValue * 10;
322 
323  default:
324  case EDA_UNITS::UNSCALED:
325  case EDA_UNITS::PERCENT:
326  return aValue;
327  }
328 }

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

Referenced by BASE_SCREEN::AddGrid(), DoubleValueFromString(), and PCB_BASE_FRAME::LoadSettings().

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

468 {
469  switch( aUnit )
470  {
471  case EDA_UNITS::INCHES:
472  if( aUseMils )
473  {
474  switch( aType )
475  {
477  return _( "mils" );
478  case EDA_DATA_TYPE::AREA:
479  return _( "sq. mils" );
481  return _( "cu. mils" );
482  }
483  }
484  else
485  {
486  switch( aType )
487  {
489  return _( "in" );
490  case EDA_DATA_TYPE::AREA:
491  return _( "sq. in" );
493  return _( "cu. in" );
494  }
495  }
496 
498  switch( aType )
499  {
501  return _( "mm" );
502  case EDA_DATA_TYPE::AREA:
503  return _( "sq. mm" );
505  return _( "cu. mm" );
506  }
507 
508  case EDA_UNITS::PERCENT:
509  return _( "%" );
510 
511  case EDA_UNITS::UNSCALED:
512  return wxEmptyString;
513 
514  case EDA_UNITS::DEGREES:
515  return _( "deg" );
516 
517  default:
518  return wxT( "??" );
519  }
520 }
#define _(s)
Definition: 3d_actions.cpp:33

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

Referenced by DIMENSION::AdjustDimensionDetails(), 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::SetUnits(), UNIT_BINDER::SetValue(), TEXT_MOD_GRID_TABLE::TEXT_MOD_GRID_TABLE(), UNIT_BINDER::UNIT_BINDER(), and DIALOG_BOARD_STATISTICS::updateWidets().

◆ MessageTextFromValue() [1/3]

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

Definition at line 127 of file base_units.cpp.

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

References MessageTextFromValue().

Referenced by DIALOG_SELECT_NET_FROM_LIST::buildNetsList(), DIALOG_PAD_PROPERTIES::displayPrimitivesList(), formatCoord(), LIB_CIRCLE::GetMsgPanelInfo(), LIB_RECTANGLE::GetMsgPanelInfo(), LIB_TEXT::GetMsgPanelInfo(), LIB_ARC::GetMsgPanelInfo(), LIB_POLYLINE::GetMsgPanelInfo(), TEXTE_PCB::GetMsgPanelInfo(), LIB_BEZIER::GetMsgPanelInfo(), WS_DRAW_ITEM_BASE::GetMsgPanelInfo(), ZONE_CONTAINER::GetMsgPanelInfo(), LIB_PIN::GetMsgPanelInfo(), SCH_BITMAP::GetMsgPanelInfo(), LIB_FIELD::GetMsgPanelInfo(), TRACK::GetMsgPanelInfo(), TEXTE_MODULE::GetMsgPanelInfo(), DRAWSEGMENT::GetMsgPanelInfo(), NETINFO_ITEM::GetMsgPanelInfo(), SCH_TEXT::GetMsgPanelInfo(), D_PAD::GetMsgPanelInfo(), TRACK::GetMsgPanelInfoBase(), VIA::GetMsgPanelInfoBase(), 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(), DRAWSEGMENT::GetSelectMenuText(), WS_DRAW_ITEM_TEXT::GetSelectMenuText(), WS_DRAW_ITEM_BITMAP::GetSelectMenuText(), VIA::GetSelectMenuText(), MessageTextFromValue(), DIALOG_BOARD_STATISTICS::saveReportClicked(), RC_ITEM::ShowCoord(), PNS::MEANDER_SKEW_PLACER::TuningInfo(), PNS::MEANDER_PLACER::TuningInfo(), PNS::DP_MEANDER_PLACER::TuningInfo(), TRACK_WIDTH_MENU::update(), DIFF_PAIR_MENU::update(), DIALOG_BOARD_STATISTICS::updateDrillGrid(), and DIALOG_BOARD_STATISTICS::updateWidets().

◆ MessageTextFromValue() [2/3]

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

Definition at line 134 of file base_units.cpp.

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

References MessageTextFromValue().

◆ MessageTextFromValue() [3/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 142 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, aUseMils );
152  // Fall through to continue computation
153 
154  case EDA_DATA_TYPE::AREA:
155  value = To_User_Unit( aUnits, value, aUseMils );
156  // Fall through to continue computation
157 
159  value = To_User_Unit( aUnits, value, aUseMils );
160  }
161 
162  if( aUnits == EDA_UNITS::INCHES )
163  {
164  if( aUseMils )
165  {
166 #if defined( EESCHEMA )
167  format = wxT( "%.0f" );
168 #else
169  format = wxT( "%.1f" );
170 #endif
171  }
172  else
173  {
174 #if defined( EESCHEMA )
175  format = wxT( "%.3f" );
176 #else
177  format = wxT( "%.4f" );
178 #endif
179  }
180  }
181  else
182  {
183 #if defined( EESCHEMA )
184  format = wxT( "%.4f" );
185 #else
186  format = wxT( "%.3f" );
187 #endif
188  }
189 
190  text.Printf( format, value );
191  text += " ";
192 
193  text += GetAbbreviatedUnitsLabel( aUnits, aUseMils, aType );
194 
195  return text;
196 }
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:95
wxString GetAbbreviatedUnitsLabel(EDA_UNITS aUnit, bool aUseMils, EDA_DATA_TYPE aType)
Get the units string for a given units type.
Definition: base_units.cpp:467

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

◆ StringFromValue()

wxString StringFromValue ( EDA_UNITS  aUnit,
double  aValue,
bool  aAddUnitSymbol = false,
bool  aUseMils = false 
)

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

235 {
236  double value_to_print = To_User_Unit( aUnits, aValue, aUseMils );
237 
238 #if defined( EESCHEMA )
239  wxString stringValue = wxString::Format( wxT( "%.3f" ), value_to_print );
240 
241  // Strip trailing zeros. However, keep at least 3 digits in mantissa
242  // For readability
243  StripTrailingZeros( stringValue, 3 );
244 
245 #else
246 
247  char buf[50];
248  int len;
249 
250  if( value_to_print != 0.0 && fabs( value_to_print ) <= 0.0001 )
251  {
252  len = sprintf( buf, "%.10f", value_to_print );
253 
254  while( --len > 0 && buf[len] == '0' )
255  buf[len] = '\0';
256 
257  if( buf[len]=='.' || buf[len]==',' )
258  buf[len] = '\0';
259  else
260  ++len;
261  }
262  else
263  {
264  if( aUnits == EDA_UNITS::INCHES && aUseMils )
265  len = sprintf( buf, "%.7g", value_to_print );
266  else
267  len = sprintf( buf, "%.10g", value_to_print );
268  }
269 
270  wxString stringValue( buf, wxConvUTF8 );
271 
272 #endif
273 
274  if( aAddUnitSymbol )
275  {
276  switch( aUnits )
277  {
278  case EDA_UNITS::INCHES:
279  if( aUseMils )
280  stringValue += wxT( " mils" );
281  else
282  stringValue += wxT( " in" );
283  break;
284 
286  stringValue += wxT( " mm" );
287  break;
288 
289  case EDA_UNITS::DEGREES:
290  stringValue += wxT( " deg" );
291  break;
292 
293  case EDA_UNITS::PERCENT:
294  stringValue += wxT( "%" );
295  break;
296 
297  case EDA_UNITS::UNSCALED:
298  break;
299  }
300  }
301 
302  return stringValue;
303 }
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:95
void StripTrailingZeros(wxString &aStringValue, unsigned aTrailingZeroAllowed)
Function StripTrailingZeros Remove trailing 0 from a string containing a converted float number.
Definition: base_units.cpp:202
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:205

References DEGREES, Format(), INCHES, MILLIMETRES, PERCENT, StripTrailingZeros(), To_User_Unit(), and UNSCALED.

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(), PCB_EDIT_FRAME::Create_MuWaveComponent(), MWAVE::CreateMicrowaveInductor(), PANEL_SETUP_BOARD_STACKUP::createRowData(), DIALOG_TRACK_VIA_PROPERTIES::DIALOG_TRACK_VIA_PROPERTIES(), LIB_PIN::getMsgPanelInfoBase(), TEXT_MOD_GRID_TABLE::GetValue(), FIELDS_GRID_TABLE< SCH_FIELD >::GetValue(), PIN_TABLE_DATA_MODEL::GetValue(), DIALOG_PLOT::init_Dialog(), DIALOG_PAD_PROPERTIES::initValues(), PANEL_SETUP_BOARD_STACKUP::onCalculateDielectricThickness(), DIALOG_TEXT_PROPERTIES::OnDimensionUnitsChange(), PANEL_SETUP_BOARD_STACKUP::onUpdateThicknessValue(), DIALOG_PAD_PROPERTIES::padValuesOK(), UNIT_BINDER::SetDoubleValue(), UNIT_BINDER::SetValue(), EDA_POSITION_CTRL::SetValue(), PANEL_SETUP_BOARD_STACKUP::synchronizeWithBoard(), PANEL_SETUP_BOARD_STACKUP::transferDataFromUIToStackup(), PANEL_LIBEDIT_SETTINGS::TransferDataToWindow(), PANEL_EESCHEMA_SETTINGS::TransferDataToWindow(), DIALOG_PAD_PRIMITIVE_POLY_PROPS::TransferDataToWindow(), UNIT_BINDER::Validate(), PANEL_SETUP_TRACKS_AND_VIAS::validateData(), and PANEL_SETUP_NETCLASSES::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 202 of file base_units.cpp.

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

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

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

References DEGREES, INCHES, and MILLIMETRES.

Referenced by DIMENSION::AdjustDimensionDetails(), BASE_SCREEN::BuildGridsChoiceList(), EDA_DRAW_FRAME::DisplayGridMsg(), GERBVIEW_FRAME::DisplayGridMsg(), PCB_BASE_FRAME::DisplayGridMsg(), formatPreviewDimension(), MessageTextFromValue(), PCB_BASE_FRAME::SaveSettings(), StringFromValue(), 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 
)

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

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

445 {
446  double value = DoubleValueFromString( aUnits, aTextValue, aUseMils );
447  return KiROUND<double, long long int>( value );
448 }
double DoubleValueFromString(EDA_UNITS aUnits, const wxString &aTextValue, bool aUseMils)
Function DoubleValueFromString converts aTextValue to a double.
Definition: base_units.cpp:331

References DoubleValueFromString().

Referenced by PIN_TABLE_DATA_MODEL::compare(), PCB_EDIT_FRAME::Create_MuWaveComponent(), MWAVE::CreateMicrowaveInductor(), PANEL_MODEDIT_DEFAULTS::getGridValue(), PANEL_SETUP_TEXT_AND_GRAPHICS::getGridValue(), PANEL_SETUP_NETCLASSES::getNetclassValue(), PANEL_SETUP_BOARD_STACKUP::GetPcbThickness(), DIALOG_ANNOTATE::GetStartNumber(), UNIT_BINDER::GetValue(), EDA_POSITION_CTRL::GetValue(), DIALOG_PAD_PRIMITIVE_POLY_PROPS::onCellChanging(), DIALOG_TEXT_PROPERTIES::OnDimensionUnitsChange(), PANEL_SETUP_BOARD_STACKUP::onThicknessChange(), PANEL_SETUP_BOARD_STACKUP::onUpdateThicknessValue(), TEXT_MOD_GRID_TABLE::SetValue(), FIELDS_GRID_TABLE< SCH_FIELD >::SetValue(), PIN_TABLE_DATA_MODEL::SetValue(), PANEL_SETUP_BOARD_STACKUP::transferDataFromUIToStackup(), PANEL_EESCHEMA_SETTINGS::TransferDataFromWindow(), PANEL_LIBEDIT_SETTINGS::TransferDataFromWindow(), PANEL_SETUP_TRACKS_AND_VIAS::TransferDataFromWindow(), and PANEL_SETUP_TRACKS_AND_VIAS::validateData().