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