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