1
/***************************************************************************
2
* Copyright (c) 2009 Jürgen Riegel <FreeCAD@juergen-riegel.net> *
4
* This file is part of the FreeCAD CAx development system. *
6
* This library is free software; you can redistribute it and/or *
7
* modify it under the terms of the GNU Library General Public *
8
* License as published by the Free Software Foundation; either *
9
* version 2 of the License, or (at your option) any later version. *
11
* This library 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 Library General Public License for more details. *
16
* You should have received a copy of the GNU Library General Public *
17
* License along with this library; see the file COPYING.LIB. If not, *
18
* write to the Free Software Foundation, Inc., 59 Temple Place, *
19
* Suite 330, Boston, MA 02111-1307, USA *
21
***************************************************************************/
24
#include "PreCompiled.h"
29
#include <CXX/WrapPython.h>
35
#include "UnitsSchemaCentimeters.h"
36
#include "UnitsSchemaInternal.h"
37
#include "UnitsSchemaImperial1.h"
38
#include "UnitsSchemaMKS.h"
39
#include "UnitsSchemaMmMin.h"
40
#include "UnitsSchemaFemMilliMeterNewton.h"
41
#include "UnitsSchemaMeterDecimal.h"
44
#define M_PI 3.14159265358979323846
47
#define M_E 2.71828182845904523536
50
#define DOUBLE_MAX 1.7976931348623157E+308 /* max decimal value of a "double"*/
53
#define DOUBLE_MIN 2.2250738585072014E-308 /* min decimal value of a "double"*/
58
// === static attributes ================================================
60
UnitsSchemaPtr UnitsApi::UserPrefSystem(new UnitsSchemaInternal());
61
UnitSystem UnitsApi::currentSystem = UnitSystem::SI1;
63
int UnitsApi::UserPrefDecimals = 2;
65
QString UnitsApi::getDescription(UnitSystem system)
69
return tr("Standard (mm, kg, s, °)");
71
return tr("MKS (m, kg, s, °)");
72
case UnitSystem::Imperial1:
73
return tr("US customary (in, lb)");
74
case UnitSystem::ImperialDecimal:
75
return tr("Imperial decimal (in, lb)");
76
case UnitSystem::Centimeters:
77
return tr("Building Euro (cm, m², m³)");
78
case UnitSystem::ImperialBuilding:
79
return tr("Building US (ft-in, sqft, cft)");
80
case UnitSystem::MmMin:
81
return tr("Metric small parts & CNC(mm, mm/min)");
82
case UnitSystem::ImperialCivil:
83
return tr("Imperial for Civil Eng (ft, ft/s)");
84
case UnitSystem::FemMilliMeterNewton:
85
return tr("FEM (mm, N, s)");
86
case UnitSystem::MeterDecimal:
87
return tr("Meter decimal (m, m², m³)");
89
return tr("Unknown schema");
93
UnitsSchemaPtr UnitsApi::createSchema(UnitSystem system)
97
return std::make_unique<UnitsSchemaInternal>();
99
return std::make_unique<UnitsSchemaMKS>();
100
case UnitSystem::Imperial1:
101
return std::make_unique<UnitsSchemaImperial1>();
102
case UnitSystem::ImperialDecimal:
103
return std::make_unique<UnitsSchemaImperialDecimal>();
104
case UnitSystem::Centimeters:
105
return std::make_unique<UnitsSchemaCentimeters>();
106
case UnitSystem::ImperialBuilding:
107
return std::make_unique<UnitsSchemaImperialBuilding>();
108
case UnitSystem::MmMin:
109
return std::make_unique<UnitsSchemaMmMin>();
110
case UnitSystem::ImperialCivil:
111
return std::make_unique<UnitsSchemaImperialCivil>();
112
case UnitSystem::FemMilliMeterNewton:
113
return std::make_unique<UnitsSchemaFemMilliMeterNewton>();
114
case UnitSystem::MeterDecimal:
115
return std::make_unique<UnitsSchemaMeterDecimal>();
123
void UnitsApi::setSchema(UnitSystem system)
125
if (UserPrefSystem) {
126
UserPrefSystem->resetSchemaUnits(); // for schemas changed the Quantity constants
129
UserPrefSystem = createSchema(system);
130
currentSystem = system;
132
// for wrong value fall back to standard schema
133
if (!UserPrefSystem) {
134
UserPrefSystem = std::make_unique<UnitsSchemaInternal>();
135
currentSystem = UnitSystem::SI1;
138
UserPrefSystem->setSchemaUnits(); // if necessary a unit schema can change the constants in
139
// Quantity (e.g. mi=1.8km rather then 1.6km).
142
QString UnitsApi::toString(const Base::Quantity& quantity, const QuantityFormat& format)
144
QString value = QString::fromLatin1("'%1 %2'")
145
.arg(quantity.getValue(), 0, format.toFormat(), format.precision)
146
.arg(quantity.getUnit().getString());
150
QString UnitsApi::toNumber(const Base::Quantity& quantity, const QuantityFormat& format)
152
return toNumber(quantity.getValue(), format);
155
QString UnitsApi::toNumber(double value, const QuantityFormat& format)
157
QString number = QString::fromLatin1("%1").arg(value, 0, format.toFormat(), format.precision);
161
// return true if the current user schema uses multiple units for length (ex. Ft/In)
162
bool UnitsApi::isMultiUnitLength()
164
return UserPrefSystem->isMultiUnitLength();
167
// return true if the current user schema uses multiple units for angles (ex. DMS)
168
bool UnitsApi::isMultiUnitAngle()
170
return UserPrefSystem->isMultiUnitAngle();
173
std::string UnitsApi::getBasicLengthUnit()
175
return UserPrefSystem->getBasicLengthUnit();
178
// === static translation methods ==========================================
180
QString UnitsApi::schemaTranslate(const Base::Quantity& quant, double& factor, QString& unitString)
182
return UserPrefSystem->schemaTranslate(quant, factor, unitString);
185
double UnitsApi::toDouble(PyObject* args, const Base::Unit& u)
187
if (PyUnicode_Check(args)) {
188
QString str = QString::fromUtf8(PyUnicode_AsUTF8(args));
190
Quantity q = Quantity::parse(str);
191
if (q.getUnit() == u) {
194
throw Base::UnitsMismatchError("Wrong unit type!");
197
if (PyFloat_Check(args)) {
198
return PyFloat_AsDouble(args);
200
if (PyLong_Check(args)) {
201
return static_cast<double>(PyLong_AsLong(args));
204
throw Base::UnitsMismatchError("Wrong parameter type!");
207
Quantity UnitsApi::toQuantity(PyObject* args, const Base::Unit& u)
210
if (PyUnicode_Check(args)) {
211
QString str = QString::fromUtf8(PyUnicode_AsUTF8(args));
213
Quantity q = Quantity::parse(str);
216
else if (PyFloat_Check(args)) {
217
d = PyFloat_AsDouble(args);
219
else if (PyLong_Check(args)) {
220
d = static_cast<double>(PyLong_AsLong(args));
223
throw Base::UnitsMismatchError("Wrong parameter type!");
226
return Quantity(d, u);
229
void UnitsApi::setDecimals(int prec)
231
UserPrefDecimals = prec;
234
int UnitsApi::getDecimals()
236
return UserPrefDecimals;