29#include <wx/translation.h>
62 return KiROUND( aVal * 1000. / 25.4 );
68 return KiROUND( aVal * 25.4 / 1000. );
74 wxString buf( aTextValue.Strip( wxString::both ) );
75 unsigned brk_point = 0;
77 while( brk_point < buf.Len() )
79 wxChar c = buf[brk_point];
81 if( !( ( c >=
'0' && c <=
'9' ) || ( c ==
'.' ) || ( c ==
',' ) || ( c ==
'-' )
89 wxString unit( buf.Mid( brk_point ).Strip( wxString::leading ).Left( 2 ).Lower() );
92 if( unit == wxT(
"um" ) || unit == wxT(
"\u00B5m" ) || unit == wxT(
"\u03BCm" ) )
94 else if( unit == wxT(
"mm" ) )
96 if( unit == wxT(
"cm" ) )
98 else if( unit == wxT(
"mi" ) || unit == wxT(
"th" ) )
100 else if( unit == wxT(
"in" ) || unit == wxT(
"\"" ) )
102 else if( unit == wxT(
"de" ) || unit == wxT(
"ra" ) )
141 return GetText( aUnits, aType ).Trim(
false );
147 std::string temp = fmt::format(
"{:.10g}", aAngle.
AsDegrees() );
156 double engUnits = aValue;
160 if( engUnits != 0.0 && fabs( engUnits ) <= 0.0001 )
162 buf = fmt::format(
"{:.10f}", engUnits );
165 while( !buf.empty() && buf[buf.size() - 1] ==
'0' )
172 if( buf[buf.size() - 1] ==
'.' )
179 buf = fmt::format(
"{:.10g}", engUnits );
196bool EDA_UNIT_UTILS::ParseInternalUnits(
const std::string& aInput,
const EDA_IU_SCALE& aIuScale,
201 if( std::from_chars( aInput.data(), aInput.data() + aInput.size(), value ).ec != std::errc() )
209bool EDA_UNIT_UTILS::ParseInternalUnits(
const std::string& aInput,
const EDA_IU_SCALE& aIuScale,
212 size_t pos = aInput.find(
' ' );
214 if( pos == std::string::npos )
217 std::string first = aInput.substr( 0, pos );
218 std::string second = aInput.substr( pos + 1 );
222 if( !ParseInternalUnits( first, aIuScale, vec.
x ) )
225 if( !ParseInternalUnits( second, aIuScale, vec.
y ) )
236#define IU_TO_MM( x, scale ) ( x / scale.IU_PER_MM )
237#define IU_TO_IN( x, scale ) ( x / scale.IU_PER_MILS / 1000 )
238#define IU_TO_MILS( x, scale ) ( x / scale.IU_PER_MILS )
239#define MM_TO_IU( x, scale ) ( x * scale.IU_PER_MM )
240#define IN_TO_IU( x, scale ) ( x * scale.IU_PER_MILS * 1000 )
241#define MILS_TO_IU( x, scale ) ( x * scale.IU_PER_MILS )
249 return IU_TO_MM( aValue, aIuScale ) * 1000;
252 return IU_TO_MM( aValue, aIuScale );
255 return IU_TO_MM( aValue, aIuScale ) / 10;
261 return IU_TO_IN( aValue, aIuScale );
285 double aValue,
bool aAddUnitsText,
288 double value_to_print = aValue;
293 value_to_print =
ToUserUnit( aIuScale, aUnits, value_to_print );
297 value_to_print =
ToUserUnit( aIuScale, aUnits, value_to_print );
301 value_to_print =
ToUserUnit( aIuScale, aUnits, value_to_print );
310 if( value_to_print != 0.0 && fabs( value_to_print ) <= 0.0001 )
312 int len = snprintf( buf,
sizeof( buf ) - 1,
"%.10f", value_to_print );
314 while( --len > 0 && buf[len] ==
'0' )
317 if( len >= 0 && ( buf[len] ==
'.' || buf[len] ==
',' ) )
322 snprintf( buf,
sizeof( buf ) - 1,
"%.10g", value_to_print );
325 wxString stringValue( buf, wxConvUTF8 );
360 long long int aValue,
371 return wxString::Format( wxT(
"%.1f°" ), aValue.
AsDegrees() );
373 return wxString::Format( wxT(
"%.1f" ), aValue.
AsDegrees() );
379 double aValue,
bool aAddUnitsText,
383 const wxChar* format;
384 double value = aValue;
389 value =
ToUserUnit( aIuScale, aUnits, value );
394 value =
ToUserUnit( aIuScale, aUnits, value );
399 value =
ToUserUnit( aIuScale, aUnits, value );
410#if defined( EESCHEMA )
411 format = wxT(
"%.0f" );
413 format = wxT(
"%.1f" );
418#if defined( EESCHEMA )
419 format = wxT(
"%.2f" );
421 format = wxT(
"%.4f" );
426#if defined( EESCHEMA )
427 format = wxT(
"%.3f" );
429 format = wxT(
"%.5f" );
434#if defined( EESCHEMA )
435 format = wxT(
"%.0f" );
437 format = wxT(
"%.2f" );
442#if defined( EESCHEMA )
443 format = wxT(
"%.3f" );
445 format = wxT(
"%.4f" );
451 format = wxT(
"%.3f" );
455 format = wxT(
"%.0f" );
459 text.Printf( format, value );
474 if( aValue.
HasMin() && aValue.
Min() > 0 )
505 return MM_TO_IU( aValue / 1000.0, aIuScale );
508 return MM_TO_IU( aValue, aIuScale );
511 return MM_TO_IU( aValue * 10, aIuScale );
517 return IN_TO_IU( aValue, aIuScale );
533 const struct lconv* lc = localeconv();
535 wxChar decimal_point = lc->decimal_point[0];
536 wxString buf( aTextValue.Strip( wxString::both ) );
539 buf.Replace( wxT(
"." ), wxString( decimal_point, 1 ) );
540 buf.Replace( wxT(
"," ), wxString( decimal_point, 1 ) );
543 unsigned brk_point = 0;
545 while( brk_point < buf.Len() )
547 wxChar ch = buf[brk_point];
549 if( !( ( ch >=
'0' && ch <=
'9' ) || ( ch == decimal_point ) || ( ch ==
'-' )
559 buf.Left( brk_point ).ToDouble( &dtmp );
571 const struct lconv* lc = localeconv();
573 wxChar decimal_point = lc->decimal_point[0];
574 wxString buf( aTextValue.Strip( wxString::both ) );
577 buf.Replace( wxT(
"." ), wxString( decimal_point, 1 ) );
578 buf.Replace( wxT(
"," ), wxString( decimal_point, 1 ) );
581 unsigned brk_point = 0;
583 while( brk_point < buf.Len() )
585 wxChar ch = buf[brk_point];
587 if( !( (ch >=
'0' && ch <=
'9') || (ch == decimal_point) || (ch ==
'-') || (ch ==
'+') ) )
594 buf.Left( brk_point ).ToDouble( &dtmp );
597 wxString unit( buf.Mid( brk_point ).Strip( wxString::leading ).Left( 2 ).Lower() );
606 if( unit == wxT(
"um" ) || unit == wxT(
"\u00B5m" ) || unit == wxT(
"\u03BCm" ) )
610 else if( unit == wxT(
"mm" ) )
614 else if( unit == wxT(
"cm" ) )
618 else if( unit == wxT(
"mi" ) || unit == wxT(
"th" ) )
622 else if( unit == wxT(
"in" ) || unit == wxT(
"\"" ) )
626 else if( unit == wxT(
"oz" ) )
634 if( unit == wxT(
"ra" ) )
635 dtmp *= 180.0f / M_PI;
665 return KiROUND<double, long long int>( value );
673 return KiROUND<double, long long int>( value );
#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)
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.
constexpr ret_type KiROUND(fp_type v)
Round a floating point number to an integer using "round halfway cases away from zero".