25#include <wx/translation.h>
30 int len = aText.length();
33 while( --len > 0 && aText[len] ==
'0' )
36 if( len >= 0 && ( aText[len] ==
'.' || aText[len] ==
',' ) )
39 aText = aText.RemoveLast( removeLast );
74 return KiROUND( aVal * 1000. / 25.4 );
80 return KiROUND( aVal * 25.4 / 1000. );
86 wxString buf( aTextValue.Strip( wxString::both ) );
87 unsigned brk_point = 0;
89 while( brk_point < buf.Len() )
91 wxChar c = buf[brk_point];
93 if( !( ( c >=
'0' && c <=
'9' ) || ( c ==
'.' ) || ( c ==
',' ) || ( c ==
'-' )
101 wxString unit( buf.Mid( brk_point ).Trim(
false ).Left( 2 ).Lower() );
104 if( unit == wxT(
"um" ) || unit == wxT(
"\u00B5m" ) || unit == wxT(
"\u03BCm" ) )
106 else if( unit == wxT(
"mm" ) )
108 if( unit == wxT(
"cm" ) )
110 else if( unit == wxT(
"mi" ) || unit == wxT(
"th" ) )
112 else if( unit == wxT(
"in" ) || unit == wxT(
"\"" ) )
114 else if( unit == wxT(
"de" ) || unit == wxT(
"ra" ) )
116 else if( unit == wxT(
"fs" ) )
118 else if( unit == wxT(
"ps" ) )
120 wxString timeUnit( buf.Mid( brk_point ).Trim(
false ).Left( 5 ).Lower() );
122 if( timeUnit == wxT(
"ps" ) )
124 else if( timeUnit == wxT(
"ps/in" ) )
126 else if( timeUnit == wxT(
"ps/cm" ) )
128 else if( timeUnit == wxT(
"ps/mm" ) )
178 return GetText( aUnits, aType ).Trim(
false );
184 std::string temp = fmt::format(
"{:.10g}", aAngle.
AsDegrees() );
194 double engUnits = aValue;
198 if( engUnits != 0.0 && fabs( engUnits ) <= 0.0001 )
200 buf = fmt::format(
"{:.10f}", engUnits );
203 while( !buf.empty() && buf[buf.size() - 1] ==
'0' )
210 if( buf[buf.size() - 1] ==
'.' )
217 buf = fmt::format(
"{:.10g}", engUnits );
246bool EDA_UNIT_UTILS::ParseInternalUnits(
const std::string& aInput,
const EDA_IU_SCALE& aIuScale,
251 if( std::from_chars( aInput.data(), aInput.data() + aInput.size(), value ).ec != std::errc() )
259bool EDA_UNIT_UTILS::ParseInternalUnits(
const std::string& aInput,
const EDA_IU_SCALE& aIuScale,
262 size_t pos = aInput.find(
' ' );
264 if( pos == std::string::npos )
267 std::string first = aInput.substr( 0, pos );
268 std::string second = aInput.substr( pos + 1 );
272 if( !ParseInternalUnits( first, aIuScale, vec.
x ) )
275 if( !ParseInternalUnits( second, aIuScale, vec.
y ) )
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 )
314 default:
return aValue;
320 double aValue,
bool aAddUnitsText,
323 double value_to_print = aValue;
329 value_to_print =
ToUserUnit( aIuScale, aUnits, value_to_print );
333 value_to_print =
ToUserUnit( aIuScale, aUnits, value_to_print );
337 value_to_print =
ToUserUnit( aIuScale, aUnits, value_to_print );
344 value_to_print =
ToUserUnit( aIuScale, aUnits, value_to_print );
348 value_to_print =
ToUserUnit( aIuScale, aUnits, value_to_print );
352 const wxChar* format =
nullptr;
356 case EDA_UNITS::MILS: format = is_eeschema ? wxT(
"%.3f" ) : wxT(
"%.5f" );
break;
357 case EDA_UNITS::INCH: format = is_eeschema ? wxT(
"%.6f" ) : wxT(
"%.8f" );
break;
362 default: format = wxT(
"%.10f" );
break;
366 text.Printf( format, value_to_print );
369 if( value_to_print != 0.0 && (
text == wxS(
"0" ) ||
text == wxS(
"-0" ) ) )
371 text.Printf( wxS(
"%.10f" ), value_to_print );
395 long long int aValue,
406 return wxString::Format( wxT(
"%.1f°" ), aValue.
AsDegrees() );
408 return wxString::Format( wxT(
"%.1f" ), aValue.
AsDegrees() );
414 double aValue,
bool aAddUnitsText,
418 const wxChar* format;
419 double value = aValue;
426 value =
ToUserUnit( aIuScale, aUnits, value );
431 value =
ToUserUnit( aIuScale, aUnits, value );
436 value =
ToUserUnit( aIuScale, aUnits, value );
443 value =
ToUserUnit( aIuScale, aUnits, value );
447 value =
ToUserUnit( aIuScale, aUnits, value );
454 case EDA_UNITS::UM: format = short_form ? wxT(
"%.0f" ) : wxT(
"%.1f" );
break;
455 case EDA_UNITS::MM: format = short_form ? wxT(
"%.3f" ) : wxT(
"%.4f" );
break;
456 case EDA_UNITS::CM: format = short_form ? wxT(
"%.3f" ) : wxT(
"%.5f" );
break;
457 case EDA_UNITS::MILS: format = short_form ? wxT(
"%.0f" ) : wxT(
"%.2f" );
break;
458 case EDA_UNITS::INCH: format = short_form ? wxT(
"%.3f" ) : wxT(
"%.4f" );
break;
468 text.Printf( format, value );
474 bool showsOnlyZeros =
true;
477 for(
auto ch :
text )
479 if( ch >=
'1' && ch <=
'9' )
481 showsOnlyZeros =
false;
488 text.Printf( wxT(
"%.3e" ), value );
495 struct lconv* lc = localeconv();
496 int length = (int)
text.Length();
498 if( length > 4 &&
text[length - 4] == *lc->decimal_point &&
text[length - 1] ==
'0' )
515 if( aValue.
HasMin() && aValue.
Min() > 0 )
568 const struct lconv* lc = localeconv();
570 wxChar decimal_point = lc->decimal_point[0];
571 wxString buf( aTextValue.Strip( wxString::both ) );
574 buf.Replace( wxT(
"." ), wxString( decimal_point, 1 ) );
575 buf.Replace( wxT(
"," ), wxString( decimal_point, 1 ) );
578 unsigned brk_point = 0;
580 while( brk_point < buf.Len() )
582 wxChar ch = buf[brk_point];
584 if( !( ( ch >=
'0' && ch <=
'9' ) || ( ch == decimal_point ) || ( ch ==
'-' )
594 buf.Left( brk_point ).ToDouble( &dtmp );
606 const struct lconv* lc = localeconv();
608 wxChar decimal_point = lc->decimal_point[0];
609 wxString buf( aTextValue.Strip( wxString::both ) );
612 buf.Replace( wxT(
"." ), wxString( decimal_point, 1 ) );
613 buf.Replace( wxT(
"," ), wxString( decimal_point, 1 ) );
616 unsigned brk_point = 0;
618 while( brk_point < buf.Len() )
620 wxChar ch = buf[brk_point];
622 if( !( (ch >=
'0' && ch <=
'9') || (ch == decimal_point) || (ch ==
'-') || (ch ==
'+') ) )
629 buf.Left( brk_point ).ToDouble( &dtmp );
632 wxString unit( buf.Mid( brk_point ).Trim(
false ).Left( 2 ).Lower() );
641 if( unit == wxT(
"um" ) || unit == wxT(
"\u00B5m" ) || unit == wxT(
"\u03BCm" ) )
645 else if( unit == wxT(
"mm" ) )
649 else if( unit == wxT(
"cm" ) )
653 else if( unit == wxT(
"mi" ) || unit == wxT(
"th" ) )
657 else if( unit == wxT(
"in" ) || unit == wxT(
"\"" ) )
661 else if( unit == wxT(
"oz" ) )
669 if( unit == wxT(
"ra" ) )
670 dtmp *= 180.0f /
M_PI;
676 wxString timeUnit( buf.Mid( brk_point ).Trim(
false ).Left( 5 ).Lower() );
678 if( timeUnit == wxT(
"fs" ) )
680 if( timeUnit == wxT(
"ps" ) )
682 else if( timeUnit == wxT(
"ps/in" ) )
684 else if( timeUnit == wxT(
"ps/cm" ) )
686 else if( timeUnit == wxT(
"ps/mm" ) )
721 double& aDoubleValue )
726 const struct lconv* lc = localeconv();
728 wxChar decimal_point = lc->decimal_point[0];
729 wxString buf( aTextValue.Strip( wxString::both ) );
732 buf.Replace( wxT(
"." ), wxString( decimal_point, 1 ) );
733 buf.Replace( wxT(
"," ), wxString( decimal_point, 1 ) );
736 unsigned brk_point = 0;
738 while( brk_point < buf.Len() )
740 wxChar ch = buf[brk_point];
742 if( !( (ch >=
'0' && ch <=
'9') || (ch == decimal_point) || (ch ==
'-') || (ch ==
'+') ) )
752 buf.Left( brk_point ).ToDouble( &dtmp );
755 wxString unit( buf.Mid( brk_point ).Strip( wxString::both ).Lower() );
759 if( unit == wxT(
"um" ) || unit == wxT(
"\u00B5m" ) || unit == wxT(
"\u03BCm" ) )
763 else if( unit == wxT(
"mm" ) )
767 else if( unit == wxT(
"cm" ) )
771 else if( unit == wxT(
"mil" ) || unit == wxT(
"mils" ) || unit == wxT(
"thou" ) )
775 else if( unit == wxT(
"in" ) || unit == wxT(
"\"" ) )
779 else if( unit == wxT(
"oz" ) )
784 else if( unit == wxT(
"ra" ) )
786 dtmp *= 180.0f /
M_PI;
788 else if( unit == wxT(
"fs" ) )
792 else if( unit == wxT(
"ps" ) )
796 else if( unit == wxT(
"ps/in" ) )
800 else if( unit == wxT(
"ps/cm" ) )
804 else if( unit == wxT(
"ps/mm" ) )
constexpr double SCH_IU_PER_MM
Schematic internal units 1=100nm.
constexpr BOX2I KiROUND(const BOX2D &aBoxD)
#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)
EDA_DATA_TYPE
The type of unit.
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 ...
#define UNIMPLEMENTED_FOR(type)
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.
KICOMMON_API bool IsImperialUnit(EDA_UNITS aUnit)
KICOMMON_API bool IsMetricUnit(EDA_UNITS aUnit)
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.
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.
const double IU_PER_PS
Internal time units are attoseconds.
const double IU_PER_PS_PER_MM
Internal delay units are attoseconds/mm.
VECTOR2< int32_t > VECTOR2I