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>
52 //=======================================================================
53 //function : STEPConstruct_UnitContext
55 //=======================================================================
57 STEPConstruct_UnitContext::STEPConstruct_UnitContext() : done(Standard_False)
59 lengthDone = planeAngleDone = solidAngleDone = hasUncertainty =
60 areaDone = volumeDone = Standard_False;
61 //pdn file r_47-sd.stp initalize field.
62 theUncertainty = RealLast();
65 //=======================================================================
68 //=======================================================================
70 void STEPConstruct_UnitContext::Init(const Standard_Real Tol3d)
74 GRC = new StepGeom_GeomRepContextAndGlobUnitAssCtxAndGlobUncertaintyAssCtx;
75 Handle(TCollection_HAsciiString) contextID =
76 new TCollection_HAsciiString("Context #1"); // ?????
78 Handle(TCollection_HAsciiString) contextType =
79 new TCollection_HAsciiString("3D Context with UNIT and UNCERTAINTY");
81 // Units : LengthUnit and PlaneAngleUnit (no SolidAngleUnit appliable)
83 Handle(StepBasic_NamedUnit) lengthUnit;
84 Standard_CString uName = 0;
85 Standard_Boolean hasPref = Standard_True;
86 StepBasic_SiPrefix siPref = StepBasic_spMilli;
87 switch ( Interface_Static::IVal ( "write.step.unit" ) ) {
88 case 1 : uName = "INCH"; break;
91 case 4 : uName = "FOOT"; break;
92 case 5 : uName = "MILE"; break;
93 case 6 : hasPref = Standard_False; break;
94 case 7 : siPref = StepBasic_spKilo; break;
95 case 8 : uName = "MIL"; break;
96 case 9 : siPref = StepBasic_spMicro; break;
97 case 10 : siPref = StepBasic_spCenti; break;
98 case 11 : uName = "MICROINCH"; break;
101 Handle(StepBasic_SiUnitAndLengthUnit) siUnit =
102 new StepBasic_SiUnitAndLengthUnit;
103 siUnit->Init(hasPref,siPref,StepBasic_sunMetre);
105 if ( uName ) { // for non-metric units, create conversion_based_unit
106 Handle(StepBasic_MeasureValueMember) val = new StepBasic_MeasureValueMember;
107 val->SetName("LENGTH_UNIT");
108 val->SetReal ( UnitsMethods::GetLengthFactorValue ( Interface_Static::IVal ( "write.step.unit" ) ) );
110 Handle(StepBasic_LengthMeasureWithUnit) measure = new StepBasic_LengthMeasureWithUnit;
112 Unit.SetValue ( siUnit );
113 measure->Init ( val, Unit );
115 Handle(StepBasic_DimensionalExponents) theDimExp = new StepBasic_DimensionalExponents;
116 theDimExp->Init ( 1., 0., 0., 0., 0., 0., 0. );
118 Handle(TCollection_HAsciiString) convName = new TCollection_HAsciiString ( uName );
119 Handle(StepBasic_ConversionBasedUnitAndLengthUnit) convUnit =
120 new StepBasic_ConversionBasedUnitAndLengthUnit;
121 convUnit->Init ( theDimExp, convName, measure );
123 lengthUnit = convUnit;
125 else lengthUnit = siUnit;
127 Handle(StepBasic_SiUnitAndPlaneAngleUnit) radianUnit =
128 new StepBasic_SiUnitAndPlaneAngleUnit;
129 radianUnit ->Init(Standard_False,
130 StepBasic_spMilli, // the unit is radian, no prefix
131 StepBasic_sunRadian);
133 Handle(StepBasic_HArray1OfNamedUnit) units =
134 new StepBasic_HArray1OfNamedUnit(1, 3);
136 Handle(StepBasic_SiUnitAndSolidAngleUnit) sradUnit =
137 new StepBasic_SiUnitAndSolidAngleUnit;
138 sradUnit ->Init(Standard_False,
139 StepBasic_spMilli, // the unit is steradian, no prefix
140 StepBasic_sunSteradian);
142 units->SetValue(1, lengthUnit);
143 units->SetValue(2, radianUnit);
144 units->SetValue(3, sradUnit);
146 // Uncertainty : 3D confusion Tolerance
148 Handle(StepBasic_HArray1OfUncertaintyMeasureWithUnit) Tols =
149 new StepBasic_HArray1OfUncertaintyMeasureWithUnit(1,1);
150 Handle(StepBasic_UncertaintyMeasureWithUnit) theTol3d =
151 new StepBasic_UncertaintyMeasureWithUnit;
153 Handle(TCollection_HAsciiString) TolName =
154 new TCollection_HAsciiString("distance_accuracy_value");
155 Handle(TCollection_HAsciiString) TolDesc =
156 new TCollection_HAsciiString("confusion accuracy");
158 Handle(StepBasic_MeasureValueMember) mvs = new StepBasic_MeasureValueMember;
159 mvs->SetName("LENGTH_MEASURE");
160 mvs->SetReal ( Tol3d / UnitsMethods::LengthFactor() );
162 Unit.SetValue ( lengthUnit );
163 theTol3d->Init(mvs, Unit, TolName, TolDesc);
164 Tols->SetValue(1, theTol3d);
166 GRC->Init(contextID, contextType, 3, units, Tols);
169 //=======================================================================
172 //=======================================================================
174 Standard_Boolean STEPConstruct_UnitContext::IsDone() const
179 //=======================================================================
182 //=======================================================================
184 Handle(StepGeom_GeomRepContextAndGlobUnitAssCtxAndGlobUncertaintyAssCtx) STEPConstruct_UnitContext::Value() const
189 // ==========================================================================
190 // Method : ConvertSiPrefix
191 // Purpose : Computes SiPrefix conversion
192 // ==========================================================================
194 Standard_Real STEPConstruct_UnitContext::ConvertSiPrefix (const StepBasic_SiPrefix aPrefix)
198 case StepBasic_spExa: return 1.E+18;
199 case StepBasic_spPeta: return 1.E+15;
200 case StepBasic_spTera: return 1.E+12;
201 case StepBasic_spGiga: return 1.E+9;
202 case StepBasic_spMega: return 1.E+6;
203 case StepBasic_spKilo: return 1.E+3;
204 case StepBasic_spHecto: return 1.E+2;
205 case StepBasic_spDeca: return 1.E+1;
206 case StepBasic_spDeci: return 1.E-1;
207 case StepBasic_spCenti: return 1.E-2;
208 case StepBasic_spMilli: return 1.E-3;
209 case StepBasic_spMicro: return 1.E-6;
210 case StepBasic_spNano: return 1.E-9;
211 case StepBasic_spPico: return 1.E-12;
212 case StepBasic_spFemto: return 1.E-15;
213 case StepBasic_spAtto: return 1.E-18;
221 // ==========================================================================
222 // Method : STEPConstruct_UnitContext::SiUnitNameFactor
224 // ==========================================================================
226 Standard_Boolean STEPConstruct_UnitContext::SiUnitNameFactor(const Handle(StepBasic_SiUnit)& aSiUnit,
227 Standard_Real& theSIUNFactor) const
230 switch ( aSiUnit->Name() )
232 case StepBasic_sunMetre:
233 case StepBasic_sunRadian:
234 case StepBasic_sunSteradian:
235 return Standard_True;
237 // cout << "Unknown SiUnitName : " << aSiUnit->Name() << endl;
238 return Standard_False;
242 // ==========================================================================
243 // Method : STEPConstruct_UnitContext::ComputeFactors
245 // ==========================================================================
247 Standard_Integer STEPConstruct_UnitContext::ComputeFactors(const Handle(StepRepr_GlobalUnitAssignedContext)& aContext)
249 Standard_Integer status = 0;
251 // Initialise the default value
252 // status : 0 if OK, else 1 2 3
253 lengthFactor = solidAngleFactor = 1.;
254 planeAngleFactor = M_PI/180.;
255 // Standard_Real theLExp = 1.;
256 // Standard_Real thePAExp = 0.;
257 // Standard_Real theSAExp = 0.;
258 lengthDone = planeAngleDone = solidAngleDone = Standard_False;
260 if (aContext.IsNull()) {
262 cout<<" -- STEPConstruct_UnitContext:ComputeFactor, Context undefined -> default"<<endl;
268 Handle(StepBasic_HArray1OfNamedUnit) theUnits = aContext->Units();
269 Standard_Integer nbU = aContext->NbUnits();
271 for (Standard_Integer i = 1; i <= nbU; i++) {
272 Handle(StepBasic_NamedUnit) theNamedUnit = aContext->UnitsValue(i);
273 status = ComputeFactors(theNamedUnit);
276 cout << " -- STEPConstruct_UnitContext:ComputeFactor: Unit item no." << i << " is not recognized" << endl;
283 Standard_Integer STEPConstruct_UnitContext::ComputeFactors(const Handle(StepBasic_NamedUnit)& aUnit)
286 //:f3 abv 8 Apr 98: ProSTEP TR8 tr8_as_sd_sw: the case of unrecognized entity
287 if ( aUnit.IsNull() )
290 Standard_Integer status = 0;
291 Standard_Real theFactor= 0.;
292 Standard_Real theSIUNF = 0.;
294 Standard_Real parameter= 0.;
295 Standard_Boolean parameterDone = Standard_False;
296 if(aUnit->IsKind(STANDARD_TYPE(StepBasic_ConversionBasedUnit))) {
297 Handle(StepBasic_ConversionBasedUnit) theCBU =
298 Handle(StepBasic_ConversionBasedUnit)::DownCast(aUnit);
299 // Handle(StepBasic_DimensionalExponents) theDimExp = theCBU->Dimensions();
300 Handle(StepBasic_MeasureWithUnit) theMWU;
301 if(!theCBU.IsNull()) {
302 theMWU = theCBU->ConversionFactor();
303 // sln 8.10.2001: the case of unrecognized entity
306 //if (!theMWU->IsKind(STANDARD_TYPE(StepBasic_LengthMeasureWithUnit))) { gka
309 Handle(StepBasic_NamedUnit) theTargetUnit = theMWU->UnitComponent().NamedUnit();
310 //StepBasic_Unit theTargetUnit = theMWU->UnitComponent();
311 Standard_Real theSIPFactor = 1.;
313 //:f5 abv 24 Apr 98: ProSTEP TR8 tr8_bv1_tc: INCHES
314 //gka Handle(StepBasic_SiUnitAndLengthUnit) theSUALU =
315 // Handle(StepBasic_SiUnitAndLengthUnit)::DownCast(theTargetUnit);
316 // Handle(StepBasic_SiUnit) theSIU;
317 // if ( ! theSUALU.IsNull() ) theSIU = Handle(StepBasic_SiUnit)::DownCast(theSUALU);
318 Handle(StepBasic_SiUnit) theSIU = //f5
319 Handle(StepBasic_SiUnit)::DownCast(theTargetUnit);//f5
321 if (!theSIU.IsNull()) {
322 if (theSIU->HasPrefix()) {
324 StepBasic_SiPrefix aPrefix = theSIU->Prefix();
325 theSIPFactor = ConvertSiPrefix(aPrefix);
327 // Treat the SiUnitName
328 if (!SiUnitNameFactor(theSIU,theSIUNF)) status = 11; // et continue
329 //cout << "The SiUnitNameFactor is :";
330 //cout << theSIUNF << endl;
333 // cout << "Recursive algo required - Aborted" << endl;
336 Standard_Real theMVAL = theMWU->ValueComponent();
337 theFactor = theSIPFactor * theMVAL; // * theSIUNF * pow(10.,theLExp)
339 parameter = theFactor;
341 parameterDone = Standard_True;
346 cout << "Error in the file : parameter double defined" << endl;
350 else if (aUnit->IsKind(STANDARD_TYPE(StepBasic_SiUnit))) {
351 Handle(StepBasic_SiUnit) theSIU = Handle(StepBasic_SiUnit)::DownCast(aUnit);
352 Standard_Real theSIPFactor = 1.;
353 if (theSIU->HasPrefix()) {
355 StepBasic_SiPrefix aPrefix = theSIU->Prefix();
356 theSIPFactor = ConvertSiPrefix(aPrefix);
359 // Treat the SiUnitName
360 if (!SiUnitNameFactor(theSIU,theSIUNF)) status = 11;
362 // final computation for lengthFactor
363 theFactor = theSIPFactor * theSIUNF;
364 parameter = theFactor;
365 if (!parameterDone) {
366 parameterDone = Standard_True;
371 cout << "Error in the file : parameter double defined" << endl;
376 // Defining a type of unit
379 cout << "Unit Type not implemented" << endl;
384 if (aUnit->IsKind(STANDARD_TYPE(StepBasic_ConversionBasedUnitAndLengthUnit))||
385 aUnit->IsKind(STANDARD_TYPE(StepBasic_SiUnitAndLengthUnit))) {
387 lengthFactor = parameter;
389 lengthFactor = parameter * 1000. / UnitsMethods::GetCasCadeLengthUnit();
392 lengthDone = Standard_True;
396 cout << "Error in the file : LengthFactor double defined" << endl;
399 } // end of LengthUnit
400 else if (aUnit->IsKind(STANDARD_TYPE(StepBasic_ConversionBasedUnitAndPlaneAngleUnit))||
401 aUnit->IsKind(STANDARD_TYPE(StepBasic_SiUnitAndPlaneAngleUnit))) {
402 planeAngleFactor = parameter;
403 planeAngleDone = Standard_True;
404 } // end of PlaneAngleUnit
405 else if (aUnit->IsKind(STANDARD_TYPE(StepBasic_ConversionBasedUnitAndSolidAngleUnit))||
406 aUnit->IsKind(STANDARD_TYPE(StepBasic_SiUnitAndSolidAngleUnit))) {
407 solidAngleFactor = parameter;
408 solidAngleDone = Standard_True;
409 } // end of SolidAngleUnit
410 else if (aUnit->IsKind(STANDARD_TYPE(StepBasic_ConversionBasedUnitAndAreaUnit)) ||
411 aUnit->IsKind(STANDARD_TYPE(StepBasic_SiUnitAndAreaUnit))) {
416 af = parameter * 1000. / UnitsMethods::GetCasCadeLengthUnit();
418 areaDone = Standard_True;
419 areaFactor = pow(af,2);
421 else if (aUnit->IsKind(STANDARD_TYPE(StepBasic_ConversionBasedUnitAndVolumeUnit)) ||
422 aUnit->IsKind(STANDARD_TYPE(StepBasic_SiUnitAndVolumeUnit))) {
427 af = parameter * 1000. / UnitsMethods::GetCasCadeLengthUnit();
429 volumeDone = Standard_True;
430 volumeFactor = pow(af,3);
436 // ==========================================================================
437 // Method : STEPConstruct_UnitContext::ComputeTolerance
439 // ==========================================================================
441 Standard_Integer STEPConstruct_UnitContext::ComputeTolerance
442 (const Handle(StepRepr_GlobalUncertaintyAssignedContext)& aContext)
444 Standard_Integer status = 0;
445 // Decode the Uncertainty information (geometric accuracy)
447 hasUncertainty = Standard_False;
448 Standard_Integer nbUncertainty = 0;
450 if (!aContext.IsNull()) nbUncertainty = aContext->NbUncertainty();
453 for (Standard_Integer un = 1 ; un <= nbUncertainty ; un ++) {
454 Handle(StepBasic_UncertaintyMeasureWithUnit) aUMWU = aContext->UncertaintyValue(un);
455 if (aUMWU.IsNull()) {
457 cout<<"BAD Uncertainty Measure with Units, n0."<<un<<endl;
461 // Decode the associated Unit
462 Handle(StepBasic_SiUnitAndLengthUnit) aUnit =
463 Handle(StepBasic_SiUnitAndLengthUnit)::DownCast(aUMWU->UnitComponent().NamedUnit());
464 if (!aUnit.IsNull()) {
465 // Extract Uncertainty value
466 Standard_Real LengthUncertainty = aUMWU->ValueComponent();
467 // Update it according to the Length Unit Factor
468 //pdn r_47-sd.stp to choose minimal uncertainty
469 if(theUncertainty > LengthUncertainty) theUncertainty = LengthUncertainty;
470 hasUncertainty = Standard_True;
473 Handle(StepBasic_ConversionBasedUnitAndLengthUnit) aCBULU =
474 Handle(StepBasic_ConversionBasedUnitAndLengthUnit)::DownCast(aUMWU->UnitComponent().NamedUnit());
475 if (!aCBULU.IsNull()) {
476 // Extract Uncertainty value
477 Standard_Real LengthUncertainty = aUMWU->ValueComponent();
478 // Update it according to the Length Unit Factor
479 //pdn r_47-sd.stp to choose minimal uncertainty
480 if(theUncertainty > LengthUncertainty) theUncertainty = LengthUncertainty; // *lengthFactor; fait par appelant
481 hasUncertainty = Standard_True;
487 if (hasUncertainty) cout << "UNCERTAINTY read as " << theUncertainty << endl;
492 // ==========================================================================
493 // Method : STEPConstruct_UnitContext::LengthFactor
495 // ==========================================================================
496 Standard_Real STEPConstruct_UnitContext::LengthFactor() const
497 { return lengthFactor; }
499 // ==========================================================================
500 // Method : STEPConstruct_UnitContext::PlaneAngleFactor
502 // ==========================================================================
503 Standard_Real STEPConstruct_UnitContext::PlaneAngleFactor() const
504 { return planeAngleFactor; }
506 // ==========================================================================
507 // Method : STEPConstruct_UnitContext::SolidAngleFactor
509 // ==========================================================================
510 Standard_Real STEPConstruct_UnitContext::SolidAngleFactor() const
511 { return solidAngleFactor; }
513 // ==========================================================================
514 // Method : STEPConstruct_UnitContext::Uncertainty
516 // ==========================================================================
517 Standard_Real STEPConstruct_UnitContext::Uncertainty () const
518 { return theUncertainty; }
520 // ==========================================================================
521 // Method : STEPConstruct_UnitContext::HasUncertainty
523 // ==========================================================================
524 Standard_Boolean STEPConstruct_UnitContext::HasUncertainty () const
525 { return hasUncertainty; }
527 // ==========================================================================
528 // Method : STEPConstruct_UnitContext::LengthDone
530 // ==========================================================================
531 Standard_Boolean STEPConstruct_UnitContext::LengthDone() const
532 { return lengthDone; }
534 // ==========================================================================
535 // Method : STEPConstruct_UnitContext::PlaneAngleDone
537 // ==========================================================================
538 Standard_Boolean STEPConstruct_UnitContext::PlaneAngleDone() const
539 { return planeAngleDone; }
541 // ==========================================================================
542 // Method : STEPConstruct_UnitContext::SolidAngleDone
544 // ==========================================================================
545 Standard_Boolean STEPConstruct_UnitContext::SolidAngleDone() const
546 { return solidAngleDone; }
548 // ==========================================================================
549 // Method : STEPConstruct_UnitContext::StatusMessage
551 // ==========================================================================
552 Standard_CString STEPConstruct_UnitContext::StatusMessage (const Standard_Integer status) const
556 case 1 : return "No GlobalUnitAssignedContext, default taken";
557 case 2 : return "No LengthMeasureWithUnit, default taken";
558 case 3 : return "No SiUnit for LengthMeasure undefined, default taken";
559 case 4 : return "No PlaneAngleMeasureWithUnit, default taken";
560 case 5 : return "No SiUnit for PlaneAngleMeasure undefined, default taken";
561 case 6 : return "No SolidAngleMeasureWithUnit, default taken";
562 case 7 : return "No SiUnit for SolidAngleMeasure undefined, default taken";
564 case 11 : return "Length Unit not recognized, default taken";
565 case 12 : return "Plane Angle Unit not recognized, default taken";
566 case 13 : return "Solid Angle Unit not recognized, default taken";
567 case 14 : return "At least one unit is twice defined";
569 case 40 : return "Bad GlobalUncertaintyAssignedContext, default unit taken";
574 return "Badly defined units, default taken";
577 Standard_Real STEPConstruct_UnitContext::AreaFactor() const
582 Standard_Real STEPConstruct_UnitContext::VolumeFactor() const
587 Standard_Boolean STEPConstruct_UnitContext::AreaDone() const
592 Standard_Boolean STEPConstruct_UnitContext::VolumeDone() const