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"
31
#include "UnitsSchemaMKS.h"
38
QString UnitsSchemaMKS::schemaTranslate(const Quantity& quant, double& factor, QString& unitString)
40
double UnitValue = std::abs(quant.getValue());
41
Unit unit = quant.getUnit();
43
// now do special treatment on all cases seems necessary:
44
if (unit == Unit::Length) { // Length handling ============================
45
if (UnitValue < 1e-6) { // smaller than 0.001 nm -> scientific notation
46
unitString = QString::fromLatin1("mm");
49
else if (UnitValue < 1e-3) {
50
unitString = QString::fromLatin1("nm");
53
else if (UnitValue < 0.1) {
54
unitString = QString::fromUtf8("\xC2\xB5m");
57
else if (UnitValue < 1e4) {
58
unitString = QString::fromLatin1("mm");
61
else if (UnitValue < 1e7) {
62
unitString = QString::fromLatin1("m");
65
else if (UnitValue < 1e10) {
66
unitString = QString::fromLatin1("km");
69
else { // bigger than 1000 km -> scientific notation
70
unitString = QString::fromLatin1("m");
74
else if (unit == Unit::Area) {
75
if (UnitValue < 100) {
76
unitString = QString::fromLatin1("mm^2");
79
else if (UnitValue < 1e6) {
80
unitString = QString::fromLatin1("cm^2");
83
else if (UnitValue < 1e12) {
84
unitString = QString::fromLatin1("m^2");
87
else { // bigger than 1 square kilometer
88
unitString = QString::fromLatin1("km^2");
92
else if (unit == Unit::Volume) {
93
if (UnitValue < 1e3) { // smaller than 1 ul
94
unitString = QString::fromLatin1("mm^3");
97
else if (UnitValue < 1e6) {
98
unitString = QString::fromLatin1("ml");
101
else if (UnitValue < 1e9) {
102
unitString = QString::fromLatin1("l");
105
else { // bigger than 1000 l
106
unitString = QString::fromLatin1("m^3");
110
else if (unit == Unit::Mass) {
111
if (UnitValue < 1e-6) {
112
unitString = QString::fromUtf8("\xC2\xB5g");
115
else if (UnitValue < 1e-3) {
116
unitString = QString::fromLatin1("mg");
119
else if (UnitValue < 1.0) {
120
unitString = QString::fromLatin1("g");
123
else if (UnitValue < 1e3) {
124
unitString = QString::fromLatin1("kg");
128
unitString = QString::fromLatin1("t");
132
else if (unit == Unit::Density) {
133
if (UnitValue < 0.0001) {
134
unitString = QString::fromLatin1("kg/m^3");
135
factor = 0.000000001;
137
else if (UnitValue < 1.0) {
138
unitString = QString::fromLatin1("kg/cm^3");
142
unitString = QString::fromLatin1("kg/mm^3");
146
else if (unit == Unit::Acceleration) {
147
unitString = QString::fromLatin1("m/s^2");
150
else if ((unit == Unit::Pressure) || (unit == Unit::Stress)) {
151
if (UnitValue < 10.0) { // Pa is the smallest
152
unitString = QString::fromLatin1("Pa");
155
else if (UnitValue < 10000.0) {
156
unitString = QString::fromLatin1("kPa");
159
else if (UnitValue < 10000000.0) {
160
unitString = QString::fromLatin1("MPa");
163
else if (UnitValue < 10000000000.0) {
164
unitString = QString::fromLatin1("GPa");
167
else { // bigger then 1000 GPa -> scientific notation
168
unitString = QString::fromLatin1("Pa");
172
else if ((unit == Unit::Stiffness)) {
173
if (UnitValue < 1) { // mN/m is the smallest
174
unitString = QString::fromLatin1("mN/m");
177
else if (UnitValue < 1e3) {
178
unitString = QString::fromLatin1("N/m");
181
else if (UnitValue < 1e6) {
182
unitString = QString::fromLatin1("kN/m");
186
unitString = QString::fromLatin1("MN/m");
190
else if ((unit == Unit::StiffnessDensity)) {
191
if (UnitValue < 1e-3) {
192
unitString = QString::fromLatin1("Pa/m");
195
else if (UnitValue < 1) {
196
unitString = QString::fromLatin1("kPa/m");
199
else if (UnitValue < 1e3) {
200
unitString = QString::fromLatin1("MPa/m");
204
unitString = QString::fromLatin1("GPa/m");
208
else if (unit == Unit::ThermalConductivity) {
209
if (UnitValue > 1000000) {
210
unitString = QString::fromLatin1("W/mm/K");
214
unitString = QString::fromLatin1("W/m/K");
218
else if (unit == Unit::ThermalExpansionCoefficient) {
219
if (UnitValue < 0.001) {
220
unitString = QString::fromUtf8("\xC2\xB5m/m/K");
224
unitString = QString::fromLatin1("m/m/K");
228
else if (unit == Unit::VolumetricThermalExpansionCoefficient) {
229
if (UnitValue < 0.001) {
230
unitString = QString::fromUtf8("mm^3/m^3/K");
234
unitString = QString::fromLatin1("m^3/m^3/K");
238
else if (unit == Unit::SpecificHeat) {
239
unitString = QString::fromLatin1("J/kg/K");
242
else if (unit == Unit::ThermalTransferCoefficient) {
243
unitString = QString::fromLatin1("W/m^2/K");
246
else if (unit == Unit::Force) {
247
if (UnitValue < 1e3) {
248
unitString = QString::fromLatin1("mN");
251
else if (UnitValue < 1e6) {
252
unitString = QString::fromLatin1("N");
255
else if (UnitValue < 1e9) {
256
unitString = QString::fromLatin1("kN");
260
unitString = QString::fromLatin1("MN");
264
else if (unit == Unit::Power) {
265
if (UnitValue < 1e6) {
266
unitString = QString::fromLatin1("mW");
269
else if (UnitValue < 1e9) {
270
unitString = QString::fromLatin1("W");
274
unitString = QString::fromLatin1("kW");
278
else if (unit == Unit::ElectricPotential) {
279
if (UnitValue < 1e6) {
280
unitString = QString::fromLatin1("mV");
283
else if (UnitValue < 1e9) {
284
unitString = QString::fromLatin1("V");
287
else if (UnitValue < 1e12) {
288
unitString = QString::fromLatin1("kV");
291
else { // > 1000 kV scientificc notation
292
unitString = QString::fromLatin1("V");
296
else if (unit == Unit::ElectricCharge) {
297
unitString = QString::fromLatin1("C");
300
else if (unit == Unit::CurrentDensity) {
301
if (UnitValue <= 1e3) {
302
unitString = QString::fromLatin1("A/m^2");
306
unitString = QString::fromLatin1("A/mm^2");
310
else if (unit == Unit::MagneticFluxDensity) {
311
if (UnitValue <= 1e-3) {
312
unitString = QString::fromLatin1("G");
316
unitString = QString::fromLatin1("T");
320
else if (unit == Unit::MagneticFieldStrength) {
321
unitString = QString::fromLatin1("A/m");
324
else if (unit == Unit::MagneticFlux) {
325
unitString = QString::fromLatin1("Wb");
328
else if (unit == Unit::Magnetization) {
329
unitString = QString::fromLatin1("A/m");
332
else if (unit == Unit::ElectricalConductance) {
333
if (UnitValue < 1e-9) {
334
unitString = QString::fromUtf8("\xC2\xB5S");
337
else if (UnitValue < 1e-6) {
338
unitString = QString::fromLatin1("mS");
342
unitString = QString::fromLatin1("S");
346
else if (unit == Unit::ElectricalResistance) {
347
if (UnitValue < 1e9) {
348
unitString = QString::fromLatin1("Ohm");
351
else if (UnitValue < 1e12) {
352
unitString = QString::fromLatin1("kOhm");
356
unitString = QString::fromLatin1("MOhm");
360
else if (unit == Unit::ElectricalConductivity) {
361
if (UnitValue < 1e-3) {
362
unitString = QString::fromLatin1("mS/m");
365
else if (UnitValue < 1.0) {
366
unitString = QString::fromLatin1("S/m");
369
else if (UnitValue < 1e3) {
370
unitString = QString::fromLatin1("kS/m");
374
unitString = QString::fromLatin1("MS/m");
378
else if (unit == Unit::ElectricalCapacitance) {
379
if (UnitValue < 1e-15) {
380
unitString = QString::fromLatin1("pF");
383
else if (UnitValue < 1e-12) {
384
unitString = QString::fromLatin1("nF");
387
else if (UnitValue < 1e-9) {
388
// \x reads everything to the end, therefore split
389
unitString = QString::fromUtf8("\xC2\xB5"
393
else if (UnitValue < 1e-6) {
394
unitString = QString::fromLatin1("mF");
398
unitString = QString::fromLatin1("F");
402
else if (unit == Unit::ElectricalInductance) {
403
if (UnitValue < 1e-6) {
404
unitString = QString::fromLatin1("nH");
407
else if (UnitValue < 1e-3) {
408
unitString = QString::fromUtf8("\xC2\xB5H");
411
else if (UnitValue < 1.0) {
412
unitString = QString::fromLatin1("mH");
416
unitString = QString::fromLatin1("H");
420
else if (unit == Unit::VacuumPermittivity) {
421
unitString = QString::fromLatin1("F/m");
424
else if (unit == Unit::Work) {
425
if (UnitValue < 1.602176634e-10) {
426
unitString = QString::fromLatin1("eV");
427
factor = 1.602176634e-13;
429
else if (UnitValue < 1.602176634e-7) {
430
unitString = QString::fromLatin1("keV");
431
factor = 1.602176634e-10;
433
else if (UnitValue < 1.602176634e-4) {
434
unitString = QString::fromLatin1("MeV");
435
factor = 1.602176634e-7;
437
else if (UnitValue < 1e6) {
438
unitString = QString::fromLatin1("mJ");
441
else if (UnitValue < 1e9) {
442
unitString = QString::fromLatin1("J");
445
else if (UnitValue < 1e12) {
446
unitString = QString::fromLatin1("kJ");
449
else if (UnitValue < 3.6e+15) {
450
unitString = QString::fromLatin1("kWh");
453
else { // bigger than 1000 kWh -> scientific notation
454
unitString = QString::fromLatin1("J");
458
else if (unit == Unit::SpecificEnergy) {
459
unitString = QString::fromLatin1("m^2/s^2");
462
else if (unit == Unit::HeatFlux) {
463
unitString = QString::fromLatin1("W/m^2");
466
else if (unit == Unit::Frequency) {
467
if (UnitValue < 1e3) {
468
unitString = QString::fromLatin1("Hz");
471
else if (UnitValue < 1e6) {
472
unitString = QString::fromLatin1("kHz");
475
else if (UnitValue < 1e9) {
476
unitString = QString::fromLatin1("MHz");
479
else if (UnitValue < 1e12) {
480
unitString = QString::fromLatin1("GHz");
484
unitString = QString::fromLatin1("THz");
488
else if (unit == Unit::Velocity) {
489
unitString = QString::fromLatin1("m/s");
492
else if (unit == Unit::DynamicViscosity) {
493
unitString = QString::fromLatin1("Pa*s");
496
else if (unit == Unit::KinematicViscosity) {
497
unitString = QString::fromLatin1("m^2/s");
500
else if (unit == Unit::VolumeFlowRate) {
501
if (UnitValue < 1e-3) { // smaller than 0.001 mm^3/s -> scientific notation
502
unitString = QString::fromLatin1("m^3/s");
505
else if (UnitValue < 1e3) {
506
unitString = QString::fromLatin1("mm^3/s");
509
else if (UnitValue < 1e6) {
510
unitString = QString::fromLatin1("ml/s");
513
else if (UnitValue < 1e9) {
514
unitString = QString::fromLatin1("l/s");
518
unitString = QString::fromLatin1("m^3/s");
522
else if (unit == Unit::DissipationRate) {
523
unitString = QString::fromLatin1("m^2/s^3");
526
else if (unit == Unit::InverseLength) {
527
if (UnitValue < 1e-6) { // smaller than 0.001 1/km -> scientific notation
528
unitString = QString::fromLatin1("1/m");
531
else if (UnitValue < 1e-3) {
532
unitString = QString::fromLatin1("1/km");
535
else if (UnitValue < 1.0) {
536
unitString = QString::fromLatin1("1/m");
539
else if (UnitValue < 1e3) {
540
unitString = QString::fromLatin1("1/mm");
543
else if (UnitValue < 1e6) {
544
unitString = QString::fromUtf8("1/\xC2\xB5m");
547
else if (UnitValue < 1e9) {
548
unitString = QString::fromLatin1("1/nm");
551
else { // larger -> scientific notation
552
unitString = QString::fromLatin1("1/m");
556
else if (unit == Unit::InverseArea) {
557
if (UnitValue < 1e-12) { // smaller than 0.001 1/km^2 -> scientific notation
558
unitString = QString::fromLatin1("1/m^2");
561
else if (UnitValue < 1e-6) {
562
unitString = QString::fromLatin1("1/km^2");
565
else if (UnitValue < 1.0) {
566
unitString = QString::fromLatin1("1/m^2");
569
else if (UnitValue < 1e2) {
570
unitString = QString::fromLatin1("1/cm^2");
574
unitString = QString::fromLatin1("1/mm^2");
578
else if (unit == Unit::InverseVolume) {
579
if (UnitValue < 1e-6) {
580
unitString = QString::fromLatin1("1/m^3");
583
else if (UnitValue < 1e-3) {
584
unitString = QString::fromLatin1("1/l");
587
else if (UnitValue < 1.0) {
588
unitString = QString::fromLatin1("1/ml");
592
unitString = QString::fromLatin1("1/mm^3");
597
// default action for all cases without special treatment:
598
unitString = quant.getUnit().getString();
602
return toLocale(quant, factor, unitString);