KiCad PCB EDA Suite
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
71 void FetchUnitsFromString( const wxString& aTextValue, EDA_UNITS& aUnits );
72
82 wxString GetText( EDA_UNITS aUnits, EDA_DATA_TYPE aType = EDA_DATA_TYPE::DISTANCE );
83
93 wxString GetLabel( EDA_UNITS aUnits, EDA_DATA_TYPE aType = EDA_DATA_TYPE::DISTANCE );
94
105 std::string FormatAngle( const EDA_ANGLE& aAngle );
106
118 std::string FormatInternalUnits( const EDA_IU_SCALE& aIuScale, int aValue );
119 std::string FormatInternalUnits( const EDA_IU_SCALE& aIuScale, const wxPoint& aPoint );
120 std::string FormatInternalUnits( const EDA_IU_SCALE& aIuScale, const wxSize& aSize );
121 std::string FormatInternalUnits( const EDA_IU_SCALE& aIuScale, const VECTOR2I& aPoint );
122
123 constexpr inline int Mils2IU( const EDA_IU_SCALE& aIuScale, int mils )
124 {
125 double x = mils * aIuScale.IU_PER_MILS;
126 return int( x < 0 ? x - 0.5 : x + 0.5 );
127 }
128
129 namespace UI
130 {
139 double ToUserUnit( const EDA_IU_SCALE& aIuScale, EDA_UNITS aUnit, double aValue );
140
154 wxString StringFromValue( const EDA_IU_SCALE& aIuScale, EDA_UNITS aUnits, double aValue,
155 bool aAddUnitsText = false,
157
172 wxString MessageTextFromValue( const EDA_IU_SCALE& aIuScale, EDA_UNITS aUnits,
173 double aValue, bool aAddUnitsText = true,
175
176 wxString MessageTextFromValue( const EDA_IU_SCALE& aIuScale, EDA_UNITS aUnits,
177 int aValue, bool aAddUnitLabel = true,
179
180 wxString MessageTextFromValue( const EDA_IU_SCALE& aIuScale, EDA_UNITS aUnits,
181 long long int aValue, bool aAddUnitLabel = true,
183
184 wxString MessageTextFromValue( EDA_ANGLE aValue, bool aAddUnitLabel = true );
185
186
190 double FromUserUnit( const EDA_IU_SCALE& aIuScale, EDA_UNITS aUnit, double aValue );
191
192
202 double DoubleValueFromString( const EDA_IU_SCALE& aIuScale, EDA_UNITS aUnits,
203 const wxString& aTextValue,
205
206 double DoubleValueFromString( const wxString& aTextValue );
207
217 long long int ValueFromString( const EDA_IU_SCALE& aIuScale, EDA_UNITS aUnits,
218 const wxString& aTextValue,
220
221 long long int ValueFromString( const wxString& aTextValue);
222 }
223}
224
225#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:323
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:393
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:530
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:233
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:451
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:198
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:102
bool IsImperialUnit(EDA_UNITS aUnit)
Definition: eda_units.cpp:29
bool IsMetricUnit(EDA_UNITS aUnit)
Definition: eda_units.cpp:45
constexpr int Mils2IU(const EDA_IU_SCALE &aIuScale, int mils)
Definition: eda_units.h:123
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:129
int Mm2mils(double aVal)
Convert mm to mils.
Definition: eda_units.cpp:60
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:143
std::string FormatAngle(const EDA_ANGLE &aAngle)
Converts aAngle from board units to a string appropriate for writing to file.
Definition: eda_units.cpp:135
int Mils2mm(double aVal)
Convert mils to mm.
Definition: eda_units.cpp:66
void FetchUnitsFromString(const wxString &aTextValue, EDA_UNITS &aUnits)
Writes any unit info found in the string to aUnits.
Definition: eda_units.cpp:72
const double IU_PER_MILS
Definition: base_units.h:78