KiCad PCB EDA Suite
base_units.cpp
Go to the documentation of this file.
1 /*
2  * This program source code file is part of KiCad, a free EDA CAD application.
3  *
4  * Copyright (C) 2012 CERN
5  * Copyright (C) 1992-2011 KiCad Developers, see change_log.txt for contributors.
6  *
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, you may find one here:
20  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
21  * or you may search the http://www.gnu.org website for the version 2 license,
22  * or you may write to the Free Software Foundation, Inc.,
23  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24  */
25 
37 #include <macros.h>
38 #include <base_struct.h>
39 #include <class_title_block.h>
40 #include <common.h>
41 #include <base_units.h>
43 
44 
45 #if defined( PCBNEW ) || defined( CVPCB ) || defined( EESCHEMA ) || defined( GERBVIEW ) || defined( PL_EDITOR )
46 #define IU_TO_MM( x ) ( x / IU_PER_MM )
47 #define IU_TO_IN( x ) ( x / IU_PER_MILS / 1000 )
48 #define MM_TO_IU( x ) ( x * IU_PER_MM )
49 #define IN_TO_IU( x ) ( x * IU_PER_MILS * 1000 )
50 #else
51 #error "Cannot resolve internal units due to no definition of EESCHEMA, CVPCB or PCBNEW."
52 #endif
53 
54 
55 // Helper function to print a float number without using scientific notation
56 // and no trailing 0
57 // So we cannot always just use the %g or the %f format to print a fp number
58 // this helper function uses the %f format when needed, or %g when %f is
59 // not well working and then removes trailing 0
60 
61 std::string Double2Str( double aValue )
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 }
89 
90 
91 double To_User_Unit( EDA_UNITS_T aUnit, double aValue )
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 }
108 
109 /* Convert a value to a string using double notation.
110  * For readability, the mantissa has 0, 1, 3 or 4 digits, depending on units
111  * for unit = inch the mantissa has 3 digits (Eeschema) or 4 digits
112  * for unit = mil the mantissa has 0 digits (Eeschema) or 1 digits
113  * for unit = mm the mantissa has 3 digits (Eeschema) or 4 digits
114  * Should be used only to display info in status,
115  * but not in dialogs, because 4 digits only
116  * could truncate the actual value
117  */
118 wxString CoordinateToString( int aValue, bool aConvertToMils )
119 {
120  return LengthDoubleToString( (double) aValue, aConvertToMils );
121 }
122 
123 wxString LengthDoubleToString( double aValue, bool aConvertToMils )
124 {
125  wxString text;
126  const wxChar* format;
127  double value = To_User_Unit( g_UserUnit, aValue );
128 
129  if( g_UserUnit == INCHES )
130  {
131  if( aConvertToMils )
132  {
133 #if defined( EESCHEMA )
134  format = wxT( "%.0f" );
135 #else
136  format = wxT( "%.1f" );
137 #endif
138  value *= 1000;
139  }
140  else
141  {
142 #if defined( EESCHEMA )
143  format = wxT( "%.3f" );
144 #else
145  format = wxT( "%.4f" );
146 #endif
147  }
148  }
149  else
150  {
151 #if defined( EESCHEMA )
152  format = wxT( "%.2f" );
153 #else
154  format = wxT( "%.3f" );
155 #endif
156  }
157 
158  text.Printf( format, value );
159 
160  if( g_UserUnit == INCHES )
161  text += ( aConvertToMils ) ? _( " mils" ) : _( " in" );
162  else
163  text += _( " mm" );
164 
165  return text;
166 }
167 
168 /* Remove trailing 0 from a string containing a converted float number.
169  * the trailing 0 are removed if the mantissa has more
170  * than aTrailingZeroAllowed digits and some trailing 0
171  */
172 void StripTrailingZeros( wxString& aStringValue, unsigned aTrailingZeroAllowed )
173 {
174  struct lconv * lc = localeconv();
175  char sep = lc->decimal_point[0];
176  unsigned sep_pos = aStringValue.Find( sep );
177 
178  if( sep_pos > 0 )
179  {
180  // We want to keep at least aTrailingZeroAllowed digits after the separator
181  unsigned min_len = sep_pos + aTrailingZeroAllowed + 1;
182 
183  while( aStringValue.Len() > min_len )
184  {
185  if( aStringValue.Last() == '0' )
186  aStringValue.RemoveLast();
187  else
188  break;
189  }
190  }
191 }
192 
193 
194 /* Convert a value to a string using double notation.
195  * For readability, the mantissa has 3 or more digits,
196  * the trailing 0 are removed if the mantissa has more than 3 digits
197  * and some trailing 0
198  * This function should be used to display values in dialogs because a value
199  * entered in mm (for instance 2.0 mm) could need up to 8 digits mantissa
200  * if displayed in inch to avoid truncation or rounding made just by the printf function.
201  * otherwise the actual value is rounded when read from dialog and converted
202  * in internal units, and therefore modified.
203  */
204 wxString StringFromValue( EDA_UNITS_T aUnit, int aValue, bool aAddUnitSymbol )
205 {
206  double value_to_print = To_User_Unit( aUnit, aValue );
207 
208 #if defined( EESCHEMA )
209  wxString stringValue = wxString::Format( wxT( "%.3f" ), value_to_print );
210 
211  // Strip trailing zeros. However, keep at least 3 digits in mantissa
212  // For readability
213  StripTrailingZeros( stringValue, 3 );
214 
215 #else
216 
217  char buf[50];
218  int len;
219 
220  if( value_to_print != 0.0 && fabs( value_to_print ) <= 0.0001 )
221  {
222  len = sprintf( buf, "%.10f", value_to_print );
223 
224  while( --len > 0 && buf[len] == '0' )
225  buf[len] = '\0';
226 
227  if( buf[len]=='.' || buf[len]==',' )
228  buf[len] = '\0';
229  else
230  ++len;
231  }
232  else
233  {
234  len = sprintf( buf, "%.10g", value_to_print );
235  }
236 
237  wxString stringValue( buf, wxConvUTF8 );
238 
239 #endif
240 
241  if( aAddUnitSymbol )
242  {
243  switch( aUnit )
244  {
245  case INCHES:
246  stringValue += _( " \"" );
247  break;
248 
249  case MILLIMETRES:
250  stringValue += _( " mm" );
251  break;
252 
253  case DEGREES:
254  stringValue += _( " deg" );
255  break;
256 
257  case UNSCALED_UNITS:
258  break;
259  }
260  }
261 
262  return stringValue;
263 }
264 
265 
266 void PutValueInLocalUnits( wxTextCtrl& aTextCtr, int aValue )
267 {
268  wxString msg = StringFromValue( g_UserUnit, aValue );
269 
270  aTextCtr.SetValue( msg );
271 }
272 
273 
274 double From_User_Unit( EDA_UNITS_T aUnit, double aValue )
275 {
276  double value;
277 
278  switch( aUnit )
279  {
280  case MILLIMETRES:
281  value = MM_TO_IU( aValue );
282  break;
283 
284  case INCHES:
285  value = IN_TO_IU( aValue );
286  break;
287 
288  case DEGREES:
289  // Convert to "decidegrees"
290  value = aValue * 10;
291  break;
292 
293  default:
294  case UNSCALED_UNITS:
295  value = aValue;
296  }
297 
298  return value;
299 }
300 
301 
302 double DoubleValueFromString( EDA_UNITS_T aUnits, const wxString& aTextValue )
303 {
304  double value;
305  double dtmp = 0;
306 
307  // Acquire the 'right' decimal point separator
308  const struct lconv* lc = localeconv();
309 
310  wxChar decimal_point = lc->decimal_point[0];
311  wxString buf( aTextValue.Strip( wxString::both ) );
312 
313  // Convert the period in decimal point
314  buf.Replace( wxT( "." ), wxString( decimal_point, 1 ) );
315 
316  // Find the end of the numeric part
317  unsigned brk_point = 0;
318 
319  while( brk_point < buf.Len() )
320  {
321  wxChar ch = buf[brk_point];
322 
323  if( !( (ch >= '0' && ch <='9') || (ch == decimal_point) || (ch == '-') || (ch == '+') ) )
324  {
325  break;
326  }
327 
328  ++brk_point;
329  }
330 
331  // Extract the numeric part
332  buf.Left( brk_point );
333 
334  buf.ToDouble( &dtmp );
335 
336  // Check the optional unit designator (2 ch significant)
337  wxString unit( buf.Mid( brk_point ).Strip( wxString::leading ).Left( 2 ).Lower() );
338 
339  if( aUnits == INCHES || aUnits == MILLIMETRES )
340  {
341  if( unit == wxT( "in" ) || unit == wxT( "\"" ) )
342  {
343  aUnits = INCHES;
344  }
345  else if( unit == wxT( "mm" ) )
346  {
347  aUnits = MILLIMETRES;
348  }
349  else if( unit == wxT( "mi" ) || unit == wxT( "th" ) ) // Mils or thous
350  {
351  aUnits = INCHES;
352  dtmp /= 1000;
353  }
354  }
355  else if( aUnits == DEGREES )
356  {
357  if( unit == wxT( "ra" ) ) // Radians
358  {
359  dtmp *= 180.0f / M_PI;
360  }
361  }
362 
363  value = From_User_Unit( aUnits, dtmp );
364 
365  return value;
366 }
367 
368 
369 int ValueFromString( EDA_UNITS_T aUnits, const wxString& aTextValue )
370 {
371  double value = DoubleValueFromString( aUnits, aTextValue );
372  return KiROUND( value );
373 }
374 
375 
376 int ValueFromString( const wxString& aTextValue )
377 {
378  int value;
379 
380  value = ValueFromString( g_UserUnit, aTextValue);
381 
382  return value;
383 }
384 
385 int ValueFromTextCtrl( const wxTextCtrl& aTextCtr )
386 {
387  int value;
388  wxString msg = aTextCtr.GetValue();
389  NumericEvaluator eval;
390 
391  if( eval.process( msg.mb_str() ) )
392  msg = wxString::FromUTF8( eval.result() );
393 
394  value = ValueFromString( g_UserUnit, msg );
395 
396  return value;
397 }
398 
399 
400 wxString& operator <<( wxString& aString, const wxPoint& aPos )
401 {
402  aString << wxT( "@ (" ) << CoordinateToString( aPos.x );
403  aString << wxT( "," ) << CoordinateToString( aPos.y );
404  aString << wxT( ")" );
405 
406  return aString;
407 }
408 
414 wxString AngleToStringDegrees( double aAngle )
415 {
416  wxString text;
417 
418  text.Printf( wxT( "%.3f" ), aAngle/10.0 );
419  StripTrailingZeros( text, 1 );
420 
421  return text;
422 }
423 
424 
425 wxString ReturnUnitSymbol( EDA_UNITS_T aUnit, const wxString& formatString )
426 {
427  wxString tmp;
428  wxString label;
429 
430  switch( aUnit )
431  {
432  case INCHES:
433  tmp = _( "\"" );
434  break;
435 
436  case MILLIMETRES:
437  tmp = _( "mm" );
438  break;
439 
440  case UNSCALED_UNITS:
441  break;
442 
443  case DEGREES:
444  wxASSERT( false );
445  break;
446  }
447 
448  if( formatString.IsEmpty() )
449  return tmp;
450 
451  label.Printf( formatString, GetChars( tmp ) );
452 
453  return label;
454 }
455 
456 
457 wxString GetUnitsLabel( EDA_UNITS_T aUnit )
458 {
459  wxString label;
460 
461  switch( aUnit )
462  {
463  case INCHES:
464  label = _( "inches" );
465  break;
466 
467  case MILLIMETRES:
468  label = _( "millimeters" );
469  break;
470 
471  case UNSCALED_UNITS:
472  label = _( "units" );
473  break;
474 
475  case DEGREES:
476  label = _( "degrees" );
477  break;
478  }
479 
480  return label;
481 }
482 
483 
485 {
486  wxString label;
487 
488  switch( aUnit )
489  {
490  case INCHES:
491  label = _( "in" );
492  break;
493 
494  case MILLIMETRES:
495  label = _( "mm" );
496  break;
497 
498  case UNSCALED_UNITS:
499  break;
500 
501  case DEGREES:
502  label = _( "deg" );
503  break;
504 
505  default:
506  label = wxT( "??" );
507  break;
508  }
509 
510  return label;
511 }
512 
513 
514 void AddUnitSymbol( wxStaticText& Stext, EDA_UNITS_T aUnit )
515 {
516  wxString msg = Stext.GetLabel();
517 
518  msg += ReturnUnitSymbol( aUnit );
519 
520  Stext.SetLabel( msg );
521 }
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
static int KiROUND(double v)
KiROUND rounds a floating point number to an int using "round halfway cases away from zero"...
Definition: common.h:106
Implementation of conversion functions that require both schematic and board internal units...
wxString ReturnUnitSymbol(EDA_UNITS_T aUnit, const wxString &formatString)
Returns the units symbol.
Definition: base_units.cpp:425
wxString GetUnitsLabel(EDA_UNITS_T aUnit)
Get a human readable units string.
Definition: base_units.cpp:457
wxString StringFromValue(EDA_UNITS_T aUnit, int aValue, bool aAddUnitSymbol)
Function StringFromValue returns the string from aValue according to units (inch, mm ...
Definition: base_units.cpp:204
std::string Double2Str(double aValue)
Helper function Double2Str to print a float number without using scientific notation and no trailing ...
Definition: base_units.cpp:61
bool process(const char *s)
This file contains miscellaneous commonly used macros and functions.
void StripTrailingZeros(wxString &aStringValue, unsigned aTrailingZeroAllowed)
Function StripTrailingZeros Remove trailing 0 from a string containing a converted float number...
Definition: base_units.cpp:172
wxString AngleToStringDegrees(double aAngle)
Function AngleToStringDegrees is a helper to convert the double aAngle (in internal unit) to a string...
Definition: base_units.cpp:414
Definition: common.h:144
wxString GetAbbreviatedUnitsLabel(EDA_UNITS_T aUnit)
Definition: base_units.cpp:484
int ValueFromString(EDA_UNITS_T aUnits, const wxString &aTextValue)
Function ValueFromString converts aTextValue in aUnits to internal units used by the application...
Definition: base_units.cpp:369
double From_User_Unit(EDA_UNITS_T aUnit, double aValue)
Return in internal units the value "val" given in a real unit such as "in", "mm" or "deg"...
Definition: base_units.cpp:274
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
int ValueFromTextCtrl(const wxTextCtrl &aTextCtr)
Convert the number Value in a string according to the internal units and the selected unit (g_UserUni...
Definition: base_units.cpp:385
EDA_UNITS_T g_UserUnit
Global variables definitions.
Definition: common.cpp:56
void PutValueInLocalUnits(wxTextCtrl &aTextCtr, int aValue)
Function PutValueInLocalUnits converts aValue from internal units to user units and append the units ...
Definition: base_units.cpp:266
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
void AddUnitSymbol(wxStaticText &Stext, EDA_UNITS_T aUnit)
Definition: base_units.cpp:514
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
const char * result() const
The common library.
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
Basic classes for most KiCad items.
double DoubleValueFromString(EDA_UNITS_T aUnits, const wxString &aTextValue)
Function DoubleValueFromString converts aTextValue to a double.
Definition: base_units.cpp:302
EDA_UNITS_T
Definition: common.h:143
wxString & operator<<(wxString &aString, const wxPoint &aPos)
Operator << overload outputs a point to the argument string in a format resembling "@ (x...
Definition: base_units.cpp:400