KiCad PCB EDA Suite
base_units.h File Reference

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

#include <common.h>
#include <convert_to_biu.h>

Go to the source code of this file.

Functions

int Mm2mils (double x)
 Convert mm to mils. More...
 
int Mils2mm (double x)
 Convert mils to mm. More...
 
std::string Double2Str (double aValue)
 Helper function Double2Str to print a float number without using scientific notation and no trailing 0 We want to avoid scientific notation in S-expr files (not easy to read) for floating numbers. More...
 
void StripTrailingZeros (wxString &aStringValue, unsigned aTrailingZeroAllowed=1)
 Function StripTrailingZeros Remove trailing 0 from a string containing a converted float number. More...
 
double To_User_Unit (EDA_UNITS_T aUnit, double aValue)
 Function To_User_Unit convert aValue in internal units to the appropriate user units defined by aUnit. More...
 
wxString CoordinateToString (int aValue, bool aConvertToMils=false)
 Function CoordinateToString is a helper to convert the integer coordinate aValue to a string in inches, millimeters, or unscaled units according to the current user units setting. 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 LengthDoubleToString (double aValue, bool aConvertToMils=false)
 Function LengthDoubleToString is a helper to convert the double length aValue to a string in inches, millimeters, or unscaled units according to the current user units setting. More...
 
wxString StringFromValue (EDA_UNITS_T aUnit, int aValue, bool aAddUnitSymbol=false)
 Function StringFromValue returns the string from aValue according to units (inch, mm ...) for display, and the initial unit for value. More...
 
wxString & operator<< (wxString &aString, const wxPoint &aPoint)
 Operator << overload outputs a point to the argument string in a format resembling "@ (x,y) More...
 
void PutValueInLocalUnits (wxTextCtrl &aTextCtr, int aValue)
 Function PutValueInLocalUnits converts aValue from internal units to user units and append the units notation (mm or ")then inserts the string an aTextCtrl. More...
 
double From_User_Unit (EDA_UNITS_T 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_T aUnits, const wxString &aTextValue)
 Function DoubleValueFromString converts aTextValue to a double. More...
 
int ValueFromString (EDA_UNITS_T aUnits, const wxString &aTextValue)
 Function ValueFromString converts aTextValue in aUnits to internal units used by the application. More...
 
int ValueFromString (const wxString &aTextValue)
 Function ValueFromString. More...
 
int ValueFromTextCtrl (const wxTextCtrl &aTextCtr)
 Convert the number Value in a string according to the internal units and the selected unit (g_UserUnit) and put it in the wxTextCtrl TextCtrl. More...
 
wxString ReturnUnitSymbol (EDA_UNITS_T aUnits=g_UserUnit, const wxString &aFormatString=" (%s):")
 Returns the units symbol. More...
 
wxString GetUnitsLabel (EDA_UNITS_T aUnits)
 Get a human readable units string. More...
 
wxString GetAbbreviatedUnitsLabel (EDA_UNITS_T aUnit=g_UserUnit)
 
void AddUnitSymbol (wxStaticText &Stext, EDA_UNITS_T aUnit=g_UserUnit)
 

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.

Function Documentation

void AddUnitSymbol ( wxStaticText &  Stext,
EDA_UNITS_T  aUnit = g_UserUnit 
)
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 414 of file base_units.cpp.

References StripTrailingZeros().

Referenced by DIALOG_GENERALOPTIONS::init().

415 {
416  wxString text;
417 
418  text.Printf( wxT( "%.3f" ), aAngle/10.0 );
419  StripTrailingZeros( text, 1 );
420 
421  return text;
422 }
void StripTrailingZeros(wxString &aStringValue, unsigned aTrailingZeroAllowed)
Function StripTrailingZeros Remove trailing 0 from a string containing a converted float number...
Definition: base_units.cpp:172
wxString CoordinateToString ( int  aValue,
bool  aConvertToMils = false 
)

Function CoordinateToString is a helper to convert the integer coordinate aValue to a string in inches, millimeters, or unscaled units according to the current user units setting.

Should be used only to display a coordinate in status, but not in dialogs, because the mantissa of the number displayed has 4 digits max for readability. (i.e. the value shows the decimils or the microns ) However the actual internal value could need up to 8 digits to be printed

Parameters
aValueThe integer coordinate to convert.
aConvertToMilsConvert inch values to mils if true. This setting has no effect if the current user unit is millimeters.
Returns
The converted string for display in user interface elements.

Definition at line 118 of file base_units.cpp.

References LengthDoubleToString().

Referenced by DIMENSION::AdjustDimensionDetails(), EDA_DRAW_FRAME::CoordinateToString(), DIALOG_PAD_PROPERTIES::displayPrimitivesList(), formatCoord(), TEXTE_PCB::GetMsgPanelInfo(), DRAWSEGMENT::GetMsgPanelInfo(), TEXTE_MODULE::GetMsgPanelInfo(), TRACK::GetMsgPanelInfo(), D_PAD::GetMsgPanelInfo(), TRACK::GetMsgPanelInfoBase(), SEGZONE::GetMsgPanelInfoBase(), VIA::GetMsgPanelInfoBase(), getOptimalModulePlacement(), PCB_TARGET::GetSelectMenuText(), LIB_CIRCLE::GetSelectMenuText(), LIB_RECTANGLE::GetSelectMenuText(), LIB_POLYLINE::GetSelectMenuText(), SCH_LINE::GetSelectMenuText(), LIB_ARC::GetSelectMenuText(), DIALOG_NETLIST::OnTestFootprintsClick(), operator<<(), and TRACK::ShowWidth().

119 {
120  return LengthDoubleToString( (double) aValue, aConvertToMils );
121 }
wxString LengthDoubleToString(double aValue, bool aConvertToMils)
Function LengthDoubleToString is a helper to convert the double length aValue to a string in inches...
Definition: base_units.cpp:123
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.

Referenced by GBR_TO_PCB_EXPORTER::export_flashed_copper_item(), PCB_IO::format(), PCB_IO::formatSetup(), GBR_TO_PCB_EXPORTER::writeCopperLineItem(), and GBR_TO_PCB_EXPORTER::writePcbLineItem().

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 }
double DoubleValueFromString ( EDA_UNITS_T  aUnits,
const wxString &  aTextValue 
)

Function DoubleValueFromString converts aTextValue to a double.

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

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

Referenced by DIALOG_CREATE_ARRAY::calculateCircularArrayProperties(), PROPERTIES_FRAME::CopyPrmsFromPanelToGeneral(), PROPERTIES_FRAME::CopyPrmsFromPanelToItem(), DIALOG_SET_GRID::getGridOrigin(), DIALOG_DXF_IMPORT::getPCBdefaultLineWidthMM(), DIALOG_DXF_IMPORT::getPcbImportOffsets(), DIALOG_POSITION_RELATIVE::GetTranslationInIU(), DIALOG_MOVE_EXACT::GetTranslationInIU(), DIALOG_EXPORT_STEP::GetXOrg(), DIALOG_EXPORT_IDF3::GetXRef(), DIALOG_EXPORT_3DFILE::GetXRef(), DIALOG_EXPORT_STEP::GetYOrg(), DIALOG_EXPORT_IDF3::GetYRef(), DIALOG_EXPORT_3DFILE::GetYRef(), DIALOG_POSITION_RELATIVE::OnOkClick(), DIALOG_MOVE_EXACT::OnOkClick(), DIALOG_CREATE_ARRAY::OnOkClick(), DIALOG_PAD_PRIMITIVES_TRANSFORM::Transform(), and ValueFromString().

303 {
304  double value;
305  double dtmp = 0;
306 
307  // Acquire the 'right' decimal point separator
308  const struct lconv* lc = localeconv();
309 
310  wxChar decimal_point = lc->decimal_point[0];
311  wxString buf( aTextValue.Strip( wxString::both ) );
312 
313  // Convert the period in decimal point
314  buf.Replace( wxT( "." ), wxString( decimal_point, 1 ) );
315 
316  // Find the end of the numeric part
317  unsigned brk_point = 0;
318 
319  while( brk_point < buf.Len() )
320  {
321  wxChar ch = buf[brk_point];
322 
323  if( !( (ch >= '0' && ch <='9') || (ch == decimal_point) || (ch == '-') || (ch == '+') ) )
324  {
325  break;
326  }
327 
328  ++brk_point;
329  }
330 
331  // Extract the numeric part
332  buf.Left( brk_point );
333 
334  buf.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 == INCHES || aUnits == MILLIMETRES )
340  {
341  if( unit == wxT( "in" ) || unit == wxT( "\"" ) )
342  {
343  aUnits = INCHES;
344  }
345  else if( unit == wxT( "mm" ) )
346  {
347  aUnits = MILLIMETRES;
348  }
349  else if( unit == wxT( "mi" ) || unit == wxT( "th" ) ) // Mils or thous
350  {
351  aUnits = INCHES;
352  dtmp /= 1000;
353  }
354  }
355  else if( aUnits == DEGREES )
356  {
357  if( unit == wxT( "ra" ) ) // Radians
358  {
359  dtmp *= 180.0f / M_PI;
360  }
361  }
362 
363  value = From_User_Unit( aUnits, dtmp );
364 
365  return value;
366 }
Definition: common.h:144
double From_User_Unit(EDA_UNITS_T aUnit, double aValue)
Return in internal units the value "val" given in a real unit such as "in", "mm" or "deg"...
Definition: base_units.cpp:274
double From_User_Unit ( EDA_UNITS_T  aUnit,
double  aValue 
)

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

Definition at line 274 of file base_units.cpp.

References DEGREES, INCHES, MILLIMETRES, and UNSCALED_UNITS.

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

275 {
276  double value;
277 
278  switch( aUnit )
279  {
280  case MILLIMETRES:
281  value = MM_TO_IU( aValue );
282  break;
283 
284  case INCHES:
285  value = IN_TO_IU( aValue );
286  break;
287 
288  case DEGREES:
289  // Convert to "decidegrees"
290  value = aValue * 10;
291  break;
292 
293  default:
294  case UNSCALED_UNITS:
295  value = aValue;
296  }
297 
298  return value;
299 }
Definition: common.h:144
wxString GetAbbreviatedUnitsLabel ( EDA_UNITS_T  aUnit = g_UserUnit)

Definition at line 484 of file base_units.cpp.

References DEGREES, INCHES, MILLIMETRES, and UNSCALED_UNITS.

Referenced by DIALOG_CREATE_ARRAY::DIALOG_CREATE_ARRAY(), DIALOG_EDIT_COMPONENT_IN_SCHEMATIC::DIALOG_EDIT_COMPONENT_IN_SCHEMATIC(), DIALOG_MOVE_EXACT::DIALOG_MOVE_EXACT(), DIALOG_PAD_PRIMITIVES_TRANSFORM::DIALOG_PAD_PRIMITIVES_TRANSFORM(), DIALOG_POSITION_RELATIVE::DIALOG_POSITION_RELATIVE(), DIALOG_TRACK_VIA_PROPERTIES::DIALOG_TRACK_VIA_PROPERTIES(), DIALOG_DRC_CONTROL::DisplayDRCValues(), SCH_EDIT_FRAME::EditLine(), DIALOG_MODULE_BOARD_EDITOR::InitBoardProperties(), DIALOG_GLOBAL_MODULES_FIELDS_EDITION::initDialog(), DIALOG_DESIGN_RULES::InitGlobalRules(), DIALOG_MODULE_MODULE_EDITOR::initModeditProperties(), DIALOG_MODEDIT_OPTIONS::initValues(), DIALOG_PAD_PROPERTIES::initValues(), SCH_EDIT_FRAME::OnFindDrcMarker(), DIALOG_EDIT_LIBENTRY_FIELDS_IN_LIB::OnInitDialog(), UNIT_BINDER::SetValue(), DIALOG_MODEDIT_FP_BODY_ITEM_PROPERTIES::TransferDataToWindow(), DIALOG_GRAPHIC_ITEM_PROPERTIES::TransferDataToWindow(), DIALOG_LAYERS_SETUP::TransferDataToWindow(), DIALOG_PAD_PRIMITIVES_PROPERTIES::TransferDataToWindow(), DIALOG_PAD_PRIMITIVE_POLY_PROPS::TransferDataToWindow(), UNIT_BINDER::UNIT_BINDER(), DIALOG_POSITION_RELATIVE::updateDlgTexts(), and DIALOG_MOVE_EXACT::updateDlgTexts().

485 {
486  wxString label;
487 
488  switch( aUnit )
489  {
490  case INCHES:
491  label = _( "in" );
492  break;
493 
494  case MILLIMETRES:
495  label = _( "mm" );
496  break;
497 
498  case UNSCALED_UNITS:
499  break;
500 
501  case DEGREES:
502  label = _( "deg" );
503  break;
504 
505  default:
506  label = wxT( "??" );
507  break;
508  }
509 
510  return label;
511 }
Definition: common.h:144
wxString GetUnitsLabel ( EDA_UNITS_T  aUnits)

Get a human readable units string.

The strings returned are full text name and not abbreviations or symbolic representations of the units. Use ReturnUnitSymbol() for that.

Parameters
aUnits- The units text to return.
Returns
The human readable units string.

Definition at line 457 of file base_units.cpp.

References DEGREES, INCHES, MILLIMETRES, and UNSCALED_UNITS.

Referenced by DIALOG_SET_GRID::DIALOG_SET_GRID(), SCH_EDIT_FRAME::EditSheet(), SCH_EDIT_FRAME::EditSheetPin(), DIALOG_PADS_MASK_CLEARANCE::myInit(), LIB_EDIT_FRAME::OnEditPin(), SCH_EDIT_FRAME::OnPreferencesOptions(), and TARGET_PROPERTIES_DIALOG_EDITOR::TARGET_PROPERTIES_DIALOG_EDITOR().

458 {
459  wxString label;
460 
461  switch( aUnit )
462  {
463  case INCHES:
464  label = _( "inches" );
465  break;
466 
467  case MILLIMETRES:
468  label = _( "millimeters" );
469  break;
470 
471  case UNSCALED_UNITS:
472  label = _( "units" );
473  break;
474 
475  case DEGREES:
476  label = _( "degrees" );
477  break;
478  }
479 
480  return label;
481 }
Definition: common.h:144
wxString LengthDoubleToString ( double  aValue,
bool  aConvertToMils = false 
)

Function LengthDoubleToString is a helper to convert the double length aValue to a string in inches, millimeters, or unscaled units according to the current user units setting.

Should be used only to display a coordinate in status, but not in dialogs, because the mantissa of the number displayed has 4 digits max for readability. (i.e. the value shows the decimils or the microns ) However the actual internal value could need up to 8 digits to be printed

Parameters
aValueThe double value to convert.
aConvertToMilsConvert inch values to mils if true. This setting has no effect if the current user unit is millimeters.
Returns
The converted string for display in user interface elements.

Definition at line 123 of file base_units.cpp.

References g_UserUnit, INCHES, and To_User_Unit().

Referenced by CoordinateToString(), TRACK::GetMsgPanelInfo(), NETINFO_ITEM::GetMsgPanelInfo(), TRACK::GetMsgPanelInfoBase(), SEGZONE::GetMsgPanelInfoBase(), DRAWSEGMENT::GetSelectMenuText(), TRACK::GetSelectMenuText(), EDA_DRAW_FRAME::LengthDoubleToString(), PNS::MEANDER_SKEW_PLACER::TuningInfo(), PNS::MEANDER_PLACER::TuningInfo(), and PNS::DP_MEANDER_PLACER::TuningInfo().

124 {
125  wxString text;
126  const wxChar* format;
127  double value = To_User_Unit( g_UserUnit, aValue );
128 
129  if( g_UserUnit == INCHES )
130  {
131  if( aConvertToMils )
132  {
133 #if defined( EESCHEMA )
134  format = wxT( "%.0f" );
135 #else
136  format = wxT( "%.1f" );
137 #endif
138  value *= 1000;
139  }
140  else
141  {
142 #if defined( EESCHEMA )
143  format = wxT( "%.3f" );
144 #else
145  format = wxT( "%.4f" );
146 #endif
147  }
148  }
149  else
150  {
151 #if defined( EESCHEMA )
152  format = wxT( "%.2f" );
153 #else
154  format = wxT( "%.3f" );
155 #endif
156  }
157 
158  text.Printf( format, value );
159 
160  if( g_UserUnit == INCHES )
161  text += ( aConvertToMils ) ? _( " mils" ) : _( " in" );
162  else
163  text += _( " mm" );
164 
165  return text;
166 }
Definition: common.h:144
EDA_UNITS_T g_UserUnit
Global variables definitions.
Definition: common.cpp:56
double To_User_Unit(EDA_UNITS_T 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
int Mils2mm ( double  x)
inline

Convert mils to mm.

Definition at line 44 of file base_units.h.

References KiROUND().

Referenced by DIALOG_PRINT_USING_PRINTER::initDialog(), PCB_EDIT_FRAME::ToPrinter(), and PL_EDITOR_FRAME::ToPrinter().

44 { return KiROUND( x * 25.4 / 1000. ); }
static int KiROUND(double v)
KiROUND rounds a floating point number to an int using "round halfway cases away from zero"...
Definition: common.h:106
int Mm2mils ( double  x)
inline

Convert mm to mils.

Definition at line 41 of file base_units.h.

References KiROUND().

Referenced by WS_DRAW_ITEM_LIST::BuildWorkSheetGraphicList(), and PCB_PARSER::parsePAGE_INFO().

41 { return KiROUND( x * 1000./25.4 ); }
static int KiROUND(double v)
KiROUND rounds a floating point number to an int using "round halfway cases away from zero"...
Definition: common.h:106
wxString& operator<< ( wxString &  aString,
const wxPoint aPoint 
)

Operator << overload outputs a point to the argument string in a format resembling "@ (x,y)

Parameters
aStringWhere to put the text describing the point value
aPointThe point to output.
Returns
wxString& - the input string

Definition at line 400 of file base_units.cpp.

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

401 {
402  aString << wxT( "@ (" ) << CoordinateToString( aPos.x );
403  aString << wxT( "," ) << CoordinateToString( aPos.y );
404  aString << wxT( ")" );
405 
406  return aString;
407 }
wxString CoordinateToString(int aValue, bool aConvertToMils)
Function CoordinateToString is a helper to convert the integer coordinate aValue to a string in inche...
Definition: base_units.cpp:118
void PutValueInLocalUnits ( wxTextCtrl &  aTextCtr,
int  aValue 
)

Function PutValueInLocalUnits converts aValue from internal units to user units and append the units notation (mm or ")then inserts the string an aTextCtrl.

This function is used in dialog boxes for entering values depending on selected units.

Definition at line 266 of file base_units.cpp.

References g_UserUnit, and StringFromValue().

Referenced by DIALOG_DIMENSION_EDITOR::DIALOG_DIMENSION_EDITOR(), DIALOG_DRC_CONTROL::DisplayDRCValues(), DIALOG_MODULE_BOARD_EDITOR::InitBoardProperties(), DIALOG_COPPER_ZONE::initDialog(), DIALOG_PLOT_SCHEMATIC::initDlg(), DIALOG_DESIGN_RULES::InitGlobalRules(), DIALOG_MODULE_MODULE_EDITOR::initModeditProperties(), DIALOG_GRAPHIC_ITEMS_OPTIONS::initValues(), DIALOG_MODEDIT_OPTIONS::initValues(), DIALOG_PAD_PROPERTIES::initValues(), DIALOG_PADS_MASK_CLEARANCE::myInit(), DIALOG_GRAPHIC_ITEM_PROPERTIES::OnLayerChoice(), DIALOG_POSITION_RELATIVE::OnPolarChanged(), DIALOG_MOVE_EXACT::OnPolarChanged(), DIALOG_SET_GRID::setGridOrigin(), DialogEditModuleText::TransferDataToWindow(), DIALOG_PCB_TEXT_PROPERTIES::TransferDataToWindow(), DIALOG_MODEDIT_FP_BODY_ITEM_PROPERTIES::TransferDataToWindow(), DIALOG_GRAPHIC_ITEM_PROPERTIES::TransferDataToWindow(), DIALOG_LAYERS_SETUP::TransferDataToWindow(), DIALOG_PAD_PRIMITIVES_PROPERTIES::TransferDataToWindow(), DIALOG_PAD_PRIMITIVE_POLY_PROPS::TransferDataToWindow(), and DIALOG_POSITION_RELATIVE::UpdateAnchor().

267 {
268  wxString msg = StringFromValue( g_UserUnit, aValue );
269 
270  aTextCtr.SetValue( msg );
271 }
wxString StringFromValue(EDA_UNITS_T aUnit, int aValue, bool aAddUnitSymbol)
Function StringFromValue returns the string from aValue according to units (inch, mm ...
Definition: base_units.cpp:204
EDA_UNITS_T g_UserUnit
Global variables definitions.
Definition: common.cpp:56
wxString ReturnUnitSymbol ( EDA_UNITS_T  aUnits = g_UserUnit,
const wxString &  aFormatString = " (%s):" 
)

Returns the units symbol.

Parameters
aUnits- Units type, default is current units setting.
aFormatString- A formatting string to embed the units symbol into. Note: the format string must contain the s format specifier.
Returns
The formatted units symbol.

Definition at line 425 of file base_units.cpp.

References DEGREES, GetChars(), INCHES, MILLIMETRES, and UNSCALED_UNITS.

Referenced by AddUnitSymbol(), EDA_GRAPHIC_TEXT_CTRL::EDA_GRAPHIC_TEXT_CTRL(), EDA_POSITION_CTRL::EDA_POSITION_CTRL(), EDA_VALUE_CTRL::EDA_VALUE_CTRL(), LIB_EDIT_FRAME::EditGraphicSymbol(), DIALOG_EDIT_ONE_FIELD::init(), DIALOG_LABEL_EDITOR::InitDialog(), DIALOG_LIB_EDIT_TEXT::initDlg(), and DIALOG_DESIGN_RULES::TestDataValidity().

426 {
427  wxString tmp;
428  wxString label;
429 
430  switch( aUnit )
431  {
432  case INCHES:
433  tmp = _( "\"" );
434  break;
435 
436  case MILLIMETRES:
437  tmp = _( "mm" );
438  break;
439 
440  case UNSCALED_UNITS:
441  break;
442 
443  case DEGREES:
444  wxASSERT( false );
445  break;
446  }
447 
448  if( formatString.IsEmpty() )
449  return tmp;
450 
451  label.Printf( formatString, GetChars( tmp ) );
452 
453  return label;
454 }
Definition: common.h:144
static const wxChar * GetChars(const wxString &s)
Function GetChars returns a wxChar* to the actual wxChar* data within a wxString, and is helpful for ...
Definition: macros.h:92
wxString StringFromValue ( EDA_UNITS_T  aUnit,
int  aValue,
bool  aAddUnitSymbol = 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
Returns
A wxString object containing value and optionally the symbol unit (like 2.000 mm)

Definition at line 204 of file base_units.cpp.

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

Referenced by Append_Track_Width_List(), TRACK_VIA_SIZE_MENU::AppendSizes(), DIALOG_PLOT::applyPlotSettings(), DIALOG_CREATE_ARRAY::calculateCircularArrayProperties(), class2gridRow(), DIALOG_EDIT_COMPONENT_IN_SCHEMATIC::copySelectedFieldToPanel(), DIALOG_EDIT_LIBENTRY_FIELDS_IN_LIB::copySelectedFieldToPanel(), PCB_EDIT_FRAME::Create_MuWaveComponent(), MWAVE::CreateMicrowaveInductor(), DIALOG_TRACK_VIA_PROPERTIES::DIALOG_TRACK_VIA_PROPERTIES(), EDA_VALUE_CTRL::EDA_VALUE_CTRL(), PCB_EDIT_FRAME::Edit_Gap(), LIB_EDIT_FRAME::EditGraphicSymbol(), SCH_EDIT_FRAME::EditLine(), SCH_EDIT_FRAME::EditSheet(), SCH_EDIT_FRAME::EditSheetPin(), FOOTPRINT_EDIT_FRAME::Enter_Edge_Width(), EDA_GRAPHIC_TEXT_CTRL::FormatSize(), LIB_CIRCLE::GetMsgPanelInfo(), LIB_RECTANGLE::GetMsgPanelInfo(), LIB_POLYLINE::GetMsgPanelInfo(), LIB_TEXT::GetMsgPanelInfo(), LIB_BEZIER::GetMsgPanelInfo(), LIB_ARC::GetMsgPanelInfo(), LIB_PIN::GetMsgPanelInfo(), LIB_FIELD::GetMsgPanelInfo(), SCH_TEXT::GetMsgPanelInfo(), LIB_PIN::getMsgPanelInfoBase(), DIALOG_NON_COPPER_ZONES_EDITOR::Init(), DIALOG_PLOT::init_Dialog(), DIALOG_SVG_PRINT::initDialog(), DIALOG_GLOBAL_MODULES_FIELDS_EDITION::initDialog(), DIALOG_LABEL_EDITOR::InitDialog(), DIALOG_COPPER_ZONE::initDialog(), DIALOG_DESIGN_RULES::InitDimensionsLists(), DIALOG_LIB_EDIT_TEXT::initDlg(), DIALOG_PRINT_USING_PRINTER::initValues(), DIALOG_PAD_PROPERTIES::onCornerSizePercentChange(), LIB_EDIT_FRAME::OnEditPin(), DIALOG_TRACK_VIA_PROPERTIES::onViaSelect(), PutValueInLocalUnits(), DIALOG_SVG_PRINT::SetPenWidth(), DIALOG_PRINT_USING_PRINTER::SetPenWidth(), UNIT_BINDER::SetValue(), EDA_POSITION_CTRL::SetValue(), EDA_VALUE_CTRL::SetValue(), TARGET_PROPERTIES_DIALOG_EDITOR::TARGET_PROPERTIES_DIALOG_EDITOR(), DIALOG_LAYERS_SETUP::TransferDataFromWindow(), DIALOG_EDIT_ONE_FIELD::TransferDataToWindow(), DIALOG_PAD_PRIMITIVE_POLY_PROPS::TransferDataToWindow(), DIALOG_GLOBAL_EDIT_TRACKS_AND_VIAS::updateNetInfo(), and DIALOG_PAD_PROPERTIES::updateRoundRectCornerValues().

205 {
206  double value_to_print = To_User_Unit( aUnit, aValue );
207 
208 #if defined( EESCHEMA )
209  wxString stringValue = wxString::Format( wxT( "%.3f" ), value_to_print );
210 
211  // Strip trailing zeros. However, keep at least 3 digits in mantissa
212  // For readability
213  StripTrailingZeros( stringValue, 3 );
214 
215 #else
216 
217  char buf[50];
218  int len;
219 
220  if( value_to_print != 0.0 && fabs( value_to_print ) <= 0.0001 )
221  {
222  len = sprintf( buf, "%.10f", value_to_print );
223 
224  while( --len > 0 && buf[len] == '0' )
225  buf[len] = '\0';
226 
227  if( buf[len]=='.' || buf[len]==',' )
228  buf[len] = '\0';
229  else
230  ++len;
231  }
232  else
233  {
234  len = sprintf( buf, "%.10g", value_to_print );
235  }
236 
237  wxString stringValue( buf, wxConvUTF8 );
238 
239 #endif
240 
241  if( aAddUnitSymbol )
242  {
243  switch( aUnit )
244  {
245  case INCHES:
246  stringValue += _( " \"" );
247  break;
248 
249  case MILLIMETRES:
250  stringValue += _( " mm" );
251  break;
252 
253  case DEGREES:
254  stringValue += _( " deg" );
255  break;
256 
257  case UNSCALED_UNITS:
258  break;
259  }
260  }
261 
262  return stringValue;
263 }
void StripTrailingZeros(wxString &aStringValue, unsigned aTrailingZeroAllowed)
Function StripTrailingZeros Remove trailing 0 from a string containing a converted float number...
Definition: base_units.cpp:172
Definition: common.h:144
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
double To_User_Unit(EDA_UNITS_T 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
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 172 of file base_units.cpp.

Referenced by AngleToStringDegrees(), and StringFromValue().

173 {
174  struct lconv * lc = localeconv();
175  char sep = lc->decimal_point[0];
176  unsigned sep_pos = aStringValue.Find( sep );
177 
178  if( sep_pos > 0 )
179  {
180  // We want to keep at least aTrailingZeroAllowed digits after the separator
181  unsigned min_len = sep_pos + aTrailingZeroAllowed + 1;
182 
183  while( aStringValue.Len() > min_len )
184  {
185  if( aStringValue.Last() == '0' )
186  aStringValue.RemoveLast();
187  else
188  break;
189  }
190  }
191 }
double To_User_Unit ( EDA_UNITS_T  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.

References DEGREES, INCHES, and MILLIMETRES.

Referenced by DIALOG_PLOT::applyPlotSettings(), BASE_SCREEN::BuildGridsChoiceList(), DIALOG_LABEL_EDITOR::DIALOG_LABEL_EDITOR(), formatPreviewDimension(), DIALOG_PLOT::init_Dialog(), LengthDoubleToString(), SCH_EDIT_FRAME::OnFindDrcMarker(), DIALOG_DESIGN_RULES::SetDataValidators(), StringFromValue(), DIALOG_DESIGN_RULES::TestDataValidity(), PL_EDITOR_FRAME::UpdateStatusBar(), SCH_BASE_FRAME::UpdateStatusBar(), PCB_BASE_FRAME::UpdateStatusBar(), GERBVIEW_FRAME::UpdateStatusBar(), PCB_EDIT_FRAME::updateTraceWidthSelectBox(), and PCB_EDIT_FRAME::updateViaSizeSelectBox().

92 {
93  switch( aUnit )
94  {
95  case MILLIMETRES:
96  return IU_TO_MM( aValue );
97 
98  case INCHES:
99  return IU_TO_IN( aValue );
100 
101  case DEGREES:
102  return aValue / 10.0f;
103 
104  default:
105  return aValue;
106  }
107 }
Definition: common.h:144
int ValueFromString ( EDA_UNITS_T  aUnits,
const wxString &  aTextValue 
)

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.
Returns
The string from Value, according to units (inch, mm ...) for display,

Definition at line 369 of file base_units.cpp.

References DoubleValueFromString(), and KiROUND().

Referenced by DIALOG_COPPER_ZONE::AcceptOptions(), DIALOG_PLOT::applyPlotSettings(), DIALOG_DESIGN_RULES::CopyDimensionsListsToBoard(), DIALOG_EDIT_COMPONENT_IN_SCHEMATIC::copyPanelToSelectedField(), DIALOG_EDIT_LIBENTRY_FIELDS_IN_LIB::copyPanelToSelectedField(), PCB_EDIT_FRAME::Create_MuWaveComponent(), MWAVE::CreateMicrowaveInductor(), PCB_EDIT_FRAME::Edit_Gap(), LIB_EDIT_FRAME::EditGraphicSymbol(), SCH_EDIT_FRAME::EditLine(), SCH_EDIT_FRAME::EditSheet(), SCH_EDIT_FRAME::EditSheetPin(), FOOTPRINT_EDIT_FRAME::Enter_Edge_Width(), UNIT_BINDER::GetValue(), EDA_POSITION_CTRL::GetValue(), EDA_VALUE_CTRL::GetValue(), DIALOG_PAD_PRIMITIVE_POLY_PROPS::onCellChanging(), LIB_EDIT_FRAME::OnEditPin(), DIALOG_LIB_EDIT_TEXT::OnOkClick(), DIALOG_NON_COPPER_ZONES_EDITOR::OnOkClick(), TARGET_PROPERTIES_DIALOG_EDITOR::OnOkClick(), DIALOG_DIMENSION_EDITOR::OnOKClick(), DIALOG_LIB_EDIT_PIN::OnPropertiesChange(), EDA_GRAPHIC_TEXT_CTRL::ParseSize(), DIALOG_DESIGN_RULES::TestDataValidity(), DIALOG_LABEL_EDITOR::TextPropertiesAccept(), DialogEditModuleText::TransferDataFromWindow(), DIALOG_EDIT_ONE_FIELD::TransferDataFromWindow(), DIALOG_PCB_TEXT_PROPERTIES::TransferDataFromWindow(), DIALOG_MODEDIT_FP_BODY_ITEM_PROPERTIES::TransferDataFromWindow(), DIALOG_GRAPHIC_ITEM_PROPERTIES::TransferDataFromWindow(), DIALOG_PAD_PRIMITIVES_PROPERTIES::TransferDataFromWindow(), DIALOG_PAD_PRIMITIVE_POLY_PROPS::TransferDataFromWindow(), DIALOG_PAD_PRIMITIVES_TRANSFORM::Transform(), DIALOG_MODEDIT_FP_BODY_ITEM_PROPERTIES::Validate(), DIALOG_GRAPHIC_ITEM_PROPERTIES::Validate(), ValueFromString(), and ValueFromTextCtrl().

370 {
371  double value = DoubleValueFromString( aUnits, aTextValue );
372  return KiROUND( value );
373 }
static int KiROUND(double v)
KiROUND rounds a floating point number to an int using "round halfway cases away from zero"...
Definition: common.h:106
double DoubleValueFromString(EDA_UNITS_T aUnits, const wxString &aTextValue)
Function DoubleValueFromString converts aTextValue to a double.
Definition: base_units.cpp:302
int ValueFromString ( const wxString &  aTextValue)

Function ValueFromString.

converts aTextValue in aUnits to internal units used by the application, unit type will be obtained from g_UserUnit.

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

References g_UserUnit, and ValueFromString().

377 {
378  int value;
379 
380  value = ValueFromString( g_UserUnit, aTextValue);
381 
382  return value;
383 }
int ValueFromString(EDA_UNITS_T aUnits, const wxString &aTextValue)
Function ValueFromString converts aTextValue in aUnits to internal units used by the application...
Definition: base_units.cpp:369
EDA_UNITS_T g_UserUnit
Global variables definitions.
Definition: common.cpp:56
int ValueFromTextCtrl ( const wxTextCtrl &  aTextCtr)

Convert the number Value in a string according to the internal units and the selected unit (g_UserUnit) and put it in the wxTextCtrl TextCtrl.

Definition at line 385 of file base_units.cpp.

References g_UserUnit, NumericEvaluator::process(), NumericEvaluator::result(), and ValueFromString().

Referenced by DIALOG_COPPER_ZONE::AcceptOptions(), DIALOG_DESIGN_RULES::CopyGlobalRulesToBoard(), DIALOG_PLOT_SCHEMATIC::getPlotOptions(), DIALOG_POSITION_RELATIVE::GetTranslationInIU(), DIALOG_MOVE_EXACT::GetTranslationInIU(), DIALOG_PADS_MASK_CLEARANCE::OnButtonOkClick(), DIALOG_MODEDIT_OPTIONS::OnOkClick(), DIALOG_GRAPHIC_ITEMS_OPTIONS::OnOkClick(), DIALOG_DRC_CONTROL::SetDrcParmeters(), DIALOG_PLOT_SCHEMATIC::SetHPGLPenWidth(), DIALOG_SVG_PRINT::SetPenWidth(), DIALOG_PRINT_USING_PRINTER::SetPenWidth(), DIALOG_DESIGN_RULES::TestDataValidity(), DIALOG_GLOBAL_MODULES_FIELDS_EDITION::TransferDataFromWindow(), DIALOG_MODULE_MODULE_EDITOR::TransferDataFromWindow(), DIALOG_MODULE_BOARD_EDITOR::TransferDataFromWindow(), DIALOG_LAYERS_SETUP::TransferDataFromWindow(), and DIALOG_PAD_PROPERTIES::transferDataToPad().

386 {
387  int value;
388  wxString msg = aTextCtr.GetValue();
389  NumericEvaluator eval;
390 
391  if( eval.process( msg.mb_str() ) )
392  msg = wxString::FromUTF8( eval.result() );
393 
394  value = ValueFromString( g_UserUnit, msg );
395 
396  return value;
397 }
bool process(const char *s)
int ValueFromString(EDA_UNITS_T aUnits, const wxString &aTextValue)
Function ValueFromString converts aTextValue in aUnits to internal units used by the application...
Definition: base_units.cpp:369
EDA_UNITS_T g_UserUnit
Global variables definitions.
Definition: common.cpp:56
const char * result() const