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, see <https://www.gnu.org/licenses/>.
18 */
19
20
21#ifndef EDA_UNITS_H
22#define EDA_UNITS_H
23
24#include <kicommon.h>
25#include <wx/string.h>
26#include <geometry/eda_angle.h>
27#include <base_units.h>
28#include <core/minoptmax.h>
29
33enum class EDA_DATA_TYPE
34{
36 AREA = 1,
37 VOLUME = 2,
39 TIME = 4,
41};
42
43enum class EDA_UNITS
44{
45 INCH = 0, // Do not use IN: it conflicts with a Windows header
46 MM = 1,
50 MILS = 5,
51 UM = 6,
52 CM = 7,
53 FS = 8, // Femtoseconds
54 PS = 9, // Picoseconds
58};
59
61{
63
65
69 KICOMMON_API int Mm2mils( double aVal );
70
74 KICOMMON_API int Mils2mm( double aVal );
75
81 KICOMMON_API bool FetchUnitsFromString( const wxString& aTextValue, EDA_UNITS& aUnits );
82
92 KICOMMON_API wxString GetText( EDA_UNITS aUnits,
94
104 KICOMMON_API wxString GetLabel( EDA_UNITS aUnits,
106
117 KICOMMON_API std::string FormatAngle( const EDA_ANGLE& aAngle );
118
131 KICOMMON_API std::string FormatInternalUnits( const EDA_IU_SCALE& aIuScale, int aValue,
133 KICOMMON_API std::string FormatInternalUnits( const EDA_IU_SCALE& aIuScale,
134 const VECTOR2I& aPoint );
135
141 KICOMMON_API double GetScaleForInternalUnitType( const EDA_IU_SCALE& aIuScale, EDA_DATA_TYPE aDataType );
142
143#if 0 // No support for std::from_chars on MacOS yet
154 KICOMMON_API bool ParseInternalUnits( const std::string& aInput, const EDA_IU_SCALE& aIuScale,
155 int& aOut );
156
167 KICOMMON_API bool ParseInternalUnits( const std::string& aInput, const EDA_IU_SCALE& aIuScale,
168 VECTOR2I& aOut );
169#endif
170
171 constexpr inline int Mils2IU( const EDA_IU_SCALE& aIuScale, int mils )
172 {
173 double x = mils * aIuScale.IU_PER_MILS;
174 return int( x < 0 ? x - 0.5 : x + 0.5 );
175 }
176
177 namespace UI
178 {
186 KICOMMON_API double ToUserUnit( const EDA_IU_SCALE& aIuScale, EDA_UNITS aUnit,
187 double aValue );
188
203 KICOMMON_API wxString StringFromValue( const EDA_IU_SCALE& aIuScale, EDA_UNITS aUnits,
204 double aValue,
205 bool aAddUnitsText = false,
207
222 KICOMMON_API wxString MessageTextFromValue( const EDA_IU_SCALE& aIuScale, EDA_UNITS aUnits,
223 double aValue, bool aAddUnitsText = true,
225
226 KICOMMON_API wxString MessageTextFromValue( const EDA_IU_SCALE& aIuScale, EDA_UNITS aUnits,
227 int aValue, bool aAddUnitLabel = true,
229
230 KICOMMON_API wxString MessageTextFromValue( const EDA_IU_SCALE& aIuScale, EDA_UNITS aUnits,
231 long long int aValue, bool aAddUnitLabel = true,
233
234 KICOMMON_API wxString MessageTextFromValue( EDA_ANGLE aValue, bool aAddUnitLabel = true );
235
236
237 KICOMMON_API wxString MessageTextFromMinOptMax( const EDA_IU_SCALE& aIuScale,
238 EDA_UNITS aUnits,
239 const MINOPTMAX<int>& aValue );
244 KICOMMON_API double FromUserUnit( const EDA_IU_SCALE& aIuScale, EDA_UNITS aUnit,
245 double aValue );
246
247
258 KICOMMON_API double DoubleValueFromString( const EDA_IU_SCALE& aIuScale, EDA_UNITS aUnits,
259 const wxString& aTextValue,
261
262 KICOMMON_API double DoubleValueFromString( const wxString& aTextValue );
263
264 KICOMMON_API bool DoubleValueFromString( const EDA_IU_SCALE& aIuScale, const wxString& aTextValue,
265 double& aDoubleValue );
266
277 KICOMMON_API long long int ValueFromString( const EDA_IU_SCALE& aIuScale, EDA_UNITS aUnits,
278 const wxString& aTextValue,
280
281 KICOMMON_API long long int ValueFromString( const wxString& aTextValue );
282 }
283}
284
285#endif
EDA_DATA_TYPE
The type of unit.
Definition eda_units.h:34
EDA_UNITS
Definition eda_units.h:44
@ PS_PER_INCH
Definition eda_units.h:55
#define KICOMMON_API
Definition kicommon.h:27
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:84
KICOMMON_API bool IsImperialUnit(EDA_UNITS aUnit)
Definition eda_units.cpp:43
KICOMMON_API bool IsMetricUnit(EDA_UNITS aUnit)
Definition eda_units.cpp:57
constexpr int Mils2IU(const EDA_IU_SCALE &aIuScale, int mils)
Definition eda_units.h:171
KICOMMON_API double GetScaleForInternalUnitType(const EDA_IU_SCALE &aIuScale, EDA_DATA_TYPE aDataType)
Returns the scaling parameter for the given units data type.
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:72
KICOMMON_API std::string FormatAngle(const EDA_ANGLE &aAngle)
Convert aAngle from board units to a string appropriate for writing to file.
KICOMMON_API std::string FormatInternalUnits(const EDA_IU_SCALE &aIuScale, int aValue, EDA_DATA_TYPE aDataType=EDA_DATA_TYPE::DISTANCE)
Converts aValue from internal units to a string appropriate for writing to file.
KICOMMON_API int Mils2mm(double aVal)
Convert mils to mm.
Definition eda_units.cpp:78
@ INCH
Definition rs274x.cpp:58
const double IU_PER_MILS
Definition base_units.h:75
static const long long MM
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:683