0023384: Translate sub-shape names between XDE document and STEP
[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();
1072 else if ( PDR->Name()->Length() >0 ) name = PDR->Name();
1073 else name = PDR->Id();
1074 }
1075 // find proper label
1076 L = FindInstance ( NAUO, STool, Tool, PDFileMap, ShapeLabelMap );
1077 if ( L.IsNull() ) continue;
1078 TCollection_ExtendedString str ( name->String() );
1079 TDataStd_Name::Set ( L, str );
1080 }
1081
1082 // for PD get name of associated product
1083 if ( enti->DynamicType() == tPD ) {
1084 L.Nullify();
1085 Handle(StepBasic_ProductDefinition) PD =
1086 Handle(StepBasic_ProductDefinition)::DownCast(enti);
1087 if(PD.IsNull()) continue;
1088 Handle(StepBasic_Product) Prod = PD->Formation()->OfProduct();
1089 if(Prod->Name()->UsefullLength()>0) name = Prod->Name();
1090 else name = Prod->Id();
1091 L = GetLabelFromPD ( PD, STool, TP, PDFileMap, ShapeLabelMap );
1092 if ( L.IsNull() ) continue;
1093 TCollection_ExtendedString str ( name->String() );
1094 TDataStd_Name::Set ( L, str );
1095 }
1096 // set a name to the document
1097 //TCollection_ExtendedString str ( name->String() );
1098 //TDataStd_Name::Set ( L, str );
1099 }
1100
1101 return Standard_True;
1102}
1103
1104//=======================================================================
1105//function : GetLabelFromPD
1106//purpose :
1107//=======================================================================
1108
1109static TDF_Label GetLabelFromPD (const Handle(StepBasic_ProductDefinition) &PD,
1110 const Handle(XCAFDoc_ShapeTool) &STool,
1111 const STEPConstruct_ValidationProps &Props,
1112 const STEPCAFControl_DataMapOfPDExternFile &PDFileMap,
1113 const XCAFDoc_DataMapOfShapeLabel &ShapeLabelMap)
1114{
1115 TDF_Label L;
1116 if ( PDFileMap.IsBound ( PD ) ) {
1117 Handle(STEPCAFControl_ExternFile) EF = PDFileMap.Find ( PD );
1118 if ( ! EF.IsNull() ) {
1119 L = EF->GetLabel();
1120 if ( ! L.IsNull() ) return L;
1121 }
1122 }
1123 TopoDS_Shape S = Props.GetPropShape ( PD );
1124 if ( S.IsNull() ) return L;
1125 if ( ShapeLabelMap.IsBound ( S ) )
1126 L = ShapeLabelMap.Find ( S );
1127 if ( L.IsNull() )
1128 STool->Search ( S, L, Standard_True, Standard_True, Standard_False );
1129 return L;
1130}
1131
1132//=======================================================================
1133//function : ReadValProps
1134//purpose :
1135//=======================================================================
1136
1137Standard_Boolean STEPCAFControl_Reader::ReadValProps (const Handle(XSControl_WorkSession) &WS,
1138 Handle(TDocStd_Document)& Doc,
1139 const STEPCAFControl_DataMapOfPDExternFile &PDFileMap,
1140 const XCAFDoc_DataMapOfShapeLabel &ShapeLabelMap) const
1141{
1142 // get starting data
1143 Handle(Interface_InterfaceModel) Model = WS->Model();
1144 Handle(XSControl_TransferReader) TR = WS->TransferReader();
1145 Handle(Transfer_TransientProcess) TP = TR->TransientProcess();
1146 Handle(XCAFDoc_ShapeTool) STool = XCAFDoc_DocumentTool::ShapeTool( Doc->Main() );
1147 if ( STool.IsNull() ) return Standard_False;
1148
1149 // load props from the STEP model
1150 TColStd_SequenceOfTransient props;
1151 STEPConstruct_ValidationProps Props ( WS );
1152 if ( ! Props.LoadProps ( props ) ) {
1153#ifdef DEB
1154 cout << "Warning: no validation props found in the model" << endl;
1155#endif
1156 return Standard_False;
1157 }
1158
1159 // interpret props one by one
1160 for (Standard_Integer i = 1; i <= props.Length(); i ++) {
1161 Handle(StepRepr_PropertyDefinitionRepresentation) PDR =
1162 Handle(StepRepr_PropertyDefinitionRepresentation)::DownCast ( props.Value(i) );
1163 if ( PDR.IsNull() ) continue;
1164
1165 TDF_Label L;
1166
1167 Handle(StepRepr_PropertyDefinition) PD = PDR->Definition().PropertyDefinition();
1168 Interface_EntityIterator subs = Props.Graph().Shareds(PD);
1169 for (subs.Start(); L.IsNull() && subs.More(); subs.Next()) {
1170 if ( subs.Value()->IsKind(STANDARD_TYPE(StepRepr_ProductDefinitionShape)) ) {
1171 Handle(StepRepr_ProductDefinitionShape) PDS = Handle(StepRepr_ProductDefinitionShape)::DownCast(subs.Value());
1172 if(PDS.IsNull()) continue;
1173 // find corresponding NAUO
1174 Handle(StepRepr_NextAssemblyUsageOccurrence) NAUO;
1175 Interface_EntityIterator subs1 = Props.Graph().Shareds(PDS);
1176 for (subs1.Start(); NAUO.IsNull() && subs1.More(); subs1.Next()) {
1177 if ( subs1.Value()->IsKind(STANDARD_TYPE(StepRepr_NextAssemblyUsageOccurrence)) )
1178 NAUO = Handle(StepRepr_NextAssemblyUsageOccurrence)::DownCast(subs1.Value());
1179 }
1180 if ( !NAUO.IsNull() ) {
1181 L = FindInstance ( NAUO, STool, WS, PDFileMap, ShapeLabelMap );
1182 if ( L.IsNull() ) continue;
1183 }
1184 else {
1185 // find corresponding ProductDefinition:
1186 Handle(StepBasic_ProductDefinition) ProdDef;
1187 Interface_EntityIterator subsPDS = Props.Graph().Shareds(PDS);
1188 for (subsPDS.Start(); ProdDef.IsNull() && subsPDS.More(); subsPDS.Next()) {
1189 if ( subsPDS.Value()->IsKind(STANDARD_TYPE(StepBasic_ProductDefinition)) )
1190 ProdDef = Handle(StepBasic_ProductDefinition)::DownCast(subsPDS.Value());
1191 }
1192 if ( ProdDef.IsNull() ) continue;
1193 L = GetLabelFromPD ( ProdDef, STool, Props, PDFileMap, ShapeLabelMap );
1194 }
1195 }
1196
1197 if ( subs.Value()->IsKind(STANDARD_TYPE(StepRepr_ShapeAspect)) ) {
1198 Handle(StepRepr_ShapeAspect) SA = Handle(StepRepr_ShapeAspect)::DownCast(subs.Value());
1199 if(SA.IsNull()) continue;
1200 // find ShapeRepresentation
1201 Handle(StepShape_ShapeRepresentation) SR;
1202 Interface_EntityIterator subs1 = Props.Graph().Sharings(SA);
1203 for(subs1.Start(); subs1.More() && SR.IsNull(); subs1.Next()) {
1204 Handle(StepRepr_PropertyDefinition) PropD1 =
1205 Handle(StepRepr_PropertyDefinition)::DownCast(subs1.Value());
1206 if(PropD1.IsNull()) continue;
1207 Interface_EntityIterator subs2 = Props.Graph().Sharings(PropD1);
1208 for(subs2.Start(); subs2.More() && SR.IsNull(); subs2.Next()) {
1209 Handle(StepShape_ShapeDefinitionRepresentation) SDR =
1210 Handle(StepShape_ShapeDefinitionRepresentation)::DownCast(subs2.Value());
1211 if(SDR.IsNull()) continue;
1212 SR = Handle(StepShape_ShapeRepresentation)::DownCast(SDR->UsedRepresentation());
1213 }
1214 }
1215 if(SR.IsNull()) continue;
1216 Handle(Transfer_Binder) binder;
1217 for(Standard_Integer ir=1; ir<=SR->NbItems() && binder.IsNull(); ir++) {
1218 if(SR->ItemsValue(ir)->IsKind(STANDARD_TYPE(StepShape_SolidModel))) {
1219 Handle(StepShape_SolidModel) SM =
1220 Handle(StepShape_SolidModel)::DownCast(SR->ItemsValue(ir));
1221 binder = TP->Find(SM);
1222 }
1223 else if(SR->ItemsValue(ir)->IsKind(STANDARD_TYPE(StepShape_ShellBasedSurfaceModel))) {
1224 Handle(StepShape_ShellBasedSurfaceModel) SBSM =
1225 Handle(StepShape_ShellBasedSurfaceModel)::DownCast(SR->ItemsValue(ir));
1226 binder = TP->Find(SBSM);
1227 }
1228 else if(SR->ItemsValue(ir)->IsKind(STANDARD_TYPE(StepShape_GeometricSet))) {
1229 Handle(StepShape_GeometricSet) GS =
1230 Handle(StepShape_GeometricSet)::DownCast(SR->ItemsValue(ir));
1231 binder = TP->Find(GS);
1232 }
1233 }
1234 if ( binder.IsNull() || ! binder->HasResult() ) continue;
1235 TopoDS_Shape S;
1236 S = TransferBRep::ShapeResult ( TP, binder );
1237 if(S.IsNull()) continue;
1238 if ( ShapeLabelMap.IsBound ( S ) )
1239 L = ShapeLabelMap.Find ( S );
1240 if ( L.IsNull() )
1241 STool->Search ( S, L, Standard_True, Standard_True, Standard_True );
1242 }
1243 }
1244
1245 if(L.IsNull()) continue;
1246
1247 // decode validation properties
1248 Handle(StepRepr_Representation) rep = PDR->UsedRepresentation();
1249 for ( Standard_Integer j=1; j <= rep->NbItems(); j++ ) {
1250 Handle(StepRepr_RepresentationItem) ent = rep->ItemsValue(j);
1251 Standard_Boolean isArea;
1252 Standard_Real val;
1253 gp_Pnt pos;
1254 if ( Props.GetPropReal ( ent, val, isArea ) ) {
1255 if ( isArea ) XCAFDoc_Area::Set ( L, val );
1256 else XCAFDoc_Volume::Set ( L, val );
1257 }
1258 else if ( Props.GetPropPnt ( ent, rep->ContextOfItems(), pos ) ) {
1259 XCAFDoc_Centroid::Set ( L, pos );
1260 }
1261 }
1262 }
1263 return Standard_True;
1264}
1265
1266//=======================================================================
1267//function : ReadLayers
1268//purpose :
1269//=======================================================================
1270
1271Standard_Boolean STEPCAFControl_Reader::ReadLayers (const Handle(XSControl_WorkSession) &WS,
1272 Handle(TDocStd_Document)& Doc) const
1273{
1274 Handle(Interface_InterfaceModel) Model = WS->Model();
1275 Handle(XSControl_TransferReader) TR = WS->TransferReader();
1276 Handle(Transfer_TransientProcess) TP = TR->TransientProcess();
1277 Handle(XCAFDoc_ShapeTool) STool = XCAFDoc_DocumentTool::ShapeTool( Doc->Main() );
1278 if ( STool.IsNull() ) return Standard_False;
1279 Handle(XCAFDoc_LayerTool) LTool = XCAFDoc_DocumentTool::LayerTool( Doc->Main() );
1280 if ( LTool.IsNull() ) return Standard_False;
1281
1282 Handle(Standard_Type) tSVPLA = STANDARD_TYPE(StepVisual_PresentationLayerAssignment);
1283 Standard_Integer nb = Model->NbEntities();
1284 Handle(TCollection_HAsciiString) name;
1285
1286 for (Standard_Integer i = 1; i <= nb; i ++) {
1287 Handle(Standard_Transient) enti = Model->Value(i);
1288 if ( ! enti->IsKind ( tSVPLA ) ) continue;
1289 Handle(StepVisual_PresentationLayerAssignment) SVPLA =
1290 Handle(StepVisual_PresentationLayerAssignment)::DownCast(enti);
1291
1292 Handle(TCollection_HAsciiString) descr = SVPLA->Description();
1293 Handle(TCollection_HAsciiString) hName = SVPLA->Name();
1294 TCollection_ExtendedString aLayerName ( hName->String() );
1295
1296 // find a target shape and its label in the document
1297 for (Standard_Integer j = 1; j <= SVPLA->NbAssignedItems(); j++ ) {
1298 StepVisual_LayeredItem LI = SVPLA->AssignedItemsValue(j);
1299 Handle(Transfer_Binder) binder = TP->Find( LI.Value() );
1300 if ( binder.IsNull() || ! binder->HasResult() ) continue;
1301
1302 TopoDS_Shape S = TransferBRep::ShapeResult ( TP, binder );
1303 if ( S.IsNull() ) continue;
1304
1305 TDF_Label shL;
1306 if ( ! STool->Search ( S, shL, Standard_True, Standard_True, Standard_True ) ) continue;
1307 LTool->SetLayer ( shL, aLayerName );
1308 }
1309
1310 // check invisibility
1311 Interface_EntityIterator subs = WS->Graph().Sharings(SVPLA);
1312 for (subs.Start(); subs.More(); subs.Next()) {
1313 if ( ! subs.Value()->IsKind(STANDARD_TYPE(StepVisual_Invisibility)) ) continue;
1314#ifdef DEB
1315 cout<< "\tLayer \"" << aLayerName << "\" is invisible"<<endl;
1316#endif
1317 //TDF_Label InvLayerLab = LTool->FindLayer(aLayerName);
1318 TDF_Label InvLayerLab = LTool->AddLayer(aLayerName); //skl for OCC3926
1319 Handle(TDataStd_UAttribute) aUAttr;
1320 aUAttr->Set( InvLayerLab, XCAFDoc::InvisibleGUID() );
1321 }
1322 }
1323 return Standard_True;
1324}
1325
1326//=======================================================================
1327//function : ReadSHUOs
1328//purpose :
1329//=======================================================================
1330
1331static Standard_Boolean findNextSHUOlevel (const Handle(XSControl_WorkSession) &WS,
1332 const Handle(StepRepr_SpecifiedHigherUsageOccurrence)& SHUO,
1333 const Handle(XCAFDoc_ShapeTool)& STool,
1334 const STEPCAFControl_DataMapOfPDExternFile &PDFileMap,
1335 const XCAFDoc_DataMapOfShapeLabel &ShapeLabelMap,
1336 TDF_LabelSequence& aLabels)
1337{
1338 Interface_EntityIterator subs = WS->HGraph()->Graph().Sharings(SHUO);
1339 Handle(StepRepr_SpecifiedHigherUsageOccurrence) subSHUO;
1340 for (subs.Start(); subs.More(); subs.Next()) {
1341 if (subs.Value()->IsKind(STANDARD_TYPE(StepRepr_SpecifiedHigherUsageOccurrence))) {
1342 subSHUO = Handle(StepRepr_SpecifiedHigherUsageOccurrence)::DownCast(subs.Value());
1343 break;
1344 }
1345 }
1346 if (subSHUO.IsNull())
1347 return Standard_False;
1348
1349 Handle(StepRepr_NextAssemblyUsageOccurrence) NUNAUO =
1350 Handle(StepRepr_NextAssemblyUsageOccurrence)::DownCast(subSHUO->NextUsage());
1351 if (NUNAUO.IsNull())
1352 return Standard_False;
1353// Handle(Interface_InterfaceModel) Model = WS->Model();
1354// Handle(XSControl_TransferReader) TR = WS->TransferReader();
1355// Handle(Transfer_TransientProcess) TP = TR->TransientProcess();
1356// Handle(Transfer_Binder) binder = TP->Find(NUNAUO);
1357// if ( binder.IsNull() || ! binder->HasResult() )
1358// return Standard_False;
1359// TopoDS_Shape NUSh = TransferBRep::ShapeResult ( TP, binder );
1360 // get label of NAUO next level
1361 TDF_Label NULab;
1362 STEPConstruct_Tool Tool( WS );
1363 NULab = STEPCAFControl_Reader::FindInstance ( NUNAUO, STool, Tool, PDFileMap, ShapeLabelMap );
1364// STool->Search(NUSh, NUlab);
1365 if (NULab.IsNull())
1366 return Standard_False;
1367 aLabels.Append( NULab );
1368 // and check by recurse.
1369 findNextSHUOlevel( WS, subSHUO, STool, PDFileMap, ShapeLabelMap, aLabels );
1370 return Standard_True;
1371}
1372
1373
1374//=======================================================================
1375//function : setSHUOintoDoc
1376//purpose : auxilary
1377//=======================================================================
1378static TDF_Label setSHUOintoDoc (const Handle(XSControl_WorkSession) &WS,
1379 const Handle(StepRepr_SpecifiedHigherUsageOccurrence)& SHUO,
1380 const Handle(XCAFDoc_ShapeTool)& STool,
1381 const STEPCAFControl_DataMapOfPDExternFile &PDFileMap,
1382 const XCAFDoc_DataMapOfShapeLabel &ShapeLabelMap)
1383{
1384 TDF_Label aMainLabel;
1385 // get upper usage NAUO from SHUO.
1386 Handle(StepRepr_NextAssemblyUsageOccurrence) UUNAUO =
1387 Handle(StepRepr_NextAssemblyUsageOccurrence)::DownCast(SHUO->UpperUsage());
1388 Handle(StepRepr_NextAssemblyUsageOccurrence) NUNAUO =
1389 Handle(StepRepr_NextAssemblyUsageOccurrence)::DownCast(SHUO->NextUsage());
1390 if ( UUNAUO.IsNull() || NUNAUO.IsNull() ) {
1391#ifdef DEB
1392 cout << "Warning: " << __FILE__ <<": Upper_usage or Next_usage of styled SHUO is null. Skip it" << endl;
1393#endif
1394 return aMainLabel;
1395 }
1396// Handle(Interface_InterfaceModel) Model = WS->Model();
1397// Handle(XSControl_TransferReader) TR = WS->TransferReader();
1398// Handle(Transfer_TransientProcess) TP = TR->TransientProcess();
1399// TopoDS_Shape UUSh, NUSh;
1400// Handle(Transfer_Binder) binder = TP->Find(UUNAUO);
1401// if ( binder.IsNull() || ! binder->HasResult() )
1402// return aMainLabel;
1403// UUSh = TransferBRep::ShapeResult ( TP, binder );
1404// binder = TP->Find(NUNAUO);
1405// if ( binder.IsNull() || ! binder->HasResult() )
1406// return aMainLabel;
1407// NUSh = TransferBRep::ShapeResult ( TP, binder );
1408
1409 // get first labels for first SHUO attribute
1410 TDF_Label UULab, NULab;
1411 STEPConstruct_Tool Tool( WS );
1412 UULab = STEPCAFControl_Reader::FindInstance ( UUNAUO, STool, Tool, PDFileMap, ShapeLabelMap );
1413 NULab = STEPCAFControl_Reader::FindInstance ( NUNAUO, STool, Tool, PDFileMap, ShapeLabelMap );
1414
1415// STool->Search(UUSh, UULab);
1416// STool->Search(NUSh, NULab);
1417 if (UULab.IsNull() || NULab.IsNull()) return aMainLabel;
1418 //create sequence fo labels to set SHUO structure into the document
1419 TDF_LabelSequence ShuoLabels;
1420 ShuoLabels.Append( UULab );
1421 ShuoLabels.Append( NULab );
1422 // add all other labels of sub SHUO entities
1423 findNextSHUOlevel( WS, SHUO, STool, PDFileMap, ShapeLabelMap, ShuoLabels );
1424 // last accord for SHUO
1425 Handle(XCAFDoc_GraphNode) anSHUOAttr;
1426 if ( STool->SetSHUO( ShuoLabels, anSHUOAttr ) )
1427 aMainLabel = anSHUOAttr->Label();
1428
1429 return aMainLabel;
1430}
1431
1432
1433//=======================================================================
1434//function : ReadSHUOs
1435//purpose :
1436//=======================================================================
1437
1438Standard_Boolean STEPCAFControl_Reader::ReadSHUOs (const Handle(XSControl_WorkSession) &WS,
1439 Handle(TDocStd_Document)& Doc,
1440 const STEPCAFControl_DataMapOfPDExternFile &PDFileMap,
1441 const XCAFDoc_DataMapOfShapeLabel &ShapeLabelMap) const
1442{
1443 // the big part code duplication from ReadColors.
1444 // It is possible to share this code functionality, just to decide how ???
1445 Handle(XCAFDoc_ColorTool) CTool = XCAFDoc_DocumentTool::ColorTool( Doc->Main() );
1446 Handle(XCAFDoc_ShapeTool) STool = CTool->ShapeTool();
1447
1448 STEPConstruct_Styles Styles ( WS );
1449 if ( ! Styles.LoadStyles() ) {
1450#ifdef DEB
1451 cout << "Warning: no styles are found in the model" << endl;
1452#endif
1453 return Standard_False;
1454 }
1455 // searching for invisible items in the model
1456 Handle(TColStd_HSequenceOfTransient) aHSeqOfInvisStyle = new TColStd_HSequenceOfTransient;
1457 Styles.LoadInvisStyles( aHSeqOfInvisStyle );
1458 // parse and search for color attributes
1459 Standard_Integer nb = Styles.NbStyles();
1460 for ( Standard_Integer i=1; i <= nb; i++ ) {
1461 Handle(StepVisual_StyledItem) style = Styles.Style ( i );
1462 if ( style.IsNull() ) continue;
1463
1464 Standard_Boolean IsVisible = Standard_True;
1465 // check the visibility of styled item.
1466 for (Standard_Integer si = 1; si <= aHSeqOfInvisStyle->Length(); si++ ) {
1467 if ( style != aHSeqOfInvisStyle->Value( si ) )
1468 continue;
1469 // found that current style is invisible.
1470#ifdef DEB
1471 cout << "Warning: item No " << i << "(" << style->Item()->DynamicType()->Name() << ") is invisible" << endl;
1472#endif
1473 IsVisible = Standard_False;
1474 break;
1475 }
1476
1477 Handle(StepVisual_Colour) SurfCol, BoundCol, CurveCol;
1478 // check if it is component style
1479 Standard_Boolean IsComponent = Standard_False;
1480 if ( ! Styles.GetColors ( style, SurfCol, BoundCol, CurveCol, IsComponent ) && IsVisible )
1481 continue;
1482 if (!IsComponent)
1483 continue;
1484 Handle(StepShape_ShapeRepresentation) aSR;
1485 findStyledSR( style, aSR );
1486 // search for SR along model
1487 if ( aSR.IsNull() )
1488 continue;
1489 Interface_EntityIterator subs = WS->HGraph()->Graph().Sharings( aSR );
1490 Handle(StepShape_ShapeDefinitionRepresentation) aSDR;
1491 for (subs.Start(); subs.More(); subs.Next()) {
1492 aSDR = Handle(StepShape_ShapeDefinitionRepresentation)::DownCast(subs.Value());
1493 if ( aSDR.IsNull() )
1494 continue;
1495 StepRepr_RepresentedDefinition aPDSselect = aSDR->Definition();
1496 Handle(StepRepr_ProductDefinitionShape) PDS =
1497 Handle(StepRepr_ProductDefinitionShape)::DownCast(aPDSselect.PropertyDefinition());
1498 if ( PDS.IsNull() )
1499 continue;
1500 StepRepr_CharacterizedDefinition aCharDef = PDS->Definition();
1501 Handle(StepRepr_SpecifiedHigherUsageOccurrence) SHUO =
1502 Handle(StepRepr_SpecifiedHigherUsageOccurrence)::DownCast(aCharDef.ProductDefinitionRelationship());
1503 if ( SHUO.IsNull() )
1504 continue;
1505
1506 // set the SHUO structure to the document
1507 TDF_Label aLabelForStyle = setSHUOintoDoc( WS, SHUO, STool, PDFileMap, ShapeLabelMap );
1508 if ( aLabelForStyle.IsNull() ) {
1509#ifdef DEB
1510 cout << "Warning: " << __FILE__ <<": coudnot create SHUO structure in the document" << endl;
1511#endif
1512 continue;
1513 }
1514 // now set the style to the SHUO main label.
1515 if ( ! SurfCol.IsNull() ) {
1516 Quantity_Color col;
1517 Styles.DecodeColor ( SurfCol, col );
1518 CTool->SetColor ( aLabelForStyle, col, XCAFDoc_ColorSurf );
1519 }
1520 if ( ! BoundCol.IsNull() ) {
1521 Quantity_Color col;
1522 Styles.DecodeColor ( BoundCol, col );
1523 CTool->SetColor ( aLabelForStyle, col, XCAFDoc_ColorCurv );
1524 }
1525 if ( ! CurveCol.IsNull() ) {
1526 Quantity_Color col;
1527 Styles.DecodeColor ( CurveCol, col );
1528 CTool->SetColor ( aLabelForStyle, col, XCAFDoc_ColorCurv );
1529 }
1530 if ( !IsVisible )
1531 // sets the invisibility for shape.
1532 CTool->SetVisibility( aLabelForStyle, Standard_False );
1533
1534 } // end search SHUO by SDR
1535 } // end iterates on styles
1536
1537 return Standard_True;
1538}
1539
1540
1541//=======================================================================
1542//function : GetLengthConversionFactor
1543//purpose :
1544//=======================================================================
1545static Standard_Boolean GetLengthConversionFactor(Handle(StepBasic_NamedUnit)& NU,
1546 Standard_Real& afact)
1547{
1548 afact=1.;
1549 if( !NU->IsKind(STANDARD_TYPE(StepBasic_ConversionBasedUnitAndLengthUnit)) ) return Standard_False;
1550 Handle(StepBasic_ConversionBasedUnitAndLengthUnit) CBULU =
1551 Handle(StepBasic_ConversionBasedUnitAndLengthUnit)::DownCast(NU);
1552 Handle(StepBasic_MeasureWithUnit) MWUCBU = CBULU->ConversionFactor();
1553 afact = MWUCBU->ValueComponent();
1554 StepBasic_Unit anUnit2 = MWUCBU->UnitComponent();
1555 if(anUnit2.CaseNum(anUnit2.Value())==1) {
1556 Handle(StepBasic_NamedUnit) NU2 = anUnit2.NamedUnit();
1557 if(NU2->IsKind(STANDARD_TYPE(StepBasic_SiUnit))) {
1558 Handle(StepBasic_SiUnit) SU = Handle(StepBasic_SiUnit)::DownCast(NU2);
1559 if(SU->Name()==StepBasic_sunMetre) {
1560 if(SU->HasPrefix())
1561 afact *= STEPConstruct_UnitContext::ConvertSiPrefix (SU->Prefix());
1562 // convert m to mm
1563 afact *= 1000.;
1564 }
1565 }
1566 }
1567 return Standard_True;
1568}
1569
1570
1571//=======================================================================
1572//function : GetMassConversionFactor
1573//purpose :
1574//=======================================================================
1575static Standard_Boolean GetMassConversionFactor(Handle(StepBasic_NamedUnit)& NU,
1576 Standard_Real& afact)
1577{
1578 afact=1.;
1579 if( !NU->IsKind(STANDARD_TYPE(StepBasic_ConversionBasedUnitAndMassUnit)) ) return Standard_False;
1580 Handle(StepBasic_ConversionBasedUnitAndMassUnit) CBUMU =
1581 Handle(StepBasic_ConversionBasedUnitAndMassUnit)::DownCast(NU);
1582 Handle(StepBasic_MeasureWithUnit) MWUCBU = CBUMU->ConversionFactor();
1583 afact = MWUCBU->ValueComponent();
1584 StepBasic_Unit anUnit2 = MWUCBU->UnitComponent();
1585 if(anUnit2.CaseNum(anUnit2.Value())==1) {
1586 Handle(StepBasic_NamedUnit) NU2 = anUnit2.NamedUnit();
1587 if(NU2->IsKind(STANDARD_TYPE(StepBasic_SiUnit))) {
1588 Handle(StepBasic_SiUnit) SU = Handle(StepBasic_SiUnit)::DownCast(NU2);
1589 if(SU->Name()==StepBasic_sunGram) {
1590 if(SU->HasPrefix())
1591 afact *= STEPConstruct_UnitContext::ConvertSiPrefix (SU->Prefix());
1592 }
1593 }
1594 }
1595 return Standard_True;
1596}
1597
1598
1599//=======================================================================
1600//function : ReadDatums
1601//purpose : auxilary
1602//=======================================================================
1603static Standard_Boolean ReadDatums(const Handle(XCAFDoc_ShapeTool) &STool,
1604 const Handle(XCAFDoc_DimTolTool) &DGTTool,
1605 const Interface_Graph &graph,
1606 Handle(Transfer_TransientProcess) &TP,
1607 const TDF_Label TolerL,
1608 const Handle(StepDimTol_GeometricToleranceWithDatumReference) GTWDR)
1609{
1610 if(GTWDR.IsNull()) return Standard_False;
1611 Handle(StepDimTol_HArray1OfDatumReference) HADR = GTWDR->DatumSystem();
1612 if(HADR.IsNull()) return Standard_False;
1613 for(Standard_Integer idr=1; idr<=HADR->Length(); idr++) {
1614 Handle(StepDimTol_DatumReference) DR = HADR->Value(idr);
1615 Handle(StepDimTol_Datum) aDatum = DR->ReferencedDatum();
1616 if(aDatum.IsNull()) continue;
1617 Interface_EntityIterator subs4 = graph.Sharings(aDatum);
1618 for(subs4.Start(); subs4.More(); subs4.Next()) {
1619 Handle(StepRepr_ShapeAspectRelationship) SAR =
1620 Handle(StepRepr_ShapeAspectRelationship)::DownCast(subs4.Value());
1621 if(SAR.IsNull()) continue;
1622 Handle(StepDimTol_DatumFeature) DF =
1623 Handle(StepDimTol_DatumFeature)::DownCast(SAR->RelatingShapeAspect());
1624 if(DF.IsNull()) continue;
1625 Interface_EntityIterator subs5 = graph.Sharings(DF);
1626 Handle(StepRepr_PropertyDefinition) PropDef;
1627 for(subs5.Start(); subs5.More() && PropDef.IsNull(); subs5.Next()) {
1628 PropDef = Handle(StepRepr_PropertyDefinition)::DownCast(subs5.Value());
1629 }
1630 if(PropDef.IsNull()) continue;
1631 Handle(StepShape_AdvancedFace) AF;
1632 subs5 = graph.Sharings(PropDef);
1633 for(subs5.Start(); subs5.More(); subs5.Next()) {
1634 Handle(StepShape_ShapeDefinitionRepresentation) SDR =
1635 Handle(StepShape_ShapeDefinitionRepresentation)::DownCast(subs5.Value());
1636 if(!SDR.IsNull()) {
1637 Handle(StepRepr_Representation) Repr = SDR->UsedRepresentation();
1638 if( !Repr.IsNull() && Repr->NbItems()>0 ) {
1639 Handle(StepRepr_RepresentationItem) RI = Repr->ItemsValue(1);
1640 AF = Handle(StepShape_AdvancedFace)::DownCast(RI);
1641 }
1642 }
1643 }
1644 if(AF.IsNull()) return Standard_False;
1645 Standard_Integer index = TP->MapIndex(AF);
1646 TopoDS_Shape aSh;
1647 if(index >0) {
1648 Handle(Transfer_Binder) binder = TP->MapItem(index);
1649 aSh = TransferBRep::ShapeResult(binder);
1650 }
1651 if(aSh.IsNull()) continue;
1652 TDF_Label shL;
1653 if( !STool->Search(aSh, shL, Standard_True, Standard_True, Standard_True) ) continue;
1654 DGTTool->SetDatum(shL,TolerL,PropDef->Name(),PropDef->Description(),aDatum->Identification());
1655 }
1656 }
1657 return Standard_True;
1658}
1659
1660
1661//=======================================================================
1662//function : ReadGDTs
1663//purpose :
1664//=======================================================================
1665
1666Standard_Boolean STEPCAFControl_Reader::ReadGDTs(const Handle(XSControl_WorkSession) &WS,
1667 Handle(TDocStd_Document)& Doc) const
1668{
1669 Handle(Interface_InterfaceModel) Model = WS->Model();
1670 Handle(XCAFDoc_ShapeTool) STool = XCAFDoc_DocumentTool::ShapeTool( Doc->Main() );
1671 Handle(XSControl_TransferReader) TR = WS->TransferReader();
1672 Handle(Transfer_TransientProcess) TP = TR->TransientProcess();
1673 Handle(XCAFDoc_DimTolTool) DGTTool = XCAFDoc_DocumentTool::DimTolTool( Doc->Main() );
1674 if ( DGTTool.IsNull() ) return Standard_False;
1675
1676 Standard_Integer nb = Model->NbEntities();
1677 const Interface_Graph& graph = TP->Graph();
1678 for(Standard_Integer i=1; i<=nb; i++) {
1679 Handle(Standard_Transient) ent = Model->Value(i);
1680 if(ent->IsKind(STANDARD_TYPE(StepRepr_ShapeAspect))) {
1681 Handle(StepRepr_ShapeAspect) SA = Handle(StepRepr_ShapeAspect)::DownCast(ent);
1682 // find RepresentationItem for current ShapeAspect
1683 Handle(StepRepr_RepresentationItem) RI;
1684 Handle(StepRepr_PropertyDefinition) PropD;
1685 Interface_EntityIterator subs3 = graph.Sharings(SA);
1686 for(subs3.Start(); subs3.More() && PropD.IsNull(); subs3.Next()) {
1687 PropD = Handle(StepRepr_PropertyDefinition)::DownCast(subs3.Value());
1688 }
1689 if(PropD.IsNull()) continue;
1690 Interface_EntityIterator subs4 = graph.Sharings(PropD);
1691 for(subs4.Start(); subs4.More(); subs4.Next()) {
1692 Handle(StepShape_ShapeDefinitionRepresentation) SDR =
1693 Handle(StepShape_ShapeDefinitionRepresentation)::DownCast(subs4.Value());
1694 if(!SDR.IsNull()) {
1695 Handle(StepRepr_Representation) Repr = SDR->UsedRepresentation();
1696 if( !Repr.IsNull() && Repr->NbItems()>0 ) {
1697 RI = Repr->ItemsValue(1);
1698 }
1699 }
1700 }
1701 if(RI.IsNull()) continue;
1702 // read DGT entities:
1703 subs3 = graph.Sharings(SA);
1704 for(subs3.Start(); subs3.More(); subs3.Next()) {
1705 if(subs3.Value()->IsKind(STANDARD_TYPE(StepShape_DimensionalSize))) {
1706 // read dimensions
1707 Handle(StepShape_EdgeCurve) EC = Handle(StepShape_EdgeCurve)::DownCast(RI);
1708 if(EC.IsNull()) continue;
1709 Handle(TCollection_HAsciiString) aName;
1710 Handle(StepShape_DimensionalSize) DimSize =
1711 Handle(StepShape_DimensionalSize)::DownCast(subs3.Value());
1712 Standard_Real dim1=-1.,dim2=-1.;
1713 subs4 = graph.Sharings(DimSize);
1714 for(subs4.Start(); subs4.More(); subs4.Next()) {
1715 Handle(StepShape_DimensionalCharacteristicRepresentation) DimCharR =
1716 Handle(StepShape_DimensionalCharacteristicRepresentation)::DownCast(subs4.Value());
1717 if(!DimCharR.IsNull()) {
1718 Handle(StepShape_ShapeDimensionRepresentation) SDimR = DimCharR->Representation();
1719 if(!SDimR.IsNull() && SDimR->NbItems()>0) {
1720 Handle(StepRepr_RepresentationItem) RI = SDimR->ItemsValue(1);
1721 Handle(StepRepr_ValueRange) VR = Handle(StepRepr_ValueRange)::DownCast(RI);
1722 if(!VR.IsNull()) {
1723 aName = VR->Name();
1724 //StepRepr_CompoundItemDefinition CID = VR->ItemElement();
1725 //if(CID.IsNull()) continue;
1726 //Handle(StepRepr_CompoundItemDefinitionMember) CIDM =
1727 // Handle(StepRepr_CompoundItemDefinitionMember)::DownCast(CID.Value());
1728 //if(CIDM.IsNull()) continue;
1729 //if(CIDM->ArrTransient().IsNull()) continue;
1730 //Handle(StepRepr_HArray1OfRepresentationItem) HARI;
1731 //if(CID.CaseMem(CIDM)==1)
1732 // HARI = CID.ListRepresentationItem();
1733 //if(CID.CaseMem(CIDM)==2)
1734 // HARI = CID.SetRepresentationItem();
1735 Handle(StepRepr_HArray1OfRepresentationItem) HARI = VR->ItemElement();
1736 if(HARI.IsNull()) continue;
1737 if(HARI->Length()>0) {
1738 Handle(StepRepr_RepresentationItem) RI1 =
1739 Handle(StepRepr_RepresentationItem)::DownCast(HARI->Value(1));
1740 if(RI1.IsNull()) continue;
1741 if(RI1->IsKind(STANDARD_TYPE(StepRepr_ReprItemAndLengthMeasureWithUnit))) {
1742 Handle(StepRepr_ReprItemAndLengthMeasureWithUnit) RILMWU =
1743 Handle(StepRepr_ReprItemAndLengthMeasureWithUnit)::DownCast(RI1);
1744 dim1 = RILMWU->GetMeasureWithUnit()->ValueComponent();
1745 StepBasic_Unit anUnit = RILMWU->GetMeasureWithUnit()->UnitComponent();
1746 Standard_Real afact=1.;
1747 if(anUnit.IsNull()) continue;
1748 if( !(anUnit.CaseNum(anUnit.Value())==1) ) continue;
1749 Handle(StepBasic_NamedUnit) NU = anUnit.NamedUnit();
1750 if(GetLengthConversionFactor(NU,afact)) dim1=dim1*afact;
1751 }
1752 }
1753 if(HARI->Length()>1) {
1754 Handle(StepRepr_RepresentationItem) RI2 =
1755 Handle(StepRepr_RepresentationItem)::DownCast(HARI->Value(2));
1756 if(RI2.IsNull()) continue;
1757 if(RI2->IsKind(STANDARD_TYPE(StepRepr_ReprItemAndLengthMeasureWithUnit))) {
1758 Handle(StepRepr_ReprItemAndLengthMeasureWithUnit) RILMWU =
1759 Handle(StepRepr_ReprItemAndLengthMeasureWithUnit)::DownCast(RI2);
1760 dim2 = RILMWU->GetMeasureWithUnit()->ValueComponent();
1761 StepBasic_Unit anUnit = RILMWU->GetMeasureWithUnit()->UnitComponent();
1762 Standard_Real afact=1.;
1763 if(anUnit.IsNull()) continue;
1764 if( !(anUnit.CaseNum(anUnit.Value())==1) ) continue;
1765 Handle(StepBasic_NamedUnit) NU = anUnit.NamedUnit();
1766 if(GetLengthConversionFactor(NU,afact)) dim2 = dim2*afact;
1767 }
1768 }
1769 }
1770 }
1771 }
1772 }
1773 if(dim1<0) continue;
1774 if(dim2<0) dim2=dim1;
1775 //cout<<"DimensionalSize: dim1="<<dim1<<" dim2="<<dim2<<endl;
1776 // now we know edge_curve and value range therefore
1777 // we can create corresponding D&GT labels
1778 Standard_Integer index = TP->MapIndex(EC);
1779 TopoDS_Shape aSh;
1780 if(index >0) {
1781 Handle(Transfer_Binder) binder = TP->MapItem(index);
1782 aSh = TransferBRep::ShapeResult(binder);
1783 }
1784 if(aSh.IsNull()) continue;
1785 TDF_Label shL;
1786 if( !STool->Search(aSh, shL, Standard_True, Standard_True, Standard_True) ) continue;
1787 Handle(TColStd_HArray1OfReal) arr = new TColStd_HArray1OfReal(1,2);
1788 arr->SetValue(1,dim1);
1789 arr->SetValue(2,dim2);
1790 DGTTool->SetDimTol(shL,1,arr,aName,DimSize->Name());
1791 }
1792 // read tolerances and datums
1793 else if(subs3.Value()->IsKind(STANDARD_TYPE(StepDimTol_GeometricTolerance))) {
1794 Handle(StepDimTol_GeometricTolerance) GT =
1795 Handle(StepDimTol_GeometricTolerance)::DownCast(subs3.Value());
1796 // read common data for tolerance
1797 //Standard_Real dim = GT->Magnitude()->ValueComponent();
1798 Handle (StepBasic_MeasureWithUnit) dim3 = GT->Magnitude();
1799 if(dim3.IsNull()) continue;
1800 Standard_Real dim = dim3->ValueComponent();
1801 StepBasic_Unit anUnit = GT->Magnitude()->UnitComponent();
1802 Standard_Real afact=1.;
1803 if(anUnit.IsNull()) continue;
1804 if( !(anUnit.CaseNum(anUnit.Value())==1) ) continue;
1805 Handle(StepBasic_NamedUnit) NU = anUnit.NamedUnit();
1806 if(GetLengthConversionFactor(NU,afact)) dim = dim*afact;
1807 //cout<<"GeometricTolerance: Magnitude = "<<dim<<endl;
1808 Handle(TColStd_HArray1OfReal) arr = new TColStd_HArray1OfReal(1,1);
1809 arr->SetValue(1,dim);
1810 Handle(TCollection_HAsciiString) aName = GT->Name();
1811 Handle(TCollection_HAsciiString) aDescription = GT->Description();
1812 Handle(StepShape_AdvancedFace) AF = Handle(StepShape_AdvancedFace)::DownCast(RI);
1813 if(AF.IsNull()) continue;
1814 Standard_Integer index = TP->MapIndex(AF);
1815 TopoDS_Shape aSh;
1816 if(index >0) {
1817 Handle(Transfer_Binder) binder = TP->MapItem(index);
1818 aSh = TransferBRep::ShapeResult(binder);
1819 }
1820 if(aSh.IsNull()) continue;
1821 TDF_Label shL;
1822 if( !STool->Search(aSh, shL, Standard_True, Standard_True, Standard_True) ) continue;
1823 // read specific data for tolerance
1824 if(GT->IsKind(STANDARD_TYPE(StepDimTol_GeoTolAndGeoTolWthDatRefAndModGeoTolAndPosTol))) {
1825 Handle(StepDimTol_GeoTolAndGeoTolWthDatRefAndModGeoTolAndPosTol) GTComplex =
1826 Handle(StepDimTol_GeoTolAndGeoTolWthDatRefAndModGeoTolAndPosTol)::DownCast(subs3.Value());
1827 Standard_Integer kind=20;
1828 Handle(StepDimTol_ModifiedGeometricTolerance) MGT =
1829 GTComplex->GetModifiedGeometricTolerance();
1830 if(!MGT.IsNull()) {
1831 kind = kind + MGT->Modifier()+1;
1832 }
1833 TDF_Label TolerL = DGTTool->SetDimTol(shL,kind,arr,aName,aDescription);
1834 // translate datums connected with this tolerance
1835 Handle(StepDimTol_GeometricToleranceWithDatumReference) GTWDR =
1836 GTComplex->GetGeometricToleranceWithDatumReference();
1837 if(!GTWDR.IsNull()) {
1838 ReadDatums(STool,DGTTool,graph,TP,TolerL,GTWDR);
1839 }
1840 }
1841 else if(GT->IsKind(STANDARD_TYPE(StepDimTol_GeometricToleranceWithDatumReference))) {
1842 Handle(StepDimTol_GeometricToleranceWithDatumReference) GTWDR =
1843 Handle(StepDimTol_GeometricToleranceWithDatumReference)::DownCast(subs3.Value());
1844 if(GTWDR.IsNull()) continue;
1845 Standard_Integer kind = 0;
1846 if (GTWDR->IsKind(STANDARD_TYPE(StepDimTol_AngularityTolerance))) kind = 24;
1847 else if(GTWDR->IsKind(STANDARD_TYPE(StepDimTol_CircularRunoutTolerance))) kind = 25;
1848 else if(GTWDR->IsKind(STANDARD_TYPE(StepDimTol_CoaxialityTolerance))) kind = 26;
1849 else if(GTWDR->IsKind(STANDARD_TYPE(StepDimTol_ConcentricityTolerance))) kind = 27;
1850 else if(GTWDR->IsKind(STANDARD_TYPE(StepDimTol_ParallelismTolerance))) kind = 28;
1851 else if(GTWDR->IsKind(STANDARD_TYPE(StepDimTol_PerpendicularityTolerance))) kind = 29;
1852 else if(GTWDR->IsKind(STANDARD_TYPE(StepDimTol_SymmetryTolerance))) kind = 30;
1853 else if(GTWDR->IsKind(STANDARD_TYPE(StepDimTol_TotalRunoutTolerance))) kind = 31;
1854 //cout<<"GTWDR: kind="<<kind<<endl;
1855 TDF_Label TolerL = DGTTool->SetDimTol(shL,kind,arr,aName,aDescription);
1856 ReadDatums(STool,DGTTool,graph,TP,TolerL,GTWDR);
1857 }
1858 else if(GT->IsKind(STANDARD_TYPE(StepDimTol_ModifiedGeometricTolerance))) {
1859 Handle(StepDimTol_ModifiedGeometricTolerance) MGT =
1860 Handle(StepDimTol_ModifiedGeometricTolerance)::DownCast(subs3.Value());
1861 Standard_Integer kind = 35 + MGT->Modifier();
1862 DGTTool->SetDimTol(shL,kind,arr,aName,aDescription);
1863 }
1864 else if(GT->IsKind(STANDARD_TYPE(StepDimTol_CylindricityTolerance))) {
1865 DGTTool->SetDimTol(shL,38,arr,aName,aDescription);
1866 }
1867 else if(GT->IsKind(STANDARD_TYPE(StepDimTol_FlatnessTolerance))) {
1868 DGTTool->SetDimTol(shL,39,arr,aName,aDescription);
1869 }
1870 else if(GT->IsKind(STANDARD_TYPE(StepDimTol_LineProfileTolerance))) {
1871 DGTTool->SetDimTol(shL,40,arr,aName,aDescription);
1872 }
1873 else if(GT->IsKind(STANDARD_TYPE(StepDimTol_PositionTolerance))) {
1874 DGTTool->SetDimTol(shL,41,arr,aName,aDescription);
1875 }
1876 else if(GT->IsKind(STANDARD_TYPE(StepDimTol_RoundnessTolerance))) {
1877 DGTTool->SetDimTol(shL,42,arr,aName,aDescription);
1878 }
1879 else if(GT->IsKind(STANDARD_TYPE(StepDimTol_StraightnessTolerance))) {
1880 DGTTool->SetDimTol(shL,43,arr,aName,aDescription);
1881 }
1882 else if(GT->IsKind(STANDARD_TYPE(StepDimTol_SurfaceProfileTolerance))) {
1883 DGTTool->SetDimTol(shL,44,arr,aName,aDescription);
1884 }
1885 }
1886 }
1887 }
1888 }
1889
1890 return Standard_True;
1891}
1892
1893
1894//=======================================================================
1895//function : FindSolidForPDS
1896//purpose : auxilary
1897//=======================================================================
1898
1899static Handle(StepShape_SolidModel) FindSolidForPDS(const Handle(StepRepr_ProductDefinitionShape) &PDS,
1900 const Interface_Graph &graph)
1901{
1902 Handle(StepShape_SolidModel) SM;
1903 Interface_EntityIterator subs = graph.Sharings(PDS);
1904 Handle(StepShape_ShapeRepresentation) SR;
1905 for(subs.Start(); subs.More() && SM.IsNull(); subs.Next()) {
1906 Handle(StepShape_ShapeDefinitionRepresentation) SDR =
1907 Handle(StepShape_ShapeDefinitionRepresentation)::DownCast(subs.Value());
1908 if(SDR.IsNull()) continue;
1909 SR = Handle(StepShape_ShapeRepresentation)::DownCast(SDR->UsedRepresentation());
1910 if(SR.IsNull()) continue;
1911 for(Standard_Integer i=1; i<=SR->NbItems() && SM.IsNull(); i++) {
1912 SM = Handle(StepShape_SolidModel)::DownCast(SR->ItemsValue(i));
1913 }
1914 if(SM.IsNull()) {
1915 Interface_EntityIterator subs1 = graph.Sharings(SR);
1916 for(subs1.Start(); subs1.More() && SM.IsNull(); subs1.Next()) {
1917 Handle(StepRepr_RepresentationRelationship) RR =
1918 Handle(StepRepr_RepresentationRelationship)::DownCast(subs1.Value());
1919 if(RR.IsNull()) continue;
1920 Handle(StepShape_ShapeRepresentation) SR2;
1921 if(RR->Rep1()==SR) SR2 = Handle(StepShape_ShapeRepresentation)::DownCast(RR->Rep2());
1922 else SR2 = Handle(StepShape_ShapeRepresentation)::DownCast(RR->Rep1());
1923 if(SR2.IsNull()) continue;
1924 for(Standard_Integer i2=1; i2<=SR2->NbItems() && SM.IsNull(); i2++) {
1925 SM = Handle(StepShape_SolidModel)::DownCast(SR2->ItemsValue(i2));
1926 }
1927 }
1928 }
1929 }
1930 return SM;
1931}
1932
1933
1934//=======================================================================
1935//function : ReadMaterials
1936//purpose :
1937//=======================================================================
1938
1939Standard_Boolean STEPCAFControl_Reader::ReadMaterials(const Handle(XSControl_WorkSession) &WS,
1940 Handle(TDocStd_Document)& Doc,
1941 const Handle(TColStd_HSequenceOfTransient) &SeqPDS) const
1942{
1943 Handle(Interface_InterfaceModel) Model = WS->Model();
1944 Handle(XCAFDoc_ShapeTool) STool = XCAFDoc_DocumentTool::ShapeTool( Doc->Main() );
1945 Handle(XSControl_TransferReader) TR = WS->TransferReader();
1946 Handle(Transfer_TransientProcess) TP = TR->TransientProcess();
1947 Handle(XCAFDoc_MaterialTool) MatTool = XCAFDoc_DocumentTool::MaterialTool( Doc->Main() );
1948 if(MatTool.IsNull()) return Standard_False;
1949
1950 const Interface_Graph& graph = TP->Graph();
1951 for(Standard_Integer i=1; i<=SeqPDS->Length(); i++) {
1952 Handle(StepRepr_ProductDefinitionShape) PDS =
1953 Handle(StepRepr_ProductDefinitionShape)::DownCast(SeqPDS->Value(i));
1d2b1ccb
G
1954 if(PDS.IsNull())
1955 continue;
1956 Handle(StepBasic_ProductDefinition) aProdDef = PDS->Definition().ProductDefinition();
1957 if(aProdDef.IsNull())
1958 continue;
7fd59977 1959 Handle(TCollection_HAsciiString) aName = new TCollection_HAsciiString("");
1960 Handle(TCollection_HAsciiString) aDescription = new TCollection_HAsciiString("");
1961 Handle(TCollection_HAsciiString) aDensName = new TCollection_HAsciiString("");
1962 Handle(TCollection_HAsciiString) aDensValType = new TCollection_HAsciiString("");
1963 Standard_Real aDensity=0;
1d2b1ccb 1964 Interface_EntityIterator subs = graph.Sharings( aProdDef);
7fd59977 1965 for(subs.Start(); subs.More(); subs.Next()) {
1966 Handle(StepRepr_PropertyDefinition) PropD =
1967 Handle(StepRepr_PropertyDefinition)::DownCast(subs.Value());
1968 if(PropD.IsNull()) continue;
1969 Interface_EntityIterator subs1 = graph.Sharings(PropD);
1970 for(subs1.Start(); subs1.More(); subs1.Next()) {
1971 Handle(StepRepr_PropertyDefinitionRepresentation) PDR =
1972 Handle(StepRepr_PropertyDefinitionRepresentation)::DownCast(subs1.Value());
1973 if(PDR.IsNull()) continue;
1974 Handle(StepRepr_Representation) Repr = PDR->UsedRepresentation();
1975 if(Repr.IsNull()) continue;
1976 Standard_Integer ir;
1977 for(ir=1; ir<=Repr->NbItems(); ir++) {
1978 Handle(StepRepr_RepresentationItem) RI = Repr->ItemsValue(ir);
1979 if(RI.IsNull()) continue;
1980 if(RI->IsKind(STANDARD_TYPE(StepRepr_DescriptiveRepresentationItem))) {
1981 // find name and description for material
1982 Handle(StepRepr_DescriptiveRepresentationItem) DRI =
1983 Handle(StepRepr_DescriptiveRepresentationItem)::DownCast(RI);
1984 aName = DRI->Name();
87073a10 1985
7fd59977 1986 aDescription = DRI->Description();
87073a10 1987 if(aName.IsNull())
1988 aName = aDescription;
7fd59977 1989 }
1990 if(RI->IsKind(STANDARD_TYPE(StepRepr_MeasureRepresentationItem))) {
1991 // try to find density for material
1992 Handle(StepRepr_MeasureRepresentationItem) MRI =
1993 Handle(StepRepr_MeasureRepresentationItem)::DownCast(RI);
1994 aDensity = MRI->Measure()->ValueComponent();
1995 aDensName = MRI->Name();
1996 aDensValType = new TCollection_HAsciiString(MRI->Measure()->ValueComponentMember()->Name());
1997 StepBasic_Unit aUnit = MRI->Measure()->UnitComponent();
1998 if(!aUnit.IsNull()) {
1999 Handle(StepBasic_DerivedUnit) DU = aUnit.DerivedUnit();
2000 if(DU.IsNull()) continue;
2001 for(Standard_Integer idu=1; idu<=DU->NbElements(); idu++) {
2002 Handle(StepBasic_DerivedUnitElement) DUE = DU->ElementsValue(idu);
2003 Handle(StepBasic_NamedUnit) NU = DUE->Unit();
2004 Standard_Real afact=1.;
2005 if(NU->IsKind(STANDARD_TYPE(StepBasic_ConversionBasedUnitAndLengthUnit))) {
2006 if(GetLengthConversionFactor(NU,afact)) aDensity = aDensity/(afact*afact*afact);
2007 // transfer length value for Density from millimeter to santimeter
2008 // in order to result density has dimension gram/(sm*sm*sm)
2009 aDensity = aDensity*1000.;
2010 }
2011 if(NU->IsKind(STANDARD_TYPE(StepBasic_ConversionBasedUnitAndMassUnit))) {
2012 if(GetMassConversionFactor(NU,afact)) aDensity=aDensity*afact;
2013 }
2014 }
2015 }
2016 }
2017 }
2018 }
2019 }
87073a10 2020
2021 if( aName.IsNull() || aName->Length()==0 )
2022 continue;
7fd59977 2023 // find shape label amd create Material link
2024 TopoDS_Shape aSh;
2025 Handle(StepShape_SolidModel) SM = FindSolidForPDS(PDS,graph);
2026 if(!SM.IsNull()) {
2027 Standard_Integer index = TP->MapIndex(SM);
2028 if(index >0) {
2029 Handle(Transfer_Binder) binder = TP->MapItem(index);
2030 if(!binder.IsNull())
2031 aSh = TransferBRep::ShapeResult(binder);
2032 }
2033 }
2034 if(aSh.IsNull()) continue;
2035 TDF_Label shL;
2036 if( !STool->Search(aSh, shL, Standard_True, Standard_True, Standard_True) ) continue;
2037 MatTool->SetMaterial(shL,aName,aDescription,aDensity,aDensName,aDensValType);
2038 }
2039
2040 return Standard_True;
2041}
2042
02a0b964 2043//=======================================================================
2044//function : SettleShapeData
2045//purpose :
2046//=======================================================================
2047
2048TDF_Label STEPCAFControl_Reader::SettleShapeData(const Handle(StepRepr_RepresentationItem)& theItem,
2049 TDF_Label& theLab,
2050 const Handle(XCAFDoc_ShapeTool)& theShapeTool,
2051 const Handle(Transfer_TransientProcess)& TP) const
2052{
2053 TDF_Label aResult = theLab;
2054
2055 Handle(TCollection_HAsciiString) hName = theItem->Name();
2056 if ( hName.IsNull() || hName->IsEmpty() )
2057 return aResult;
2058
2059 Handle(Transfer_Binder) aBinder = TP->Find(theItem);
2060 if ( aBinder.IsNull() )
2061 return aResult;
2062
2063 TopoDS_Shape aShape = TransferBRep::ShapeResult(aBinder);
2064 if ( aShape.IsNull() )
2065 return aResult;
2066
2067 // Allocate sub-Label
2068 aResult = AllocateSubLabel(theLab);
2069
2070 TCollection_AsciiString aName = hName->String();
2071 TDataStd_Name::Set(aResult, aName);
2072 theShapeTool->SetShape(aResult, aShape);
2073
2074 return aResult;
2075}
2076
2077//=======================================================================
2078//function : ExpandSubShapes
2079//purpose :
2080//=======================================================================
2081
2082void STEPCAFControl_Reader::ExpandSubShapes(const Handle(XCAFDoc_ShapeTool)& ShapeTool,
2083 const XCAFDoc_DataMapOfShapeLabel& ShapeLabelMap,
2084 const STEPCAFControl_DataMapOfShapePD& ShapePDMap) const
2085{
2086 const Handle(Interface_InterfaceModel)& Model = Reader().WS()->Model();
2087 const Handle(Transfer_TransientProcess)& TP = Reader().WS()->TransferReader()->TransientProcess();
2088 NCollection_DataMap<TopoDS_Shape, Handle(TCollection_HAsciiString)> ShapeNameMap;
2089 TColStd_MapOfTransient aRepItems;
2090
2091 // Read translation control variables
2092 Standard_Boolean doReadSNames = (Interface_Static::IVal("read.stepcaf.subshapes.name") > 0);
2093
2094 if ( !doReadSNames )
2095 return;
2096
2097 const Interface_Graph& Graph = Reader().WS()->Graph();
2098
2099 for ( STEPCAFControl_DataMapIteratorOfDataMapOfShapePD it(ShapePDMap); it.More(); it.Next() )
2100 {
2101 const TopoDS_Shape& aRootShape = it.Key();
2102 const Handle(StepBasic_ProductDefinition)& aPDef = it.Value();
2103 if ( aPDef.IsNull() )
2104 continue;
2105
2106 // Find SDR by Product
2107 Handle(StepShape_ShapeDefinitionRepresentation) aSDR;
2108 Interface_EntityIterator entIt = Graph.TypedSharings( aPDef, STANDARD_TYPE(StepShape_ShapeDefinitionRepresentation) );
2109 for ( entIt.Start(); entIt.More(); entIt.Next() )
2110 {
2111 const Handle(Standard_Transient)& aReferer = entIt.Value();
2112 aSDR = Handle(StepShape_ShapeDefinitionRepresentation)::DownCast(aReferer);
2113 if ( !aSDR.IsNull() )
2114 break;
2115 }
2116
2117 if ( aSDR.IsNull() )
2118 continue;
2119
2120 // Access shape representation
2121 Handle(StepShape_ShapeRepresentation)
2122 aShapeRepr = Handle(StepShape_ShapeRepresentation)::DownCast( aSDR->UsedRepresentation() );
2123
2124 if ( aShapeRepr.IsNull() )
2125 continue;
2126
2127 // Access representation items
2128 Handle(StepRepr_HArray1OfRepresentationItem) aReprItems = aShapeRepr->Items();
2129
2130 if ( aReprItems.IsNull() )
2131 continue;
2132
2133 if ( !ShapeLabelMap.IsBound(aRootShape) )
2134 continue;
2135
2136 TDF_Label aRootLab = ShapeLabelMap.Find(aRootShape);
2137
2138 StepRepr_SequenceOfRepresentationItem aMSBSeq;
2139 StepRepr_SequenceOfRepresentationItem aSBSMSeq;
2140
2141 // Iterate over the top level representation items collecting the
2142 // topological containers to expand
2143 for ( Standard_Integer i = aReprItems->Lower(); i <= aReprItems->Upper(); ++i )
2144 {
2145 Handle(StepRepr_RepresentationItem) aTRepr = aReprItems->Value(i);
2146 if ( aTRepr->IsKind( STANDARD_TYPE(StepShape_ManifoldSolidBrep) ) )
2147 aMSBSeq.Append(aTRepr);
2148 else if ( aTRepr->IsKind( STANDARD_TYPE(StepShape_ShellBasedSurfaceModel) ) )
2149 aSBSMSeq.Append(aTRepr);
2150 }
2151
2152 // Insert intermediate OCAF Labels for SOLIDs in case there are more
2153 // than one Manifold Solid BRep in the Shape Representation
2154 Standard_Boolean doInsertSolidLab = (aMSBSeq.Length() > 1);
2155
2156 // Expand Manifold Solid BReps
2157 for ( Standard_Integer i = 1; i <= aMSBSeq.Length(); ++i )
2158 {
2159 const Handle(StepRepr_RepresentationItem)& aManiRepr = aMSBSeq.Value(i);
2160
2161 // Put additional Label for SOLID
2162 TDF_Label aManiLab;
2163 if ( doInsertSolidLab )
2164 aManiLab = SettleShapeData(aManiRepr, aRootLab, ShapeTool, TP);
2165 else
2166 aManiLab = aRootLab;
2167
2168 ExpandManifoldSolidBrep(aManiLab, aMSBSeq.Value(i), TP, ShapeTool);
2169 }
2170
2171 // Expand Shell-Based Surface Models
2172 for ( Standard_Integer i = 1; i <= aSBSMSeq.Length(); ++i )
2173 ExpandSBSM(aRootLab, aSBSMSeq.Value(i), TP, ShapeTool);
2174 }
2175}
2176
2177//=======================================================================
2178//function : ExpandManifoldSolidBrep
2179//purpose :
2180//=======================================================================
2181
2182void STEPCAFControl_Reader::ExpandManifoldSolidBrep(TDF_Label& ShapeLab,
2183 const Handle(StepRepr_RepresentationItem)& Repr,
2184 const Handle(Transfer_TransientProcess)& TP,
2185 const Handle(XCAFDoc_ShapeTool)& ShapeTool) const
2186{
2187 // Access outer shell
2188 Handle(StepShape_ManifoldSolidBrep) aMSB = Handle(StepShape_ManifoldSolidBrep)::DownCast(Repr);
2189 Handle(StepShape_ClosedShell) aShell = aMSB->Outer();
2190
2191 // Expand shell contents to CAF tree
2192 ExpandShell(aShell, ShapeLab, TP, ShapeTool);
2193}
2194
2195//=======================================================================
2196//function : ExpandSBSM
2197//purpose :
2198//=======================================================================
2199
2200void STEPCAFControl_Reader::ExpandSBSM(TDF_Label& ShapeLab,
2201 const Handle(StepRepr_RepresentationItem)& Repr,
2202 const Handle(Transfer_TransientProcess)& TP,
2203 const Handle(XCAFDoc_ShapeTool)& ShapeTool) const
2204{
2205 Handle(StepShape_ShellBasedSurfaceModel) aSBSM = Handle(StepShape_ShellBasedSurfaceModel)::DownCast(Repr);
2206
2207 // Access boundary shells
2208 Handle(StepShape_HArray1OfShell) aShells = aSBSM->SbsmBoundary();
2209 for ( Standard_Integer s = aShells->Lower(); s <= aShells->Upper(); ++s )
2210 {
2211 const StepShape_Shell& aShell = aShells->Value(s);
2212 Handle(StepShape_ConnectedFaceSet) aCFS;
2213 Handle(StepShape_OpenShell) anOpenShell = aShell.OpenShell();
2214 Handle(StepShape_ClosedShell) aClosedShell = aShell.ClosedShell();
2215
2216 if ( !anOpenShell.IsNull() )
2217 aCFS = anOpenShell;
2218 else
2219 aCFS = aClosedShell;
2220
2221 ExpandShell(aCFS, ShapeLab, TP, ShapeTool);
2222 }
2223}
2224
2225//=======================================================================
2226//function : ExpandShell
2227//purpose :
2228//=======================================================================
2229
2230void STEPCAFControl_Reader::ExpandShell(const Handle(StepShape_ConnectedFaceSet)& Shell,
2231 TDF_Label& RootLab,
2232 const Handle(Transfer_TransientProcess)& TP,
2233 const Handle(XCAFDoc_ShapeTool)& ShapeTool) const
2234{
2235 // Record CAF data
2236 TDF_Label aShellLab = SettleShapeData(Shell, RootLab, ShapeTool, TP);
2237
2238 // Access faces
2239 Handle(StepShape_HArray1OfFace) aFaces = Shell->CfsFaces();
2240 for ( Standard_Integer f = aFaces->Lower(); f <= aFaces->Upper(); ++f )
2241 {
2242 const Handle(StepShape_Face)& aFace = aFaces->Value(f);
2243
2244 // Record CAF data
2245 TDF_Label aFaceLab = SettleShapeData(aFace, aShellLab, ShapeTool, TP);
2246
2247 // Access face bounds
2248 Handle(StepShape_HArray1OfFaceBound) aWires = aFace->Bounds();
2249 for ( Standard_Integer w = aWires->Lower(); w <= aWires->Upper(); ++w )
2250 {
2251 const Handle(StepShape_Loop)& aWire = aWires->Value(w)->Bound();
2252
2253 // Record CAF data
2254 TDF_Label aWireLab = SettleShapeData(aWire, aFaceLab, ShapeTool, TP);
2255
2256 // Access wire edges
2257 // Currently only EDGE LOOPs are considered (!)
2258 if ( !aWire->IsInstance( STANDARD_TYPE(StepShape_EdgeLoop) ) )
2259 continue;
2260
2261 // Access edges
2262 Handle(StepShape_EdgeLoop) anEdgeLoop = Handle(StepShape_EdgeLoop)::DownCast(aWire);
2263 Handle(StepShape_HArray1OfOrientedEdge) anEdges = anEdgeLoop->EdgeList();
2264 for ( Standard_Integer e = anEdges->Lower(); e <= anEdges->Upper(); ++e )
2265 {
2266 Handle(StepShape_Edge) anEdge = anEdges->Value(e)->EdgeElement();
2267
2268 // Record CAF data
2269 TDF_Label anEdgeLab = SettleShapeData(anEdge, aWireLab, ShapeTool, TP);
2270
2271 // Access vertices
2272 Handle(StepShape_Vertex) aV1 = anEdge->EdgeStart();
2273 Handle(StepShape_Vertex) aV2 = anEdge->EdgeEnd();
2274
2275 // Record CAF data
2276 SettleShapeData(aV1, anEdgeLab, ShapeTool, TP);
2277 SettleShapeData(aV2, anEdgeLab, ShapeTool, TP);
2278 }
2279 }
2280 }
2281}
7fd59977 2282
2283//=======================================================================
2284//function : SetColorMode
2285//purpose :
2286//=======================================================================
2287
2288void STEPCAFControl_Reader::SetColorMode (const Standard_Boolean colormode)
2289{
2290 myColorMode = colormode;
2291}
2292
2293//=======================================================================
2294//function : GetColorMode
2295//purpose :
2296//=======================================================================
2297
2298Standard_Boolean STEPCAFControl_Reader::GetColorMode () const
2299{
2300 return myColorMode;
2301}
2302
2303//=======================================================================
2304//function : SetNameMode
2305//purpose :
2306//=======================================================================
2307
2308void STEPCAFControl_Reader::SetNameMode (const Standard_Boolean namemode)
2309{
2310 myNameMode = namemode;
2311}
2312
2313//=======================================================================
2314//function : GetNameMode
2315//purpose :
2316//=======================================================================
2317
2318Standard_Boolean STEPCAFControl_Reader::GetNameMode () const
2319{
2320 return myNameMode;
2321}
2322
2323//=======================================================================
2324//function : SetLayerMode
2325//purpose :
2326//=======================================================================
2327
2328void STEPCAFControl_Reader::SetLayerMode (const Standard_Boolean layermode)
2329{
2330 myLayerMode = layermode;
2331}
2332
2333//=======================================================================
2334//function : GetLayerMode
2335//purpose :
2336//=======================================================================
2337
2338Standard_Boolean STEPCAFControl_Reader::GetLayerMode () const
2339{
2340 return myLayerMode;
2341}
2342
2343//=======================================================================
2344//function : SetPropsMode
2345//purpose :
2346//=======================================================================
2347
2348void STEPCAFControl_Reader::SetPropsMode (const Standard_Boolean propsmode)
2349{
2350 myPropsMode = propsmode;
2351}
2352
2353//=======================================================================
2354//function : GetPropsMode
2355//purpose :
2356//=======================================================================
2357
2358Standard_Boolean STEPCAFControl_Reader::GetPropsMode () const
2359{
2360 return myPropsMode;
2361}
2362
2363//=======================================================================
2364//function : SetSHUOMode
2365//purpose :
2366//=======================================================================
2367
2368void STEPCAFControl_Reader::SetSHUOMode (const Standard_Boolean mode)
2369{
2370 mySHUOMode = mode;
2371}
2372
2373//=======================================================================
2374//function : GetSHUOMode
2375//purpose :
2376//=======================================================================
2377
2378Standard_Boolean STEPCAFControl_Reader::GetSHUOMode () const
2379{
2380 return mySHUOMode;
2381}
2382
2383//=======================================================================
2384//function : SetGDTMode
2385//purpose :
2386//=======================================================================
2387
2388void STEPCAFControl_Reader::SetGDTMode (const Standard_Boolean gdtmode)
2389{
2390 myGDTMode = gdtmode;
2391}
2392
2393//=======================================================================
2394//function : GetGDTMode
2395//purpose :
2396//=======================================================================
2397
2398Standard_Boolean STEPCAFControl_Reader::GetGDTMode () const
2399{
2400 return myGDTMode;
2401}
2402
2403
2404//=======================================================================
2405//function : SetMatMode
2406//purpose :
2407//=======================================================================
2408
2409void STEPCAFControl_Reader::SetMatMode (const Standard_Boolean matmode)
2410{
2411 myMatMode = matmode;
2412}
2413
2414//=======================================================================
2415//function : GetMatMode
2416//purpose :
2417//=======================================================================
2418
2419Standard_Boolean STEPCAFControl_Reader::GetMatMode () const
2420{
2421 return myMatMode;
2422}