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>
42 
43 
44 #if defined( PCBNEW ) || defined( CVPCB ) || defined( EESCHEMA ) || defined( GERBVIEW ) || defined( PL_EDITOR )
45 #define IU_TO_MM( x ) ( x / IU_PER_MM )
46 #define IU_TO_IN( x ) ( x / IU_PER_MILS / 1000 )
47 #define MM_TO_IU( x ) ( x * IU_PER_MM )
48 #define IN_TO_IU( x ) ( x * IU_PER_MILS * 1000 )
49 #else
50 #error "Cannot resolve internal units due to no definition of EESCHEMA, CVPCB or PCBNEW."
51 #endif
52 
53 
54 // Helper function to print a float number without using scientific notation
55 // and no trailing 0
56 // So we cannot always just use the %g or the %f format to print a fp number
57 // this helper function uses the %f format when needed, or %g when %f is
58 // not well working and then removes trailing 0
59 
60 std::string Double2Str( double aValue )
61 {
62  char buf[50];
63  int len;
64 
65  if( aValue != 0.0 && fabs( aValue ) <= 0.0001 )
66  {
67  // For these small values, %f works fine,
68  // and %g gives an exponent
69  len = sprintf( buf, "%.16f", aValue );
70 
71  while( --len > 0 && buf[len] == '0' )
72  buf[len] = '\0';
73 
74  if( buf[len] == '.' )
75  buf[len] = '\0';
76  else
77  ++len;
78  }
79  else
80  {
81  // For these values, %g works fine, and sometimes %f
82  // gives a bad value (try aValue = 1.222222222222, with %.16f format!)
83  len = sprintf( buf, "%.16g", aValue );
84  }
85 
86  return std::string( buf, len );
87 }
88 
89 
90 double To_User_Unit( EDA_UNITS_T aUnit, double aValue )
91 {
92  switch( aUnit )
93  {
94  case MILLIMETRES:
95  return IU_TO_MM( aValue );
96 
97  case INCHES:
98  return IU_TO_IN( aValue );
99 
100  case DEGREES:
101  return aValue / 10.0f;
102 
103  default:
104  return aValue;
105  }
106 }
107 
108 /* Convert a value to a string using double notation.
109  * For readability, the mantissa has 0, 1, 3 or 4 digits, depending on units
110  * for unit = inch the mantissa has 3 digits (Eeschema) or 4 digits
111  * for unit = mil the mantissa has 0 digits (Eeschema) or 1 digits
112  * for unit = mm the mantissa has 3 digits (Eeschema) or 4 digits
113  * Should be used only to display info in status,
114  * but not in dialogs, because 4 digits only
115  * could truncate the actual value
116  */
117 wxString CoordinateToString( int aValue, bool aConvertToMils )
118 {
119  return LengthDoubleToString( (double) aValue, aConvertToMils );
120 }
121 
122 wxString LengthDoubleToString( double aValue, bool aConvertToMils )
123 {
124  wxString text;
125  const wxChar* format;
126  double value = To_User_Unit( g_UserUnit, aValue );
127 
128  if( g_UserUnit == INCHES )
129  {
130  if( aConvertToMils )
131  {
132 #if defined( EESCHEMA )
133  format = wxT( "%.0f" );
134 #else
135  format = wxT( "%.1f" );
136 #endif
137  value *= 1000;
138  }
139  else
140  {
141 #if defined( EESCHEMA )
142  format = wxT( "%.3f" );
143 #else
144  format = wxT( "%.4f" );
145 #endif
146  }
147  }
148  else
149  {
150 #if defined( EESCHEMA )
151  format = wxT( "%.2f" );
152 #else
153  format = wxT( "%.3f" );
154 #endif
155  }
156 
157  text.Printf( format, value );
158 
159  if( g_UserUnit == INCHES )
160  text += ( aConvertToMils ) ? _( " mils" ) : _( " in" );
161  else
162  text += _( " mm" );
163 
164  return text;
165 }
166 
167 /* Remove trailing 0 from a string containing a converted float number.
168  * the trailing 0 are removed if the mantissa has more
169  * than aTrailingZeroAllowed digits and some trailing 0
170  */
171 void StripTrailingZeros( wxString& aStringValue, unsigned aTrailingZeroAllowed )
172 {
173  struct lconv * lc = localeconv();
174  char sep = lc->decimal_point[0];
175  unsigned sep_pos = aStringValue.Find( sep );
176 
177  if( sep_pos > 0 )
178  {
179  // We want to keep at least aTrailingZeroAllowed digits after the separator
180  unsigned min_len = sep_pos + aTrailingZeroAllowed + 1;
181 
182  while( aStringValue.Len() > min_len )
183  {
184  if( aStringValue.Last() == '0' )
185  aStringValue.RemoveLast();
186  else
187  break;
188  }
189  }
190 }
191 
192 
193 /* Convert a value to a string using double notation.
194  * For readability, the mantissa has 3 or more digits,
195  * the trailing 0 are removed if the mantissa has more than 3 digits
196  * and some trailing 0
197  * This function should be used to display values in dialogs because a value
198  * entered in mm (for instance 2.0 mm) could need up to 8 digits mantissa
199  * if displayed in inch to avoid truncation or rounding made just by the printf function.
200  * otherwise the actual value is rounded when read from dialog and converted
201  * in internal units, and therefore modified.
202  */
203 wxString StringFromValue( EDA_UNITS_T aUnit, int aValue, bool aAddUnitSymbol )
204 {
205  double value_to_print = To_User_Unit( aUnit, aValue );
206 
207 #if defined( EESCHEMA )
208  wxString stringValue = wxString::Format( wxT( "%.3f" ), value_to_print );
209 
210  // Strip trailing zeros. However, keep at least 3 digits in mantissa
211  // For readability
212  StripTrailingZeros( stringValue, 3 );
213 
214 #else
215 
216  char buf[50];
217  int len;
218 
219  if( value_to_print != 0.0 && fabs( value_to_print ) <= 0.0001 )
220  {
221  len = sprintf( buf, "%.10f", value_to_print );
222 
223  while( --len > 0 && buf[len] == '0' )
224  buf[len] = '\0';
225 
226  if( buf[len]=='.' || buf[len]==',' )
227  buf[len] = '\0';
228  else
229  ++len;
230  }
231  else
232  {
233  len = sprintf( buf, "%.10g", value_to_print );
234  }
235 
236  wxString stringValue( buf, wxConvUTF8 );
237 
238 #endif
239 
240  if( aAddUnitSymbol )
241  {
242  switch( aUnit )
243  {
244  case INCHES:
245  stringValue += _( " \"" );
246  break;
247 
248  case MILLIMETRES:
249  stringValue += _( " mm" );
250  break;
251 
252  case DEGREES:
253  stringValue += _( " deg" );
254  break;
255 
256  case UNSCALED_UNITS:
257  break;
258  }
259  }
260 
261  return stringValue;
262 }
263 
264 
265 void PutValueInLocalUnits( wxTextCtrl& aTextCtr, int aValue )
266 {
267  wxString msg = StringFromValue( g_UserUnit, aValue );
268 
269  aTextCtr.SetValue( msg );
270 }
271 
272 
273 double From_User_Unit( EDA_UNITS_T aUnit, double aValue )
274 {
275  double value;
276 
277  switch( aUnit )
278  {
279  case MILLIMETRES:
280  value = MM_TO_IU( aValue );
281  break;
282 
283  case INCHES:
284  value = IN_TO_IU( aValue );
285  break;
286 
287  case DEGREES:
288  // Convert to "decidegrees"
289  value = aValue * 10;
290  break;
291 
292  default:
293  case UNSCALED_UNITS:
294  value = aValue;
295  }
296 
297  return value;
298 }
299 
300 
301 double DoubleValueFromString( EDA_UNITS_T aUnits, const wxString& aTextValue )
302 {
303  double value;
304  double dtmp = 0;
305 
306  // Acquire the 'right' decimal point separator
307  const struct lconv* lc = localeconv();
308 
309  wxChar decimal_point = lc->decimal_point[0];
310  wxString buf( aTextValue.Strip( wxString::both ) );
311 
312  // Convert the period in decimal point
313  buf.Replace( wxT( "." ), wxString( decimal_point, 1 ) );
314 
315  // Find the end of the numeric part
316  unsigned brk_point = 0;
317 
318  while( brk_point < buf.Len() )
319  {
320  wxChar ch = buf[brk_point];
321 
322  if( !( (ch >= '0' && ch <='9') || (ch == decimal_point) || (ch == '-') || (ch == '+') ) )
323  {
324  break;
325  }
326 
327  ++brk_point;
328  }
329 
330  // Extract the numeric part
331  buf.Left( brk_point );
332 
333  buf.ToDouble( &dtmp );
334 
335  // Check the optional unit designator (2 ch significant)
336  wxString unit( buf.Mid( brk_point ).Strip( wxString::leading ).Left( 2 ).Lower() );
337 
338  if( aUnits == INCHES || aUnits == MILLIMETRES )
339  {
340  if( unit == wxT( "in" ) || unit == wxT( "\"" ) )
341  {
342  aUnits = INCHES;
343  }
344  else if( unit == wxT( "mm" ) )
345  {
346  aUnits = MILLIMETRES;
347  }
348  else if( unit == wxT( "mi" ) || unit == wxT( "th" ) ) // Mils or thous
349  {
350  aUnits = INCHES;
351  dtmp /= 1000;
352  }
353  }
354  else if( aUnits == DEGREES )
355  {
356  if( unit == wxT( "ra" ) ) // Radians
357  {
358  dtmp *= 180.0f / M_PI;
359  }
360  }
361 
362  value = From_User_Unit( aUnits, dtmp );
363 
364  return value;
365 }
366 
367 
368 int ValueFromString( EDA_UNITS_T aUnits, const wxString& aTextValue )
369 {
370  double value = DoubleValueFromString( aUnits, aTextValue );
371  return KiROUND( value );
372 }
373 
374 
375 int ValueFromString( const wxString& aTextValue )
376 {
377  int value;
378 
379  value = ValueFromString( g_UserUnit, aTextValue);
380 
381  return value;
382 }
383 
384 int ValueFromTextCtrl( const wxTextCtrl& aTextCtr )
385 {
386  int value;
387  wxString msg = aTextCtr.GetValue();
388 
389  value = ValueFromString( g_UserUnit, msg );
390 
391  return value;
392 }
393 
394 
395 wxString& operator <<( wxString& aString, const wxPoint& aPos )
396 {
397  aString << wxT( "@ (" ) << CoordinateToString( aPos.x );
398  aString << wxT( "," ) << CoordinateToString( aPos.y );
399  aString << wxT( ")" );
400 
401  return aString;
402 }
403 
409 wxString AngleToStringDegrees( double aAngle )
410 {
411  wxString text;
412 
413  text.Printf( wxT( "%.3f" ), aAngle/10.0 );
414  StripTrailingZeros( text, 1 );
415 
416  return text;
417 }
418 
419 
420 wxString ReturnUnitSymbol( EDA_UNITS_T aUnit, const wxString& formatString )
421 {
422  wxString tmp;
423  wxString label;
424 
425  switch( aUnit )
426  {
427  case INCHES:
428  tmp = _( "\"" );
429  break;
430 
431  case MILLIMETRES:
432  tmp = _( "mm" );
433  break;
434 
435  case UNSCALED_UNITS:
436  break;
437 
438  case DEGREES:
439  wxASSERT( false );
440  break;
441  }
442 
443  if( formatString.IsEmpty() )
444  return tmp;
445 
446  label.Printf( formatString, GetChars( tmp ) );
447 
448  return label;
449 }
450 
451 
452 wxString GetUnitsLabel( EDA_UNITS_T aUnit )
453 {
454  wxString label;
455 
456  switch( aUnit )
457  {
458  case INCHES:
459  label = _( "inches" );
460  break;
461 
462  case MILLIMETRES:
463  label = _( "millimeters" );
464  break;
465 
466  case UNSCALED_UNITS:
467  label = _( "units" );
468  break;
469 
470  case DEGREES:
471  label = _( "degrees" );
472  break;
473  }
474 
475  return label;
476 }
477 
478 
480 {
481  wxString label;
482 
483  switch( aUnit )
484  {
485  case INCHES:
486  label = _( "in" );
487  break;
488 
489  case MILLIMETRES:
490  label = _( "mm" );
491  break;
492 
493  case UNSCALED_UNITS:
494  break;
495 
496  case DEGREES:
497  label = _( "deg" );
498  break;
499 
500  default:
501  label = wxT( "??" );
502  break;
503  }
504 
505  return label;
506 }
507 
508 
509 void AddUnitSymbol( wxStaticText& Stext, EDA_UNITS_T aUnit )
510 {
511  wxString msg = Stext.GetLabel();
512 
513  msg += ReturnUnitSymbol( aUnit );
514 
515  Stext.SetLabel( msg );
516 }
wxString CoordinateToString(int aValue, bool aConvertToMils)
Function CoordinateToString is a helper to convert the integer coordinate aValue to a string in inche...
Definition: base_units.cpp:117
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:420
wxString GetUnitsLabel(EDA_UNITS_T aUnit)
Get a human readable units string.
Definition: base_units.cpp:452
wxString StringFromValue(EDA_UNITS_T aUnit, int aValue, bool aAddUnitSymbol)
Function StringFromValue returns the string from aValue according to units (inch, mm ...
Definition: base_units.cpp:203
std::string Double2Str(double aValue)
Helper function Double2Str to print a float number without using scientific notation and no trailing ...
Definition: base_units.cpp:60
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:171
wxString AngleToStringDegrees(double aAngle)
Function AngleToStringDegrees is a helper to convert the double aAngle (in internal unit) to a string...
Definition: base_units.cpp:409
Definition: common.h:145
wxString GetAbbreviatedUnitsLabel(EDA_UNITS_T aUnit)
Definition: base_units.cpp:479
int ValueFromString(EDA_UNITS_T aUnits, const wxString &aTextValue)
Function ValueFromString converts aTextValue in aUnits to internal units used by the application...
Definition: base_units.cpp:368
double From_User_Unit(EDA_UNITS_T aUnit, double aValue)
Return in internal units the value "val" given in a real unit such as "in", "mm" or "deg"...
Definition: base_units.cpp:273
wxString LengthDoubleToString(double aValue, bool aConvertToMils)
Function LengthDoubleToString is a helper to convert the double length aValue to a string in inches...
Definition: base_units.cpp:122
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:384
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:265
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:509
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
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:90
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:301
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:395