1 // Copyright (c) 1998-1999 Matra Datavision
2 // Copyright (c) 1999-2014 OPEN CASCADE SAS
4 // This file is part of Open CASCADE Technology software library.
6 // This library is free software; you can redistribute it and/or modify it under
7 // the terms of the GNU Lesser General Public License version 2.1 as published
8 // by the Free Software Foundation, with special exception defined in the file
9 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
10 // distribution for complete text of the license and disclaimer of any warranty.
12 // Alternatively, this file may be used under the terms of Open CASCADE
13 // commercial license or contractual agreement.
16 #include <OSD_Environment.hxx>
17 #include <Resource_Manager.hxx>
18 #include <Standard_NoSuchObject.hxx>
19 #include <TCollection_AsciiString.hxx>
21 #include <Units_Dimensions.hxx>
22 #include <Units_Quantity.hxx>
23 #include <Units_Unit.hxx>
24 #include <Units_UnitsSystem.hxx>
25 #include <UnitsAPI.hxx>
27 static Handle(Resource_Manager) CurrentUnits,SICurrentUnits,MDTVCurrentUnits;
28 static Units_UnitsSystem LocalSystemUnits,SILocalSystemUnits,MDTVLocalSystemUnits;
29 static TCollection_AsciiString rstring;
30 static UnitsAPI_SystemUnits localSystem = UnitsAPI_SI;
31 static UnitsAPI_SystemUnits currentSystem = UnitsAPI_DEFAULT;
33 //=======================================================================
34 //function : CheckLoading
36 //=======================================================================
38 void UnitsAPI::CheckLoading (const UnitsAPI_SystemUnits aSystemUnits)
40 if( currentSystem != aSystemUnits || CurrentUnits.IsNull()) {
41 switch (aSystemUnits) {
42 case UnitsAPI_DEFAULT :
43 if( !CurrentUnits.IsNull() ) break;
46 currentSystem = UnitsAPI_SI;
47 if( SICurrentUnits.IsNull() ) {
49 OSD_Environment env3("CSF_CurrentUnits");
50 TCollection_AsciiString csfcurrent (env3.Value());
51 if( csfcurrent.Length() > 0 )
52 SICurrentUnits = new Resource_Manager(csfcurrent.ToCString());
54 SICurrentUnits = new Resource_Manager("CurrentUnits");
56 SICurrentUnits = new Resource_Manager("CurrentUnits");
59 CurrentUnits = SICurrentUnits;
60 LocalSystemUnits = SILocalSystemUnits;
63 currentSystem = UnitsAPI_MDTV;
64 if( MDTVCurrentUnits.IsNull() ) {
66 OSD_Environment env4("CSF_MDTVCurrentUnits");
67 TCollection_AsciiString csfmdtvcurrent (env4.Value());
68 if( csfmdtvcurrent.Length() > 0 )
69 MDTVCurrentUnits = new Resource_Manager(csfmdtvcurrent.ToCString());
71 MDTVCurrentUnits = new Resource_Manager("MDTVCurrentUnits");
73 MDTVCurrentUnits = new Resource_Manager("MDTVCurrentUnits");
76 CurrentUnits = MDTVCurrentUnits;
77 if( MDTVLocalSystemUnits.IsEmpty() ) {
78 MDTVLocalSystemUnits.Specify("LENGTH","mm");
79 MDTVLocalSystemUnits.Specify("AREA","mm\xB2");
80 MDTVLocalSystemUnits.Specify("VOLUME","mm\xB3");
81 MDTVLocalSystemUnits.Specify("INERTIA","mm**4");
82 MDTVLocalSystemUnits.Specify("SPEED","mm/s");
83 MDTVLocalSystemUnits.Specify("ACCELERATION","mm/s\xB2");
84 MDTVLocalSystemUnits.Specify("VOLUMIC MASS","kg/mm\xB3");
85 MDTVLocalSystemUnits.Specify("VOLUME FLOW","mm\xB3/s");
86 MDTVLocalSystemUnits.Specify("CONSUMPTION","mm\xB2");
87 MDTVLocalSystemUnits.Specify("QUANTITY OF MOVEMENT","kg*mm/s");
88 MDTVLocalSystemUnits.Specify("KINETIC MOMENT","kg*mm\xB2/s");
89 MDTVLocalSystemUnits.Specify("MOMENT OF INERTIA","kg*mm\xB2");
90 MDTVLocalSystemUnits.Specify("FORCE","kg*mm/s\xB2");
91 MDTVLocalSystemUnits.Specify("LINEIC FORCE","kg/s\xB2");
92 MDTVLocalSystemUnits.Specify("MOMENT OF A FORCE","kg*mm\xB2/s\xB2");
93 MDTVLocalSystemUnits.Specify("PRESSURE","kg/(mm*s\xB2)");
94 MDTVLocalSystemUnits.Specify("DYNAMIC VISCOSITY","kg/(mm*s)");
95 MDTVLocalSystemUnits.Specify("KINETIC VISCOSITY","mm\xB2/s");
96 MDTVLocalSystemUnits.Specify("TENSION SUPERFICIELLE","mm/s\xB2");
97 MDTVLocalSystemUnits.Specify("ENERGY","kg*mm\xB2/s\xB2");
98 MDTVLocalSystemUnits.Specify("POWER","kg*mm\xB2/s\xB3");
99 MDTVLocalSystemUnits.Specify("LINEIC POWER","kg*mm/s\xB3");
100 MDTVLocalSystemUnits.Specify("SURFACIC POWER","kg/s\xB3");
101 MDTVLocalSystemUnits.Specify("VOLUMIC POWER","kg/(mm*s\xB3)");
102 MDTVLocalSystemUnits.Specify("THERMICAL CONDUCTIVITY","kg*mm/(s\xB3*\xB0K)");
103 MDTVLocalSystemUnits.Specify("THERMICAL CONVECTIVITY","kg/(s\xB3*\xB0K)");
104 MDTVLocalSystemUnits.Specify("THERMICAL MASSIC CAPACITY","mm\xB2/(s\xB2*\xB0K)");
105 MDTVLocalSystemUnits.Specify("ENTROPY","kg*mm\xB2/(s\xB2*\xB0K)");
106 MDTVLocalSystemUnits.Specify("ENTHALPY","kg*mm\xB2/s\xB2");
107 MDTVLocalSystemUnits.Specify("LUMINANCE","cd/mm\xB2");
108 MDTVLocalSystemUnits.Specify("LUMINOUS EFFICACITY","s\xB3*Lu/(kg*mm\xB2)");
109 MDTVLocalSystemUnits.Specify("ELECTRIC FIELD","V/mm");
110 MDTVLocalSystemUnits.Specify("ELECTRIC CAPACITANCE","s**4*A\xB2/(kg*mm\xB2)");
111 MDTVLocalSystemUnits.Specify("MAGNETIC FIELD","A/mm");
112 MDTVLocalSystemUnits.Specify("MAGNETIC FLUX","kg*mm\xB2/(s\xB2*A)");
113 MDTVLocalSystemUnits.Specify("INDUCTANCE","kg*mm\xB2/(s\xB2*A\xB2)");
114 MDTVLocalSystemUnits.Specify("RELUCTANCE","s\xB2*A\xB2/(kg*mm\xB2)");
115 MDTVLocalSystemUnits.Specify("RESISTIVITY","O*mm");
116 MDTVLocalSystemUnits.Specify("CONDUCTIVITY","S/mm");
117 MDTVLocalSystemUnits.Specify("MOLAR MASS","kg/mol");
118 MDTVLocalSystemUnits.Specify("MOLAR VOLUME","mm\xB3/mol");
119 MDTVLocalSystemUnits.Specify("CONCENTRATION","kg/mm\xB3");
120 MDTVLocalSystemUnits.Specify("MOLAR CONCENTRATION","mol/mm\xB3");
121 MDTVLocalSystemUnits.Specify("ACCOUSTIC INTENSITY","mm/A\xB2");
122 MDTVLocalSystemUnits.Specify("DOSE EQUIVALENT","mm\xB2/s\xB2");
123 MDTVLocalSystemUnits.Specify("ABSORBED DOSE","mm\xB2/s\xB2");
124 MDTVLocalSystemUnits.Specify("FLUX OF MAGNETIC INDUCTION","kg*mm\xB2/(s\xB2*A)");
125 MDTVLocalSystemUnits.Specify("ROTATION ACCELERATION","rad/s\xB2");
126 MDTVLocalSystemUnits.Specify("TRANSLATION STIFFNESS","kg/s\xB2");
127 MDTVLocalSystemUnits.Specify("ROTATION STIFFNESS","kg*mm\xB2/(s\xB2*rad)");
128 MDTVLocalSystemUnits.Activates();
130 LocalSystemUnits = MDTVLocalSystemUnits;
137 //=======================================================================
138 //function : CurrentToLS
140 //=======================================================================
142 Standard_Real UnitsAPI::CurrentToLS(const Standard_Real aData,
143 const Standard_CString aQuantity)
145 Standard_Real aValue = aData;
146 CheckLoading (localSystem);
147 if( CurrentUnits->Find(aQuantity) ) {
148 TCollection_AsciiString current(CurrentUnits->Value(aQuantity));
149 aValue = Units::ToSI(aData,current.ToCString());
150 aValue = LocalSystemUnits.ConvertSIValueToUserSystem(aQuantity,aValue);
154 cout <<"Warning: UnitsAPI,the quantity '" << aQuantity << "' does not exist in the current units system" << endl;
162 //=======================================================================
163 //function : CurrentToSI
165 //=======================================================================
167 Standard_Real UnitsAPI::CurrentToSI(const Standard_Real aData,
168 const Standard_CString aQuantity)
170 Standard_Real aValue = aData;
171 CheckLoading (UnitsAPI_DEFAULT);
172 if( CurrentUnits->Find(aQuantity) ) {
173 TCollection_AsciiString current(CurrentUnits->Value(aQuantity));
174 aValue = Units::ToSI(aData,current.ToCString());
178 cout<<"Warning: UnitsAPI,the quantity '" << aQuantity << "' does not exist in the current units system" << endl;
186 //=======================================================================
187 //function : CurrentFromLS
189 //=======================================================================
191 Standard_Real UnitsAPI::CurrentFromLS(const Standard_Real aData,
192 const Standard_CString aQuantity)
194 Standard_Real aValue = aData;
195 CheckLoading (localSystem);
196 if( CurrentUnits->Find(aQuantity) ) {
197 TCollection_AsciiString current(CurrentUnits->Value(aQuantity));
198 aValue = LocalSystemUnits.ConvertUserSystemValueToSI(aQuantity,aData);
199 aValue = Units::FromSI(aValue,current.ToCString());
203 cout<<"Warning: UnitsAPI,the quantity '" << aQuantity << "' does not exist in the current units system" << endl;
211 //=======================================================================
212 //function : CurrentFromSI
214 //=======================================================================
216 Standard_Real UnitsAPI::CurrentFromSI(const Standard_Real aData,
217 const Standard_CString aQuantity)
219 Standard_Real aValue = aData;
220 CheckLoading (UnitsAPI_DEFAULT);
221 if( CurrentUnits->Find(aQuantity) ) {
222 TCollection_AsciiString current(CurrentUnits->Value(aQuantity));
223 aValue = Units::FromSI(aData,current.ToCString());
227 cout<<"Warning: UnitsAPI,the quantity '" << aQuantity << "' does not exist in the current units system" << endl;
235 //=======================================================================
236 //function : CurrentToAny
238 //=======================================================================
240 Standard_Real UnitsAPI::CurrentToAny(const Standard_Real aData,
241 const Standard_CString aQuantity,
242 const Standard_CString aUnit) {
243 Standard_Real aValue = aData;
244 CheckLoading (UnitsAPI_DEFAULT);
245 if( CurrentUnits->Find(aQuantity) ) {
246 TCollection_AsciiString current(CurrentUnits->Value(aQuantity));
247 aValue = AnyToAny(aData,current.ToCString(),aUnit);
251 cout<<"Warning: UnitsAPI,the quantity '" << aQuantity << "' does not exist in the current units system" << endl;
259 //=======================================================================
260 //function : CurrentFromAny
262 //=======================================================================
264 Standard_Real UnitsAPI::CurrentFromAny(const Standard_Real aData,
265 const Standard_CString aQuantity,
266 const Standard_CString aUnit)
268 Standard_Real aValue = aData;
269 CheckLoading (UnitsAPI_DEFAULT);
270 if( CurrentUnits->Find(aQuantity) ) {
271 TCollection_AsciiString current(CurrentUnits->Value(aQuantity));
272 aValue = AnyToAny(aData,aUnit,current.ToCString());
276 cout<<"Warning: UnitsAPI,the quantity '" << aQuantity << "' does not exist in the current units system" << endl;
284 //=======================================================================
287 //=======================================================================
289 Standard_Real UnitsAPI::AnyToLS(const Standard_Real aData,
290 const Standard_CString aUnit)
292 Standard_Real aValue = aData;
293 CheckLoading (localSystem);
294 Handle(Units_Dimensions) aDim;
295 aValue = Units::ToSI(aValue,aUnit,aDim);
298 Standard_CString quantity = aDim->Quantity();
300 aValue = LocalSystemUnits.ConvertSIValueToUserSystem(quantity,aValue);
304 cout<<"Warning: BAD Quantity returns in UnitsAPI::AnyToLS(" << aData << "," << aUnit << ")" << endl;
310 //=======================================================================
313 //=======================================================================
315 Standard_Real UnitsAPI::AnyToLS(const Standard_Real aData,
316 const Standard_CString aUnit,
317 Handle(Units_Dimensions) &aDim)
319 Standard_Real aValue = aData;
320 CheckLoading (localSystem);
321 aValue = Units::ToSI(aValue,aUnit,aDim);
322 Standard_CString quantity = aDim->Quantity();
326 aValue = LocalSystemUnits.ConvertSIValueToUserSystem(quantity,aValue);
330 cout<<"Warning: BAD Quantity returns in UnitsAPI::AnyToLS(" << aData << "," << aUnit << "," << aDim.get() << ")" << endl;
336 //=======================================================================
339 //=======================================================================
341 Standard_Real UnitsAPI::AnyToSI(const Standard_Real aData,
342 const Standard_CString aUnit)
344 Standard_Real aValue;
345 CheckLoading (UnitsAPI_DEFAULT);
346 aValue = Units::ToSI(aData,aUnit);
351 //=======================================================================
354 //=======================================================================
356 Standard_Real UnitsAPI::AnyToSI(const Standard_Real aData,
357 const Standard_CString aUnit,
358 Handle(Units_Dimensions) &aDim)
360 Standard_Real aValue;
361 CheckLoading (UnitsAPI_DEFAULT);
362 aValue = Units::ToSI(aData,aUnit,aDim);
367 //=======================================================================
368 //function : AnyFromLS
370 //=======================================================================
372 Standard_Real UnitsAPI::AnyFromLS(const Standard_Real aData,
373 const Standard_CString aUnit)
375 Standard_Real aValue = aData;
376 CheckLoading (localSystem);
377 Handle(Units_Dimensions) aDim;
378 aValue = Units::FromSI(aValue,aUnit,aDim);
379 Standard_CString quantity = aDim->Quantity();
381 aValue = LocalSystemUnits.ConvertUserSystemValueToSI(quantity,aValue);
385 cout<<"Warning: BAD Quantity returns in UnitsAPI::AnyToLS(" << aData << "," << aUnit << ")" << endl;
392 //=======================================================================
393 //function : AnyFromSI
395 //=======================================================================
397 Standard_Real UnitsAPI::AnyFromSI(const Standard_Real aData,
398 const Standard_CString aUnit)
400 Standard_Real aValue;
401 CheckLoading (UnitsAPI_DEFAULT);
402 aValue = Units::FromSI(aData,aUnit);
407 //=======================================================================
408 //function : AnyToAny
410 //=======================================================================
412 Standard_Real UnitsAPI::AnyToAny(const Standard_Real aData,
413 const Standard_CString aUnit1,
414 const Standard_CString aUnit2)
416 Standard_Real aValue = aData;
417 CheckLoading (UnitsAPI_DEFAULT);
418 aValue = Units::Convert(aValue,aUnit1,aUnit2);
423 //=======================================================================
426 //=======================================================================
428 Standard_Real UnitsAPI::LSToSI(const Standard_Real aData,
429 const Standard_CString aQuantity)
431 Standard_Real aValue = aData;
432 CheckLoading (localSystem);
433 if( CurrentUnits->Find(aQuantity) ) {
434 aValue = LocalSystemUnits.ConvertUserSystemValueToSI(aQuantity,aData);
438 cout<<"Warning: UnitsAPI,the quantity '" << aQuantity << "' does not exist in the current units system" << endl;
446 //=======================================================================
449 //=======================================================================
451 Standard_Real UnitsAPI::SIToLS(const Standard_Real aData,
452 const Standard_CString aQuantity)
454 Standard_Real aValue = aData;
455 CheckLoading (localSystem);
456 if( CurrentUnits->Find(aQuantity) ) {
457 aValue = LocalSystemUnits.ConvertSIValueToUserSystem(aQuantity,aValue);
461 cout<<"Warning: UnitsAPI,the quantity '" << aQuantity << "' does not exist in the current units system" << endl;
469 //=======================================================================
470 //function : SetLocalSystem
472 //=======================================================================
474 void UnitsAPI::SetLocalSystem(const UnitsAPI_SystemUnits aSystemUnits)
476 CheckLoading (aSystemUnits);
477 localSystem = currentSystem;
481 //=======================================================================
482 //function : LocalSystem
484 //=======================================================================
486 UnitsAPI_SystemUnits UnitsAPI::LocalSystem()
492 //=======================================================================
493 //function : SetCurrentUnit
495 //=======================================================================
497 void UnitsAPI::SetCurrentUnit(const Standard_CString aQuantity,
498 const Standard_CString anUnit)
500 CheckLoading(localSystem);
501 CurrentUnits->SetResource(aQuantity,anUnit);
505 //=======================================================================
508 //=======================================================================
510 void UnitsAPI::Save()
512 CheckLoading(localSystem);
513 CurrentUnits->Save();
517 //=======================================================================
520 //=======================================================================
522 void UnitsAPI::Reload()
524 currentSystem = UnitsAPI_DEFAULT;
525 CheckLoading(localSystem);
529 //=======================================================================
530 //function : CurrentUnit
532 //=======================================================================
534 static TCollection_AsciiString astring;
535 Standard_CString UnitsAPI::CurrentUnit(const Standard_CString aQuantity)
537 CheckLoading(localSystem);
538 astring = CurrentUnits->Value(aQuantity);
539 return astring.ToCString();
543 //=======================================================================
544 //function : Dimensions
546 //=======================================================================
548 Handle(Units_Dimensions) UnitsAPI::Dimensions(const Standard_CString aType)
550 return Units::Dimensions(aType);
554 //=======================================================================
555 //function : DimensionLess
557 //=======================================================================
559 Handle(Units_Dimensions) UnitsAPI::DimensionLess()
561 return Units_Dimensions::ALess();
565 //=======================================================================
566 //function : DimensionMass
568 //=======================================================================
570 Handle(Units_Dimensions) UnitsAPI::DimensionMass()
572 return Units_Dimensions::AMass();
576 //=======================================================================
577 //function : DimensionLength
579 //=======================================================================
581 Handle(Units_Dimensions) UnitsAPI::DimensionLength()
583 return Units_Dimensions::ALength();
587 //=======================================================================
588 //function : DimensionTime
590 //=======================================================================
592 Handle(Units_Dimensions) UnitsAPI::DimensionTime()
594 return Units_Dimensions::ATime() ;
598 //=======================================================================
599 //function : DimensionElectricCurrent
601 //=======================================================================
603 Handle(Units_Dimensions) UnitsAPI::DimensionElectricCurrent()
605 return Units_Dimensions::AElectricCurrent() ;
609 //=======================================================================
610 //function : DimensionThermodynamicTemperature
612 //=======================================================================
614 Handle(Units_Dimensions) UnitsAPI::DimensionThermodynamicTemperature()
616 return Units_Dimensions::AThermodynamicTemperature();
620 //=======================================================================
621 //function : DimensionAmountOfSubstance
623 //=======================================================================
625 Handle(Units_Dimensions) UnitsAPI::DimensionAmountOfSubstance()
627 return Units_Dimensions::AAmountOfSubstance();
631 //=======================================================================
632 //function : DimensionLuminousIntensity
634 //=======================================================================
636 Handle(Units_Dimensions) UnitsAPI::DimensionLuminousIntensity()
638 return Units_Dimensions::ALuminousIntensity();
642 //=======================================================================
643 //function : DimensionPlaneAngle
645 //=======================================================================
647 Handle(Units_Dimensions) UnitsAPI::DimensionPlaneAngle()
649 return Units_Dimensions::APlaneAngle();
653 //=======================================================================
654 //function : DimensionSolidAngle
656 //=======================================================================
658 Handle(Units_Dimensions) UnitsAPI::DimensionSolidAngle()
660 return Units_Dimensions::ASolidAngle();
664 //=======================================================================
667 //=======================================================================
669 Standard_Boolean UnitsAPI::Check(const Standard_CString aQuantity,
670 const Standard_CString /*aUnit*/)
672 Standard_Boolean status = Standard_False;
673 CheckLoading (UnitsAPI_DEFAULT);
674 if( CurrentUnits->Find(aQuantity) ) {
675 TCollection_AsciiString current(CurrentUnits->Value(aQuantity));
676 // aValue = AnyToAny(aData,current.ToCString(),aUnit);
677 // aValue = Units::Convert(aValue,aUnit1,aUnit2);