FreeCAD

Форк
0
/
UnitsSchemaMKS.cpp 
603 строки · 19.6 Кб
1
/***************************************************************************
2
 *   Copyright (c) 2009 Jürgen Riegel <FreeCAD@juergen-riegel.net>         *
3
 *                                                                         *
4
 *   This file is part of the FreeCAD CAx development system.              *
5
 *                                                                         *
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.      *
10
 *                                                                         *
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.                  *
15
 *                                                                         *
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                                *
20
 *                                                                         *
21
 ***************************************************************************/
22

23

24
#include "PreCompiled.h"
25
#ifdef __GNUC__
26
#include <unistd.h>
27
#endif
28

29
#include <QString>
30

31
#include "UnitsSchemaMKS.h"
32
#include <cmath>
33

34

35
using namespace Base;
36

37

38
QString UnitsSchemaMKS::schemaTranslate(const Quantity& quant, double& factor, QString& unitString)
39
{
40
    double UnitValue = std::abs(quant.getValue());
41
    Unit unit = quant.getUnit();
42

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");
47
            factor = 1.0;
48
        }
49
        else if (UnitValue < 1e-3) {
50
            unitString = QString::fromLatin1("nm");
51
            factor = 1e-6;
52
        }
53
        else if (UnitValue < 0.1) {
54
            unitString = QString::fromUtf8("\xC2\xB5m");
55
            factor = 1e-3;
56
        }
57
        else if (UnitValue < 1e4) {
58
            unitString = QString::fromLatin1("mm");
59
            factor = 1.0;
60
        }
61
        else if (UnitValue < 1e7) {
62
            unitString = QString::fromLatin1("m");
63
            factor = 1e3;
64
        }
65
        else if (UnitValue < 1e10) {
66
            unitString = QString::fromLatin1("km");
67
            factor = 1e6;
68
        }
69
        else {  // bigger than 1000 km -> scientific notation
70
            unitString = QString::fromLatin1("m");
71
            factor = 1e3;
72
        }
73
    }
74
    else if (unit == Unit::Area) {
75
        if (UnitValue < 100) {
76
            unitString = QString::fromLatin1("mm^2");
77
            factor = 1.0;
78
        }
79
        else if (UnitValue < 1e6) {
80
            unitString = QString::fromLatin1("cm^2");
81
            factor = 100;
82
        }
83
        else if (UnitValue < 1e12) {
84
            unitString = QString::fromLatin1("m^2");
85
            factor = 1e6;
86
        }
87
        else {  // bigger than 1 square kilometer
88
            unitString = QString::fromLatin1("km^2");
89
            factor = 1e12;
90
        }
91
    }
92
    else if (unit == Unit::Volume) {
93
        if (UnitValue < 1e3) {  // smaller than 1 ul
94
            unitString = QString::fromLatin1("mm^3");
95
            factor = 1.0;
96
        }
97
        else if (UnitValue < 1e6) {
98
            unitString = QString::fromLatin1("ml");
99
            factor = 1e3;
100
        }
101
        else if (UnitValue < 1e9) {
102
            unitString = QString::fromLatin1("l");
103
            factor = 1e6;
104
        }
105
        else {  // bigger than 1000 l
106
            unitString = QString::fromLatin1("m^3");
107
            factor = 1e9;
108
        }
109
    }
110
    else if (unit == Unit::Mass) {
111
        if (UnitValue < 1e-6) {
112
            unitString = QString::fromUtf8("\xC2\xB5g");
113
            factor = 1e-9;
114
        }
115
        else if (UnitValue < 1e-3) {
116
            unitString = QString::fromLatin1("mg");
117
            factor = 1e-6;
118
        }
119
        else if (UnitValue < 1.0) {
120
            unitString = QString::fromLatin1("g");
121
            factor = 1e-3;
122
        }
123
        else if (UnitValue < 1e3) {
124
            unitString = QString::fromLatin1("kg");
125
            factor = 1.0;
126
        }
127
        else {
128
            unitString = QString::fromLatin1("t");
129
            factor = 1e3;
130
        }
131
    }
132
    else if (unit == Unit::Density) {
133
        if (UnitValue < 0.0001) {
134
            unitString = QString::fromLatin1("kg/m^3");
135
            factor = 0.000000001;
136
        }
137
        else if (UnitValue < 1.0) {
138
            unitString = QString::fromLatin1("kg/cm^3");
139
            factor = 0.001;
140
        }
141
        else {
142
            unitString = QString::fromLatin1("kg/mm^3");
143
            factor = 1.0;
144
        }
145
    }
146
    else if (unit == Unit::Acceleration) {
147
        unitString = QString::fromLatin1("m/s^2");
148
        factor = 1000.0;
149
    }
150
    else if ((unit == Unit::Pressure) || (unit == Unit::Stress)) {
151
        if (UnitValue < 10.0) {  // Pa is the smallest
152
            unitString = QString::fromLatin1("Pa");
153
            factor = 0.001;
154
        }
155
        else if (UnitValue < 10000.0) {
156
            unitString = QString::fromLatin1("kPa");
157
            factor = 1.0;
158
        }
159
        else if (UnitValue < 10000000.0) {
160
            unitString = QString::fromLatin1("MPa");
161
            factor = 1000.0;
162
        }
163
        else if (UnitValue < 10000000000.0) {
164
            unitString = QString::fromLatin1("GPa");
165
            factor = 1000000.0;
166
        }
167
        else {  // bigger then 1000 GPa -> scientific notation
168
            unitString = QString::fromLatin1("Pa");
169
            factor = 0.001;
170
        }
171
    }
172
    else if ((unit == Unit::Stiffness)) {
173
        if (UnitValue < 1) {  // mN/m is the smallest
174
            unitString = QString::fromLatin1("mN/m");
175
            factor = 1e-3;
176
        }
177
        else if (UnitValue < 1e3) {
178
            unitString = QString::fromLatin1("N/m");
179
            factor = 1.0;
180
        }
181
        else if (UnitValue < 1e6) {
182
            unitString = QString::fromLatin1("kN/m");
183
            factor = 1e3;
184
        }
185
        else {
186
            unitString = QString::fromLatin1("MN/m");
187
            factor = 1e6;
188
        }
189
    }
190
    else if ((unit == Unit::StiffnessDensity)) {
191
        if (UnitValue < 1e-3) {
192
            unitString = QString::fromLatin1("Pa/m");
193
            factor = 1e-6;
194
        }
195
        else if (UnitValue < 1) {
196
            unitString = QString::fromLatin1("kPa/m");
197
            factor = 1e-3;
198
        }
199
        else if (UnitValue < 1e3) {
200
            unitString = QString::fromLatin1("MPa/m");
201
            factor = 1.0;
202
        }
203
        else {
204
            unitString = QString::fromLatin1("GPa/m");
205
            factor = 1e3;
206
        }
207
    }
208
    else if (unit == Unit::ThermalConductivity) {
209
        if (UnitValue > 1000000) {
210
            unitString = QString::fromLatin1("W/mm/K");
211
            factor = 1000000.0;
212
        }
213
        else {
214
            unitString = QString::fromLatin1("W/m/K");
215
            factor = 1000.0;
216
        }
217
    }
218
    else if (unit == Unit::ThermalExpansionCoefficient) {
219
        if (UnitValue < 0.001) {
220
            unitString = QString::fromUtf8("\xC2\xB5m/m/K");
221
            factor = 0.000001;
222
        }
223
        else {
224
            unitString = QString::fromLatin1("m/m/K");
225
            factor = 1.0;
226
        }
227
    }
228
    else if (unit == Unit::VolumetricThermalExpansionCoefficient) {
229
        if (UnitValue < 0.001) {
230
            unitString = QString::fromUtf8("mm^3/m^3/K");
231
            factor = 1e-9;
232
        }
233
        else {
234
            unitString = QString::fromLatin1("m^3/m^3/K");
235
            factor = 1.0;
236
        }
237
    }
238
    else if (unit == Unit::SpecificHeat) {
239
        unitString = QString::fromLatin1("J/kg/K");
240
        factor = 1000000.0;
241
    }
242
    else if (unit == Unit::ThermalTransferCoefficient) {
243
        unitString = QString::fromLatin1("W/m^2/K");
244
        factor = 1.0;
245
    }
246
    else if (unit == Unit::Force) {
247
        if (UnitValue < 1e3) {
248
            unitString = QString::fromLatin1("mN");
249
            factor = 1.0;
250
        }
251
        else if (UnitValue < 1e6) {
252
            unitString = QString::fromLatin1("N");
253
            factor = 1e3;
254
        }
255
        else if (UnitValue < 1e9) {
256
            unitString = QString::fromLatin1("kN");
257
            factor = 1e6;
258
        }
259
        else {
260
            unitString = QString::fromLatin1("MN");
261
            factor = 1e9;
262
        }
263
    }
264
    else if (unit == Unit::Power) {
265
        if (UnitValue < 1e6) {
266
            unitString = QString::fromLatin1("mW");
267
            factor = 1e3;
268
        }
269
        else if (UnitValue < 1e9) {
270
            unitString = QString::fromLatin1("W");
271
            factor = 1e6;
272
        }
273
        else {
274
            unitString = QString::fromLatin1("kW");
275
            factor = 1e9;
276
        }
277
    }
278
    else if (unit == Unit::ElectricPotential) {
279
        if (UnitValue < 1e6) {
280
            unitString = QString::fromLatin1("mV");
281
            factor = 1e3;
282
        }
283
        else if (UnitValue < 1e9) {
284
            unitString = QString::fromLatin1("V");
285
            factor = 1e6;
286
        }
287
        else if (UnitValue < 1e12) {
288
            unitString = QString::fromLatin1("kV");
289
            factor = 1e9;
290
        }
291
        else {  // > 1000 kV scientificc notation
292
            unitString = QString::fromLatin1("V");
293
            factor = 1e6;
294
        }
295
    }
296
    else if (unit == Unit::ElectricCharge) {
297
        unitString = QString::fromLatin1("C");
298
        factor = 1.0;
299
    }
300
    else if (unit == Unit::CurrentDensity) {
301
        if (UnitValue <= 1e3) {
302
            unitString = QString::fromLatin1("A/m^2");
303
            factor = 1e-6;
304
        }
305
        else {
306
            unitString = QString::fromLatin1("A/mm^2");
307
            factor = 1;
308
        }
309
    }
310
    else if (unit == Unit::MagneticFluxDensity) {
311
        if (UnitValue <= 1e-3) {
312
            unitString = QString::fromLatin1("G");
313
            factor = 1e-4;
314
        }
315
        else {
316
            unitString = QString::fromLatin1("T");
317
            factor = 1.0;
318
        }
319
    }
320
    else if (unit == Unit::MagneticFieldStrength) {
321
        unitString = QString::fromLatin1("A/m");
322
        factor = 1e-3;
323
    }
324
    else if (unit == Unit::MagneticFlux) {
325
        unitString = QString::fromLatin1("Wb");
326
        factor = 1e6;
327
    }
328
    else if (unit == Unit::Magnetization) {
329
        unitString = QString::fromLatin1("A/m");
330
        factor = 1e-3;
331
    }
332
    else if (unit == Unit::ElectricalConductance) {
333
        if (UnitValue < 1e-9) {
334
            unitString = QString::fromUtf8("\xC2\xB5S");
335
            factor = 1e-12;
336
        }
337
        else if (UnitValue < 1e-6) {
338
            unitString = QString::fromLatin1("mS");
339
            factor = 1e-9;
340
        }
341
        else {
342
            unitString = QString::fromLatin1("S");
343
            factor = 1e-6;
344
        }
345
    }
346
    else if (unit == Unit::ElectricalResistance) {
347
        if (UnitValue < 1e9) {
348
            unitString = QString::fromLatin1("Ohm");
349
            factor = 1e6;
350
        }
351
        else if (UnitValue < 1e12) {
352
            unitString = QString::fromLatin1("kOhm");
353
            factor = 1e9;
354
        }
355
        else {
356
            unitString = QString::fromLatin1("MOhm");
357
            factor = 1e12;
358
        }
359
    }
360
    else if (unit == Unit::ElectricalConductivity) {
361
        if (UnitValue < 1e-3) {
362
            unitString = QString::fromLatin1("mS/m");
363
            factor = 1e-12;
364
        }
365
        else if (UnitValue < 1.0) {
366
            unitString = QString::fromLatin1("S/m");
367
            factor = 1e-9;
368
        }
369
        else if (UnitValue < 1e3) {
370
            unitString = QString::fromLatin1("kS/m");
371
            factor = 1e-6;
372
        }
373
        else {
374
            unitString = QString::fromLatin1("MS/m");
375
            factor = 1e-3;
376
        }
377
    }
378
    else if (unit == Unit::ElectricalCapacitance) {
379
        if (UnitValue < 1e-15) {
380
            unitString = QString::fromLatin1("pF");
381
            factor = 1e-18;
382
        }
383
        else if (UnitValue < 1e-12) {
384
            unitString = QString::fromLatin1("nF");
385
            factor = 1e-15;
386
        }
387
        else if (UnitValue < 1e-9) {
388
            // \x reads everything to the end, therefore split
389
            unitString = QString::fromUtf8("\xC2\xB5"
390
                                           "F");
391
            factor = 1e-12;
392
        }
393
        else if (UnitValue < 1e-6) {
394
            unitString = QString::fromLatin1("mF");
395
            factor = 1e-9;
396
        }
397
        else {
398
            unitString = QString::fromLatin1("F");
399
            factor = 1e-6;
400
        }
401
    }
402
    else if (unit == Unit::ElectricalInductance) {
403
        if (UnitValue < 1e-6) {
404
            unitString = QString::fromLatin1("nH");
405
            factor = 1e-3;
406
        }
407
        else if (UnitValue < 1e-3) {
408
            unitString = QString::fromUtf8("\xC2\xB5H");
409
            factor = 1.0;
410
        }
411
        else if (UnitValue < 1.0) {
412
            unitString = QString::fromLatin1("mH");
413
            factor = 1e3;
414
        }
415
        else {
416
            unitString = QString::fromLatin1("H");
417
            factor = 1e6;
418
        }
419
    }
420
    else if (unit == Unit::VacuumPermittivity) {
421
        unitString = QString::fromLatin1("F/m");
422
        factor = 1e-9;
423
    }
424
    else if (unit == Unit::Work) {
425
        if (UnitValue < 1.602176634e-10) {
426
            unitString = QString::fromLatin1("eV");
427
            factor = 1.602176634e-13;
428
        }
429
        else if (UnitValue < 1.602176634e-7) {
430
            unitString = QString::fromLatin1("keV");
431
            factor = 1.602176634e-10;
432
        }
433
        else if (UnitValue < 1.602176634e-4) {
434
            unitString = QString::fromLatin1("MeV");
435
            factor = 1.602176634e-7;
436
        }
437
        else if (UnitValue < 1e6) {
438
            unitString = QString::fromLatin1("mJ");
439
            factor = 1e3;
440
        }
441
        else if (UnitValue < 1e9) {
442
            unitString = QString::fromLatin1("J");
443
            factor = 1e6;
444
        }
445
        else if (UnitValue < 1e12) {
446
            unitString = QString::fromLatin1("kJ");
447
            factor = 1e9;
448
        }
449
        else if (UnitValue < 3.6e+15) {
450
            unitString = QString::fromLatin1("kWh");
451
            factor = 3.6e+12;
452
        }
453
        else {  // bigger than 1000 kWh -> scientific notation
454
            unitString = QString::fromLatin1("J");
455
            factor = 1e6;
456
        }
457
    }
458
    else if (unit == Unit::SpecificEnergy) {
459
        unitString = QString::fromLatin1("m^2/s^2");
460
        factor = 1000000;
461
    }
462
    else if (unit == Unit::HeatFlux) {
463
        unitString = QString::fromLatin1("W/m^2");
464
        factor = 1.0;
465
    }
466
    else if (unit == Unit::Frequency) {
467
        if (UnitValue < 1e3) {
468
            unitString = QString::fromLatin1("Hz");
469
            factor = 1.0;
470
        }
471
        else if (UnitValue < 1e6) {
472
            unitString = QString::fromLatin1("kHz");
473
            factor = 1e3;
474
        }
475
        else if (UnitValue < 1e9) {
476
            unitString = QString::fromLatin1("MHz");
477
            factor = 1e6;
478
        }
479
        else if (UnitValue < 1e12) {
480
            unitString = QString::fromLatin1("GHz");
481
            factor = 1e9;
482
        }
483
        else {
484
            unitString = QString::fromLatin1("THz");
485
            factor = 1e12;
486
        }
487
    }
488
    else if (unit == Unit::Velocity) {
489
        unitString = QString::fromLatin1("m/s");
490
        factor = 1000.0;
491
    }
492
    else if (unit == Unit::DynamicViscosity) {
493
        unitString = QString::fromLatin1("Pa*s");
494
        factor = 0.001;
495
    }
496
    else if (unit == Unit::KinematicViscosity) {
497
        unitString = QString::fromLatin1("m^2/s");
498
        factor = 1e6;
499
    }
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");
503
            factor = 1e9;
504
        }
505
        else if (UnitValue < 1e3) {
506
            unitString = QString::fromLatin1("mm^3/s");
507
            factor = 1.0;
508
        }
509
        else if (UnitValue < 1e6) {
510
            unitString = QString::fromLatin1("ml/s");
511
            factor = 1e3;
512
        }
513
        else if (UnitValue < 1e9) {
514
            unitString = QString::fromLatin1("l/s");
515
            factor = 1e6;
516
        }
517
        else {
518
            unitString = QString::fromLatin1("m^3/s");
519
            factor = 1e9;
520
        }
521
    }
522
    else if (unit == Unit::DissipationRate) {
523
        unitString = QString::fromLatin1("m^2/s^3");
524
        factor = 1e6;
525
    }
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");
529
            factor = 1e-3;
530
        }
531
        else if (UnitValue < 1e-3) {
532
            unitString = QString::fromLatin1("1/km");
533
            factor = 1e-6;
534
        }
535
        else if (UnitValue < 1.0) {
536
            unitString = QString::fromLatin1("1/m");
537
            factor = 1e-3;
538
        }
539
        else if (UnitValue < 1e3) {
540
            unitString = QString::fromLatin1("1/mm");
541
            factor = 1.0;
542
        }
543
        else if (UnitValue < 1e6) {
544
            unitString = QString::fromUtf8("1/\xC2\xB5m");
545
            factor = 1e3;
546
        }
547
        else if (UnitValue < 1e9) {
548
            unitString = QString::fromLatin1("1/nm");
549
            factor = 1e6;
550
        }
551
        else {  // larger -> scientific notation
552
            unitString = QString::fromLatin1("1/m");
553
            factor = 1e-3;
554
        }
555
    }
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");
559
            factor = 1e-6;
560
        }
561
        else if (UnitValue < 1e-6) {
562
            unitString = QString::fromLatin1("1/km^2");
563
            factor = 1e-12;
564
        }
565
        else if (UnitValue < 1.0) {
566
            unitString = QString::fromLatin1("1/m^2");
567
            factor = 1e-6;
568
        }
569
        else if (UnitValue < 1e2) {
570
            unitString = QString::fromLatin1("1/cm^2");
571
            factor = 1e-2;
572
        }
573
        else {
574
            unitString = QString::fromLatin1("1/mm^2");
575
            factor = 1.0;
576
        }
577
    }
578
    else if (unit == Unit::InverseVolume) {
579
        if (UnitValue < 1e-6) {
580
            unitString = QString::fromLatin1("1/m^3");
581
            factor = 1e-9;
582
        }
583
        else if (UnitValue < 1e-3) {
584
            unitString = QString::fromLatin1("1/l");
585
            factor = 1e-6;
586
        }
587
        else if (UnitValue < 1.0) {
588
            unitString = QString::fromLatin1("1/ml");
589
            factor = 1e-3;
590
        }
591
        else {
592
            unitString = QString::fromLatin1("1/mm^3");
593
            factor = 1.0;
594
        }
595
    }
596
    else {
597
        // default action for all cases without special treatment:
598
        unitString = quant.getUnit().getString();
599
        factor = 1.0;
600
    }
601

602
    return toLocale(quant, factor, unitString);
603
}
604

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.