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 (C) 1992-2023 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
77 KICOMMON_API bool FetchUnitsFromString( const wxString& aTextValue, EDA_UNITS& aUnits );
78
88 KICOMMON_API wxString GetText( EDA_UNITS aUnits, EDA_DATA_TYPE aType = EDA_DATA_TYPE::DISTANCE );
89
99 KICOMMON_API wxString GetLabel( EDA_UNITS aUnits,
100 EDA_DATA_TYPE aType = EDA_DATA_TYPE::DISTANCE );
101
112 KICOMMON_API std::string FormatAngle( const EDA_ANGLE& aAngle );
113
125 KICOMMON_API std::string FormatInternalUnits( const EDA_IU_SCALE& aIuScale, int aValue );
126 KICOMMON_API std::string FormatInternalUnits( const EDA_IU_SCALE& aIuScale,
127 const VECTOR2I& aPoint );
128
129#if 0 // No support for std::from_chars on MacOS yet
140 KICOMMON_API bool ParseInternalUnits( const std::string& aInput, const EDA_IU_SCALE& aIuScale,
141 int& aOut );
142
153 KICOMMON_API bool ParseInternalUnits( const std::string& aInput, const EDA_IU_SCALE& aIuScale,
154 VECTOR2I& aOut );
155#endif
156
157 constexpr inline int Mils2IU( const EDA_IU_SCALE& aIuScale, int mils )
158 {
159 double x = mils * aIuScale.IU_PER_MILS;
160 return int( x < 0 ? x - 0.5 : x + 0.5 );
161 }
162
163 namespace UI
164 {
173 KICOMMON_API double ToUserUnit( const EDA_IU_SCALE& aIuScale, EDA_UNITS aUnit,
174 double aValue );
175
189 KICOMMON_API wxString StringFromValue( const EDA_IU_SCALE& aIuScale, EDA_UNITS aUnits,
190 double aValue,
191 bool aAddUnitsText = false,
192 EDA_DATA_TYPE aType = EDA_DATA_TYPE::DISTANCE );
193
208 KICOMMON_API wxString MessageTextFromValue( const EDA_IU_SCALE& aIuScale, EDA_UNITS aUnits,
209 double aValue, bool aAddUnitsText = true,
210 EDA_DATA_TYPE aType = EDA_DATA_TYPE::DISTANCE );
211
212 KICOMMON_API wxString MessageTextFromValue( const EDA_IU_SCALE& aIuScale, EDA_UNITS aUnits,
213 int aValue, bool aAddUnitLabel = true,
214 EDA_DATA_TYPE aType = EDA_DATA_TYPE::DISTANCE );
215
216 KICOMMON_API wxString MessageTextFromValue( const EDA_IU_SCALE& aIuScale, EDA_UNITS aUnits,
217 long long int aValue, bool aAddUnitLabel = true,
218 EDA_DATA_TYPE aType = EDA_DATA_TYPE::DISTANCE );
219
220 KICOMMON_API wxString MessageTextFromValue( EDA_ANGLE aValue, bool aAddUnitLabel = true );
221
222
223 KICOMMON_API wxString MessageTextFromMinOptMax( const EDA_IU_SCALE& aIuScale,
224 EDA_UNITS aUnits,
225 const MINOPTMAX<int>& aValue );
230 KICOMMON_API double FromUserUnit( const EDA_IU_SCALE& aIuScale, EDA_UNITS aUnit,
231 double aValue );
232
233
244 KICOMMON_API double DoubleValueFromString( const EDA_IU_SCALE& aIuScale, EDA_UNITS aUnits,
245 const wxString& aTextValue,
246 EDA_DATA_TYPE aType = EDA_DATA_TYPE::DISTANCE );
247
248 KICOMMON_API double DoubleValueFromString( const wxString& aTextValue );
249
260 KICOMMON_API long long int ValueFromString( const EDA_IU_SCALE& aIuScale, EDA_UNITS aUnits,
261 const wxString& aTextValue,
262 EDA_DATA_TYPE aType = EDA_DATA_TYPE::DISTANCE );
263
264 KICOMMON_API long long int ValueFromString( const wxString& aTextValue );
265 }
266}
267
268#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:378
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:499
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.
Definition: eda_units.cpp:660
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.
Definition: eda_units.cpp:284
KICOMMON_API wxString MessageTextFromMinOptMax(const EDA_IU_SCALE &aIuScale, EDA_UNITS aUnits, const MINOPTMAX< int > &aValue)
Definition: eda_units.cpp:468
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.
Definition: eda_units.cpp:565
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...
Definition: eda_units.cpp:243
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:110
KICOMMON_API bool FetchUnitsFromString(const wxString &aTextValue, EDA_UNITS &aUnits)
Writes any unit info found in the string to aUnits.
Definition: eda_units.cpp:72
KICOMMON_API bool IsImperialUnit(EDA_UNITS aUnit)
Definition: eda_units.cpp:31
KICOMMON_API bool IsMetricUnit(EDA_UNITS aUnit)
Definition: eda_units.cpp:45
constexpr int Mils2IU(const EDA_IU_SCALE &aIuScale, int mils)
Definition: eda_units.h:157
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:139
KICOMMON_API int Mm2mils(double aVal)
Convert mm to mils.
Definition: eda_units.cpp:60
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:153
KICOMMON_API std::string FormatAngle(const EDA_ANGLE &aAngle)
Converts aAngle from board units to a string appropriate for writing to file.
Definition: eda_units.cpp:145
KICOMMON_API int Mils2mm(double aVal)
Convert mils to mm.
Definition: eda_units.cpp:66
const double IU_PER_MILS
Definition: base_units.h:78