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, bool aUseMils)
 Function To_User_Unit convert aValue in internal units to the appropriate user units defined by aUnit. More...
 
wxString MessageTextFromValue (EDA_UNITS_T aUnits, int aValue, bool aUseMils)
 
wxString MessageTextFromValue (EDA_UNITS_T aUnits, long long int aValue, bool aUseMils)
 
wxString MessageTextFromValue (EDA_UNITS_T aUnits, double aValue, bool aUseMils)
 Function MessageTextFromValue is a helper to convert the double length aValue to a string in inches, millimeters, or unscaled units. More...
 
void StripTrailingZeros (wxString &aStringValue, unsigned aTrailingZeroAllowed)
 Function StripTrailingZeros Remove trailing 0 from a string containing a converted float number. More...
 
wxString StringFromValue (EDA_UNITS_T aUnits, double aValue, bool aAddUnitSymbol, bool aUseMils)
 Function StringFromValue returns the string from aValue according to units (inch, mm ...) for display, and the initial unit for value. More...
 
double From_User_Unit (EDA_UNITS_T aUnits, double aValue, bool aUseMils)
 Return in internal units the value "val" given in a real unit such as "in", "mm" or "deg". More...
 
double DoubleValueFromString (EDA_UNITS_T aUnits, const wxString &aTextValue, bool aUseMils)
 Function DoubleValueFromString converts aTextValue to a double. More...
 
void FetchUnitsFromString (const wxString &aTextValue, EDA_UNITS_T &aUnits, bool &aUseMils)
 Function FetchUnitsFromString writes any unit info found in the string to aUnits and aUseMils. More...
 
long long int ValueFromString (EDA_UNITS_T aUnits, const wxString &aTextValue, bool aUseMils)
 Function ValueFromString converts aTextValue in aUnits to internal units used by the application. More...
 
wxString AngleToStringDegrees (double aAngle)
 Function AngleToStringDegrees is a helper to convert the double aAngle (in internal unit) to a string in degrees. More...
 
wxString GetAbbreviatedUnitsLabel (EDA_UNITS_T aUnit, bool aUseMils)
 Get the units string for a given units type. More...
 
std::string FormatInternalUnits (int aValue)
 Function FormatInternalUnits converts aValue from internal units to a string appropriate for writing to file. More...
 
std::string FormatAngle (double aAngle)
 Function FormatAngle converts aAngle from board units to a string appropriate for writing to file. More...
 
std::string FormatInternalUnits (const wxPoint &aPoint)
 
std::string FormatInternalUnits (const VECTOR2I &aPoint)
 
std::string FormatInternalUnits (const wxSize &aSize)
 

Detailed Description

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

Author
Wayne Stambaugh stamb.nosp@m.augh.nosp@m.w@ver.nosp@m.izon.nosp@m..net
Note
This file is an ugly hack to solve the problem of formatting the base units for either schematics or boards in objects that are include in both domains. At some point in the future. This code should be rolled back into the appropriate object and build with the correct internal unit formatting depending on the application.

Definition in file base_units.cpp.

Function Documentation

◆ AngleToStringDegrees()

wxString AngleToStringDegrees ( double  aAngle)

Function AngleToStringDegrees is a helper to convert the double aAngle (in internal unit) to a string in degrees.

Definition at line 439 of file base_units.cpp.

440 {
441  wxString text;
442 
443  text.Printf( wxT( "%.3f" ), aAngle/10.0 );
444  StripTrailingZeros( text, 1 );
445 
446  return text;
447 }
void StripTrailingZeros(wxString &aStringValue, unsigned aTrailingZeroAllowed)
Function StripTrailingZeros Remove trailing 0 from a string containing a converted float number.
Definition: base_units.cpp:185

References StripTrailingZeros().

Referenced by PANEL_PCBNEW_SETTINGS::TransferDataToWindow().

◆ Double2Str()

std::string Double2Str ( double  aValue)

Helper function Double2Str to print a float number without using scientific notation and no trailing 0 We want to avoid scientific notation in S-expr files (not easy to read) for floating numbers.

So we cannot always just use the g or the f format to print a fp number this helper function uses the f format when needed, or g when f is not well working and then removes trailing 0

Definition at line 63 of file base_units.cpp.

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

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

◆ DoubleValueFromString()

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

Function DoubleValueFromString converts aTextValue to a double.

Parameters
aUnitsThe units of aTextValue.
aTextValueA reference to a wxString object containing the string to convert.
aUseMilsIndicates mils should be used for imperial units (inches).
Returns
A double representing that value in internal units

Definition at line 314 of file base_units.cpp.

315 {
316  double value;
317  double dtmp = 0;
318 
319  // Acquire the 'right' decimal point separator
320  const struct lconv* lc = localeconv();
321 
322  wxChar decimal_point = lc->decimal_point[0];
323  wxString buf( aTextValue.Strip( wxString::both ) );
324 
325  // Convert the period in decimal point
326  buf.Replace( wxT( "." ), wxString( decimal_point, 1 ) );
327 
328  // Find the end of the numeric part
329  unsigned brk_point = 0;
330 
331  while( brk_point < buf.Len() )
332  {
333  wxChar ch = buf[brk_point];
334 
335  if( !( (ch >= '0' && ch <='9') || (ch == decimal_point) || (ch == '-') || (ch == '+') ) )
336  {
337  break;
338  }
339 
340  ++brk_point;
341  }
342 
343  // Extract the numeric part
344  buf.Left( brk_point );
345 
346  buf.ToDouble( &dtmp );
347 
348  // Check the optional unit designator (2 ch significant)
349  wxString unit( buf.Mid( brk_point ).Strip( wxString::leading ).Left( 2 ).Lower() );
350 
351  if( aUnits == INCHES || aUnits == MILLIMETRES )
352  {
353  if( unit == wxT( "in" ) || unit == wxT( "\"" ) )
354  {
355  aUnits = INCHES;
356  aUseMils = false;
357  }
358  else if( unit == wxT( "mm" ) )
359  {
360  aUnits = MILLIMETRES;
361  }
362  else if( unit == wxT( "mi" ) || unit == wxT( "th" ) ) // "mils" or "thou"
363  {
364  aUnits = INCHES;
365  aUseMils = true;
366  }
367  else if( unit == "oz" ) // 1 oz = 1.37 mils
368  {
369  aUnits = INCHES;
370  aUseMils = true;
371  dtmp *= 1.37;
372  }
373  }
374  else if( aUnits == DEGREES )
375  {
376  if( unit == wxT( "ra" ) ) // Radians
377  {
378  dtmp *= 180.0f / M_PI;
379  }
380  }
381 
382  value = From_User_Unit( aUnits, dtmp, aUseMils );
383 
384  return value;
385 }
double From_User_Unit(EDA_UNITS_T aUnits, double aValue, bool aUseMils)
Return in internal units the value "val" given in a real unit such as "in", "mm" or "deg".
Definition: base_units.cpp:289
Definition: common.h:134

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

Referenced by PROPERTIES_FRAME::CopyPrmsFromPanelToGeneral(), PROPERTIES_FRAME::CopyPrmsFromPanelToItem(), PANEL_PREV_3D::doIncrementOffset(), PANEL_PREV_3D::doIncrementRotation(), PANEL_PREV_3D::doIncrementScale(), DIALOG_IMPORT_GFX::getPCBdefaultLineWidthMM(), DIALOG_EXPORT_STEP::GetXOrg(), DIALOG_EXPORT_IDF3::GetXRef(), DIALOG_EXPORT_3DFILE::GetXRef(), DIALOG_EXPORT_STEP::GetYOrg(), DIALOG_EXPORT_IDF3::GetYRef(), DIALOG_EXPORT_3DFILE::GetYRef(), PANEL_PREV_3D::onMouseWheelOffset(), PANEL_PREV_3D::onMouseWheelRot(), PANEL_PREV_3D::onMouseWheelScale(), rotationFromString(), TEXT_MOD_GRID_TABLE::SetValue(), DIALOG_IMPORT_GFX::TransferDataFromWindow(), DIALOG_CREATE_ARRAY::TransferDataFromWindow(), DIALOG_PAD_PRIMITIVES_TRANSFORM::Transform(), PANEL_PREV_3D::updateOrientation(), DIALOG_IMPORT_GFX::updatePcbImportOffsets_mm(), and ValueFromString().

◆ FetchUnitsFromString()

void FetchUnitsFromString ( const wxString &  aTextValue,
EDA_UNITS_T aUnits,
bool &  aUseMils 
)

Function FetchUnitsFromString writes any unit info found in the string to aUnits and aUseMils.

Definition at line 388 of file base_units.cpp.

389 {
390  wxString buf( aTextValue.Strip( wxString::both ) );
391  unsigned brk_point = 0;
392 
393  while( brk_point < buf.Len() )
394  {
395  wxChar c = buf[brk_point];
396 
397  if( !( (c >= '0' && c <='9') || (c == '.') || (c == ',') || (c == '-') || (c == '+') ) )
398  break;
399 
400  ++brk_point;
401  }
402 
403  // Check the unit designator (2 ch significant)
404  wxString unit( buf.Mid( brk_point ).Strip( wxString::leading ).Left( 2 ).Lower() );
405 
406  if( unit == wxT( "in" ) || unit == wxT( "\"" ) )
407  {
408  aUnits = INCHES;
409  aUseMils = false;
410  }
411  else if( unit == wxT( "mm" ) )
412  {
413  aUnits = MILLIMETRES;
414  }
415  else if( unit == wxT( "mi" ) || unit == wxT( "th" ) ) // "mils" or "thou"
416  {
417  aUnits = INCHES;
418  aUseMils = true;
419  }
420  else if( unit == wxT( "de" ) || unit == wxT( "ra" ) ) // "deg" or "rad"
421  {
422  aUnits = DEGREES;
423  }
424 }
Definition: common.h:134

References DEGREES, INCHES, and MILLIMETRES.

Referenced by DIALOG_TEXT_PROPERTIES::OnDimensionTextChange(), and PCB_PARSER::parseDIMENSION().

◆ FormatAngle()

std::string FormatAngle ( double  aAngle)

Function FormatAngle converts aAngle from board units to a string appropriate for writing to file.

Note
Internal angles for board items can be either degrees or tenths of degree on how KiCad is built.
Parameters
aAngleA angle value to convert.
Returns
A std::string object containing the converted angle.

Definition at line 511 of file base_units.cpp.

512 {
513  char temp[50];
514  int len;
515 
516  len = snprintf( temp, sizeof(temp), "%.10g", aAngle / 10.0 );
517 
518  return std::string( temp, len );
519 }

Referenced by DIALOG_PAD_PROPERTIES::displayPrimitivesList(), and PCB_IO::format().

◆ FormatInternalUnits() [1/4]

std::string FormatInternalUnits ( int  aValue)

Function FormatInternalUnits converts aValue from internal units to a string appropriate for writing to file.

Note
Internal units for board items can be either deci-mils or nanometers depending on how KiCad is built.
Parameters
aValueA coordinate value to convert.
Returns
A std::string object containing the converted value.

Definition at line 478 of file base_units.cpp.

479 {
480  char buf[50];
481  double engUnits = aValue;
482  int len;
483 
484 #ifndef EESCHEMA
485  engUnits /= IU_PER_MM;
486 #endif
487 
488  if( engUnits != 0.0 && fabs( engUnits ) <= 0.0001 )
489  {
490  len = snprintf( buf, sizeof(buf), "%.10f", engUnits );
491 
492  while( --len > 0 && buf[len] == '0' )
493  buf[len] = '\0';
494 
495 #ifndef EESCHEMA
496  if( buf[len] == '.' )
497  buf[len] = '\0';
498  else
499 #endif
500  ++len;
501  }
502  else
503  {
504  len = snprintf( buf, sizeof(buf), "%.10g", engUnits );
505  }
506 
507  return std::string( buf, len );
508 }

Referenced by NETCLASS::Format(), PCB_IO::format(), EDA_TEXT::Format(), BOARD_STACKUP::FormatBoardStackup(), PCB_IO::formatDefaults(), PCB_IO::formatGeneral(), FormatInternalUnits(), and PCB_IO::formatSetup().

◆ FormatInternalUnits() [2/4]

std::string FormatInternalUnits ( const wxPoint &  aPoint)

Definition at line 522 of file base_units.cpp.

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

References FormatInternalUnits().

◆ FormatInternalUnits() [3/4]

std::string FormatInternalUnits ( const VECTOR2I aPoint)

Definition at line 528 of file base_units.cpp.

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

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

◆ FormatInternalUnits() [4/4]

std::string FormatInternalUnits ( const wxSize &  aSize)

Definition at line 534 of file base_units.cpp.

535 {
536  return FormatInternalUnits( aSize.GetWidth() ) + " " + FormatInternalUnits( aSize.GetHeight() );
537 }
std::string FormatInternalUnits(int aValue)
Function FormatInternalUnits converts aValue from internal units to a string appropriate for writing ...
Definition: base_units.cpp:478

References FormatInternalUnits().

◆ From_User_Unit()

double From_User_Unit ( EDA_UNITS_T  aUnits,
double  aValue,
bool  aUseMils 
)

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

Definition at line 289 of file base_units.cpp.

290 {
291  switch( aUnits )
292  {
293  case MILLIMETRES:
294  return MM_TO_IU( aValue );
295 
296  case INCHES:
297  if( aUseMils )
298  return MILS_TO_IU( aValue );
299  else
300  return IN_TO_IU( aValue );
301 
302  case DEGREES:
303  // Convert to "decidegrees"
304  return aValue * 10;
305 
306  default:
307  case UNSCALED_UNITS:
308  case PERCENT:
309  return aValue;
310  }
311 }
Definition: common.h:134

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

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

◆ GetAbbreviatedUnitsLabel()

wxString GetAbbreviatedUnitsLabel ( EDA_UNITS_T  aUnit,
bool  aUseMils = false 
)

Get the units string for a given units type.

Parameters
aUnits- The units requested.
Returns
The human readable units string.

Definition at line 450 of file base_units.cpp.

451 {
452  switch( aUnit )
453  {
454  case INCHES:
455  if( aUseMils )
456  return _( "mils" );
457  else
458  return _( "in" );
459 
460  case MILLIMETRES:
461  return _( "mm" );
462 
463  case PERCENT:
464  return _( "%" );
465 
466  case UNSCALED_UNITS:
467  return wxEmptyString;
468 
469  case DEGREES:
470  return _( "deg" );
471 
472  default:
473  return wxT( "??" );
474  }
475 }
Definition: common.h:134
#define _(s)

References _, DEGREES, INCHES, MILLIMETRES, PERCENT, and UNSCALED_UNITS.

Referenced by DIMENSION::AdjustDimensionDetails(), DIALOG_PLOT::applyPlotSettings(), UNIT_BINDER::ChangeValue(), DIALOG_TRACK_VIA_PROPERTIES::DIALOG_TRACK_VIA_PROPERTIES(), PANEL_PREV_3D::formatOffsetValue(), formatPreviewDimension(), PANEL_PREV_3D::formatRotationValue(), MessageTextFromValue(), DIALOG_BOARD_STATISTICS::saveReportClicked(), UNIT_BINDER::SetUnits(), UNIT_BINDER::SetValue(), TEXT_MOD_GRID_TABLE::TEXT_MOD_GRID_TABLE(), UNIT_BINDER::UNIT_BINDER(), and DIALOG_BOARD_STATISTICS::updateWidets().

◆ MessageTextFromValue() [1/3]

wxString MessageTextFromValue ( EDA_UNITS_T  aUnits,
int  aValue,
bool  aUseMils 
)

Definition at line 125 of file base_units.cpp.

126 {
127  return MessageTextFromValue( aUnits, double( aValue ), aUseMils );
128 }
wxString MessageTextFromValue(EDA_UNITS_T aUnits, int aValue, bool aUseMils)
Definition: base_units.cpp:125

References MessageTextFromValue().

Referenced by DIALOG_SELECT_NET_FROM_LIST::buildNetsList(), DIALOG_PAD_PROPERTIES::displayPrimitivesList(), formatCoord(), LIB_CIRCLE::GetMsgPanelInfo(), LIB_RECTANGLE::GetMsgPanelInfo(), LIB_TEXT::GetMsgPanelInfo(), LIB_ARC::GetMsgPanelInfo(), LIB_POLYLINE::GetMsgPanelInfo(), TEXTE_PCB::GetMsgPanelInfo(), WS_DRAW_ITEM_BASE::GetMsgPanelInfo(), LIB_BEZIER::GetMsgPanelInfo(), LIB_PIN::GetMsgPanelInfo(), SCH_BITMAP::GetMsgPanelInfo(), LIB_FIELD::GetMsgPanelInfo(), TRACK::GetMsgPanelInfo(), TEXTE_MODULE::GetMsgPanelInfo(), SCH_TEXT::GetMsgPanelInfo(), DRAWSEGMENT::GetMsgPanelInfo(), NETINFO_ITEM::GetMsgPanelInfo(), D_PAD::GetMsgPanelInfo(), TRACK::GetMsgPanelInfoBase(), VIA::GetMsgPanelInfoBase(), LIB_RECTANGLE::GetSelectMenuText(), LIB_CIRCLE::GetSelectMenuText(), PCB_TARGET::GetSelectMenuText(), LIB_POLYLINE::GetSelectMenuText(), MARKER_PCB::GetSelectMenuText(), LIB_ARC::GetSelectMenuText(), WS_DRAW_ITEM_LINE::GetSelectMenuText(), WS_DRAW_ITEM_POLYPOLYGONS::GetSelectMenuText(), SCH_LINE::GetSelectMenuText(), TRACK::GetSelectMenuText(), WS_DRAW_ITEM_RECT::GetSelectMenuText(), DRAWSEGMENT::GetSelectMenuText(), WS_DRAW_ITEM_TEXT::GetSelectMenuText(), VIA::GetSelectMenuText(), WS_DRAW_ITEM_BITMAP::GetSelectMenuText(), MessageTextFromValue(), DIALOG_BOARD_STATISTICS::saveReportClicked(), DRC_ITEM::ShowCoord(), PNS::MEANDER_SKEW_PLACER::TuningInfo(), PNS::MEANDER_PLACER::TuningInfo(), PNS::DP_MEANDER_PLACER::TuningInfo(), TRACK_WIDTH_MENU::update(), DIFF_PAIR_MENU::update(), DIALOG_BOARD_STATISTICS::updateDrillGrid(), and DIALOG_BOARD_STATISTICS::updateWidets().

◆ MessageTextFromValue() [2/3]

wxString MessageTextFromValue ( EDA_UNITS_T  aUnits,
long long int  aValue,
bool  aUseMils 
)

Definition at line 132 of file base_units.cpp.

133 {
134  return MessageTextFromValue( aUnits, double( aValue ), aUseMils );
135 }
wxString MessageTextFromValue(EDA_UNITS_T aUnits, int aValue, bool aUseMils)
Definition: base_units.cpp:125

References MessageTextFromValue().

◆ MessageTextFromValue() [3/3]

wxString MessageTextFromValue ( EDA_UNITS_T  aUnits,
double  aValue,
bool  aUseMils = false 
)

Function MessageTextFromValue is a helper to convert the double length aValue to a string in inches, millimeters, or unscaled units.

Should be used only to display a coordinate in status, but not in dialogs, files, etc., because the mantissa of the number displayed has 4 digits max for readability. The actual internal value could need up to 8 digits to be printed.

Use StringFromValue() instead where precision matters.

Parameters
aUnitsThe units to show the value in. The unit string is added to the message text.
aValueThe double value to convert.
aUseMilsConvert inch values to mils if true.
Returns
The converted string for display in user interface elements.

Definition at line 139 of file base_units.cpp.

140 {
141  wxString text;
142  const wxChar* format;
143  double value = To_User_Unit( aUnits, aValue, aUseMils );
144 
145  if( aUnits == INCHES )
146  {
147  if( aUseMils )
148  {
149 #if defined( EESCHEMA )
150  format = wxT( "%.0f" );
151 #else
152  format = wxT( "%.1f" );
153 #endif
154  }
155  else
156  {
157 #if defined( EESCHEMA )
158  format = wxT( "%.3f" );
159 #else
160  format = wxT( "%.4f" );
161 #endif
162  }
163  }
164  else
165  {
166 #if defined( EESCHEMA )
167  format = wxT( "%.2f" );
168 #else
169  format = wxT( "%.3f" );
170 #endif
171  }
172 
173  text.Printf( format, value );
174  text += " ";
175 
176  text += GetAbbreviatedUnitsLabel( aUnits, aUseMils );
177 
178  return text;
179 }
double To_User_Unit(EDA_UNITS_T aUnit, double aValue, bool aUseMils)
Function To_User_Unit convert aValue in internal units to the appropriate user units defined by aUnit...
Definition: base_units.cpp:93
Definition: common.h:134
wxString GetAbbreviatedUnitsLabel(EDA_UNITS_T aUnit, bool aUseMils)
Get the units string for a given units type.
Definition: base_units.cpp:450

References GetAbbreviatedUnitsLabel(), INCHES, and To_User_Unit().

◆ StringFromValue()

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

Function StringFromValue returns the string from aValue according to units (inch, mm ...) for display, and the initial unit for value.

For readability, the mantissa has 3 or more digits (max 8 digits), the trailing 0 are removed if the mantissa has more than 3 digits and some trailing 0 This function should be used to display values in dialogs because a value entered in mm (for instance 2.0 mm) could need up to 8 digits mantissa if displayed in inch to avoid truncation or rounding made just by the printf function. otherwise the actual value is rounded when read from dialog and converted in internal units, and therefore modified.

Parameters
aUnit= display units (INCHES, MILLIMETRE ..)
aValue= value in Internal_Unit
aAddUnitSymbol= true to add symbol unit to the string value
aUseMilsIndicates mils should be used for imperial units (inches).
Returns
A wxString object containing value and optionally the symbol unit (like 2.000 mm)

Definition at line 217 of file base_units.cpp.

218 {
219  double value_to_print = To_User_Unit( aUnits, aValue, aUseMils );
220 
221 #if defined( EESCHEMA )
222  wxString stringValue = wxString::Format( wxT( "%.3f" ), value_to_print );
223 
224  // Strip trailing zeros. However, keep at least 3 digits in mantissa
225  // For readability
226  StripTrailingZeros( stringValue, 3 );
227 
228 #else
229 
230  char buf[50];
231  int len;
232 
233  if( value_to_print != 0.0 && fabs( value_to_print ) <= 0.0001 )
234  {
235  len = sprintf( buf, "%.10f", value_to_print );
236 
237  while( --len > 0 && buf[len] == '0' )
238  buf[len] = '\0';
239 
240  if( buf[len]=='.' || buf[len]==',' )
241  buf[len] = '\0';
242  else
243  ++len;
244  }
245  else
246  {
247  if( aUnits == INCHES && aUseMils )
248  len = sprintf( buf, "%.7g", value_to_print );
249  else
250  len = sprintf( buf, "%.10g", value_to_print );
251  }
252 
253  wxString stringValue( buf, wxConvUTF8 );
254 
255 #endif
256 
257  if( aAddUnitSymbol )
258  {
259  switch( aUnits )
260  {
261  case INCHES:
262  if( aUseMils )
263  stringValue += wxT( " mils" );
264  else
265  stringValue += wxT( " in" );
266  break;
267 
268  case MILLIMETRES:
269  stringValue += wxT( " mm" );
270  break;
271 
272  case DEGREES:
273  stringValue += wxT( " deg" );
274  break;
275 
276  case PERCENT:
277  stringValue += wxT( "%" );
278  break;
279 
280  case UNSCALED_UNITS:
281  break;
282  }
283  }
284 
285  return stringValue;
286 }
void StripTrailingZeros(wxString &aStringValue, unsigned aTrailingZeroAllowed)
Function StripTrailingZeros Remove trailing 0 from a string containing a converted float number.
Definition: base_units.cpp:185
double To_User_Unit(EDA_UNITS_T aUnit, double aValue, bool aUseMils)
Function To_User_Unit convert aValue in internal units to the appropriate user units defined by aUnit...
Definition: base_units.cpp:93
Definition: common.h:134
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, CPTREE &aTree)
Function Format outputs a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:205

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

Referenced by PANEL_SETUP_TRACKS_AND_VIAS::AppendDiffPairs(), PANEL_SETUP_TRACKS_AND_VIAS::AppendTrackWidth(), PANEL_SETUP_TRACKS_AND_VIAS::AppendViaSize(), DIALOG_PLOT::applyPlotSettings(), BuildStackupReport(), UNIT_BINDER::ChangeValue(), ConvertOutlineToPolygon(), PCB_EDIT_FRAME::Create_MuWaveComponent(), MWAVE::CreateMicrowaveInductor(), PANEL_SETUP_BOARD_STACKUP::createRowData(), DIALOG_TRACK_VIA_PROPERTIES::DIALOG_TRACK_VIA_PROPERTIES(), LIB_PIN::getMsgPanelInfoBase(), TEXT_MOD_GRID_TABLE::GetValue(), FIELDS_GRID_TABLE< SCH_FIELD >::GetValue(), PIN_TABLE_DATA_MODEL::GetValue(), DIALOG_PLOT::init_Dialog(), DIALOG_PAD_PROPERTIES::initValues(), PANEL_SETUP_BOARD_STACKUP::onCalculateDielectricThickness(), DIALOG_TEXT_PROPERTIES::OnDimensionUnitsChange(), PANEL_SETUP_BOARD_STACKUP::onUpdateThicknessValue(), DIALOG_PAD_PROPERTIES::padValuesOK(), UNIT_BINDER::SetValue(), EDA_POSITION_CTRL::SetValue(), PANEL_SETUP_BOARD_STACKUP::synchronizeWithBoard(), PANEL_SETUP_BOARD_STACKUP::transferDataFromUIToStackup(), PANEL_EESCHEMA_SETTINGS::TransferDataToWindow(), PANEL_LIBEDIT_SETTINGS::TransferDataToWindow(), DIALOG_PAD_PRIMITIVE_POLY_PROPS::TransferDataToWindow(), UNIT_BINDER::Validate(), PANEL_SETUP_TRACKS_AND_VIAS::validateData(), and PANEL_SETUP_NETCLASSES::validateData().

◆ StripTrailingZeros()

void StripTrailingZeros ( wxString &  aStringValue,
unsigned  aTrailingZeroAllowed = 1 
)

Function StripTrailingZeros Remove trailing 0 from a string containing a converted float number.

The trailing 0 are removed if the mantissa has more than aTrailingZeroAllowed digits and some trailing 0

Definition at line 185 of file base_units.cpp.

186 {
187  struct lconv * lc = localeconv();
188  char sep = lc->decimal_point[0];
189  unsigned sep_pos = aStringValue.Find( sep );
190 
191  if( sep_pos > 0 )
192  {
193  // We want to keep at least aTrailingZeroAllowed digits after the separator
194  unsigned min_len = sep_pos + aTrailingZeroAllowed + 1;
195 
196  while( aStringValue.Len() > min_len )
197  {
198  if( aStringValue.Last() == '0' )
199  aStringValue.RemoveLast();
200  else
201  break;
202  }
203  }
204 }

Referenced by AngleToStringDegrees(), and StringFromValue().

◆ To_User_Unit()

double To_User_Unit ( EDA_UNITS_T  aUnit,
double  aValue,
bool  aUseMils = false 
)

Function To_User_Unit convert aValue in internal units to the appropriate user units defined by aUnit.

Returns
The converted value, in double
Parameters
aUnitThe units to convert aValue to.
aValueThe value in internal units to convert.
aUseMilsIndicates mils should be used for imperial units (inches).

Definition at line 93 of file base_units.cpp.

94 {
95  switch( aUnit )
96  {
97  case MILLIMETRES:
98  return IU_TO_MM( aValue );
99 
100  case INCHES:
101  if( aUseMils )
102  return IU_TO_MILS( aValue );
103  else
104  return IU_TO_IN( aValue );
105 
106  case DEGREES:
107  return aValue / 10.0f;
108 
109  default:
110  return aValue;
111  }
112 }
Definition: common.h:134

References DEGREES, INCHES, and MILLIMETRES.

Referenced by DIMENSION::AdjustDimensionDetails(), BASE_SCREEN::BuildGridsChoiceList(), PL_EDITOR_FRAME::DisplayGridMsg(), GERBVIEW_FRAME::DisplayGridMsg(), EDA_DRAW_FRAME::DisplayGridMsg(), PCB_BASE_FRAME::DisplayGridMsg(), formatPreviewDimension(), MessageTextFromValue(), PCB_BASE_FRAME::SaveSettings(), StringFromValue(), PL_EDITOR_FRAME::UpdateStatusBar(), SCH_BASE_FRAME::UpdateStatusBar(), PCB_BASE_FRAME::UpdateStatusBar(), GERBVIEW_FRAME::UpdateStatusBar(), PCB_EDIT_FRAME::UpdateTrackWidthSelectBox(), and PCB_EDIT_FRAME::UpdateViaSizeSelectBox().

◆ ValueFromString()

long long int ValueFromString ( EDA_UNITS_T  aUnits,
const wxString &  aTextValue,
bool  aUseMils = false 
)

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

Parameters
aUnitsThe units of aTextValue.
aTextValueA reference to a wxString object containing the string to convert.
aUseMilsIndicates mils should be used for imperial units (inches).
Returns
The string from Value, according to units (inch, mm ...) for display,

Definition at line 427 of file base_units.cpp.

428 {
429  double value = DoubleValueFromString( aUnits, aTextValue, aUseMils );
430  return KiROUND<double, long long int>( value );
431 }
double DoubleValueFromString(EDA_UNITS_T aUnits, const wxString &aTextValue, bool aUseMils)
Function DoubleValueFromString converts aTextValue to a double.
Definition: base_units.cpp:314

References DoubleValueFromString().

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