1 // Copyright (c) 1998-1999 Matra Datavision
2 // Copyright (c) 1999-2012 OPEN CASCADE SAS
4 // The content of this file is subject to the Open CASCADE Technology Public
5 // License Version 6.5 (the "License"). You may not use the content of this file
6 // except in compliance with the License. Please obtain a copy of the License
7 // at http://www.opencascade.org and read it completely before using this file.
9 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
10 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
12 // The Original Code and all software distributed under the License is
13 // distributed on an "AS IS" basis, without warranty of any kind, and the
14 // Initial Developer hereby disclaims all such warranties, including without
15 // limitation, any warranties of merchantability, fitness for a particular
16 // purpose or non-infringement. Please see the License for the specific terms
17 // and conditions governing the rights and limitations under the License.
19 #include <UnitsAPI.ixx>
21 #include <Units_Unit.hxx>
22 #include <Units_Quantity.hxx>
23 #include <Units_UnitsSystem.hxx>
24 #include <Resource_Manager.hxx>
25 #include <TCollection_AsciiString.hxx>
26 #include <Standard_NoSuchObject.hxx>
27 #include <OSD_Environment.hxx>
29 static Handle(Resource_Manager) CurrentUnits,SICurrentUnits,MDTVCurrentUnits;
30 static Units_UnitsSystem LocalSystemUnits,SILocalSystemUnits,MDTVLocalSystemUnits;
31 static TCollection_AsciiString rstring;
32 static UnitsAPI_SystemUnits localSystem = UnitsAPI_SI;
33 static UnitsAPI_SystemUnits currentSystem = UnitsAPI_DEFAULT;
34 static OSD_Environment env1("CSF_UnitsLexicon");
35 static OSD_Environment env2("CSF_UnitsDefinition");
37 static OSD_Environment env3("CSF_CurrentUnits");
38 static OSD_Environment env4("CSF_MDTVCurrentUnits");
42 //=======================================================================
43 //function : CheckLoading
45 //=======================================================================
47 void UnitsAPI::CheckLoading (const UnitsAPI_SystemUnits aSystemUnits)
49 if( currentSystem != aSystemUnits || CurrentUnits.IsNull()) {
50 TCollection_AsciiString slexiconfile(env1.Value());
51 if( slexiconfile.Length() > 0 )
52 Units::LexiconFile(slexiconfile.ToCString());
54 OSD_Environment CasRootEnv("CASROOT");
55 TCollection_AsciiString CasRootString(CasRootEnv.Value());
56 if (CasRootString.Length() > 0 ) {
57 CasRootString += "/src/UnitsAPI/Lexi_Expr.dat" ;
58 Units::LexiconFile(CasRootString.ToCString());
61 Standard_NoSuchObject::Raise("environment variable CSF_UnitsLexicon undefined");
64 TCollection_AsciiString sunitsfile(env2.Value());
65 if( sunitsfile.Length() > 0 )
66 Units::UnitsFile(sunitsfile.ToCString());
68 OSD_Environment CasRootEnv("CASROOT");
69 TCollection_AsciiString CasRootString(CasRootEnv.Value());
70 if (CasRootString.Length() > 0 ) {
71 CasRootString += "/src/UnitsAPI/Units.dat";
72 Units::UnitsFile(CasRootString.ToCString());
75 Standard_NoSuchObject::Raise("environment variable CSF_UnitsDefinition undefined");
78 switch (aSystemUnits) {
79 case UnitsAPI_DEFAULT :
80 if( !CurrentUnits.IsNull() ) break;
82 currentSystem = UnitsAPI_SI;
83 if( SICurrentUnits.IsNull() ) {
85 TCollection_AsciiString csfcurrent (env3.Value());
86 if( csfcurrent.Length() > 0 )
87 SICurrentUnits = new Resource_Manager(csfcurrent.ToCString());
89 SICurrentUnits = new Resource_Manager("CurrentUnits");
91 SICurrentUnits = new Resource_Manager("CurrentUnits");
94 CurrentUnits = SICurrentUnits;
95 LocalSystemUnits = SILocalSystemUnits;
98 currentSystem = UnitsAPI_MDTV;
99 if( MDTVCurrentUnits.IsNull() ) {
101 TCollection_AsciiString csfmdtvcurrent (env4.Value());
102 if( csfmdtvcurrent.Length() > 0 )
103 MDTVCurrentUnits = new Resource_Manager(csfmdtvcurrent.ToCString());
105 MDTVCurrentUnits = new Resource_Manager("MDTVCurrentUnits");
107 MDTVCurrentUnits = new Resource_Manager("MDTVCurrentUnits");
110 CurrentUnits = MDTVCurrentUnits;
111 if( MDTVLocalSystemUnits.IsEmpty() ) {
112 MDTVLocalSystemUnits.Specify("LENGTH","mm");
113 MDTVLocalSystemUnits.Specify("AREA","mm\xB2");
114 MDTVLocalSystemUnits.Specify("VOLUME","mm\xB3");
115 MDTVLocalSystemUnits.Specify("INERTIA","mm**4");
116 MDTVLocalSystemUnits.Specify("SPEED","mm/s");
117 MDTVLocalSystemUnits.Specify("ACCELERATION","mm/s\xB2");
118 MDTVLocalSystemUnits.Specify("VOLUMIC MASS","kg/mm\xB3");
119 MDTVLocalSystemUnits.Specify("VOLUME FLOW","mm\xB3/s");
120 MDTVLocalSystemUnits.Specify("CONSUMPTION","mm\xB2");
121 MDTVLocalSystemUnits.Specify("QUANTITY OF MOVEMENT","kg*mm/s");
122 MDTVLocalSystemUnits.Specify("KINETIC MOMENT","kg*mm\xB2/s");
123 MDTVLocalSystemUnits.Specify("MOMENT OF INERTIA","kg*mm\xB2");
124 MDTVLocalSystemUnits.Specify("FORCE","kg*mm/s\xB2");
125 MDTVLocalSystemUnits.Specify("LINEIC FORCE","kg/s\xB2");
126 MDTVLocalSystemUnits.Specify("MOMENT OF A FORCE","kg*mm\xB2/s\xB2");
127 MDTVLocalSystemUnits.Specify("PRESSURE","kg/(mm*s\xB2)");
128 MDTVLocalSystemUnits.Specify("DYNAMIC VISCOSITY","kg/(mm*s)");
129 MDTVLocalSystemUnits.Specify("KINETIC VISCOSITY","mm\xB2/s");
130 MDTVLocalSystemUnits.Specify("TENSION SUPERFICIELLE","mm/s\xB2");
131 MDTVLocalSystemUnits.Specify("ENERGY","kg*mm\xB2/s\xB2");
132 MDTVLocalSystemUnits.Specify("POWER","kg*mm\xB2/s\xB3");
133 MDTVLocalSystemUnits.Specify("LINEIC POWER","kg*mm/s\xB3");
134 MDTVLocalSystemUnits.Specify("SURFACIC POWER","kg/s\xB3");
135 MDTVLocalSystemUnits.Specify("VOLUMIC POWER","kg/(mm*s\xB3)");
136 MDTVLocalSystemUnits.Specify("THERMICAL CONDUCTIVITY","kg*mm/(s\xB3*\xB0K)");
137 MDTVLocalSystemUnits.Specify("THERMICAL CONVECTIVITY","kg/(s\xB3*\xB0K)");
138 MDTVLocalSystemUnits.Specify("THERMICAL MASSIC CAPACITY","mm\xB2/(s\xB2*\xB0K)");
139 MDTVLocalSystemUnits.Specify("ENTROPY","kg*mm\xB2/(s\xB2*\xB0K)");
140 MDTVLocalSystemUnits.Specify("ENTHALPY","kg*mm\xB2/s\xB2");
141 MDTVLocalSystemUnits.Specify("LUMINANCE","cd/mm\xB2");
142 MDTVLocalSystemUnits.Specify("LUMINOUS EFFICACITY","s\xB3*Lu/(kg*mm\xB2)");
143 MDTVLocalSystemUnits.Specify("ELECTRIC FIELD","V/mm");
144 MDTVLocalSystemUnits.Specify("ELECTRIC CAPACITANCE","s**4*A\xB2/(kg*mm\xB2)");
145 MDTVLocalSystemUnits.Specify("MAGNETIC FIELD","A/mm");
146 MDTVLocalSystemUnits.Specify("MAGNETIC FLUX","kg*mm\xB2/(s\xB2*A)");
147 MDTVLocalSystemUnits.Specify("INDUCTANCE","kg*mm\xB2/(s\xB2*A\xB2)");
148 MDTVLocalSystemUnits.Specify("RELUCTANCE","s\xB2*A\xB2/(kg*mm\xB2)");
149 MDTVLocalSystemUnits.Specify("RESISTIVITY","O*mm");
150 MDTVLocalSystemUnits.Specify("CONDUCTIVITY","S/mm");
151 MDTVLocalSystemUnits.Specify("MOLAR MASS","kg/mol");
152 MDTVLocalSystemUnits.Specify("MOLAR VOLUME","mm\xB3/mol");
153 MDTVLocalSystemUnits.Specify("CONCENTRATION","kg/mm\xB3");
154 MDTVLocalSystemUnits.Specify("MOLAR CONCENTRATION","mol/mm\xB3");
155 MDTVLocalSystemUnits.Specify("ACCOUSTIC INTENSITY","mm/A\xB2");
156 MDTVLocalSystemUnits.Specify("DOSE EQUIVALENT","mm\xB2/s\xB2");
157 MDTVLocalSystemUnits.Specify("ABSORBED DOSE","mm\xB2/s\xB2");
158 MDTVLocalSystemUnits.Specify("FLUX OF MAGNETIC INDUCTION","kg*mm\xB2/(s\xB2*A)");
159 MDTVLocalSystemUnits.Specify("ROTATION ACCELERATION","rad/s\xB2");
160 MDTVLocalSystemUnits.Specify("TRANSLATION STIFFNESS","kg/s\xB2");
161 MDTVLocalSystemUnits.Specify("ROTATION STIFFNESS","kg*mm\xB2/(s\xB2*rad)");
162 MDTVLocalSystemUnits.Activates();
164 LocalSystemUnits = MDTVLocalSystemUnits;
171 //=======================================================================
172 //function : CurrentToLS
174 //=======================================================================
176 Standard_Real UnitsAPI::CurrentToLS(const Standard_Real aData,
177 const Standard_CString aQuantity)
179 Standard_Real aValue = aData;
180 CheckLoading (localSystem);
181 if( CurrentUnits->Find(aQuantity) ) {
182 TCollection_AsciiString current(CurrentUnits->Value(aQuantity));
183 aValue = Units::ToSI(aData,current.ToCString());
184 aValue = LocalSystemUnits.ConvertSIValueToUserSystem(aQuantity,aValue);
187 cout <<"Warning: UnitsAPI,the quantity '" << aQuantity << "' does not exist in the current units system" << endl;
194 //=======================================================================
195 //function : CurrentToSI
197 //=======================================================================
199 Standard_Real UnitsAPI::CurrentToSI(const Standard_Real aData,
200 const Standard_CString aQuantity)
202 Standard_Real aValue = aData;
203 CheckLoading (UnitsAPI_DEFAULT);
204 if( CurrentUnits->Find(aQuantity) ) {
205 TCollection_AsciiString current(CurrentUnits->Value(aQuantity));
206 aValue = Units::ToSI(aData,current.ToCString());
209 cout<<"Warning: UnitsAPI,the quantity '" << aQuantity << "' does not exist in the current units system" << endl;
216 //=======================================================================
217 //function : CurrentFromLS
219 //=======================================================================
221 Standard_Real UnitsAPI::CurrentFromLS(const Standard_Real aData,
222 const Standard_CString aQuantity)
224 Standard_Real aValue = aData;
225 CheckLoading (localSystem);
226 if( CurrentUnits->Find(aQuantity) ) {
227 TCollection_AsciiString current(CurrentUnits->Value(aQuantity));
228 aValue = LocalSystemUnits.ConvertUserSystemValueToSI(aQuantity,aData);
229 aValue = Units::FromSI(aValue,current.ToCString());
232 cout<<"Warning: UnitsAPI,the quantity '" << aQuantity << "' does not exist in the current units system" << endl;
239 //=======================================================================
240 //function : CurrentFromSI
242 //=======================================================================
244 Standard_Real UnitsAPI::CurrentFromSI(const Standard_Real aData,
245 const Standard_CString aQuantity)
247 Standard_Real aValue = aData;
248 CheckLoading (UnitsAPI_DEFAULT);
249 if( CurrentUnits->Find(aQuantity) ) {
250 TCollection_AsciiString current(CurrentUnits->Value(aQuantity));
251 aValue = Units::FromSI(aData,current.ToCString());
254 cout<<"Warning: UnitsAPI,the quantity '" << aQuantity << "' does not exist in the current units system" << endl;
261 //=======================================================================
262 //function : CurrentToAny
264 //=======================================================================
266 Standard_Real UnitsAPI::CurrentToAny(const Standard_Real aData,
267 const Standard_CString aQuantity,
268 const Standard_CString aUnit) {
269 Standard_Real aValue = aData;
270 CheckLoading (UnitsAPI_DEFAULT);
271 if( CurrentUnits->Find(aQuantity) ) {
272 TCollection_AsciiString current(CurrentUnits->Value(aQuantity));
273 aValue = AnyToAny(aData,current.ToCString(),aUnit);
276 cout<<"Warning: UnitsAPI,the quantity '" << aQuantity << "' does not exist in the current units system" << endl;
283 //=======================================================================
284 //function : CurrentFromAny
286 //=======================================================================
288 Standard_Real UnitsAPI::CurrentFromAny(const Standard_Real aData,
289 const Standard_CString aQuantity,
290 const Standard_CString aUnit)
292 Standard_Real aValue = aData;
293 CheckLoading (UnitsAPI_DEFAULT);
294 if( CurrentUnits->Find(aQuantity) ) {
295 TCollection_AsciiString current(CurrentUnits->Value(aQuantity));
296 aValue = AnyToAny(aData,aUnit,current.ToCString());
299 cout<<"Warning: UnitsAPI,the quantity '" << aQuantity << "' does not exist in the current units system" << endl;
306 //=======================================================================
309 //=======================================================================
311 Standard_Real UnitsAPI::AnyToLS(const Standard_Real aData,
312 const Standard_CString aUnit)
314 Standard_Real aValue = aData;
315 CheckLoading (localSystem);
316 Handle(Units_Dimensions) aDim;
317 aValue = Units::ToSI(aValue,aUnit,aDim);
320 Standard_CString quantity = aDim->Quantity();
322 aValue = LocalSystemUnits.ConvertSIValueToUserSystem(quantity,aValue);
325 cout<<"Warning: BAD Quantity returns in UnitsAPI::AnyToLS(" << aData << "," << aUnit << ")" << endl;
330 //=======================================================================
333 //=======================================================================
335 Standard_Real UnitsAPI::AnyToLS(const Standard_Real aData,
336 const Standard_CString aUnit,
337 Handle(Units_Dimensions) &aDim)
339 Standard_Real aValue = aData;
340 CheckLoading (localSystem);
341 aValue = Units::ToSI(aValue,aUnit,aDim);
342 Standard_CString quantity = aDim->Quantity();
346 aValue = LocalSystemUnits.ConvertSIValueToUserSystem(quantity,aValue);
349 cout<<"Warning: BAD Quantity returns in UnitsAPI::AnyToLS(" << aData << "," << aUnit << "," << aDim << ")" << endl;
354 //=======================================================================
357 //=======================================================================
359 Standard_Real UnitsAPI::AnyToSI(const Standard_Real aData,
360 const Standard_CString aUnit)
362 Standard_Real aValue;
363 CheckLoading (UnitsAPI_DEFAULT);
364 aValue = Units::ToSI(aData,aUnit);
369 //=======================================================================
372 //=======================================================================
374 Standard_Real UnitsAPI::AnyToSI(const Standard_Real aData,
375 const Standard_CString aUnit,
376 Handle(Units_Dimensions) &aDim)
378 Standard_Real aValue;
379 CheckLoading (UnitsAPI_DEFAULT);
380 aValue = Units::ToSI(aData,aUnit,aDim);
385 //=======================================================================
386 //function : AnyFromLS
388 //=======================================================================
390 Standard_Real UnitsAPI::AnyFromLS(const Standard_Real aData,
391 const Standard_CString aUnit)
393 Standard_Real aValue = aData;
394 CheckLoading (localSystem);
395 Handle(Units_Dimensions) aDim;
396 aValue = Units::FromSI(aValue,aUnit,aDim);
397 Standard_CString quantity = aDim->Quantity();
399 aValue = LocalSystemUnits.ConvertUserSystemValueToSI(quantity,aValue);
402 cout<<"Warning: BAD Quantity returns in UnitsAPI::AnyToLS(" << aData << "," << aUnit << ")" << endl;
408 //=======================================================================
409 //function : AnyFromSI
411 //=======================================================================
413 Standard_Real UnitsAPI::AnyFromSI(const Standard_Real aData,
414 const Standard_CString aUnit)
416 Standard_Real aValue;
417 CheckLoading (UnitsAPI_DEFAULT);
418 aValue = Units::FromSI(aData,aUnit);
423 //=======================================================================
424 //function : AnyToAny
426 //=======================================================================
428 Standard_Real UnitsAPI::AnyToAny(const Standard_Real aData,
429 const Standard_CString aUnit1,
430 const Standard_CString aUnit2)
432 Standard_Real aValue = aData;
433 CheckLoading (UnitsAPI_DEFAULT);
434 aValue = Units::Convert(aValue,aUnit1,aUnit2);
439 //=======================================================================
442 //=======================================================================
444 Standard_Real UnitsAPI::LSToSI(const Standard_Real aData,
445 const Standard_CString aQuantity)
447 Standard_Real aValue = aData;
448 CheckLoading (localSystem);
449 if( CurrentUnits->Find(aQuantity) ) {
450 aValue = LocalSystemUnits.ConvertUserSystemValueToSI(aQuantity,aData);
453 cout<<"Warning: UnitsAPI,the quantity '" << aQuantity << "' does not exist in the current units system" << endl;
460 //=======================================================================
463 //=======================================================================
465 Standard_Real UnitsAPI::SIToLS(const Standard_Real aData,
466 const Standard_CString aQuantity)
468 Standard_Real aValue = aData;
469 CheckLoading (localSystem);
470 if( CurrentUnits->Find(aQuantity) ) {
471 aValue = LocalSystemUnits.ConvertSIValueToUserSystem(aQuantity,aValue);
474 cout<<"Warning: UnitsAPI,the quantity '" << aQuantity << "' does not exist in the current units system" << endl;
481 //=======================================================================
482 //function : SetLocalSystem
484 //=======================================================================
486 void UnitsAPI::SetLocalSystem(const UnitsAPI_SystemUnits aSystemUnits)
488 CheckLoading (aSystemUnits);
489 localSystem = currentSystem;
493 //=======================================================================
494 //function : LocalSystem
496 //=======================================================================
498 UnitsAPI_SystemUnits UnitsAPI::LocalSystem()
504 //=======================================================================
505 //function : SetCurrentUnit
507 //=======================================================================
509 void UnitsAPI::SetCurrentUnit(const Standard_CString aQuantity,
510 const Standard_CString anUnit)
512 CheckLoading(localSystem);
513 CurrentUnits->SetResource(aQuantity,anUnit);
517 //=======================================================================
520 //=======================================================================
522 void UnitsAPI::Save()
524 CheckLoading(localSystem);
525 CurrentUnits->Save();
529 //=======================================================================
532 //=======================================================================
534 void UnitsAPI::Reload()
536 currentSystem = UnitsAPI_DEFAULT;
537 CheckLoading(localSystem);
541 //=======================================================================
542 //function : CurrentUnit
544 //=======================================================================
546 static TCollection_AsciiString astring;
547 Standard_CString UnitsAPI::CurrentUnit(const Standard_CString aQuantity)
549 CheckLoading(localSystem);
550 astring = CurrentUnits->Value(aQuantity);
551 return astring.ToCString();
555 //=======================================================================
556 //function : Dimensions
558 //=======================================================================
560 Handle(Units_Dimensions) UnitsAPI::Dimensions(const Standard_CString aType)
562 return Units::Dimensions(aType);
566 //=======================================================================
567 //function : DimensionLess
569 //=======================================================================
571 Handle(Units_Dimensions) UnitsAPI::DimensionLess()
573 return Units_Dimensions::ALess();
577 //=======================================================================
578 //function : DimensionMass
580 //=======================================================================
582 Handle(Units_Dimensions) UnitsAPI::DimensionMass()
584 return Units_Dimensions::AMass();
588 //=======================================================================
589 //function : DimensionLength
591 //=======================================================================
593 Handle(Units_Dimensions) UnitsAPI::DimensionLength()
595 return Units_Dimensions::ALength();
599 //=======================================================================
600 //function : DimensionTime
602 //=======================================================================
604 Handle(Units_Dimensions) UnitsAPI::DimensionTime()
606 return Units_Dimensions::ATime() ;
610 //=======================================================================
611 //function : DimensionElectricCurrent
613 //=======================================================================
615 Handle(Units_Dimensions) UnitsAPI::DimensionElectricCurrent()
617 return Units_Dimensions::AElectricCurrent() ;
621 //=======================================================================
622 //function : DimensionThermodynamicTemperature
624 //=======================================================================
626 Handle(Units_Dimensions) UnitsAPI::DimensionThermodynamicTemperature()
628 return Units_Dimensions::AThermodynamicTemperature();
632 //=======================================================================
633 //function : DimensionAmountOfSubstance
635 //=======================================================================
637 Handle(Units_Dimensions) UnitsAPI::DimensionAmountOfSubstance()
639 return Units_Dimensions::AAmountOfSubstance();
643 //=======================================================================
644 //function : DimensionLuminousIntensity
646 //=======================================================================
648 Handle(Units_Dimensions) UnitsAPI::DimensionLuminousIntensity()
650 return Units_Dimensions::ALuminousIntensity();
654 //=======================================================================
655 //function : DimensionPlaneAngle
657 //=======================================================================
659 Handle(Units_Dimensions) UnitsAPI::DimensionPlaneAngle()
661 return Units_Dimensions::APlaneAngle();
665 //=======================================================================
666 //function : DimensionSolidAngle
668 //=======================================================================
670 Handle(Units_Dimensions) UnitsAPI::DimensionSolidAngle()
672 return Units_Dimensions::ASolidAngle();
676 //=======================================================================
679 //=======================================================================
681 Standard_Boolean UnitsAPI::Check(const Standard_CString aQuantity,
682 const Standard_CString /*aUnit*/)
684 Standard_Boolean status = Standard_False;
685 CheckLoading (UnitsAPI_DEFAULT);
686 if( CurrentUnits->Find(aQuantity) ) {
687 TCollection_AsciiString current(CurrentUnits->Value(aQuantity));
688 // aValue = AnyToAny(aData,current.ToCString(),aUnit);
689 // aValue = Units::Convert(aValue,aUnit1,aUnit2);