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 <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  text += " ";
160 
161  if( g_UserUnit == INCHES )
162  text += ( aConvertToMils ) ? _( "mils" ) : _( "in" );
163  else
164  text += _( "mm" );
165 
166  return text;
167 }
168 
169 /* Remove trailing 0 from a string containing a converted float number.
170  * the trailing 0 are removed if the mantissa has more
171  * than aTrailingZeroAllowed digits and some trailing 0
172  */
173 void StripTrailingZeros( wxString& aStringValue, unsigned aTrailingZeroAllowed )
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 }
193 
194 
195 /* Convert a value to a string using double notation.
196  * For readability, the mantissa has 3 or more digits,
197  * the trailing 0 are removed if the mantissa has more than 3 digits
198  * and some trailing 0
199  * This function should be used to display values in dialogs because a value
200  * entered in mm (for instance 2.0 mm) could need up to 8 digits mantissa
201  * if displayed in inch to avoid truncation or rounding made just by the printf function.
202  * otherwise the actual value is rounded when read from dialog and converted
203  * in internal units, and therefore modified.
204  */
205 wxString StringFromValue( EDA_UNITS_T aUnit, int aValue, bool aAddUnitSymbol )
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 }
265 
266 
267 void PutValueInLocalUnits( wxTextCtrl& aTextCtr, int aValue )
268 {
269  wxString msg = StringFromValue( g_UserUnit, aValue );
270 
271  aTextCtr.SetValue( msg );
272 }
273 
274 
275 double From_User_Unit( EDA_UNITS_T aUnit, double aValue )
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 }
301 
302 
303 double DoubleValueFromString( EDA_UNITS_T aUnits, const wxString& aTextValue )
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 }
368 
369 
370 int ValueFromString( EDA_UNITS_T aUnits, const wxString& aTextValue )
371 {
372  double value = DoubleValueFromString( aUnits, aTextValue );
373  return KiROUND( value );
374 }
375 
376 
377 int ValueFromString( const wxString& aTextValue )
378 {
379  int value;
380 
381  value = ValueFromString( g_UserUnit, aTextValue);
382 
383  return value;
384 }
385 
386 int ValueFromTextCtrl( const wxTextCtrl& aTextCtr )
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 }
399 
400 
401 wxString& operator <<( wxString& aString, const wxPoint& aPos )
402 {
403  aString << wxT( "@ (" ) << CoordinateToString( aPos.x );
404  aString << wxT( "," ) << CoordinateToString( aPos.y );
405  aString << wxT( ")" );
406 
407  return aString;
408 }
409 
415 wxString AngleToStringDegrees( double aAngle )
416 {
417  wxString text;
418 
419  text.Printf( wxT( "%.3f" ), aAngle/10.0 );
420  StripTrailingZeros( text, 1 );
421 
422  return text;
423 }
424 
425 
426 wxString ReturnUnitSymbol( EDA_UNITS_T aUnit, const wxString& formatString )
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 }
456 
457 
458 wxString GetUnitsLabel( EDA_UNITS_T aUnit )
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 }
483 
484 
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 }
513 
514 
515 void AddUnitSymbol( wxStaticText& Stext, EDA_UNITS_T aUnit )
516 {
517  wxString msg = Stext.GetLabel();
518 
519  msg += ReturnUnitSymbol( aUnit );
520 
521  Stext.SetLabel( msg );
522 }
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:107
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:426
wxString GetUnitsLabel(EDA_UNITS_T aUnit)
Get a human readable units string.
Definition: base_units.cpp:458
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
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:173
wxString AngleToStringDegrees(double aAngle)
Function AngleToStringDegrees is a helper to convert the double aAngle (in internal unit) to a string...
Definition: base_units.cpp:415
Definition: common.h:145
wxString GetAbbreviatedUnitsLabel(EDA_UNITS_T aUnit)
Definition: base_units.cpp:485
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
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
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:386
EDA_UNITS_T g_UserUnit
Global variables definitions.
Definition: common.cpp:57
void PutValueInLocalUnits(wxTextCtrl &aTextCtr, int aValue)
Function PutValueInLocalUnits converts aValue from internal units to user units and append the units ...
Definition: base_units.cpp:267
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:515
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:303
EDA_UNITS_T
Definition: common.h:144
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:401