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.
15 #include <UnitsAPI.ixx>
17 #include <Units_Unit.hxx>
18 #include <Units_Quantity.hxx>
19 #include <Units_UnitsSystem.hxx>
20 #include <Resource_Manager.hxx>
21 #include <TCollection_AsciiString.hxx>
22 #include <Standard_NoSuchObject.hxx>
23 #include <OSD_Environment.hxx>
25 static Handle(Resource_Manager) CurrentUnits,SICurrentUnits,MDTVCurrentUnits;
26 static Units_UnitsSystem LocalSystemUnits,SILocalSystemUnits,MDTVLocalSystemUnits;
27 static TCollection_AsciiString rstring;
28 static UnitsAPI_SystemUnits localSystem = UnitsAPI_SI;
29 static UnitsAPI_SystemUnits currentSystem = UnitsAPI_DEFAULT;
30 static OSD_Environment env1("CSF_UnitsLexicon");
31 static OSD_Environment env2("CSF_UnitsDefinition");
33 static OSD_Environment env3("CSF_CurrentUnits");
34 static OSD_Environment env4("CSF_MDTVCurrentUnits");
38 //=======================================================================
39 //function : CheckLoading
41 //=======================================================================
43 void UnitsAPI::CheckLoading (const UnitsAPI_SystemUnits aSystemUnits)
45 if( currentSystem != aSystemUnits || CurrentUnits.IsNull()) {
46 TCollection_AsciiString slexiconfile(env1.Value());
47 if( slexiconfile.Length() > 0 )
48 Units::LexiconFile(slexiconfile.ToCString());
50 OSD_Environment CasRootEnv("CASROOT");
51 TCollection_AsciiString CasRootString(CasRootEnv.Value());
52 if (CasRootString.Length() > 0 ) {
53 CasRootString += "/src/UnitsAPI/Lexi_Expr.dat" ;
54 Units::LexiconFile(CasRootString.ToCString());
57 Standard_NoSuchObject::Raise("environment variable CSF_UnitsLexicon undefined");
60 TCollection_AsciiString sunitsfile(env2.Value());
61 if( sunitsfile.Length() > 0 )
62 Units::UnitsFile(sunitsfile.ToCString());
64 OSD_Environment CasRootEnv("CASROOT");
65 TCollection_AsciiString CasRootString(CasRootEnv.Value());
66 if (CasRootString.Length() > 0 ) {
67 CasRootString += "/src/UnitsAPI/Units.dat";
68 Units::UnitsFile(CasRootString.ToCString());
71 Standard_NoSuchObject::Raise("environment variable CSF_UnitsDefinition undefined");
74 switch (aSystemUnits) {
75 case UnitsAPI_DEFAULT :
76 if( !CurrentUnits.IsNull() ) break;
78 currentSystem = UnitsAPI_SI;
79 if( SICurrentUnits.IsNull() ) {
81 TCollection_AsciiString csfcurrent (env3.Value());
82 if( csfcurrent.Length() > 0 )
83 SICurrentUnits = new Resource_Manager(csfcurrent.ToCString());
85 SICurrentUnits = new Resource_Manager("CurrentUnits");
87 SICurrentUnits = new Resource_Manager("CurrentUnits");
90 CurrentUnits = SICurrentUnits;
91 LocalSystemUnits = SILocalSystemUnits;
94 currentSystem = UnitsAPI_MDTV;
95 if( MDTVCurrentUnits.IsNull() ) {
97 TCollection_AsciiString csfmdtvcurrent (env4.Value());
98 if( csfmdtvcurrent.Length() > 0 )
99 MDTVCurrentUnits = new Resource_Manager(csfmdtvcurrent.ToCString());
101 MDTVCurrentUnits = new Resource_Manager("MDTVCurrentUnits");
103 MDTVCurrentUnits = new Resource_Manager("MDTVCurrentUnits");
106 CurrentUnits = MDTVCurrentUnits;
107 if( MDTVLocalSystemUnits.IsEmpty() ) {
108 MDTVLocalSystemUnits.Specify("LENGTH","mm");
109 MDTVLocalSystemUnits.Specify("AREA","mm\xB2");
110 MDTVLocalSystemUnits.Specify("VOLUME","mm\xB3");
111 MDTVLocalSystemUnits.Specify("INERTIA","mm**4");
112 MDTVLocalSystemUnits.Specify("SPEED","mm/s");
113 MDTVLocalSystemUnits.Specify("ACCELERATION","mm/s\xB2");
114 MDTVLocalSystemUnits.Specify("VOLUMIC MASS","kg/mm\xB3");
115 MDTVLocalSystemUnits.Specify("VOLUME FLOW","mm\xB3/s");
116 MDTVLocalSystemUnits.Specify("CONSUMPTION","mm\xB2");
117 MDTVLocalSystemUnits.Specify("QUANTITY OF MOVEMENT","kg*mm/s");
118 MDTVLocalSystemUnits.Specify("KINETIC MOMENT","kg*mm\xB2/s");
119 MDTVLocalSystemUnits.Specify("MOMENT OF INERTIA","kg*mm\xB2");
120 MDTVLocalSystemUnits.Specify("FORCE","kg*mm/s\xB2");
121 MDTVLocalSystemUnits.Specify("LINEIC FORCE","kg/s\xB2");
122 MDTVLocalSystemUnits.Specify("MOMENT OF A FORCE","kg*mm\xB2/s\xB2");
123 MDTVLocalSystemUnits.Specify("PRESSURE","kg/(mm*s\xB2)");
124 MDTVLocalSystemUnits.Specify("DYNAMIC VISCOSITY","kg/(mm*s)");
125 MDTVLocalSystemUnits.Specify("KINETIC VISCOSITY","mm\xB2/s");
126 MDTVLocalSystemUnits.Specify("TENSION SUPERFICIELLE","mm/s\xB2");
127 MDTVLocalSystemUnits.Specify("ENERGY","kg*mm\xB2/s\xB2");
128 MDTVLocalSystemUnits.Specify("POWER","kg*mm\xB2/s\xB3");
129 MDTVLocalSystemUnits.Specify("LINEIC POWER","kg*mm/s\xB3");
130 MDTVLocalSystemUnits.Specify("SURFACIC POWER","kg/s\xB3");
131 MDTVLocalSystemUnits.Specify("VOLUMIC POWER","kg/(mm*s\xB3)");
132 MDTVLocalSystemUnits.Specify("THERMICAL CONDUCTIVITY","kg*mm/(s\xB3*\xB0K)");
133 MDTVLocalSystemUnits.Specify("THERMICAL CONVECTIVITY","kg/(s\xB3*\xB0K)");
134 MDTVLocalSystemUnits.Specify("THERMICAL MASSIC CAPACITY","mm\xB2/(s\xB2*\xB0K)");
135 MDTVLocalSystemUnits.Specify("ENTROPY","kg*mm\xB2/(s\xB2*\xB0K)");
136 MDTVLocalSystemUnits.Specify("ENTHALPY","kg*mm\xB2/s\xB2");
137 MDTVLocalSystemUnits.Specify("LUMINANCE","cd/mm\xB2");
138 MDTVLocalSystemUnits.Specify("LUMINOUS EFFICACITY","s\xB3*Lu/(kg*mm\xB2)");
139 MDTVLocalSystemUnits.Specify("ELECTRIC FIELD","V/mm");
140 MDTVLocalSystemUnits.Specify("ELECTRIC CAPACITANCE","s**4*A\xB2/(kg*mm\xB2)");
141 MDTVLocalSystemUnits.Specify("MAGNETIC FIELD","A/mm");
142 MDTVLocalSystemUnits.Specify("MAGNETIC FLUX","kg*mm\xB2/(s\xB2*A)");
143 MDTVLocalSystemUnits.Specify("INDUCTANCE","kg*mm\xB2/(s\xB2*A\xB2)");
144 MDTVLocalSystemUnits.Specify("RELUCTANCE","s\xB2*A\xB2/(kg*mm\xB2)");
145 MDTVLocalSystemUnits.Specify("RESISTIVITY","O*mm");
146 MDTVLocalSystemUnits.Specify("CONDUCTIVITY","S/mm");
147 MDTVLocalSystemUnits.Specify("MOLAR MASS","kg/mol");
148 MDTVLocalSystemUnits.Specify("MOLAR VOLUME","mm\xB3/mol");
149 MDTVLocalSystemUnits.Specify("CONCENTRATION","kg/mm\xB3");
150 MDTVLocalSystemUnits.Specify("MOLAR CONCENTRATION","mol/mm\xB3");
151 MDTVLocalSystemUnits.Specify("ACCOUSTIC INTENSITY","mm/A\xB2");
152 MDTVLocalSystemUnits.Specify("DOSE EQUIVALENT","mm\xB2/s\xB2");
153 MDTVLocalSystemUnits.Specify("ABSORBED DOSE","mm\xB2/s\xB2");
154 MDTVLocalSystemUnits.Specify("FLUX OF MAGNETIC INDUCTION","kg*mm\xB2/(s\xB2*A)");
155 MDTVLocalSystemUnits.Specify("ROTATION ACCELERATION","rad/s\xB2");
156 MDTVLocalSystemUnits.Specify("TRANSLATION STIFFNESS","kg/s\xB2");
157 MDTVLocalSystemUnits.Specify("ROTATION STIFFNESS","kg*mm\xB2/(s\xB2*rad)");
158 MDTVLocalSystemUnits.Activates();
160 LocalSystemUnits = MDTVLocalSystemUnits;
167 //=======================================================================
168 //function : CurrentToLS
170 //=======================================================================
172 Standard_Real UnitsAPI::CurrentToLS(const Standard_Real aData,
173 const Standard_CString aQuantity)
175 Standard_Real aValue = aData;
176 CheckLoading (localSystem);
177 if( CurrentUnits->Find(aQuantity) ) {
178 TCollection_AsciiString current(CurrentUnits->Value(aQuantity));
179 aValue = Units::ToSI(aData,current.ToCString());
180 aValue = LocalSystemUnits.ConvertSIValueToUserSystem(aQuantity,aValue);
183 cout <<"Warning: UnitsAPI,the quantity '" << aQuantity << "' does not exist in the current units system" << endl;
190 //=======================================================================
191 //function : CurrentToSI
193 //=======================================================================
195 Standard_Real UnitsAPI::CurrentToSI(const Standard_Real aData,
196 const Standard_CString aQuantity)
198 Standard_Real aValue = aData;
199 CheckLoading (UnitsAPI_DEFAULT);
200 if( CurrentUnits->Find(aQuantity) ) {
201 TCollection_AsciiString current(CurrentUnits->Value(aQuantity));
202 aValue = Units::ToSI(aData,current.ToCString());
205 cout<<"Warning: UnitsAPI,the quantity '" << aQuantity << "' does not exist in the current units system" << endl;
212 //=======================================================================
213 //function : CurrentFromLS
215 //=======================================================================
217 Standard_Real UnitsAPI::CurrentFromLS(const Standard_Real aData,
218 const Standard_CString aQuantity)
220 Standard_Real aValue = aData;
221 CheckLoading (localSystem);
222 if( CurrentUnits->Find(aQuantity) ) {
223 TCollection_AsciiString current(CurrentUnits->Value(aQuantity));
224 aValue = LocalSystemUnits.ConvertUserSystemValueToSI(aQuantity,aData);
225 aValue = Units::FromSI(aValue,current.ToCString());
228 cout<<"Warning: UnitsAPI,the quantity '" << aQuantity << "' does not exist in the current units system" << endl;
235 //=======================================================================
236 //function : CurrentFromSI
238 //=======================================================================
240 Standard_Real UnitsAPI::CurrentFromSI(const Standard_Real aData,
241 const Standard_CString aQuantity)
243 Standard_Real aValue = aData;
244 CheckLoading (UnitsAPI_DEFAULT);
245 if( CurrentUnits->Find(aQuantity) ) {
246 TCollection_AsciiString current(CurrentUnits->Value(aQuantity));
247 aValue = Units::FromSI(aData,current.ToCString());
250 cout<<"Warning: UnitsAPI,the quantity '" << aQuantity << "' does not exist in the current units system" << endl;
257 //=======================================================================
258 //function : CurrentToAny
260 //=======================================================================
262 Standard_Real UnitsAPI::CurrentToAny(const Standard_Real aData,
263 const Standard_CString aQuantity,
264 const Standard_CString aUnit) {
265 Standard_Real aValue = aData;
266 CheckLoading (UnitsAPI_DEFAULT);
267 if( CurrentUnits->Find(aQuantity) ) {
268 TCollection_AsciiString current(CurrentUnits->Value(aQuantity));
269 aValue = AnyToAny(aData,current.ToCString(),aUnit);
272 cout<<"Warning: UnitsAPI,the quantity '" << aQuantity << "' does not exist in the current units system" << endl;
279 //=======================================================================
280 //function : CurrentFromAny
282 //=======================================================================
284 Standard_Real UnitsAPI::CurrentFromAny(const Standard_Real aData,
285 const Standard_CString aQuantity,
286 const Standard_CString aUnit)
288 Standard_Real aValue = aData;
289 CheckLoading (UnitsAPI_DEFAULT);
290 if( CurrentUnits->Find(aQuantity) ) {
291 TCollection_AsciiString current(CurrentUnits->Value(aQuantity));
292 aValue = AnyToAny(aData,aUnit,current.ToCString());
295 cout<<"Warning: UnitsAPI,the quantity '" << aQuantity << "' does not exist in the current units system" << endl;
302 //=======================================================================
305 //=======================================================================
307 Standard_Real UnitsAPI::AnyToLS(const Standard_Real aData,
308 const Standard_CString aUnit)
310 Standard_Real aValue = aData;
311 CheckLoading (localSystem);
312 Handle(Units_Dimensions) aDim;
313 aValue = Units::ToSI(aValue,aUnit,aDim);
316 Standard_CString quantity = aDim->Quantity();
318 aValue = LocalSystemUnits.ConvertSIValueToUserSystem(quantity,aValue);
321 cout<<"Warning: BAD Quantity returns in UnitsAPI::AnyToLS(" << aData << "," << aUnit << ")" << endl;
326 //=======================================================================
329 //=======================================================================
331 Standard_Real UnitsAPI::AnyToLS(const Standard_Real aData,
332 const Standard_CString aUnit,
333 Handle(Units_Dimensions) &aDim)
335 Standard_Real aValue = aData;
336 CheckLoading (localSystem);
337 aValue = Units::ToSI(aValue,aUnit,aDim);
338 Standard_CString quantity = aDim->Quantity();
342 aValue = LocalSystemUnits.ConvertSIValueToUserSystem(quantity,aValue);
345 cout<<"Warning: BAD Quantity returns in UnitsAPI::AnyToLS(" << aData << "," << aUnit << "," << aDim << ")" << endl;
350 //=======================================================================
353 //=======================================================================
355 Standard_Real UnitsAPI::AnyToSI(const Standard_Real aData,
356 const Standard_CString aUnit)
358 Standard_Real aValue;
359 CheckLoading (UnitsAPI_DEFAULT);
360 aValue = Units::ToSI(aData,aUnit);
365 //=======================================================================
368 //=======================================================================
370 Standard_Real UnitsAPI::AnyToSI(const Standard_Real aData,
371 const Standard_CString aUnit,
372 Handle(Units_Dimensions) &aDim)
374 Standard_Real aValue;
375 CheckLoading (UnitsAPI_DEFAULT);
376 aValue = Units::ToSI(aData,aUnit,aDim);
381 //=======================================================================
382 //function : AnyFromLS
384 //=======================================================================
386 Standard_Real UnitsAPI::AnyFromLS(const Standard_Real aData,
387 const Standard_CString aUnit)
389 Standard_Real aValue = aData;
390 CheckLoading (localSystem);
391 Handle(Units_Dimensions) aDim;
392 aValue = Units::FromSI(aValue,aUnit,aDim);
393 Standard_CString quantity = aDim->Quantity();
395 aValue = LocalSystemUnits.ConvertUserSystemValueToSI(quantity,aValue);
398 cout<<"Warning: BAD Quantity returns in UnitsAPI::AnyToLS(" << aData << "," << aUnit << ")" << endl;
404 //=======================================================================
405 //function : AnyFromSI
407 //=======================================================================
409 Standard_Real UnitsAPI::AnyFromSI(const Standard_Real aData,
410 const Standard_CString aUnit)
412 Standard_Real aValue;
413 CheckLoading (UnitsAPI_DEFAULT);
414 aValue = Units::FromSI(aData,aUnit);
419 //=======================================================================
420 //function : AnyToAny
422 //=======================================================================
424 Standard_Real UnitsAPI::AnyToAny(const Standard_Real aData,
425 const Standard_CString aUnit1,
426 const Standard_CString aUnit2)
428 Standard_Real aValue = aData;
429 CheckLoading (UnitsAPI_DEFAULT);
430 aValue = Units::Convert(aValue,aUnit1,aUnit2);
435 //=======================================================================
438 //=======================================================================
440 Standard_Real UnitsAPI::LSToSI(const Standard_Real aData,
441 const Standard_CString aQuantity)
443 Standard_Real aValue = aData;
444 CheckLoading (localSystem);
445 if( CurrentUnits->Find(aQuantity) ) {
446 aValue = LocalSystemUnits.ConvertUserSystemValueToSI(aQuantity,aData);
449 cout<<"Warning: UnitsAPI,the quantity '" << aQuantity << "' does not exist in the current units system" << endl;
456 //=======================================================================
459 //=======================================================================
461 Standard_Real UnitsAPI::SIToLS(const Standard_Real aData,
462 const Standard_CString aQuantity)
464 Standard_Real aValue = aData;
465 CheckLoading (localSystem);
466 if( CurrentUnits->Find(aQuantity) ) {
467 aValue = LocalSystemUnits.ConvertSIValueToUserSystem(aQuantity,aValue);
470 cout<<"Warning: UnitsAPI,the quantity '" << aQuantity << "' does not exist in the current units system" << endl;
477 //=======================================================================
478 //function : SetLocalSystem
480 //=======================================================================
482 void UnitsAPI::SetLocalSystem(const UnitsAPI_SystemUnits aSystemUnits)
484 CheckLoading (aSystemUnits);
485 localSystem = currentSystem;
489 //=======================================================================
490 //function : LocalSystem
492 //=======================================================================
494 UnitsAPI_SystemUnits UnitsAPI::LocalSystem()
500 //=======================================================================
501 //function : SetCurrentUnit
503 //=======================================================================
505 void UnitsAPI::SetCurrentUnit(const Standard_CString aQuantity,
506 const Standard_CString anUnit)
508 CheckLoading(localSystem);
509 CurrentUnits->SetResource(aQuantity,anUnit);
513 //=======================================================================
516 //=======================================================================
518 void UnitsAPI::Save()
520 CheckLoading(localSystem);
521 CurrentUnits->Save();
525 //=======================================================================
528 //=======================================================================
530 void UnitsAPI::Reload()
532 currentSystem = UnitsAPI_DEFAULT;
533 CheckLoading(localSystem);
537 //=======================================================================
538 //function : CurrentUnit
540 //=======================================================================
542 static TCollection_AsciiString astring;
543 Standard_CString UnitsAPI::CurrentUnit(const Standard_CString aQuantity)
545 CheckLoading(localSystem);
546 astring = CurrentUnits->Value(aQuantity);
547 return astring.ToCString();
551 //=======================================================================
552 //function : Dimensions
554 //=======================================================================
556 Handle(Units_Dimensions) UnitsAPI::Dimensions(const Standard_CString aType)
558 return Units::Dimensions(aType);
562 //=======================================================================
563 //function : DimensionLess
565 //=======================================================================
567 Handle(Units_Dimensions) UnitsAPI::DimensionLess()
569 return Units_Dimensions::ALess();
573 //=======================================================================
574 //function : DimensionMass
576 //=======================================================================
578 Handle(Units_Dimensions) UnitsAPI::DimensionMass()
580 return Units_Dimensions::AMass();
584 //=======================================================================
585 //function : DimensionLength
587 //=======================================================================
589 Handle(Units_Dimensions) UnitsAPI::DimensionLength()
591 return Units_Dimensions::ALength();
595 //=======================================================================
596 //function : DimensionTime
598 //=======================================================================
600 Handle(Units_Dimensions) UnitsAPI::DimensionTime()
602 return Units_Dimensions::ATime() ;
606 //=======================================================================
607 //function : DimensionElectricCurrent
609 //=======================================================================
611 Handle(Units_Dimensions) UnitsAPI::DimensionElectricCurrent()
613 return Units_Dimensions::AElectricCurrent() ;
617 //=======================================================================
618 //function : DimensionThermodynamicTemperature
620 //=======================================================================
622 Handle(Units_Dimensions) UnitsAPI::DimensionThermodynamicTemperature()
624 return Units_Dimensions::AThermodynamicTemperature();
628 //=======================================================================
629 //function : DimensionAmountOfSubstance
631 //=======================================================================
633 Handle(Units_Dimensions) UnitsAPI::DimensionAmountOfSubstance()
635 return Units_Dimensions::AAmountOfSubstance();
639 //=======================================================================
640 //function : DimensionLuminousIntensity
642 //=======================================================================
644 Handle(Units_Dimensions) UnitsAPI::DimensionLuminousIntensity()
646 return Units_Dimensions::ALuminousIntensity();
650 //=======================================================================
651 //function : DimensionPlaneAngle
653 //=======================================================================
655 Handle(Units_Dimensions) UnitsAPI::DimensionPlaneAngle()
657 return Units_Dimensions::APlaneAngle();
661 //=======================================================================
662 //function : DimensionSolidAngle
664 //=======================================================================
666 Handle(Units_Dimensions) UnitsAPI::DimensionSolidAngle()
668 return Units_Dimensions::ASolidAngle();
672 //=======================================================================
675 //=======================================================================
677 Standard_Boolean UnitsAPI::Check(const Standard_CString aQuantity,
678 const Standard_CString /*aUnit*/)
680 Standard_Boolean status = Standard_False;
681 CheckLoading (UnitsAPI_DEFAULT);
682 if( CurrentUnits->Find(aQuantity) ) {
683 TCollection_AsciiString current(CurrentUnits->Value(aQuantity));
684 // aValue = AnyToAny(aData,current.ToCString(),aUnit);
685 // aValue = Units::Convert(aValue,aUnit1,aUnit2);