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, see <https://www.gnu.org/licenses/>.
18 */
19
20#include <eda_units.h>
21#include <fmt/format.h>
22#include <math/util.h> // for KiROUND
23#include <macros.h>
24#include <charconv>
25#include <wx/translation.h>
26
27
28static void removeTrailingZeros( wxString& aText )
29{
30 int len = aText.length();
31 int removeLast = 0;
32
33 while( --len > 0 && aText[len] == '0' )
34 removeLast++;
35
36 if( len >= 0 && ( aText[len] == '.' || aText[len] == ',' ) )
37 removeLast++;
38
39 aText = aText.RemoveLast( removeLast );
40}
41
42
44{
45 switch( aUnit )
46 {
47 case EDA_UNITS::INCH:
48 case EDA_UNITS::MILS:
49 return true;
50
51 default:
52 return false;
53 }
54}
55
56
58{
59 switch( aUnit )
60 {
61 case EDA_UNITS::UM:
62 case EDA_UNITS::MM:
63 case EDA_UNITS::CM:
64 return true;
65
66 default:
67 return false;
68 }
69}
70
71
72int EDA_UNIT_UTILS::Mm2mils( double aVal )
73{
74 return KiROUND( aVal * 1000. / 25.4 );
75}
76
77
78int EDA_UNIT_UTILS::Mils2mm( double aVal )
79{
80 return KiROUND( aVal * 25.4 / 1000. );
81}
82
83
84bool EDA_UNIT_UTILS::FetchUnitsFromString( const wxString& aTextValue, EDA_UNITS& aUnits )
85{
86 wxString buf( aTextValue.Strip( wxString::both ) );
87 unsigned brk_point = 0;
88
89 while( brk_point < buf.Len() )
90 {
91 wxChar c = buf[brk_point];
92
93 if( !( ( c >= '0' && c <= '9' ) || ( c == '.' ) || ( c == ',' ) || ( c == '-' )
94 || ( c == '+' ) ) )
95 break;
96
97 ++brk_point;
98 }
99
100 // Check the unit designator (2 ch significant)
101 wxString unit( buf.Mid( brk_point ).Trim( false ).Left( 2 ).Lower() );
102
103 //check for um, μm (µ is MICRO SIGN) and µm (µ is GREEK SMALL LETTER MU) for micrometre
104 if( unit == wxT( "um" ) || unit == wxT( "\u00B5m" ) || unit == wxT( "\u03BCm" ) )
105 aUnits = EDA_UNITS::UM;
106 else if( unit == wxT( "mm" ) )
107 aUnits = EDA_UNITS::MM;
108 else if( unit == wxT( "cm" ) )
109 aUnits = EDA_UNITS::CM;
110 else if( unit == wxT( "mi" ) || unit == wxT( "th" ) ) // "mils" or "thou"
111 aUnits = EDA_UNITS::MILS;
112 else if( unit == wxT( "in" ) || unit == wxT( "\"" ) )
113 aUnits = EDA_UNITS::INCH;
114 else if( unit == wxT( "de" ) || unit == wxT( "ra" ) ) // "deg" or "rad"
115 aUnits = EDA_UNITS::DEGREES;
116 else if( unit == wxT("fs" ) )
117 aUnits = EDA_UNITS::FS;
118 else if( unit == wxT( "ps" ) )
119 {
120 wxString timeUnit( buf.Mid( brk_point ).Trim( false ).Left( 5 ).Lower() );
121
122 if( timeUnit == wxT( "ps" ) )
123 aUnits = EDA_UNITS::PS;
124 else if( timeUnit == wxT( "ps/in" ) )
125 aUnits = EDA_UNITS::PS_PER_INCH;
126 else if( timeUnit == wxT( "ps/cm" ) )
127 aUnits = EDA_UNITS::PS_PER_CM;
128 else if( timeUnit == wxT( "ps/mm" ) )
129 aUnits = EDA_UNITS::PS_PER_MM;
130 else
131 return false;
132 }
133 else
134 return false;
135
136 return true;
137}
138
139
141{
142 wxString label;
143
144 switch( aUnits )
145 {
146 case EDA_UNITS::UM: label = wxT( " \u00B5m" ); break; //00B5 for µ
147 case EDA_UNITS::MM: label = wxT( " mm" ); break;
148 case EDA_UNITS::CM: label = wxT( " cm" ); break;
149 case EDA_UNITS::DEGREES: label = wxT( "°" ); break;
150 case EDA_UNITS::MILS: label = wxT( " mils" ); break;
151 case EDA_UNITS::INCH: label = wxT( " in" ); break;
152 case EDA_UNITS::PERCENT: label = wxT( "%" ); break;
153 case EDA_UNITS::FS: label = wxT( " fs" ); break;
154 case EDA_UNITS::PS: label = wxT( " ps" ); break;
155 case EDA_UNITS::PS_PER_INCH: label = wxT( " ps/in" ); break;
156 case EDA_UNITS::PS_PER_CM: label = wxT( " ps/cm"); break;
157 case EDA_UNITS::PS_PER_MM: label = wxT( " ps/mm"); break;
158 case EDA_UNITS::UNSCALED: break;
159 default: UNIMPLEMENTED_FOR( wxS( "Unknown units" ) ); break;
160 }
161
162 switch( aType )
163 {
164 case EDA_DATA_TYPE::VOLUME: label += wxT( "³" ); break;
165 case EDA_DATA_TYPE::AREA: label += wxT( "²" ); break;
166 case EDA_DATA_TYPE::DISTANCE: break;
167 case EDA_DATA_TYPE::ANGLE: break;
168 case EDA_DATA_TYPE::TIME: break;
169 case EDA_DATA_TYPE::LENGTH_DELAY: break;
170 default: UNIMPLEMENTED_FOR( wxS( "Unknown measurement" ) ); break;
171 }
172
173 return label;
174}
175
176
178{
179 return GetText( aUnits, aType ).Trim( false );
180}
181
182
183std::string EDA_UNIT_UTILS::FormatAngle( const EDA_ANGLE& aAngle )
184{
185 std::string temp = fmt::format( "{:.10g}", aAngle.AsDegrees() );
186
187 return temp;
188}
189
190
191std::string EDA_UNIT_UTILS::FormatInternalUnits( const EDA_IU_SCALE& aIuScale, const int aValue,
192 const EDA_DATA_TYPE aDataType )
193{
194 std::string buf;
195 double engUnits = aValue;
196
197 engUnits /= GetScaleForInternalUnitType( aIuScale, aDataType );
198
199 if( engUnits != 0.0 && fabs( engUnits ) <= 0.0001 )
200 {
201 buf = fmt::format( "{:.10f}", engUnits );
202
203 // remove trailing zeros
204 while( !buf.empty() && buf[buf.size() - 1] == '0' )
205 {
206 buf.pop_back();
207 }
208
209 // if the value was really small
210 // we may have just stripped all the zeros after the decimal
211 if( buf[buf.size() - 1] == '.' )
212 {
213 buf.pop_back();
214 }
215 }
216 else
217 {
218 buf = fmt::format( "{:.10g}", engUnits );
219 }
220
221 return buf;
222}
223
224
226 const VECTOR2I& aPoint )
227{
228 return FormatInternalUnits( aIuScale, aPoint.x ) + " "
229 + FormatInternalUnits( aIuScale, aPoint.y );
230}
231
232
234{
235 switch( aDataType )
236 {
237 case EDA_DATA_TYPE::TIME: return aIuScale.IU_PER_PS;
238 case EDA_DATA_TYPE::LENGTH_DELAY: return aIuScale.IU_PER_PS_PER_MM;
239 case EDA_DATA_TYPE::UNITLESS: return 1.0;
240 case EDA_DATA_TYPE::ANGLE: return 1.0;
241 default: return aIuScale.IU_PER_MM;
242 }
243}
244
245
246#if 0 // No support for std::from_chars on MacOS yet
247
248bool EDA_UNIT_UTILS::ParseInternalUnits( const std::string& aInput, const EDA_IU_SCALE& aIuScale, int& aOut )
249{
250 double value;
251
252 if( std::from_chars( aInput.data(), aInput.data() + aInput.size(), value ).ec != std::errc() )
253 return false;
254
255 aOut = value * aIuScale.IU_PER_MM;
256 return true;
257}
258
259
260bool EDA_UNIT_UTILS::ParseInternalUnits( const std::string& aInput, const EDA_IU_SCALE& aIuScale, VECTOR2I& aOut )
261{
262 size_t pos = aInput.find( ' ' );
263
264 if( pos == std::string::npos )
265 return false;
266
267 std::string first = aInput.substr( 0, pos );
268 std::string second = aInput.substr( pos + 1 );
269
270 VECTOR2I vec;
271
272 if( !ParseInternalUnits( first, aIuScale, vec.x ) )
273 return false;
274
275 if( !ParseInternalUnits( second, aIuScale, vec.y ) )
276 return false;
277
278 aOut = vec;
279
280 return true;
281}
282
283#endif
284
285
286#define IU_TO_MM( x, scale ) ( x / scale.IU_PER_MM )
287#define IU_TO_IN( x, scale ) ( x / scale.IU_PER_MILS / 1000 )
288#define IU_TO_MILS( x, scale ) ( x / scale.IU_PER_MILS )
289#define IU_TO_PS( x, scale ) ( x / scale.IU_PER_PS )
290#define IU_TO_PS_PER_MM( x, scale ) ( x / scale.IU_PER_PS_PER_MM )
291#define MM_TO_IU( x, scale ) ( x * scale.IU_PER_MM )
292#define IN_TO_IU( x, scale ) ( x * scale.IU_PER_MILS * 1000 )
293#define MILS_TO_IU( x, scale ) ( x * scale.IU_PER_MILS )
294#define PS_TO_IU( x, scale ) ( x * scale.IU_PER_PS )
295#define PS_PER_MM_TO_IU( x, scale ) ( x * scale.IU_PER_PS_PER_MM )
296
297
298double EDA_UNIT_UTILS::UI::ToUserUnit( const EDA_IU_SCALE& aIuScale, EDA_UNITS aUnit, double aValue )
299{
300 switch( aUnit )
301 {
302 case EDA_UNITS::UM: return IU_TO_MM( aValue, aIuScale ) * 1000;
303 case EDA_UNITS::MM: return IU_TO_MM( aValue, aIuScale );
304 case EDA_UNITS::CM: return IU_TO_MM( aValue, aIuScale ) / 10;
305 case EDA_UNITS::MILS: return IU_TO_MILS( aValue, aIuScale );
306 case EDA_UNITS::INCH: return IU_TO_IN( aValue, aIuScale );
307 case EDA_UNITS::DEGREES: return aValue;
308 case EDA_UNITS::FS: return IU_TO_PS( aValue, aIuScale ) * 1000.0;
309 case EDA_UNITS::PS: return IU_TO_PS( aValue, aIuScale );
310 case EDA_UNITS::PS_PER_INCH: return IU_TO_PS_PER_MM( aValue, aIuScale ) * 25.4;
311 case EDA_UNITS::PS_PER_CM: return IU_TO_PS_PER_MM( aValue, aIuScale ) * 10;
312 case EDA_UNITS::PS_PER_MM: return IU_TO_PS_PER_MM( aValue, aIuScale );
313 default: return aValue;
314 }
315}
316
317
318wxString EDA_UNIT_UTILS::UI::StringFromValue( const EDA_IU_SCALE& aIuScale, EDA_UNITS aUnits, double aValue,
319 bool aAddUnitsText, EDA_DATA_TYPE aType )
320{
321 double value_to_print = aValue;
322 bool is_eeschema = ( aIuScale.IU_PER_MM == SCH_IU_PER_MM );
323
324 switch( aType )
325 {
327 value_to_print = ToUserUnit( aIuScale, aUnits, value_to_print );
329
331 value_to_print = ToUserUnit( aIuScale, aUnits, value_to_print );
333
335 value_to_print = ToUserUnit( aIuScale, aUnits, value_to_print );
336 break;
337
340 break;
341
343 value_to_print = ToUserUnit( aIuScale, aUnits, value_to_print );
344 break;
345
347 value_to_print = ToUserUnit( aIuScale, aUnits, value_to_print );
348 break;
349 }
350
351 const wxChar* format = nullptr;
352
353 switch( aUnits )
354 {
355 case EDA_UNITS::MILS: format = is_eeschema ? wxT( "%.3f" ) : wxT( "%.5f" ); break;
356 case EDA_UNITS::INCH: format = is_eeschema ? wxT( "%.6f" ) : wxT( "%.8f" ); break;
357 case EDA_UNITS::DEGREES: format = wxT( "%.4f" ); break;
358 case EDA_UNITS::PS_PER_INCH: format = wxT( "%.4f" ); break;
359 case EDA_UNITS::PS_PER_CM: format = wxT( "%.3f" ); break;
360 case EDA_UNITS::PS_PER_MM: format = wxT( "%.3f" ); break;
361 default: format = wxT( "%.10f" ); break;
362 }
363
364 wxString text;
365 text.Printf( format, value_to_print );
367
368 if( value_to_print != 0.0 && ( text == wxS( "0" ) || text == wxS( "-0" ) ) )
369 {
370 text.Printf( wxS( "%.10f" ), value_to_print );
372 }
373
374 if( aAddUnitsText )
375 text << EDA_UNIT_UTILS::GetText( aUnits, aType );
376
377 return text;
378}
379
380
381
382// A lower-precision (for readability) version of StringFromValue()
383wxString EDA_UNIT_UTILS::UI::MessageTextFromValue( const EDA_IU_SCALE& aIuScale, EDA_UNITS aUnits, int aValue,
384 bool aAddUnitLabel, EDA_DATA_TYPE aType )
385{
386 return MessageTextFromValue( aIuScale, aUnits, double( aValue ), aAddUnitLabel, aType );
387}
388
389
390// A lower-precision (for readability) version of StringFromValue()
392 long long int aValue, bool aAddUnitLabel, EDA_DATA_TYPE aType )
393{
394 return MessageTextFromValue( aIuScale, aUnits, double( aValue ), aAddUnitLabel, aType );
395}
396
397
398wxString EDA_UNIT_UTILS::UI::MessageTextFromValue( EDA_ANGLE aValue, bool aAddUnitLabel )
399{
400 if( aAddUnitLabel )
401 return wxString::Format( wxT( "%.1f°" ), aValue.AsDegrees() );
402 else
403 return wxString::Format( wxT( "%.1f" ), aValue.AsDegrees() );
404}
405
406
407// A lower-precision (for readability) version of StringFromValue()
408wxString EDA_UNIT_UTILS::UI::MessageTextFromValue( const EDA_IU_SCALE& aIuScale, EDA_UNITS aUnits, double aValue,
409 bool aAddUnitsText, EDA_DATA_TYPE aType )
410{
411 wxString text;
412 const wxChar* format;
413 double value = aValue;
414 bool short_form = ( aIuScale.IU_PER_MM == SCH_IU_PER_MM )
415 || ( aType == EDA_DATA_TYPE::VOLUME || aType == EDA_DATA_TYPE::AREA );
416
417 switch( aType )
418 {
420 value = ToUserUnit( aIuScale, aUnits, value );
421 // Fall through to continue computation
423
425 value = ToUserUnit( aIuScale, aUnits, value );
426 // Fall through to continue computation
428
430 value = ToUserUnit( aIuScale, aUnits, value );
431 break;
432
435 break;
436
438 value = ToUserUnit( aIuScale, aUnits, value );
439 break;
440
442 value = ToUserUnit( aIuScale, aUnits, value );
443 break;
444 }
445
446 switch( aUnits )
447 {
448 default:
449 case EDA_UNITS::UM: format = short_form ? wxT( "%.0f" ) : wxT( "%.1f" ); break;
450 case EDA_UNITS::MM: format = short_form ? wxT( "%.3f" ) : wxT( "%.4f" ); break;
451 case EDA_UNITS::CM: format = short_form ? wxT( "%.3f" ) : wxT( "%.5f" ); break;
452 case EDA_UNITS::MILS: format = short_form ? wxT( "%.0f" ) : wxT( "%.2f" ); break;
453 case EDA_UNITS::INCH: format = short_form ? wxT( "%.3f" ) : wxT( "%.4f" ); break;
454 case EDA_UNITS::DEGREES: format = wxT( "%.3f" ); break;
455 case EDA_UNITS::UNSCALED: format = wxT( "%.0f" ); break;
456 case EDA_UNITS::FS: format = wxT( "%.4f" ); break;
457 case EDA_UNITS::PS: format = wxT( "%.2f" ); break;
458 case EDA_UNITS::PS_PER_INCH: format = wxT( "%.2f" ); break;
459 case EDA_UNITS::PS_PER_CM: format = wxT( "%.2f" ); break;
460 case EDA_UNITS::PS_PER_MM: format = wxT( "%.2f" ); break;
461 }
462
463 text.Printf( format, value );
464
465 // Check if the formatted value shows only zeros but the actual value is non-zero
466 // If so, use scientific notation instead
467 if( value != 0.0 )
468 {
469 bool showsOnlyZeros = true;
470
471 // Check if the text contains only zeros (allowing for decimal point, minus sign, etc.)
472 for( auto ch : text )
473 {
474 if( ch >= '1' && ch <= '9' )
475 {
476 showsOnlyZeros = false;
477 break;
478 }
479 }
480
481 if( showsOnlyZeros )
482 {
483 text.Printf( wxT( "%.3e" ), value );
484 }
485 }
486
487 // Trim to 2-1/2 digits after the decimal place for short-form mm
488 if( short_form && aUnits == EDA_UNITS::MM )
489 {
490 struct lconv* lc = localeconv();
491 int length = (int) text.Length();
492
493 if( length > 4 && text[length - 4] == *lc->decimal_point && text[length - 1] == '0' )
494 text = text.Left( length - 1 );
495 }
496
497 if( aAddUnitsText )
498 text += EDA_UNIT_UTILS::GetText( aUnits, aType );
499
500 return text;
501}
502
503
505 const MINOPTMAX<int>& aValue )
506{
507 wxString msg;
508
509 if( aValue.HasMin() && aValue.Min() > 0 )
510 {
511 msg += _( "min" ) + wxS( " " ) + MessageTextFromValue( aIuScale, aUnits, aValue.Min() );
512 }
513
514 if( aValue.HasOpt() )
515 {
516 if( !msg.IsEmpty() )
517 msg += wxS( "; " );
518
519 msg += _( "opt" ) + wxS( " " ) + MessageTextFromValue( aIuScale, aUnits, aValue.Opt() );
520 }
521
522 if( aValue.HasMax() )
523 {
524 if( !msg.IsEmpty() )
525 msg += wxS( "; " );
526
527 msg += _( "max" ) + wxS( " " ) + MessageTextFromValue( aIuScale, aUnits, aValue.Max() );
528 }
529
530 return msg;
531};
532
533
534double EDA_UNIT_UTILS::UI::FromUserUnit( const EDA_IU_SCALE& aIuScale, EDA_UNITS aUnits, double aValue )
535{
536 switch( aUnits )
537 {
538 case EDA_UNITS::UM: return MM_TO_IU( aValue / 1000.0, aIuScale );
539 case EDA_UNITS::MM: return MM_TO_IU( aValue, aIuScale );
540 case EDA_UNITS::CM: return MM_TO_IU( aValue * 10, aIuScale );
541 case EDA_UNITS::MILS: return MILS_TO_IU( aValue, aIuScale );
542 case EDA_UNITS::INCH: return IN_TO_IU( aValue, aIuScale );
543 case EDA_UNITS::FS: return PS_TO_IU( aValue / 1000.0, aIuScale );
544 case EDA_UNITS::PS: return PS_TO_IU( aValue, aIuScale );
545 case EDA_UNITS::PS_PER_INCH: return PS_PER_MM_TO_IU( aValue / 25.4, aIuScale );
546 case EDA_UNITS::PS_PER_CM: return PS_PER_MM_TO_IU( aValue / 10, aIuScale );
547 case EDA_UNITS::PS_PER_MM: return PS_PER_MM_TO_IU( aValue, aIuScale );
548 default:
551 case EDA_UNITS::PERCENT: return aValue;
552 }
553}
554
555
556double EDA_UNIT_UTILS::UI::DoubleValueFromString( const wxString& aTextValue )
557{
558 double dtmp = 0;
559
560 // Acquire the 'right' decimal point separator
561 const struct lconv* lc = localeconv();
562
563 wxChar decimal_point = lc->decimal_point[0];
564 wxString buf( aTextValue.Strip( wxString::both ) );
565
566 // Convert any entered decimal point separators to the 'right' one
567 buf.Replace( wxT( "." ), wxString( decimal_point, 1 ) );
568 buf.Replace( wxT( "," ), wxString( decimal_point, 1 ) );
569
570 // Find the end of the numeric part
571 unsigned brk_point = 0;
572
573 while( brk_point < buf.Len() )
574 {
575 wxChar ch = buf[brk_point];
576
577 if( !( ( ch >= '0' && ch <= '9' ) || ( ch == decimal_point ) || ( ch == '-' )
578 || ( ch == '+' ) ) )
579 {
580 break;
581 }
582
583 ++brk_point;
584 }
585
586 // Extract the numeric part
587 buf.Left( brk_point ).ToDouble( &dtmp );
588
589 return dtmp;
590}
591
592
594 const wxString& aTextValue, EDA_DATA_TYPE aType )
595{
596 double dtmp = 0;
597
598 // Acquire the 'right' decimal point separator
599 const struct lconv* lc = localeconv();
600
601 wxChar decimal_point = lc->decimal_point[0];
602 wxString buf( aTextValue.Strip( wxString::both ) );
603
604 // Convert any entered decimal point separators to the 'right' one
605 buf.Replace( wxT( "." ), wxString( decimal_point, 1 ) );
606 buf.Replace( wxT( "," ), wxString( decimal_point, 1 ) );
607
608 // Find the end of the numeric part
609 unsigned brk_point = 0;
610
611 while( brk_point < buf.Len() )
612 {
613 wxChar ch = buf[brk_point];
614
615 if( !( (ch >= '0' && ch <= '9') || (ch == decimal_point) || (ch == '-') || (ch == '+') ) )
616 break;
617
618 ++brk_point;
619 }
620
621 // Extract the numeric part
622 buf.Left( brk_point ).ToDouble( &dtmp );
623
624 // Check the optional unit designator (2 ch significant)
625 wxString unit( buf.Mid( brk_point ).Trim( false ).Left( 2 ).Lower() );
626
627 if( aUnits == EDA_UNITS::UM
628 || aUnits == EDA_UNITS::MM
629 || aUnits == EDA_UNITS::CM
630 || aUnits == EDA_UNITS::MILS
631 || aUnits == EDA_UNITS::INCH )
632 {
633 //check for um, μm (µ is MICRO SIGN) and µm (µ is GREEK SMALL LETTER MU) for micrometre
634 if( unit == wxT( "um" ) || unit == wxT( "\u00B5m" ) || unit == wxT( "\u03BCm" ) )
635 {
636 aUnits = EDA_UNITS::UM;
637 }
638 else if( unit == wxT( "mm" ) )
639 {
640 aUnits = EDA_UNITS::MM;
641 }
642 else if( unit == wxT( "cm" ) )
643 {
644 aUnits = EDA_UNITS::CM;
645 }
646 else if( unit == wxT( "mi" ) || unit == wxT( "th" ) )
647 {
648 aUnits = EDA_UNITS::MILS;
649 }
650 else if( unit == wxT( "in" ) || unit == wxT( "\"" ) )
651 {
652 aUnits = EDA_UNITS::INCH;
653 }
654 else if( unit == wxT( "oz" ) ) // 1 oz = 1.37 mils
655 {
656 aUnits = EDA_UNITS::MILS;
657 dtmp *= 1.37;
658 }
659 }
660 else if( aUnits == EDA_UNITS::DEGREES )
661 {
662 if( unit == wxT( "ra" ) ) // Radians
663 dtmp *= 180.0f / M_PI;
664 }
665 else if( aUnits == EDA_UNITS::FS || aUnits == EDA_UNITS::PS || aUnits == EDA_UNITS::PS_PER_INCH
666 || aUnits == EDA_UNITS::PS_PER_CM || aUnits == EDA_UNITS::PS_PER_MM )
667
668 {
669 wxString timeUnit( buf.Mid( brk_point ).Trim( false ).Left( 5 ).Lower() );
670
671 if( timeUnit == wxT( "fs" ) )
672 aUnits = EDA_UNITS::FS;
673 if( timeUnit == wxT( "ps" ) )
674 aUnits = EDA_UNITS::PS;
675 else if( timeUnit == wxT( "ps/in" ) )
676 aUnits = EDA_UNITS::PS_PER_INCH;
677 else if( timeUnit == wxT( "ps/cm" ) )
678 aUnits = EDA_UNITS::PS_PER_CM;
679 else if( timeUnit == wxT( "ps/mm" ) )
680 aUnits = EDA_UNITS::PS_PER_MM;
681 }
682
683 switch( aType )
684 {
686 dtmp = FromUserUnit( aIuScale, aUnits, dtmp );
688
690 dtmp = FromUserUnit( aIuScale, aUnits, dtmp );
692
694 dtmp = FromUserUnit( aIuScale, aUnits, dtmp );
695 break;
696
699 break;
700
702 dtmp = FromUserUnit( aIuScale, aUnits, dtmp );
703 break;
704
706 dtmp = FromUserUnit( aIuScale, aUnits, dtmp );
707 break;
708 }
709
710 return dtmp;
711}
712
713
714bool EDA_UNIT_UTILS::UI::DoubleValueFromString( const EDA_IU_SCALE& aIuScale, const wxString& aTextValue,
715 double& aDoubleValue )
716{
717 double dtmp = 0;
718
719 // Acquire the 'right' decimal point separator
720 const struct lconv* lc = localeconv();
721
722 wxChar decimal_point = lc->decimal_point[0];
723 wxString buf( aTextValue.Strip( wxString::both ) );
724
725 // Convert any entered decimal point separators to the 'right' one
726 buf.Replace( wxT( "." ), wxString( decimal_point, 1 ) );
727 buf.Replace( wxT( "," ), wxString( decimal_point, 1 ) );
728
729 // Find the end of the numeric part
730 unsigned brk_point = 0;
731
732 while( brk_point < buf.Len() )
733 {
734 wxChar ch = buf[brk_point];
735
736 if( !( (ch >= '0' && ch <= '9') || (ch == decimal_point) || (ch == '-') || (ch == '+') ) )
737 break;
738
739 ++brk_point;
740 }
741
742 if( brk_point == 0 )
743 return false;
744
745 // Extract the numeric part
746 buf.Left( brk_point ).ToDouble( &dtmp );
747
748 // Check the unit designator
749 wxString unit( buf.Mid( brk_point ).Strip( wxString::both ).Lower() );
750 EDA_UNITS units = EDA_UNITS::MM; // Make gcc quiet
751
752 //check for um, μm (µ is MICRO SIGN) and µm (µ is GREEK SMALL LETTER MU) for micrometre
753 if( unit == wxT( "um" ) || unit == wxT( "\u00B5m" ) || unit == wxT( "\u03BCm" ) )
754 {
755 units = EDA_UNITS::UM;
756 }
757 else if( unit == wxT( "mm" ) )
758 {
759 units = EDA_UNITS::MM;
760 }
761 else if( unit == wxT( "cm" ) )
762 {
763 units = EDA_UNITS::CM;
764 }
765 else if( unit == wxT( "mil" ) || unit == wxT( "mils" ) || unit == wxT( "thou" ) )
766 {
767 units = EDA_UNITS::MILS;
768 }
769 else if( unit == wxT( "in" ) || unit == wxT( "\"" ) )
770 {
771 units = EDA_UNITS::INCH;
772 }
773 else if( unit == wxT( "oz" ) ) // 1 oz = 1.37 mils
774 {
775 units = EDA_UNITS::MILS;
776 dtmp *= 1.37;
777 }
778 else if( unit == wxT( "ra" ) ) // Radians
779 {
780 dtmp *= 180.0f / M_PI;
781 }
782 else if( unit == wxT( "fs" ) )
783 {
784 units = EDA_UNITS::FS;
785 }
786 else if( unit == wxT( "ps" ) )
787 {
788 units = EDA_UNITS::PS;
789 }
790 else if( unit == wxT( "ps/in" ) )
791 {
793 }
794 else if( unit == wxT( "ps/cm" ) )
795 {
796 units = EDA_UNITS::PS_PER_CM;
797 }
798 else if( unit == wxT( "ps/mm" ) )
799 {
800 units = EDA_UNITS::PS_PER_MM;
801 }
802 else
803 {
804 return false;
805 }
806
807 aDoubleValue = FromUserUnit( aIuScale, units, dtmp );
808 return true;
809}
810
811
812long long int EDA_UNIT_UTILS::UI::ValueFromString( const EDA_IU_SCALE& aIuScale, EDA_UNITS aUnits,
813 const wxString& aTextValue, EDA_DATA_TYPE aType )
814{
815 double value = DoubleValueFromString( aIuScale, aUnits, aTextValue, aType );
816
817 return KiROUND<double, long long int>( value );
818}
819
820
821long long int EDA_UNIT_UTILS::UI::ValueFromString( const wxString& aTextValue )
822{
823 double value = DoubleValueFromString( aTextValue );
824
825 return KiROUND<double, long long int>( value );
826}
constexpr double SCH_IU_PER_MM
Schematic internal units 1=100nm.
Definition base_units.h:70
constexpr BOX2I KiROUND(const BOX2D &aBoxD)
Definition box2.h:985
double AsDegrees() const
Definition eda_angle.h:115
T Min() const
Definition minoptmax.h:29
bool HasMax() const
Definition minoptmax.h:38
bool HasMin() const
Definition minoptmax.h:37
T Max() const
Definition minoptmax.h:30
T Opt() const
Definition minoptmax.h:31
bool HasOpt() const
Definition minoptmax.h:39
#define _(s)
#define IU_TO_IN(x, scale)
#define IU_TO_PS_PER_MM(x, scale)
#define IN_TO_IU(x, scale)
#define IU_TO_MILS(x, scale)
#define IU_TO_PS(x, scale)
#define MM_TO_IU(x, scale)
#define PS_TO_IU(x, scale)
#define PS_PER_MM_TO_IU(x, scale)
#define IU_TO_MM(x, scale)
#define MILS_TO_IU(x, scale)
static void removeTrailingZeros(wxString &aText)
Definition eda_units.cpp:28
EDA_DATA_TYPE
The type of unit.
Definition eda_units.h:34
EDA_UNITS
Definition eda_units.h:45
@ PS_PER_INCH
Definition eda_units.h:56
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:79
#define UNIMPLEMENTED_FOR(type)
Definition macros.h:92
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.
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",...
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.
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.
KICOMMON_API wxString MessageTextFromMinOptMax(const EDA_IU_SCALE &aIuScale, EDA_UNITS aUnits, const MINOPTMAX< int > &aValue)
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.
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.
KICOMMON_API wxString GetText(EDA_UNITS aUnits, EDA_DATA_TYPE aType=EDA_DATA_TYPE::DISTANCE)
Get the units string for a given units type.
KICOMMON_API bool FetchUnitsFromString(const wxString &aTextValue, EDA_UNITS &aUnits)
Write any unit info found in the string to aUnits.
Definition eda_units.cpp:84
KICOMMON_API bool IsImperialUnit(EDA_UNITS aUnit)
Definition eda_units.cpp:43
KICOMMON_API bool IsMetricUnit(EDA_UNITS aUnit)
Definition eda_units.cpp:57
KICOMMON_API double GetScaleForInternalUnitType(const EDA_IU_SCALE &aIuScale, EDA_DATA_TYPE aDataType)
Returns the scaling parameter for the given units data type.
KICOMMON_API wxString GetLabel(EDA_UNITS aUnits, EDA_DATA_TYPE aType=EDA_DATA_TYPE::DISTANCE)
Get the units string for a given units type.
KICOMMON_API int Mm2mils(double aVal)
Convert mm to mils.
Definition eda_units.cpp:72
KICOMMON_API std::string FormatAngle(const EDA_ANGLE &aAngle)
Convert aAngle from board units to a string appropriate for writing to file.
KICOMMON_API std::string FormatInternalUnits(const EDA_IU_SCALE &aIuScale, int aValue, EDA_DATA_TYPE aDataType=EDA_DATA_TYPE::DISTANCE)
Converts aValue from internal units to a string appropriate for writing to file.
KICOMMON_API int Mils2mm(double aVal)
Convert mils to mm.
Definition eda_units.cpp:78
const double IU_PER_PS
Internal time units are attoseconds.
Definition base_units.h:76
const double IU_PER_MM
Definition base_units.h:74
const double IU_PER_PS_PER_MM
Internal delay units are attoseconds/mm.
Definition base_units.h:77
#define M_PI
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:708