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-2022 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 <wx/string.h>
29#include <geometry/eda_angle.h>
30#include <base_units.h>
31
35enum class EDA_DATA_TYPE
36{
37 DISTANCE = 0,
38 AREA = 1,
39 VOLUME = 2
40};
41
42enum class EDA_UNITS
43{
44 INCHES = 0,
45 MILLIMETRES = 1,
46 UNSCALED = 2,
47 DEGREES = 3,
48 PERCENT = 4,
49 MILS = 5,
50};
51
53{
54 bool IsImperialUnit( EDA_UNITS aUnit );
55
56 bool IsMetricUnit( EDA_UNITS aUnit );
57
61 int Mm2mils( double aVal );
62
66 int Mils2mm( double aVal );
67
72 bool FetchUnitsFromString( const wxString& aTextValue, EDA_UNITS& aUnits );
73
83 wxString GetText( EDA_UNITS aUnits, EDA_DATA_TYPE aType = EDA_DATA_TYPE::DISTANCE );
84
94 wxString GetLabel( EDA_UNITS aUnits, EDA_DATA_TYPE aType = EDA_DATA_TYPE::DISTANCE );
95
106 std::string FormatAngle( const EDA_ANGLE& aAngle );
107
119 std::string FormatInternalUnits( const EDA_IU_SCALE& aIuScale, int aValue );
120 std::string FormatInternalUnits( const EDA_IU_SCALE& aIuScale, const VECTOR2I& aPoint );
121
122 constexpr inline int Mils2IU( const EDA_IU_SCALE& aIuScale, int mils )
123 {
124 double x = mils * aIuScale.IU_PER_MILS;
125 return int( x < 0 ? x - 0.5 : x + 0.5 );
126 }
127
128 namespace UI
129 {
138 double ToUserUnit( const EDA_IU_SCALE& aIuScale, EDA_UNITS aUnit, double aValue );
139
153 wxString StringFromValue( const EDA_IU_SCALE& aIuScale, EDA_UNITS aUnits, double aValue,
154 bool aAddUnitsText = false,
155 EDA_DATA_TYPE aType = EDA_DATA_TYPE::DISTANCE );
156
171 wxString MessageTextFromValue( const EDA_IU_SCALE& aIuScale, EDA_UNITS aUnits,
172 double aValue, bool aAddUnitsText = true,
173 EDA_DATA_TYPE aType = EDA_DATA_TYPE::DISTANCE );
174
175 wxString MessageTextFromValue( const EDA_IU_SCALE& aIuScale, EDA_UNITS aUnits,
176 int aValue, bool aAddUnitLabel = true,
177 EDA_DATA_TYPE aType = EDA_DATA_TYPE::DISTANCE );
178
179 wxString MessageTextFromValue( const EDA_IU_SCALE& aIuScale, EDA_UNITS aUnits,
180 long long int aValue, bool aAddUnitLabel = true,
181 EDA_DATA_TYPE aType = EDA_DATA_TYPE::DISTANCE );
182
183 wxString MessageTextFromValue( EDA_ANGLE aValue, bool aAddUnitLabel = true );
184
185
189 double FromUserUnit( const EDA_IU_SCALE& aIuScale, EDA_UNITS aUnit, double aValue );
190
191
202 double DoubleValueFromString( const EDA_IU_SCALE& aIuScale, EDA_UNITS aUnits,
203 const wxString& aTextValue,
204 EDA_DATA_TYPE aType = EDA_DATA_TYPE::DISTANCE );
205
206 double DoubleValueFromString( const wxString& aTextValue );
207
218 long long int ValueFromString( const EDA_IU_SCALE& aIuScale, EDA_UNITS aUnits,
219 const wxString& aTextValue,
220 EDA_DATA_TYPE aType = EDA_DATA_TYPE::DISTANCE );
221
222 long long int ValueFromString( const wxString& aTextValue);
223 }
224}
225
226#endif
EDA_DATA_TYPE
The type of unit.
Definition: eda_units.h:36
EDA_UNITS
Definition: eda_units.h:43
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:315
double FromUserUnit(const EDA_IU_SCALE &aIuScale, EDA_UNITS aUnit, double aValue)
Return in internal units the value "val" given in a real unit such as "in", "mm" or "deg".
Definition: eda_units.cpp:385
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:525
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:225
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:445
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:190
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:101
bool FetchUnitsFromString(const wxString &aTextValue, EDA_UNITS &aUnits)
Writes any unit info found in the string to aUnits.
Definition: eda_units.cpp:68
bool IsImperialUnit(EDA_UNITS aUnit)
Definition: eda_units.cpp:29
bool IsMetricUnit(EDA_UNITS aUnit)
Definition: eda_units.cpp:43
constexpr int Mils2IU(const EDA_IU_SCALE &aIuScale, int mils)
Definition: eda_units.h:122
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:128
int Mm2mils(double aVal)
Convert mm to mils.
Definition: eda_units.cpp:56
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:142
std::string FormatAngle(const EDA_ANGLE &aAngle)
Converts aAngle from board units to a string appropriate for writing to file.
Definition: eda_units.cpp:134
int Mils2mm(double aVal)
Convert mils to mm.
Definition: eda_units.cpp:62
const double IU_PER_MILS
Definition: base_units.h:78