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 <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 IU_TO_MILS( x ) ( x / IU_PER_MILS )
49 #define MM_TO_IU( x ) ( x * IU_PER_MM )
50 #define IN_TO_IU( x ) ( x * IU_PER_MILS * 1000 )
51 #define MILS_TO_IU( x ) ( x * IU_PER_MILS )
52 #else
53 #error "Cannot resolve internal units due to no definition of EESCHEMA, CVPCB or PCBNEW."
54 #endif
55 
56 
57 // Helper function to print a float number without using scientific notation
58 // and no trailing 0
59 // So we cannot always just use the %g or the %f format to print a fp number
60 // this helper function uses the %f format when needed, or %g when %f is
61 // not well working and then removes trailing 0
62 
63 std::string Double2Str( double aValue )
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 }
91 
92 
93 double To_User_Unit( EDA_UNITS_T aUnit, double aValue, bool aUseMils )
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 }
113 
114 /* Convert a value to a string using double notation.
115  * For readability, the mantissa has 0, 1, 3 or 4 digits, depending on units
116  * for unit = inch the mantissa has 3 digits (Eeschema) or 4 digits
117  * for unit = mil the mantissa has 0 digits (Eeschema) or 1 digits
118  * for unit = mm the mantissa has 3 digits (Eeschema) or 4 digits
119  * Should be used only to display info in status,
120  * but not in dialogs, because 4 digits only
121  * could truncate the actual value
122  */
123 
124 // A lower-precision (for readability) version of StringFromValue()
125 wxString MessageTextFromValue( EDA_UNITS_T aUnits, int aValue, bool aUseMils )
126 {
127  return MessageTextFromValue( aUnits, (double) aValue, aUseMils );
128 }
129 
130 
131 // A lower-precision (for readability) version of StringFromValue()
132 wxString MessageTextFromValue( EDA_UNITS_T aUnits, double aValue, bool aUseMils )
133 {
134  wxString text;
135  const wxChar* format;
136  double value = To_User_Unit( aUnits, aValue, aUseMils );
137 
138  if( aUnits == INCHES )
139  {
140  if( aUseMils )
141  {
142 #if defined( EESCHEMA )
143  format = wxT( "%.0f" );
144 #else
145  format = wxT( "%.1f" );
146 #endif
147  }
148  else
149  {
150 #if defined( EESCHEMA )
151  format = wxT( "%.3f" );
152 #else
153  format = wxT( "%.4f" );
154 #endif
155  }
156  }
157  else
158  {
159 #if defined( EESCHEMA )
160  format = wxT( "%.2f" );
161 #else
162  format = wxT( "%.3f" );
163 #endif
164  }
165 
166  text.Printf( format, value );
167  text += " ";
168 
169  text += GetAbbreviatedUnitsLabel( aUnits, aUseMils );
170 
171  return text;
172 }
173 
174 /* Remove trailing 0 from a string containing a converted float number.
175  * the trailing 0 are removed if the mantissa has more
176  * than aTrailingZeroAllowed digits and some trailing 0
177  */
178 void StripTrailingZeros( wxString& aStringValue, unsigned aTrailingZeroAllowed )
179 {
180  struct lconv * lc = localeconv();
181  char sep = lc->decimal_point[0];
182  unsigned sep_pos = aStringValue.Find( sep );
183 
184  if( sep_pos > 0 )
185  {
186  // We want to keep at least aTrailingZeroAllowed digits after the separator
187  unsigned min_len = sep_pos + aTrailingZeroAllowed + 1;
188 
189  while( aStringValue.Len() > min_len )
190  {
191  if( aStringValue.Last() == '0' )
192  aStringValue.RemoveLast();
193  else
194  break;
195  }
196  }
197 }
198 
199 
200 /* Convert a value to a string using double notation.
201  * For readability, the mantissa has 3 or more digits,
202  * the trailing 0 are removed if the mantissa has more than 3 digits
203  * and some trailing 0
204  * This function should be used to display values in dialogs because a value
205  * entered in mm (for instance 2.0 mm) could need up to 8 digits mantissa
206  * if displayed in inch to avoid truncation or rounding made just by the printf function.
207  * otherwise the actual value is rounded when read from dialog and converted
208  * in internal units, and therefore modified.
209  */
210 wxString StringFromValue( EDA_UNITS_T aUnits, int aValue, bool aAddUnitSymbol, bool aUseMils )
211 {
212  double value_to_print = To_User_Unit( aUnits, aValue, aUseMils );
213 
214 #if defined( EESCHEMA )
215  wxString stringValue = wxString::Format( wxT( "%.3f" ), value_to_print );
216 
217  // Strip trailing zeros. However, keep at least 3 digits in mantissa
218  // For readability
219  StripTrailingZeros( stringValue, 3 );
220 
221 #else
222 
223  char buf[50];
224  int len;
225 
226  if( value_to_print != 0.0 && fabs( value_to_print ) <= 0.0001 )
227  {
228  len = sprintf( buf, "%.10f", value_to_print );
229 
230  while( --len > 0 && buf[len] == '0' )
231  buf[len] = '\0';
232 
233  if( buf[len]=='.' || buf[len]==',' )
234  buf[len] = '\0';
235  else
236  ++len;
237  }
238  else
239  {
240  if( aUnits == INCHES && aUseMils )
241  len = sprintf( buf, "%.7g", value_to_print );
242  else
243  len = sprintf( buf, "%.10g", value_to_print );
244  }
245 
246  wxString stringValue( buf, wxConvUTF8 );
247 
248 #endif
249 
250  if( aAddUnitSymbol )
251  {
252  switch( aUnits )
253  {
254  case INCHES:
255  if( aUseMils )
256  stringValue += wxT( " mils" );
257  else
258  stringValue += wxT( " in" );
259  break;
260 
261  case MILLIMETRES:
262  stringValue += wxT( " mm" );
263  break;
264 
265  case DEGREES:
266  stringValue += wxT( " deg" );
267  break;
268 
269  case UNSCALED_UNITS:
270  break;
271  }
272  }
273 
274  return stringValue;
275 }
276 
277 
278 double From_User_Unit( EDA_UNITS_T aUnits, double aValue, bool aUseMils )
279 {
280  switch( aUnits )
281  {
282  case MILLIMETRES:
283  return MM_TO_IU( aValue );
284 
285  case INCHES:
286  if( aUseMils )
287  return MILS_TO_IU( aValue );
288  else
289  return IN_TO_IU( aValue );
290 
291  case DEGREES:
292  // Convert to "decidegrees"
293  return aValue * 10;
294 
295  default:
296  case UNSCALED_UNITS:
297  return aValue;
298  }
299 }
300 
301 
302 double DoubleValueFromString( EDA_UNITS_T aUnits, const wxString& aTextValue, bool aUseMils )
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  aUseMils = false;
345  }
346  else if( unit == wxT( "mm" ) )
347  {
348  aUnits = MILLIMETRES;
349  }
350  else if( unit == wxT( "mi" ) || unit == wxT( "th" ) ) // "mils" or "thou"
351  {
352  aUnits = INCHES;
353  aUseMils = true;
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, aUseMils );
365 
366  return value;
367 }
368 
369 
370 void FetchUnitsFromString( const wxString& aTextValue, EDA_UNITS_T& aUnits, bool& aUseMils )
371 {
372  wxString buf( aTextValue.Strip( wxString::both ) );
373  unsigned brk_point = 0;
374 
375  while( brk_point < buf.Len() )
376  {
377  wxChar c = buf[brk_point];
378 
379  if( !( (c >= '0' && c <='9') || (c == '.') || (c == ',') || (c == '-') || (c == '+') ) )
380  break;
381 
382  ++brk_point;
383  }
384 
385  // Check the unit designator (2 ch significant)
386  wxString unit( buf.Mid( brk_point ).Strip( wxString::leading ).Left( 2 ).Lower() );
387 
388  if( unit == wxT( "in" ) || unit == wxT( "\"" ) )
389  {
390  aUnits = INCHES;
391  aUseMils = false;
392  }
393  else if( unit == wxT( "mm" ) )
394  {
395  aUnits = MILLIMETRES;
396  }
397  else if( unit == wxT( "mi" ) || unit == wxT( "th" ) ) // "mils" or "thou"
398  {
399  aUnits = INCHES;
400  aUseMils = true;
401  }
402  else if( unit == wxT( "de" ) || unit == wxT( "ra" ) ) // "deg" or "rad"
403  {
404  aUnits = DEGREES;
405  }
406 }
407 
408 
409 int ValueFromString( EDA_UNITS_T aUnits, const wxString& aTextValue, bool aUseMils )
410 {
411  double value = DoubleValueFromString( aUnits, aTextValue, aUseMils );
412  return KiROUND( value );
413 }
414 
415 
421 wxString AngleToStringDegrees( double aAngle )
422 {
423  wxString text;
424 
425  text.Printf( wxT( "%.3f" ), aAngle/10.0 );
426  StripTrailingZeros( text, 1 );
427 
428  return text;
429 }
430 
431 
432 wxString GetAbbreviatedUnitsLabel( EDA_UNITS_T aUnit, bool aUseMils )
433 {
434  switch( aUnit )
435  {
436  case INCHES:
437  if( aUseMils )
438  return _( "mils" );
439  else
440  return _( "in" );
441 
442  case MILLIMETRES:
443  return _( "mm" );
444 
445  case UNSCALED_UNITS:
446  return wxEmptyString;
447 
448  case DEGREES:
449  return _( "deg" );
450 
451  default:
452  return wxT( "??" );
453  }
454 }
455 
static int KiROUND(double v)
Round a floating point number to an integer using "round halfway cases away from zero".
Definition: common.h:120
Implementation of conversion functions that require both schematic and board internal units...
std::string Double2Str(double aValue)
Helper function Double2Str to print a float number without using scientific notation and no trailing ...
Definition: base_units.cpp:63
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:278
int ValueFromString(EDA_UNITS_T aUnits, const wxString &aTextValue, bool aUseMils)
Function ValueFromString converts aTextValue in aUnits to internal units used by the application...
Definition: base_units.cpp:409
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: base_units.cpp:370
This file contains miscellaneous commonly used macros and functions.
wxString MessageTextFromValue(EDA_UNITS_T aUnits, int aValue, bool aUseMils)
Definition: base_units.cpp:125
void StripTrailingZeros(wxString &aStringValue, unsigned aTrailingZeroAllowed)
Function StripTrailingZeros Remove trailing 0 from a string containing a converted float number...
Definition: base_units.cpp:178
wxString AngleToStringDegrees(double aAngle)
Function AngleToStringDegrees is a helper to convert the double aAngle (in internal unit) to a string...
Definition: base_units.cpp:421
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:160
double DoubleValueFromString(EDA_UNITS_T aUnits, const wxString &aTextValue, bool aUseMils)
Function DoubleValueFromString converts aTextValue to a double.
Definition: base_units.cpp:302
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.
wxString StringFromValue(EDA_UNITS_T aUnits, int aValue, bool aAddUnitSymbol, bool aUseMils)
Function StringFromValue returns the string from aValue according to units (inch, mm ...
Definition: base_units.cpp:210
Basic classes for most KiCad items.
EDA_UNITS_T
Definition: common.h:159
wxString GetAbbreviatedUnitsLabel(EDA_UNITS_T aUnit, bool aUseMils)
Get the units string for a given units type.
Definition: base_units.cpp:432