1 // Copyright (c) 1999-2014 OPEN CASCADE SAS
3 // This file is part of Open CASCADE Technology software library.
5 // This library is free software; you can redistribute it and/or modify it under
6 // the terms of the GNU Lesser General Public License version 2.1 as published
7 // by the Free Software Foundation, with special exception defined in the file
8 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
9 // distribution for complete text of the license and disclaimer of any warranty.
11 // Alternatively, this file may be used under the terms of Open CASCADE
12 // commercial license or contractual agreement.
14 #include <STEPControl_Reader.ixx>
15 #include <STEPControl_Controller.hxx>
16 #include <XSControl_Controller.hxx>
17 #include <XSControl_TransferReader.hxx>
18 #include <Interface_ShareFlags.hxx>
20 #include <STEPControl_ActorRead.hxx>
22 #include <StepBasic_ProductDefinition.hxx>
23 #include <Interface_EntityIterator.hxx>
24 #include <Interface_Graph.hxx>
25 #include <Interface_Static.hxx>
26 #include <StepRepr_NextAssemblyUsageOccurrence.hxx>
27 #include <StepBasic_ProductDefinitionContext.hxx>
28 #include <StepBasic_ApplicationContext.hxx>
29 #include <TCollection_AsciiString.hxx>
30 #include <StepBasic_ProductDefinitionWithAssociatedDocuments.hxx>
31 #include <StepBasic_DocumentProductEquivalence.hxx>
32 #include <StepShape_ShapeDefinitionRepresentation.hxx>
33 #include <StepShape_ShapeRepresentation.hxx>
34 #include <StepRepr_PropertyDefinition.hxx>
35 #include <StepRepr_RepresentationRelationship.hxx>
36 #include <StepRepr_ShapeAspect.hxx>
37 #include <StepRepr_ProductDefinitionShape.hxx>
38 #include <StepRepr_NextAssemblyUsageOccurrence.hxx>
39 #include <StepRepr_RepresentationMap.hxx>
40 #include <StepRepr_MappedItem.hxx>
41 #include <Transfer_TransientProcess.hxx>
42 #include <TColStd_HSequenceOfTransient.hxx>
43 #include <TColStd_MapOfAsciiString.hxx>
44 #include <StepBasic_SiUnitName.hxx>
45 #include <StepBasic_SiPrefix.hxx>
46 #include <StepRepr_GlobalUnitAssignedContext.hxx>
47 #include <StepRepr_RepresentationContext.hxx>
48 #include <StepRepr_GlobalUncertaintyAssignedContext.hxx>
49 #include <StepRepr_GlobalUncertaintyAssignedContext.hxx>
50 #include <StepGeom_GeometricRepresentationContextAndGlobalUnitAssignedContext.hxx>
51 #include <StepGeom_GeomRepContextAndGlobUnitAssCtxAndGlobUncertaintyAssCtx.hxx>
52 #include <StepBasic_HArray1OfNamedUnit.hxx>
53 #include <StepBasic_NamedUnit.hxx>
54 #include <StepBasic_ConversionBasedUnit.hxx>
55 #include <StepBasic_SiUnitAndLengthUnit.hxx>
56 #include <StepBasic_SiUnitAndLengthUnit.hxx>
57 #include <StepBasic_SolidAngleUnit.hxx>
58 #include <StepBasic_MeasureWithUnit.hxx>
59 #include <StepBasic_LengthMeasureWithUnit.hxx>
60 #include <StepBasic_PlaneAngleMeasureWithUnit.hxx>
61 #include <StepBasic_SolidAngleMeasureWithUnit.hxx>
62 #include <STEPConstruct_UnitContext.hxx>
63 #include <StepBasic_SiUnitAndPlaneAngleUnit.hxx>
64 #include <StepBasic_SiUnitAndSolidAngleUnit.hxx>
65 #include <XSControl_WorkSession.hxx>
66 #include <StepData_StepModel.hxx>
67 #include <TColStd_SequenceOfAsciiString.hxx>
68 #include <StepRepr_RepresentationContext.hxx>
69 #include <TColStd_Array1OfAsciiString.hxx>
70 #include <TColStd_Array1OfReal.hxx>
72 //=======================================================================
73 //function : STEPControl_Reader
75 //=======================================================================
77 STEPControl_Reader::STEPControl_Reader ()
79 STEPControl_Controller::Init();
83 //=======================================================================
84 //function : STEPControl_Reader
86 //=======================================================================
88 STEPControl_Reader::STEPControl_Reader
89 (const Handle(XSControl_WorkSession)& WS, const Standard_Boolean scratch)
91 STEPControl_Controller::Init();
96 //=======================================================================
97 //function : StepModel
99 //=======================================================================
101 Handle(StepData_StepModel) STEPControl_Reader::StepModel () const
103 return Handle(StepData_StepModel)::DownCast(Model());
106 //=======================================================================
107 //function : TransferRoot
109 //=======================================================================
111 Standard_Boolean STEPControl_Reader::TransferRoot (const Standard_Integer num)
113 return TransferOneRoot (num);
116 //=======================================================================
117 //function : NbRootsForTransfer
119 //=======================================================================
121 Standard_Integer STEPControl_Reader::NbRootsForTransfer()
123 if (therootsta) return theroots.Length();
124 therootsta = Standard_True;
127 Standard_Integer nb = Model()->NbEntities();
128 for (Standard_Integer i = 1; i <= nb; i ++) {
129 Handle(Standard_Transient) ent = Model()->Value(i);
130 if(ent->IsKind(STANDARD_TYPE(StepBasic_ProductDefinition))) {
131 // PTV 31.01.2003 TRJ11 exclude Product Definition With Associated Document from roots
132 if (ent->IsKind(STANDARD_TYPE(StepBasic_ProductDefinitionWithAssociatedDocuments))) {
133 // check if PDWAD-> PDF <-Document_Product_Equivalence.
134 Standard_Boolean iSexclude = Standard_False;
135 Handle(StepBasic_ProductDefinitionWithAssociatedDocuments) PDWAD =
136 Handle(StepBasic_ProductDefinitionWithAssociatedDocuments)::DownCast(ent);
137 Interface_EntityIterator PDWADsubs = WS()->Graph().Shareds(PDWAD);
138 for (PDWADsubs.Start(); PDWADsubs.More(); PDWADsubs.Next()) {
139 if ( !PDWADsubs.Value()->IsKind(STANDARD_TYPE(StepBasic_ProductDefinitionFormation)))
141 Handle(StepBasic_ProductDefinitionFormation) localPDF =
142 Handle(StepBasic_ProductDefinitionFormation)::DownCast(PDWADsubs.Value());
143 Interface_EntityIterator PDFsubs = WS()->Graph().Sharings(localPDF);
144 for( PDFsubs.Start(); PDFsubs.More(); PDFsubs.Next() )
145 if (PDFsubs.Value()->IsKind(STANDARD_TYPE(StepBasic_DocumentProductEquivalence))) {
146 iSexclude = Standard_True;
154 cout << "Warning: STEPControl_Reader::NbRootsForTransfer exclude PDWAD from roots" << endl;
159 Handle(StepBasic_ProductDefinition) PD =
160 Handle(StepBasic_ProductDefinition)::DownCast(ent);
161 Standard_Boolean IsRoot = Standard_True;
162 const Interface_Graph& graph = WS()->Graph();
163 // determinate roots used NextAssemblyUsageOccurrence
164 Interface_EntityIterator subs = graph.Sharings(PD);
165 for(subs.Start(); subs.More(); subs.Next()) {
166 Handle(StepRepr_NextAssemblyUsageOccurrence) NAUO =
167 Handle(StepRepr_NextAssemblyUsageOccurrence)::DownCast(subs.Value());
168 if (NAUO.IsNull()) continue;
169 if (PD==NAUO->RelatedProductDefinition()) IsRoot=Standard_False;
171 // determinate roots used ProductDefinitionContext
173 const char *str1 = Interface_Static::CVal("read.step.product.context");
174 Standard_Integer ICS = Interface_Static::IVal("read.step.product.context");
176 subs = graph.Shareds(PD);
177 for(subs.Start(); subs.More(); subs.Next()) {
178 Handle(StepBasic_ProductDefinitionContext) PDC =
179 Handle(StepBasic_ProductDefinitionContext)::DownCast(subs.Value());
180 if (PDC.IsNull()) continue;
181 const char *str2 = PDC->LifeCycleStage()->String().ToCString();
182 const char *str3 = PDC->Name()->String().ToCString();
183 if( !( strcasecmp(str1,str2)==0 || strcasecmp(str1,str3)==0 ) )
184 IsRoot=Standard_False;
188 // determinate roots used ProductDefinitionFormationRelationship
189 //subs = graph.Shareds(PD);
190 //for(subs.Start(); subs.More(); subs.Next()) {
191 // Handle(StepBasic_ProductDefinitionFormation) PDF =
192 // Handle(StepBasic_ProductDefinitionFormation)::DownCast(subs.Value());
193 // if (PDF.IsNull()) continue;
194 // Interface_EntityIterator subs1 = graph.Sharings(PDF);
195 // for(subs1.Start(); subs1.More(); subs1.Next()) {
196 // Handle(StepBasic_ProductDefinitionFormationRelationship) PDFR =
197 // Handle(StepBasic_ProductDefinitionFormationRelationship)::DownCast(subs1.Value());
198 // if (PDFR.IsNull()) continue;
199 // if (PDF==PDFR->RelatedProductDefinition()) IsRoot=Standard_False;
203 theroots.Append(ent);
204 WS()->MapReader()->RootsForTransfer()->Append(ent);
207 TCollection_AsciiString aProdMode = Interface_Static::CVal("read.step.product.mode");
208 if(!aProdMode.IsEqual("ON")) {
209 if(ent->IsKind(STANDARD_TYPE(StepShape_ShapeDefinitionRepresentation))) {
210 Standard_Boolean IsRoot = Standard_True;
211 Handle(StepShape_ShapeDefinitionRepresentation) SDR =
212 Handle(StepShape_ShapeDefinitionRepresentation)::DownCast(ent);
213 Handle(StepRepr_PropertyDefinition) PropDef = SDR->Definition().PropertyDefinition();
214 if(!PropDef.IsNull()) {
215 Handle(StepBasic_ProductDefinition) PD = PropDef->Definition().ProductDefinition();
216 if(!PD.IsNull()) IsRoot = Standard_False;
218 Handle(StepRepr_ShapeAspect) SA = PropDef->Definition().ShapeAspect();
220 Handle(StepRepr_ProductDefinitionShape) PDS = SA->OfShape();
221 PD = PDS->Definition().ProductDefinition();
222 if(!PD.IsNull()) IsRoot = Standard_False;
226 Handle(StepRepr_NextAssemblyUsageOccurrence) NAUO =
227 Handle(StepRepr_NextAssemblyUsageOccurrence)::DownCast(PropDef->Definition().ProductDefinitionRelationship());
228 if(!NAUO.IsNull()) IsRoot = Standard_False;
231 Handle(StepShape_ShapeRepresentation) SR =
232 Handle(StepShape_ShapeRepresentation)::DownCast(SDR->UsedRepresentation());
233 if(SR.IsNull()) IsRoot = Standard_False;
237 theroots.Append(ent);
238 WS()->MapReader()->RootsForTransfer()->Append(ent);
241 if(ent->IsKind(STANDARD_TYPE(StepShape_ShapeRepresentation))) {
242 Standard_Boolean IsRoot = Standard_True;
243 Handle(StepShape_ShapeRepresentation) SR =
244 Handle(StepShape_ShapeRepresentation)::DownCast(ent);
245 const Interface_Graph& graph = WS()->Graph();
246 Interface_EntityIterator subs = graph.Sharings(SR);
247 for(subs.Start(); subs.More() && IsRoot; subs.Next()) {
248 Handle(StepShape_ShapeDefinitionRepresentation) SDR =
249 Handle(StepShape_ShapeDefinitionRepresentation)::DownCast(subs.Value());
250 if(!SDR.IsNull()) IsRoot = Standard_False;
252 Handle(StepRepr_RepresentationRelationship) RR =
253 Handle(StepRepr_RepresentationRelationship)::DownCast(subs.Value());
255 Handle(StepShape_ShapeRepresentation) SR2 =
256 Handle(StepShape_ShapeRepresentation)::DownCast(RR->Rep1());
258 SR2 = Handle(StepShape_ShapeRepresentation)::DownCast(RR->Rep2());
259 Interface_EntityIterator subs2 = graph.Sharings(SR2);
260 for(subs2.Start(); subs2.More(); subs2.Next()) {
261 Handle(StepShape_ShapeDefinitionRepresentation) SDR2 =
262 Handle(StepShape_ShapeDefinitionRepresentation)::DownCast(subs2.Value());
263 if(!SDR2.IsNull()) IsRoot = Standard_False;
265 // if(SR==SRR->Rep2()) IsRoot = Standard_False;
271 Handle(StepRepr_RepresentationMap) RM =
272 Handle(StepRepr_RepresentationMap)::DownCast(subs.Value());
274 Interface_EntityIterator subs2 = graph.Sharings(RM);
275 for(subs2.Start(); subs2.More(); subs2.Next()) {
276 Handle(StepRepr_MappedItem) MI = Handle(StepRepr_MappedItem)::DownCast(subs2.Value());
278 Interface_EntityIterator subs3 = graph.Sharings(MI);
279 for(subs3.Start(); subs3.More(); subs3.Next()) {
280 Handle(StepShape_ShapeRepresentation) SR2 =
281 Handle(StepShape_ShapeRepresentation)::DownCast(subs3.Value());
282 if(!SR2.IsNull()) IsRoot = Standard_False;
290 theroots.Append(ent);
291 WS()->MapReader()->RootsForTransfer()->Append(ent);
299 return theroots.Length();
302 //=======================================================================
303 //function : FileUnits
305 //=======================================================================
307 void STEPControl_Reader::FileUnits( TColStd_SequenceOfAsciiString& theUnitLengthNames,
308 TColStd_SequenceOfAsciiString& theUnitAngleNames,
309 TColStd_SequenceOfAsciiString& theUnitSolidAngleNames)
311 Standard_Integer nbroots = NbRootsForTransfer();
320 const Interface_Graph& graph = WS()->Graph();
321 TColStd_MapOfAsciiString aMapUnits[3];
323 Standard_Integer i =1;
324 for( ; i <= nbroots; i++)
326 Handle(Standard_Transient) anEnt = theroots(i);
327 Standard_Integer num = graph.EntityNumber(anEnt);
330 Handle(StepBasic_ProductDefinition) aProdDef =
331 Handle(StepBasic_ProductDefinition)::DownCast(anEnt);
332 Handle(StepShape_ShapeDefinitionRepresentation) aShapeDefRepr;
333 if(!aProdDef.IsNull())
335 Interface_EntityIterator subsPD = graph.Sharings(aProdDef);
336 for(subsPD.Start(); subsPD.More() && aShapeDefRepr.IsNull(); subsPD.Next())
338 Handle(StepRepr_ProductDefinitionShape) aProdDefShape =
339 Handle(StepRepr_ProductDefinitionShape)::DownCast(subsPD.Value());
340 if(aProdDefShape.IsNull())
342 Interface_EntityIterator subsSR = graph.Sharings(aProdDefShape);
343 Handle(StepShape_ShapeRepresentation) SR;
344 for(subsSR.Start(); subsSR.More() && aShapeDefRepr.IsNull(); subsSR.Next())
346 Handle(StepShape_ShapeDefinitionRepresentation) aCurShapeDefRepr =
347 Handle(StepShape_ShapeDefinitionRepresentation)::DownCast(subsSR.Value());
348 if(aCurShapeDefRepr.IsNull())
350 Handle(StepRepr_Representation) aUseRepr = aCurShapeDefRepr->UsedRepresentation();
351 if(aUseRepr.IsNull())
353 Handle(StepShape_ShapeRepresentation) aShapeRepr =
354 Handle(StepShape_ShapeRepresentation)::DownCast(aUseRepr);
355 if(aShapeRepr.IsNull())
357 aShapeDefRepr = aCurShapeDefRepr;
366 aShapeDefRepr = Handle(StepShape_ShapeDefinitionRepresentation)::DownCast(anEnt);
367 if(!aShapeDefRepr.IsNull())
369 Handle(StepShape_ShapeRepresentation) aShapeRepr =
370 Handle(StepShape_ShapeRepresentation)::DownCast(aShapeDefRepr->UsedRepresentation());
371 Handle(StepRepr_RepresentationContext) aRepCont = aShapeRepr->ContextOfItems();
372 if (aRepCont.IsNull())
374 TColStd_Array1OfAsciiString aNameUnits(1,3);
375 TColStd_Array1OfReal aFactorUnits(1,3);
376 if(findUnits(aRepCont,aNameUnits,aFactorUnits))
378 Standard_Integer k = LENGTH;
379 for ( ; k <= SOLID_ANGLE ; k++)
381 if(!aMapUnits[k].Contains(aNameUnits(k+1)))
383 aMapUnits[k].Add(aNameUnits(k+1));
384 TColStd_SequenceOfAsciiString& anUnitSeq = (k == LENGTH ?
385 theUnitLengthNames : ( k == ANLGE ? theUnitAngleNames : theUnitSolidAngleNames ));
386 anUnitSeq.Append(aNameUnits(k+1));
394 //for case when units was not found through PDF or SDR
395 if(theUnitLengthNames.IsEmpty())
397 Handle(Interface_InterfaceModel) aModel = WS()->Model();
400 Standard_Integer i = 1, nb = aModel->NbEntities();
403 Handle(Standard_Transient) anEnt = aModel->Value(i);
404 Handle(StepRepr_RepresentationContext) aRepCont = Handle(StepRepr_RepresentationContext)::DownCast(anEnt);
405 if (aRepCont.IsNull())
407 TColStd_Array1OfAsciiString aNameUnits(1,3);
408 TColStd_Array1OfReal aFactorUnits(1,3);
409 if(findUnits(aRepCont,aNameUnits,aFactorUnits))
411 Standard_Integer k = LENGTH;
412 for ( ; k <= SOLID_ANGLE ; k++)
414 if(!aMapUnits[k].Contains(aNameUnits(k+1)))
416 aMapUnits[k].Add(aNameUnits(k+1));
417 TColStd_SequenceOfAsciiString& anUnitSeq = (k == LENGTH ?
418 theUnitLengthNames : ( k == ANLGE ? theUnitAngleNames : theUnitSolidAngleNames ));
419 anUnitSeq.Append(aNameUnits(k+1));
427 //=======================================================================
428 //function : getSiName
430 //=======================================================================
432 inline static TCollection_AsciiString getSiName(const Handle(StepBasic_SiUnit)& theUnit)
435 TCollection_AsciiString aName;
436 if (theUnit->HasPrefix()) {
437 switch (theUnit->Prefix()) {
438 case StepBasic_spExa: aName += "exa"; break;
439 case StepBasic_spPeta: aName += "peta"; break;
440 case StepBasic_spTera: aName += "tera"; break;
441 case StepBasic_spGiga: aName += "giga"; break;
442 case StepBasic_spMega: aName += "mega"; break;
443 case StepBasic_spHecto: aName += "hecto"; break;
444 case StepBasic_spDeca: aName += "deca"; break;
445 case StepBasic_spDeci: aName += "deci"; break;
447 case StepBasic_spPico: aName += "pico"; break;
448 case StepBasic_spFemto: aName += "femto"; break;
449 case StepBasic_spAtto: aName += "atto"; break;
451 case StepBasic_spKilo : aName += "kilo"; break;
452 case StepBasic_spCenti :aName += "centi"; break;
453 case StepBasic_spMilli :aName += "milli"; break;
454 case StepBasic_spMicro :aName += "micro"; break;
455 case StepBasic_spNano :aName += "nano"; break;
460 switch(theUnit->Name()) {
461 case StepBasic_sunMetre : aName += "metre"; break;
462 case StepBasic_sunRadian : aName += "radian"; break;
463 case StepBasic_sunSteradian : aName += "steradian"; break;
469 //=======================================================================
470 //function : findUnits
472 //=======================================================================
474 Standard_Boolean STEPControl_Reader::findUnits(
475 const Handle(StepRepr_RepresentationContext)& theRepCont,
476 TColStd_Array1OfAsciiString& theNameUnits,
477 TColStd_Array1OfReal& theFactorUnits)
479 Handle(StepRepr_GlobalUnitAssignedContext) aContext;
480 Handle(StepRepr_GlobalUncertaintyAssignedContext) aTol;
482 if (theRepCont->IsKind(STANDARD_TYPE(StepGeom_GeometricRepresentationContextAndGlobalUnitAssignedContext))) {
483 aContext = Handle(StepGeom_GeometricRepresentationContextAndGlobalUnitAssignedContext)::
484 DownCast(theRepCont)->GlobalUnitAssignedContext();
488 if (theRepCont->IsKind(STANDARD_TYPE(StepGeom_GeomRepContextAndGlobUnitAssCtxAndGlobUncertaintyAssCtx))) {
489 aContext = Handle(StepGeom_GeomRepContextAndGlobUnitAssCtxAndGlobUncertaintyAssCtx)::
490 DownCast(theRepCont)->GlobalUnitAssignedContext();
493 if(aContext.IsNull())
494 return Standard_False;
496 Handle(StepBasic_HArray1OfNamedUnit) anUnits = aContext->Units();
497 Standard_Integer nbU = aContext->NbUnits();
498 Standard_Integer nbFind = 0;
499 for (Standard_Integer i = 1; i <= nbU; i++) {
500 Handle(StepBasic_NamedUnit) aNamedUnit = aContext->UnitsValue(i);
501 Handle(StepBasic_ConversionBasedUnit) aConvUnit =
502 Handle(StepBasic_ConversionBasedUnit)::DownCast(aNamedUnit);
503 Standard_Integer anInd = 0;
504 TCollection_AsciiString aName;
505 Standard_Real anUnitFact = 0;
506 if( !aConvUnit.IsNull() )
508 Handle(StepBasic_MeasureWithUnit) aMeasWithUnit =
509 aConvUnit->ConversionFactor();
511 if(aMeasWithUnit.IsNull())
514 if( aMeasWithUnit->IsKind(STANDARD_TYPE(StepBasic_LengthMeasureWithUnit)) )
516 else if( aMeasWithUnit->IsKind(STANDARD_TYPE(StepBasic_PlaneAngleMeasureWithUnit)) )
518 else if( aMeasWithUnit->IsKind(STANDARD_TYPE(StepBasic_SolidAngleMeasureWithUnit)) )
522 aName = aConvUnit->Name()->String();
523 anUnitFact = aMeasWithUnit->ValueComponent();
527 Handle(StepBasic_SiUnit) aSiUnit = Handle(StepBasic_SiUnit)::DownCast(aNamedUnit);
528 if( aSiUnit.IsNull())
530 if(aSiUnit->IsKind(STANDARD_TYPE(StepBasic_SiUnitAndLengthUnit)))
532 else if(aSiUnit->IsKind(STANDARD_TYPE(StepBasic_SiUnitAndPlaneAngleUnit)))
534 else if(aSiUnit->IsKind(STANDARD_TYPE(StepBasic_SiUnitAndSolidAngleUnit)))
538 anUnitFact = (!aSiUnit->HasPrefix() ?
539 1. : STEPConstruct_UnitContext::ConvertSiPrefix(aSiUnit->Prefix()));
540 aName = getSiName(aSiUnit);
547 theNameUnits.SetValue(anInd, aName);
548 theFactorUnits.SetValue(anInd, anUnitFact);