Debian Codegraph @

src/Base/UnitsApi.cpp

raw
1 // SPDX-License-Identifier: LGPL-2.1-or-later
2
3 /***************************************************************************
4 * Copyright (c) 2009 Jürgen Riegel <[email protected]> *
5 * *
6 * This file is part of the FreeCAD CAx development system. *
7 * *
8 * This library is free software; you can redistribute it and/or *
9 * modify it under the terms of the GNU Library General Public *
10 * License as published by the Free Software Foundation; either *
11 * version 2 of the License, or (at your option) any later version. *
12 * *
13 * This library is distributed in the hope that it will be useful, *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16 * GNU Library General Public License for more details. *
17 * *
18 * You should have received a copy of the GNU Library General Public *
19 * License along with this library; see the file COPYING.LIB. If not, *
20 * write to the Free Software Foundation, Inc., 59 Temple Place, *
21 * Suite 330, Boston, MA 02111-1307, USA *
22 * *
23 ***************************************************************************/
24
25 #include <iomanip>
26
27 #include <CXX/WrapPython.h>
28
29 #include "Exception.h"
30 #include "UnitsApi.h"
31 #include "UnitsSchema.h"
32 #include "UnitsSchemas.h"
33 #include "UnitsSchemasData.h"
34
35 using Base::UnitsApi;
36 using Base::UnitsSchema;
37 using Base::UnitsSchemas;
38
39 std::vector<std::string> UnitsApi::getDescriptions()
40 {
41 return schemas->descriptions();
42 }
43
44 std::vector<std::string> UnitsApi::getNames()
45 {
46 return schemas->names();
47 }
48
49 std::size_t UnitsApi::count()
50 {
51 return static_cast<int>(schemas->count());
52 }
53
54 bool UnitsApi::isMultiUnitAngle()
55 {
56 return schemas->currentSchema()->isMultiUnitAngle();
57 }
58
59 bool UnitsApi::isMultiUnitLength()
60 {
61 return schemas->currentSchema()->isMultiUnitLength();
62 }
63
64 std::string UnitsApi::getBasicLengthUnit()
65 {
66 return schemas->currentSchema()->getBasicLengthUnit();
67 }
68
69 void UnitsApi::setDecimals(const int prec)
70 {
71 decimals = prec;
72 }
73
74 int UnitsApi::getDecimals()
75 {
76 return decimals < 0 ? schemas->getDecimals() : decimals;
77 }
78
79 void UnitsApi::setDenominator(int frac)
80 {
81 denominator = frac;
82 }
83
84 int UnitsApi::getDenominator()
85 {
86 return denominator < 0 ? schemas->defFractDenominator() : denominator;
87 }
88
89 std::unique_ptr<UnitsSchema> UnitsApi::createSchema(const std::size_t num)
90 {
91 return std::make_unique<UnitsSchema>(schemas->spec(num));
92 }
93
94 void UnitsApi::setSchema(const std::string& name)
95 {
96 schemas->select(name);
97 }
98
99 void UnitsApi::setSchema(const size_t num)
100 {
101 schemas->select(num);
102 }
103
104 double UnitsApi::toDouble(PyObject* args, const Base::Unit& u)
105 {
106 if (PyUnicode_Check(args)) {
107 std::string str(PyUnicode_AsUTF8(args));
108 // Parse the string
109 Quantity q = Quantity::parse(str);
110 if (q.getUnit() == u) {
111 return q.getValue();
112 }
113 throw Base::UnitsMismatchError("Wrong unit type!");
114 }
115
116 if (PyFloat_Check(args)) {
117 return PyFloat_AsDouble(args);
118 }
119 if (PyLong_Check(args)) {
120 return static_cast<double>(PyLong_AsLong(args));
121 }
122
123 throw Base::UnitsMismatchError("Wrong parameter type!");
124 }
125
126 std::string UnitsApi::schemaTranslate(const Quantity& quant, double& factor, std::string& unitString)
127 {
128 return schemas->currentSchema()->translate(quant, factor, unitString);
129 }
130
131 std::string UnitsApi::schemaTranslate(const Quantity& quant)
132 {
133 double dummy1 {}; // to satisfy GCC
134 std::string dummy2;
135 return schemas->currentSchema()->translate(quant, dummy1, dummy2);
136 }