KiCad PCB EDA Suite
base_units.cpp File Reference

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

#include <macros.h>
#include <base_struct.h>
#include <title_block.h>
#include <common.h>
#include <base_units.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_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)
 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 LengthDoubleToString (double aValue, bool aConvertToMils)
 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...
 
void StripTrailingZeros (wxString &aStringValue, unsigned aTrailingZeroAllowed)
 Function StripTrailingZeros Remove trailing 0 from a string containing a converted float number. More...
 
wxString StringFromValue (EDA_UNITS_T aUnit, int aValue, bool aAddUnitSymbol)
 Function StringFromValue returns the string from aValue according to units (inch, mm ...) for display, and the initial unit for value. 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 & operator<< (wxString &aString, const wxPoint &aPos)
 Operator << overload outputs a point to the argument string in a format resembling "@ (x,y) 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 ReturnUnitSymbol (EDA_UNITS_T aUnit, const wxString &formatString)
 Returns the units symbol. More...
 
wxString GetUnitsLabel (EDA_UNITS_T aUnit)
 Get a human readable units string. More...
 
wxString GetAbbreviatedUnitsLabel (EDA_UNITS_T aUnit)
 
void AddUnitSymbol (wxStaticText &Stext, EDA_UNITS_T aUnit)
 

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

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

References StripTrailingZeros().

Referenced by DIALOG_GENERALOPTIONS::init(), and Mils2mm().

416 {
417  wxString text;
418 
419  text.Printf( wxT( "%.3f" ), aAngle/10.0 );
420  StripTrailingZeros( text, 1 );
421 
422  return text;
423 }
void StripTrailingZeros(wxString &aStringValue, unsigned aTrailingZeroAllowed)
Function StripTrailingZeros Remove trailing 0 from a string containing a converted float number...
Definition: base_units.cpp:173
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(), EDA_DRAW_FRAME::GeneralControl(), TEXTE_PCB::GetMsgPanelInfo(), TEXTE_MODULE::GetMsgPanelInfo(), DRAWSEGMENT::GetMsgPanelInfo(), TRACK::GetMsgPanelInfo(), D_PAD::GetMsgPanelInfo(), TRACK::GetMsgPanelInfoBase(), SEGZONE::GetMsgPanelInfoBase(), VIA::GetMsgPanelInfoBase(), PCB_TARGET::GetSelectMenuText(), LIB_CIRCLE::GetSelectMenuText(), LIB_RECTANGLE::GetSelectMenuText(), LIB_POLYLINE::GetSelectMenuText(), LIB_ARC::GetSelectMenuText(), SCH_LINE::GetSelectMenuText(), Mils2mm(), 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(), Mils2mm(), GBR_TO_PCB_EXPORTER::writeCopperLineItem(), GBR_TO_PCB_EXPORTER::writePcbLineItem(), and GBR_TO_PCB_EXPORTER::writePcbPolygonItem().

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

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

Referenced by DIALOG_PLOT::applyPlotSettings(), DIALOG_CREATE_ARRAY::calculateCircularArrayProperties(), PROPERTIES_FRAME::CopyPrmsFromPanelToGeneral(), PROPERTIES_FRAME::CopyPrmsFromPanelToItem(), DIALOG_SET_GRID::getGridOrigin(), DIALOG_SET_GRID::getGridSize(), 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(), Mils2mm(), DIALOG_POSITION_RELATIVE::OnOkClick(), DIALOG_MOVE_EXACT::TransferDataFromWindow(), DIALOG_CREATE_ARRAY::TransferDataFromWindow(), DIALOG_PAD_PRIMITIVES_TRANSFORM::Transform(), and ValueFromString().

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

References DEGREES, INCHES, MILLIMETRES, and UNSCALED_UNITS.

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

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

Definition at line 485 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_SET_GRID::DIALOG_SET_GRID(), DIALOG_TRACK_VIA_PROPERTIES::DIALOG_TRACK_VIA_PROPERTIES(), DIALOG_DRC_CONTROL::DisplayDRCValues(), SCH_EDIT_FRAME::EditLine(), DIALOG_FOOTPRINT_BOARD_EDITOR::InitBoardProperties(), DIALOG_GLOBAL_FOOTPRINTS_FIELDS_EDITION::initDialog(), DIALOG_DESIGN_RULES::InitGlobalRules(), DIALOG_FOOTPRINT_FP_EDITOR::initModeditProperties(), DIALOG_MODEDIT_OPTIONS::initValues(), DIALOG_PAD_PROPERTIES::initValues(), Mils2mm(), 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().

486 {
487  wxString label;
488 
489  switch( aUnit )
490  {
491  case INCHES:
492  label = _( "in" );
493  break;
494 
495  case MILLIMETRES:
496  label = _( "mm" );
497  break;
498 
499  case UNSCALED_UNITS:
500  break;
501 
502  case DEGREES:
503  label = _( "deg" );
504  break;
505 
506  default:
507  label = wxT( "??" );
508  break;
509  }
510 
511  return label;
512 }
Definition: common.h:146
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 458 of file base_units.cpp.

References DEGREES, INCHES, MILLIMETRES, and UNSCALED_UNITS.

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

459 {
460  wxString label;
461 
462  switch( aUnit )
463  {
464  case INCHES:
465  label = _( "inches" );
466  break;
467 
468  case MILLIMETRES:
469  label = _( "millimeters" );
470  break;
471 
472  case UNSCALED_UNITS:
473  label = _( "units" );
474  break;
475 
476  case DEGREES:
477  label = _( "degrees" );
478  break;
479  }
480 
481  return label;
482 }
Definition: common.h:146
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(), EDA_DRAW_FRAME::GeneralControl(), TRACK::GetMsgPanelInfo(), NETINFO_ITEM::GetMsgPanelInfo(), TRACK::GetMsgPanelInfoBase(), SEGZONE::GetMsgPanelInfoBase(), DRAWSEGMENT::GetSelectMenuText(), TRACK::GetSelectMenuText(), EDA_DRAW_FRAME::LengthDoubleToString(), Mils2mm(), 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  text += " ";
160 
161  if( g_UserUnit == INCHES )
162  text += ( aConvertToMils ) ? _( "mils" ) : _( "in" );
163  else
164  text += _( "mm" );
165 
166  return text;
167 }
Definition: common.h:146
EDA_UNITS_T g_UserUnit
Global variables definitions.
Definition: common.cpp:57
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
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 401 of file base_units.cpp.

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

Referenced by EDA_ITEM::GetMenuImage(), and Mils2mm().

402 {
403  aString << wxT( "@ (" ) << CoordinateToString( aPos.x );
404  aString << wxT( "," ) << CoordinateToString( aPos.y );
405  aString << wxT( ")" );
406 
407  return aString;
408 }
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 267 of file base_units.cpp.

References g_UserUnit, and StringFromValue().

Referenced by DIALOG_DIMENSION_EDITOR::DIALOG_DIMENSION_EDITOR(), DIALOG_POSITION_RELATIVE::DIALOG_POSITION_RELATIVE(), DIALOG_DRC_CONTROL::DisplayDRCValues(), DIALOG_FOOTPRINT_BOARD_EDITOR::InitBoardProperties(), DIALOG_COPPER_ZONE::initDialog(), DIALOG_PLOT_SCHEMATIC::initDlg(), DIALOG_DESIGN_RULES::InitGlobalRules(), DIALOG_FOOTPRINT_FP_EDITOR::initModeditProperties(), DIALOG_GRAPHIC_ITEMS_OPTIONS::initValues(), DIALOG_MODEDIT_OPTIONS::initValues(), DIALOG_PAD_PROPERTIES::initValues(), Mils2mm(), DIALOG_PADS_MASK_CLEARANCE::myInit(), DIALOG_GRAPHIC_ITEM_PROPERTIES::OnLayerChoice(), DIALOG_POSITION_RELATIVE::OnPolarChanged(), DIALOG_MOVE_EXACT::OnPolarChanged(), DIALOG_SET_GRID::setGridOrigin(), DIALOG_EDIT_FPTEXT::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().

268 {
269  wxString msg = StringFromValue( g_UserUnit, aValue );
270 
271  aTextCtr.SetValue( msg );
272 }
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:205
EDA_UNITS_T g_UserUnit
Global variables definitions.
Definition: common.cpp:57
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 426 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(), Mils2mm(), and DIALOG_DESIGN_RULES::TestDataValidity().

427 {
428  wxString tmp;
429  wxString label;
430 
431  switch( aUnit )
432  {
433  case INCHES:
434  tmp = _( "\"" );
435  break;
436 
437  case MILLIMETRES:
438  tmp = _( "mm" );
439  break;
440 
441  case UNSCALED_UNITS:
442  break;
443 
444  case DEGREES:
445  wxASSERT( false );
446  break;
447  }
448 
449  if( formatString.IsEmpty() )
450  return tmp;
451 
452  label.Printf( formatString, GetChars( tmp ) );
453 
454  return label;
455 }
Definition: common.h:146
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 205 of file base_units.cpp.

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

Referenced by Append_Track_Width_List(), 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_ARC::GetMsgPanelInfo(), LIB_BEZIER::GetMsgPanelInfo(), LIB_PIN::GetMsgPanelInfo(), LIB_FIELD::GetMsgPanelInfo(), SCH_TEXT::GetMsgPanelInfo(), LIB_PIN::getMsgPanelInfoBase(), TRACK_VIA_SIZE_MENU::getTrackDescription(), TRACK_VIA_SIZE_MENU::getViaDescription(), DIALOG_NON_COPPER_ZONES_EDITOR::Init(), DIALOG_PLOT::init_Dialog(), DIALOG_SVG_PRINT::initDialog(), DIALOG_GLOBAL_FOOTPRINTS_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(), Mils2mm(), DIALOG_PAD_PROPERTIES::onCornerSizePercentChange(), LIB_EDIT_FRAME::OnEditPin(), DIALOG_TRACK_VIA_PROPERTIES::onViaSelect(), DIALOG_PAD_PROPERTIES::padValuesOK(), 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().

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

Referenced by AngleToStringDegrees(), Mils2mm(), and StringFromValue().

174 {
175  struct lconv * lc = localeconv();
176  char sep = lc->decimal_point[0];
177  unsigned sep_pos = aStringValue.Find( sep );
178 
179  if( sep_pos > 0 )
180  {
181  // We want to keep at least aTrailingZeroAllowed digits after the separator
182  unsigned min_len = sep_pos + aTrailingZeroAllowed + 1;
183 
184  while( aStringValue.Len() > min_len )
185  {
186  if( aStringValue.Last() == '0' )
187  aStringValue.RemoveLast();
188  else
189  break;
190  }
191  }
192 }
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(), Mils2mm(), SCH_EDIT_FRAME::OnFindDrcMarker(), PCB_BASE_FRAME::SaveSettings(), DIALOG_DESIGN_RULES::SetDataValidators(), DIALOG_SET_GRID::setGridSize(), 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:146
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 370 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(), DIALOG_ANNOTATE::GetStartNumber(), UNIT_BINDER::GetValue(), EDA_POSITION_CTRL::GetValue(), EDA_VALUE_CTRL::GetValue(), Mils2mm(), 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(), DIALOG_EDIT_FPTEXT::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().

371 {
372  double value = DoubleValueFromString( aUnits, aTextValue );
373  return KiROUND( value );
374 }
static int KiROUND(double v)
Round a floating point number to an integer 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:303
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 377 of file base_units.cpp.

References g_UserUnit, and ValueFromString().

378 {
379  int value;
380 
381  value = ValueFromString( g_UserUnit, aTextValue);
382 
383  return value;
384 }
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:370
EDA_UNITS_T g_UserUnit
Global variables definitions.
Definition: common.cpp:57
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 386 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(), Mils2mm(), 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_FOOTPRINTS_FIELDS_EDITION::TransferDataFromWindow(), DIALOG_FOOTPRINT_FP_EDITOR::TransferDataFromWindow(), DIALOG_FOOTPRINT_BOARD_EDITOR::TransferDataFromWindow(), DIALOG_LAYERS_SETUP::TransferDataFromWindow(), and DIALOG_PAD_PROPERTIES::transferDataToPad().

387 {
388  int value;
389  wxString msg = aTextCtr.GetValue();
390  NumericEvaluator eval;
391 
392  if( eval.process( msg.mb_str() ) )
393  msg = wxString::FromUTF8( eval.result() );
394 
395  value = ValueFromString( g_UserUnit, msg );
396 
397  return value;
398 }
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:370
EDA_UNITS_T g_UserUnit
Global variables definitions.
Definition: common.cpp:57
const char * result() const