0023736: Exeption during reading STEP file in Test Harness
[occt.git] / src / STEPCAFControl / STEPCAFControl_Reader.cxx
CommitLineData
b311480e 1// Created on: 2000-08-15
2// Created by: Andrey BETENEV
3// Copyright (c) 2000-2012 OPEN CASCADE SAS
4//
5// The content of this file is subject to the Open CASCADE Technology Public
6// License Version 6.5 (the "License"). You may not use the content of this file
7// except in compliance with the License. Please obtain a copy of the License
8// at http://www.opencascade.org and read it completely before using this file.
9//
10// The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
11// main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
12//
13// The Original Code and all software distributed under the License is
14// distributed on an "AS IS" basis, without warranty of any kind, and the
15// Initial Developer hereby disclaims all such warranties, including without
16// limitation, any warranties of merchantability, fitness for a particular
17// purpose or non-infringement. Please see the License for the specific terms
18// and conditions governing the rights and limitations under the License.
19
7fd59977 20
21#include <STEPCAFControl_Reader.ixx>
22
23#include <Quantity_Color.hxx>
24#include <TCollection_HAsciiString.hxx>
25#include <TopoDS_Shape.hxx>
26#include <TopoDS_Iterator.hxx>
27#include <TopTools_MapOfShape.hxx>
28#include <TopoDS_Compound.hxx>
29
30#include <Transfer_TransientProcess.hxx>
31#include <TransferBRep.hxx>
32#include <Transfer_Binder.hxx>
33#include <Interface_InterfaceModel.hxx>
34#include <XSControl_TransferReader.hxx>
35
36#include <StepBasic_Product.hxx>
37#include <StepBasic_ProductDefinition.hxx>
38#include <StepBasic_ProductDefinitionRelationship.hxx>
39#include <StepBasic_ProductDefinitionFormation.hxx>
40#include <StepShape_ContextDependentShapeRepresentation.hxx>
41#include <StepShape_ShapeDefinitionRepresentation.hxx>
42#include <StepShape_ShapeDefinitionRepresentation.hxx>
43#include <StepRepr_ProductDefinitionShape.hxx>
44#include <StepRepr_PropertyDefinition.hxx>
45#include <StepRepr_ShapeAspect.hxx>
46#include <StepRepr_MeasureRepresentationItem.hxx>
47#include <StepRepr_DescriptiveRepresentationItem.hxx>
02a0b964 48#include <StepRepr_SequenceOfRepresentationItem.hxx>
7fd59977 49#include <StepVisual_StyledItem.hxx>
50#include <StepAP214_AppliedExternalIdentificationAssignment.hxx>
51
52#include <STEPConstruct.hxx>
53#include <STEPConstruct_Styles.hxx>
54#include <STEPConstruct_ExternRefs.hxx>
55#include <STEPConstruct_UnitContext.hxx>
56#include <STEPCAFControl_Controller.hxx>
57#include <STEPCAFControl_DataMapOfSDRExternFile.hxx>
58
59#include <TDataStd_Name.hxx>
60#include <TDF_Label.hxx>
61#include <XCAFDoc_ColorTool.hxx>
62#include <XCAFDoc_ShapeTool.hxx>
63#include <XCAFDoc_DocumentTool.hxx>
64#include <XCAFDoc_DimTolTool.hxx>
65#include <XCAFDoc_MaterialTool.hxx>
66#include <XCAFDoc_DataMapOfShapeLabel.hxx>
67#include <STEPConstruct_ValidationProps.hxx>
68#include <StepRepr_Representation.hxx>
69#include <XCAFDoc_Area.hxx>
70#include <XCAFDoc_Volume.hxx>
71#include <XCAFDoc_Centroid.hxx>
72#include <StepVisual_PresentationLayerAssignment.hxx>
73#include <TColStd_HSequenceOfTransient.hxx>
74#include <StepVisual_LayeredItem.hxx>
75#include <XCAFDoc_LayerTool.hxx>
76#include <Interface_EntityIterator.hxx>
77#include <StepRepr_ShapeRepresentationRelationship.hxx>
78#include <STEPConstruct_Assembly.hxx>
79#include <TDF_Tool.hxx>
80#include <StepVisual_Invisibility.hxx>
81#include <TDataStd_UAttribute.hxx>
82#include <XCAFDoc.hxx>
83#include <OSD_Path.hxx>
84#include <TColStd_SequenceOfHAsciiString.hxx>
85
86#include <TDataStd_TreeNode.hxx>
87#include <TNaming_NamedShape.hxx>
88#include <BRep_Builder.hxx>
89
90#include <STEPCAFControl_DataMapOfShapePD.hxx>
91#include <STEPCAFControl_DataMapOfPDExternFile.hxx>
92#include <StepVisual_PresentationStyleByContext.hxx>
93#include <StepVisual_StyleContextSelect.hxx>
94#include <StepRepr_RepresentedDefinition.hxx>
95#include <StepRepr_CharacterizedDefinition.hxx>
96#include <StepRepr_SpecifiedHigherUsageOccurrence.hxx>
97#include <XCAFDoc_GraphNode.hxx>
98#include <STEPCAFControl_Reader.hxx>
99
100// skl 21.08.2003 for reading G&DT
101#include <StepShape_DimensionalSize.hxx>
102#include <StepDimTol_GeometricTolerance.hxx>
103#include <StepShape_EdgeCurve.hxx>
104#include <StepShape_DimensionalCharacteristicRepresentation.hxx>
105#include <StepShape_ShapeDimensionRepresentation.hxx>
106#include <StepRepr_ValueRange.hxx>
107#include <StepRepr_ReprItemAndLengthMeasureWithUnit.hxx>
108#include <StepBasic_MeasureWithUnit.hxx>
109#include <StepDimTol_GeoTolAndGeoTolWthDatRefAndModGeoTolAndPosTol.hxx>
110#include <StepDimTol_ModifiedGeometricTolerance.hxx>
111#include <StepDimTol_GeometricToleranceWithDatumReference.hxx>
112//#include <StepRepr_CompoundItemDefinition.hxx>
113//#include <StepRepr_CompoundItemDefinitionMember.hxx>
114#include <TColStd_HArray1OfTransient.hxx>
115#include <StepRepr_HArray1OfRepresentationItem.hxx>
116#include <TColStd_IndexedDataMapOfTransientTransient.hxx>
117//#include <StepBasic_ConversionBasedUnit.hxx>
118#include <StepBasic_Unit.hxx>
119#include <StepBasic_NamedUnit.hxx>
120#include <StepBasic_SiUnit.hxx>
121#include <StepBasic_ConversionBasedUnitAndLengthUnit.hxx>
122#include <StepBasic_ConversionBasedUnitAndMassUnit.hxx>
123#include <StepBasic_DerivedUnit.hxx>
124#include <StepBasic_DerivedUnitElement.hxx>
125#include <StepBasic_MeasureValueMember.hxx>
126
127//#include <TDataStd_Real.hxx>
128//#include <TDataStd_Constraint.hxx>
129//#include <TDataStd_ConstraintEnum.hxx>
130//#include <TNaming_Tool.hxx>
131//#include <AIS_InteractiveObject.hxx>
132//#include <TPrsStd_ConstraintTools.hxx>
133//#include <AIS_DiameterDimension.hxx>
134//#include <TPrsStd_Position.hxx>
135//#include <TPrsStd_AISPresentation.hxx>
136//#include <TNaming_Builder.hxx>
137#include <TColStd_HArray1OfReal.hxx>
138#include <StepShape_AdvancedFace.hxx>
139#include <StepDimTol_HArray1OfDatumReference.hxx>
140#include <StepDimTol_DatumReference.hxx>
141#include <StepDimTol_Datum.hxx>
142#include <StepDimTol_DatumFeature.hxx>
143#include <StepRepr_ShapeAspectRelationship.hxx>
144
145#include <StepDimTol_AngularityTolerance.hxx>
146#include <StepDimTol_CircularRunoutTolerance.hxx>
147#include <StepDimTol_CoaxialityTolerance.hxx>
148#include <StepDimTol_ConcentricityTolerance.hxx>
149#include <StepDimTol_ParallelismTolerance.hxx>
150#include <StepDimTol_PerpendicularityTolerance.hxx>
151#include <StepDimTol_SymmetryTolerance.hxx>
152#include <StepDimTol_TotalRunoutTolerance.hxx>
153#include <StepDimTol_CylindricityTolerance.hxx>
154#include <StepDimTol_FlatnessTolerance.hxx>
155#include <StepDimTol_LineProfileTolerance.hxx>
156#include <StepDimTol_PositionTolerance.hxx>
157#include <StepDimTol_RoundnessTolerance.hxx>
158#include <StepDimTol_StraightnessTolerance.hxx>
159#include <StepDimTol_SurfaceProfileTolerance.hxx>
160
161#include <StepShape_SolidModel.hxx>
162#include <StepShape_ShellBasedSurfaceModel.hxx>
163#include <StepShape_GeometricSet.hxx>
1d2b1ccb 164#include <StepBasic_ProductDefinition.hxx>
02a0b964 165#include <NCollection_DataMap.hxx>
166#include <StepShape_ManifoldSolidBrep.hxx>
167#include <Interface_Static.hxx>
168#include <TColStd_MapOfTransient.hxx>
169#include <TColStd_MapIteratorOfMapOfTransient.hxx>
170#include <TopTools_ListOfShape.hxx>
171#include <TopTools_ListIteratorOfListOfShape.hxx>
172#include <STEPCAFControl_DataMapIteratorOfDataMapOfShapePD.hxx>
173#include <StepShape_ClosedShell.hxx>
174#include <StepShape_HArray1OfFace.hxx>
175#include <StepShape_HArray1OfFaceBound.hxx>
176#include <StepShape_Loop.hxx>
177#include <StepShape_EdgeLoop.hxx>
178#include <StepShape_HArray1OfOrientedEdge.hxx>
179#include <StepShape_HArray1OfShell.hxx>
7fd59977 180
02a0b964 181#ifdef DEB
182//! Converts address of the passed shape (TShape) to string.
183//! \param theShape [in] Shape to dump.
184//! \return corresponding string.
185TCollection_AsciiString AddrToString(const TopoDS_Shape& theShape)
186{
187 std::string anAddrStr;
188 std::ostringstream ost;
189 ost << theShape.TShape().Access();
190 anAddrStr = ost.str();
191
192 TCollection_AsciiString aStr =
193 TCollection_AsciiString("[").Cat( anAddrStr.c_str() ).Cat("]");
194
195 return aStr;
196}
197#endif
198
199//=======================================================================
200//function : IsEqual
201//purpose : global function to check equality of topological shapes
202//=======================================================================
203
204inline Standard_Boolean IsEqual(const TopoDS_Shape& theShape1,
205 const TopoDS_Shape& theShape2)
206{
207 return theShape1.IsEqual(theShape2);
208}
7fd59977 209
02a0b964 210//=======================================================================
211//function : AllocateSubLabel
212//purpose :
213//=======================================================================
214
215static TDF_Label AllocateSubLabel(TDF_Label& theRoot)
216{
217 return TDF_TagSource::NewChild(theRoot);
218}
7fd59977 219
220//=======================================================================
221//function : STEPCAFControl_Reader
222//purpose :
223//=======================================================================
224
225STEPCAFControl_Reader::STEPCAFControl_Reader ():
226 myColorMode( Standard_True ),
227 myNameMode ( Standard_True ),
228 myLayerMode( Standard_True ),
229 myPropsMode( Standard_True ),
230 myGDTMode ( Standard_True ),
231 myMatMode ( Standard_True )
232{
233 STEPCAFControl_Controller::Init();
234 myFiles = new STEPCAFControl_DictionaryOfExternFile;
235}
236
237
238//=======================================================================
239//function : STEPCAFControl_Reader
240//purpose :
241//=======================================================================
242
243STEPCAFControl_Reader::STEPCAFControl_Reader (const Handle(XSControl_WorkSession)& WS,
02a0b964 244 const Standard_Boolean scratch) :
7fd59977 245 myColorMode( Standard_True ),
246 myNameMode ( Standard_True ),
247 myLayerMode( Standard_True ),
248 myPropsMode( Standard_True ),
249 myGDTMode ( Standard_True ),
250 myMatMode ( Standard_True )
251{
252 STEPCAFControl_Controller::Init();
253 Init ( WS, scratch );
254}
255
256
257//=======================================================================
258//function : Init
259//purpose :
260//=======================================================================
261
262void STEPCAFControl_Reader::Init (const Handle(XSControl_WorkSession)& WS,
263 const Standard_Boolean scratch)
264{
265// necessary only in Writer, to set good actor: WS->SelectNorm ( "STEP" );
266 myReader.SetWS (WS,scratch);
267 myFiles = new STEPCAFControl_DictionaryOfExternFile;
268}
269
270
271//=======================================================================
272//function : ReadFile
273//purpose :
274//=======================================================================
275
276IFSelect_ReturnStatus STEPCAFControl_Reader::ReadFile (const Standard_CString filename)
277{
278 return myReader.ReadFile ( filename );
279}
280
281
282//=======================================================================
283//function : NbRootsForTransfer
284//purpose :
285//=======================================================================
286
287Standard_Integer STEPCAFControl_Reader::NbRootsForTransfer ()
288{
289 return myReader.NbRootsForTransfer();
290}
291
292
293//=======================================================================
294//function : TransferOneRoot
295//purpose :
296//=======================================================================
297
298Standard_Boolean STEPCAFControl_Reader::TransferOneRoot (const Standard_Integer num,
299 Handle(TDocStd_Document) &doc)
300{
301 TDF_LabelSequence Lseq;
302 return Transfer ( myReader, num, doc, Lseq );
303}
304
305
306//=======================================================================
307//function : Transfer
308//purpose :
309//=======================================================================
310
311Standard_Boolean STEPCAFControl_Reader::Transfer (Handle(TDocStd_Document) &doc)
312{
313 TDF_LabelSequence Lseq;
314 return Transfer ( myReader, 0, doc, Lseq );
315}
316
317
318//=======================================================================
319//function : Perform
320//purpose :
321//=======================================================================
322
323Standard_Boolean STEPCAFControl_Reader::Perform (const Standard_CString filename,
324 Handle(TDocStd_Document) &doc)
325{
326 if ( ReadFile ( filename ) != IFSelect_RetDone ) return Standard_False;
327 return Transfer ( doc );
328}
329
330
331//=======================================================================
332//function : Perform
333//purpose :
334//=======================================================================
335
336Standard_Boolean STEPCAFControl_Reader::Perform (const TCollection_AsciiString &filename,
337 Handle(TDocStd_Document) &doc)
338{
339 if ( ReadFile ( filename.ToCString() ) != IFSelect_RetDone ) return Standard_False;
340 return Transfer ( doc );
341}
342
343
344//=======================================================================
345//function : ExternFiles
346//purpose :
347//=======================================================================
348
349const Handle(STEPCAFControl_DictionaryOfExternFile) &STEPCAFControl_Reader::ExternFiles () const
350{
351 return myFiles;
352}
353
354
355//=======================================================================
356//function : ExternFile
357//purpose :
358//=======================================================================
359
360Standard_Boolean STEPCAFControl_Reader::ExternFile (const Standard_CString name,
361 Handle(STEPCAFControl_ExternFile) &ef) const
362{
363 ef.Nullify();
364 if ( myFiles.IsNull() || ! myFiles->HasItem ( name ) )
365 return Standard_False;
366 ef = myFiles->Item ( name );
367 return Standard_True;
368}
369
370
371//=======================================================================
372//function : Reader
373//purpose :
374//=======================================================================
375
376STEPControl_Reader &STEPCAFControl_Reader::ChangeReader ()
377{
378 return myReader;
379}
380
381
382//=======================================================================
383//function : Reader
384//purpose :
385//=======================================================================
386
387const STEPControl_Reader &STEPCAFControl_Reader::Reader () const
388{
389 return myReader;
390}
391
392
393//=======================================================================
394//function : FillShapesMap
395//purpose : auxiliary: fill a map by all compounds and their components
396//=======================================================================
397
398static void FillShapesMap (const TopoDS_Shape &S, TopTools_MapOfShape &map)
399{
400 TopoDS_Shape S0 = S;
401 TopLoc_Location loc;
402 S0.Location ( loc );
403 map.Add ( S0 );
404 if ( S.ShapeType() != TopAbs_COMPOUND ) return;
405 for ( TopoDS_Iterator it(S); it.More(); it.Next() )
406 FillShapesMap ( it.Value(), map );
407}
408
409
410//=======================================================================
411//function : Transfer
412//purpose : basic working method
413//=======================================================================
414
415Standard_Boolean STEPCAFControl_Reader::Transfer (STEPControl_Reader &reader,
416 const Standard_Integer nroot,
417 Handle(TDocStd_Document) &doc,
418 TDF_LabelSequence &Lseq,
419 const Standard_Boolean asOne)
420{
421 reader.ClearShapes();
422 Standard_Integer i;
423
424 // Read all shapes
425 Standard_Integer num = reader.NbRootsForTransfer();
426 if ( num <=0 ) return Standard_False;
427 if ( nroot ) {
428 if ( nroot > num ) return Standard_False;
429 reader.TransferOneRoot ( num );
430 }
431 else {
432 for ( i=1; i <= num; i++ ) reader.TransferOneRoot ( i );
433 }
434 num = reader.NbShapes();
435 if ( num <=0 ) return Standard_False;
436
437 // Fill a map of (top-level) shapes resulting from that transfer
438 // Only these shapes will be considered further
439 TopTools_MapOfShape ShapesMap, NewShapesMap;
440 for ( i=1; i <= num; i++ ) FillShapesMap ( reader.Shape(i), ShapesMap );
441
442 // Collect information on shapes originating from SDRs
443 // this will be used to distinguish compounds representing assemblies
444 // from the ones representing hybrid models and shape sets
445 STEPCAFControl_DataMapOfShapePD ShapePDMap;
446 STEPCAFControl_DataMapOfPDExternFile PDFileMap;
447 Handle(Interface_InterfaceModel) Model = reader.Model();
448 Handle(Transfer_TransientProcess) TP = reader.WS()->TransferReader()->TransientProcess();
449 Standard_Integer nb = Model->NbEntities();
450
451 Handle(TColStd_HSequenceOfTransient) SeqPDS = new TColStd_HSequenceOfTransient;
452
453 for (i = 1; i <= nb; i ++) {
454 Handle(Standard_Transient) enti = Model->Value(i);
455 if(enti->IsKind(STANDARD_TYPE(StepRepr_ProductDefinitionShape))) {
456 // sequence for acceleration ReadMaterials
457 SeqPDS->Append(enti);
458 }
459 if ( enti->IsKind ( STANDARD_TYPE(StepBasic_ProductDefinition ) ) ) {
460 Handle(StepBasic_ProductDefinition) PD =
461 Handle(StepBasic_ProductDefinition)::DownCast(enti);
462 Standard_Integer index = TP->MapIndex(PD);
463 if ( index >0 ) {
464 Handle(Transfer_Binder) binder = TP->MapItem (index);
465 TopoDS_Shape S = TransferBRep::ShapeResult(binder);
466 if ( ! S.IsNull() && ShapesMap.Contains(S) ) {
467 NewShapesMap.Add(S);
468 ShapePDMap.Bind ( S, PD );
469 Handle(STEPCAFControl_ExternFile) EF;
470 PDFileMap.Bind ( PD, EF );
471 }
472 }
473 }
474 if ( enti->IsKind ( STANDARD_TYPE(StepShape_ShapeRepresentation) ) ) {
475 Standard_Integer index = TP->MapIndex(enti);
476 if ( index >0 ) {
477 Handle(Transfer_Binder) binder = TP->MapItem (index);
478 TopoDS_Shape S = TransferBRep::ShapeResult(binder);
479 if ( ! S.IsNull() && ShapesMap.Contains(S) )
480 NewShapesMap.Add(S);
481 }
482 }
483 }
484
485 // get directory name of the main file
486 OSD_Path mainfile ( reader.WS()->LoadedFile() );
487 mainfile.SetName ( "" );
488 mainfile.SetExtension ( "" );
489 TCollection_AsciiString dpath;
490 mainfile.SystemName ( dpath );
491
492 // Load external references (only for relevant SDRs)
493 // and fill map SDR -> extern file
494 STEPConstruct_ExternRefs ExtRefs ( reader.WS() );
495 ExtRefs.LoadExternRefs();
496 for ( i=1; i <= ExtRefs.NbExternRefs(); i++ ) {
497 // check extern ref format
498 Handle(TCollection_HAsciiString) format = ExtRefs.Format(i);
499 if ( ! format.IsNull() ) {
500 static Handle(TCollection_HAsciiString) ap203 = new TCollection_HAsciiString ( "STEP AP203" );
501 static Handle(TCollection_HAsciiString) ap214 = new TCollection_HAsciiString ( "STEP AP214" );
502 if ( ! format->IsSameString ( ap203, Standard_False ) &&
503 ! format->IsSameString ( ap214, Standard_False ) ) {
504#ifdef DEB
505 cout << "Warning: STEPCAFControl_Reader::Transfer: Extern document is neither STEP AP203 nor AP214" << endl;
506#else
507 continue;
508#endif
509 }
510 }
511#ifdef DEB
512 else cout << "Warning: STEPCAFControl_Reader::Transfer: Extern document format not defined" << endl;
513#endif
514
515 // get and check filename of the current extern ref
516 const Standard_CString filename = ExtRefs.FileName(i);
517
518cout<<"filename="<<filename<<endl;
519
520 if ( ! filename || ! filename[0] ) {
521#ifdef DEB
522 cout << "Warning: STEPCAFControl_Reader::Transfer: Extern reference file name is empty" << endl;
523#endif
524 continue; // not a valid extern ref
525 }
526
527 // compute true path to the extern file
528 TCollection_AsciiString fullname = OSD_Path::AbsolutePath ( dpath, filename );
529 if ( fullname.Length() <= 0 ) fullname = filename;
530
531/*
532 char fullname[1024];
533 char *mainfile = reader.WS()->LoadedFile();
534 if ( ! mainfile ) mainfile = "";
535 Standard_Integer slash = 0;
536 for ( Standard_Integer k=0; mainfile[k]; k++ )
537 if ( mainfile[k] == '/' ) slash = k;
538 strncpy ( fullname, mainfile, slash );
539 sprintf ( &fullname[slash], "%s%s", ( mainfile[0] ? "/" : "" ), filename );
540*/
541
542 // get and check PD associated with the current extern ref
543 Handle(StepBasic_ProductDefinition) PD = ExtRefs.ProdDef(i);
544 if ( PD.IsNull() ) continue; // not a valid extern ref
545 if ( ! PDFileMap.IsBound ( PD ) ) continue; // this PD is not concerned by current transfer
546
547 // read extern file (or use existing data) and record its data
548 Handle(STEPCAFControl_ExternFile) EF =
549 ReadExternFile ( filename, fullname.ToCString(), doc );
550 PDFileMap.Bind ( PD, EF );
551 }
552
553 // and insert them to the document
554 Handle(XCAFDoc_ShapeTool) STool = XCAFDoc_DocumentTool::ShapeTool( doc->Main() );
555 if ( STool.IsNull() ) return Standard_False;
556 XCAFDoc_DataMapOfShapeLabel map;
557 if ( asOne )
558 Lseq.Append ( AddShape ( reader.OneShape(), STool, NewShapesMap, ShapePDMap, PDFileMap, map ) );
559 else {
560 for ( i=1; i <= num; i++ ) {
561 Lseq.Append ( AddShape ( reader.Shape(i), STool, NewShapesMap, ShapePDMap, PDFileMap, map ) );
562 }
563 }
564
565 // read colors
566 if ( GetColorMode() )
567 ReadColors ( reader.WS(), doc, PDFileMap, map );
568
569 // read names
570 if ( GetNameMode() )
571 ReadNames ( reader.WS(), doc, PDFileMap, map );
572
573 // read validation props
574 if ( GetPropsMode() )
575 ReadValProps ( reader.WS(), doc, PDFileMap, map );
576
577 // read layers
578 if ( GetLayerMode() )
579 ReadLayers ( reader.WS(), doc );
580
581 // read SHUO entities from STEP model
582 if ( GetSHUOMode() )
583 ReadSHUOs ( reader.WS(), doc, PDFileMap, map );
584
585 // read GDT entities from STEP model
586 if(GetGDTMode())
587 ReadGDTs(reader.WS(),doc);
588
589 // read Material entities from STEP model
590 if(GetMatMode())
591 ReadMaterials(reader.WS(),doc,SeqPDS);
592
02a0b964 593 // Expand resulting CAF structure for sub-shapes (optionally with their
594 // names) if requested
595 ExpandSubShapes(STool, map, ShapePDMap);
596
7fd59977 597 return Standard_True;
598}
599
600//=======================================================================
601//function : AddShape
602//purpose :
603//=======================================================================
604
605TDF_Label STEPCAFControl_Reader::AddShape (const TopoDS_Shape &S,
606 const Handle(XCAFDoc_ShapeTool) &STool,
607 const TopTools_MapOfShape &NewShapesMap,
608 const STEPCAFControl_DataMapOfShapePD &ShapePDMap,
609 const STEPCAFControl_DataMapOfPDExternFile &PDFileMap,
610 XCAFDoc_DataMapOfShapeLabel &ShapeLabelMap) const
611{
612 // if shape has already been mapped, just return corresponding label
613 if ( ShapeLabelMap.IsBound ( S ) ) {
614 return ShapeLabelMap.Find ( S );
615 }
616
617 // if shape is located, create instance
618 if ( ! S.Location().IsIdentity() ) {
619 TopoDS_Shape S0 = S;
620 TopLoc_Location loc;
621 S0.Location ( loc );
622 AddShape ( S0, STool, NewShapesMap, ShapePDMap, PDFileMap, ShapeLabelMap );
623 TDF_Label L = STool->AddShape ( S, Standard_False ); // should create reference
624 ShapeLabelMap.Bind ( S, L );
625 return L;
626 }
627
628 // if shape is not compound, simple add it
629 if ( S.ShapeType() != TopAbs_COMPOUND ) {
630 TDF_Label L = STool->AddShape ( S, Standard_False );
631 ShapeLabelMap.Bind ( S, L );
632 return L;
633 }
634
635 // for compounds, compute number of subshapes and check whether this is assembly
636 Standard_Boolean isAssembly = Standard_False;
637 Standard_Integer nbComponents = 0;
638 TopoDS_Iterator it;
639 for ( it.Initialize(S); it.More(); it.Next(), nbComponents++ ) {
640 TopoDS_Shape Sub0 = it.Value();
641 TopLoc_Location loc;
642 Sub0.Location ( loc );
643 if ( NewShapesMap.Contains ( Sub0 ) ) isAssembly = Standard_True;
644 }
645
646// if(nbComponents>0) isAssembly = Standard_True;
647
648 // check whether it has associated external ref
649 TColStd_SequenceOfHAsciiString SHAS;
650 if ( ShapePDMap.IsBound ( S ) && PDFileMap.IsBound ( ShapePDMap.Find(S) ) ) {
651 Handle(STEPCAFControl_ExternFile) EF = PDFileMap.Find ( ShapePDMap.Find(S) );
652 if ( ! EF.IsNull() ) {
653 // (store information on extern refs in the document)
654 SHAS.Append(EF->GetName());
655 // if yes, just return corresponding label
656 if ( ! EF->GetLabel().IsNull() ) {
657 // but if components >0, ignore extern ref!
658 if ( nbComponents <=0 ) {
659 ShapeLabelMap.Bind ( S, EF->GetLabel() );
660 STool->SetExternRefs(EF->GetLabel(),SHAS);
661 return EF->GetLabel();
662 }
663 }
664#ifdef DEB
665 if ( ! EF->GetLabel().IsNull() )
666 cout << "Warning: STEPCAFControl_Reader::AddShape: Non-empty shape with external ref; ref is ignored" << endl;
667 else if ( nbComponents <=0 )
668 cout << "Warning: STEPCAFControl_Reader::AddShape: Result of reading extern ref is Null" << endl;
669#endif
670 }
671 }
672
673 // add compound either as a whole,
674 if ( ! isAssembly ) {
675 TDF_Label L = STool->AddShape ( S, Standard_False );
676 if ( SHAS.Length() >0 ) STool->SetExternRefs(L,SHAS);
677 ShapeLabelMap.Bind ( S, L );
678 return L;
679 }
680
681 // or as assembly, component-by-component
682 TDF_Label L = STool->NewShape();
683 for ( it.Initialize(S); it.More(); it.Next(), nbComponents++ ) {
684 TopoDS_Shape Sub0 = it.Value();
685 TopLoc_Location loc;
686 Sub0.Location ( loc );
687 TDF_Label subL = AddShape ( Sub0, STool, NewShapesMap, ShapePDMap, PDFileMap, ShapeLabelMap );
688 if ( ! subL.IsNull() ) {
689 STool->AddComponent ( L, subL, it.Value().Location() );
690 }
691 }
692 if ( SHAS.Length() >0 ) STool->SetExternRefs(L,SHAS);
693 ShapeLabelMap.Bind ( S, L );
694 //STool->SetShape ( L, S ); // it is necessary for assemblies OCC1747 // commemted by skl for OCC2941
695
696 return L;
697}
698
699//=======================================================================
700//function : ReadExternFile
701//purpose :
702//=======================================================================
703
704Handle(STEPCAFControl_ExternFile) STEPCAFControl_Reader::ReadExternFile (const Standard_CString file,
705 const Standard_CString fullname,
706 Handle(TDocStd_Document)& doc)
707{
708 // if the file is already read, associate it with SDR
709 if ( myFiles->HasItem ( file, Standard_True ) ) {
710 return myFiles->Item ( file );
711 }
712
713#ifdef DEB
714 cout << "Reading extern file: " << fullname << endl;
715#endif
716
717 // create new WorkSession and Reader
718 Handle(XSControl_WorkSession) newWS = new XSControl_WorkSession;
719 newWS->SelectNorm ( "STEP" );
720 STEPControl_Reader sr ( newWS, Standard_False );
721
722 // start to fill the resulting ExternFile structure
723 Handle(STEPCAFControl_ExternFile) EF = new STEPCAFControl_ExternFile;
724 EF->SetWS ( newWS );
725 EF->SetName ( new TCollection_HAsciiString ( file ) );
726
727 // read file
728 EF->SetLoadStatus ( sr.ReadFile ( fullname ) );
729
730 // transfer in single-result mode
731 if ( EF->GetLoadStatus() == IFSelect_RetDone ) {
732 TDF_LabelSequence labels;
733 EF->SetTransferStatus ( Transfer ( sr, 0, doc, labels, Standard_True ) );
734 if ( labels.Length() >0 ) EF->SetLabel ( labels.Value(1) );
735 }
736
737 // add read file to dictionary
738 myFiles->SetItem ( file, EF );
739
740 return EF;
741}
742
743
744//=======================================================================
745//function : SetColorToSubshape
746//purpose : auxilary
747//=======================================================================
748static void SetColorToSubshape(const Handle(XCAFDoc_ColorTool) & CTool,
749 const TopoDS_Shape & S,
750 const Quantity_Color& col,
751 const XCAFDoc_ColorType type)
752{
753 for (TopoDS_Iterator it(S); it.More(); it.Next())
754 if (! CTool->SetColor( it.Value(), col, type)) break;
755}
756
757
758//=======================================================================
759//function : findStyledSR
760//purpose : auxilary
761//=======================================================================
762static void findStyledSR (const Handle(StepVisual_StyledItem) &style,
763 Handle(StepShape_ShapeRepresentation)& aSR)
764{
765 // search Shape Represenatation for component styled item
766 for ( Standard_Integer j=1; j <= style->NbStyles(); j++ ) {
767 Handle(StepVisual_PresentationStyleByContext) PSA =
768 Handle(StepVisual_PresentationStyleByContext)::DownCast(style->StylesValue ( j ));
769 if ( PSA.IsNull() )
770 continue;
771 StepVisual_StyleContextSelect aStyleCntxSlct = PSA->StyleContext();
772 Handle(StepShape_ShapeRepresentation) aCurrentSR =
773 Handle(StepShape_ShapeRepresentation)::DownCast(aStyleCntxSlct.Representation());
774 if ( aCurrentSR.IsNull() )
775 continue;
776 aSR = aCurrentSR;
777 break;
778 }
779}
780
781
782//=======================================================================
783//function : ReadColors
784//purpose :
785//=======================================================================
786
787Standard_Boolean STEPCAFControl_Reader::ReadColors (const Handle(XSControl_WorkSession) &WS,
788 Handle(TDocStd_Document)& Doc,
789 const STEPCAFControl_DataMapOfPDExternFile &PDFileMap,
790 const XCAFDoc_DataMapOfShapeLabel &ShapeLabelMap) const
791{
792 STEPConstruct_Styles Styles ( WS );
793 if ( ! Styles.LoadStyles() ) {
794#ifdef DEB
795 cout << "Warning: no styles are found in the model" << endl;
796#endif
797 return Standard_False;
798 }
799 // searching for invisible items in the model
800 Handle(TColStd_HSequenceOfTransient) aHSeqOfInvisStyle = new TColStd_HSequenceOfTransient;
801 Styles.LoadInvisStyles( aHSeqOfInvisStyle );
802
803 Handle(XCAFDoc_ColorTool) CTool = XCAFDoc_DocumentTool::ColorTool( Doc->Main() );
804 if ( CTool.IsNull() ) return Standard_False;
805
806 // parse and search for color attributes
807 Standard_Integer nb = Styles.NbStyles();
808 for ( Standard_Integer i=1; i <= nb; i++ ) {
809 Handle(StepVisual_StyledItem) style = Styles.Style ( i );
810 if ( style.IsNull() ) continue;
811
812 Standard_Boolean IsVisible = Standard_True;
813 // check the visibility of styled item.
814 for (Standard_Integer si = 1; si <= aHSeqOfInvisStyle->Length(); si++ ) {
815 if ( style != aHSeqOfInvisStyle->Value( si ) )
816 continue;
817 // found that current style is invisible.
7fd59977 818 IsVisible = Standard_False;
819 break;
820 }
821
822 Handle(StepVisual_Colour) SurfCol, BoundCol, CurveCol;
823 // check if it is component style
824 Standard_Boolean IsComponent = Standard_False;
825 if ( ! Styles.GetColors ( style, SurfCol, BoundCol, CurveCol, IsComponent ) && IsVisible )
826 continue;
827
828 // find shape
829 TopoDS_Shape S = STEPConstruct::FindShape ( Styles.TransientProcess(), style->Item() );
830 Standard_Boolean isSkipSHUOstyle = Standard_False;
831 // take shape with real location.
832 while ( IsComponent ) {
833 // take SR of NAUO
834 Handle(StepShape_ShapeRepresentation) aSR;
835 findStyledSR( style, aSR );
836 // search for SR along model
837 if (aSR.IsNull())
838 break;
839// Handle(Interface_InterfaceModel) Model = WS->Model();
840 Handle(XSControl_TransferReader) TR = WS->TransferReader();
841 Handle(Transfer_TransientProcess) TP = TR->TransientProcess();
842 Interface_EntityIterator subs = WS->HGraph()->Graph().Sharings( aSR );
843 Handle(StepShape_ShapeDefinitionRepresentation) aSDR;
844 for (subs.Start(); subs.More(); subs.Next()) {
845 aSDR = Handle(StepShape_ShapeDefinitionRepresentation)::DownCast(subs.Value());
846 if ( aSDR.IsNull() )
847 continue;
848 StepRepr_RepresentedDefinition aPDSselect = aSDR->Definition();
849 Handle(StepRepr_ProductDefinitionShape) PDS =
850 Handle(StepRepr_ProductDefinitionShape)::DownCast(aPDSselect.PropertyDefinition());
851 if ( PDS.IsNull() )
852 continue;
853 StepRepr_CharacterizedDefinition aCharDef = PDS->Definition();
854
855 Handle(StepRepr_AssemblyComponentUsage) ACU =
856 Handle(StepRepr_AssemblyComponentUsage)::DownCast(aCharDef.ProductDefinitionRelationship());
857 // PTV 10.02.2003 skip styled item that refer to SHUO
858 if (ACU->IsKind(STANDARD_TYPE(StepRepr_SpecifiedHigherUsageOccurrence))) {
859 isSkipSHUOstyle = Standard_True;
860 break;
861 }
862 Handle(StepRepr_NextAssemblyUsageOccurrence) NAUO =
863 Handle(StepRepr_NextAssemblyUsageOccurrence)::DownCast(ACU);
864 if ( NAUO.IsNull() )
865 continue;
866
867 TopoDS_Shape aSh;
868 // PTV 10.02.2003 to find component of assembly CORRECTLY
869 STEPConstruct_Tool Tool( WS );
870 TDF_Label aShLab = FindInstance ( NAUO, CTool->ShapeTool(), Tool, PDFileMap, ShapeLabelMap );
871 aSh = CTool->ShapeTool()->GetShape(aShLab);
872// Handle(Transfer_Binder) binder = TP->Find(NAUO);
873// if ( binder.IsNull() || ! binder->HasResult() )
874// continue;
875// aSh = TransferBRep::ShapeResult ( TP, binder );
876 if (!aSh.IsNull()) {
877 S = aSh;
878 break;
879 }
880 }
881 break;
882 }
883 if (isSkipSHUOstyle)
884 continue; // skip styled item which refer to SHUO
885
36f35343 886 if ( S.IsNull() )
7fd59977 887 continue;
7fd59977 888
889 if ( ! SurfCol.IsNull() ) {
890 Quantity_Color col;
891 Styles.DecodeColor ( SurfCol, col );
892 if ( ! CTool->SetColor ( S, col, XCAFDoc_ColorSurf ))
893 SetColorToSubshape( CTool, S, col, XCAFDoc_ColorSurf );
894 }
895 if ( ! BoundCol.IsNull() ) {
896 Quantity_Color col;
897 Styles.DecodeColor ( BoundCol, col );
898 if ( ! CTool->SetColor ( S, col, XCAFDoc_ColorCurv ))
899 SetColorToSubshape( CTool, S, col, XCAFDoc_ColorCurv );
900 }
901 if ( ! CurveCol.IsNull() ) {
902 Quantity_Color col;
903 Styles.DecodeColor ( CurveCol, col );
904 if ( ! CTool->SetColor ( S, col, XCAFDoc_ColorCurv ))
905 SetColorToSubshape( CTool, S, col, XCAFDoc_ColorCurv );
906 }
907 if ( !IsVisible ) {
908 // sets the invisibility for shape.
909 TDF_Label aInvL;
910 if ( CTool->ShapeTool()->Search( S, aInvL ) )
911 CTool->SetVisibility( aInvL, Standard_False );
912 }
913 }
914 CTool->ReverseChainsOfTreeNodes();
915 return Standard_True;
916}
917
918//=======================================================================
919//function : GetLabelFromPD
920//purpose :
921//=======================================================================
922
923static TDF_Label GetLabelFromPD (const Handle(StepBasic_ProductDefinition) &PD,
924 const Handle(XCAFDoc_ShapeTool) &STool,
925 const Handle(Transfer_TransientProcess) &TP,
926 const STEPCAFControl_DataMapOfPDExternFile &PDFileMap,
927 const XCAFDoc_DataMapOfShapeLabel &ShapeLabelMap)
928{
929 TDF_Label L;
930 if ( PDFileMap.IsBound ( PD ) ) {
931 Handle(STEPCAFControl_ExternFile) EF = PDFileMap.Find ( PD );
932 if ( ! EF.IsNull() ) {
933 L = EF->GetLabel();
934 if ( ! L.IsNull() ) return L;
935 }
936 }
937
938 TopoDS_Shape S;
939 Handle(Transfer_Binder) binder = TP->Find(PD);
940 if ( binder.IsNull() || ! binder->HasResult() ) return L;
941 S = TransferBRep::ShapeResult ( TP, binder );
942 if ( S.IsNull() ) return L;
943
944 if ( S.IsNull() ) return L;
945 if ( ShapeLabelMap.IsBound ( S ) )
946 L = ShapeLabelMap.Find ( S );
947 if ( L.IsNull() )
948 STool->Search ( S, L, Standard_True, Standard_True, Standard_False );
949 return L;
950}
951
952//=======================================================================
953//function : FindInstance
954//purpose :
955//=======================================================================
956
957TDF_Label STEPCAFControl_Reader::FindInstance (const Handle(StepRepr_NextAssemblyUsageOccurrence) &NAUO,
958 const Handle(XCAFDoc_ShapeTool) &STool,
959 const STEPConstruct_Tool &Tool,
960 const STEPCAFControl_DataMapOfPDExternFile &PDFileMap,
961 const XCAFDoc_DataMapOfShapeLabel &ShapeLabelMap)
962{
963 TDF_Label L;
964
965 // get shape resulting from CDSR (in fact, only location is interesting)
966 Handle(Transfer_TransientProcess) TP = Tool.TransientProcess();
967 Handle(Transfer_Binder) binder = TP->Find(NAUO);
968 if ( binder.IsNull() || ! binder->HasResult() ) {
969#ifdef DEB
970 cout << "Error: STEPCAFControl_Reader::FindInstance: NAUO is not mapped to shape" << endl;
971#endif
972 return L;
973 }
974
975 TopoDS_Shape S = TransferBRep::ShapeResult ( TP, binder );
976 if ( S.IsNull() ) {
977#ifdef DEB
978 cout << "Error: STEPCAFControl_Reader::FindInstance: NAUO is not mapped to shape" << endl;
979#endif
980 return L;
981 }
982
983 // find component`s original label
984 Handle(StepBasic_ProductDefinition) PD = NAUO->RelatedProductDefinition();
985 if ( PD.IsNull() ) return L;
986 TDF_Label Lref = GetLabelFromPD ( PD, STool, TP, PDFileMap, ShapeLabelMap );
987 if ( Lref.IsNull() ) return L;
988
989 // find main shape (assembly) label
990 PD.Nullify();
991 PD = NAUO->RelatingProductDefinition();
992 if ( PD.IsNull() ) return L;
993 TDF_Label L0 = GetLabelFromPD ( PD, STool, TP, PDFileMap, ShapeLabelMap );
994 if ( L0.IsNull() ) return L;
995
996 // if CDSR and NAUO are reversed, swap labels
997 Handle(StepShape_ContextDependentShapeRepresentation) CDSR;
998 Interface_EntityIterator subs1 = Tool.Graph().Sharings(NAUO);
999 for (subs1.Start(); subs1.More(); subs1.Next()) {
1000 Handle(StepRepr_ProductDefinitionShape) PDS =
1001 Handle(StepRepr_ProductDefinitionShape)::DownCast(subs1.Value());
1002 if(PDS.IsNull()) continue;
1003 Interface_EntityIterator subs2 = Tool.Graph().Sharings(PDS);
1004 for (subs2.Start(); subs2.More(); subs2.Next()) {
1005 Handle(StepShape_ContextDependentShapeRepresentation) CDSRtmp =
1006 Handle(StepShape_ContextDependentShapeRepresentation)::DownCast(subs2.Value());
1007 if (CDSRtmp.IsNull()) continue;
1008 CDSR = CDSRtmp;
1009 }
1010 }
1011 if (CDSR.IsNull()) return L;
1012// if ( STEPConstruct_Assembly::CheckSRRReversesNAUO ( Tool.Model(), CDSR ) ) {
1013// TDF_Label Lsw = L0; L0 = Lref; Lref = Lsw;
1014// }
1015
1016 // iterate on components to find proper one
1017 TDF_LabelSequence seq;
1018 XCAFDoc_ShapeTool::GetComponents ( L0, seq );
1019 for ( Standard_Integer k=1; L.IsNull() && k <= seq.Length(); k++ ) {
1020 TDF_Label Lcomp = seq(k), Lref2;
1021 if ( XCAFDoc_ShapeTool::GetReferredShape ( Lcomp, Lref2 ) &&
1022 Lref2 == Lref &&
1023 S.Location() == XCAFDoc_ShapeTool::GetLocation ( Lcomp ) )
1024 L = Lcomp;
1025 }
1026
1027 return L;
1028}
1029
1030//=======================================================================
1031//function : ReadNames
1032//purpose :
1033//=======================================================================
1034
1035Standard_Boolean STEPCAFControl_Reader::ReadNames (const Handle(XSControl_WorkSession) &WS,
1036 Handle(TDocStd_Document)& Doc,
1037 const STEPCAFControl_DataMapOfPDExternFile &PDFileMap,
1038 const XCAFDoc_DataMapOfShapeLabel &ShapeLabelMap) const
1039{
1040 // get starting data
1041 Handle(Interface_InterfaceModel) Model = WS->Model();
1042 Handle(XSControl_TransferReader) TR = WS->TransferReader();
1043 Handle(Transfer_TransientProcess) TP = TR->TransientProcess();
1044 Handle(XCAFDoc_ShapeTool) STool = XCAFDoc_DocumentTool::ShapeTool( Doc->Main() );
1045 if ( STool.IsNull() ) return Standard_False;
1046 STEPConstruct_Tool Tool ( WS );
1047
1048 // iterate on model to find all SDRs and CDSRs
1049 Standard_Integer nb = Model->NbEntities();
1050 Handle(Standard_Type) tNAUO = STANDARD_TYPE(StepRepr_NextAssemblyUsageOccurrence);
1051 Handle(Standard_Type) tPD = STANDARD_TYPE(StepBasic_ProductDefinition);
1052 Handle(TCollection_HAsciiString) name;
1053 TDF_Label L;
1054 for (Standard_Integer i = 1; i <= nb; i++) {
1055 Handle(Standard_Transient) enti = Model->Value(i);
1056
1057 // get description of NAUO
1058 if ( enti->DynamicType() == tNAUO ) {
1059 L.Nullify();
1060 Handle(StepRepr_NextAssemblyUsageOccurrence) NAUO =
1061 Handle(StepRepr_NextAssemblyUsageOccurrence)::DownCast(enti);
1062 if(NAUO.IsNull()) continue;
1063 Interface_EntityIterator subs = WS->Graph().Sharings(NAUO);
1064 for (subs.Start(); subs.More(); subs.Next()) {
1065 Handle(StepRepr_ProductDefinitionShape) PDS =
1066 Handle(StepRepr_ProductDefinitionShape)::DownCast(subs.Value());
1067 if(PDS.IsNull()) continue;
1068 Handle(StepBasic_ProductDefinitionRelationship) PDR = PDS->Definition().ProductDefinitionRelationship();
1069 if ( PDR.IsNull() ) continue;
1070 if ( PDR->HasDescription() &&
1071 PDR->Description()->Length() >0 ) name = PDR->Description();
ab4a5554 1072 else if ( !PDR->Name().IsNull() && PDR->Name()->Length() >0 ) name = PDR->Name();
1073 else if ( !PDR->Id().IsNull()) name = PDR->Id();
1074 else name = new TCollection_HAsciiString;
7fd59977 1075 }
1076 // find proper label
1077 L = FindInstance ( NAUO, STool, Tool, PDFileMap, ShapeLabelMap );
1078 if ( L.IsNull() ) continue;
1079 TCollection_ExtendedString str ( name->String() );
1080 TDataStd_Name::Set ( L, str );
1081 }
1082
1083 // for PD get name of associated product
1084 if ( enti->DynamicType() == tPD ) {
1085 L.Nullify();
1086 Handle(StepBasic_ProductDefinition) PD =
1087 Handle(StepBasic_ProductDefinition)::DownCast(enti);
1088 if(PD.IsNull()) continue;
1089 Handle(StepBasic_Product) Prod = PD->Formation()->OfProduct();
ab4a5554 1090 if(!Prod->Name().IsNull() && Prod->Name()->UsefullLength()>0) name = Prod->Name();
1091 else if (!Prod->Id().IsNull()) name = Prod->Id();
1092 else name = new TCollection_HAsciiString;
7fd59977 1093 L = GetLabelFromPD ( PD, STool, TP, PDFileMap, ShapeLabelMap );
1094 if ( L.IsNull() ) continue;
1095 TCollection_ExtendedString str ( name->String() );
1096 TDataStd_Name::Set ( L, str );
1097 }
1098 // set a name to the document
1099 //TCollection_ExtendedString str ( name->String() );
1100 //TDataStd_Name::Set ( L, str );
1101 }
1102
1103 return Standard_True;
1104}
1105
1106//=======================================================================
1107//function : GetLabelFromPD
1108//purpose :
1109//=======================================================================
1110
1111static TDF_Label GetLabelFromPD (const Handle(StepBasic_ProductDefinition) &PD,
1112 const Handle(XCAFDoc_ShapeTool) &STool,
1113 const STEPConstruct_ValidationProps &Props,
1114 const STEPCAFControl_DataMapOfPDExternFile &PDFileMap,
1115 const XCAFDoc_DataMapOfShapeLabel &ShapeLabelMap)
1116{
1117 TDF_Label L;
1118 if ( PDFileMap.IsBound ( PD ) ) {
1119 Handle(STEPCAFControl_ExternFile) EF = PDFileMap.Find ( PD );
1120 if ( ! EF.IsNull() ) {
1121 L = EF->GetLabel();
1122 if ( ! L.IsNull() ) return L;
1123 }
1124 }
1125 TopoDS_Shape S = Props.GetPropShape ( PD );
1126 if ( S.IsNull() ) return L;
1127 if ( ShapeLabelMap.IsBound ( S ) )
1128 L = ShapeLabelMap.Find ( S );
1129 if ( L.IsNull() )
1130 STool->Search ( S, L, Standard_True, Standard_True, Standard_False );
1131 return L;
1132}
1133
1134//=======================================================================
1135//function : ReadValProps
1136//purpose :
1137//=======================================================================
1138
1139Standard_Boolean STEPCAFControl_Reader::ReadValProps (const Handle(XSControl_WorkSession) &WS,
1140 Handle(TDocStd_Document)& Doc,
1141 const STEPCAFControl_DataMapOfPDExternFile &PDFileMap,
1142 const XCAFDoc_DataMapOfShapeLabel &ShapeLabelMap) const
1143{
1144 // get starting data
1145 Handle(Interface_InterfaceModel) Model = WS->Model();
1146 Handle(XSControl_TransferReader) TR = WS->TransferReader();
1147 Handle(Transfer_TransientProcess) TP = TR->TransientProcess();
1148 Handle(XCAFDoc_ShapeTool) STool = XCAFDoc_DocumentTool::ShapeTool( Doc->Main() );
1149 if ( STool.IsNull() ) return Standard_False;
1150
1151 // load props from the STEP model
1152 TColStd_SequenceOfTransient props;
1153 STEPConstruct_ValidationProps Props ( WS );
1154 if ( ! Props.LoadProps ( props ) ) {
1155#ifdef DEB
1156 cout << "Warning: no validation props found in the model" << endl;
1157#endif
1158 return Standard_False;
1159 }
1160
1161 // interpret props one by one
1162 for (Standard_Integer i = 1; i <= props.Length(); i ++) {
1163 Handle(StepRepr_PropertyDefinitionRepresentation) PDR =
1164 Handle(StepRepr_PropertyDefinitionRepresentation)::DownCast ( props.Value(i) );
1165 if ( PDR.IsNull() ) continue;
1166
1167 TDF_Label L;
1168
1169 Handle(StepRepr_PropertyDefinition) PD = PDR->Definition().PropertyDefinition();
1170 Interface_EntityIterator subs = Props.Graph().Shareds(PD);
1171 for (subs.Start(); L.IsNull() && subs.More(); subs.Next()) {
1172 if ( subs.Value()->IsKind(STANDARD_TYPE(StepRepr_ProductDefinitionShape)) ) {
1173 Handle(StepRepr_ProductDefinitionShape) PDS = Handle(StepRepr_ProductDefinitionShape)::DownCast(subs.Value());
1174 if(PDS.IsNull()) continue;
1175 // find corresponding NAUO
1176 Handle(StepRepr_NextAssemblyUsageOccurrence) NAUO;
1177 Interface_EntityIterator subs1 = Props.Graph().Shareds(PDS);
1178 for (subs1.Start(); NAUO.IsNull() && subs1.More(); subs1.Next()) {
1179 if ( subs1.Value()->IsKind(STANDARD_TYPE(StepRepr_NextAssemblyUsageOccurrence)) )
1180 NAUO = Handle(StepRepr_NextAssemblyUsageOccurrence)::DownCast(subs1.Value());
1181 }
1182 if ( !NAUO.IsNull() ) {
1183 L = FindInstance ( NAUO, STool, WS, PDFileMap, ShapeLabelMap );
1184 if ( L.IsNull() ) continue;
1185 }
1186 else {
1187 // find corresponding ProductDefinition:
1188 Handle(StepBasic_ProductDefinition) ProdDef;
1189 Interface_EntityIterator subsPDS = Props.Graph().Shareds(PDS);
1190 for (subsPDS.Start(); ProdDef.IsNull() && subsPDS.More(); subsPDS.Next()) {
1191 if ( subsPDS.Value()->IsKind(STANDARD_TYPE(StepBasic_ProductDefinition)) )
1192 ProdDef = Handle(StepBasic_ProductDefinition)::DownCast(subsPDS.Value());
1193 }
1194 if ( ProdDef.IsNull() ) continue;
1195 L = GetLabelFromPD ( ProdDef, STool, Props, PDFileMap, ShapeLabelMap );
1196 }
1197 }
1198
1199 if ( subs.Value()->IsKind(STANDARD_TYPE(StepRepr_ShapeAspect)) ) {
1200 Handle(StepRepr_ShapeAspect) SA = Handle(StepRepr_ShapeAspect)::DownCast(subs.Value());
1201 if(SA.IsNull()) continue;
1202 // find ShapeRepresentation
1203 Handle(StepShape_ShapeRepresentation) SR;
1204 Interface_EntityIterator subs1 = Props.Graph().Sharings(SA);
1205 for(subs1.Start(); subs1.More() && SR.IsNull(); subs1.Next()) {
1206 Handle(StepRepr_PropertyDefinition) PropD1 =
1207 Handle(StepRepr_PropertyDefinition)::DownCast(subs1.Value());
1208 if(PropD1.IsNull()) continue;
1209 Interface_EntityIterator subs2 = Props.Graph().Sharings(PropD1);
1210 for(subs2.Start(); subs2.More() && SR.IsNull(); subs2.Next()) {
1211 Handle(StepShape_ShapeDefinitionRepresentation) SDR =
1212 Handle(StepShape_ShapeDefinitionRepresentation)::DownCast(subs2.Value());
1213 if(SDR.IsNull()) continue;
1214 SR = Handle(StepShape_ShapeRepresentation)::DownCast(SDR->UsedRepresentation());
1215 }
1216 }
1217 if(SR.IsNull()) continue;
1218 Handle(Transfer_Binder) binder;
1219 for(Standard_Integer ir=1; ir<=SR->NbItems() && binder.IsNull(); ir++) {
1220 if(SR->ItemsValue(ir)->IsKind(STANDARD_TYPE(StepShape_SolidModel))) {
1221 Handle(StepShape_SolidModel) SM =
1222 Handle(StepShape_SolidModel)::DownCast(SR->ItemsValue(ir));
1223 binder = TP->Find(SM);
1224 }
1225 else if(SR->ItemsValue(ir)->IsKind(STANDARD_TYPE(StepShape_ShellBasedSurfaceModel))) {
1226 Handle(StepShape_ShellBasedSurfaceModel) SBSM =
1227 Handle(StepShape_ShellBasedSurfaceModel)::DownCast(SR->ItemsValue(ir));
1228 binder = TP->Find(SBSM);
1229 }
1230 else if(SR->ItemsValue(ir)->IsKind(STANDARD_TYPE(StepShape_GeometricSet))) {
1231 Handle(StepShape_GeometricSet) GS =
1232 Handle(StepShape_GeometricSet)::DownCast(SR->ItemsValue(ir));
1233 binder = TP->Find(GS);
1234 }
1235 }
1236 if ( binder.IsNull() || ! binder->HasResult() ) continue;
1237 TopoDS_Shape S;
1238 S = TransferBRep::ShapeResult ( TP, binder );
1239 if(S.IsNull()) continue;
1240 if ( ShapeLabelMap.IsBound ( S ) )
1241 L = ShapeLabelMap.Find ( S );
1242 if ( L.IsNull() )
1243 STool->Search ( S, L, Standard_True, Standard_True, Standard_True );
1244 }
1245 }
1246
1247 if(L.IsNull()) continue;
1248
1249 // decode validation properties
1250 Handle(StepRepr_Representation) rep = PDR->UsedRepresentation();
1251 for ( Standard_Integer j=1; j <= rep->NbItems(); j++ ) {
1252 Handle(StepRepr_RepresentationItem) ent = rep->ItemsValue(j);
1253 Standard_Boolean isArea;
1254 Standard_Real val;
1255 gp_Pnt pos;
1256 if ( Props.GetPropReal ( ent, val, isArea ) ) {
1257 if ( isArea ) XCAFDoc_Area::Set ( L, val );
1258 else XCAFDoc_Volume::Set ( L, val );
1259 }
1260 else if ( Props.GetPropPnt ( ent, rep->ContextOfItems(), pos ) ) {
1261 XCAFDoc_Centroid::Set ( L, pos );
1262 }
1263 }
1264 }
1265 return Standard_True;
1266}
1267
1268//=======================================================================
1269//function : ReadLayers
1270//purpose :
1271//=======================================================================
1272
1273Standard_Boolean STEPCAFControl_Reader::ReadLayers (const Handle(XSControl_WorkSession) &WS,
1274 Handle(TDocStd_Document)& Doc) const
1275{
1276 Handle(Interface_InterfaceModel) Model = WS->Model();
1277 Handle(XSControl_TransferReader) TR = WS->TransferReader();
1278 Handle(Transfer_TransientProcess) TP = TR->TransientProcess();
1279 Handle(XCAFDoc_ShapeTool) STool = XCAFDoc_DocumentTool::ShapeTool( Doc->Main() );
1280 if ( STool.IsNull() ) return Standard_False;
1281 Handle(XCAFDoc_LayerTool) LTool = XCAFDoc_DocumentTool::LayerTool( Doc->Main() );
1282 if ( LTool.IsNull() ) return Standard_False;
1283
1284 Handle(Standard_Type) tSVPLA = STANDARD_TYPE(StepVisual_PresentationLayerAssignment);
1285 Standard_Integer nb = Model->NbEntities();
1286 Handle(TCollection_HAsciiString) name;
1287
1288 for (Standard_Integer i = 1; i <= nb; i ++) {
1289 Handle(Standard_Transient) enti = Model->Value(i);
1290 if ( ! enti->IsKind ( tSVPLA ) ) continue;
1291 Handle(StepVisual_PresentationLayerAssignment) SVPLA =
1292 Handle(StepVisual_PresentationLayerAssignment)::DownCast(enti);
1293
1294 Handle(TCollection_HAsciiString) descr = SVPLA->Description();
1295 Handle(TCollection_HAsciiString) hName = SVPLA->Name();
1296 TCollection_ExtendedString aLayerName ( hName->String() );
1297
1298 // find a target shape and its label in the document
1299 for (Standard_Integer j = 1; j <= SVPLA->NbAssignedItems(); j++ ) {
1300 StepVisual_LayeredItem LI = SVPLA->AssignedItemsValue(j);
1301 Handle(Transfer_Binder) binder = TP->Find( LI.Value() );
1302 if ( binder.IsNull() || ! binder->HasResult() ) continue;
1303
1304 TopoDS_Shape S = TransferBRep::ShapeResult ( TP, binder );
1305 if ( S.IsNull() ) continue;
1306
1307 TDF_Label shL;
1308 if ( ! STool->Search ( S, shL, Standard_True, Standard_True, Standard_True ) ) continue;
1309 LTool->SetLayer ( shL, aLayerName );
1310 }
1311
1312 // check invisibility
1313 Interface_EntityIterator subs = WS->Graph().Sharings(SVPLA);
1314 for (subs.Start(); subs.More(); subs.Next()) {
1315 if ( ! subs.Value()->IsKind(STANDARD_TYPE(StepVisual_Invisibility)) ) continue;
1316#ifdef DEB
1317 cout<< "\tLayer \"" << aLayerName << "\" is invisible"<<endl;
1318#endif
1319 //TDF_Label InvLayerLab = LTool->FindLayer(aLayerName);
1320 TDF_Label InvLayerLab = LTool->AddLayer(aLayerName); //skl for OCC3926
1321 Handle(TDataStd_UAttribute) aUAttr;
1322 aUAttr->Set( InvLayerLab, XCAFDoc::InvisibleGUID() );
1323 }
1324 }
1325 return Standard_True;
1326}
1327
1328//=======================================================================
1329//function : ReadSHUOs
1330//purpose :
1331//=======================================================================
1332
1333static Standard_Boolean findNextSHUOlevel (const Handle(XSControl_WorkSession) &WS,
1334 const Handle(StepRepr_SpecifiedHigherUsageOccurrence)& SHUO,
1335 const Handle(XCAFDoc_ShapeTool)& STool,
1336 const STEPCAFControl_DataMapOfPDExternFile &PDFileMap,
1337 const XCAFDoc_DataMapOfShapeLabel &ShapeLabelMap,
1338 TDF_LabelSequence& aLabels)
1339{
1340 Interface_EntityIterator subs = WS->HGraph()->Graph().Sharings(SHUO);
1341 Handle(StepRepr_SpecifiedHigherUsageOccurrence) subSHUO;
1342 for (subs.Start(); subs.More(); subs.Next()) {
1343 if (subs.Value()->IsKind(STANDARD_TYPE(StepRepr_SpecifiedHigherUsageOccurrence))) {
1344 subSHUO = Handle(StepRepr_SpecifiedHigherUsageOccurrence)::DownCast(subs.Value());
1345 break;
1346 }
1347 }
1348 if (subSHUO.IsNull())
1349 return Standard_False;
1350
1351 Handle(StepRepr_NextAssemblyUsageOccurrence) NUNAUO =
1352 Handle(StepRepr_NextAssemblyUsageOccurrence)::DownCast(subSHUO->NextUsage());
1353 if (NUNAUO.IsNull())
1354 return Standard_False;
1355// Handle(Interface_InterfaceModel) Model = WS->Model();
1356// Handle(XSControl_TransferReader) TR = WS->TransferReader();
1357// Handle(Transfer_TransientProcess) TP = TR->TransientProcess();
1358// Handle(Transfer_Binder) binder = TP->Find(NUNAUO);
1359// if ( binder.IsNull() || ! binder->HasResult() )
1360// return Standard_False;
1361// TopoDS_Shape NUSh = TransferBRep::ShapeResult ( TP, binder );
1362 // get label of NAUO next level
1363 TDF_Label NULab;
1364 STEPConstruct_Tool Tool( WS );
1365 NULab = STEPCAFControl_Reader::FindInstance ( NUNAUO, STool, Tool, PDFileMap, ShapeLabelMap );
1366// STool->Search(NUSh, NUlab);
1367 if (NULab.IsNull())
1368 return Standard_False;
1369 aLabels.Append( NULab );
1370 // and check by recurse.
1371 findNextSHUOlevel( WS, subSHUO, STool, PDFileMap, ShapeLabelMap, aLabels );
1372 return Standard_True;
1373}
1374
1375
1376//=======================================================================
1377//function : setSHUOintoDoc
1378//purpose : auxilary
1379//=======================================================================
1380static TDF_Label setSHUOintoDoc (const Handle(XSControl_WorkSession) &WS,
1381 const Handle(StepRepr_SpecifiedHigherUsageOccurrence)& SHUO,
1382 const Handle(XCAFDoc_ShapeTool)& STool,
1383 const STEPCAFControl_DataMapOfPDExternFile &PDFileMap,
1384 const XCAFDoc_DataMapOfShapeLabel &ShapeLabelMap)
1385{
1386 TDF_Label aMainLabel;
1387 // get upper usage NAUO from SHUO.
1388 Handle(StepRepr_NextAssemblyUsageOccurrence) UUNAUO =
1389 Handle(StepRepr_NextAssemblyUsageOccurrence)::DownCast(SHUO->UpperUsage());
1390 Handle(StepRepr_NextAssemblyUsageOccurrence) NUNAUO =
1391 Handle(StepRepr_NextAssemblyUsageOccurrence)::DownCast(SHUO->NextUsage());
1392 if ( UUNAUO.IsNull() || NUNAUO.IsNull() ) {
1393#ifdef DEB
1394 cout << "Warning: " << __FILE__ <<": Upper_usage or Next_usage of styled SHUO is null. Skip it" << endl;
1395#endif
1396 return aMainLabel;
1397 }
1398// Handle(Interface_InterfaceModel) Model = WS->Model();
1399// Handle(XSControl_TransferReader) TR = WS->TransferReader();
1400// Handle(Transfer_TransientProcess) TP = TR->TransientProcess();
1401// TopoDS_Shape UUSh, NUSh;
1402// Handle(Transfer_Binder) binder = TP->Find(UUNAUO);
1403// if ( binder.IsNull() || ! binder->HasResult() )
1404// return aMainLabel;
1405// UUSh = TransferBRep::ShapeResult ( TP, binder );
1406// binder = TP->Find(NUNAUO);
1407// if ( binder.IsNull() || ! binder->HasResult() )
1408// return aMainLabel;
1409// NUSh = TransferBRep::ShapeResult ( TP, binder );
1410
1411 // get first labels for first SHUO attribute
1412 TDF_Label UULab, NULab;
1413 STEPConstruct_Tool Tool( WS );
1414 UULab = STEPCAFControl_Reader::FindInstance ( UUNAUO, STool, Tool, PDFileMap, ShapeLabelMap );
1415 NULab = STEPCAFControl_Reader::FindInstance ( NUNAUO, STool, Tool, PDFileMap, ShapeLabelMap );
1416
1417// STool->Search(UUSh, UULab);
1418// STool->Search(NUSh, NULab);
1419 if (UULab.IsNull() || NULab.IsNull()) return aMainLabel;
1420 //create sequence fo labels to set SHUO structure into the document
1421 TDF_LabelSequence ShuoLabels;
1422 ShuoLabels.Append( UULab );
1423 ShuoLabels.Append( NULab );
1424 // add all other labels of sub SHUO entities
1425 findNextSHUOlevel( WS, SHUO, STool, PDFileMap, ShapeLabelMap, ShuoLabels );
1426 // last accord for SHUO
1427 Handle(XCAFDoc_GraphNode) anSHUOAttr;
1428 if ( STool->SetSHUO( ShuoLabels, anSHUOAttr ) )
1429 aMainLabel = anSHUOAttr->Label();
1430
1431 return aMainLabel;
1432}
1433
1434
1435//=======================================================================
1436//function : ReadSHUOs
1437//purpose :
1438//=======================================================================
1439
1440Standard_Boolean STEPCAFControl_Reader::ReadSHUOs (const Handle(XSControl_WorkSession) &WS,
1441 Handle(TDocStd_Document)& Doc,
1442 const STEPCAFControl_DataMapOfPDExternFile &PDFileMap,
1443 const XCAFDoc_DataMapOfShapeLabel &ShapeLabelMap) const
1444{
1445 // the big part code duplication from ReadColors.
1446 // It is possible to share this code functionality, just to decide how ???
1447 Handle(XCAFDoc_ColorTool) CTool = XCAFDoc_DocumentTool::ColorTool( Doc->Main() );
1448 Handle(XCAFDoc_ShapeTool) STool = CTool->ShapeTool();
1449
1450 STEPConstruct_Styles Styles ( WS );
1451 if ( ! Styles.LoadStyles() ) {
1452#ifdef DEB
1453 cout << "Warning: no styles are found in the model" << endl;
1454#endif
1455 return Standard_False;
1456 }
1457 // searching for invisible items in the model
1458 Handle(TColStd_HSequenceOfTransient) aHSeqOfInvisStyle = new TColStd_HSequenceOfTransient;
1459 Styles.LoadInvisStyles( aHSeqOfInvisStyle );
1460 // parse and search for color attributes
1461 Standard_Integer nb = Styles.NbStyles();
1462 for ( Standard_Integer i=1; i <= nb; i++ ) {
1463 Handle(StepVisual_StyledItem) style = Styles.Style ( i );
1464 if ( style.IsNull() ) continue;
1465
1466 Standard_Boolean IsVisible = Standard_True;
1467 // check the visibility of styled item.
1468 for (Standard_Integer si = 1; si <= aHSeqOfInvisStyle->Length(); si++ ) {
1469 if ( style != aHSeqOfInvisStyle->Value( si ) )
1470 continue;
1471 // found that current style is invisible.
1472#ifdef DEB
1473 cout << "Warning: item No " << i << "(" << style->Item()->DynamicType()->Name() << ") is invisible" << endl;
1474#endif
1475 IsVisible = Standard_False;
1476 break;
1477 }
1478
1479 Handle(StepVisual_Colour) SurfCol, BoundCol, CurveCol;
1480 // check if it is component style
1481 Standard_Boolean IsComponent = Standard_False;
1482 if ( ! Styles.GetColors ( style, SurfCol, BoundCol, CurveCol, IsComponent ) && IsVisible )
1483 continue;
1484 if (!IsComponent)
1485 continue;
1486 Handle(StepShape_ShapeRepresentation) aSR;
1487 findStyledSR( style, aSR );
1488 // search for SR along model
1489 if ( aSR.IsNull() )
1490 continue;
1491 Interface_EntityIterator subs = WS->HGraph()->Graph().Sharings( aSR );
1492 Handle(StepShape_ShapeDefinitionRepresentation) aSDR;
1493 for (subs.Start(); subs.More(); subs.Next()) {
1494 aSDR = Handle(StepShape_ShapeDefinitionRepresentation)::DownCast(subs.Value());
1495 if ( aSDR.IsNull() )
1496 continue;
1497 StepRepr_RepresentedDefinition aPDSselect = aSDR->Definition();
1498 Handle(StepRepr_ProductDefinitionShape) PDS =
1499 Handle(StepRepr_ProductDefinitionShape)::DownCast(aPDSselect.PropertyDefinition());
1500 if ( PDS.IsNull() )
1501 continue;
1502 StepRepr_CharacterizedDefinition aCharDef = PDS->Definition();
1503 Handle(StepRepr_SpecifiedHigherUsageOccurrence) SHUO =
1504 Handle(StepRepr_SpecifiedHigherUsageOccurrence)::DownCast(aCharDef.ProductDefinitionRelationship());
1505 if ( SHUO.IsNull() )
1506 continue;
1507
1508 // set the SHUO structure to the document
1509 TDF_Label aLabelForStyle = setSHUOintoDoc( WS, SHUO, STool, PDFileMap, ShapeLabelMap );
1510 if ( aLabelForStyle.IsNull() ) {
1511#ifdef DEB
1512 cout << "Warning: " << __FILE__ <<": coudnot create SHUO structure in the document" << endl;
1513#endif
1514 continue;
1515 }
1516 // now set the style to the SHUO main label.
1517 if ( ! SurfCol.IsNull() ) {
1518 Quantity_Color col;
1519 Styles.DecodeColor ( SurfCol, col );
1520 CTool->SetColor ( aLabelForStyle, col, XCAFDoc_ColorSurf );
1521 }
1522 if ( ! BoundCol.IsNull() ) {
1523 Quantity_Color col;
1524 Styles.DecodeColor ( BoundCol, col );
1525 CTool->SetColor ( aLabelForStyle, col, XCAFDoc_ColorCurv );
1526 }
1527 if ( ! CurveCol.IsNull() ) {
1528 Quantity_Color col;
1529 Styles.DecodeColor ( CurveCol, col );
1530 CTool->SetColor ( aLabelForStyle, col, XCAFDoc_ColorCurv );
1531 }
1532 if ( !IsVisible )
1533 // sets the invisibility for shape.
1534 CTool->SetVisibility( aLabelForStyle, Standard_False );
1535
1536 } // end search SHUO by SDR
1537 } // end iterates on styles
1538
1539 return Standard_True;
1540}
1541
1542
1543//=======================================================================
1544//function : GetLengthConversionFactor
1545//purpose :
1546//=======================================================================
1547static Standard_Boolean GetLengthConversionFactor(Handle(StepBasic_NamedUnit)& NU,
1548 Standard_Real& afact)
1549{
1550 afact=1.;
1551 if( !NU->IsKind(STANDARD_TYPE(StepBasic_ConversionBasedUnitAndLengthUnit)) ) return Standard_False;
1552 Handle(StepBasic_ConversionBasedUnitAndLengthUnit) CBULU =
1553 Handle(StepBasic_ConversionBasedUnitAndLengthUnit)::DownCast(NU);
1554 Handle(StepBasic_MeasureWithUnit) MWUCBU = CBULU->ConversionFactor();
1555 afact = MWUCBU->ValueComponent();
1556 StepBasic_Unit anUnit2 = MWUCBU->UnitComponent();
1557 if(anUnit2.CaseNum(anUnit2.Value())==1) {
1558 Handle(StepBasic_NamedUnit) NU2 = anUnit2.NamedUnit();
1559 if(NU2->IsKind(STANDARD_TYPE(StepBasic_SiUnit))) {
1560 Handle(StepBasic_SiUnit) SU = Handle(StepBasic_SiUnit)::DownCast(NU2);
1561 if(SU->Name()==StepBasic_sunMetre) {
1562 if(SU->HasPrefix())
1563 afact *= STEPConstruct_UnitContext::ConvertSiPrefix (SU->Prefix());
1564 // convert m to mm
1565 afact *= 1000.;
1566 }
1567 }
1568 }
1569 return Standard_True;
1570}
1571
1572
1573//=======================================================================
1574//function : GetMassConversionFactor
1575//purpose :
1576//=======================================================================
1577static Standard_Boolean GetMassConversionFactor(Handle(StepBasic_NamedUnit)& NU,
1578 Standard_Real& afact)
1579{
1580 afact=1.;
1581 if( !NU->IsKind(STANDARD_TYPE(StepBasic_ConversionBasedUnitAndMassUnit)) ) return Standard_False;
1582 Handle(StepBasic_ConversionBasedUnitAndMassUnit) CBUMU =
1583 Handle(StepBasic_ConversionBasedUnitAndMassUnit)::DownCast(NU);
1584 Handle(StepBasic_MeasureWithUnit) MWUCBU = CBUMU->ConversionFactor();
1585 afact = MWUCBU->ValueComponent();
1586 StepBasic_Unit anUnit2 = MWUCBU->UnitComponent();
1587 if(anUnit2.CaseNum(anUnit2.Value())==1) {
1588 Handle(StepBasic_NamedUnit) NU2 = anUnit2.NamedUnit();
1589 if(NU2->IsKind(STANDARD_TYPE(StepBasic_SiUnit))) {
1590 Handle(StepBasic_SiUnit) SU = Handle(StepBasic_SiUnit)::DownCast(NU2);
1591 if(SU->Name()==StepBasic_sunGram) {
1592 if(SU->HasPrefix())
1593 afact *= STEPConstruct_UnitContext::ConvertSiPrefix (SU->Prefix());
1594 }
1595 }
1596 }
1597 return Standard_True;
1598}
1599
1600
1601//=======================================================================
1602//function : ReadDatums
1603//purpose : auxilary
1604//=======================================================================
1605static Standard_Boolean ReadDatums(const Handle(XCAFDoc_ShapeTool) &STool,
1606 const Handle(XCAFDoc_DimTolTool) &DGTTool,
1607 const Interface_Graph &graph,
1608 Handle(Transfer_TransientProcess) &TP,
1609 const TDF_Label TolerL,
1610 const Handle(StepDimTol_GeometricToleranceWithDatumReference) GTWDR)
1611{
1612 if(GTWDR.IsNull()) return Standard_False;
1613 Handle(StepDimTol_HArray1OfDatumReference) HADR = GTWDR->DatumSystem();
1614 if(HADR.IsNull()) return Standard_False;
1615 for(Standard_Integer idr=1; idr<=HADR->Length(); idr++) {
1616 Handle(StepDimTol_DatumReference) DR = HADR->Value(idr);
1617 Handle(StepDimTol_Datum) aDatum = DR->ReferencedDatum();
1618 if(aDatum.IsNull()) continue;
1619 Interface_EntityIterator subs4 = graph.Sharings(aDatum);
1620 for(subs4.Start(); subs4.More(); subs4.Next()) {
1621 Handle(StepRepr_ShapeAspectRelationship) SAR =
1622 Handle(StepRepr_ShapeAspectRelationship)::DownCast(subs4.Value());
1623 if(SAR.IsNull()) continue;
1624 Handle(StepDimTol_DatumFeature) DF =
1625 Handle(StepDimTol_DatumFeature)::DownCast(SAR->RelatingShapeAspect());
1626 if(DF.IsNull()) continue;
1627 Interface_EntityIterator subs5 = graph.Sharings(DF);
1628 Handle(StepRepr_PropertyDefinition) PropDef;
1629 for(subs5.Start(); subs5.More() && PropDef.IsNull(); subs5.Next()) {
1630 PropDef = Handle(StepRepr_PropertyDefinition)::DownCast(subs5.Value());
1631 }
1632 if(PropDef.IsNull()) continue;
1633 Handle(StepShape_AdvancedFace) AF;
1634 subs5 = graph.Sharings(PropDef);
1635 for(subs5.Start(); subs5.More(); subs5.Next()) {
1636 Handle(StepShape_ShapeDefinitionRepresentation) SDR =
1637 Handle(StepShape_ShapeDefinitionRepresentation)::DownCast(subs5.Value());
1638 if(!SDR.IsNull()) {
1639 Handle(StepRepr_Representation) Repr = SDR->UsedRepresentation();
1640 if( !Repr.IsNull() && Repr->NbItems()>0 ) {
1641 Handle(StepRepr_RepresentationItem) RI = Repr->ItemsValue(1);
1642 AF = Handle(StepShape_AdvancedFace)::DownCast(RI);
1643 }
1644 }
1645 }
1646 if(AF.IsNull()) return Standard_False;
1647 Standard_Integer index = TP->MapIndex(AF);
1648 TopoDS_Shape aSh;
1649 if(index >0) {
1650 Handle(Transfer_Binder) binder = TP->MapItem(index);
1651 aSh = TransferBRep::ShapeResult(binder);
1652 }
1653 if(aSh.IsNull()) continue;
1654 TDF_Label shL;
1655 if( !STool->Search(aSh, shL, Standard_True, Standard_True, Standard_True) ) continue;
1656 DGTTool->SetDatum(shL,TolerL,PropDef->Name(),PropDef->Description(),aDatum->Identification());
1657 }
1658 }
1659 return Standard_True;
1660}
1661
1662
1663//=======================================================================
1664//function : ReadGDTs
1665//purpose :
1666//=======================================================================
1667
1668Standard_Boolean STEPCAFControl_Reader::ReadGDTs(const Handle(XSControl_WorkSession) &WS,
1669 Handle(TDocStd_Document)& Doc) const
1670{
1671 Handle(Interface_InterfaceModel) Model = WS->Model();
1672 Handle(XCAFDoc_ShapeTool) STool = XCAFDoc_DocumentTool::ShapeTool( Doc->Main() );
1673 Handle(XSControl_TransferReader) TR = WS->TransferReader();
1674 Handle(Transfer_TransientProcess) TP = TR->TransientProcess();
1675 Handle(XCAFDoc_DimTolTool) DGTTool = XCAFDoc_DocumentTool::DimTolTool( Doc->Main() );
1676 if ( DGTTool.IsNull() ) return Standard_False;
1677
1678 Standard_Integer nb = Model->NbEntities();
1679 const Interface_Graph& graph = TP->Graph();
1680 for(Standard_Integer i=1; i<=nb; i++) {
1681 Handle(Standard_Transient) ent = Model->Value(i);
1682 if(ent->IsKind(STANDARD_TYPE(StepRepr_ShapeAspect))) {
1683 Handle(StepRepr_ShapeAspect) SA = Handle(StepRepr_ShapeAspect)::DownCast(ent);
1684 // find RepresentationItem for current ShapeAspect
1685 Handle(StepRepr_RepresentationItem) RI;
1686 Handle(StepRepr_PropertyDefinition) PropD;
1687 Interface_EntityIterator subs3 = graph.Sharings(SA);
1688 for(subs3.Start(); subs3.More() && PropD.IsNull(); subs3.Next()) {
1689 PropD = Handle(StepRepr_PropertyDefinition)::DownCast(subs3.Value());
1690 }
1691 if(PropD.IsNull()) continue;
1692 Interface_EntityIterator subs4 = graph.Sharings(PropD);
1693 for(subs4.Start(); subs4.More(); subs4.Next()) {
1694 Handle(StepShape_ShapeDefinitionRepresentation) SDR =
1695 Handle(StepShape_ShapeDefinitionRepresentation)::DownCast(subs4.Value());
1696 if(!SDR.IsNull()) {
1697 Handle(StepRepr_Representation) Repr = SDR->UsedRepresentation();
1698 if( !Repr.IsNull() && Repr->NbItems()>0 ) {
1699 RI = Repr->ItemsValue(1);
1700 }
1701 }
1702 }
1703 if(RI.IsNull()) continue;
1704 // read DGT entities:
1705 subs3 = graph.Sharings(SA);
1706 for(subs3.Start(); subs3.More(); subs3.Next()) {
1707 if(subs3.Value()->IsKind(STANDARD_TYPE(StepShape_DimensionalSize))) {
1708 // read dimensions
1709 Handle(StepShape_EdgeCurve) EC = Handle(StepShape_EdgeCurve)::DownCast(RI);
1710 if(EC.IsNull()) continue;
1711 Handle(TCollection_HAsciiString) aName;
1712 Handle(StepShape_DimensionalSize) DimSize =
1713 Handle(StepShape_DimensionalSize)::DownCast(subs3.Value());
1714 Standard_Real dim1=-1.,dim2=-1.;
1715 subs4 = graph.Sharings(DimSize);
1716 for(subs4.Start(); subs4.More(); subs4.Next()) {
1717 Handle(StepShape_DimensionalCharacteristicRepresentation) DimCharR =
1718 Handle(StepShape_DimensionalCharacteristicRepresentation)::DownCast(subs4.Value());
1719 if(!DimCharR.IsNull()) {
1720 Handle(StepShape_ShapeDimensionRepresentation) SDimR = DimCharR->Representation();
1721 if(!SDimR.IsNull() && SDimR->NbItems()>0) {
1722 Handle(StepRepr_RepresentationItem) RI = SDimR->ItemsValue(1);
1723 Handle(StepRepr_ValueRange) VR = Handle(StepRepr_ValueRange)::DownCast(RI);
1724 if(!VR.IsNull()) {
1725 aName = VR->Name();
1726 //StepRepr_CompoundItemDefinition CID = VR->ItemElement();
1727 //if(CID.IsNull()) continue;
1728 //Handle(StepRepr_CompoundItemDefinitionMember) CIDM =
1729 // Handle(StepRepr_CompoundItemDefinitionMember)::DownCast(CID.Value());
1730 //if(CIDM.IsNull()) continue;
1731 //if(CIDM->ArrTransient().IsNull()) continue;
1732 //Handle(StepRepr_HArray1OfRepresentationItem) HARI;
1733 //if(CID.CaseMem(CIDM)==1)
1734 // HARI = CID.ListRepresentationItem();
1735 //if(CID.CaseMem(CIDM)==2)
1736 // HARI = CID.SetRepresentationItem();
1737 Handle(StepRepr_HArray1OfRepresentationItem) HARI = VR->ItemElement();
1738 if(HARI.IsNull()) continue;
1739 if(HARI->Length()>0) {
1740 Handle(StepRepr_RepresentationItem) RI1 =
1741 Handle(StepRepr_RepresentationItem)::DownCast(HARI->Value(1));
1742 if(RI1.IsNull()) continue;
1743 if(RI1->IsKind(STANDARD_TYPE(StepRepr_ReprItemAndLengthMeasureWithUnit))) {
1744 Handle(StepRepr_ReprItemAndLengthMeasureWithUnit) RILMWU =
1745 Handle(StepRepr_ReprItemAndLengthMeasureWithUnit)::DownCast(RI1);
1746 dim1 = RILMWU->GetMeasureWithUnit()->ValueComponent();
1747 StepBasic_Unit anUnit = RILMWU->GetMeasureWithUnit()->UnitComponent();
1748 Standard_Real afact=1.;
1749 if(anUnit.IsNull()) continue;
1750 if( !(anUnit.CaseNum(anUnit.Value())==1) ) continue;
1751 Handle(StepBasic_NamedUnit) NU = anUnit.NamedUnit();
1752 if(GetLengthConversionFactor(NU,afact)) dim1=dim1*afact;
1753 }
1754 }
1755 if(HARI->Length()>1) {
1756 Handle(StepRepr_RepresentationItem) RI2 =
1757 Handle(StepRepr_RepresentationItem)::DownCast(HARI->Value(2));
1758 if(RI2.IsNull()) continue;
1759 if(RI2->IsKind(STANDARD_TYPE(StepRepr_ReprItemAndLengthMeasureWithUnit))) {
1760 Handle(StepRepr_ReprItemAndLengthMeasureWithUnit) RILMWU =
1761 Handle(StepRepr_ReprItemAndLengthMeasureWithUnit)::DownCast(RI2);
1762 dim2 = RILMWU->GetMeasureWithUnit()->ValueComponent();
1763 StepBasic_Unit anUnit = RILMWU->GetMeasureWithUnit()->UnitComponent();
1764 Standard_Real afact=1.;
1765 if(anUnit.IsNull()) continue;
1766 if( !(anUnit.CaseNum(anUnit.Value())==1) ) continue;
1767 Handle(StepBasic_NamedUnit) NU = anUnit.NamedUnit();
1768 if(GetLengthConversionFactor(NU,afact)) dim2 = dim2*afact;
1769 }
1770 }
1771 }
1772 }
1773 }
1774 }
1775 if(dim1<0) continue;
1776 if(dim2<0) dim2=dim1;
1777 //cout<<"DimensionalSize: dim1="<<dim1<<" dim2="<<dim2<<endl;
1778 // now we know edge_curve and value range therefore
1779 // we can create corresponding D&GT labels
1780 Standard_Integer index = TP->MapIndex(EC);
1781 TopoDS_Shape aSh;
1782 if(index >0) {
1783 Handle(Transfer_Binder) binder = TP->MapItem(index);
1784 aSh = TransferBRep::ShapeResult(binder);
1785 }
1786 if(aSh.IsNull()) continue;
1787 TDF_Label shL;
1788 if( !STool->Search(aSh, shL, Standard_True, Standard_True, Standard_True) ) continue;
1789 Handle(TColStd_HArray1OfReal) arr = new TColStd_HArray1OfReal(1,2);
1790 arr->SetValue(1,dim1);
1791 arr->SetValue(2,dim2);
1792 DGTTool->SetDimTol(shL,1,arr,aName,DimSize->Name());
1793 }
1794 // read tolerances and datums
1795 else if(subs3.Value()->IsKind(STANDARD_TYPE(StepDimTol_GeometricTolerance))) {
1796 Handle(StepDimTol_GeometricTolerance) GT =
1797 Handle(StepDimTol_GeometricTolerance)::DownCast(subs3.Value());
1798 // read common data for tolerance
1799 //Standard_Real dim = GT->Magnitude()->ValueComponent();
1800 Handle (StepBasic_MeasureWithUnit) dim3 = GT->Magnitude();
1801 if(dim3.IsNull()) continue;
1802 Standard_Real dim = dim3->ValueComponent();
1803 StepBasic_Unit anUnit = GT->Magnitude()->UnitComponent();
1804 Standard_Real afact=1.;
1805 if(anUnit.IsNull()) continue;
1806 if( !(anUnit.CaseNum(anUnit.Value())==1) ) continue;
1807 Handle(StepBasic_NamedUnit) NU = anUnit.NamedUnit();
1808 if(GetLengthConversionFactor(NU,afact)) dim = dim*afact;
1809 //cout<<"GeometricTolerance: Magnitude = "<<dim<<endl;
1810 Handle(TColStd_HArray1OfReal) arr = new TColStd_HArray1OfReal(1,1);
1811 arr->SetValue(1,dim);
1812 Handle(TCollection_HAsciiString) aName = GT->Name();
1813 Handle(TCollection_HAsciiString) aDescription = GT->Description();
1814 Handle(StepShape_AdvancedFace) AF = Handle(StepShape_AdvancedFace)::DownCast(RI);
1815 if(AF.IsNull()) continue;
1816 Standard_Integer index = TP->MapIndex(AF);
1817 TopoDS_Shape aSh;
1818 if(index >0) {
1819 Handle(Transfer_Binder) binder = TP->MapItem(index);
1820 aSh = TransferBRep::ShapeResult(binder);
1821 }
1822 if(aSh.IsNull()) continue;
1823 TDF_Label shL;
1824 if( !STool->Search(aSh, shL, Standard_True, Standard_True, Standard_True) ) continue;
1825 // read specific data for tolerance
1826 if(GT->IsKind(STANDARD_TYPE(StepDimTol_GeoTolAndGeoTolWthDatRefAndModGeoTolAndPosTol))) {
1827 Handle(StepDimTol_GeoTolAndGeoTolWthDatRefAndModGeoTolAndPosTol) GTComplex =
1828 Handle(StepDimTol_GeoTolAndGeoTolWthDatRefAndModGeoTolAndPosTol)::DownCast(subs3.Value());
1829 Standard_Integer kind=20;
1830 Handle(StepDimTol_ModifiedGeometricTolerance) MGT =
1831 GTComplex->GetModifiedGeometricTolerance();
1832 if(!MGT.IsNull()) {
1833 kind = kind + MGT->Modifier()+1;
1834 }
1835 TDF_Label TolerL = DGTTool->SetDimTol(shL,kind,arr,aName,aDescription);
1836 // translate datums connected with this tolerance
1837 Handle(StepDimTol_GeometricToleranceWithDatumReference) GTWDR =
1838 GTComplex->GetGeometricToleranceWithDatumReference();
1839 if(!GTWDR.IsNull()) {
1840 ReadDatums(STool,DGTTool,graph,TP,TolerL,GTWDR);
1841 }
1842 }
1843 else if(GT->IsKind(STANDARD_TYPE(StepDimTol_GeometricToleranceWithDatumReference))) {
1844 Handle(StepDimTol_GeometricToleranceWithDatumReference) GTWDR =
1845 Handle(StepDimTol_GeometricToleranceWithDatumReference)::DownCast(subs3.Value());
1846 if(GTWDR.IsNull()) continue;
1847 Standard_Integer kind = 0;
1848 if (GTWDR->IsKind(STANDARD_TYPE(StepDimTol_AngularityTolerance))) kind = 24;
1849 else if(GTWDR->IsKind(STANDARD_TYPE(StepDimTol_CircularRunoutTolerance))) kind = 25;
1850 else if(GTWDR->IsKind(STANDARD_TYPE(StepDimTol_CoaxialityTolerance))) kind = 26;
1851 else if(GTWDR->IsKind(STANDARD_TYPE(StepDimTol_ConcentricityTolerance))) kind = 27;
1852 else if(GTWDR->IsKind(STANDARD_TYPE(StepDimTol_ParallelismTolerance))) kind = 28;
1853 else if(GTWDR->IsKind(STANDARD_TYPE(StepDimTol_PerpendicularityTolerance))) kind = 29;
1854 else if(GTWDR->IsKind(STANDARD_TYPE(StepDimTol_SymmetryTolerance))) kind = 30;
1855 else if(GTWDR->IsKind(STANDARD_TYPE(StepDimTol_TotalRunoutTolerance))) kind = 31;
1856 //cout<<"GTWDR: kind="<<kind<<endl;
1857 TDF_Label TolerL = DGTTool->SetDimTol(shL,kind,arr,aName,aDescription);
1858 ReadDatums(STool,DGTTool,graph,TP,TolerL,GTWDR);
1859 }
1860 else if(GT->IsKind(STANDARD_TYPE(StepDimTol_ModifiedGeometricTolerance))) {
1861 Handle(StepDimTol_ModifiedGeometricTolerance) MGT =
1862 Handle(StepDimTol_ModifiedGeometricTolerance)::DownCast(subs3.Value());
1863 Standard_Integer kind = 35 + MGT->Modifier();
1864 DGTTool->SetDimTol(shL,kind,arr,aName,aDescription);
1865 }
1866 else if(GT->IsKind(STANDARD_TYPE(StepDimTol_CylindricityTolerance))) {
1867 DGTTool->SetDimTol(shL,38,arr,aName,aDescription);
1868 }
1869 else if(GT->IsKind(STANDARD_TYPE(StepDimTol_FlatnessTolerance))) {
1870 DGTTool->SetDimTol(shL,39,arr,aName,aDescription);
1871 }
1872 else if(GT->IsKind(STANDARD_TYPE(StepDimTol_LineProfileTolerance))) {
1873 DGTTool->SetDimTol(shL,40,arr,aName,aDescription);
1874 }
1875 else if(GT->IsKind(STANDARD_TYPE(StepDimTol_PositionTolerance))) {
1876 DGTTool->SetDimTol(shL,41,arr,aName,aDescription);
1877 }
1878 else if(GT->IsKind(STANDARD_TYPE(StepDimTol_RoundnessTolerance))) {
1879 DGTTool->SetDimTol(shL,42,arr,aName,aDescription);
1880 }
1881 else if(GT->IsKind(STANDARD_TYPE(StepDimTol_StraightnessTolerance))) {
1882 DGTTool->SetDimTol(shL,43,arr,aName,aDescription);
1883 }
1884 else if(GT->IsKind(STANDARD_TYPE(StepDimTol_SurfaceProfileTolerance))) {
1885 DGTTool->SetDimTol(shL,44,arr,aName,aDescription);
1886 }
1887 }
1888 }
1889 }
1890 }
1891
1892 return Standard_True;
1893}
1894
1895
1896//=======================================================================
1897//function : FindSolidForPDS
1898//purpose : auxilary
1899//=======================================================================
1900
1901static Handle(StepShape_SolidModel) FindSolidForPDS(const Handle(StepRepr_ProductDefinitionShape) &PDS,
1902 const Interface_Graph &graph)
1903{
1904 Handle(StepShape_SolidModel) SM;
1905 Interface_EntityIterator subs = graph.Sharings(PDS);
1906 Handle(StepShape_ShapeRepresentation) SR;
1907 for(subs.Start(); subs.More() && SM.IsNull(); subs.Next()) {
1908 Handle(StepShape_ShapeDefinitionRepresentation) SDR =
1909 Handle(StepShape_ShapeDefinitionRepresentation)::DownCast(subs.Value());
1910 if(SDR.IsNull()) continue;
1911 SR = Handle(StepShape_ShapeRepresentation)::DownCast(SDR->UsedRepresentation());
1912 if(SR.IsNull()) continue;
1913 for(Standard_Integer i=1; i<=SR->NbItems() && SM.IsNull(); i++) {
1914 SM = Handle(StepShape_SolidModel)::DownCast(SR->ItemsValue(i));
1915 }
1916 if(SM.IsNull()) {
1917 Interface_EntityIterator subs1 = graph.Sharings(SR);
1918 for(subs1.Start(); subs1.More() && SM.IsNull(); subs1.Next()) {
1919 Handle(StepRepr_RepresentationRelationship) RR =
1920 Handle(StepRepr_RepresentationRelationship)::DownCast(subs1.Value());
1921 if(RR.IsNull()) continue;
1922 Handle(StepShape_ShapeRepresentation) SR2;
1923 if(RR->Rep1()==SR) SR2 = Handle(StepShape_ShapeRepresentation)::DownCast(RR->Rep2());
1924 else SR2 = Handle(StepShape_ShapeRepresentation)::DownCast(RR->Rep1());
1925 if(SR2.IsNull()) continue;
1926 for(Standard_Integer i2=1; i2<=SR2->NbItems() && SM.IsNull(); i2++) {
1927 SM = Handle(StepShape_SolidModel)::DownCast(SR2->ItemsValue(i2));
1928 }
1929 }
1930 }
1931 }
1932 return SM;
1933}
1934
1935
1936//=======================================================================
1937//function : ReadMaterials
1938//purpose :
1939//=======================================================================
1940
1941Standard_Boolean STEPCAFControl_Reader::ReadMaterials(const Handle(XSControl_WorkSession) &WS,
1942 Handle(TDocStd_Document)& Doc,
1943 const Handle(TColStd_HSequenceOfTransient) &SeqPDS) const
1944{
1945 Handle(Interface_InterfaceModel) Model = WS->Model();
1946 Handle(XCAFDoc_ShapeTool) STool = XCAFDoc_DocumentTool::ShapeTool( Doc->Main() );
1947 Handle(XSControl_TransferReader) TR = WS->TransferReader();
1948 Handle(Transfer_TransientProcess) TP = TR->TransientProcess();
1949 Handle(XCAFDoc_MaterialTool) MatTool = XCAFDoc_DocumentTool::MaterialTool( Doc->Main() );
1950 if(MatTool.IsNull()) return Standard_False;
1951
1952 const Interface_Graph& graph = TP->Graph();
1953 for(Standard_Integer i=1; i<=SeqPDS->Length(); i++) {
1954 Handle(StepRepr_ProductDefinitionShape) PDS =
1955 Handle(StepRepr_ProductDefinitionShape)::DownCast(SeqPDS->Value(i));
1d2b1ccb
G
1956 if(PDS.IsNull())
1957 continue;
1958 Handle(StepBasic_ProductDefinition) aProdDef = PDS->Definition().ProductDefinition();
1959 if(aProdDef.IsNull())
1960 continue;
7fd59977 1961 Handle(TCollection_HAsciiString) aName = new TCollection_HAsciiString("");
1962 Handle(TCollection_HAsciiString) aDescription = new TCollection_HAsciiString("");
1963 Handle(TCollection_HAsciiString) aDensName = new TCollection_HAsciiString("");
1964 Handle(TCollection_HAsciiString) aDensValType = new TCollection_HAsciiString("");
1965 Standard_Real aDensity=0;
1d2b1ccb 1966 Interface_EntityIterator subs = graph.Sharings( aProdDef);
7fd59977 1967 for(subs.Start(); subs.More(); subs.Next()) {
1968 Handle(StepRepr_PropertyDefinition) PropD =
1969 Handle(StepRepr_PropertyDefinition)::DownCast(subs.Value());
1970 if(PropD.IsNull()) continue;
1971 Interface_EntityIterator subs1 = graph.Sharings(PropD);
1972 for(subs1.Start(); subs1.More(); subs1.Next()) {
1973 Handle(StepRepr_PropertyDefinitionRepresentation) PDR =
1974 Handle(StepRepr_PropertyDefinitionRepresentation)::DownCast(subs1.Value());
1975 if(PDR.IsNull()) continue;
1976 Handle(StepRepr_Representation) Repr = PDR->UsedRepresentation();
1977 if(Repr.IsNull()) continue;
1978 Standard_Integer ir;
1979 for(ir=1; ir<=Repr->NbItems(); ir++) {
1980 Handle(StepRepr_RepresentationItem) RI = Repr->ItemsValue(ir);
1981 if(RI.IsNull()) continue;
1982 if(RI->IsKind(STANDARD_TYPE(StepRepr_DescriptiveRepresentationItem))) {
1983 // find name and description for material
1984 Handle(StepRepr_DescriptiveRepresentationItem) DRI =
1985 Handle(StepRepr_DescriptiveRepresentationItem)::DownCast(RI);
1986 aName = DRI->Name();
87073a10 1987
7fd59977 1988 aDescription = DRI->Description();
87073a10 1989 if(aName.IsNull())
1990 aName = aDescription;
7fd59977 1991 }
1992 if(RI->IsKind(STANDARD_TYPE(StepRepr_MeasureRepresentationItem))) {
1993 // try to find density for material
1994 Handle(StepRepr_MeasureRepresentationItem) MRI =
1995 Handle(StepRepr_MeasureRepresentationItem)::DownCast(RI);
1996 aDensity = MRI->Measure()->ValueComponent();
1997 aDensName = MRI->Name();
1998 aDensValType = new TCollection_HAsciiString(MRI->Measure()->ValueComponentMember()->Name());
1999 StepBasic_Unit aUnit = MRI->Measure()->UnitComponent();
2000 if(!aUnit.IsNull()) {
2001 Handle(StepBasic_DerivedUnit) DU = aUnit.DerivedUnit();
2002 if(DU.IsNull()) continue;
2003 for(Standard_Integer idu=1; idu<=DU->NbElements(); idu++) {
2004 Handle(StepBasic_DerivedUnitElement) DUE = DU->ElementsValue(idu);
2005 Handle(StepBasic_NamedUnit) NU = DUE->Unit();
2006 Standard_Real afact=1.;
2007 if(NU->IsKind(STANDARD_TYPE(StepBasic_ConversionBasedUnitAndLengthUnit))) {
2008 if(GetLengthConversionFactor(NU,afact)) aDensity = aDensity/(afact*afact*afact);
2009 // transfer length value for Density from millimeter to santimeter
2010 // in order to result density has dimension gram/(sm*sm*sm)
2011 aDensity = aDensity*1000.;
2012 }
2013 if(NU->IsKind(STANDARD_TYPE(StepBasic_ConversionBasedUnitAndMassUnit))) {
2014 if(GetMassConversionFactor(NU,afact)) aDensity=aDensity*afact;
2015 }
2016 }
2017 }
2018 }
2019 }
2020 }
2021 }
87073a10 2022
2023 if( aName.IsNull() || aName->Length()==0 )
2024 continue;
7fd59977 2025 // find shape label amd create Material link
2026 TopoDS_Shape aSh;
2027 Handle(StepShape_SolidModel) SM = FindSolidForPDS(PDS,graph);
2028 if(!SM.IsNull()) {
2029 Standard_Integer index = TP->MapIndex(SM);
2030 if(index >0) {
2031 Handle(Transfer_Binder) binder = TP->MapItem(index);
2032 if(!binder.IsNull())
2033 aSh = TransferBRep::ShapeResult(binder);
2034 }
2035 }
2036 if(aSh.IsNull()) continue;
2037 TDF_Label shL;
2038 if( !STool->Search(aSh, shL, Standard_True, Standard_True, Standard_True) ) continue;
2039 MatTool->SetMaterial(shL,aName,aDescription,aDensity,aDensName,aDensValType);
2040 }
2041
2042 return Standard_True;
2043}
2044
02a0b964 2045//=======================================================================
2046//function : SettleShapeData
2047//purpose :
2048//=======================================================================
2049
2050TDF_Label STEPCAFControl_Reader::SettleShapeData(const Handle(StepRepr_RepresentationItem)& theItem,
2051 TDF_Label& theLab,
2052 const Handle(XCAFDoc_ShapeTool)& theShapeTool,
2053 const Handle(Transfer_TransientProcess)& TP) const
2054{
2055 TDF_Label aResult = theLab;
2056
2057 Handle(TCollection_HAsciiString) hName = theItem->Name();
2058 if ( hName.IsNull() || hName->IsEmpty() )
2059 return aResult;
2060
2061 Handle(Transfer_Binder) aBinder = TP->Find(theItem);
2062 if ( aBinder.IsNull() )
2063 return aResult;
2064
2065 TopoDS_Shape aShape = TransferBRep::ShapeResult(aBinder);
2066 if ( aShape.IsNull() )
2067 return aResult;
2068
2069 // Allocate sub-Label
2070 aResult = AllocateSubLabel(theLab);
2071
2072 TCollection_AsciiString aName = hName->String();
2073 TDataStd_Name::Set(aResult, aName);
2074 theShapeTool->SetShape(aResult, aShape);
2075
2076 return aResult;
2077}
2078
2079//=======================================================================
2080//function : ExpandSubShapes
2081//purpose :
2082//=======================================================================
2083
2084void STEPCAFControl_Reader::ExpandSubShapes(const Handle(XCAFDoc_ShapeTool)& ShapeTool,
2085 const XCAFDoc_DataMapOfShapeLabel& ShapeLabelMap,
2086 const STEPCAFControl_DataMapOfShapePD& ShapePDMap) const
2087{
2088 const Handle(Interface_InterfaceModel)& Model = Reader().WS()->Model();
2089 const Handle(Transfer_TransientProcess)& TP = Reader().WS()->TransferReader()->TransientProcess();
2090 NCollection_DataMap<TopoDS_Shape, Handle(TCollection_HAsciiString)> ShapeNameMap;
2091 TColStd_MapOfTransient aRepItems;
2092
2093 // Read translation control variables
2094 Standard_Boolean doReadSNames = (Interface_Static::IVal("read.stepcaf.subshapes.name") > 0);
2095
2096 if ( !doReadSNames )
2097 return;
2098
2099 const Interface_Graph& Graph = Reader().WS()->Graph();
2100
2101 for ( STEPCAFControl_DataMapIteratorOfDataMapOfShapePD it(ShapePDMap); it.More(); it.Next() )
2102 {
2103 const TopoDS_Shape& aRootShape = it.Key();
2104 const Handle(StepBasic_ProductDefinition)& aPDef = it.Value();
2105 if ( aPDef.IsNull() )
2106 continue;
2107
2108 // Find SDR by Product
2109 Handle(StepShape_ShapeDefinitionRepresentation) aSDR;
2110 Interface_EntityIterator entIt = Graph.TypedSharings( aPDef, STANDARD_TYPE(StepShape_ShapeDefinitionRepresentation) );
2111 for ( entIt.Start(); entIt.More(); entIt.Next() )
2112 {
2113 const Handle(Standard_Transient)& aReferer = entIt.Value();
2114 aSDR = Handle(StepShape_ShapeDefinitionRepresentation)::DownCast(aReferer);
2115 if ( !aSDR.IsNull() )
2116 break;
2117 }
2118
2119 if ( aSDR.IsNull() )
2120 continue;
2121
2122 // Access shape representation
2123 Handle(StepShape_ShapeRepresentation)
2124 aShapeRepr = Handle(StepShape_ShapeRepresentation)::DownCast( aSDR->UsedRepresentation() );
2125
2126 if ( aShapeRepr.IsNull() )
2127 continue;
2128
2129 // Access representation items
2130 Handle(StepRepr_HArray1OfRepresentationItem) aReprItems = aShapeRepr->Items();
2131
2132 if ( aReprItems.IsNull() )
2133 continue;
2134
2135 if ( !ShapeLabelMap.IsBound(aRootShape) )
2136 continue;
2137
2138 TDF_Label aRootLab = ShapeLabelMap.Find(aRootShape);
2139
2140 StepRepr_SequenceOfRepresentationItem aMSBSeq;
2141 StepRepr_SequenceOfRepresentationItem aSBSMSeq;
2142
2143 // Iterate over the top level representation items collecting the
2144 // topological containers to expand
2145 for ( Standard_Integer i = aReprItems->Lower(); i <= aReprItems->Upper(); ++i )
2146 {
2147 Handle(StepRepr_RepresentationItem) aTRepr = aReprItems->Value(i);
2148 if ( aTRepr->IsKind( STANDARD_TYPE(StepShape_ManifoldSolidBrep) ) )
2149 aMSBSeq.Append(aTRepr);
2150 else if ( aTRepr->IsKind( STANDARD_TYPE(StepShape_ShellBasedSurfaceModel) ) )
2151 aSBSMSeq.Append(aTRepr);
2152 }
2153
2154 // Insert intermediate OCAF Labels for SOLIDs in case there are more
2155 // than one Manifold Solid BRep in the Shape Representation
2156 Standard_Boolean doInsertSolidLab = (aMSBSeq.Length() > 1);
2157
2158 // Expand Manifold Solid BReps
2159 for ( Standard_Integer i = 1; i <= aMSBSeq.Length(); ++i )
2160 {
2161 const Handle(StepRepr_RepresentationItem)& aManiRepr = aMSBSeq.Value(i);
2162
2163 // Put additional Label for SOLID
2164 TDF_Label aManiLab;
2165 if ( doInsertSolidLab )
2166 aManiLab = SettleShapeData(aManiRepr, aRootLab, ShapeTool, TP);
2167 else
2168 aManiLab = aRootLab;
2169
2170 ExpandManifoldSolidBrep(aManiLab, aMSBSeq.Value(i), TP, ShapeTool);
2171 }
2172
2173 // Expand Shell-Based Surface Models
2174 for ( Standard_Integer i = 1; i <= aSBSMSeq.Length(); ++i )
2175 ExpandSBSM(aRootLab, aSBSMSeq.Value(i), TP, ShapeTool);
2176 }
2177}
2178
2179//=======================================================================
2180//function : ExpandManifoldSolidBrep
2181//purpose :
2182//=======================================================================
2183
2184void STEPCAFControl_Reader::ExpandManifoldSolidBrep(TDF_Label& ShapeLab,
2185 const Handle(StepRepr_RepresentationItem)& Repr,
2186 const Handle(Transfer_TransientProcess)& TP,
2187 const Handle(XCAFDoc_ShapeTool)& ShapeTool) const
2188{
2189 // Access outer shell
2190 Handle(StepShape_ManifoldSolidBrep) aMSB = Handle(StepShape_ManifoldSolidBrep)::DownCast(Repr);
2191 Handle(StepShape_ClosedShell) aShell = aMSB->Outer();
2192
2193 // Expand shell contents to CAF tree
2194 ExpandShell(aShell, ShapeLab, TP, ShapeTool);
2195}
2196
2197//=======================================================================
2198//function : ExpandSBSM
2199//purpose :
2200//=======================================================================
2201
2202void STEPCAFControl_Reader::ExpandSBSM(TDF_Label& ShapeLab,
2203 const Handle(StepRepr_RepresentationItem)& Repr,
2204 const Handle(Transfer_TransientProcess)& TP,
2205 const Handle(XCAFDoc_ShapeTool)& ShapeTool) const
2206{
2207 Handle(StepShape_ShellBasedSurfaceModel) aSBSM = Handle(StepShape_ShellBasedSurfaceModel)::DownCast(Repr);
2208
2209 // Access boundary shells
2210 Handle(StepShape_HArray1OfShell) aShells = aSBSM->SbsmBoundary();
2211 for ( Standard_Integer s = aShells->Lower(); s <= aShells->Upper(); ++s )
2212 {
2213 const StepShape_Shell& aShell = aShells->Value(s);
2214 Handle(StepShape_ConnectedFaceSet) aCFS;
2215 Handle(StepShape_OpenShell) anOpenShell = aShell.OpenShell();
2216 Handle(StepShape_ClosedShell) aClosedShell = aShell.ClosedShell();
2217
2218 if ( !anOpenShell.IsNull() )
2219 aCFS = anOpenShell;
2220 else
2221 aCFS = aClosedShell;
2222
2223 ExpandShell(aCFS, ShapeLab, TP, ShapeTool);
2224 }
2225}
2226
2227//=======================================================================
2228//function : ExpandShell
2229//purpose :
2230//=======================================================================
2231
2232void STEPCAFControl_Reader::ExpandShell(const Handle(StepShape_ConnectedFaceSet)& Shell,
2233 TDF_Label& RootLab,
2234 const Handle(Transfer_TransientProcess)& TP,
2235 const Handle(XCAFDoc_ShapeTool)& ShapeTool) const
2236{
2237 // Record CAF data
2238 TDF_Label aShellLab = SettleShapeData(Shell, RootLab, ShapeTool, TP);
2239
2240 // Access faces
2241 Handle(StepShape_HArray1OfFace) aFaces = Shell->CfsFaces();
2242 for ( Standard_Integer f = aFaces->Lower(); f <= aFaces->Upper(); ++f )
2243 {
2244 const Handle(StepShape_Face)& aFace = aFaces->Value(f);
2245
2246 // Record CAF data
2247 TDF_Label aFaceLab = SettleShapeData(aFace, aShellLab, ShapeTool, TP);
2248
2249 // Access face bounds
2250 Handle(StepShape_HArray1OfFaceBound) aWires = aFace->Bounds();
2251 for ( Standard_Integer w = aWires->Lower(); w <= aWires->Upper(); ++w )
2252 {
2253 const Handle(StepShape_Loop)& aWire = aWires->Value(w)->Bound();
2254
2255 // Record CAF data
2256 TDF_Label aWireLab = SettleShapeData(aWire, aFaceLab, ShapeTool, TP);
2257
2258 // Access wire edges
2259 // Currently only EDGE LOOPs are considered (!)
2260 if ( !aWire->IsInstance( STANDARD_TYPE(StepShape_EdgeLoop) ) )
2261 continue;
2262
2263 // Access edges
2264 Handle(StepShape_EdgeLoop) anEdgeLoop = Handle(StepShape_EdgeLoop)::DownCast(aWire);
2265 Handle(StepShape_HArray1OfOrientedEdge) anEdges = anEdgeLoop->EdgeList();
2266 for ( Standard_Integer e = anEdges->Lower(); e <= anEdges->Upper(); ++e )
2267 {
2268 Handle(StepShape_Edge) anEdge = anEdges->Value(e)->EdgeElement();
2269
2270 // Record CAF data
2271 TDF_Label anEdgeLab = SettleShapeData(anEdge, aWireLab, ShapeTool, TP);
2272
2273 // Access vertices
2274 Handle(StepShape_Vertex) aV1 = anEdge->EdgeStart();
2275 Handle(StepShape_Vertex) aV2 = anEdge->EdgeEnd();
2276
2277 // Record CAF data
2278 SettleShapeData(aV1, anEdgeLab, ShapeTool, TP);
2279 SettleShapeData(aV2, anEdgeLab, ShapeTool, TP);
2280 }
2281 }
2282 }
2283}
7fd59977 2284
2285//=======================================================================
2286//function : SetColorMode
2287//purpose :
2288//=======================================================================
2289
2290void STEPCAFControl_Reader::SetColorMode (const Standard_Boolean colormode)
2291{
2292 myColorMode = colormode;
2293}
2294
2295//=======================================================================
2296//function : GetColorMode
2297//purpose :
2298//=======================================================================
2299
2300Standard_Boolean STEPCAFControl_Reader::GetColorMode () const
2301{
2302 return myColorMode;
2303}
2304
2305//=======================================================================
2306//function : SetNameMode
2307//purpose :
2308//=======================================================================
2309
2310void STEPCAFControl_Reader::SetNameMode (const Standard_Boolean namemode)
2311{
2312 myNameMode = namemode;
2313}
2314
2315//=======================================================================
2316//function : GetNameMode
2317//purpose :
2318//=======================================================================
2319
2320Standard_Boolean STEPCAFControl_Reader::GetNameMode () const
2321{
2322 return myNameMode;
2323}
2324
2325//=======================================================================
2326//function : SetLayerMode
2327//purpose :
2328//=======================================================================
2329
2330void STEPCAFControl_Reader::SetLayerMode (const Standard_Boolean layermode)
2331{
2332 myLayerMode = layermode;
2333}
2334
2335//=======================================================================
2336//function : GetLayerMode
2337//purpose :
2338//=======================================================================
2339
2340Standard_Boolean STEPCAFControl_Reader::GetLayerMode () const
2341{
2342 return myLayerMode;
2343}
2344
2345//=======================================================================
2346//function : SetPropsMode
2347//purpose :
2348//=======================================================================
2349
2350void STEPCAFControl_Reader::SetPropsMode (const Standard_Boolean propsmode)
2351{
2352 myPropsMode = propsmode;
2353}
2354
2355//=======================================================================
2356//function : GetPropsMode
2357//purpose :
2358//=======================================================================
2359
2360Standard_Boolean STEPCAFControl_Reader::GetPropsMode () const
2361{
2362 return myPropsMode;
2363}
2364
2365//=======================================================================
2366//function : SetSHUOMode
2367//purpose :
2368//=======================================================================
2369
2370void STEPCAFControl_Reader::SetSHUOMode (const Standard_Boolean mode)
2371{
2372 mySHUOMode = mode;
2373}
2374
2375//=======================================================================
2376//function : GetSHUOMode
2377//purpose :
2378//=======================================================================
2379
2380Standard_Boolean STEPCAFControl_Reader::GetSHUOMode () const
2381{
2382 return mySHUOMode;
2383}
2384
2385//=======================================================================
2386//function : SetGDTMode
2387//purpose :
2388//=======================================================================
2389
2390void STEPCAFControl_Reader::SetGDTMode (const Standard_Boolean gdtmode)
2391{
2392 myGDTMode = gdtmode;
2393}
2394
2395//=======================================================================
2396//function : GetGDTMode
2397//purpose :
2398//=======================================================================
2399
2400Standard_Boolean STEPCAFControl_Reader::GetGDTMode () const
2401{
2402 return myGDTMode;
2403}
2404
2405
2406//=======================================================================
2407//function : SetMatMode
2408//purpose :
2409//=======================================================================
2410
2411void STEPCAFControl_Reader::SetMatMode (const Standard_Boolean matmode)
2412{
2413 myMatMode = matmode;
2414}
2415
2416//=======================================================================
2417//function : GetMatMode
2418//purpose :
2419//=======================================================================
2420
2421Standard_Boolean STEPCAFControl_Reader::GetMatMode () const
2422{
2423 return myMatMode;
2424}