KiCad PCB EDA Suite
Loading...
Searching...
No Matches
eda_units.h
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, you may find one here:
18 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19 * or you may search the http://www.gnu.org website for the version 2 license,
20 * or you may write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 */
23
24
25#ifndef EDA_UNITS_H
26#define EDA_UNITS_H
27
28#include <kicommon.h>
29#include <wx/string.h>
30#include <geometry/eda_angle.h>
31#include <base_units.h>
32#include <core/minoptmax.h>
33
37enum class EDA_DATA_TYPE
38{
39 DISTANCE = 0,
40 AREA = 1,
41 VOLUME = 2,
42 UNITLESS = 3
43};
44
45enum class EDA_UNITS
46{
47 INCHES = 0,
48 MILLIMETRES = 1,
49 UNSCALED = 2,
50 DEGREES = 3,
51 PERCENT = 4,
52 MILS = 5,
53 MICROMETRES = 6, //The S is because MILLIMETRES already has an S at the end. But it is
54 CENTIMETRES = 7, //micrometre and centimetre and not micrometre (or cm) times second.
55};
56
58{
60
62
66 KICOMMON_API int Mm2mils( double aVal );
67
71 KICOMMON_API int Mils2mm( double aVal );
72
78 KICOMMON_API bool FetchUnitsFromString( const wxString& aTextValue, EDA_UNITS& aUnits );
79
89 KICOMMON_API wxString GetText( EDA_UNITS aUnits,
90 EDA_DATA_TYPE aType = EDA_DATA_TYPE::DISTANCE );
91
101 KICOMMON_API wxString GetLabel( EDA_UNITS aUnits,
102 EDA_DATA_TYPE aType = EDA_DATA_TYPE::DISTANCE );
103
114 KICOMMON_API std::string FormatAngle( const EDA_ANGLE& aAngle );
115
127 KICOMMON_API std::string FormatInternalUnits( const EDA_IU_SCALE& aIuScale, int aValue );
128 KICOMMON_API std::string FormatInternalUnits( const EDA_IU_SCALE& aIuScale,
129 const VECTOR2I& aPoint );
130
131#if 0 // No support for std::from_chars on MacOS yet
142 KICOMMON_API bool ParseInternalUnits( const std::string& aInput, const EDA_IU_SCALE& aIuScale,
143 int& aOut );
144
155 KICOMMON_API bool ParseInternalUnits( const std::string& aInput, const EDA_IU_SCALE& aIuScale,
156 VECTOR2I& aOut );
157#endif
158
159 constexpr inline int Mils2IU( const EDA_IU_SCALE& aIuScale, int mils )
160 {
161 double x = mils * aIuScale.IU_PER_MILS;
162 return int( x < 0 ? x - 0.5 : x + 0.5 );
163 }
164
165 namespace UI
166 {
174 KICOMMON_API double ToUserUnit( const EDA_IU_SCALE& aIuScale, EDA_UNITS aUnit,
175 double aValue );
176
191 KICOMMON_API wxString StringFromValue( const EDA_IU_SCALE& aIuScale, EDA_UNITS aUnits,
192 double aValue,
193 bool aAddUnitsText = false,
194 EDA_DATA_TYPE aType = EDA_DATA_TYPE::DISTANCE );
195
210 KICOMMON_API wxString MessageTextFromValue( const EDA_IU_SCALE& aIuScale, EDA_UNITS aUnits,
211 double aValue, bool aAddUnitsText = true,
212 EDA_DATA_TYPE aType = EDA_DATA_TYPE::DISTANCE );
213
214 KICOMMON_API wxString MessageTextFromValue( const EDA_IU_SCALE& aIuScale, EDA_UNITS aUnits,
215 int aValue, bool aAddUnitLabel = true,
216 EDA_DATA_TYPE aType = EDA_DATA_TYPE::DISTANCE );
217
218 KICOMMON_API wxString MessageTextFromValue( const EDA_IU_SCALE& aIuScale, EDA_UNITS aUnits,
219 long long int aValue, bool aAddUnitLabel = true,
220 EDA_DATA_TYPE aType = EDA_DATA_TYPE::DISTANCE );
221
222 KICOMMON_API wxString MessageTextFromValue( EDA_ANGLE aValue, bool aAddUnitLabel = true );
223
224
225 KICOMMON_API wxString MessageTextFromMinOptMax( const EDA_IU_SCALE& aIuScale,
226 EDA_UNITS aUnits,
227 const MINOPTMAX<int>& aValue );
232 KICOMMON_API double FromUserUnit( const EDA_IU_SCALE& aIuScale, EDA_UNITS aUnit,
233 double aValue );
234
235
246 KICOMMON_API double DoubleValueFromString( const EDA_IU_SCALE& aIuScale, EDA_UNITS aUnits,
247 const wxString& aTextValue,
248 EDA_DATA_TYPE aType = EDA_DATA_TYPE::DISTANCE );
249
250 KICOMMON_API double DoubleValueFromString( const wxString& aTextValue );
251
262 KICOMMON_API long long int ValueFromString( const EDA_IU_SCALE& aIuScale, EDA_UNITS aUnits,
263 const wxString& aTextValue,
264 EDA_DATA_TYPE aType = EDA_DATA_TYPE::DISTANCE );
265
266 KICOMMON_API long long int ValueFromString( const wxString& aTextValue );
267 }
268}
269
270#endif
EDA_DATA_TYPE
The type of unit.
Definition: eda_units.h:38
EDA_UNITS
Definition: eda_units.h:46
#define KICOMMON_API
Definition: kicommon.h:28
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.
Definition: eda_units.cpp:385
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",...
Definition: eda_units.cpp:487
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.
Definition: eda_units.cpp:648
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.
Definition: eda_units.cpp:290
KICOMMON_API wxString MessageTextFromMinOptMax(const EDA_IU_SCALE &aIuScale, EDA_UNITS aUnits, const MINOPTMAX< int > &aValue)
Definition: eda_units.cpp:456
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.
Definition: eda_units.cpp:553
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.
Definition: eda_units.cpp:261
KICOMMON_API wxString GetText(EDA_UNITS aUnits, EDA_DATA_TYPE aType=EDA_DATA_TYPE::DISTANCE)
Get the units string for a given units type.
Definition: eda_units.cpp:127
KICOMMON_API bool FetchUnitsFromString(const wxString &aTextValue, EDA_UNITS &aUnits)
Write any unit info found in the string to aUnits.
Definition: eda_units.cpp:88
KICOMMON_API bool IsImperialUnit(EDA_UNITS aUnit)
Definition: eda_units.cpp:47
KICOMMON_API bool IsMetricUnit(EDA_UNITS aUnit)
Definition: eda_units.cpp:61
constexpr int Mils2IU(const EDA_IU_SCALE &aIuScale, int mils)
Definition: eda_units.h:159
KICOMMON_API wxString GetLabel(EDA_UNITS aUnits, EDA_DATA_TYPE aType=EDA_DATA_TYPE::DISTANCE)
Get the units string for a given units type.
Definition: eda_units.cpp:156
KICOMMON_API int Mm2mils(double aVal)
Convert mm to mils.
Definition: eda_units.cpp:76
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.
Definition: eda_units.cpp:170
KICOMMON_API std::string FormatAngle(const EDA_ANGLE &aAngle)
Convert aAngle from board units to a string appropriate for writing to file.
Definition: eda_units.cpp:162
KICOMMON_API int Mils2mm(double aVal)
Convert mils to mm.
Definition: eda_units.cpp:82
const double IU_PER_MILS
Definition: base_units.h:77