1 // Created on: 1996-01-18
2 // Created by: Frederic MAUPAS
3 // Copyright (c) 1996-1999 Matra Datavision
4 // Copyright (c) 1999-2014 OPEN CASCADE SAS
6 // This file is part of Open CASCADE Technology software library.
8 // This library is free software; you can redistribute it and/or modify it under
9 // the terms of the GNU Lesser General Public License version 2.1 as published
10 // by the Free Software Foundation, with special exception defined in the file
11 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
12 // distribution for complete text of the license and disclaimer of any warranty.
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
17 //abv 17.11.99: renamed from StepPDR_MakeUnitAndToleranceContext and merged with STEPControl_Unit
18 //abv 30.02.00: ability to write file in units other than MM
20 #include <STEPConstruct_UnitContext.ixx>
22 #include <TCollection_HAsciiString.hxx>
24 #include <StepBasic_SiUnit.hxx>
25 #include <StepBasic_SiPrefix.hxx>
26 #include <StepBasic_NamedUnit.hxx>
27 #include <StepBasic_LengthUnit.hxx>
28 #include <StepBasic_PlaneAngleUnit.hxx>
29 #include <StepBasic_SolidAngleUnit.hxx>
30 #include <StepBasic_SiUnitAndLengthUnit.hxx>
31 #include <StepBasic_SiUnitAndPlaneAngleUnit.hxx>
32 #include <StepBasic_SiUnitAndSolidAngleUnit.hxx>
33 #include <StepBasic_SiUnitAndAreaUnit.hxx>
34 #include <StepBasic_SiUnitAndVolumeUnit.hxx>
35 #include <StepBasic_UncertaintyMeasureWithUnit.hxx>
36 #include <StepBasic_DimensionalExponents.hxx>
37 #include <StepBasic_MeasureValueMember.hxx>
38 #include <StepBasic_MeasureWithUnit.hxx>
39 #include <StepBasic_LengthMeasureWithUnit.hxx>
40 #include <StepBasic_PlaneAngleMeasureWithUnit.hxx>
41 #include <StepBasic_SolidAngleMeasureWithUnit.hxx>
42 #include <StepBasic_ConversionBasedUnitAndLengthUnit.hxx>
43 #include <StepBasic_ConversionBasedUnitAndPlaneAngleUnit.hxx>
44 #include <StepBasic_ConversionBasedUnitAndSolidAngleUnit.hxx>
45 #include <StepBasic_ConversionBasedUnitAndAreaUnit.hxx>
46 #include <StepBasic_ConversionBasedUnitAndVolumeUnit.hxx>
47 #include <StepBasic_HArray1OfNamedUnit.hxx>
48 #include <StepBasic_HArray1OfUncertaintyMeasureWithUnit.hxx>
49 #include <UnitsMethods.hxx>
50 #include <Interface_Static.hxx>
51 #include <StepBasic_ConversionBasedUnit.hxx>
53 //=======================================================================
54 //function : STEPConstruct_UnitContext
56 //=======================================================================
58 STEPConstruct_UnitContext::STEPConstruct_UnitContext() : done(Standard_False)
60 lengthDone = planeAngleDone = solidAngleDone = hasUncertainty =
61 areaDone = volumeDone = Standard_False;
62 //pdn file r_47-sd.stp initalize field.
63 theUncertainty = RealLast();
66 //=======================================================================
69 //=======================================================================
71 void STEPConstruct_UnitContext::Init(const Standard_Real Tol3d)
75 GRC = new StepGeom_GeomRepContextAndGlobUnitAssCtxAndGlobUncertaintyAssCtx;
76 Handle(TCollection_HAsciiString) contextID =
77 new TCollection_HAsciiString("Context #1"); // ?????
79 Handle(TCollection_HAsciiString) contextType =
80 new TCollection_HAsciiString("3D Context with UNIT and UNCERTAINTY");
82 // Units : LengthUnit and PlaneAngleUnit (no SolidAngleUnit appliable)
84 Handle(StepBasic_NamedUnit) lengthUnit;
85 Standard_CString uName = 0;
86 Standard_Boolean hasPref = Standard_True;
87 StepBasic_SiPrefix siPref = StepBasic_spMilli;
88 switch ( Interface_Static::IVal ( "write.step.unit" ) ) {
89 case 1 : uName = "INCH"; break;
92 case 4 : uName = "FOOT"; break;
93 case 5 : uName = "MILE"; break;
94 case 6 : hasPref = Standard_False; break;
95 case 7 : siPref = StepBasic_spKilo; break;
96 case 8 : uName = "MIL"; break;
97 case 9 : siPref = StepBasic_spMicro; break;
98 case 10 : siPref = StepBasic_spCenti; break;
99 case 11 : uName = "MICROINCH"; break;
102 Handle(StepBasic_SiUnitAndLengthUnit) siUnit =
103 new StepBasic_SiUnitAndLengthUnit;
104 siUnit->Init(hasPref,siPref,StepBasic_sunMetre);
106 if ( uName ) { // for non-metric units, create conversion_based_unit
107 Handle(StepBasic_MeasureValueMember) val = new StepBasic_MeasureValueMember;
108 val->SetName("LENGTH_UNIT");
109 val->SetReal ( UnitsMethods::GetLengthFactorValue ( Interface_Static::IVal ( "write.step.unit" ) ) );
111 Handle(StepBasic_LengthMeasureWithUnit) measure = new StepBasic_LengthMeasureWithUnit;
113 Unit.SetValue ( siUnit );
114 measure->Init ( val, Unit );
116 Handle(StepBasic_DimensionalExponents) theDimExp = new StepBasic_DimensionalExponents;
117 theDimExp->Init ( 1., 0., 0., 0., 0., 0., 0. );
119 Handle(TCollection_HAsciiString) convName = new TCollection_HAsciiString ( uName );
120 Handle(StepBasic_ConversionBasedUnitAndLengthUnit) convUnit =
121 new StepBasic_ConversionBasedUnitAndLengthUnit;
122 convUnit->Init ( theDimExp, convName, measure );
124 lengthUnit = convUnit;
126 else lengthUnit = siUnit;
128 Handle(StepBasic_SiUnitAndPlaneAngleUnit) radianUnit =
129 new StepBasic_SiUnitAndPlaneAngleUnit;
130 radianUnit ->Init(Standard_False,
131 StepBasic_spMilli, // the unit is radian, no prefix
132 StepBasic_sunRadian);
134 Handle(StepBasic_HArray1OfNamedUnit) units =
135 new StepBasic_HArray1OfNamedUnit(1, 3);
137 Handle(StepBasic_SiUnitAndSolidAngleUnit) sradUnit =
138 new StepBasic_SiUnitAndSolidAngleUnit;
139 sradUnit ->Init(Standard_False,
140 StepBasic_spMilli, // the unit is steradian, no prefix
141 StepBasic_sunSteradian);
143 units->SetValue(1, lengthUnit);
144 units->SetValue(2, radianUnit);
145 units->SetValue(3, sradUnit);
147 // Uncertainty : 3D confusion Tolerance
149 Handle(StepBasic_HArray1OfUncertaintyMeasureWithUnit) Tols =
150 new StepBasic_HArray1OfUncertaintyMeasureWithUnit(1,1);
151 Handle(StepBasic_UncertaintyMeasureWithUnit) theTol3d =
152 new StepBasic_UncertaintyMeasureWithUnit;
154 Handle(TCollection_HAsciiString) TolName =
155 new TCollection_HAsciiString("distance_accuracy_value");
156 Handle(TCollection_HAsciiString) TolDesc =
157 new TCollection_HAsciiString("confusion accuracy");
159 Handle(StepBasic_MeasureValueMember) mvs = new StepBasic_MeasureValueMember;
160 mvs->SetName("LENGTH_MEASURE");
161 mvs->SetReal ( Tol3d / UnitsMethods::LengthFactor() );
163 Unit.SetValue ( lengthUnit );
164 theTol3d->Init(mvs, Unit, TolName, TolDesc);
165 Tols->SetValue(1, theTol3d);
167 GRC->Init(contextID, contextType, 3, units, Tols);
170 //=======================================================================
173 //=======================================================================
175 Standard_Boolean STEPConstruct_UnitContext::IsDone() const
180 //=======================================================================
183 //=======================================================================
185 Handle(StepGeom_GeomRepContextAndGlobUnitAssCtxAndGlobUncertaintyAssCtx) STEPConstruct_UnitContext::Value() const
190 // ==========================================================================
191 // Method : ConvertSiPrefix
192 // Purpose : Computes SiPrefix conversion
193 // ==========================================================================
195 Standard_Real STEPConstruct_UnitContext::ConvertSiPrefix (const StepBasic_SiPrefix aPrefix)
199 case StepBasic_spExa: return 1.E+18;
200 case StepBasic_spPeta: return 1.E+15;
201 case StepBasic_spTera: return 1.E+12;
202 case StepBasic_spGiga: return 1.E+9;
203 case StepBasic_spMega: return 1.E+6;
204 case StepBasic_spKilo: return 1.E+3;
205 case StepBasic_spHecto: return 1.E+2;
206 case StepBasic_spDeca: return 1.E+1;
207 case StepBasic_spDeci: return 1.E-1;
208 case StepBasic_spCenti: return 1.E-2;
209 case StepBasic_spMilli: return 1.E-3;
210 case StepBasic_spMicro: return 1.E-6;
211 case StepBasic_spNano: return 1.E-9;
212 case StepBasic_spPico: return 1.E-12;
213 case StepBasic_spFemto: return 1.E-15;
214 case StepBasic_spAtto: return 1.E-18;
222 // ==========================================================================
223 // Method : STEPConstruct_UnitContext::SiUnitNameFactor
225 // ==========================================================================
227 Standard_Boolean STEPConstruct_UnitContext::SiUnitNameFactor(const Handle(StepBasic_SiUnit)& aSiUnit,
228 Standard_Real& theSIUNFactor) const
231 switch ( aSiUnit->Name() )
233 case StepBasic_sunMetre:
234 case StepBasic_sunRadian:
235 case StepBasic_sunSteradian:
236 return Standard_True;
238 // cout << "Unknown SiUnitName : " << aSiUnit->Name() << endl;
239 return Standard_False;
243 // ==========================================================================
244 // Method : STEPConstruct_UnitContext::ComputeFactors
246 // ==========================================================================
248 Standard_Integer STEPConstruct_UnitContext::ComputeFactors(const Handle(StepRepr_GlobalUnitAssignedContext)& aContext)
250 Standard_Integer status = 0;
252 // Initialise the default value
253 // status : 0 if OK, else 1 2 3
254 lengthFactor = solidAngleFactor = 1.;
255 planeAngleFactor = M_PI/180.;
256 // Standard_Real theLExp = 1.;
257 // Standard_Real thePAExp = 0.;
258 // Standard_Real theSAExp = 0.;
259 lengthDone = planeAngleDone = solidAngleDone = Standard_False;
261 if (aContext.IsNull()) {
263 cout<<" -- STEPConstruct_UnitContext:ComputeFactor, Context undefined -> default"<<endl;
269 Handle(StepBasic_HArray1OfNamedUnit) theUnits = aContext->Units();
270 Standard_Integer nbU = aContext->NbUnits();
272 for (Standard_Integer i = 1; i <= nbU; i++) {
273 Handle(StepBasic_NamedUnit) theNamedUnit = aContext->UnitsValue(i);
274 status = ComputeFactors(theNamedUnit);
277 cout << " -- STEPConstruct_UnitContext:ComputeFactor: Unit item no." << i << " is not recognized" << endl;
284 Standard_Integer STEPConstruct_UnitContext::ComputeFactors(const Handle(StepBasic_NamedUnit)& aUnit)
287 //:f3 abv 8 Apr 98: ProSTEP TR8 tr8_as_sd_sw: the case of unrecognized entity
288 if ( aUnit.IsNull() )
291 Standard_Integer status = 0;
292 Standard_Real theFactor= 0.;
293 Standard_Real theSIUNF = 0.;
295 Standard_Real parameter= 0.;
296 Standard_Boolean parameterDone = Standard_False;
297 if(aUnit->IsKind(STANDARD_TYPE(StepBasic_ConversionBasedUnit))) {
298 Handle(StepBasic_ConversionBasedUnit) theCBU =
299 Handle(StepBasic_ConversionBasedUnit)::DownCast(aUnit);
300 // Handle(StepBasic_DimensionalExponents) theDimExp = theCBU->Dimensions();
301 Handle(StepBasic_MeasureWithUnit) theMWU;
302 if(!theCBU.IsNull()) {
303 theMWU = theCBU->ConversionFactor();
304 // sln 8.10.2001: the case of unrecognized entity
307 //if (!theMWU->IsKind(STANDARD_TYPE(StepBasic_LengthMeasureWithUnit))) { gka
310 Handle(StepBasic_NamedUnit) theTargetUnit = theMWU->UnitComponent().NamedUnit();
311 //StepBasic_Unit theTargetUnit = theMWU->UnitComponent();
312 Standard_Real theSIPFactor = 1.;
314 //:f5 abv 24 Apr 98: ProSTEP TR8 tr8_bv1_tc: INCHES
315 //gka Handle(StepBasic_SiUnitAndLengthUnit) theSUALU =
316 // Handle(StepBasic_SiUnitAndLengthUnit)::DownCast(theTargetUnit);
317 // Handle(StepBasic_SiUnit) theSIU;
318 // if ( ! theSUALU.IsNull() ) theSIU = Handle(StepBasic_SiUnit)::DownCast(theSUALU);
319 Handle(StepBasic_SiUnit) theSIU = //f5
320 Handle(StepBasic_SiUnit)::DownCast(theTargetUnit);//f5
322 if (!theSIU.IsNull()) {
323 if (theSIU->HasPrefix()) {
325 StepBasic_SiPrefix aPrefix = theSIU->Prefix();
326 theSIPFactor = ConvertSiPrefix(aPrefix);
328 // Treat the SiUnitName
329 if (!SiUnitNameFactor(theSIU,theSIUNF)) status = 11; // et continue
330 //cout << "The SiUnitNameFactor is :";
331 //cout << theSIUNF << endl;
334 // cout << "Recursive algo required - Aborted" << endl;
337 Standard_Real theMVAL = theMWU->ValueComponent();
338 theFactor = theSIPFactor * theMVAL; // * theSIUNF * pow(10.,theLExp)
340 parameter = theFactor;
342 parameterDone = Standard_True;
347 cout << "Error in the file : parameter double defined" << endl;
351 else if (aUnit->IsKind(STANDARD_TYPE(StepBasic_SiUnit))) {
352 Handle(StepBasic_SiUnit) theSIU = Handle(StepBasic_SiUnit)::DownCast(aUnit);
353 Standard_Real theSIPFactor = 1.;
354 if (theSIU->HasPrefix()) {
356 StepBasic_SiPrefix aPrefix = theSIU->Prefix();
357 theSIPFactor = ConvertSiPrefix(aPrefix);
360 // Treat the SiUnitName
361 if (!SiUnitNameFactor(theSIU,theSIUNF)) status = 11;
363 // final computation for lengthFactor
364 theFactor = theSIPFactor * theSIUNF;
365 parameter = theFactor;
366 if (!parameterDone) {
367 parameterDone = Standard_True;
372 cout << "Error in the file : parameter double defined" << endl;
377 // Defining a type of unit
380 cout << "Unit Type not implemented" << endl;
385 if (aUnit->IsKind(STANDARD_TYPE(StepBasic_ConversionBasedUnitAndLengthUnit))||
386 aUnit->IsKind(STANDARD_TYPE(StepBasic_SiUnitAndLengthUnit))) {
388 lengthFactor = parameter;
390 lengthFactor = parameter * 1000. / UnitsMethods::GetCasCadeLengthUnit();
393 lengthDone = Standard_True;
397 cout << "Error in the file : LengthFactor double defined" << endl;
400 } // end of LengthUnit
401 else if (aUnit->IsKind(STANDARD_TYPE(StepBasic_ConversionBasedUnitAndPlaneAngleUnit))||
402 aUnit->IsKind(STANDARD_TYPE(StepBasic_SiUnitAndPlaneAngleUnit))) {
403 planeAngleFactor = parameter;
404 planeAngleDone = Standard_True;
405 } // end of PlaneAngleUnit
406 else if (aUnit->IsKind(STANDARD_TYPE(StepBasic_ConversionBasedUnitAndSolidAngleUnit))||
407 aUnit->IsKind(STANDARD_TYPE(StepBasic_SiUnitAndSolidAngleUnit))) {
408 solidAngleFactor = parameter;
409 solidAngleDone = Standard_True;
410 } // end of SolidAngleUnit
411 else if (aUnit->IsKind(STANDARD_TYPE(StepBasic_ConversionBasedUnitAndAreaUnit)) ||
412 aUnit->IsKind(STANDARD_TYPE(StepBasic_SiUnitAndAreaUnit))) {
417 af = parameter * 1000. / UnitsMethods::GetCasCadeLengthUnit();
419 areaDone = Standard_True;
420 areaFactor = pow(af,2);
422 else if (aUnit->IsKind(STANDARD_TYPE(StepBasic_ConversionBasedUnitAndVolumeUnit)) ||
423 aUnit->IsKind(STANDARD_TYPE(StepBasic_SiUnitAndVolumeUnit))) {
428 af = parameter * 1000. / UnitsMethods::GetCasCadeLengthUnit();
430 volumeDone = Standard_True;
431 volumeFactor = pow(af,3);
437 // ==========================================================================
438 // Method : STEPConstruct_UnitContext::ComputeTolerance
440 // ==========================================================================
442 Standard_Integer STEPConstruct_UnitContext::ComputeTolerance
443 (const Handle(StepRepr_GlobalUncertaintyAssignedContext)& aContext)
445 Standard_Integer status = 0;
446 // Decode the Uncertainty information (geometric accuracy)
448 hasUncertainty = Standard_False;
449 Standard_Integer nbUncertainty = 0;
451 if (!aContext.IsNull()) nbUncertainty = aContext->NbUncertainty();
454 for (Standard_Integer un = 1 ; un <= nbUncertainty ; un ++) {
455 Handle(StepBasic_UncertaintyMeasureWithUnit) aUMWU = aContext->UncertaintyValue(un);
456 if (aUMWU.IsNull()) {
458 cout<<"BAD Uncertainty Measure with Units, n0."<<un<<endl;
462 // Decode the associated Unit
463 Handle(StepBasic_SiUnitAndLengthUnit) aUnit =
464 Handle(StepBasic_SiUnitAndLengthUnit)::DownCast(aUMWU->UnitComponent().NamedUnit());
465 if (!aUnit.IsNull()) {
466 // Extract Uncertainty value
467 Standard_Real LengthUncertainty = aUMWU->ValueComponent();
468 // Update it according to the Length Unit Factor
469 //pdn r_47-sd.stp to choose minimal uncertainty
470 if(theUncertainty > LengthUncertainty) theUncertainty = LengthUncertainty;
471 hasUncertainty = Standard_True;
474 Handle(StepBasic_ConversionBasedUnitAndLengthUnit) aCBULU =
475 Handle(StepBasic_ConversionBasedUnitAndLengthUnit)::DownCast(aUMWU->UnitComponent().NamedUnit());
476 if (!aCBULU.IsNull()) {
477 // Extract Uncertainty value
478 Standard_Real LengthUncertainty = aUMWU->ValueComponent();
479 // Update it according to the Length Unit Factor
480 //pdn r_47-sd.stp to choose minimal uncertainty
481 if(theUncertainty > LengthUncertainty) theUncertainty = LengthUncertainty; // *lengthFactor; fait par appelant
482 hasUncertainty = Standard_True;
488 if (hasUncertainty) cout << "UNCERTAINTY read as " << theUncertainty << endl;
493 // ==========================================================================
494 // Method : STEPConstruct_UnitContext::LengthFactor
496 // ==========================================================================
497 Standard_Real STEPConstruct_UnitContext::LengthFactor() const
498 { return lengthFactor; }
500 // ==========================================================================
501 // Method : STEPConstruct_UnitContext::PlaneAngleFactor
503 // ==========================================================================
504 Standard_Real STEPConstruct_UnitContext::PlaneAngleFactor() const
505 { return planeAngleFactor; }
507 // ==========================================================================
508 // Method : STEPConstruct_UnitContext::SolidAngleFactor
510 // ==========================================================================
511 Standard_Real STEPConstruct_UnitContext::SolidAngleFactor() const
512 { return solidAngleFactor; }
514 // ==========================================================================
515 // Method : STEPConstruct_UnitContext::Uncertainty
517 // ==========================================================================
518 Standard_Real STEPConstruct_UnitContext::Uncertainty () const
519 { return theUncertainty; }
521 // ==========================================================================
522 // Method : STEPConstruct_UnitContext::HasUncertainty
524 // ==========================================================================
525 Standard_Boolean STEPConstruct_UnitContext::HasUncertainty () const
526 { return hasUncertainty; }
528 // ==========================================================================
529 // Method : STEPConstruct_UnitContext::LengthDone
531 // ==========================================================================
532 Standard_Boolean STEPConstruct_UnitContext::LengthDone() const
533 { return lengthDone; }
535 // ==========================================================================
536 // Method : STEPConstruct_UnitContext::PlaneAngleDone
538 // ==========================================================================
539 Standard_Boolean STEPConstruct_UnitContext::PlaneAngleDone() const
540 { return planeAngleDone; }
542 // ==========================================================================
543 // Method : STEPConstruct_UnitContext::SolidAngleDone
545 // ==========================================================================
546 Standard_Boolean STEPConstruct_UnitContext::SolidAngleDone() const
547 { return solidAngleDone; }
549 // ==========================================================================
550 // Method : STEPConstruct_UnitContext::StatusMessage
552 // ==========================================================================
553 Standard_CString STEPConstruct_UnitContext::StatusMessage (const Standard_Integer status) const
557 case 1 : return "No GlobalUnitAssignedContext, default taken";
558 case 2 : return "No LengthMeasureWithUnit, default taken";
559 case 3 : return "No SiUnit for LengthMeasure undefined, default taken";
560 case 4 : return "No PlaneAngleMeasureWithUnit, default taken";
561 case 5 : return "No SiUnit for PlaneAngleMeasure undefined, default taken";
562 case 6 : return "No SolidAngleMeasureWithUnit, default taken";
563 case 7 : return "No SiUnit for SolidAngleMeasure undefined, default taken";
565 case 11 : return "Length Unit not recognized, default taken";
566 case 12 : return "Plane Angle Unit not recognized, default taken";
567 case 13 : return "Solid Angle Unit not recognized, default taken";
568 case 14 : return "At least one unit is twice defined";
570 case 40 : return "Bad GlobalUncertaintyAssignedContext, default unit taken";
575 return "Badly defined units, default taken";
578 Standard_Real STEPConstruct_UnitContext::AreaFactor() const
583 Standard_Real STEPConstruct_UnitContext::VolumeFactor() const
588 Standard_Boolean STEPConstruct_UnitContext::AreaDone() const
593 Standard_Boolean STEPConstruct_UnitContext::VolumeDone() const