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

References StripTrailingZeros().

Referenced by DIALOG_GENERALOPTIONS::init().

410 {
411  wxString text;
412 
413  text.Printf( wxT( "%.3f" ), aAngle/10.0 );
414  StripTrailingZeros( text, 1 );
415 
416  return text;
417 }
void StripTrailingZeros(wxString &aStringValue, unsigned aTrailingZeroAllowed)
Function StripTrailingZeros Remove trailing 0 from a string containing a converted float number...
Definition: base_units.cpp:171
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 117 of file base_units.cpp.

References LengthDoubleToString().

Referenced by DIMENSION::AdjustDimensionDetails(), EDA_DRAW_FRAME::CoordinateToString(), 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().

118 {
119  return LengthDoubleToString( (double) aValue, aConvertToMils );
120 }
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:122
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 60 of file base_units.cpp.

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

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

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

References DEGREES, INCHES, MILLIMETRES, and UNSCALED_UNITS.

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

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

Definition at line 479 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_LAYERS_SETUP::DIALOG_LAYERS_SETUP(), DIALOG_MOVE_EXACT::DIALOG_MOVE_EXACT(), DIALOG_POSITION_RELATIVE::DIALOG_POSITION_RELATIVE(), DIALOG_DRC_CONTROL::DisplayDRCValues(), 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(), WX_UNIT_BINDER::SetValue(), DIALOG_GRAPHIC_ITEM_PROPERTIES::TransferDataToWindow(), DIALOG_MODEDIT_FP_BODY_ITEM_PROPERTIES::TransferDataToWindow(), DIALOG_POSITION_RELATIVE::updateDlgTexts(), DIALOG_MOVE_EXACT::updateDlgTexts(), and WX_UNIT_BINDER::WX_UNIT_BINDER().

480 {
481  wxString label;
482 
483  switch( aUnit )
484  {
485  case INCHES:
486  label = _( "in" );
487  break;
488 
489  case MILLIMETRES:
490  label = _( "mm" );
491  break;
492 
493  case UNSCALED_UNITS:
494  break;
495 
496  case DEGREES:
497  label = _( "deg" );
498  break;
499 
500  default:
501  label = wxT( "??" );
502  break;
503  }
504 
505  return label;
506 }
Definition: common.h:145
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 452 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(), WX_UNIT_TEXT::SetUnits(), TARGET_PROPERTIES_DIALOG_EDITOR::TARGET_PROPERTIES_DIALOG_EDITOR(), and WX_UNIT_TEXT::WX_UNIT_TEXT().

453 {
454  wxString label;
455 
456  switch( aUnit )
457  {
458  case INCHES:
459  label = _( "inches" );
460  break;
461 
462  case MILLIMETRES:
463  label = _( "millimeters" );
464  break;
465 
466  case UNSCALED_UNITS:
467  label = _( "units" );
468  break;
469 
470  case DEGREES:
471  label = _( "degrees" );
472  break;
473  }
474 
475  return label;
476 }
Definition: common.h:145
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 122 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().

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

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

396 {
397  aString << wxT( "@ (" ) << CoordinateToString( aPos.x );
398  aString << wxT( "," ) << CoordinateToString( aPos.y );
399  aString << wxT( ")" );
400 
401  return aString;
402 }
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:117
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 265 of file base_units.cpp.

References g_UserUnit, and StringFromValue().

Referenced by DIALOG_DIMENSION_EDITOR::DIALOG_DIMENSION_EDITOR(), DIALOG_LAYERS_SETUP::DIALOG_LAYERS_SETUP(), 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_GRAPHIC_ITEM_PROPERTIES::TransferDataToWindow(), DIALOG_MODEDIT_FP_BODY_ITEM_PROPERTIES::TransferDataToWindow(), and DIALOG_POSITION_RELATIVE::UpdateAnchor().

266 {
267  wxString msg = StringFromValue( g_UserUnit, aValue );
268 
269  aTextCtr.SetValue( msg );
270 }
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:203
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 420 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().

421 {
422  wxString tmp;
423  wxString label;
424 
425  switch( aUnit )
426  {
427  case INCHES:
428  tmp = _( "\"" );
429  break;
430 
431  case MILLIMETRES:
432  tmp = _( "mm" );
433  break;
434 
435  case UNSCALED_UNITS:
436  break;
437 
438  case DEGREES:
439  wxASSERT( false );
440  break;
441  }
442 
443  if( formatString.IsEmpty() )
444  return tmp;
445 
446  label.Printf( formatString, GetChars( tmp ) );
447 
448  return label;
449 }
Definition: common.h:145
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 203 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(), EDA_VALUE_CTRL::EDA_VALUE_CTRL(), PCB_EDIT_FRAME::Edit_Gap(), LIB_EDIT_FRAME::EditGraphicSymbol(), 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(), PutValueInLocalUnits(), DIALOG_SVG_PRINT::SetPenWidth(), DIALOG_PRINT_USING_PRINTER::SetPenWidth(), WX_UNIT_BINDER::SetValue(), EDA_POSITION_CTRL::SetValue(), EDA_VALUE_CTRL::SetValue(), TARGET_PROPERTIES_DIALOG_EDITOR::TARGET_PROPERTIES_DIALOG_EDITOR(), DIALOG_EDIT_ONE_FIELD::TransferDataToWindow(), DIALOG_GLOBAL_EDIT_TRACKS_AND_VIAS::updateNetInfo(), and DIALOG_PAD_PROPERTIES::updateRoundRectCornerValues().

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

Referenced by AngleToStringDegrees(), and StringFromValue().

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

91 {
92  switch( aUnit )
93  {
94  case MILLIMETRES:
95  return IU_TO_MM( aValue );
96 
97  case INCHES:
98  return IU_TO_IN( aValue );
99 
100  case DEGREES:
101  return aValue / 10.0f;
102 
103  default:
104  return aValue;
105  }
106 }
Definition: common.h:145
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 368 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::EditSheet(), SCH_EDIT_FRAME::EditSheetPin(), FOOTPRINT_EDIT_FRAME::Enter_Edge_Width(), WX_UNIT_BINDER::GetValue(), EDA_POSITION_CTRL::GetValue(), EDA_VALUE_CTRL::GetValue(), 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_GRAPHIC_ITEM_PROPERTIES::TransferDataFromWindow(), DIALOG_MODEDIT_FP_BODY_ITEM_PROPERTIES::TransferDataFromWindow(), DIALOG_MODEDIT_FP_BODY_ITEM_PROPERTIES::Validate(), DIALOG_GRAPHIC_ITEM_PROPERTIES::Validate(), ValueFromString(), and ValueFromTextCtrl().

369 {
370  double value = DoubleValueFromString( aUnits, aTextValue );
371  return KiROUND( value );
372 }
static int KiROUND(double v)
KiROUND rounds a floating point number to an int using "round halfway cases away from zero"...
Definition: common.h:107
double DoubleValueFromString(EDA_UNITS_T aUnits, const wxString &aTextValue)
Function DoubleValueFromString converts aTextValue to a double.
Definition: base_units.cpp:301
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 375 of file base_units.cpp.

References g_UserUnit, and ValueFromString().

376 {
377  int value;
378 
379  value = ValueFromString( g_UserUnit, aTextValue);
380 
381  return value;
382 }
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:368
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 384 of file base_units.cpp.

References g_UserUnit, 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_LAYERS_SETUP::OnOkButtonClick(), 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(), and DIALOG_PAD_PROPERTIES::transferDataToPad().

385 {
386  int value;
387  wxString msg = aTextCtr.GetValue();
388 
389  value = ValueFromString( g_UserUnit, msg );
390 
391  return value;
392 }
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:368
EDA_UNITS_T g_UserUnit
Global variables definitions.
Definition: common.cpp:56