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;
45 currentSystem = UnitsAPI_SI;
46 if( SICurrentUnits.IsNull() ) {
48 OSD_Environment env3("CSF_CurrentUnits");
49 TCollection_AsciiString csfcurrent (env3.Value());
50 if( csfcurrent.Length() > 0 )
51 SICurrentUnits = new Resource_Manager(csfcurrent.ToCString());
53 SICurrentUnits = new Resource_Manager("CurrentUnits");
55 SICurrentUnits = new Resource_Manager("CurrentUnits");
58 CurrentUnits = SICurrentUnits;
59 LocalSystemUnits = SILocalSystemUnits;
62 currentSystem = UnitsAPI_MDTV;
63 if( MDTVCurrentUnits.IsNull() ) {
65 OSD_Environment env4("CSF_MDTVCurrentUnits");
66 TCollection_AsciiString csfmdtvcurrent (env4.Value());
67 if( csfmdtvcurrent.Length() > 0 )
68 MDTVCurrentUnits = new Resource_Manager(csfmdtvcurrent.ToCString());
70 MDTVCurrentUnits = new Resource_Manager("MDTVCurrentUnits");
72 MDTVCurrentUnits = new Resource_Manager("MDTVCurrentUnits");
75 CurrentUnits = MDTVCurrentUnits;
76 if( MDTVLocalSystemUnits.IsEmpty() ) {
77 MDTVLocalSystemUnits.Specify("LENGTH","mm");
78 MDTVLocalSystemUnits.Specify("AREA","mm\xB2");
79 MDTVLocalSystemUnits.Specify("VOLUME","mm\xB3");
80 MDTVLocalSystemUnits.Specify("INERTIA","mm**4");
81 MDTVLocalSystemUnits.Specify("SPEED","mm/s");
82 MDTVLocalSystemUnits.Specify("ACCELERATION","mm/s\xB2");
83 MDTVLocalSystemUnits.Specify("VOLUMIC MASS","kg/mm\xB3");
84 MDTVLocalSystemUnits.Specify("VOLUME FLOW","mm\xB3/s");
85 MDTVLocalSystemUnits.Specify("CONSUMPTION","mm\xB2");
86 MDTVLocalSystemUnits.Specify("QUANTITY OF MOVEMENT","kg*mm/s");
87 MDTVLocalSystemUnits.Specify("KINETIC MOMENT","kg*mm\xB2/s");
88 MDTVLocalSystemUnits.Specify("MOMENT OF INERTIA","kg*mm\xB2");
89 MDTVLocalSystemUnits.Specify("FORCE","kg*mm/s\xB2");
90 MDTVLocalSystemUnits.Specify("LINEIC FORCE","kg/s\xB2");
91 MDTVLocalSystemUnits.Specify("MOMENT OF A FORCE","kg*mm\xB2/s\xB2");
92 MDTVLocalSystemUnits.Specify("PRESSURE","kg/(mm*s\xB2)");
93 MDTVLocalSystemUnits.Specify("DYNAMIC VISCOSITY","kg/(mm*s)");
94 MDTVLocalSystemUnits.Specify("KINETIC VISCOSITY","mm\xB2/s");
95 MDTVLocalSystemUnits.Specify("TENSION SUPERFICIELLE","mm/s\xB2");
96 MDTVLocalSystemUnits.Specify("ENERGY","kg*mm\xB2/s\xB2");
97 MDTVLocalSystemUnits.Specify("POWER","kg*mm\xB2/s\xB3");
98 MDTVLocalSystemUnits.Specify("LINEIC POWER","kg*mm/s\xB3");
99 MDTVLocalSystemUnits.Specify("SURFACIC POWER","kg/s\xB3");
100 MDTVLocalSystemUnits.Specify("VOLUMIC POWER","kg/(mm*s\xB3)");
101 MDTVLocalSystemUnits.Specify("THERMICAL CONDUCTIVITY","kg*mm/(s\xB3*\xB0K)");
102 MDTVLocalSystemUnits.Specify("THERMICAL CONVECTIVITY","kg/(s\xB3*\xB0K)");
103 MDTVLocalSystemUnits.Specify("THERMICAL MASSIC CAPACITY","mm\xB2/(s\xB2*\xB0K)");
104 MDTVLocalSystemUnits.Specify("ENTROPY","kg*mm\xB2/(s\xB2*\xB0K)");
105 MDTVLocalSystemUnits.Specify("ENTHALPY","kg*mm\xB2/s\xB2");
106 MDTVLocalSystemUnits.Specify("LUMINANCE","cd/mm\xB2");
107 MDTVLocalSystemUnits.Specify("LUMINOUS EFFICACITY","s\xB3*Lu/(kg*mm\xB2)");
108 MDTVLocalSystemUnits.Specify("ELECTRIC FIELD","V/mm");
109 MDTVLocalSystemUnits.Specify("ELECTRIC CAPACITANCE","s**4*A\xB2/(kg*mm\xB2)");
110 MDTVLocalSystemUnits.Specify("MAGNETIC FIELD","A/mm");
111 MDTVLocalSystemUnits.Specify("MAGNETIC FLUX","kg*mm\xB2/(s\xB2*A)");
112 MDTVLocalSystemUnits.Specify("INDUCTANCE","kg*mm\xB2/(s\xB2*A\xB2)");
113 MDTVLocalSystemUnits.Specify("RELUCTANCE","s\xB2*A\xB2/(kg*mm\xB2)");
114 MDTVLocalSystemUnits.Specify("RESISTIVITY","O*mm");
115 MDTVLocalSystemUnits.Specify("CONDUCTIVITY","S/mm");
116 MDTVLocalSystemUnits.Specify("MOLAR MASS","kg/mol");
117 MDTVLocalSystemUnits.Specify("MOLAR VOLUME","mm\xB3/mol");
118 MDTVLocalSystemUnits.Specify("CONCENTRATION","kg/mm\xB3");
119 MDTVLocalSystemUnits.Specify("MOLAR CONCENTRATION","mol/mm\xB3");
120 MDTVLocalSystemUnits.Specify("ACCOUSTIC INTENSITY","mm/A\xB2");
121 MDTVLocalSystemUnits.Specify("DOSE EQUIVALENT","mm\xB2/s\xB2");
122 MDTVLocalSystemUnits.Specify("ABSORBED DOSE","mm\xB2/s\xB2");
123 MDTVLocalSystemUnits.Specify("FLUX OF MAGNETIC INDUCTION","kg*mm\xB2/(s\xB2*A)");
124 MDTVLocalSystemUnits.Specify("ROTATION ACCELERATION","rad/s\xB2");
125 MDTVLocalSystemUnits.Specify("TRANSLATION STIFFNESS","kg/s\xB2");
126 MDTVLocalSystemUnits.Specify("ROTATION STIFFNESS","kg*mm\xB2/(s\xB2*rad)");
127 MDTVLocalSystemUnits.Activates();
129 LocalSystemUnits = MDTVLocalSystemUnits;
136 //=======================================================================
137 //function : CurrentToLS
139 //=======================================================================
141 Standard_Real UnitsAPI::CurrentToLS(const Standard_Real aData,
142 const Standard_CString aQuantity)
144 Standard_Real aValue = aData;
145 CheckLoading (localSystem);
146 if( CurrentUnits->Find(aQuantity) ) {
147 TCollection_AsciiString current(CurrentUnits->Value(aQuantity));
148 aValue = Units::ToSI(aData,current.ToCString());
149 aValue = LocalSystemUnits.ConvertSIValueToUserSystem(aQuantity,aValue);
153 cout <<"Warning: UnitsAPI,the quantity '" << aQuantity << "' does not exist in the current units system" << endl;
161 //=======================================================================
162 //function : CurrentToSI
164 //=======================================================================
166 Standard_Real UnitsAPI::CurrentToSI(const Standard_Real aData,
167 const Standard_CString aQuantity)
169 Standard_Real aValue = aData;
170 CheckLoading (UnitsAPI_DEFAULT);
171 if( CurrentUnits->Find(aQuantity) ) {
172 TCollection_AsciiString current(CurrentUnits->Value(aQuantity));
173 aValue = Units::ToSI(aData,current.ToCString());
177 cout<<"Warning: UnitsAPI,the quantity '" << aQuantity << "' does not exist in the current units system" << endl;
185 //=======================================================================
186 //function : CurrentFromLS
188 //=======================================================================
190 Standard_Real UnitsAPI::CurrentFromLS(const Standard_Real aData,
191 const Standard_CString aQuantity)
193 Standard_Real aValue = aData;
194 CheckLoading (localSystem);
195 if( CurrentUnits->Find(aQuantity) ) {
196 TCollection_AsciiString current(CurrentUnits->Value(aQuantity));
197 aValue = LocalSystemUnits.ConvertUserSystemValueToSI(aQuantity,aData);
198 aValue = Units::FromSI(aValue,current.ToCString());
202 cout<<"Warning: UnitsAPI,the quantity '" << aQuantity << "' does not exist in the current units system" << endl;
210 //=======================================================================
211 //function : CurrentFromSI
213 //=======================================================================
215 Standard_Real UnitsAPI::CurrentFromSI(const Standard_Real aData,
216 const Standard_CString aQuantity)
218 Standard_Real aValue = aData;
219 CheckLoading (UnitsAPI_DEFAULT);
220 if( CurrentUnits->Find(aQuantity) ) {
221 TCollection_AsciiString current(CurrentUnits->Value(aQuantity));
222 aValue = Units::FromSI(aData,current.ToCString());
226 cout<<"Warning: UnitsAPI,the quantity '" << aQuantity << "' does not exist in the current units system" << endl;
234 //=======================================================================
235 //function : CurrentToAny
237 //=======================================================================
239 Standard_Real UnitsAPI::CurrentToAny(const Standard_Real aData,
240 const Standard_CString aQuantity,
241 const Standard_CString aUnit) {
242 Standard_Real aValue = aData;
243 CheckLoading (UnitsAPI_DEFAULT);
244 if( CurrentUnits->Find(aQuantity) ) {
245 TCollection_AsciiString current(CurrentUnits->Value(aQuantity));
246 aValue = AnyToAny(aData,current.ToCString(),aUnit);
250 cout<<"Warning: UnitsAPI,the quantity '" << aQuantity << "' does not exist in the current units system" << endl;
258 //=======================================================================
259 //function : CurrentFromAny
261 //=======================================================================
263 Standard_Real UnitsAPI::CurrentFromAny(const Standard_Real aData,
264 const Standard_CString aQuantity,
265 const Standard_CString aUnit)
267 Standard_Real aValue = aData;
268 CheckLoading (UnitsAPI_DEFAULT);
269 if( CurrentUnits->Find(aQuantity) ) {
270 TCollection_AsciiString current(CurrentUnits->Value(aQuantity));
271 aValue = AnyToAny(aData,aUnit,current.ToCString());
275 cout<<"Warning: UnitsAPI,the quantity '" << aQuantity << "' does not exist in the current units system" << endl;
283 //=======================================================================
286 //=======================================================================
288 Standard_Real UnitsAPI::AnyToLS(const Standard_Real aData,
289 const Standard_CString aUnit)
291 Standard_Real aValue = aData;
292 CheckLoading (localSystem);
293 Handle(Units_Dimensions) aDim;
294 aValue = Units::ToSI(aValue,aUnit,aDim);
297 Standard_CString quantity = aDim->Quantity();
299 aValue = LocalSystemUnits.ConvertSIValueToUserSystem(quantity,aValue);
303 cout<<"Warning: BAD Quantity returns in UnitsAPI::AnyToLS(" << aData << "," << aUnit << ")" << endl;
309 //=======================================================================
312 //=======================================================================
314 Standard_Real UnitsAPI::AnyToLS(const Standard_Real aData,
315 const Standard_CString aUnit,
316 Handle(Units_Dimensions) &aDim)
318 Standard_Real aValue = aData;
319 CheckLoading (localSystem);
320 aValue = Units::ToSI(aValue,aUnit,aDim);
321 Standard_CString quantity = aDim->Quantity();
325 aValue = LocalSystemUnits.ConvertSIValueToUserSystem(quantity,aValue);
329 cout<<"Warning: BAD Quantity returns in UnitsAPI::AnyToLS(" << aData << "," << aUnit << "," << aDim.get() << ")" << endl;
335 //=======================================================================
338 //=======================================================================
340 Standard_Real UnitsAPI::AnyToSI(const Standard_Real aData,
341 const Standard_CString aUnit)
343 Standard_Real aValue;
344 CheckLoading (UnitsAPI_DEFAULT);
345 aValue = Units::ToSI(aData,aUnit);
350 //=======================================================================
353 //=======================================================================
355 Standard_Real UnitsAPI::AnyToSI(const Standard_Real aData,
356 const Standard_CString aUnit,
357 Handle(Units_Dimensions) &aDim)
359 Standard_Real aValue;
360 CheckLoading (UnitsAPI_DEFAULT);
361 aValue = Units::ToSI(aData,aUnit,aDim);
366 //=======================================================================
367 //function : AnyFromLS
369 //=======================================================================
371 Standard_Real UnitsAPI::AnyFromLS(const Standard_Real aData,
372 const Standard_CString aUnit)
374 Standard_Real aValue = aData;
375 CheckLoading (localSystem);
376 Handle(Units_Dimensions) aDim;
377 aValue = Units::FromSI(aValue,aUnit,aDim);
378 Standard_CString quantity = aDim->Quantity();
380 aValue = LocalSystemUnits.ConvertUserSystemValueToSI(quantity,aValue);
384 cout<<"Warning: BAD Quantity returns in UnitsAPI::AnyToLS(" << aData << "," << aUnit << ")" << endl;
391 //=======================================================================
392 //function : AnyFromSI
394 //=======================================================================
396 Standard_Real UnitsAPI::AnyFromSI(const Standard_Real aData,
397 const Standard_CString aUnit)
399 Standard_Real aValue;
400 CheckLoading (UnitsAPI_DEFAULT);
401 aValue = Units::FromSI(aData,aUnit);
406 //=======================================================================
407 //function : AnyToAny
409 //=======================================================================
411 Standard_Real UnitsAPI::AnyToAny(const Standard_Real aData,
412 const Standard_CString aUnit1,
413 const Standard_CString aUnit2)
415 Standard_Real aValue = aData;
416 CheckLoading (UnitsAPI_DEFAULT);
417 aValue = Units::Convert(aValue,aUnit1,aUnit2);
422 //=======================================================================
425 //=======================================================================
427 Standard_Real UnitsAPI::LSToSI(const Standard_Real aData,
428 const Standard_CString aQuantity)
430 Standard_Real aValue = aData;
431 CheckLoading (localSystem);
432 if( CurrentUnits->Find(aQuantity) ) {
433 aValue = LocalSystemUnits.ConvertUserSystemValueToSI(aQuantity,aData);
437 cout<<"Warning: UnitsAPI,the quantity '" << aQuantity << "' does not exist in the current units system" << endl;
445 //=======================================================================
448 //=======================================================================
450 Standard_Real UnitsAPI::SIToLS(const Standard_Real aData,
451 const Standard_CString aQuantity)
453 Standard_Real aValue = aData;
454 CheckLoading (localSystem);
455 if( CurrentUnits->Find(aQuantity) ) {
456 aValue = LocalSystemUnits.ConvertSIValueToUserSystem(aQuantity,aValue);
460 cout<<"Warning: UnitsAPI,the quantity '" << aQuantity << "' does not exist in the current units system" << endl;
468 //=======================================================================
469 //function : SetLocalSystem
471 //=======================================================================
473 void UnitsAPI::SetLocalSystem(const UnitsAPI_SystemUnits aSystemUnits)
475 CheckLoading (aSystemUnits);
476 localSystem = currentSystem;
480 //=======================================================================
481 //function : LocalSystem
483 //=======================================================================
485 UnitsAPI_SystemUnits UnitsAPI::LocalSystem()
491 //=======================================================================
492 //function : SetCurrentUnit
494 //=======================================================================
496 void UnitsAPI::SetCurrentUnit(const Standard_CString aQuantity,
497 const Standard_CString anUnit)
499 CheckLoading(localSystem);
500 CurrentUnits->SetResource(aQuantity,anUnit);
504 //=======================================================================
507 //=======================================================================
509 void UnitsAPI::Save()
511 CheckLoading(localSystem);
512 CurrentUnits->Save();
516 //=======================================================================
519 //=======================================================================
521 void UnitsAPI::Reload()
523 currentSystem = UnitsAPI_DEFAULT;
524 CheckLoading(localSystem);
528 //=======================================================================
529 //function : CurrentUnit
531 //=======================================================================
533 static TCollection_AsciiString astring;
534 Standard_CString UnitsAPI::CurrentUnit(const Standard_CString aQuantity)
536 CheckLoading(localSystem);
537 astring = CurrentUnits->Value(aQuantity);
538 return astring.ToCString();
542 //=======================================================================
543 //function : Dimensions
545 //=======================================================================
547 Handle(Units_Dimensions) UnitsAPI::Dimensions(const Standard_CString aType)
549 return Units::Dimensions(aType);
553 //=======================================================================
554 //function : DimensionLess
556 //=======================================================================
558 Handle(Units_Dimensions) UnitsAPI::DimensionLess()
560 return Units_Dimensions::ALess();
564 //=======================================================================
565 //function : DimensionMass
567 //=======================================================================
569 Handle(Units_Dimensions) UnitsAPI::DimensionMass()
571 return Units_Dimensions::AMass();
575 //=======================================================================
576 //function : DimensionLength
578 //=======================================================================
580 Handle(Units_Dimensions) UnitsAPI::DimensionLength()
582 return Units_Dimensions::ALength();
586 //=======================================================================
587 //function : DimensionTime
589 //=======================================================================
591 Handle(Units_Dimensions) UnitsAPI::DimensionTime()
593 return Units_Dimensions::ATime() ;
597 //=======================================================================
598 //function : DimensionElectricCurrent
600 //=======================================================================
602 Handle(Units_Dimensions) UnitsAPI::DimensionElectricCurrent()
604 return Units_Dimensions::AElectricCurrent() ;
608 //=======================================================================
609 //function : DimensionThermodynamicTemperature
611 //=======================================================================
613 Handle(Units_Dimensions) UnitsAPI::DimensionThermodynamicTemperature()
615 return Units_Dimensions::AThermodynamicTemperature();
619 //=======================================================================
620 //function : DimensionAmountOfSubstance
622 //=======================================================================
624 Handle(Units_Dimensions) UnitsAPI::DimensionAmountOfSubstance()
626 return Units_Dimensions::AAmountOfSubstance();
630 //=======================================================================
631 //function : DimensionLuminousIntensity
633 //=======================================================================
635 Handle(Units_Dimensions) UnitsAPI::DimensionLuminousIntensity()
637 return Units_Dimensions::ALuminousIntensity();
641 //=======================================================================
642 //function : DimensionPlaneAngle
644 //=======================================================================
646 Handle(Units_Dimensions) UnitsAPI::DimensionPlaneAngle()
648 return Units_Dimensions::APlaneAngle();
652 //=======================================================================
653 //function : DimensionSolidAngle
655 //=======================================================================
657 Handle(Units_Dimensions) UnitsAPI::DimensionSolidAngle()
659 return Units_Dimensions::ASolidAngle();
663 //=======================================================================
666 //=======================================================================
668 Standard_Boolean UnitsAPI::Check(const Standard_CString aQuantity,
669 const Standard_CString /*aUnit*/)
671 Standard_Boolean status = Standard_False;
672 CheckLoading (UnitsAPI_DEFAULT);
673 if( CurrentUnits->Find(aQuantity) ) {
674 TCollection_AsciiString current(CurrentUnits->Value(aQuantity));
675 // aValue = AnyToAny(aData,current.ToCString(),aUnit);
676 // aValue = Units::Convert(aValue,aUnit1,aUnit2);