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{
40 AREA = 1,
41 VOLUME = 2,
43 TIME = 4,
45};
46
47enum class EDA_UNITS
48{
49 INCH = 0, // Do not use IN: it conflicts with a Windows header
50 MM = 1,
54 MILS = 5,
55 UM = 6,
56 CM = 7,
57 FS = 8, // Femtoseconds
58 PS = 9, // Picoseconds
62};
63
65{
67
69
73 KICOMMON_API int Mm2mils( double aVal );
74
78 KICOMMON_API int Mils2mm( double aVal );
79
85 KICOMMON_API bool FetchUnitsFromString( const wxString& aTextValue, EDA_UNITS& aUnits );
86
96 KICOMMON_API wxString GetText( EDA_UNITS aUnits,
98
108 KICOMMON_API wxString GetLabel( EDA_UNITS aUnits,
110
121 KICOMMON_API std::string FormatAngle( const EDA_ANGLE& aAngle );
122
134 KICOMMON_API std::string FormatInternalUnits( const EDA_IU_SCALE& aIuScale, int aValue );
135 KICOMMON_API std::string FormatInternalUnits( const EDA_IU_SCALE& aIuScale,
136 const VECTOR2I& aPoint );
137
138#if 0 // No support for std::from_chars on MacOS yet
149 KICOMMON_API bool ParseInternalUnits( const std::string& aInput, const EDA_IU_SCALE& aIuScale,
150 int& aOut );
151
162 KICOMMON_API bool ParseInternalUnits( const std::string& aInput, const EDA_IU_SCALE& aIuScale,
163 VECTOR2I& aOut );
164#endif
165
166 constexpr inline int Mils2IU( const EDA_IU_SCALE& aIuScale, int mils )
167 {
168 double x = mils * aIuScale.IU_PER_MILS;
169 return int( x < 0 ? x - 0.5 : x + 0.5 );
170 }
171
172 namespace UI
173 {
181 KICOMMON_API double ToUserUnit( const EDA_IU_SCALE& aIuScale, EDA_UNITS aUnit,
182 double aValue );
183
198 KICOMMON_API wxString StringFromValue( const EDA_IU_SCALE& aIuScale, EDA_UNITS aUnits,
199 double aValue,
200 bool aAddUnitsText = false,
202
217 KICOMMON_API wxString MessageTextFromValue( const EDA_IU_SCALE& aIuScale, EDA_UNITS aUnits,
218 double aValue, bool aAddUnitsText = true,
220
221 KICOMMON_API wxString MessageTextFromValue( const EDA_IU_SCALE& aIuScale, EDA_UNITS aUnits,
222 int aValue, bool aAddUnitLabel = true,
224
225 KICOMMON_API wxString MessageTextFromValue( const EDA_IU_SCALE& aIuScale, EDA_UNITS aUnits,
226 long long int aValue, bool aAddUnitLabel = true,
228
229 KICOMMON_API wxString MessageTextFromValue( EDA_ANGLE aValue, bool aAddUnitLabel = true );
230
231
232 KICOMMON_API wxString MessageTextFromMinOptMax( const EDA_IU_SCALE& aIuScale,
233 EDA_UNITS aUnits,
234 const MINOPTMAX<int>& aValue );
239 KICOMMON_API double FromUserUnit( const EDA_IU_SCALE& aIuScale, EDA_UNITS aUnit,
240 double aValue );
241
242
253 KICOMMON_API double DoubleValueFromString( const EDA_IU_SCALE& aIuScale, EDA_UNITS aUnits,
254 const wxString& aTextValue,
256
257 KICOMMON_API double DoubleValueFromString( const wxString& aTextValue );
258
259 KICOMMON_API bool DoubleValueFromString( const EDA_IU_SCALE& aIuScale, const wxString& aTextValue,
260 double& aDoubleValue );
261
272 KICOMMON_API long long int ValueFromString( const EDA_IU_SCALE& aIuScale, EDA_UNITS aUnits,
273 const wxString& aTextValue,
275
276 KICOMMON_API long long int ValueFromString( const wxString& aTextValue );
277 }
278}
279
280#endif
EDA_DATA_TYPE
The type of unit.
Definition eda_units.h:38
EDA_UNITS
Definition eda_units.h:48
@ PS_PER_INCH
Definition eda_units.h:59
#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.
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)
Convert 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)
Return 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)
Convert aTextValue to a double.
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.
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)
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:166
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.
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.
KICOMMON_API std::string FormatAngle(const EDA_ANGLE &aAngle)
Convert aAngle from board units to a string appropriate for writing to file.
KICOMMON_API int Mils2mm(double aVal)
Convert mils to mm.
Definition eda_units.cpp:82
@ INCH
Definition rs274x.cpp:62
const double IU_PER_MILS
Definition base_units.h:77
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:695