2013-05-26 04:36:44 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2012 CERN
|
2021-02-01 17:57:16 +00:00
|
|
|
* Copyright (C) 1992-2021 KiCad Developers, see AUTHORS.txt for contributors.
|
2013-05-26 04:36:44 +00:00
|
|
|
*
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, you may find one here:
|
|
|
|
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
|
|
|
* or you may search the http://www.gnu.org website for the version 2 license,
|
|
|
|
* or you may write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2021-02-01 17:57:16 +00:00
|
|
|
* @author Wayne Stambaugh <stambaughw@gmail.com>
|
2013-05-26 04:36:44 +00:00
|
|
|
* @file base_units.cpp
|
|
|
|
* @brief Code to handle objects that require both schematic and board internal units.
|
|
|
|
* @note This file is an ugly hack to solve the problem of formatting the base units
|
|
|
|
* for either schematics or boards in objects that are include in both domains.
|
|
|
|
* At some point in the future. This code should be rolled back into the
|
|
|
|
* appropriate object and build with the correct internal unit formatting
|
|
|
|
* depending on the application.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <base_units.h>
|
2020-01-07 17:12:59 +00:00
|
|
|
#include <common.h>
|
2020-10-24 15:23:19 +00:00
|
|
|
#include <kicad_string.h>
|
2020-01-07 17:12:59 +00:00
|
|
|
#include <math/util.h> // for KiROUND
|
|
|
|
#include <macros.h>
|
|
|
|
#include <title_block.h>
|
|
|
|
|
2020-06-14 15:08:47 +00:00
|
|
|
#if defined( PCBNEW ) || defined( CVPCB ) || defined( EESCHEMA ) || defined( GERBVIEW ) || defined( PL_EDITOR )
|
2013-05-26 04:36:44 +00:00
|
|
|
#define IU_TO_MM( x ) ( x / IU_PER_MM )
|
|
|
|
#define IU_TO_IN( x ) ( x / IU_PER_MILS / 1000 )
|
2018-02-02 15:44:53 +00:00
|
|
|
#define IU_TO_MILS( x ) ( x / IU_PER_MILS )
|
2013-05-26 04:36:44 +00:00
|
|
|
#define MM_TO_IU( x ) ( x * IU_PER_MM )
|
|
|
|
#define IN_TO_IU( x ) ( x * IU_PER_MILS * 1000 )
|
2018-02-02 15:44:53 +00:00
|
|
|
#define MILS_TO_IU( x ) ( x * IU_PER_MILS )
|
2020-06-14 15:08:47 +00:00
|
|
|
#else
|
|
|
|
#error "Cannot resolve internal units due to no definition of EESCHEMA, CVPCB or PCBNEW."
|
|
|
|
#endif
|
2013-05-26 04:36:44 +00:00
|
|
|
|
2013-07-19 18:27:22 +00:00
|
|
|
|
2020-10-02 20:51:24 +00:00
|
|
|
double To_User_Unit( EDA_UNITS aUnit, double aValue )
|
2013-05-26 04:36:44 +00:00
|
|
|
{
|
|
|
|
switch( aUnit )
|
|
|
|
{
|
2019-12-20 14:11:39 +00:00
|
|
|
case EDA_UNITS::MILLIMETRES:
|
2013-05-26 04:36:44 +00:00
|
|
|
return IU_TO_MM( aValue );
|
|
|
|
|
2020-10-02 20:51:24 +00:00
|
|
|
case EDA_UNITS::MILS:
|
|
|
|
return IU_TO_MILS( aValue );
|
|
|
|
|
2019-12-20 14:11:39 +00:00
|
|
|
case EDA_UNITS::INCHES:
|
2020-10-02 20:51:24 +00:00
|
|
|
return IU_TO_IN( aValue );
|
2013-05-26 04:36:44 +00:00
|
|
|
|
2019-12-20 14:11:39 +00:00
|
|
|
case EDA_UNITS::DEGREES:
|
2015-02-12 03:22:24 +00:00
|
|
|
return aValue / 10.0f;
|
|
|
|
|
2013-05-26 04:36:44 +00:00
|
|
|
default:
|
|
|
|
return aValue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-01 17:57:16 +00:00
|
|
|
/**
|
|
|
|
* Convert a value to a string using double notation.
|
|
|
|
*
|
2013-05-26 04:36:44 +00:00
|
|
|
* For readability, the mantissa has 0, 1, 3 or 4 digits, depending on units
|
|
|
|
* for unit = inch the mantissa has 3 digits (Eeschema) or 4 digits
|
|
|
|
* for unit = mil the mantissa has 0 digits (Eeschema) or 1 digits
|
|
|
|
* for unit = mm the mantissa has 3 digits (Eeschema) or 4 digits
|
|
|
|
* Should be used only to display info in status,
|
|
|
|
* but not in dialogs, because 4 digits only
|
|
|
|
* could truncate the actual value
|
|
|
|
*/
|
2018-03-06 12:06:46 +00:00
|
|
|
|
|
|
|
// A lower-precision (for readability) version of StringFromValue()
|
2020-10-04 15:29:18 +00:00
|
|
|
wxString MessageTextFromValue( EDA_UNITS aUnits, int aValue, bool aAddUnitLabel,
|
|
|
|
EDA_DATA_TYPE aType )
|
2018-03-06 12:06:46 +00:00
|
|
|
{
|
2020-10-04 15:29:18 +00:00
|
|
|
return MessageTextFromValue( aUnits, double( aValue ), aAddUnitLabel, aType );
|
2019-09-03 23:38:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// A lower-precision (for readability) version of StringFromValue()
|
2020-10-04 15:29:18 +00:00
|
|
|
wxString MessageTextFromValue( EDA_UNITS aUnits, long long int aValue, bool aAddUnitLabel,
|
|
|
|
EDA_DATA_TYPE aType )
|
2019-09-03 23:38:58 +00:00
|
|
|
{
|
2020-10-04 15:29:18 +00:00
|
|
|
return MessageTextFromValue( aUnits, double( aValue ), aAddUnitLabel, aType );
|
2018-03-06 12:06:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// A lower-precision (for readability) version of StringFromValue()
|
2020-10-04 15:29:18 +00:00
|
|
|
wxString MessageTextFromValue( EDA_UNITS aUnits, double aValue, bool aAddUnitLabel,
|
|
|
|
EDA_DATA_TYPE aType )
|
2013-05-26 04:36:44 +00:00
|
|
|
{
|
|
|
|
wxString text;
|
|
|
|
const wxChar* format;
|
2020-02-05 09:43:52 +00:00
|
|
|
double value = aValue;
|
|
|
|
|
|
|
|
switch( aType )
|
|
|
|
{
|
|
|
|
case EDA_DATA_TYPE::VOLUME:
|
2020-10-02 20:51:24 +00:00
|
|
|
value = To_User_Unit( aUnits, value );
|
2020-02-05 09:43:52 +00:00
|
|
|
// Fall through to continue computation
|
2020-04-24 23:44:09 +00:00
|
|
|
KI_FALLTHROUGH;
|
2020-02-05 09:43:52 +00:00
|
|
|
|
|
|
|
case EDA_DATA_TYPE::AREA:
|
2020-10-02 20:51:24 +00:00
|
|
|
value = To_User_Unit( aUnits, value );
|
2020-02-05 09:43:52 +00:00
|
|
|
// Fall through to continue computation
|
2020-04-24 23:44:09 +00:00
|
|
|
KI_FALLTHROUGH;
|
2020-02-05 09:43:52 +00:00
|
|
|
|
|
|
|
case EDA_DATA_TYPE::DISTANCE:
|
2020-10-02 20:51:24 +00:00
|
|
|
value = To_User_Unit( aUnits, value );
|
2020-02-05 09:43:52 +00:00
|
|
|
}
|
2013-05-26 04:36:44 +00:00
|
|
|
|
2020-10-02 20:51:24 +00:00
|
|
|
switch( aUnits )
|
2013-05-26 04:36:44 +00:00
|
|
|
{
|
2020-10-02 20:51:24 +00:00
|
|
|
default:
|
|
|
|
case EDA_UNITS::MILLIMETRES:
|
2013-05-26 04:36:44 +00:00
|
|
|
#if defined( EESCHEMA )
|
2020-10-06 10:47:01 +00:00
|
|
|
format = wxT( "%.2f" );
|
2013-05-26 04:36:44 +00:00
|
|
|
#else
|
2020-10-07 22:24:27 +00:00
|
|
|
format = wxT( "%.4f" );
|
2013-05-26 04:36:44 +00:00
|
|
|
#endif
|
2020-10-02 20:51:24 +00:00
|
|
|
break;
|
2020-10-04 15:29:18 +00:00
|
|
|
|
2020-10-02 20:51:24 +00:00
|
|
|
case EDA_UNITS::MILS:
|
2013-05-26 04:36:44 +00:00
|
|
|
#if defined( EESCHEMA )
|
2020-10-02 20:51:24 +00:00
|
|
|
format = wxT( "%.0f" );
|
2013-05-26 04:36:44 +00:00
|
|
|
#else
|
2020-10-07 22:24:27 +00:00
|
|
|
format = wxT( "%.2f" );
|
2013-05-26 04:36:44 +00:00
|
|
|
#endif
|
2020-10-02 20:51:24 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case EDA_UNITS::INCHES:
|
2013-05-26 04:36:44 +00:00
|
|
|
#if defined( EESCHEMA )
|
|
|
|
format = wxT( "%.3f" );
|
2020-10-06 10:47:01 +00:00
|
|
|
#else
|
|
|
|
format = wxT( "%.4f" );
|
2013-05-26 04:36:44 +00:00
|
|
|
#endif
|
2020-10-02 20:51:24 +00:00
|
|
|
break;
|
2020-10-04 15:29:18 +00:00
|
|
|
|
2020-10-06 10:47:01 +00:00
|
|
|
case EDA_UNITS::DEGREES:
|
2020-12-10 14:46:20 +00:00
|
|
|
// 3 digits in mantissa should be good for rotation in degree
|
|
|
|
format = wxT( "%.3f" );
|
2020-10-06 10:47:01 +00:00
|
|
|
break;
|
|
|
|
|
2020-10-04 15:29:18 +00:00
|
|
|
case EDA_UNITS::UNSCALED:
|
|
|
|
format = wxT( "%.0f" );
|
|
|
|
break;
|
2013-05-26 04:36:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
text.Printf( format, value );
|
|
|
|
|
2020-10-04 15:29:18 +00:00
|
|
|
if( aAddUnitLabel )
|
|
|
|
{
|
|
|
|
text += " ";
|
|
|
|
text += GetAbbreviatedUnitsLabel( aUnits, aType );
|
|
|
|
}
|
2013-05-26 04:36:44 +00:00
|
|
|
|
|
|
|
return text;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-02-01 17:57:16 +00:00
|
|
|
/**
|
|
|
|
* Convert a value to a string using double notation.
|
|
|
|
*
|
2013-08-03 05:15:23 +00:00
|
|
|
* For readability, the mantissa has 3 or more digits,
|
2013-05-26 04:36:44 +00:00
|
|
|
* the trailing 0 are removed if the mantissa has more than 3 digits
|
|
|
|
* and some trailing 0
|
|
|
|
* This function should be used to display values in dialogs because a value
|
|
|
|
* entered in mm (for instance 2.0 mm) could need up to 8 digits mantissa
|
|
|
|
* if displayed in inch to avoid truncation or rounding made just by the printf function.
|
|
|
|
* otherwise the actual value is rounded when read from dialog and converted
|
|
|
|
* in internal units, and therefore modified.
|
|
|
|
*/
|
2021-02-01 17:57:16 +00:00
|
|
|
wxString StringFromValue( EDA_UNITS aUnits, double aValue, bool aAddUnitSymbol,
|
|
|
|
EDA_DATA_TYPE aType )
|
2013-05-26 04:36:44 +00:00
|
|
|
{
|
2020-06-29 16:16:37 +00:00
|
|
|
double value_to_print = aValue;
|
|
|
|
|
|
|
|
switch( aType )
|
|
|
|
{
|
|
|
|
case EDA_DATA_TYPE::VOLUME:
|
2020-10-02 20:51:24 +00:00
|
|
|
value_to_print = To_User_Unit( aUnits, value_to_print );
|
2020-06-29 16:16:37 +00:00
|
|
|
KI_FALLTHROUGH;
|
|
|
|
|
|
|
|
case EDA_DATA_TYPE::AREA:
|
2020-10-02 20:51:24 +00:00
|
|
|
value_to_print = To_User_Unit( aUnits, value_to_print );
|
2020-06-29 16:16:37 +00:00
|
|
|
KI_FALLTHROUGH;
|
|
|
|
|
|
|
|
case EDA_DATA_TYPE::DISTANCE:
|
2020-10-02 20:51:24 +00:00
|
|
|
value_to_print = To_User_Unit( aUnits, value_to_print );
|
2020-06-29 16:16:37 +00:00
|
|
|
}
|
|
|
|
|
2013-05-26 04:36:44 +00:00
|
|
|
|
2013-08-03 20:20:55 +00:00
|
|
|
char buf[50];
|
|
|
|
int len;
|
|
|
|
|
|
|
|
if( value_to_print != 0.0 && fabs( value_to_print ) <= 0.0001 )
|
|
|
|
{
|
|
|
|
len = sprintf( buf, "%.10f", value_to_print );
|
|
|
|
|
|
|
|
while( --len > 0 && buf[len] == '0' )
|
|
|
|
buf[len] = '\0';
|
|
|
|
|
|
|
|
if( buf[len]=='.' || buf[len]==',' )
|
|
|
|
buf[len] = '\0';
|
|
|
|
else
|
|
|
|
++len;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-10-02 20:51:24 +00:00
|
|
|
if( aUnits == EDA_UNITS::MILS )
|
2018-03-13 11:03:41 +00:00
|
|
|
len = sprintf( buf, "%.7g", value_to_print );
|
|
|
|
else
|
|
|
|
len = sprintf( buf, "%.10g", value_to_print );
|
2013-08-03 20:20:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
wxString stringValue( buf, wxConvUTF8 );
|
2013-08-03 05:15:23 +00:00
|
|
|
|
2013-05-26 04:36:44 +00:00
|
|
|
if( aAddUnitSymbol )
|
|
|
|
{
|
2018-03-06 12:06:46 +00:00
|
|
|
switch( aUnits )
|
2013-05-26 04:36:44 +00:00
|
|
|
{
|
2019-12-20 14:11:39 +00:00
|
|
|
case EDA_UNITS::MILLIMETRES:
|
2018-02-02 15:44:53 +00:00
|
|
|
stringValue += wxT( " mm" );
|
2013-05-26 04:36:44 +00:00
|
|
|
break;
|
|
|
|
|
2019-12-20 14:11:39 +00:00
|
|
|
case EDA_UNITS::DEGREES:
|
2018-02-02 15:44:53 +00:00
|
|
|
stringValue += wxT( " deg" );
|
2015-02-12 03:22:24 +00:00
|
|
|
break;
|
|
|
|
|
2020-10-02 20:51:24 +00:00
|
|
|
case EDA_UNITS::MILS:
|
|
|
|
stringValue += wxT( " mils" );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case EDA_UNITS::INCHES:
|
|
|
|
stringValue += wxT( " in" );
|
|
|
|
break;
|
|
|
|
|
2019-12-20 14:11:39 +00:00
|
|
|
case EDA_UNITS::PERCENT:
|
2019-06-29 16:36:29 +00:00
|
|
|
stringValue += wxT( "%" );
|
|
|
|
break;
|
|
|
|
|
2019-12-20 14:11:39 +00:00
|
|
|
case EDA_UNITS::UNSCALED:
|
2013-05-26 04:36:44 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return stringValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-10-02 20:51:24 +00:00
|
|
|
double From_User_Unit( EDA_UNITS aUnits, double aValue )
|
2013-05-26 04:36:44 +00:00
|
|
|
{
|
2018-03-06 12:06:46 +00:00
|
|
|
switch( aUnits )
|
2013-05-26 04:36:44 +00:00
|
|
|
{
|
2019-12-20 14:11:39 +00:00
|
|
|
case EDA_UNITS::MILLIMETRES:
|
2018-02-02 15:44:53 +00:00
|
|
|
return MM_TO_IU( aValue );
|
2013-05-26 04:36:44 +00:00
|
|
|
|
2020-10-02 20:51:24 +00:00
|
|
|
case EDA_UNITS::MILS:
|
|
|
|
return MILS_TO_IU( aValue );
|
|
|
|
|
2019-12-20 14:11:39 +00:00
|
|
|
case EDA_UNITS::INCHES:
|
2020-10-02 20:51:24 +00:00
|
|
|
return IN_TO_IU( aValue );
|
2013-05-26 04:36:44 +00:00
|
|
|
|
2019-12-20 14:11:39 +00:00
|
|
|
case EDA_UNITS::DEGREES:
|
2015-02-12 03:22:24 +00:00
|
|
|
// Convert to "decidegrees"
|
2018-02-02 15:44:53 +00:00
|
|
|
return aValue * 10;
|
2015-02-12 03:22:24 +00:00
|
|
|
|
2013-05-26 04:36:44 +00:00
|
|
|
default:
|
2019-12-20 14:11:39 +00:00
|
|
|
case EDA_UNITS::UNSCALED:
|
|
|
|
case EDA_UNITS::PERCENT:
|
2018-02-02 15:44:53 +00:00
|
|
|
return aValue;
|
2013-05-26 04:36:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-10-02 20:51:24 +00:00
|
|
|
double DoubleValueFromString( EDA_UNITS aUnits, const wxString& aTextValue, EDA_DATA_TYPE aType )
|
2013-05-26 04:36:44 +00:00
|
|
|
{
|
|
|
|
double dtmp = 0;
|
|
|
|
|
|
|
|
// Acquire the 'right' decimal point separator
|
|
|
|
const struct lconv* lc = localeconv();
|
2013-07-09 05:48:26 +00:00
|
|
|
|
|
|
|
wxChar decimal_point = lc->decimal_point[0];
|
|
|
|
wxString buf( aTextValue.Strip( wxString::both ) );
|
2013-05-26 04:36:44 +00:00
|
|
|
|
2021-01-09 17:16:09 +00:00
|
|
|
// Convert any entered decimal point separators to the 'right' one
|
2013-05-26 04:36:44 +00:00
|
|
|
buf.Replace( wxT( "." ), wxString( decimal_point, 1 ) );
|
2021-01-09 17:16:09 +00:00
|
|
|
buf.Replace( wxT( "," ), wxString( decimal_point, 1 ) );
|
2013-05-26 04:36:44 +00:00
|
|
|
|
|
|
|
// Find the end of the numeric part
|
|
|
|
unsigned brk_point = 0;
|
|
|
|
|
|
|
|
while( brk_point < buf.Len() )
|
|
|
|
{
|
|
|
|
wxChar ch = buf[brk_point];
|
|
|
|
|
2021-01-09 17:16:09 +00:00
|
|
|
if( !( (ch >= '0' && ch <= '9') || (ch == decimal_point) || (ch == '-') || (ch == '+') ) )
|
2013-05-26 04:36:44 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
++brk_point;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Extract the numeric part
|
2021-03-20 12:17:11 +00:00
|
|
|
buf.Left( brk_point ).ToDouble( &dtmp );
|
2013-05-26 04:36:44 +00:00
|
|
|
|
|
|
|
// Check the optional unit designator (2 ch significant)
|
|
|
|
wxString unit( buf.Mid( brk_point ).Strip( wxString::leading ).Left( 2 ).Lower() );
|
|
|
|
|
2021-02-01 17:57:16 +00:00
|
|
|
if( aUnits == EDA_UNITS::MILLIMETRES || aUnits == EDA_UNITS::MILS
|
|
|
|
|| aUnits == EDA_UNITS::INCHES )
|
2013-05-26 04:36:44 +00:00
|
|
|
{
|
2020-10-02 20:51:24 +00:00
|
|
|
if( unit == wxT( "mm" ) )
|
2015-02-12 03:22:24 +00:00
|
|
|
{
|
2020-10-02 20:51:24 +00:00
|
|
|
aUnits = EDA_UNITS::MILLIMETRES;
|
2015-02-12 03:22:24 +00:00
|
|
|
}
|
2020-10-02 20:51:24 +00:00
|
|
|
else if( unit == wxT( "mi" ) || unit == wxT( "th" ) )
|
2015-02-12 03:22:24 +00:00
|
|
|
{
|
2020-10-02 20:51:24 +00:00
|
|
|
aUnits = EDA_UNITS::MILS;
|
2015-02-12 03:22:24 +00:00
|
|
|
}
|
2020-10-02 20:51:24 +00:00
|
|
|
else if( unit == wxT( "in" ) || unit == wxT( "\"" ) )
|
2015-02-12 03:22:24 +00:00
|
|
|
{
|
2019-12-20 14:11:39 +00:00
|
|
|
aUnits = EDA_UNITS::INCHES;
|
2015-02-12 03:22:24 +00:00
|
|
|
}
|
2020-10-02 20:51:24 +00:00
|
|
|
else if( unit == "oz" ) // 1 oz = 1.37 mils
|
2019-07-27 19:09:43 +00:00
|
|
|
{
|
2020-10-02 20:51:24 +00:00
|
|
|
aUnits = EDA_UNITS::MILS;
|
2019-07-27 19:09:43 +00:00
|
|
|
dtmp *= 1.37;
|
|
|
|
}
|
2013-05-26 04:36:44 +00:00
|
|
|
}
|
2019-12-20 14:11:39 +00:00
|
|
|
else if( aUnits == EDA_UNITS::DEGREES )
|
2013-05-26 04:36:44 +00:00
|
|
|
{
|
2015-02-12 03:22:24 +00:00
|
|
|
if( unit == wxT( "ra" ) ) // Radians
|
|
|
|
{
|
|
|
|
dtmp *= 180.0f / M_PI;
|
|
|
|
}
|
2013-05-26 04:36:44 +00:00
|
|
|
}
|
|
|
|
|
2020-06-29 16:16:37 +00:00
|
|
|
switch( aType )
|
|
|
|
{
|
|
|
|
case EDA_DATA_TYPE::VOLUME:
|
2020-10-02 20:51:24 +00:00
|
|
|
dtmp = From_User_Unit( aUnits, dtmp );
|
2020-06-29 16:16:37 +00:00
|
|
|
KI_FALLTHROUGH;
|
|
|
|
|
|
|
|
case EDA_DATA_TYPE::AREA:
|
2020-10-02 20:51:24 +00:00
|
|
|
dtmp = From_User_Unit( aUnits, dtmp );
|
2020-06-29 16:16:37 +00:00
|
|
|
KI_FALLTHROUGH;
|
|
|
|
|
|
|
|
case EDA_DATA_TYPE::DISTANCE:
|
2020-10-02 20:51:24 +00:00
|
|
|
dtmp = From_User_Unit( aUnits, dtmp );
|
2020-06-29 16:16:37 +00:00
|
|
|
}
|
2013-05-26 04:36:44 +00:00
|
|
|
|
2020-06-29 16:16:37 +00:00
|
|
|
return dtmp;
|
2015-02-12 03:22:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-10-02 20:51:24 +00:00
|
|
|
void FetchUnitsFromString( const wxString& aTextValue, EDA_UNITS& aUnits )
|
2018-07-21 12:49:19 +00:00
|
|
|
{
|
2018-07-26 20:10:55 +00:00
|
|
|
wxString buf( aTextValue.Strip( wxString::both ) );
|
2018-07-21 12:49:19 +00:00
|
|
|
unsigned brk_point = 0;
|
|
|
|
|
|
|
|
while( brk_point < buf.Len() )
|
|
|
|
{
|
2018-07-26 20:10:55 +00:00
|
|
|
wxChar c = buf[brk_point];
|
2018-07-21 12:49:19 +00:00
|
|
|
|
2018-07-26 20:10:55 +00:00
|
|
|
if( !( (c >= '0' && c <='9') || (c == '.') || (c == ',') || (c == '-') || (c == '+') ) )
|
2018-07-21 12:49:19 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
++brk_point;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check the unit designator (2 ch significant)
|
|
|
|
wxString unit( buf.Mid( brk_point ).Strip( wxString::leading ).Left( 2 ).Lower() );
|
|
|
|
|
2020-10-02 20:51:24 +00:00
|
|
|
if( unit == wxT( "mm" ) )
|
2019-12-20 14:11:39 +00:00
|
|
|
aUnits = EDA_UNITS::MILLIMETRES;
|
2020-10-02 20:51:24 +00:00
|
|
|
else if( unit == wxT( "mi" ) || unit == wxT( "th" ) ) // "mils" or "thou"
|
|
|
|
aUnits = EDA_UNITS::MILS;
|
|
|
|
else if( unit == wxT( "in" ) || unit == wxT( "\"" ) )
|
2019-12-20 14:11:39 +00:00
|
|
|
aUnits = EDA_UNITS::INCHES;
|
2020-10-02 20:51:24 +00:00
|
|
|
else if( unit == wxT( "de" ) || unit == wxT( "ra" ) ) // "deg" or "rad"
|
2019-12-20 14:11:39 +00:00
|
|
|
aUnits = EDA_UNITS::DEGREES;
|
2018-07-21 12:49:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-10-02 20:51:24 +00:00
|
|
|
long long int ValueFromString( EDA_UNITS aUnits, const wxString& aTextValue, EDA_DATA_TYPE aType )
|
2015-02-12 03:22:24 +00:00
|
|
|
{
|
2020-10-02 20:51:24 +00:00
|
|
|
double value = DoubleValueFromString( aUnits, aTextValue, aType );
|
2021-04-14 17:47:03 +00:00
|
|
|
|
2019-09-03 23:38:58 +00:00
|
|
|
return KiROUND<double, long long int>( value );
|
2013-05-26 04:36:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-10-02 20:51:24 +00:00
|
|
|
wxString GetAbbreviatedUnitsLabel( EDA_UNITS aUnit, EDA_DATA_TYPE aType )
|
2017-02-09 10:16:28 +00:00
|
|
|
{
|
|
|
|
switch( aUnit )
|
|
|
|
{
|
2019-12-20 14:11:39 +00:00
|
|
|
case EDA_UNITS::MILLIMETRES:
|
2020-02-05 09:43:52 +00:00
|
|
|
switch( aType )
|
|
|
|
{
|
2020-04-27 07:23:25 +00:00
|
|
|
default:
|
|
|
|
wxASSERT( 0 );
|
|
|
|
KI_FALLTHROUGH;
|
2020-02-05 09:43:52 +00:00
|
|
|
case EDA_DATA_TYPE::DISTANCE:
|
|
|
|
return _( "mm" );
|
|
|
|
case EDA_DATA_TYPE::AREA:
|
|
|
|
return _( "sq. mm" );
|
|
|
|
case EDA_DATA_TYPE::VOLUME:
|
|
|
|
return _( "cu. mm" );
|
|
|
|
}
|
2017-02-09 10:16:28 +00:00
|
|
|
|
2020-10-02 20:51:24 +00:00
|
|
|
case EDA_UNITS::MILS:
|
|
|
|
switch( aType )
|
|
|
|
{
|
|
|
|
default:
|
|
|
|
wxASSERT( 0 );
|
|
|
|
KI_FALLTHROUGH;
|
|
|
|
case EDA_DATA_TYPE::DISTANCE:
|
|
|
|
return _( "mils" );
|
|
|
|
case EDA_DATA_TYPE::AREA:
|
|
|
|
return _( "sq. mils" );
|
|
|
|
case EDA_DATA_TYPE::VOLUME:
|
|
|
|
return _( "cu. mils" );
|
|
|
|
}
|
2020-10-04 15:29:18 +00:00
|
|
|
|
2020-10-02 20:51:24 +00:00
|
|
|
case EDA_UNITS::INCHES:
|
|
|
|
switch( aType )
|
|
|
|
{
|
|
|
|
default:
|
|
|
|
wxASSERT( 0 );
|
|
|
|
KI_FALLTHROUGH;
|
|
|
|
case EDA_DATA_TYPE::DISTANCE:
|
|
|
|
return _( "in" );
|
|
|
|
case EDA_DATA_TYPE::AREA:
|
|
|
|
return _( "sq. in" );
|
|
|
|
case EDA_DATA_TYPE::VOLUME:
|
|
|
|
return _( "cu. in" );
|
|
|
|
}
|
|
|
|
|
2019-12-20 14:11:39 +00:00
|
|
|
case EDA_UNITS::PERCENT:
|
2019-06-29 16:36:29 +00:00
|
|
|
return _( "%" );
|
|
|
|
|
2019-12-20 14:11:39 +00:00
|
|
|
case EDA_UNITS::UNSCALED:
|
2018-02-02 15:44:53 +00:00
|
|
|
return wxEmptyString;
|
2017-02-09 10:16:28 +00:00
|
|
|
|
2019-12-20 14:11:39 +00:00
|
|
|
case EDA_UNITS::DEGREES:
|
2018-02-02 15:44:53 +00:00
|
|
|
return _( "deg" );
|
2017-02-09 10:16:28 +00:00
|
|
|
|
|
|
|
default:
|
2020-04-27 07:23:25 +00:00
|
|
|
return wxT( "??" );
|
2017-02-09 10:16:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-08 05:42:50 +00:00
|
|
|
|
|
|
|
std::string FormatInternalUnits( int aValue )
|
|
|
|
{
|
|
|
|
char buf[50];
|
|
|
|
double engUnits = aValue;
|
|
|
|
int len;
|
|
|
|
|
|
|
|
engUnits /= IU_PER_MM;
|
|
|
|
|
|
|
|
if( engUnits != 0.0 && fabs( engUnits ) <= 0.0001 )
|
|
|
|
{
|
|
|
|
len = snprintf( buf, sizeof(buf), "%.10f", engUnits );
|
|
|
|
|
2021-02-01 17:57:16 +00:00
|
|
|
// Make sure snprintf() didn't fail and the locale numeric separator is correct.
|
|
|
|
wxCHECK( len >= 0 && len < 50 && strchr( buf, ',' ) == NULL, std::string( "" ) );
|
|
|
|
|
2019-01-08 05:42:50 +00:00
|
|
|
while( --len > 0 && buf[len] == '0' )
|
|
|
|
buf[len] = '\0';
|
|
|
|
|
|
|
|
if( buf[len] == '.' )
|
|
|
|
buf[len] = '\0';
|
|
|
|
else
|
|
|
|
++len;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
len = snprintf( buf, sizeof(buf), "%.10g", engUnits );
|
2021-02-01 17:57:16 +00:00
|
|
|
|
|
|
|
// Make sure snprintf() didn't fail and the locale numeric separator is correct.
|
|
|
|
wxCHECK( len >= 0 && len < 50 && strchr( buf, ',' ) == NULL , std::string( "" ) );
|
2019-01-08 05:42:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return std::string( buf, len );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
std::string FormatAngle( double aAngle )
|
|
|
|
{
|
|
|
|
char temp[50];
|
|
|
|
int len;
|
|
|
|
|
|
|
|
len = snprintf( temp, sizeof(temp), "%.10g", aAngle / 10.0 );
|
|
|
|
|
|
|
|
return std::string( temp, len );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
std::string FormatInternalUnits( const wxPoint& aPoint )
|
|
|
|
{
|
|
|
|
return FormatInternalUnits( aPoint.x ) + " " + FormatInternalUnits( aPoint.y );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
std::string FormatInternalUnits( const VECTOR2I& aPoint )
|
|
|
|
{
|
|
|
|
return FormatInternalUnits( aPoint.x ) + " " + FormatInternalUnits( aPoint.y );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
std::string FormatInternalUnits( const wxSize& aSize )
|
|
|
|
{
|
|
|
|
return FormatInternalUnits( aSize.GetWidth() ) + " " + FormatInternalUnits( aSize.GetHeight() );
|
|
|
|
}
|