29#include <wx/translation.h>
34 int len = aText.length();
37 while( --len > 0 && aText[len] ==
'0' )
40 if( len >= 0 && ( aText[len] ==
'.' || aText[len] ==
',' ) )
43 aText = aText.RemoveLast( removeLast );
78 return KiROUND( aVal * 1000. / 25.4 );
84 return KiROUND( aVal * 25.4 / 1000. );
90 wxString buf( aTextValue.Strip( wxString::both ) );
91 unsigned brk_point = 0;
93 while( brk_point < buf.Len() )
95 wxChar c = buf[brk_point];
97 if( !( ( c >=
'0' && c <=
'9' ) || ( c ==
'.' ) || ( c ==
',' ) || ( c ==
'-' )
105 wxString unit( buf.Mid( brk_point ).Strip( wxString::leading ).Left( 2 ).Lower() );
108 if( unit == wxT(
"um" ) || unit == wxT(
"\u00B5m" ) || unit == wxT(
"\u03BCm" ) )
110 else if( unit == wxT(
"mm" ) )
112 if( unit == wxT(
"cm" ) )
114 else if( unit == wxT(
"mi" ) || unit == wxT(
"th" ) )
116 else if( unit == wxT(
"in" ) || unit == wxT(
"\"" ) )
118 else if( unit == wxT(
"de" ) || unit == wxT(
"ra" ) )
157 return GetText( aUnits, aType ).Trim(
false );
163 std::string temp = fmt::format(
"{:.10g}", aAngle.
AsDegrees() );
172 double engUnits = aValue;
176 if( engUnits != 0.0 && fabs( engUnits ) <= 0.0001 )
178 buf = fmt::format(
"{:.10f}", engUnits );
181 while( !buf.empty() && buf[buf.size() - 1] ==
'0' )
188 if( buf[buf.size() - 1] ==
'.' )
195 buf = fmt::format(
"{:.10g}", engUnits );
212bool EDA_UNIT_UTILS::ParseInternalUnits(
const std::string& aInput,
const EDA_IU_SCALE& aIuScale,
217 if( std::from_chars( aInput.data(), aInput.data() + aInput.size(), value ).ec != std::errc() )
225bool EDA_UNIT_UTILS::ParseInternalUnits(
const std::string& aInput,
const EDA_IU_SCALE& aIuScale,
228 size_t pos = aInput.find(
' ' );
230 if( pos == std::string::npos )
233 std::string first = aInput.substr( 0, pos );
234 std::string second = aInput.substr( pos + 1 );
238 if( !ParseInternalUnits( first, aIuScale, vec.
x ) )
241 if( !ParseInternalUnits( second, aIuScale, vec.
y ) )
252#define IU_TO_MM( x, scale ) ( x / scale.IU_PER_MM )
253#define IU_TO_IN( x, scale ) ( x / scale.IU_PER_MILS / 1000 )
254#define IU_TO_MILS( x, scale ) ( x / scale.IU_PER_MILS )
255#define MM_TO_IU( x, scale ) ( x * scale.IU_PER_MM )
256#define IN_TO_IU( x, scale ) ( x * scale.IU_PER_MILS * 1000 )
257#define MILS_TO_IU( x, scale ) ( x * scale.IU_PER_MILS )
265 return IU_TO_MM( aValue, aIuScale ) * 1000;
268 return IU_TO_MM( aValue, aIuScale );
271 return IU_TO_MM( aValue, aIuScale ) / 10;
277 return IU_TO_IN( aValue, aIuScale );
301 double aValue,
bool aAddUnitsText,
304 double value_to_print = aValue;
310 value_to_print =
ToUserUnit( aIuScale, aUnits, value_to_print );
314 value_to_print =
ToUserUnit( aIuScale, aUnits, value_to_print );
318 value_to_print =
ToUserUnit( aIuScale, aUnits, value_to_print );
325 const wxChar* format =
nullptr;
331 format = is_eeschema ? wxT(
"%.3f" ) : wxT(
"%.5f" );
335 format = is_eeschema ? wxT(
"%.6f" ) : wxT(
"%.8f" );
339 format = wxT(
"%.4f" );
343 format = wxT(
"%.10f" );
348 text.Printf( format, value_to_print );
351 if( value_to_print != 0.0 && (
text == wxS(
"0" ) ||
text == wxS(
"-0" ) ) )
353 text.Printf( wxS(
"%.10f" ), value_to_print );
390 long long int aValue,
401 return wxString::Format( wxT(
"%.1f°" ), aValue.
AsDegrees() );
403 return wxString::Format( wxT(
"%.1f" ), aValue.
AsDegrees() );
409 double aValue,
bool aAddUnitsText,
413 const wxChar* format;
414 double value = aValue;
420 value =
ToUserUnit( aIuScale, aUnits, value );
425 value =
ToUserUnit( aIuScale, aUnits, value );
430 value =
ToUserUnit( aIuScale, aUnits, value );
441 format = is_eeschema ? wxT(
"%.0f" ) : wxT(
"%.1f" );
445 format = is_eeschema ? wxT(
"%.2f" ) : wxT(
"%.4f" );
449 format = is_eeschema ? wxT(
"%.3f" ) : wxT(
"%.5f" );
453 format = is_eeschema ? wxT(
"%.0f" ) : wxT(
"%.2f" );
457 format = is_eeschema ? wxT(
"%.3f" ) : wxT(
"%.4f" );
462 format = wxT(
"%.3f" );
466 format = wxT(
"%.0f" );
470 text.Printf( format, value );
485 if( aValue.
HasMin() && aValue.
Min() > 0 )
516 return MM_TO_IU( aValue / 1000.0, aIuScale );
519 return MM_TO_IU( aValue, aIuScale );
522 return MM_TO_IU( aValue * 10, aIuScale );
528 return IN_TO_IU( aValue, aIuScale );
544 const struct lconv* lc = localeconv();
546 wxChar decimal_point = lc->decimal_point[0];
547 wxString buf( aTextValue.Strip( wxString::both ) );
550 buf.Replace( wxT(
"." ), wxString( decimal_point, 1 ) );
551 buf.Replace( wxT(
"," ), wxString( decimal_point, 1 ) );
554 unsigned brk_point = 0;
556 while( brk_point < buf.Len() )
558 wxChar ch = buf[brk_point];
560 if( !( ( ch >=
'0' && ch <=
'9' ) || ( ch == decimal_point ) || ( ch ==
'-' )
570 buf.Left( brk_point ).ToDouble( &dtmp );
582 const struct lconv* lc = localeconv();
584 wxChar decimal_point = lc->decimal_point[0];
585 wxString buf( aTextValue.Strip( wxString::both ) );
588 buf.Replace( wxT(
"." ), wxString( decimal_point, 1 ) );
589 buf.Replace( wxT(
"," ), wxString( decimal_point, 1 ) );
592 unsigned brk_point = 0;
594 while( brk_point < buf.Len() )
596 wxChar ch = buf[brk_point];
598 if( !( (ch >=
'0' && ch <=
'9') || (ch == decimal_point) || (ch ==
'-') || (ch ==
'+') ) )
605 buf.Left( brk_point ).ToDouble( &dtmp );
608 wxString unit( buf.Mid( brk_point ).Strip( wxString::leading ).Left( 2 ).Lower() );
617 if( unit == wxT(
"um" ) || unit == wxT(
"\u00B5m" ) || unit == wxT(
"\u03BCm" ) )
621 else if( unit == wxT(
"mm" ) )
625 else if( unit == wxT(
"cm" ) )
629 else if( unit == wxT(
"mi" ) || unit == wxT(
"th" ) )
633 else if( unit == wxT(
"in" ) || unit == wxT(
"\"" ) )
637 else if( unit == wxT(
"oz" ) )
645 if( unit == wxT(
"ra" ) )
646 dtmp *= 180.0f / M_PI;
676 return KiROUND<double, long long int>( value );
684 return KiROUND<double, long long int>( value );
constexpr double SCH_IU_PER_MM
constexpr BOX2I KiROUND(const BOX2D &aBoxD)
#define IU_TO_IN(x, scale)
#define IN_TO_IU(x, scale)
#define IU_TO_MILS(x, scale)
#define 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)
Function ValueFromString converts 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)
Returns 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)
Function DoubleValueFromString converts aTextValue to a double.
KICOMMON_API double ToUserUnit(const EDA_IU_SCALE &aIuScale, EDA_UNITS aUnit, double aValue)
Function To_User_Unit 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)
Writes 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 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 FormatInternalUnits(const EDA_IU_SCALE &aIuScale, int aValue)
Converts aValue from internal units to a string appropriate for writing to file.
KICOMMON_API std::string FormatAngle(const EDA_ANGLE &aAngle)
Converts aAngle from board units to a string appropriate for writing to file.
KICOMMON_API int Mils2mm(double aVal)
Convert mils to mm.