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 OSD_Environment env1("CSF_UnitsLexicon");
42 TCollection_AsciiString slexiconfile(env1.Value());
43 if( slexiconfile.Length() > 0 )
44 Units::LexiconFile(slexiconfile.ToCString());
46 OSD_Environment CasRootEnv("CASROOT");
47 TCollection_AsciiString CasRootString(CasRootEnv.Value());
48 if (CasRootString.Length() > 0 ) {
49 CasRootString += "/src/UnitsAPI/Lexi_Expr.dat" ;
50 Units::LexiconFile(CasRootString.ToCString());
53 Standard_NoSuchObject::Raise("environment variable CSF_UnitsLexicon undefined");
56 OSD_Environment env2("CSF_UnitsDefinition");
57 TCollection_AsciiString sunitsfile(env2.Value());
58 if( sunitsfile.Length() > 0 )
59 Units::UnitsFile(sunitsfile.ToCString());
61 OSD_Environment CasRootEnv("CASROOT");
62 TCollection_AsciiString CasRootString(CasRootEnv.Value());
63 if (CasRootString.Length() > 0 ) {
64 CasRootString += "/src/UnitsAPI/Units.dat";
65 Units::UnitsFile(CasRootString.ToCString());
68 Standard_NoSuchObject::Raise("environment variable CSF_UnitsDefinition undefined");
71 switch (aSystemUnits) {
72 case UnitsAPI_DEFAULT :
73 if( !CurrentUnits.IsNull() ) break;
75 currentSystem = UnitsAPI_SI;
76 if( SICurrentUnits.IsNull() ) {
78 OSD_Environment env3("CSF_CurrentUnits");
79 TCollection_AsciiString csfcurrent (env3.Value());
80 if( csfcurrent.Length() > 0 )
81 SICurrentUnits = new Resource_Manager(csfcurrent.ToCString());
83 SICurrentUnits = new Resource_Manager("CurrentUnits");
85 SICurrentUnits = new Resource_Manager("CurrentUnits");
88 CurrentUnits = SICurrentUnits;
89 LocalSystemUnits = SILocalSystemUnits;
92 currentSystem = UnitsAPI_MDTV;
93 if( MDTVCurrentUnits.IsNull() ) {
95 OSD_Environment env4("CSF_MDTVCurrentUnits");
96 TCollection_AsciiString csfmdtvcurrent (env4.Value());
97 if( csfmdtvcurrent.Length() > 0 )
98 MDTVCurrentUnits = new Resource_Manager(csfmdtvcurrent.ToCString());
100 MDTVCurrentUnits = new Resource_Manager("MDTVCurrentUnits");
102 MDTVCurrentUnits = new Resource_Manager("MDTVCurrentUnits");
105 CurrentUnits = MDTVCurrentUnits;
106 if( MDTVLocalSystemUnits.IsEmpty() ) {
107 MDTVLocalSystemUnits.Specify("LENGTH","mm");
108 MDTVLocalSystemUnits.Specify("AREA","mm\xB2");
109 MDTVLocalSystemUnits.Specify("VOLUME","mm\xB3");
110 MDTVLocalSystemUnits.Specify("INERTIA","mm**4");
111 MDTVLocalSystemUnits.Specify("SPEED","mm/s");
112 MDTVLocalSystemUnits.Specify("ACCELERATION","mm/s\xB2");
113 MDTVLocalSystemUnits.Specify("VOLUMIC MASS","kg/mm\xB3");
114 MDTVLocalSystemUnits.Specify("VOLUME FLOW","mm\xB3/s");
115 MDTVLocalSystemUnits.Specify("CONSUMPTION","mm\xB2");
116 MDTVLocalSystemUnits.Specify("QUANTITY OF MOVEMENT","kg*mm/s");
117 MDTVLocalSystemUnits.Specify("KINETIC MOMENT","kg*mm\xB2/s");
118 MDTVLocalSystemUnits.Specify("MOMENT OF INERTIA","kg*mm\xB2");
119 MDTVLocalSystemUnits.Specify("FORCE","kg*mm/s\xB2");
120 MDTVLocalSystemUnits.Specify("LINEIC FORCE","kg/s\xB2");
121 MDTVLocalSystemUnits.Specify("MOMENT OF A FORCE","kg*mm\xB2/s\xB2");
122 MDTVLocalSystemUnits.Specify("PRESSURE","kg/(mm*s\xB2)");
123 MDTVLocalSystemUnits.Specify("DYNAMIC VISCOSITY","kg/(mm*s)");
124 MDTVLocalSystemUnits.Specify("KINETIC VISCOSITY","mm\xB2/s");
125 MDTVLocalSystemUnits.Specify("TENSION SUPERFICIELLE","mm/s\xB2");
126 MDTVLocalSystemUnits.Specify("ENERGY","kg*mm\xB2/s\xB2");
127 MDTVLocalSystemUnits.Specify("POWER","kg*mm\xB2/s\xB3");
128 MDTVLocalSystemUnits.Specify("LINEIC POWER","kg*mm/s\xB3");
129 MDTVLocalSystemUnits.Specify("SURFACIC POWER","kg/s\xB3");
130 MDTVLocalSystemUnits.Specify("VOLUMIC POWER","kg/(mm*s\xB3)");
131 MDTVLocalSystemUnits.Specify("THERMICAL CONDUCTIVITY","kg*mm/(s\xB3*\xB0K)");
132 MDTVLocalSystemUnits.Specify("THERMICAL CONVECTIVITY","kg/(s\xB3*\xB0K)");
133 MDTVLocalSystemUnits.Specify("THERMICAL MASSIC CAPACITY","mm\xB2/(s\xB2*\xB0K)");
134 MDTVLocalSystemUnits.Specify("ENTROPY","kg*mm\xB2/(s\xB2*\xB0K)");
135 MDTVLocalSystemUnits.Specify("ENTHALPY","kg*mm\xB2/s\xB2");
136 MDTVLocalSystemUnits.Specify("LUMINANCE","cd/mm\xB2");
137 MDTVLocalSystemUnits.Specify("LUMINOUS EFFICACITY","s\xB3*Lu/(kg*mm\xB2)");
138 MDTVLocalSystemUnits.Specify("ELECTRIC FIELD","V/mm");
139 MDTVLocalSystemUnits.Specify("ELECTRIC CAPACITANCE","s**4*A\xB2/(kg*mm\xB2)");
140 MDTVLocalSystemUnits.Specify("MAGNETIC FIELD","A/mm");
141 MDTVLocalSystemUnits.Specify("MAGNETIC FLUX","kg*mm\xB2/(s\xB2*A)");
142 MDTVLocalSystemUnits.Specify("INDUCTANCE","kg*mm\xB2/(s\xB2*A\xB2)");
143 MDTVLocalSystemUnits.Specify("RELUCTANCE","s\xB2*A\xB2/(kg*mm\xB2)");
144 MDTVLocalSystemUnits.Specify("RESISTIVITY","O*mm");
145 MDTVLocalSystemUnits.Specify("CONDUCTIVITY","S/mm");
146 MDTVLocalSystemUnits.Specify("MOLAR MASS","kg/mol");
147 MDTVLocalSystemUnits.Specify("MOLAR VOLUME","mm\xB3/mol");
148 MDTVLocalSystemUnits.Specify("CONCENTRATION","kg/mm\xB3");
149 MDTVLocalSystemUnits.Specify("MOLAR CONCENTRATION","mol/mm\xB3");
150 MDTVLocalSystemUnits.Specify("ACCOUSTIC INTENSITY","mm/A\xB2");
151 MDTVLocalSystemUnits.Specify("DOSE EQUIVALENT","mm\xB2/s\xB2");
152 MDTVLocalSystemUnits.Specify("ABSORBED DOSE","mm\xB2/s\xB2");
153 MDTVLocalSystemUnits.Specify("FLUX OF MAGNETIC INDUCTION","kg*mm\xB2/(s\xB2*A)");
154 MDTVLocalSystemUnits.Specify("ROTATION ACCELERATION","rad/s\xB2");
155 MDTVLocalSystemUnits.Specify("TRANSLATION STIFFNESS","kg/s\xB2");
156 MDTVLocalSystemUnits.Specify("ROTATION STIFFNESS","kg*mm\xB2/(s\xB2*rad)");
157 MDTVLocalSystemUnits.Activates();
159 LocalSystemUnits = MDTVLocalSystemUnits;
166 //=======================================================================
167 //function : CurrentToLS
169 //=======================================================================
171 Standard_Real UnitsAPI::CurrentToLS(const Standard_Real aData,
172 const Standard_CString aQuantity)
174 Standard_Real aValue = aData;
175 CheckLoading (localSystem);
176 if( CurrentUnits->Find(aQuantity) ) {
177 TCollection_AsciiString current(CurrentUnits->Value(aQuantity));
178 aValue = Units::ToSI(aData,current.ToCString());
179 aValue = LocalSystemUnits.ConvertSIValueToUserSystem(aQuantity,aValue);
183 cout <<"Warning: UnitsAPI,the quantity '" << aQuantity << "' does not exist in the current units system" << endl;
191 //=======================================================================
192 //function : CurrentToSI
194 //=======================================================================
196 Standard_Real UnitsAPI::CurrentToSI(const Standard_Real aData,
197 const Standard_CString aQuantity)
199 Standard_Real aValue = aData;
200 CheckLoading (UnitsAPI_DEFAULT);
201 if( CurrentUnits->Find(aQuantity) ) {
202 TCollection_AsciiString current(CurrentUnits->Value(aQuantity));
203 aValue = Units::ToSI(aData,current.ToCString());
207 cout<<"Warning: UnitsAPI,the quantity '" << aQuantity << "' does not exist in the current units system" << endl;
215 //=======================================================================
216 //function : CurrentFromLS
218 //=======================================================================
220 Standard_Real UnitsAPI::CurrentFromLS(const Standard_Real aData,
221 const Standard_CString aQuantity)
223 Standard_Real aValue = aData;
224 CheckLoading (localSystem);
225 if( CurrentUnits->Find(aQuantity) ) {
226 TCollection_AsciiString current(CurrentUnits->Value(aQuantity));
227 aValue = LocalSystemUnits.ConvertUserSystemValueToSI(aQuantity,aData);
228 aValue = Units::FromSI(aValue,current.ToCString());
232 cout<<"Warning: UnitsAPI,the quantity '" << aQuantity << "' does not exist in the current units system" << endl;
240 //=======================================================================
241 //function : CurrentFromSI
243 //=======================================================================
245 Standard_Real UnitsAPI::CurrentFromSI(const Standard_Real aData,
246 const Standard_CString aQuantity)
248 Standard_Real aValue = aData;
249 CheckLoading (UnitsAPI_DEFAULT);
250 if( CurrentUnits->Find(aQuantity) ) {
251 TCollection_AsciiString current(CurrentUnits->Value(aQuantity));
252 aValue = Units::FromSI(aData,current.ToCString());
256 cout<<"Warning: UnitsAPI,the quantity '" << aQuantity << "' does not exist in the current units system" << endl;
264 //=======================================================================
265 //function : CurrentToAny
267 //=======================================================================
269 Standard_Real UnitsAPI::CurrentToAny(const Standard_Real aData,
270 const Standard_CString aQuantity,
271 const Standard_CString aUnit) {
272 Standard_Real aValue = aData;
273 CheckLoading (UnitsAPI_DEFAULT);
274 if( CurrentUnits->Find(aQuantity) ) {
275 TCollection_AsciiString current(CurrentUnits->Value(aQuantity));
276 aValue = AnyToAny(aData,current.ToCString(),aUnit);
280 cout<<"Warning: UnitsAPI,the quantity '" << aQuantity << "' does not exist in the current units system" << endl;
288 //=======================================================================
289 //function : CurrentFromAny
291 //=======================================================================
293 Standard_Real UnitsAPI::CurrentFromAny(const Standard_Real aData,
294 const Standard_CString aQuantity,
295 const Standard_CString aUnit)
297 Standard_Real aValue = aData;
298 CheckLoading (UnitsAPI_DEFAULT);
299 if( CurrentUnits->Find(aQuantity) ) {
300 TCollection_AsciiString current(CurrentUnits->Value(aQuantity));
301 aValue = AnyToAny(aData,aUnit,current.ToCString());
305 cout<<"Warning: UnitsAPI,the quantity '" << aQuantity << "' does not exist in the current units system" << endl;
313 //=======================================================================
316 //=======================================================================
318 Standard_Real UnitsAPI::AnyToLS(const Standard_Real aData,
319 const Standard_CString aUnit)
321 Standard_Real aValue = aData;
322 CheckLoading (localSystem);
323 Handle(Units_Dimensions) aDim;
324 aValue = Units::ToSI(aValue,aUnit,aDim);
327 Standard_CString quantity = aDim->Quantity();
329 aValue = LocalSystemUnits.ConvertSIValueToUserSystem(quantity,aValue);
333 cout<<"Warning: BAD Quantity returns in UnitsAPI::AnyToLS(" << aData << "," << aUnit << ")" << endl;
339 //=======================================================================
342 //=======================================================================
344 Standard_Real UnitsAPI::AnyToLS(const Standard_Real aData,
345 const Standard_CString aUnit,
346 Handle(Units_Dimensions) &aDim)
348 Standard_Real aValue = aData;
349 CheckLoading (localSystem);
350 aValue = Units::ToSI(aValue,aUnit,aDim);
351 Standard_CString quantity = aDim->Quantity();
355 aValue = LocalSystemUnits.ConvertSIValueToUserSystem(quantity,aValue);
359 cout<<"Warning: BAD Quantity returns in UnitsAPI::AnyToLS(" << aData << "," << aUnit << "," << aDim.get() << ")" << endl;
365 //=======================================================================
368 //=======================================================================
370 Standard_Real UnitsAPI::AnyToSI(const Standard_Real aData,
371 const Standard_CString aUnit)
373 Standard_Real aValue;
374 CheckLoading (UnitsAPI_DEFAULT);
375 aValue = Units::ToSI(aData,aUnit);
380 //=======================================================================
383 //=======================================================================
385 Standard_Real UnitsAPI::AnyToSI(const Standard_Real aData,
386 const Standard_CString aUnit,
387 Handle(Units_Dimensions) &aDim)
389 Standard_Real aValue;
390 CheckLoading (UnitsAPI_DEFAULT);
391 aValue = Units::ToSI(aData,aUnit,aDim);
396 //=======================================================================
397 //function : AnyFromLS
399 //=======================================================================
401 Standard_Real UnitsAPI::AnyFromLS(const Standard_Real aData,
402 const Standard_CString aUnit)
404 Standard_Real aValue = aData;
405 CheckLoading (localSystem);
406 Handle(Units_Dimensions) aDim;
407 aValue = Units::FromSI(aValue,aUnit,aDim);
408 Standard_CString quantity = aDim->Quantity();
410 aValue = LocalSystemUnits.ConvertUserSystemValueToSI(quantity,aValue);
414 cout<<"Warning: BAD Quantity returns in UnitsAPI::AnyToLS(" << aData << "," << aUnit << ")" << endl;
421 //=======================================================================
422 //function : AnyFromSI
424 //=======================================================================
426 Standard_Real UnitsAPI::AnyFromSI(const Standard_Real aData,
427 const Standard_CString aUnit)
429 Standard_Real aValue;
430 CheckLoading (UnitsAPI_DEFAULT);
431 aValue = Units::FromSI(aData,aUnit);
436 //=======================================================================
437 //function : AnyToAny
439 //=======================================================================
441 Standard_Real UnitsAPI::AnyToAny(const Standard_Real aData,
442 const Standard_CString aUnit1,
443 const Standard_CString aUnit2)
445 Standard_Real aValue = aData;
446 CheckLoading (UnitsAPI_DEFAULT);
447 aValue = Units::Convert(aValue,aUnit1,aUnit2);
452 //=======================================================================
455 //=======================================================================
457 Standard_Real UnitsAPI::LSToSI(const Standard_Real aData,
458 const Standard_CString aQuantity)
460 Standard_Real aValue = aData;
461 CheckLoading (localSystem);
462 if( CurrentUnits->Find(aQuantity) ) {
463 aValue = LocalSystemUnits.ConvertUserSystemValueToSI(aQuantity,aData);
467 cout<<"Warning: UnitsAPI,the quantity '" << aQuantity << "' does not exist in the current units system" << endl;
475 //=======================================================================
478 //=======================================================================
480 Standard_Real UnitsAPI::SIToLS(const Standard_Real aData,
481 const Standard_CString aQuantity)
483 Standard_Real aValue = aData;
484 CheckLoading (localSystem);
485 if( CurrentUnits->Find(aQuantity) ) {
486 aValue = LocalSystemUnits.ConvertSIValueToUserSystem(aQuantity,aValue);
490 cout<<"Warning: UnitsAPI,the quantity '" << aQuantity << "' does not exist in the current units system" << endl;
498 //=======================================================================
499 //function : SetLocalSystem
501 //=======================================================================
503 void UnitsAPI::SetLocalSystem(const UnitsAPI_SystemUnits aSystemUnits)
505 CheckLoading (aSystemUnits);
506 localSystem = currentSystem;
510 //=======================================================================
511 //function : LocalSystem
513 //=======================================================================
515 UnitsAPI_SystemUnits UnitsAPI::LocalSystem()
521 //=======================================================================
522 //function : SetCurrentUnit
524 //=======================================================================
526 void UnitsAPI::SetCurrentUnit(const Standard_CString aQuantity,
527 const Standard_CString anUnit)
529 CheckLoading(localSystem);
530 CurrentUnits->SetResource(aQuantity,anUnit);
534 //=======================================================================
537 //=======================================================================
539 void UnitsAPI::Save()
541 CheckLoading(localSystem);
542 CurrentUnits->Save();
546 //=======================================================================
549 //=======================================================================
551 void UnitsAPI::Reload()
553 currentSystem = UnitsAPI_DEFAULT;
554 CheckLoading(localSystem);
558 //=======================================================================
559 //function : CurrentUnit
561 //=======================================================================
563 static TCollection_AsciiString astring;
564 Standard_CString UnitsAPI::CurrentUnit(const Standard_CString aQuantity)
566 CheckLoading(localSystem);
567 astring = CurrentUnits->Value(aQuantity);
568 return astring.ToCString();
572 //=======================================================================
573 //function : Dimensions
575 //=======================================================================
577 Handle(Units_Dimensions) UnitsAPI::Dimensions(const Standard_CString aType)
579 return Units::Dimensions(aType);
583 //=======================================================================
584 //function : DimensionLess
586 //=======================================================================
588 Handle(Units_Dimensions) UnitsAPI::DimensionLess()
590 return Units_Dimensions::ALess();
594 //=======================================================================
595 //function : DimensionMass
597 //=======================================================================
599 Handle(Units_Dimensions) UnitsAPI::DimensionMass()
601 return Units_Dimensions::AMass();
605 //=======================================================================
606 //function : DimensionLength
608 //=======================================================================
610 Handle(Units_Dimensions) UnitsAPI::DimensionLength()
612 return Units_Dimensions::ALength();
616 //=======================================================================
617 //function : DimensionTime
619 //=======================================================================
621 Handle(Units_Dimensions) UnitsAPI::DimensionTime()
623 return Units_Dimensions::ATime() ;
627 //=======================================================================
628 //function : DimensionElectricCurrent
630 //=======================================================================
632 Handle(Units_Dimensions) UnitsAPI::DimensionElectricCurrent()
634 return Units_Dimensions::AElectricCurrent() ;
638 //=======================================================================
639 //function : DimensionThermodynamicTemperature
641 //=======================================================================
643 Handle(Units_Dimensions) UnitsAPI::DimensionThermodynamicTemperature()
645 return Units_Dimensions::AThermodynamicTemperature();
649 //=======================================================================
650 //function : DimensionAmountOfSubstance
652 //=======================================================================
654 Handle(Units_Dimensions) UnitsAPI::DimensionAmountOfSubstance()
656 return Units_Dimensions::AAmountOfSubstance();
660 //=======================================================================
661 //function : DimensionLuminousIntensity
663 //=======================================================================
665 Handle(Units_Dimensions) UnitsAPI::DimensionLuminousIntensity()
667 return Units_Dimensions::ALuminousIntensity();
671 //=======================================================================
672 //function : DimensionPlaneAngle
674 //=======================================================================
676 Handle(Units_Dimensions) UnitsAPI::DimensionPlaneAngle()
678 return Units_Dimensions::APlaneAngle();
682 //=======================================================================
683 //function : DimensionSolidAngle
685 //=======================================================================
687 Handle(Units_Dimensions) UnitsAPI::DimensionSolidAngle()
689 return Units_Dimensions::ASolidAngle();
693 //=======================================================================
696 //=======================================================================
698 Standard_Boolean UnitsAPI::Check(const Standard_CString aQuantity,
699 const Standard_CString /*aUnit*/)
701 Standard_Boolean status = Standard_False;
702 CheckLoading (UnitsAPI_DEFAULT);
703 if( CurrentUnits->Find(aQuantity) ) {
704 TCollection_AsciiString current(CurrentUnits->Value(aQuantity));
705 // aValue = AnyToAny(aData,current.ToCString(),aUnit);
706 // aValue = Units::Convert(aValue,aUnit1,aUnit2);