KiCad PCB EDA Suite
Loading...
Searching...
No Matches
eda_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 The KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, you may find one here:
18 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19 * or you may search the http://www.gnu.org website for the version 2 license,
20 * or you may write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 */
23
24#include <eda_units.h>
25#include <fmt/core.h>
26#include <math/util.h> // for KiROUND
27#include <macros.h>
28#include <charconv>
29#include <wx/translation.h>
30
31
32static void removeTrailingZeros( wxString& aText )
33{
34 int len = aText.length();
35 int removeLast = 0;
36
37 while( --len > 0 && aText[len] == '0' )
38 removeLast++;
39
40 if( len >= 0 && ( aText[len] == '.' || aText[len] == ',' ) )
41 removeLast++;
42
43 aText = aText.RemoveLast( removeLast );
44}
45
46
48{
49 switch( aUnit )
50 {
52 case EDA_UNITS::MILS:
53 return true;
54
55 default:
56 return false;
57 }
58}
59
60
62{
63 switch( aUnit )
64 {
68 return true;
69
70 default:
71 return false;
72 }
73}
74
75
76int EDA_UNIT_UTILS::Mm2mils( double aVal )
77{
78 return KiROUND( aVal * 1000. / 25.4 );
79}
80
81
82int EDA_UNIT_UTILS::Mils2mm( double aVal )
83{
84 return KiROUND( aVal * 25.4 / 1000. );
85}
86
87
88bool EDA_UNIT_UTILS::FetchUnitsFromString( const wxString& aTextValue, EDA_UNITS& aUnits )
89{
90 wxString buf( aTextValue.Strip( wxString::both ) );
91 unsigned brk_point = 0;
92
93 while( brk_point < buf.Len() )
94 {
95 wxChar c = buf[brk_point];
96
97 if( !( ( c >= '0' && c <= '9' ) || ( c == '.' ) || ( c == ',' ) || ( c == '-' )
98 || ( c == '+' ) ) )
99 break;
100
101 ++brk_point;
102 }
103
104 // Check the unit designator (2 ch significant)
105 wxString unit( buf.Mid( brk_point ).Strip( wxString::leading ).Left( 2 ).Lower() );
106
107 //check for um, μm (µ is MICRO SIGN) and µm (µ is GREEK SMALL LETTER MU) for micrometre
108 if( unit == wxT( "um" ) || unit == wxT( "\u00B5m" ) || unit == wxT( "\u03BCm" ) )
109 aUnits = EDA_UNITS::MICROMETRES;
110 else if( unit == wxT( "mm" ) )
111 aUnits = EDA_UNITS::MILLIMETRES;
112 if( unit == wxT( "cm" ) )
113 aUnits = EDA_UNITS::CENTIMETRES;
114 else if( unit == wxT( "mi" ) || unit == wxT( "th" ) ) // "mils" or "thou"
115 aUnits = EDA_UNITS::MILS;
116 else if( unit == wxT( "in" ) || unit == wxT( "\"" ) )
117 aUnits = EDA_UNITS::INCHES;
118 else if( unit == wxT( "de" ) || unit == wxT( "ra" ) ) // "deg" or "rad"
119 aUnits = EDA_UNITS::DEGREES;
120 else
121 return false;
122
123 return true;
124}
125
126
128{
129 wxString label;
130
131 switch( aUnits )
132 {
133 case EDA_UNITS::MICROMETRES: label = wxT( " \u00B5m" ); break; //00B5 for µ
134 case EDA_UNITS::MILLIMETRES: label = wxT( " mm" ); break;
135 case EDA_UNITS::CENTIMETRES: label = wxT( " cm" ); break;
136 case EDA_UNITS::DEGREES: label = wxT( "°" ); break;
137 case EDA_UNITS::MILS: label = wxT( " mils" ); break;
138 case EDA_UNITS::INCHES: label = wxT( " in" ); break;
139 case EDA_UNITS::PERCENT: label = wxT( "%" ); break;
140 case EDA_UNITS::UNSCALED: break;
141 default: UNIMPLEMENTED_FOR( wxS( "Unknown units" ) ); break;
142 }
143
144 switch( aType )
145 {
146 case EDA_DATA_TYPE::VOLUME: label += wxT( "³" ); break;
147 case EDA_DATA_TYPE::AREA: label += wxT( "²" ); break;
148 case EDA_DATA_TYPE::DISTANCE: break;
149 default: UNIMPLEMENTED_FOR( wxS( "Unknown measurement" ) ); break;
150 }
151
152 return label;
153}
154
155
157{
158 return GetText( aUnits, aType ).Trim( false );
159}
160
161
162std::string EDA_UNIT_UTILS::FormatAngle( const EDA_ANGLE& aAngle )
163{
164 std::string temp = fmt::format( "{:.10g}", aAngle.AsDegrees() );
165
166 return temp;
167}
168
169
170std::string EDA_UNIT_UTILS::FormatInternalUnits( const EDA_IU_SCALE& aIuScale, int aValue )
171{
172 std::string buf;
173 double engUnits = aValue;
174
175 engUnits /= aIuScale.IU_PER_MM;
176
177 if( engUnits != 0.0 && fabs( engUnits ) <= 0.0001 )
178 {
179 buf = fmt::format( "{:.10f}", engUnits );
180
181 // remove trailing zeros
182 while( !buf.empty() && buf[buf.size() - 1] == '0' )
183 {
184 buf.pop_back();
185 }
186
187 // if the value was really small
188 // we may have just stripped all the zeros after the decimal
189 if( buf[buf.size() - 1] == '.' )
190 {
191 buf.pop_back();
192 }
193 }
194 else
195 {
196 buf = fmt::format( "{:.10g}", engUnits );
197 }
198
199 return buf;
200}
201
202
204 const VECTOR2I& aPoint )
205{
206 return FormatInternalUnits( aIuScale, aPoint.x ) + " "
207 + FormatInternalUnits( aIuScale, aPoint.y );
208}
209
210
211#if 0 // No support for std::from_chars on MacOS yet
212
213bool EDA_UNIT_UTILS::ParseInternalUnits( const std::string& aInput, const EDA_IU_SCALE& aIuScale,
214 int& aOut )
215{
216 double value;
217
218 if( std::from_chars( aInput.data(), aInput.data() + aInput.size(), value ).ec != std::errc() )
219 return false;
220
221 aOut = value * aIuScale.IU_PER_MM;
222 return true;
223}
224
225
226bool EDA_UNIT_UTILS::ParseInternalUnits( const std::string& aInput, const EDA_IU_SCALE& aIuScale,
227 VECTOR2I& aOut )
228{
229 size_t pos = aInput.find( ' ' );
230
231 if( pos == std::string::npos )
232 return false;
233
234 std::string first = aInput.substr( 0, pos );
235 std::string second = aInput.substr( pos + 1 );
236
237 VECTOR2I vec;
238
239 if( !ParseInternalUnits( first, aIuScale, vec.x ) )
240 return false;
241
242 if( !ParseInternalUnits( second, aIuScale, vec.y ) )
243 return false;
244
245 aOut = vec;
246
247 return true;
248}
249
250#endif
251
252
253#define IU_TO_MM( x, scale ) ( x / scale.IU_PER_MM )
254#define IU_TO_IN( x, scale ) ( x / scale.IU_PER_MILS / 1000 )
255#define IU_TO_MILS( x, scale ) ( x / scale.IU_PER_MILS )
256#define MM_TO_IU( x, scale ) ( x * scale.IU_PER_MM )
257#define IN_TO_IU( x, scale ) ( x * scale.IU_PER_MILS * 1000 )
258#define MILS_TO_IU( x, scale ) ( x * scale.IU_PER_MILS )
259
260
262 double aValue )
263{
264 switch( aUnit )
265 {
267 return IU_TO_MM( aValue, aIuScale ) * 1000;
268
270 return IU_TO_MM( aValue, aIuScale );
271
273 return IU_TO_MM( aValue, aIuScale ) / 10;
274
275 case EDA_UNITS::MILS:
276 return IU_TO_MILS( aValue, aIuScale );
277
279 return IU_TO_IN( aValue, aIuScale );
280
282 return aValue;
283
284 default:
285 return aValue;
286 }
287}
288
289
291 double aValue, bool aAddUnitsText,
292 EDA_DATA_TYPE aType )
293{
294 double value_to_print = aValue;
295 bool is_eeschema = ( aIuScale.IU_PER_MM == SCH_IU_PER_MM );
296
297 switch( aType )
298 {
300 value_to_print = ToUserUnit( aIuScale, aUnits, value_to_print );
302
304 value_to_print = ToUserUnit( aIuScale, aUnits, value_to_print );
306
308 value_to_print = ToUserUnit( aIuScale, aUnits, value_to_print );
309 break;
310
312 break;
313 }
314
315 const wxChar* format = nullptr;
316
317 switch( aUnits )
318 {
319
320 case EDA_UNITS::MILS:
321 format = is_eeschema ? wxT( "%.3f" ) : wxT( "%.5f" );
322 break;
323
325 format = is_eeschema ? wxT( "%.6f" ) : wxT( "%.8f" );
326 break;
327
329 format = wxT( "%.4f" );
330 break;
331
332 default:
333 format = wxT( "%.10f" );
334 break;
335 }
336
337 wxString text;
338 text.Printf( format, value_to_print );
340
341 if( value_to_print != 0.0 && ( text == wxS( "0" ) || text == wxS( "-0" ) ) )
342 {
343 text.Printf( wxS( "%.10f" ), value_to_print );
345 }
346
347 if( aAddUnitsText )
348 text << EDA_UNIT_UTILS::GetText( aUnits, aType );
349
350 return text;
351}
352
353
354
355// A lower-precision (for readability) version of StringFromValue()
357 int aValue,
358 bool aAddUnitLabel,
359 EDA_DATA_TYPE aType )
360{
361 return MessageTextFromValue( aIuScale, aUnits, double( aValue ), aAddUnitLabel, aType );
362}
363
364
365// A lower-precision (for readability) version of StringFromValue()
367 long long int aValue,
368 bool aAddUnitLabel,
369 EDA_DATA_TYPE aType )
370{
371 return MessageTextFromValue( aIuScale, aUnits, double( aValue ), aAddUnitLabel, aType );
372}
373
374
375wxString EDA_UNIT_UTILS::UI::MessageTextFromValue( EDA_ANGLE aValue, bool aAddUnitLabel )
376{
377 if( aAddUnitLabel )
378 return wxString::Format( wxT( "%.1f°" ), aValue.AsDegrees() );
379 else
380 return wxString::Format( wxT( "%.1f" ), aValue.AsDegrees() );
381}
382
383
384// A lower-precision (for readability) version of StringFromValue()
386 double aValue, bool aAddUnitsText,
387 EDA_DATA_TYPE aType )
388{
389 wxString text;
390 const wxChar* format;
391 double value = aValue;
392 bool is_eeschema = ( aIuScale.IU_PER_MM == SCH_IU_PER_MM );
393
394 switch( aType )
395 {
397 value = ToUserUnit( aIuScale, aUnits, value );
398 // Fall through to continue computation
400
402 value = ToUserUnit( aIuScale, aUnits, value );
403 // Fall through to continue computation
405
407 value = ToUserUnit( aIuScale, aUnits, value );
408 break;
409
411 break;
412 }
413
414 switch( aUnits )
415 {
416 default:
418 format = is_eeschema ? wxT( "%.0f" ) : wxT( "%.1f" );
419 break;
420
422 format = is_eeschema ? wxT( "%.2f" ) : wxT( "%.4f" );
423 break;
424
426 format = is_eeschema ? wxT( "%.3f" ) : wxT( "%.5f" );
427 break;
428
429 case EDA_UNITS::MILS:
430 format = is_eeschema ? wxT( "%.0f" ) : wxT( "%.2f" );
431 break;
432
434 format = is_eeschema ? wxT( "%.3f" ) : wxT( "%.4f" );
435 break;
436
438 // 3 digits in mantissa should be good for rotation in degree
439 format = wxT( "%.3f" );
440 break;
441
443 format = wxT( "%.0f" );
444 break;
445 }
446
447 text.Printf( format, value );
448
449 if( aAddUnitsText )
450 text += EDA_UNIT_UTILS::GetText( aUnits, aType );
451
452 return text;
453}
454
455
457 EDA_UNITS aUnits,
458 const MINOPTMAX<int>& aValue )
459{
460 wxString msg;
461
462 if( aValue.HasMin() && aValue.Min() > 0 )
463 {
464 msg += _( "min" ) + wxS( " " ) + MessageTextFromValue( aIuScale, aUnits, aValue.Min() );
465 }
466
467 if( aValue.HasOpt() )
468 {
469 if( !msg.IsEmpty() )
470 msg += wxS( "; " );
471
472 msg += _( "opt" ) + wxS( " " ) + MessageTextFromValue( aIuScale, aUnits, aValue.Opt() );
473 }
474
475 if( aValue.HasMax() )
476 {
477 if( !msg.IsEmpty() )
478 msg += wxS( "; " );
479
480 msg += _( "max" ) + wxS( " " ) + MessageTextFromValue( aIuScale, aUnits, aValue.Max() );
481 }
482
483 return msg;
484};
485
486
488 double aValue )
489{
490 switch( aUnits )
491 {
493 return MM_TO_IU( aValue / 1000.0, aIuScale );
494
496 return MM_TO_IU( aValue, aIuScale );
497
499 return MM_TO_IU( aValue * 10, aIuScale );
500
501 case EDA_UNITS::MILS:
502 return MILS_TO_IU( aValue, aIuScale );
503
505 return IN_TO_IU( aValue, aIuScale );
506
507 default:
511 return aValue;
512 }
513}
514
515
516double EDA_UNIT_UTILS::UI::DoubleValueFromString( const wxString& aTextValue )
517{
518 double dtmp = 0;
519
520 // Acquire the 'right' decimal point separator
521 const struct lconv* lc = localeconv();
522
523 wxChar decimal_point = lc->decimal_point[0];
524 wxString buf( aTextValue.Strip( wxString::both ) );
525
526 // Convert any entered decimal point separators to the 'right' one
527 buf.Replace( wxT( "." ), wxString( decimal_point, 1 ) );
528 buf.Replace( wxT( "," ), wxString( decimal_point, 1 ) );
529
530 // Find the end of the numeric part
531 unsigned brk_point = 0;
532
533 while( brk_point < buf.Len() )
534 {
535 wxChar ch = buf[brk_point];
536
537 if( !( ( ch >= '0' && ch <= '9' ) || ( ch == decimal_point ) || ( ch == '-' )
538 || ( ch == '+' ) ) )
539 {
540 break;
541 }
542
543 ++brk_point;
544 }
545
546 // Extract the numeric part
547 buf.Left( brk_point ).ToDouble( &dtmp );
548
549 return dtmp;
550}
551
552
554 const wxString& aTextValue, EDA_DATA_TYPE aType )
555{
556 double dtmp = 0;
557
558 // Acquire the 'right' decimal point separator
559 const struct lconv* lc = localeconv();
560
561 wxChar decimal_point = lc->decimal_point[0];
562 wxString buf( aTextValue.Strip( wxString::both ) );
563
564 // Convert any entered decimal point separators to the 'right' one
565 buf.Replace( wxT( "." ), wxString( decimal_point, 1 ) );
566 buf.Replace( wxT( "," ), wxString( decimal_point, 1 ) );
567
568 // Find the end of the numeric part
569 unsigned brk_point = 0;
570
571 while( brk_point < buf.Len() )
572 {
573 wxChar ch = buf[brk_point];
574
575 if( !( (ch >= '0' && ch <= '9') || (ch == decimal_point) || (ch == '-') || (ch == '+') ) )
576 break;
577
578 ++brk_point;
579 }
580
581 // Extract the numeric part
582 buf.Left( brk_point ).ToDouble( &dtmp );
583
584 // Check the optional unit designator (2 ch significant)
585 wxString unit( buf.Mid( brk_point ).Strip( wxString::leading ).Left( 2 ).Lower() );
586
587 if( aUnits == EDA_UNITS::MICROMETRES
588 || aUnits == EDA_UNITS::MILLIMETRES
589 || aUnits == EDA_UNITS::CENTIMETRES
590 || aUnits == EDA_UNITS::MILS
591 || aUnits == EDA_UNITS::INCHES )
592 {
593 //check for um, μm (µ is MICRO SIGN) and µm (µ is GREEK SMALL LETTER MU) for micrometre
594 if( unit == wxT( "um" ) || unit == wxT( "\u00B5m" ) || unit == wxT( "\u03BCm" ) )
595 {
596 aUnits = EDA_UNITS::MICROMETRES;
597 }
598 else if( unit == wxT( "mm" ) )
599 {
600 aUnits = EDA_UNITS::MILLIMETRES;
601 }
602 else if( unit == wxT( "cm" ) )
603 {
604 aUnits = EDA_UNITS::CENTIMETRES;
605 }
606 else if( unit == wxT( "mi" ) || unit == wxT( "th" ) )
607 {
608 aUnits = EDA_UNITS::MILS;
609 }
610 else if( unit == wxT( "in" ) || unit == wxT( "\"" ) )
611 {
612 aUnits = EDA_UNITS::INCHES;
613 }
614 else if( unit == wxT( "oz" ) ) // 1 oz = 1.37 mils
615 {
616 aUnits = EDA_UNITS::MILS;
617 dtmp *= 1.37;
618 }
619 }
620 else if( aUnits == EDA_UNITS::DEGREES )
621 {
622 if( unit == wxT( "ra" ) ) // Radians
623 dtmp *= 180.0f / M_PI;
624 }
625
626 switch( aType )
627 {
629 dtmp = FromUserUnit( aIuScale, aUnits, dtmp );
631
633 dtmp = FromUserUnit( aIuScale, aUnits, dtmp );
635
637 dtmp = FromUserUnit( aIuScale, aUnits, dtmp );
638 break;
639
641 break;
642 }
643
644 return dtmp;
645}
646
647
648long long int EDA_UNIT_UTILS::UI::ValueFromString( const EDA_IU_SCALE& aIuScale, EDA_UNITS aUnits,
649 const wxString& aTextValue, EDA_DATA_TYPE aType )
650{
651 double value = DoubleValueFromString( aIuScale, aUnits, aTextValue, aType );
652
653 return KiROUND<double, long long int>( value );
654}
655
656
657long long int EDA_UNIT_UTILS::UI::ValueFromString( const wxString& aTextValue )
658{
659 double value = DoubleValueFromString( aTextValue );
660
661 return KiROUND<double, long long int>( value );
662}
constexpr double SCH_IU_PER_MM
Schematic internal units 1=100nm.
Definition: base_units.h:72
constexpr BOX2I KiROUND(const BOX2D &aBoxD)
Definition: box2.h:990
double AsDegrees() const
Definition: eda_angle.h:113
T Min() const
Definition: minoptmax.h:33
bool HasMax() const
Definition: minoptmax.h:38
bool HasMin() const
Definition: minoptmax.h:37
T Max() const
Definition: minoptmax.h:34
T Opt() const
Definition: minoptmax.h:35
bool HasOpt() const
Definition: minoptmax.h:39
#define _(s)
#define IU_TO_IN(x, scale)
Definition: eda_units.cpp:254
#define IN_TO_IU(x, scale)
Definition: eda_units.cpp:257
#define IU_TO_MILS(x, scale)
Definition: eda_units.cpp:255
#define MM_TO_IU(x, scale)
Definition: eda_units.cpp:256
#define IU_TO_MM(x, scale)
Definition: eda_units.cpp:253
#define MILS_TO_IU(x, scale)
Definition: eda_units.cpp:258
static void removeTrailingZeros(wxString &aText)
Definition: eda_units.cpp:32
EDA_DATA_TYPE
The type of unit.
Definition: eda_units.h:38
EDA_UNITS
Definition: eda_units.h:46
This file contains miscellaneous commonly used macros and functions.
#define KI_FALLTHROUGH
The KI_FALLTHROUGH macro is to be used when switch statement cases should purposely fallthrough from ...
Definition: macros.h:83
#define UNIMPLEMENTED_FOR(type)
Definition: macros.h:96
KICOMMON_API wxString MessageTextFromValue(const EDA_IU_SCALE &aIuScale, EDA_UNITS aUnits, double aValue, bool aAddUnitsText=true, EDA_DATA_TYPE aType=EDA_DATA_TYPE::DISTANCE)
A helper to convert the double length aValue to a string in inches, millimeters, or unscaled units.
Definition: eda_units.cpp:385
KICOMMON_API double FromUserUnit(const EDA_IU_SCALE &aIuScale, EDA_UNITS aUnit, double aValue)
Return in internal units the value aValue given in a real unit such as "in", "mm",...
Definition: eda_units.cpp:487
KICOMMON_API long long int ValueFromString(const EDA_IU_SCALE &aIuScale, EDA_UNITS aUnits, const wxString &aTextValue, EDA_DATA_TYPE aType=EDA_DATA_TYPE::DISTANCE)
Convert aTextValue in aUnits to internal units used by the application.
Definition: eda_units.cpp:648
KICOMMON_API wxString StringFromValue(const EDA_IU_SCALE &aIuScale, EDA_UNITS aUnits, double aValue, bool aAddUnitsText=false, EDA_DATA_TYPE aType=EDA_DATA_TYPE::DISTANCE)
Return the string from aValue according to aUnits (inch, mm ...) for display.
Definition: eda_units.cpp:290
KICOMMON_API wxString MessageTextFromMinOptMax(const EDA_IU_SCALE &aIuScale, EDA_UNITS aUnits, const MINOPTMAX< int > &aValue)
Definition: eda_units.cpp:456
KICOMMON_API double DoubleValueFromString(const EDA_IU_SCALE &aIuScale, EDA_UNITS aUnits, const wxString &aTextValue, EDA_DATA_TYPE aType=EDA_DATA_TYPE::DISTANCE)
Convert aTextValue to a double.
Definition: eda_units.cpp:553
KICOMMON_API double ToUserUnit(const EDA_IU_SCALE &aIuScale, EDA_UNITS aUnit, double aValue)
Convert aValue in internal units to the appropriate user units defined by aUnit.
Definition: eda_units.cpp:261
KICOMMON_API wxString GetText(EDA_UNITS aUnits, EDA_DATA_TYPE aType=EDA_DATA_TYPE::DISTANCE)
Get the units string for a given units type.
Definition: eda_units.cpp:127
KICOMMON_API bool FetchUnitsFromString(const wxString &aTextValue, EDA_UNITS &aUnits)
Write any unit info found in the string to aUnits.
Definition: eda_units.cpp:88
KICOMMON_API bool IsImperialUnit(EDA_UNITS aUnit)
Definition: eda_units.cpp:47
KICOMMON_API bool IsMetricUnit(EDA_UNITS aUnit)
Definition: eda_units.cpp:61
KICOMMON_API wxString GetLabel(EDA_UNITS aUnits, EDA_DATA_TYPE aType=EDA_DATA_TYPE::DISTANCE)
Get the units string for a given units type.
Definition: eda_units.cpp:156
KICOMMON_API int Mm2mils(double aVal)
Convert mm to mils.
Definition: eda_units.cpp:76
KICOMMON_API std::string FormatInternalUnits(const EDA_IU_SCALE &aIuScale, int aValue)
Converts aValue from internal units to a string appropriate for writing to file.
Definition: eda_units.cpp:170
KICOMMON_API std::string FormatAngle(const EDA_ANGLE &aAngle)
Convert aAngle from board units to a string appropriate for writing to file.
Definition: eda_units.cpp:162
KICOMMON_API int Mils2mm(double aVal)
Convert mils to mm.
Definition: eda_units.cpp:82
const double IU_PER_MM
Definition: base_units.h:76