0026859: Export of GDT from XCAF to STEP
[occt.git] / src / STEPCAFControl / STEPCAFControl_Reader.cxx
1 // Created on: 2000-08-15
2 // Created by: Andrey BETENEV
3 // Copyright (c) 2000-2014 OPEN CASCADE SAS
4 //
5 // This file is part of Open CASCADE Technology software library.
6 //
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
12 //
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
15
16
17 #include <BRep_Builder.hxx>
18 #include <Interface_EntityIterator.hxx>
19 #include <Interface_InterfaceModel.hxx>
20 #include <StepData_StepModel.hxx>
21 #include <HeaderSection_FileSchema.hxx>
22 #include <Interface_Static.hxx>
23 #include <NCollection_DataMap.hxx>
24 #include <OSD_Path.hxx>
25 #include <Quantity_Color.hxx>
26 #include <StepAP214_AppliedExternalIdentificationAssignment.hxx>
27 #include <StepBasic_ConversionBasedUnitAndLengthUnit.hxx>
28 #include <StepBasic_ConversionBasedUnitAndPlaneAngleUnit.hxx>
29 #include <StepBasic_ConversionBasedUnitAndMassUnit.hxx>
30 #include <StepBasic_DerivedUnit.hxx>
31 #include <StepBasic_DerivedUnitElement.hxx>
32 #include <StepBasic_MeasureValueMember.hxx>
33 #include <StepBasic_MeasureWithUnit.hxx>
34 #include <StepBasic_NamedUnit.hxx>
35 #include <StepBasic_Product.hxx>
36 #include <StepBasic_ProductDefinition.hxx>
37 #include <StepBasic_ProductDefinitionFormation.hxx>
38 #include <StepBasic_ProductDefinitionRelationship.hxx>
39 #include <StepBasic_SiUnit.hxx>
40 #include <StepBasic_Unit.hxx>
41 #include <STEPCAFControl_Controller.hxx>
42 #include <STEPCAFControl_DataMapIteratorOfDataMapOfShapePD.hxx>
43 #include <STEPCAFControl_DataMapOfPDExternFile.hxx>
44 #include <STEPCAFControl_DataMapOfSDRExternFile.hxx>
45 #include <STEPCAFControl_DataMapOfShapePD.hxx>
46 #include <STEPCAFControl_DictionaryOfExternFile.hxx>
47 #include <STEPCAFControl_ExternFile.hxx>
48 #include <STEPCAFControl_Reader.hxx>
49 #include <STEPConstruct.hxx>
50 #include <STEPConstruct_Assembly.hxx>
51 #include <STEPConstruct_ExternRefs.hxx>
52 #include <STEPConstruct_Styles.hxx>
53 #include <STEPConstruct_Tool.hxx>
54 #include <STEPConstruct_UnitContext.hxx>
55 #include <STEPConstruct_ValidationProps.hxx>
56 #include <STEPControl_Reader.hxx>
57 #include <StepGeom_GeometricRepresentationItem.hxx>
58 #include <StepGeom_Axis2Placement3d.hxx>
59 #include <StepGeom_Direction.hxx>
60 #include <StepDimTol_AngularityTolerance.hxx>
61 #include <StepDimTol_CircularRunoutTolerance.hxx>
62 #include <StepDimTol_CoaxialityTolerance.hxx>
63 #include <StepDimTol_ConcentricityTolerance.hxx>
64 #include <StepDimTol_CylindricityTolerance.hxx>
65 #include <StepDimTol_Datum.hxx>
66 #include <StepDimTol_DatumFeature.hxx>
67 #include <StepDimTol_DatumReference.hxx>
68 #include <StepDimTol_DatumReferenceElement.hxx>
69 #include <StepDimTol_DatumReferenceCompartment.hxx>
70 #include <StepDimTol_DatumSystem.hxx>
71 #include <StepDimTol_FlatnessTolerance.hxx>
72 #include <StepDimTol_GeometricTolerance.hxx>
73 #include <StepDimTol_GeometricToleranceWithDatumReference.hxx>
74 #include <StepDimTol_GeoTolAndGeoTolWthDatRefAndModGeoTolAndPosTol.hxx>
75 #include <StepDimTol_GeoTolAndGeoTolWthDatRef.hxx>
76 #include <StepDimTol_GeoTolAndGeoTolWthDatRefAndGeoTolWthMaxTol.hxx>
77 #include <StepDimTol_GeoTolAndGeoTolWthDatRefAndUneqDisGeoTol.hxx>
78 #include <StepDimTol_GeoTolAndGeoTolWthMaxTol.hxx>
79 #include <StepDimTol_HArray1OfDatumReference.hxx>
80 #include <StepDimTol_LineProfileTolerance.hxx>
81 #include <StepDimTol_ModifiedGeometricTolerance.hxx>
82 #include <StepDimTol_ParallelismTolerance.hxx>
83 #include <StepDimTol_PerpendicularityTolerance.hxx>
84 #include <StepDimTol_PositionTolerance.hxx>
85 #include <StepDimTol_ProjectedZoneDefinition.hxx>
86 #include <StepDimTol_RoundnessTolerance.hxx>
87 #include <StepDimTol_RunoutZoneDefinition.hxx>
88 #include <StepDimTol_StraightnessTolerance.hxx>
89 #include <StepDimTol_SurfaceProfileTolerance.hxx>
90 #include <StepDimTol_SymmetryTolerance.hxx>
91 #include <StepDimTol_ToleranceZone.hxx>
92 #include <StepDimTol_ToleranceZoneForm.hxx>
93 #include <StepDimTol_TotalRunoutTolerance.hxx>
94 #include <StepDimTol_GeometricToleranceWithModifiers.hxx>
95 #include <StepDimTol_HArray1OfGeometricToleranceModifier.hxx>
96 #include <StepDimTol_GeoTolAndGeoTolWthDatRefAndGeoTolWthMod.hxx>
97 #include <StepDimTol_GeoTolAndGeoTolWthMod.hxx>
98 #include <StepDimTol_GeometricToleranceWithMaximumTolerance.hxx>
99 #include <StepDimTol_PlacedDatumTargetFeature.hxx>
100 #include <StepRepr_AssemblyComponentUsage.hxx>
101 #include <StepRepr_CharacterizedDefinition.hxx>
102 #include <StepRepr_CompoundRepresentationItem.hxx>
103 #include <StepRepr_DescriptiveRepresentationItem.hxx>
104 #include <StepRepr_HArray1OfRepresentationItem.hxx>
105 #include <StepRepr_MeasureRepresentationItem.hxx>
106 #include <StepRepr_NextAssemblyUsageOccurrence.hxx>
107 #include <StepRepr_ProductDefinitionShape.hxx>
108 #include <StepRepr_PropertyDefinition.hxx>
109 #include <StepRepr_PropertyDefinitionRepresentation.hxx>
110 #include <StepRepr_Representation.hxx>
111 #include <StepRepr_RepresentationItem.hxx>
112 #include <StepRepr_HArray1OfRepresentationItem.hxx>
113 #include <StepRepr_RepresentationRelationship.hxx>
114 #include <StepRepr_RepresentedDefinition.hxx>
115 #include <StepRepr_ReprItemAndLengthMeasureWithUnit.hxx>
116 #include <StepRepr_ReprItemAndPlaneAngleMeasureWithUnit.hxx>
117 #include <StepRepr_SequenceOfRepresentationItem.hxx>
118 #include <StepRepr_ShapeAspect.hxx>
119 #include <StepRepr_CompositeShapeAspect.hxx>
120 #include <StepRepr_AllAroundShapeAspect.hxx>
121 #include <StepRepr_CompShAspAndDatumFeatAndShAsp.hxx>
122 #include <StepRepr_ShapeAspectRelationship.hxx>
123 #include <StepRepr_ShapeRepresentationRelationship.hxx>
124 #include <StepRepr_SpecifiedHigherUsageOccurrence.hxx>
125 #include <StepRepr_ValueRange.hxx>
126 #include <StepRepr_FeatureForDatumTargetRelationship.hxx>
127 #include <StepShape_AdvancedFace.hxx>
128 #include <StepShape_AdvancedBrepShapeRepresentation.hxx>
129 #include <StepShape_AngleRelator.hxx>
130 #include <StepShape_AngularSize.hxx>
131 #include <StepShape_AngularLocation.hxx>
132 #include <StepShape_ClosedShell.hxx>
133 #include <StepShape_ConnectedFaceSet.hxx>
134 #include <StepShape_ContextDependentShapeRepresentation.hxx>
135 #include <StepRepr_CompGroupShAspAndCompShAspAndDatumFeatAndShAsp.hxx>
136 #include <StepRepr_CompShAspAndDatumFeatAndShAsp.hxx>
137 #include <StepShape_DimensionalCharacteristicRepresentation.hxx>
138 #include <StepShape_DimensionalSizeWithPath.hxx>
139 #include <StepShape_DimensionalLocationWithPath.hxx>
140 #include <StepShape_ShapeDimensionRepresentationItem.hxx>
141 #include <StepShape_ShapeRepresentationWithParameters.hxx>
142 #include <StepShape_HArray1OfShapeDimensionRepresentationItem.hxx>
143 #include <StepShape_DimensionalSize.hxx>
144 #include <StepShape_DimensionalLocation.hxx>
145 #include <StepShape_EdgeCurve.hxx>
146 #include <StepShape_EdgeLoop.hxx>
147 #include <StepShape_GeometricSet.hxx>
148 #include <StepShape_HArray1OfFace.hxx>
149 #include <StepShape_HArray1OfFaceBound.hxx>
150 #include <StepShape_HArray1OfOrientedEdge.hxx>
151 #include <StepShape_HArray1OfShell.hxx>
152 #include <StepShape_LimitsAndFits.hxx>
153 #include <StepShape_Loop.hxx>
154 #include <StepShape_ManifoldSolidBrep.hxx>
155 #include <StepShape_PlusMinusTolerance.hxx>
156 #include <StepShape_QualifiedRepresentationItem.hxx>
157 #include <StepShape_OpenShell.hxx>
158 #include <StepShape_ShapeDefinitionRepresentation.hxx>
159 #include <StepShape_ShapeDimensionRepresentation.hxx>
160 #include <StepShape_ShapeRepresentation.hxx>
161 #include <StepShape_ShellBasedSurfaceModel.hxx>
162 #include <StepShape_SolidModel.hxx>
163 #include <StepShape_ToleranceMethodDefinition.hxx>
164 #include <StepShape_ToleranceValue.hxx>
165 #include <StepShape_ValueFormatTypeQualifier.hxx>
166 #include <StepShape_Vertex.hxx>
167 #include <StepVisual_Invisibility.hxx>
168 #include <StepVisual_LayeredItem.hxx>
169 #include <StepVisual_PresentationLayerAssignment.hxx>
170 #include <StepVisual_PresentationStyleByContext.hxx>
171 #include <StepVisual_StyleContextSelect.hxx>
172 #include <StepVisual_StyledItem.hxx>
173 #include <StepShape_TypeQualifier.hxx>
174 #include <TCollection_AsciiString.hxx>
175 #include <TCollection_HAsciiString.hxx>
176 #include <TColStd_HArray1OfReal.hxx>
177 #include <TColStd_HArray1OfTransient.hxx>
178 #include <TColStd_HSequenceOfTransient.hxx>
179 #include <TColStd_IndexedDataMapOfTransientTransient.hxx>
180 #include <TColStd_MapIteratorOfMapOfTransient.hxx>
181 #include <TColStd_MapOfTransient.hxx>
182 #include <TColStd_SequenceOfHAsciiString.hxx>
183 #include <TDataStd_Name.hxx>
184 #include <TDataStd_TreeNode.hxx>
185 #include <TDataStd_UAttribute.hxx>
186 #include <TDF_Label.hxx>
187 #include <TDF_Tool.hxx>
188 #include <TDocStd_Document.hxx>
189 #include <TNaming_NamedShape.hxx>
190 #include <TopoDS.hxx>
191 #include <TopoDS_Compound.hxx>
192 #include <TopoDS_Iterator.hxx>
193 #include <TopoDS_Shape.hxx>
194 #include <TopTools_ListIteratorOfListOfShape.hxx>
195 #include <TopTools_ListOfShape.hxx>
196 #include <TopTools_MapOfShape.hxx>
197 #include <Transfer_Binder.hxx>
198 #include <Transfer_TransientProcess.hxx>
199 #include <TransferBRep.hxx>
200 #include <XCAFDoc.hxx>
201 #include <XCAFDoc_Area.hxx>
202 #include <XCAFDoc_Centroid.hxx>
203 #include <XCAFDoc_ColorTool.hxx>
204 #include <XCAFDoc_DataMapOfShapeLabel.hxx>
205 #include <XCAFDoc_DimTolTool.hxx>
206 #include <XCAFDoc_Dimension.hxx>
207 #include <XCAFDoc_Datum.hxx>
208 #include <XCAFDoc_GeomTolerance.hxx>
209 #include <XCAFDoc_DocumentTool.hxx>
210 #include <XCAFDoc_GraphNode.hxx>
211 #include <XCAFDoc_LayerTool.hxx>
212 #include <XCAFDoc_MaterialTool.hxx>
213 #include <XCAFDoc_ShapeTool.hxx>
214 #include <XCAFDoc_Volume.hxx>
215 #include <XCAFDimTolObjects_DimensionModifiersSequence.hxx>
216 #include <XCAFDimTolObjects_GeomToleranceType.hxx>
217 #include <XCAFDimTolObjects_DimensionObject.hxx>
218 #include <XCAFDimTolObjects_GeomToleranceObject.hxx>
219 #include <XCAFDimTolObjects_DatumObject.hxx>
220 #include <XSControl_TransferReader.hxx>
221 #include <XSControl_WorkSession.hxx>
222 #include <StepAP242_GeometricItemSpecificUsage.hxx>
223 #include <StepGeom_CartesianPoint.hxx>
224 #include <STEPConstruct_GDTProperty.hxx>
225 // skl 21.08.2003 for reading G&DT
226 //#include <StepRepr_CompoundItemDefinition.hxx>
227 //#include <StepRepr_CompoundItemDefinitionMember.hxx>
228 //#include <StepBasic_ConversionBasedUnit.hxx>
229 //#include <TDataStd_Real.hxx>
230 //#include <TDataStd_Constraint.hxx>
231 //#include <TDataStd_ConstraintEnum.hxx>
232 //#include <TNaming_Tool.hxx>
233 //#include <AIS_InteractiveObject.hxx>
234 //#include <TPrsStd_ConstraintTools.hxx>
235 //#include <AIS_DiameterDimension.hxx>
236 //#include <TPrsStd_Position.hxx>
237 //#include <TPrsStd_AISPresentation.hxx>
238 //#include <TNaming_Builder.hxx>
239 #ifdef OCCT_DEBUG
240 //! Converts address of the passed shape (TShape) to string.
241 //! \param theShape [in] Shape to dump.
242 //! \return corresponding string.
243 TCollection_AsciiString AddrToString(const TopoDS_Shape& theShape)
244 {
245   std::string anAddrStr;
246   std::ostringstream ost;
247   ost << theShape.TShape().get();
248   anAddrStr = ost.str();
249
250   TCollection_AsciiString aStr =
251     TCollection_AsciiString("[").Cat( anAddrStr.c_str() ).Cat("]");
252
253   return aStr;
254 }
255 #endif
256
257 //=======================================================================
258 //function : IsEqual
259 //purpose  : global function to check equality of topological shapes
260 //=======================================================================
261
262 inline Standard_Boolean IsEqual(const TopoDS_Shape& theShape1,
263                                 const TopoDS_Shape& theShape2) 
264 {
265   return theShape1.IsEqual(theShape2);
266 }
267
268 //=======================================================================
269 //function : AllocateSubLabel
270 //purpose  :
271 //=======================================================================
272
273 static TDF_Label AllocateSubLabel(TDF_Label& theRoot)
274 {
275   return TDF_TagSource::NewChild(theRoot);
276 }
277
278 //=======================================================================
279 //function : STEPCAFControl_Reader
280 //purpose  : 
281 //=======================================================================
282
283 STEPCAFControl_Reader::STEPCAFControl_Reader ():
284        myColorMode( Standard_True ),
285        myNameMode ( Standard_True ),
286        myLayerMode( Standard_True ),
287        myPropsMode( Standard_True ),
288            mySHUOMode ( Standard_False ),
289        myGDTMode  ( Standard_True ),
290        myMatMode  ( Standard_True )
291 {
292   STEPCAFControl_Controller::Init();
293   myFiles = new STEPCAFControl_DictionaryOfExternFile;
294 }
295
296
297 //=======================================================================
298 //function : STEPCAFControl_Reader
299 //purpose  : 
300 //=======================================================================
301
302 STEPCAFControl_Reader::STEPCAFControl_Reader (const Handle(XSControl_WorkSession)& WS,
303                                               const Standard_Boolean scratch) :
304        myColorMode( Standard_True ),
305        myNameMode ( Standard_True ),
306        myLayerMode( Standard_True ),
307        myPropsMode( Standard_True ),
308            mySHUOMode ( Standard_False ),
309        myGDTMode  ( Standard_True ),
310        myMatMode  ( Standard_True )
311 {
312   STEPCAFControl_Controller::Init();
313   Init ( WS, scratch );
314 }
315
316
317 //=======================================================================
318 //function : Init
319 //purpose  : 
320 //=======================================================================
321
322 void STEPCAFControl_Reader::Init (const Handle(XSControl_WorkSession)& WS,
323                                   const Standard_Boolean scratch)
324 {
325 // necessary only in Writer, to set good actor:  WS->SelectNorm ( "STEP" );
326   myReader.SetWS (WS,scratch);
327   myFiles = new STEPCAFControl_DictionaryOfExternFile;
328 }
329
330
331 //=======================================================================
332 //function : ReadFile
333 //purpose  : 
334 //=======================================================================
335
336 IFSelect_ReturnStatus STEPCAFControl_Reader::ReadFile (const Standard_CString filename)
337 {
338   return myReader.ReadFile ( filename );
339 }
340
341
342 //=======================================================================
343 //function : NbRootsForTransfer
344 //purpose  : 
345 //=======================================================================
346
347 Standard_Integer STEPCAFControl_Reader::NbRootsForTransfer () 
348 {
349   return myReader.NbRootsForTransfer();
350 }
351
352
353 //=======================================================================
354 //function : TransferOneRoot
355 //purpose  : 
356 //=======================================================================
357
358 Standard_Boolean STEPCAFControl_Reader::TransferOneRoot (const Standard_Integer num,
359                                                          Handle(TDocStd_Document) &doc) 
360 {
361   TDF_LabelSequence Lseq;
362   return Transfer ( myReader, num, doc, Lseq );
363 }
364
365
366 //=======================================================================
367 //function : Transfer
368 //purpose  : 
369 //=======================================================================
370
371 Standard_Boolean STEPCAFControl_Reader::Transfer (Handle(TDocStd_Document) &doc)
372 {
373   TDF_LabelSequence Lseq;
374   return Transfer ( myReader, 0, doc, Lseq );
375 }
376
377
378 //=======================================================================
379 //function : Perform
380 //purpose  : 
381 //=======================================================================
382
383 Standard_Boolean STEPCAFControl_Reader::Perform (const Standard_CString filename,
384                                                  Handle(TDocStd_Document) &doc)
385 {
386   if ( ReadFile ( filename ) != IFSelect_RetDone ) return Standard_False;
387   return Transfer ( doc );
388 }
389   
390
391 //=======================================================================
392 //function : Perform
393 //purpose  : 
394 //=======================================================================
395
396 Standard_Boolean STEPCAFControl_Reader::Perform (const TCollection_AsciiString &filename,
397                                                  Handle(TDocStd_Document) &doc)
398 {
399   if ( ReadFile ( filename.ToCString() ) != IFSelect_RetDone ) return Standard_False;
400   return Transfer ( doc );
401 }
402   
403
404 //=======================================================================
405 //function : ExternFiles
406 //purpose  : 
407 //=======================================================================
408
409 const Handle(STEPCAFControl_DictionaryOfExternFile) &STEPCAFControl_Reader::ExternFiles () const
410 {
411   return myFiles;
412 }
413         
414
415 //=======================================================================
416 //function : ExternFile
417 //purpose  : 
418 //=======================================================================
419
420 Standard_Boolean STEPCAFControl_Reader::ExternFile (const Standard_CString name,
421                                                     Handle(STEPCAFControl_ExternFile) &ef) const
422 {
423   ef.Nullify();
424   if ( myFiles.IsNull() || ! myFiles->HasItem ( name ) ) 
425     return Standard_False;
426   ef = myFiles->Item ( name );
427   return Standard_True;
428 }
429
430
431 //=======================================================================
432 //function : Reader
433 //purpose  : 
434 //=======================================================================
435
436 STEPControl_Reader &STEPCAFControl_Reader::ChangeReader () 
437 {
438   return myReader;
439 }
440   
441
442 //=======================================================================
443 //function : Reader
444 //purpose  : 
445 //=======================================================================
446
447 const STEPControl_Reader &STEPCAFControl_Reader::Reader () const
448 {
449   return myReader;
450 }
451   
452
453 //=======================================================================
454 //function : FillShapesMap
455 //purpose  : auxiliary: fill a map by all compounds and their components
456 //=======================================================================
457
458 static void FillShapesMap (const TopoDS_Shape &S, TopTools_MapOfShape &map)
459 {
460   TopoDS_Shape S0 = S;
461   TopLoc_Location loc;
462   S0.Location ( loc );
463   map.Add ( S0 );
464   if ( S.ShapeType() != TopAbs_COMPOUND ) return;
465   for ( TopoDS_Iterator it(S); it.More(); it.Next() ) 
466     FillShapesMap ( it.Value(), map );
467 }
468
469
470 //=======================================================================
471 //function : Transfer
472 //purpose  : basic working method
473 //=======================================================================
474
475 Standard_Boolean STEPCAFControl_Reader::Transfer (STEPControl_Reader &reader,
476                                                   const Standard_Integer nroot,
477                                                   Handle(TDocStd_Document) &doc,
478                                                   TDF_LabelSequence &Lseq,
479                                                   const Standard_Boolean asOne)
480 {
481   reader.ClearShapes();
482   Standard_Integer i;
483   
484   // Read all shapes
485   Standard_Integer num = reader.NbRootsForTransfer();
486   if ( num <=0 ) return Standard_False;
487   if ( nroot ) {
488     if ( nroot > num ) return Standard_False;
489     reader.TransferOneRoot ( nroot );
490   }
491   else {
492     for ( i=1; i <= num; i++ ) reader.TransferOneRoot ( i );
493   }
494   num = reader.NbShapes();
495   if ( num <=0 ) return Standard_False;
496
497   // Fill a map of (top-level) shapes resulting from that transfer
498   // Only these shapes will be considered further
499   TopTools_MapOfShape ShapesMap, NewShapesMap;
500   for ( i=1; i <= num; i++ ) FillShapesMap ( reader.Shape(i), ShapesMap );
501   
502   // Collect information on shapes originating from SDRs
503   // this will be used to distinguish compounds representing assemblies
504   // from the ones representing hybrid models and shape sets
505   STEPCAFControl_DataMapOfShapePD ShapePDMap;
506   STEPCAFControl_DataMapOfPDExternFile PDFileMap;
507   Handle(Interface_InterfaceModel) Model = reader.Model();
508   Handle(Transfer_TransientProcess) TP = reader.WS()->TransferReader()->TransientProcess();
509   Standard_Integer nb = Model->NbEntities();
510
511   Handle(TColStd_HSequenceOfTransient) SeqPDS = new TColStd_HSequenceOfTransient;
512
513   for (i = 1; i <= nb; i ++) {
514     Handle(Standard_Transient) enti = Model->Value(i);
515     if(enti->IsKind(STANDARD_TYPE(StepRepr_ProductDefinitionShape))) {
516       // sequence for acceleration ReadMaterials
517       SeqPDS->Append(enti);
518     }
519     if ( enti->IsKind ( STANDARD_TYPE(StepBasic_ProductDefinition ) ) ) {
520       Handle(StepBasic_ProductDefinition) PD = 
521         Handle(StepBasic_ProductDefinition)::DownCast(enti);
522       Standard_Integer index = TP->MapIndex(PD);
523       if ( index >0 ) {
524         Handle(Transfer_Binder) binder = TP->MapItem (index);
525         TopoDS_Shape S = TransferBRep::ShapeResult(binder);
526         if ( ! S.IsNull() && ShapesMap.Contains(S) ) {
527           NewShapesMap.Add(S);
528           ShapePDMap.Bind ( S, PD ); 
529           Handle(STEPCAFControl_ExternFile) EF;
530           PDFileMap.Bind ( PD, EF );
531         }
532       }
533     }
534     if ( enti->IsKind ( STANDARD_TYPE(StepShape_ShapeRepresentation) ) ) {
535       Standard_Integer index = TP->MapIndex(enti);
536       if ( index >0 ) {
537         Handle(Transfer_Binder) binder = TP->MapItem (index);
538         TopoDS_Shape S = TransferBRep::ShapeResult(binder);
539         if ( ! S.IsNull() && ShapesMap.Contains(S) )
540           NewShapesMap.Add(S);
541       }
542     }
543   }
544
545   // get directory name of the main file
546   OSD_Path mainfile ( reader.WS()->LoadedFile() );
547   mainfile.SetName ( "" );
548   mainfile.SetExtension ( "" );
549   TCollection_AsciiString dpath;
550   mainfile.SystemName ( dpath );
551
552   // Load external references (only for relevant SDRs)
553   // and fill map SDR -> extern file
554   STEPConstruct_ExternRefs ExtRefs ( reader.WS() );
555   ExtRefs.LoadExternRefs();
556   for ( i=1; i <= ExtRefs.NbExternRefs(); i++ ) {
557     // check extern ref format
558     Handle(TCollection_HAsciiString) format = ExtRefs.Format(i);
559     if ( ! format.IsNull() ) {
560       static Handle(TCollection_HAsciiString) ap203 = new TCollection_HAsciiString ( "STEP AP203" );
561       static Handle(TCollection_HAsciiString) ap214 = new TCollection_HAsciiString ( "STEP AP214" );
562       if ( ! format->IsSameString ( ap203, Standard_False ) && 
563            ! format->IsSameString ( ap214, Standard_False ) ) {
564 #ifdef OCCT_DEBUG
565         cout << "Warning: STEPCAFControl_Reader::Transfer: Extern document is neither STEP AP203 nor AP214" << endl;
566 #else
567         continue;
568 #endif
569       }
570     }
571 #ifdef OCCT_DEBUG
572     else cout << "Warning: STEPCAFControl_Reader::Transfer: Extern document format not defined" << endl;
573 #endif
574     
575     // get and check filename of the current extern ref
576     const Standard_CString filename = ExtRefs.FileName(i);
577
578 #ifdef OCCT_DEBUG
579     cout<<"filename="<<filename<<endl;
580 #endif
581
582     if ( ! filename || ! filename[0] ) {
583 #ifdef OCCT_DEBUG
584       cout << "Warning: STEPCAFControl_Reader::Transfer: Extern reference file name is empty" << endl;
585 #endif
586       continue; // not a valid extern ref
587     }
588
589     // compute true path to the extern file
590     TCollection_AsciiString fullname = OSD_Path::AbsolutePath ( dpath, filename );
591     if ( fullname.Length() <= 0 ) fullname = filename;
592
593 /*    
594     char fullname[1024];
595     char *mainfile = reader.WS()->LoadedFile();
596     if ( ! mainfile ) mainfile = "";
597     Standard_Integer slash = 0;
598     for ( Standard_Integer k=0; mainfile[k]; k++ )
599       if ( mainfile[k] == '/' ) slash = k;
600     strncpy ( fullname, mainfile, slash );
601     sprintf ( &fullname[slash], "%s%s", ( mainfile[0] ? "/" : "" ), filename );
602 */
603     
604     // get and check PD associated with the current extern ref
605     Handle(StepBasic_ProductDefinition) PD = ExtRefs.ProdDef(i);
606     if ( PD.IsNull() ) continue; // not a valid extern ref
607     if ( ! PDFileMap.IsBound ( PD ) ) continue; // this PD is not concerned by current transfer
608     
609     // read extern file (or use existing data) and record its data
610     Handle(STEPCAFControl_ExternFile) EF = 
611       ReadExternFile ( filename, fullname.ToCString(), doc );
612     PDFileMap.Bind ( PD, EF );
613   }
614   
615   // and insert them to the document
616   Handle(XCAFDoc_ShapeTool) STool = XCAFDoc_DocumentTool::ShapeTool( doc->Main() );
617   if ( STool.IsNull() ) return Standard_False;
618   XCAFDoc_DataMapOfShapeLabel map;
619   if ( asOne )
620     Lseq.Append ( AddShape ( reader.OneShape(), STool, NewShapesMap, ShapePDMap, PDFileMap, map ) );
621   else {
622     for ( i=1; i <= num; i++ ) {
623       Lseq.Append ( AddShape ( reader.Shape(i), STool, NewShapesMap, ShapePDMap, PDFileMap, map ) );
624     }
625   }
626   
627   // read colors
628   if ( GetColorMode() )
629     ReadColors ( reader.WS(), doc, PDFileMap, map );
630   
631   // read names
632   if ( GetNameMode() )
633     ReadNames ( reader.WS(), doc, PDFileMap, map );
634
635   // read validation props
636   if ( GetPropsMode() )
637     ReadValProps ( reader.WS(), doc, PDFileMap, map );
638
639   // read layers
640   if ( GetLayerMode() )
641     ReadLayers ( reader.WS(), doc );
642   
643   // read SHUO entities from STEP model
644   if ( GetSHUOMode() )
645     ReadSHUOs ( reader.WS(), doc, PDFileMap, map );
646
647   // read GDT entities from STEP model
648   if(GetGDTMode())
649     ReadGDTs(reader.WS(),doc);
650
651   // read Material entities from STEP model
652   if(GetMatMode())
653     ReadMaterials(reader.WS(),doc,SeqPDS);
654
655   // Expand resulting CAF structure for sub-shapes (optionally with their
656   // names) if requested
657   ExpandSubShapes(STool, map, ShapePDMap);
658
659   return Standard_True;
660 }
661
662 //=======================================================================
663 //function : AddShape
664 //purpose  : 
665 //=======================================================================
666
667 TDF_Label STEPCAFControl_Reader::AddShape (const TopoDS_Shape &S, 
668                                            const Handle(XCAFDoc_ShapeTool) &STool,
669                                            const TopTools_MapOfShape &NewShapesMap,
670                                            const STEPCAFControl_DataMapOfShapePD &ShapePDMap,
671                                            const STEPCAFControl_DataMapOfPDExternFile &PDFileMap,
672                                            XCAFDoc_DataMapOfShapeLabel &ShapeLabelMap) const
673 {
674   // if shape has already been mapped, just return corresponding label
675   if ( ShapeLabelMap.IsBound ( S ) ) {
676     return ShapeLabelMap.Find ( S );
677   }
678   
679   // if shape is located, create instance
680   if ( ! S.Location().IsIdentity() ) {
681     TopoDS_Shape S0 = S;
682     TopLoc_Location loc;
683     S0.Location ( loc );
684     AddShape ( S0, STool, NewShapesMap, ShapePDMap, PDFileMap, ShapeLabelMap );
685     TDF_Label L = STool->AddShape ( S, Standard_False ); // should create reference
686     ShapeLabelMap.Bind ( S, L );
687     return L;
688   }
689   
690   // if shape is not compound, simple add it
691   if ( S.ShapeType() != TopAbs_COMPOUND ) {
692     TDF_Label L = STool->AddShape ( S, Standard_False );
693     ShapeLabelMap.Bind ( S, L );
694     return L;
695   }
696   
697   // for compounds, compute number of subshapes and check whether this is assembly
698   Standard_Boolean isAssembly = Standard_False;
699   Standard_Integer nbComponents = 0;
700   TopoDS_Iterator it;
701   for ( it.Initialize(S); it.More(); it.Next(), nbComponents++ ) {
702     TopoDS_Shape Sub0 = it.Value();
703     TopLoc_Location loc;
704     Sub0.Location ( loc );
705     if ( NewShapesMap.Contains ( Sub0 ) ) isAssembly = Standard_True;
706   }
707
708 //  if(nbComponents>0) isAssembly = Standard_True;
709   
710   // check whether it has associated external ref
711   TColStd_SequenceOfHAsciiString SHAS;
712   if ( ShapePDMap.IsBound ( S ) && PDFileMap.IsBound ( ShapePDMap.Find(S) ) ) {
713     Handle(STEPCAFControl_ExternFile) EF = PDFileMap.Find ( ShapePDMap.Find(S) );
714     if ( ! EF.IsNull() ) {
715       // (store information on extern refs in the document)
716       SHAS.Append(EF->GetName());
717       // if yes, just return corresponding label
718       if ( ! EF->GetLabel().IsNull() ) {
719         // but if components >0, ignore extern ref!
720         if ( nbComponents <=0 ) {
721           ShapeLabelMap.Bind ( S, EF->GetLabel() );
722           STool->SetExternRefs(EF->GetLabel(),SHAS);
723           return EF->GetLabel();
724         }
725       }
726 #ifdef OCCT_DEBUG
727       if ( ! EF->GetLabel().IsNull() )
728         cout << "Warning: STEPCAFControl_Reader::AddShape: Non-empty shape with external ref; ref is ignored" << endl;
729       else if ( nbComponents <=0 ) 
730         cout << "Warning: STEPCAFControl_Reader::AddShape: Result of reading extern ref is Null" << endl;
731 #endif
732     }
733   }
734   
735   // add compound either as a whole,
736   if ( ! isAssembly ) {
737     TDF_Label L = STool->AddShape ( S, Standard_False );
738     if ( SHAS.Length() >0 ) STool->SetExternRefs(L,SHAS);
739     ShapeLabelMap.Bind ( S, L );
740     return L;
741   }
742   
743   // or as assembly, component-by-component
744   TDF_Label L = STool->NewShape();
745   for ( it.Initialize(S); it.More(); it.Next(), nbComponents++ ) {
746     TopoDS_Shape Sub0 = it.Value();
747     TopLoc_Location loc;
748     Sub0.Location ( loc );
749     TDF_Label subL = AddShape ( Sub0, STool, NewShapesMap, ShapePDMap, PDFileMap, ShapeLabelMap );
750     if ( ! subL.IsNull() ) {
751       STool->AddComponent ( L, subL, it.Value().Location() );
752     }
753   }
754   if ( SHAS.Length() >0 ) STool->SetExternRefs(L,SHAS);
755   ShapeLabelMap.Bind ( S, L );
756   //STool->SetShape ( L, S ); // it is necessary for assemblies OCC1747 // commemted by skl for OCC2941
757
758   return L;
759 }
760
761 //=======================================================================
762 //function : ReadExternFile
763 //purpose  : 
764 //=======================================================================
765
766 Handle(STEPCAFControl_ExternFile) STEPCAFControl_Reader::ReadExternFile (const Standard_CString file, 
767                                                                          const Standard_CString fullname, 
768                                                                          Handle(TDocStd_Document)& doc) 
769 {
770   // if the file is already read, associate it with SDR
771   if ( myFiles->HasItem ( file, Standard_True ) ) {
772     return myFiles->Item ( file );
773   }
774
775 #ifdef OCCT_DEBUG
776   cout << "Reading extern file: " << fullname << endl;
777 #endif
778  
779   // create new WorkSession and Reader
780   Handle(XSControl_WorkSession) newWS = new XSControl_WorkSession;
781   newWS->SelectNorm ( "STEP" );
782   STEPControl_Reader sr ( newWS, Standard_False );
783   
784   // start to fill the resulting ExternFile structure
785   Handle(STEPCAFControl_ExternFile) EF = new STEPCAFControl_ExternFile;
786   EF->SetWS ( newWS );
787   EF->SetName ( new TCollection_HAsciiString ( file ) );
788   
789   // read file
790   EF->SetLoadStatus ( sr.ReadFile ( fullname ) );
791   
792   // transfer in single-result mode
793   if ( EF->GetLoadStatus() == IFSelect_RetDone ) {
794     TDF_LabelSequence labels;
795     EF->SetTransferStatus ( Transfer ( sr, 0, doc, labels, Standard_True ) );
796     if ( labels.Length() >0 ) EF->SetLabel ( labels.Value(1) );
797   }
798   
799   // add read file to dictionary
800   myFiles->SetItem ( file, EF );
801   
802   return EF;
803 }
804
805
806 //=======================================================================
807 //function : SetColorToSubshape
808 //purpose  : auxilary
809 //=======================================================================
810 static void SetColorToSubshape(const Handle(XCAFDoc_ColorTool) & CTool,
811                                const TopoDS_Shape & S,
812                                const Quantity_Color& col,
813                                const XCAFDoc_ColorType type)
814 {
815   for (TopoDS_Iterator it(S); it.More(); it.Next())
816     if (! CTool->SetColor( it.Value(), col, type)) break;
817 }
818
819
820 //=======================================================================
821 //function : findStyledSR
822 //purpose  : auxilary
823 //=======================================================================
824 static void findStyledSR (const Handle(StepVisual_StyledItem) &style,
825                           Handle(StepShape_ShapeRepresentation)& aSR)
826 {
827   // search Shape Represenatation for component styled item
828   for ( Standard_Integer j=1; j <= style->NbStyles(); j++ ) {
829     Handle(StepVisual_PresentationStyleByContext) PSA = 
830       Handle(StepVisual_PresentationStyleByContext)::DownCast(style->StylesValue ( j ));
831     if ( PSA.IsNull() )
832       continue;
833     StepVisual_StyleContextSelect aStyleCntxSlct = PSA->StyleContext();
834     Handle(StepShape_ShapeRepresentation) aCurrentSR = 
835       Handle(StepShape_ShapeRepresentation)::DownCast(aStyleCntxSlct.Representation());
836     if ( aCurrentSR.IsNull() )
837       continue;
838     aSR = aCurrentSR;
839       break;
840   }
841 }
842
843
844 //=======================================================================
845 //function : ReadColors
846 //purpose  : 
847 //=======================================================================
848
849 Standard_Boolean STEPCAFControl_Reader::ReadColors (const Handle(XSControl_WorkSession) &WS,
850                                                     Handle(TDocStd_Document)& Doc,
851                                                     const STEPCAFControl_DataMapOfPDExternFile &PDFileMap,
852                                                     const XCAFDoc_DataMapOfShapeLabel &ShapeLabelMap) const
853 {
854   STEPConstruct_Styles Styles ( WS );
855   if ( ! Styles.LoadStyles() ) {
856 #ifdef OCCT_DEBUG
857     cout << "Warning: no styles are found in the model" << endl;
858 #endif
859     return Standard_False;
860   }
861   // searching for invisible items in the model
862   Handle(TColStd_HSequenceOfTransient) aHSeqOfInvisStyle = new TColStd_HSequenceOfTransient;
863   Styles.LoadInvisStyles( aHSeqOfInvisStyle );
864   
865   Handle(XCAFDoc_ColorTool) CTool = XCAFDoc_DocumentTool::ColorTool( Doc->Main() );
866   if ( CTool.IsNull() ) return Standard_False;
867
868   // parse and search for color attributes
869   Standard_Integer nb = Styles.NbStyles();
870   for ( Standard_Integer i=1; i <= nb; i++ ) {
871     Handle(StepVisual_StyledItem) style = Styles.Style ( i );
872     if ( style.IsNull() ) continue;
873     
874     Standard_Boolean IsVisible = Standard_True;
875     // check the visibility of styled item.
876     for (Standard_Integer si = 1; si <= aHSeqOfInvisStyle->Length(); si++ ) {
877       if ( style != aHSeqOfInvisStyle->Value( si ) )
878         continue;
879       // found that current style is invisible.
880       IsVisible = Standard_False;
881       break;
882     }
883
884     Handle(StepVisual_Colour) SurfCol, BoundCol, CurveCol;
885     // check if it is component style
886     Standard_Boolean IsComponent = Standard_False;
887     if ( ! Styles.GetColors ( style, SurfCol, BoundCol, CurveCol, IsComponent ) && IsVisible )
888       continue;
889     
890     // find shape
891     TopoDS_Shape S = STEPConstruct::FindShape ( Styles.TransientProcess(), style->Item() );
892     Standard_Boolean isSkipSHUOstyle = Standard_False;
893     // take shape with real location.
894     while ( IsComponent ) {
895       // take SR of NAUO
896       Handle(StepShape_ShapeRepresentation) aSR;
897       findStyledSR( style, aSR );
898       // search for SR along model
899       if (aSR.IsNull())
900         break;
901 //       Handle(Interface_InterfaceModel) Model = WS->Model();
902       Handle(XSControl_TransferReader) TR = WS->TransferReader();
903       Handle(Transfer_TransientProcess) TP = TR->TransientProcess();
904       Interface_EntityIterator subs = WS->HGraph()->Graph().Sharings( aSR );
905       Handle(StepShape_ShapeDefinitionRepresentation) aSDR;
906       for (subs.Start(); subs.More(); subs.Next()) {
907         aSDR = Handle(StepShape_ShapeDefinitionRepresentation)::DownCast(subs.Value());
908         if ( aSDR.IsNull() )
909           continue;
910         StepRepr_RepresentedDefinition aPDSselect = aSDR->Definition();
911         Handle(StepRepr_ProductDefinitionShape) PDS = 
912           Handle(StepRepr_ProductDefinitionShape)::DownCast(aPDSselect.PropertyDefinition());
913         if ( PDS.IsNull() )
914           continue;
915         StepRepr_CharacterizedDefinition aCharDef = PDS->Definition();
916         
917         Handle(StepRepr_AssemblyComponentUsage) ACU = 
918           Handle(StepRepr_AssemblyComponentUsage)::DownCast(aCharDef.ProductDefinitionRelationship());
919         // PTV 10.02.2003 skip styled item that refer to SHUO
920         if (ACU->IsKind(STANDARD_TYPE(StepRepr_SpecifiedHigherUsageOccurrence))) {
921           isSkipSHUOstyle = Standard_True;
922           break;
923         }
924         Handle(StepRepr_NextAssemblyUsageOccurrence) NAUO =
925           Handle(StepRepr_NextAssemblyUsageOccurrence)::DownCast(ACU);
926         if ( NAUO.IsNull() )
927           continue;
928         
929         TopoDS_Shape aSh;
930         // PTV 10.02.2003 to find component of assembly CORRECTLY
931         STEPConstruct_Tool Tool( WS );
932         TDF_Label aShLab = FindInstance ( NAUO, CTool->ShapeTool(), Tool, PDFileMap, ShapeLabelMap );
933         aSh = CTool->ShapeTool()->GetShape(aShLab);
934 //         Handle(Transfer_Binder) binder = TP->Find(NAUO);
935 //         if ( binder.IsNull() || ! binder->HasResult() )
936 //           continue;
937 //         aSh = TransferBRep::ShapeResult ( TP, binder );
938         if (!aSh.IsNull()) {
939           S = aSh;
940           break;
941         }
942       }
943       break;
944     }
945     if (isSkipSHUOstyle)
946       continue; // skip styled item which refer to SHUO
947     
948     if ( S.IsNull() )
949       continue;
950     
951     if ( ! SurfCol.IsNull() ) {
952       Quantity_Color col;
953       Styles.DecodeColor ( SurfCol, col );
954       if ( ! CTool->SetColor ( S, col, XCAFDoc_ColorSurf ))
955         SetColorToSubshape( CTool, S, col, XCAFDoc_ColorSurf );
956     }
957     if ( ! BoundCol.IsNull() ) {
958       Quantity_Color col;
959       Styles.DecodeColor ( BoundCol, col );
960       if ( ! CTool->SetColor ( S, col, XCAFDoc_ColorCurv ))
961         SetColorToSubshape(  CTool, S, col, XCAFDoc_ColorCurv );
962     }
963     if ( ! CurveCol.IsNull() ) {
964       Quantity_Color col;
965       Styles.DecodeColor ( CurveCol, col );
966       if ( ! CTool->SetColor ( S, col, XCAFDoc_ColorCurv ))
967         SetColorToSubshape(  CTool, S, col, XCAFDoc_ColorCurv );
968     }
969     if ( !IsVisible ) {
970       // sets the invisibility for shape.
971       TDF_Label aInvL;
972       if ( CTool->ShapeTool()->Search( S, aInvL ) )
973         CTool->SetVisibility( aInvL, Standard_False );
974     }
975   }
976   CTool->ReverseChainsOfTreeNodes();
977   return Standard_True;
978 }
979
980 //=======================================================================
981 //function : GetLabelFromPD
982 //purpose  : 
983 //=======================================================================
984
985 static TDF_Label GetLabelFromPD (const Handle(StepBasic_ProductDefinition) &PD,
986                                  const Handle(XCAFDoc_ShapeTool) &STool,
987                                  const Handle(Transfer_TransientProcess) &TP,
988                                  const STEPCAFControl_DataMapOfPDExternFile &PDFileMap,
989                                  const XCAFDoc_DataMapOfShapeLabel &ShapeLabelMap)
990 {
991   TDF_Label L;
992   if ( PDFileMap.IsBound ( PD ) ) {
993     Handle(STEPCAFControl_ExternFile) EF = PDFileMap.Find ( PD );
994     if ( ! EF.IsNull() ) {
995       L = EF->GetLabel();
996       if ( ! L.IsNull() ) return L;
997     }
998   }
999
1000   TopoDS_Shape S;
1001   Handle(Transfer_Binder) binder = TP->Find(PD);
1002   if ( binder.IsNull() || ! binder->HasResult() ) return L;
1003   S = TransferBRep::ShapeResult ( TP, binder );
1004   if ( S.IsNull() ) return L;
1005
1006   if ( S.IsNull() ) return L;
1007   if ( ShapeLabelMap.IsBound ( S ) )
1008     L = ShapeLabelMap.Find ( S );
1009   if ( L.IsNull() )
1010     STool->Search ( S, L, Standard_True, Standard_True, Standard_False );
1011   return L;
1012 }
1013
1014 //=======================================================================
1015 //function : FindInstance
1016 //purpose  : 
1017 //=======================================================================
1018
1019 TDF_Label STEPCAFControl_Reader::FindInstance (const Handle(StepRepr_NextAssemblyUsageOccurrence) &NAUO,
1020                                                const Handle(XCAFDoc_ShapeTool) &STool,
1021                                                const STEPConstruct_Tool &Tool,
1022                                                const STEPCAFControl_DataMapOfPDExternFile &PDFileMap,
1023                                                const XCAFDoc_DataMapOfShapeLabel &ShapeLabelMap)
1024 {
1025   TDF_Label L;
1026   
1027   // get shape resulting from CDSR (in fact, only location is interesting)
1028   Handle(Transfer_TransientProcess) TP = Tool.TransientProcess();
1029   Handle(Transfer_Binder) binder = TP->Find(NAUO);
1030   if ( binder.IsNull() || ! binder->HasResult() ) {
1031 #ifdef OCCT_DEBUG
1032     cout << "Error: STEPCAFControl_Reader::FindInstance: NAUO is not mapped to shape" << endl;
1033 #endif
1034     return L;
1035   }
1036   
1037   TopoDS_Shape S = TransferBRep::ShapeResult ( TP, binder );
1038   if ( S.IsNull() ) {
1039 #ifdef OCCT_DEBUG
1040     cout << "Error: STEPCAFControl_Reader::FindInstance: NAUO is not mapped to shape" << endl;
1041 #endif
1042     return L;
1043   }
1044
1045   // find component`s original label
1046   Handle(StepBasic_ProductDefinition) PD = NAUO->RelatedProductDefinition();
1047   if ( PD.IsNull() ) return L;
1048   TDF_Label Lref = GetLabelFromPD ( PD, STool, TP, PDFileMap, ShapeLabelMap );
1049   if ( Lref.IsNull() ) return L;
1050   
1051   // find main shape (assembly) label
1052   PD.Nullify();
1053   PD = NAUO->RelatingProductDefinition();
1054   if ( PD.IsNull() ) return L;
1055   TDF_Label L0 = GetLabelFromPD ( PD, STool, TP, PDFileMap, ShapeLabelMap );
1056   if ( L0.IsNull() ) return L;
1057   
1058   // if CDSR and NAUO are reversed, swap labels
1059   Handle(StepShape_ContextDependentShapeRepresentation) CDSR;
1060   Interface_EntityIterator subs1 = Tool.Graph().Sharings(NAUO);
1061   for (subs1.Start(); subs1.More(); subs1.Next()) {
1062     Handle(StepRepr_ProductDefinitionShape) PDS = 
1063       Handle(StepRepr_ProductDefinitionShape)::DownCast(subs1.Value());
1064     if(PDS.IsNull()) continue;
1065     Interface_EntityIterator subs2 = Tool.Graph().Sharings(PDS);
1066     for (subs2.Start(); subs2.More(); subs2.Next()) {
1067       Handle(StepShape_ContextDependentShapeRepresentation) CDSRtmp = 
1068         Handle(StepShape_ContextDependentShapeRepresentation)::DownCast(subs2.Value());
1069       if (CDSRtmp.IsNull()) continue;
1070       CDSR = CDSRtmp;
1071     }
1072   }
1073   if (CDSR.IsNull()) return L;
1074 //  if ( STEPConstruct_Assembly::CheckSRRReversesNAUO ( Tool.Model(), CDSR ) ) {
1075 //    TDF_Label Lsw = L0; L0 = Lref; Lref = Lsw;
1076 //  }
1077   
1078   // iterate on components to find proper one
1079   TDF_LabelSequence seq;
1080   XCAFDoc_ShapeTool::GetComponents ( L0, seq );
1081   for ( Standard_Integer k=1; L.IsNull() && k <= seq.Length(); k++ ) {
1082     TDF_Label Lcomp = seq(k), Lref2;
1083     if ( XCAFDoc_ShapeTool::GetReferredShape ( Lcomp, Lref2 ) && 
1084         Lref2 == Lref &&
1085         S.Location() == XCAFDoc_ShapeTool::GetLocation ( Lcomp ) ) 
1086       L = Lcomp;
1087   }
1088   
1089   return L;
1090 }
1091
1092 //=======================================================================
1093 //function : ReadNames
1094 //purpose  : 
1095 //=======================================================================
1096
1097 Standard_Boolean STEPCAFControl_Reader::ReadNames (const Handle(XSControl_WorkSession) &WS,
1098                                                    Handle(TDocStd_Document)& Doc,
1099                                                    const STEPCAFControl_DataMapOfPDExternFile &PDFileMap,
1100                                                    const XCAFDoc_DataMapOfShapeLabel &ShapeLabelMap) const
1101 {
1102   // get starting data
1103   Handle(Interface_InterfaceModel) Model = WS->Model();
1104   Handle(XSControl_TransferReader) TR = WS->TransferReader();
1105   Handle(Transfer_TransientProcess) TP = TR->TransientProcess();
1106   Handle(XCAFDoc_ShapeTool) STool = XCAFDoc_DocumentTool::ShapeTool( Doc->Main() );
1107   if ( STool.IsNull() ) return Standard_False;
1108   STEPConstruct_Tool Tool ( WS );
1109
1110   // iterate on model to find all SDRs and CDSRs
1111   Standard_Integer nb = Model->NbEntities();
1112   Handle(Standard_Type) tNAUO = STANDARD_TYPE(StepRepr_NextAssemblyUsageOccurrence);
1113   Handle(Standard_Type) tPD  = STANDARD_TYPE(StepBasic_ProductDefinition);
1114   Handle(TCollection_HAsciiString) name;
1115   TDF_Label L;
1116   for (Standard_Integer i = 1; i <= nb; i++) {
1117     Handle(Standard_Transient) enti = Model->Value(i);
1118
1119     // get description of NAUO
1120     if ( enti->DynamicType() == tNAUO ) {
1121       L.Nullify();
1122       Handle(StepRepr_NextAssemblyUsageOccurrence) NAUO = 
1123         Handle(StepRepr_NextAssemblyUsageOccurrence)::DownCast(enti);
1124       if(NAUO.IsNull()) continue;
1125       Interface_EntityIterator subs = WS->Graph().Sharings(NAUO);
1126       for (subs.Start(); subs.More(); subs.Next()) {
1127         Handle(StepRepr_ProductDefinitionShape) PDS = 
1128           Handle(StepRepr_ProductDefinitionShape)::DownCast(subs.Value());
1129         if(PDS.IsNull()) continue;
1130         Handle(StepBasic_ProductDefinitionRelationship) PDR = PDS->Definition().ProductDefinitionRelationship();
1131         if ( PDR.IsNull() ) continue;
1132         if ( PDR->HasDescription() && 
1133             PDR->Description()->Length() >0 ) name = PDR->Description();
1134         else if ( !PDR->Name().IsNull() && PDR->Name()->Length() >0 ) name = PDR->Name();
1135         else if ( !PDR->Id().IsNull()) name = PDR->Id();
1136         else name = new TCollection_HAsciiString;
1137       }
1138       // find proper label
1139       L = FindInstance ( NAUO, STool, Tool, PDFileMap, ShapeLabelMap );
1140       if ( L.IsNull() ) continue;
1141       TCollection_ExtendedString str ( name->String() );
1142       TDataStd_Name::Set ( L, str );
1143     }
1144
1145     // for PD get name of associated product
1146     if ( enti->DynamicType() == tPD ) {
1147       L.Nullify();
1148       Handle(StepBasic_ProductDefinition) PD = 
1149         Handle(StepBasic_ProductDefinition)::DownCast(enti);
1150       if(PD.IsNull()) continue;
1151       Handle(StepBasic_Product) Prod = (!PD->Formation().IsNull() ? PD->Formation()->OfProduct() : NULL);
1152       if (Prod.IsNull())
1153         name = new TCollection_HAsciiString;
1154       else if (!Prod->Name().IsNull() && Prod->Name()->UsefullLength() > 0) 
1155         name = Prod->Name();
1156       else if (!Prod->Id().IsNull()) 
1157         name = Prod->Id();
1158       else 
1159         name = new TCollection_HAsciiString;
1160       L = GetLabelFromPD ( PD, STool, TP, PDFileMap, ShapeLabelMap );
1161       if ( L.IsNull() ) continue;
1162       TCollection_ExtendedString str ( name->String() );
1163       TDataStd_Name::Set ( L, str );
1164     }
1165     // set a name to the document
1166     //TCollection_ExtendedString str ( name->String() );
1167     //TDataStd_Name::Set ( L, str );
1168   }
1169
1170   return Standard_True;
1171 }
1172
1173 //=======================================================================
1174 //function : GetLabelFromPD
1175 //purpose  : 
1176 //=======================================================================
1177
1178 static TDF_Label GetLabelFromPD (const Handle(StepBasic_ProductDefinition) &PD,
1179                                  const Handle(XCAFDoc_ShapeTool) &STool,
1180                                  const STEPConstruct_ValidationProps &Props,
1181                                  const STEPCAFControl_DataMapOfPDExternFile &PDFileMap,
1182                                  const XCAFDoc_DataMapOfShapeLabel &ShapeLabelMap)
1183 {
1184   TDF_Label L;
1185   if ( PDFileMap.IsBound ( PD ) ) {
1186     Handle(STEPCAFControl_ExternFile) EF = PDFileMap.Find ( PD );
1187     if ( ! EF.IsNull() ) {
1188       L = EF->GetLabel();
1189       if ( ! L.IsNull() ) return L;
1190     }
1191   }
1192   TopoDS_Shape S = Props.GetPropShape ( PD );
1193   if ( S.IsNull() ) return L;
1194   if ( ShapeLabelMap.IsBound ( S ) )
1195     L = ShapeLabelMap.Find ( S );
1196   if ( L.IsNull() )
1197     STool->Search ( S, L, Standard_True, Standard_True, Standard_False );
1198   return L;
1199 }
1200
1201 //=======================================================================
1202 //function : ReadValProps
1203 //purpose  : 
1204 //=======================================================================
1205
1206 Standard_Boolean STEPCAFControl_Reader::ReadValProps (const Handle(XSControl_WorkSession) &WS,
1207                                                       Handle(TDocStd_Document)& Doc,
1208                                                       const STEPCAFControl_DataMapOfPDExternFile &PDFileMap,
1209                                                       const XCAFDoc_DataMapOfShapeLabel &ShapeLabelMap) const
1210 {
1211   // get starting data
1212   Handle(Interface_InterfaceModel) Model = WS->Model();
1213   Handle(XSControl_TransferReader) TR = WS->TransferReader();
1214   Handle(Transfer_TransientProcess) TP = TR->TransientProcess();
1215   Handle(XCAFDoc_ShapeTool) STool = XCAFDoc_DocumentTool::ShapeTool( Doc->Main() );
1216   if ( STool.IsNull() ) return Standard_False;
1217
1218   // load props from the STEP model
1219   TColStd_SequenceOfTransient props;
1220   STEPConstruct_ValidationProps Props ( WS );
1221   if ( ! Props.LoadProps ( props ) ) {
1222 #ifdef OCCT_DEBUG
1223     cout << "Warning: no validation props found in the model" << endl;
1224 #endif
1225     return Standard_False;
1226   }
1227
1228   // interpret props one by one
1229   for (Standard_Integer i = 1; i <= props.Length(); i ++) {
1230     Handle(StepRepr_PropertyDefinitionRepresentation) PDR = 
1231       Handle(StepRepr_PropertyDefinitionRepresentation)::DownCast ( props.Value(i) );
1232     if ( PDR.IsNull() ) continue;
1233
1234     TDF_Label L;
1235
1236     Handle(StepRepr_PropertyDefinition) PD = PDR->Definition().PropertyDefinition();
1237     Interface_EntityIterator subs = Props.Graph().Shareds(PD);
1238     for (subs.Start(); L.IsNull() && subs.More(); subs.Next()) {
1239       if ( subs.Value()->IsKind(STANDARD_TYPE(StepRepr_ProductDefinitionShape)) ) {
1240         Handle(StepRepr_ProductDefinitionShape) PDS = Handle(StepRepr_ProductDefinitionShape)::DownCast(subs.Value());
1241         if(PDS.IsNull()) continue;
1242         // find corresponding NAUO
1243         Handle(StepRepr_NextAssemblyUsageOccurrence) NAUO;
1244         Interface_EntityIterator subs1 = Props.Graph().Shareds(PDS);
1245         for (subs1.Start(); NAUO.IsNull() && subs1.More(); subs1.Next()) {
1246           if ( subs1.Value()->IsKind(STANDARD_TYPE(StepRepr_NextAssemblyUsageOccurrence)) ) 
1247             NAUO = Handle(StepRepr_NextAssemblyUsageOccurrence)::DownCast(subs1.Value());
1248         }
1249         if ( !NAUO.IsNull() ) {
1250           L = FindInstance ( NAUO, STool, WS, PDFileMap, ShapeLabelMap );
1251           if ( L.IsNull() ) continue;
1252         }
1253         else {
1254           // find corresponding ProductDefinition:
1255           Handle(StepBasic_ProductDefinition) ProdDef;
1256           Interface_EntityIterator subsPDS = Props.Graph().Shareds(PDS);
1257           for (subsPDS.Start(); ProdDef.IsNull() && subsPDS.More(); subsPDS.Next()) {
1258             if ( subsPDS.Value()->IsKind(STANDARD_TYPE(StepBasic_ProductDefinition)) ) 
1259               ProdDef = Handle(StepBasic_ProductDefinition)::DownCast(subsPDS.Value());
1260           }
1261           if ( ProdDef.IsNull() ) continue;
1262           L = GetLabelFromPD ( ProdDef, STool, Props, PDFileMap, ShapeLabelMap );
1263         }
1264       }
1265
1266       if ( subs.Value()->IsKind(STANDARD_TYPE(StepRepr_ShapeAspect)) ) {
1267         Handle(StepRepr_ShapeAspect) SA = Handle(StepRepr_ShapeAspect)::DownCast(subs.Value());
1268         if(SA.IsNull()) continue;
1269         // find ShapeRepresentation
1270         Handle(StepShape_ShapeRepresentation) SR;
1271         Interface_EntityIterator subs1 = Props.Graph().Sharings(SA);
1272         for(subs1.Start(); subs1.More() && SR.IsNull(); subs1.Next()) {
1273           Handle(StepRepr_PropertyDefinition) PropD1 = 
1274             Handle(StepRepr_PropertyDefinition)::DownCast(subs1.Value());
1275           if(PropD1.IsNull()) continue;
1276           Interface_EntityIterator subs2 = Props.Graph().Sharings(PropD1);
1277           for(subs2.Start(); subs2.More() && SR.IsNull(); subs2.Next()) {
1278             Handle(StepShape_ShapeDefinitionRepresentation) SDR =
1279               Handle(StepShape_ShapeDefinitionRepresentation)::DownCast(subs2.Value());
1280             if(SDR.IsNull()) continue;
1281             SR = Handle(StepShape_ShapeRepresentation)::DownCast(SDR->UsedRepresentation());
1282           }
1283         }
1284         if(SR.IsNull()) continue;
1285         Handle(Transfer_Binder) binder;
1286         for(Standard_Integer ir=1; ir<=SR->NbItems() && binder.IsNull(); ir++) {
1287           if(SR->ItemsValue(ir)->IsKind(STANDARD_TYPE(StepShape_SolidModel))) {
1288             Handle(StepShape_SolidModel) SM = 
1289               Handle(StepShape_SolidModel)::DownCast(SR->ItemsValue(ir));
1290             binder = TP->Find(SM);
1291           }
1292           else if(SR->ItemsValue(ir)->IsKind(STANDARD_TYPE(StepShape_ShellBasedSurfaceModel))) {
1293             Handle(StepShape_ShellBasedSurfaceModel) SBSM =
1294               Handle(StepShape_ShellBasedSurfaceModel)::DownCast(SR->ItemsValue(ir));
1295             binder = TP->Find(SBSM);
1296           }
1297           else if(SR->ItemsValue(ir)->IsKind(STANDARD_TYPE(StepShape_GeometricSet))) {
1298             Handle(StepShape_GeometricSet) GS =
1299               Handle(StepShape_GeometricSet)::DownCast(SR->ItemsValue(ir));
1300             binder = TP->Find(GS);
1301           }
1302         }
1303         if ( binder.IsNull() || ! binder->HasResult() ) continue;
1304         TopoDS_Shape S;
1305         S = TransferBRep::ShapeResult ( TP, binder );
1306         if(S.IsNull()) continue;
1307         if ( ShapeLabelMap.IsBound ( S ) )
1308           L = ShapeLabelMap.Find ( S );
1309         if ( L.IsNull() )
1310           STool->Search ( S, L, Standard_True, Standard_True, Standard_True );
1311       }
1312     }
1313
1314     if(L.IsNull()) continue;
1315       
1316     // decode validation properties
1317     Handle(StepRepr_Representation) rep = PDR->UsedRepresentation();
1318     for ( Standard_Integer j=1; j <= rep->NbItems(); j++ ) {
1319       Handle(StepRepr_RepresentationItem) ent = rep->ItemsValue(j);
1320       Standard_Boolean isArea;
1321       Standard_Real val;
1322       gp_Pnt pos;
1323       if ( Props.GetPropReal ( ent, val, isArea ) ) {
1324         if ( isArea ) XCAFDoc_Area::Set ( L, val );
1325         else XCAFDoc_Volume::Set ( L, val );
1326       }
1327       else if ( Props.GetPropPnt ( ent, rep->ContextOfItems(), pos ) ) {
1328         XCAFDoc_Centroid::Set ( L, pos );
1329       }
1330     }
1331   }
1332   return Standard_True;
1333 }
1334
1335 //=======================================================================
1336 //function : ReadLayers
1337 //purpose  : 
1338 //=======================================================================
1339
1340 Standard_Boolean STEPCAFControl_Reader::ReadLayers (const Handle(XSControl_WorkSession) &WS,
1341                                                     Handle(TDocStd_Document)& Doc) const
1342 {
1343   Handle(Interface_InterfaceModel) Model = WS->Model();
1344   Handle(XSControl_TransferReader) TR = WS->TransferReader();
1345   Handle(Transfer_TransientProcess) TP = TR->TransientProcess();
1346   Handle(XCAFDoc_ShapeTool) STool = XCAFDoc_DocumentTool::ShapeTool( Doc->Main() );
1347   if ( STool.IsNull() ) return Standard_False;
1348   Handle(XCAFDoc_LayerTool) LTool = XCAFDoc_DocumentTool::LayerTool( Doc->Main() );
1349   if ( LTool.IsNull() ) return Standard_False;
1350   
1351   Handle(Standard_Type) tSVPLA = STANDARD_TYPE(StepVisual_PresentationLayerAssignment);
1352   Standard_Integer nb = Model->NbEntities();
1353   Handle(TCollection_HAsciiString) name;
1354   
1355   for (Standard_Integer i = 1; i <= nb; i ++) {
1356     Handle(Standard_Transient) enti = Model->Value(i);
1357     if ( ! enti->IsKind ( tSVPLA ) ) continue;
1358     Handle(StepVisual_PresentationLayerAssignment) SVPLA = 
1359       Handle(StepVisual_PresentationLayerAssignment)::DownCast(enti);
1360     
1361     Handle(TCollection_HAsciiString) descr = SVPLA->Description();
1362     Handle(TCollection_HAsciiString) hName = SVPLA->Name();
1363     TCollection_ExtendedString aLayerName ( hName->String() );
1364      
1365     // find a target shape and its label in the document
1366     for (Standard_Integer j = 1; j <= SVPLA->NbAssignedItems(); j++ ) {
1367       StepVisual_LayeredItem LI = SVPLA->AssignedItemsValue(j);
1368       Handle(Transfer_Binder) binder = TP->Find( LI.Value() );
1369       if ( binder.IsNull() || ! binder->HasResult() ) continue;
1370       
1371       TopoDS_Shape S = TransferBRep::ShapeResult ( TP, binder );
1372       if ( S.IsNull() ) continue;
1373         
1374       TDF_Label shL;
1375       if ( ! STool->Search ( S, shL, Standard_True, Standard_True, Standard_True ) ) continue;
1376       LTool->SetLayer ( shL, aLayerName );
1377     }
1378     
1379     // check invisibility
1380     Interface_EntityIterator subs = WS->Graph().Sharings(SVPLA);
1381     for (subs.Start(); subs.More(); subs.Next()) {
1382       if ( ! subs.Value()->IsKind(STANDARD_TYPE(StepVisual_Invisibility)) ) continue;
1383 #ifdef OCCT_DEBUG
1384       cout<< "\tLayer \"" << aLayerName << "\" is invisible"<<endl;
1385 #endif
1386       //TDF_Label InvLayerLab = LTool->FindLayer(aLayerName);
1387       TDF_Label InvLayerLab = LTool->AddLayer(aLayerName); //skl for OCC3926
1388       Handle(TDataStd_UAttribute) aUAttr;
1389       aUAttr->Set( InvLayerLab, XCAFDoc::InvisibleGUID() );
1390     }
1391   }
1392   return Standard_True;
1393 }
1394
1395 //=======================================================================
1396 //function : ReadSHUOs
1397 //purpose  : 
1398 //=======================================================================
1399
1400 static Standard_Boolean findNextSHUOlevel (const Handle(XSControl_WorkSession) &WS,
1401                                            const Handle(StepRepr_SpecifiedHigherUsageOccurrence)& SHUO,
1402                                            const Handle(XCAFDoc_ShapeTool)& STool,
1403                                            const STEPCAFControl_DataMapOfPDExternFile &PDFileMap,
1404                                            const XCAFDoc_DataMapOfShapeLabel &ShapeLabelMap,
1405                                            TDF_LabelSequence& aLabels)
1406 {
1407   Interface_EntityIterator subs = WS->HGraph()->Graph().Sharings(SHUO);
1408   Handle(StepRepr_SpecifiedHigherUsageOccurrence) subSHUO;
1409   for (subs.Start(); subs.More(); subs.Next()) {
1410     if (subs.Value()->IsKind(STANDARD_TYPE(StepRepr_SpecifiedHigherUsageOccurrence))) {
1411       subSHUO = Handle(StepRepr_SpecifiedHigherUsageOccurrence)::DownCast(subs.Value());
1412       break;
1413     }
1414   }
1415   if (subSHUO.IsNull())
1416     return Standard_False;
1417   
1418   Handle(StepRepr_NextAssemblyUsageOccurrence) NUNAUO =
1419     Handle(StepRepr_NextAssemblyUsageOccurrence)::DownCast(subSHUO->NextUsage());
1420   if (NUNAUO.IsNull())
1421     return Standard_False;
1422 //   Handle(Interface_InterfaceModel) Model = WS->Model();
1423 //   Handle(XSControl_TransferReader) TR = WS->TransferReader();
1424 //   Handle(Transfer_TransientProcess) TP = TR->TransientProcess();
1425 //   Handle(Transfer_Binder) binder = TP->Find(NUNAUO);
1426 //   if ( binder.IsNull() || ! binder->HasResult() )
1427 //     return Standard_False;
1428 //   TopoDS_Shape NUSh = TransferBRep::ShapeResult ( TP, binder );
1429   // get label of NAUO next level
1430   TDF_Label NULab;
1431   STEPConstruct_Tool Tool( WS );
1432   NULab = STEPCAFControl_Reader::FindInstance ( NUNAUO, STool, Tool, PDFileMap, ShapeLabelMap ); 
1433 //   STool->Search(NUSh, NUlab);
1434   if (NULab.IsNull())
1435     return Standard_False;
1436   aLabels.Append( NULab );
1437   // and check by recurse.
1438   findNextSHUOlevel( WS, subSHUO, STool, PDFileMap, ShapeLabelMap, aLabels );
1439   return Standard_True;
1440 }
1441
1442
1443 //=======================================================================
1444 //function : setSHUOintoDoc
1445 //purpose  : auxilary
1446 //=======================================================================
1447 static TDF_Label setSHUOintoDoc (const Handle(XSControl_WorkSession) &WS,
1448                                  const Handle(StepRepr_SpecifiedHigherUsageOccurrence)& SHUO,
1449                                  const Handle(XCAFDoc_ShapeTool)& STool,
1450                                  const STEPCAFControl_DataMapOfPDExternFile &PDFileMap,
1451                                  const XCAFDoc_DataMapOfShapeLabel &ShapeLabelMap)
1452 {
1453   TDF_Label aMainLabel;
1454   // get upper usage NAUO from SHUO.
1455   Handle(StepRepr_NextAssemblyUsageOccurrence) UUNAUO =
1456     Handle(StepRepr_NextAssemblyUsageOccurrence)::DownCast(SHUO->UpperUsage());
1457   Handle(StepRepr_NextAssemblyUsageOccurrence) NUNAUO =
1458     Handle(StepRepr_NextAssemblyUsageOccurrence)::DownCast(SHUO->NextUsage());
1459   if ( UUNAUO.IsNull() || NUNAUO.IsNull() ) {
1460 #ifdef OCCT_DEBUG
1461     cout << "Warning: " << __FILE__ <<": Upper_usage or Next_usage of styled SHUO is null. Skip it" << endl;
1462 #endif
1463     return aMainLabel;
1464   }
1465 //   Handle(Interface_InterfaceModel) Model = WS->Model();
1466 //   Handle(XSControl_TransferReader) TR = WS->TransferReader();
1467 //   Handle(Transfer_TransientProcess) TP = TR->TransientProcess();
1468 //   TopoDS_Shape UUSh, NUSh;
1469 //   Handle(Transfer_Binder) binder = TP->Find(UUNAUO);
1470 //   if ( binder.IsNull() || ! binder->HasResult() )
1471 //     return aMainLabel;
1472 //   UUSh = TransferBRep::ShapeResult ( TP, binder );
1473 //   binder = TP->Find(NUNAUO);
1474 //   if ( binder.IsNull() || ! binder->HasResult() )
1475 //     return aMainLabel;
1476 //   NUSh = TransferBRep::ShapeResult ( TP, binder );
1477
1478   // get first labels for first SHUO attribute
1479   TDF_Label UULab, NULab;
1480   STEPConstruct_Tool Tool( WS );
1481   UULab = STEPCAFControl_Reader::FindInstance ( UUNAUO, STool, Tool, PDFileMap, ShapeLabelMap ); 
1482   NULab = STEPCAFControl_Reader::FindInstance ( NUNAUO, STool, Tool, PDFileMap, ShapeLabelMap ); 
1483   
1484 //   STool->Search(UUSh, UULab);
1485 //   STool->Search(NUSh, NULab);
1486   if (UULab.IsNull() || NULab.IsNull()) return aMainLabel;
1487   //create sequence fo labels to set SHUO structure into the document
1488   TDF_LabelSequence ShuoLabels;
1489   ShuoLabels.Append( UULab );
1490   ShuoLabels.Append( NULab );
1491   // add all other labels of sub SHUO entities
1492   findNextSHUOlevel( WS, SHUO, STool, PDFileMap, ShapeLabelMap, ShuoLabels );
1493   // last accord for SHUO
1494   Handle(XCAFDoc_GraphNode) anSHUOAttr;
1495   if ( STool->SetSHUO( ShuoLabels, anSHUOAttr ) )
1496     aMainLabel = anSHUOAttr->Label();
1497   
1498   return aMainLabel;
1499 }
1500
1501
1502 //=======================================================================
1503 //function : ReadSHUOs
1504 //purpose  : 
1505 //=======================================================================
1506
1507 Standard_Boolean STEPCAFControl_Reader::ReadSHUOs (const Handle(XSControl_WorkSession) &WS,
1508                                                    Handle(TDocStd_Document)& Doc,
1509                                                    const STEPCAFControl_DataMapOfPDExternFile &PDFileMap,
1510                                                    const XCAFDoc_DataMapOfShapeLabel &ShapeLabelMap) const
1511 {
1512   // the big part code duplication from ReadColors.
1513   // It is possible to share this code functionality, just to decide how ???
1514   Handle(XCAFDoc_ColorTool) CTool = XCAFDoc_DocumentTool::ColorTool( Doc->Main() );
1515   Handle(XCAFDoc_ShapeTool) STool = CTool->ShapeTool();
1516   
1517   STEPConstruct_Styles Styles ( WS );
1518   if ( ! Styles.LoadStyles() ) {
1519 #ifdef OCCT_DEBUG
1520     cout << "Warning: no styles are found in the model" << endl;
1521 #endif
1522     return Standard_False;
1523   }
1524   // searching for invisible items in the model
1525   Handle(TColStd_HSequenceOfTransient) aHSeqOfInvisStyle = new TColStd_HSequenceOfTransient;
1526   Styles.LoadInvisStyles( aHSeqOfInvisStyle );
1527   // parse and search for color attributes
1528   Standard_Integer nb = Styles.NbStyles();
1529   for ( Standard_Integer i=1; i <= nb; i++ ) {
1530     Handle(StepVisual_StyledItem) style = Styles.Style ( i );
1531     if ( style.IsNull() ) continue;
1532     
1533     Standard_Boolean IsVisible = Standard_True;
1534     // check the visibility of styled item.
1535     for (Standard_Integer si = 1; si <= aHSeqOfInvisStyle->Length(); si++ ) {
1536       if ( style != aHSeqOfInvisStyle->Value( si ) )
1537         continue;
1538       // found that current style is invisible.
1539 #ifdef OCCT_DEBUG
1540       cout << "Warning: item No " << i << "(" << style->Item()->DynamicType()->Name() << ") is invisible" << endl;
1541 #endif
1542       IsVisible = Standard_False;
1543       break;
1544     }
1545
1546     Handle(StepVisual_Colour) SurfCol, BoundCol, CurveCol;
1547     // check if it is component style
1548     Standard_Boolean IsComponent = Standard_False;
1549     if ( ! Styles.GetColors ( style, SurfCol, BoundCol, CurveCol, IsComponent ) && IsVisible )
1550       continue;
1551     if (!IsComponent)
1552       continue;
1553     Handle(StepShape_ShapeRepresentation) aSR;
1554     findStyledSR( style, aSR );
1555     // search for SR along model
1556     if ( aSR.IsNull() )
1557       continue;
1558     Interface_EntityIterator subs = WS->HGraph()->Graph().Sharings( aSR );
1559     Handle(StepShape_ShapeDefinitionRepresentation) aSDR;
1560     for (subs.Start(); subs.More(); subs.Next()) {
1561       aSDR = Handle(StepShape_ShapeDefinitionRepresentation)::DownCast(subs.Value());
1562       if ( aSDR.IsNull() )
1563         continue;
1564       StepRepr_RepresentedDefinition aPDSselect = aSDR->Definition();
1565       Handle(StepRepr_ProductDefinitionShape) PDS = 
1566         Handle(StepRepr_ProductDefinitionShape)::DownCast(aPDSselect.PropertyDefinition());
1567       if ( PDS.IsNull() )
1568         continue;
1569       StepRepr_CharacterizedDefinition aCharDef = PDS->Definition();
1570       Handle(StepRepr_SpecifiedHigherUsageOccurrence) SHUO =
1571         Handle(StepRepr_SpecifiedHigherUsageOccurrence)::DownCast(aCharDef.ProductDefinitionRelationship());
1572       if ( SHUO.IsNull() )
1573         continue;
1574       
1575       // set the SHUO structure to the document
1576       TDF_Label aLabelForStyle = setSHUOintoDoc( WS, SHUO, STool, PDFileMap, ShapeLabelMap );
1577       if ( aLabelForStyle.IsNull() ) {
1578 #ifdef OCCT_DEBUG
1579         cout << "Warning: " << __FILE__ <<": coudnot create SHUO structure in the document" << endl;
1580 #endif
1581         continue;
1582       }
1583       // now set the style to the SHUO main label.
1584       if ( ! SurfCol.IsNull() ) {
1585         Quantity_Color col;
1586         Styles.DecodeColor ( SurfCol, col );
1587         CTool->SetColor ( aLabelForStyle, col, XCAFDoc_ColorSurf );
1588       }
1589       if ( ! BoundCol.IsNull() ) {
1590         Quantity_Color col;
1591         Styles.DecodeColor ( BoundCol, col );
1592         CTool->SetColor ( aLabelForStyle, col, XCAFDoc_ColorCurv );
1593       }
1594       if ( ! CurveCol.IsNull() ) {
1595         Quantity_Color col;
1596         Styles.DecodeColor ( CurveCol, col );
1597         CTool->SetColor ( aLabelForStyle, col, XCAFDoc_ColorCurv );
1598       }
1599       if ( !IsVisible )
1600         // sets the invisibility for shape.
1601         CTool->SetVisibility( aLabelForStyle, Standard_False );
1602       
1603     } // end search SHUO by SDR
1604   } // end iterates on styles
1605       
1606   return Standard_True;
1607 }
1608
1609
1610 //=======================================================================
1611 //function : GetLengthConversionFactor
1612 //purpose  : 
1613 //=======================================================================
1614 static Standard_Boolean GetLengthConversionFactor(Handle(StepBasic_NamedUnit)& NU,
1615                                                   Standard_Real& afact)
1616 {
1617   afact=1.;
1618   if( !NU->IsKind(STANDARD_TYPE(StepBasic_ConversionBasedUnitAndLengthUnit)) ) return Standard_False;
1619   Handle(StepBasic_ConversionBasedUnitAndLengthUnit) CBULU =
1620     Handle(StepBasic_ConversionBasedUnitAndLengthUnit)::DownCast(NU);
1621   Handle(StepBasic_MeasureWithUnit) MWUCBU = CBULU->ConversionFactor();
1622   afact = MWUCBU->ValueComponent();
1623   StepBasic_Unit anUnit2 = MWUCBU->UnitComponent();
1624   if(anUnit2.CaseNum(anUnit2.Value())==1) {
1625     Handle(StepBasic_NamedUnit) NU2 = anUnit2.NamedUnit();
1626     if(NU2->IsKind(STANDARD_TYPE(StepBasic_SiUnit))) {
1627       Handle(StepBasic_SiUnit) SU = Handle(StepBasic_SiUnit)::DownCast(NU2);
1628       if(SU->Name()==StepBasic_sunMetre) {
1629         if(SU->HasPrefix()) 
1630           afact *= STEPConstruct_UnitContext::ConvertSiPrefix (SU->Prefix());
1631         // convert m to mm
1632         afact *= 1000.;
1633       }
1634     }
1635   }
1636   return Standard_True;
1637 }
1638
1639
1640 //=======================================================================
1641 //function : GetAngleConversionFactor
1642 //purpose  : 
1643 //=======================================================================
1644 static Standard_Boolean GetAngleConversionFactor(Handle(StepBasic_NamedUnit)& NU,
1645                                                   Standard_Real& afact)
1646 {
1647   afact=1.;
1648   if( !NU->IsKind(STANDARD_TYPE(StepBasic_ConversionBasedUnitAndPlaneAngleUnit)) ) return Standard_False;
1649   Handle(StepBasic_ConversionBasedUnitAndPlaneAngleUnit) CBULU =
1650     Handle(StepBasic_ConversionBasedUnitAndPlaneAngleUnit)::DownCast(NU);
1651   Handle(StepBasic_MeasureWithUnit) MWUCBU = CBULU->ConversionFactor();
1652   afact = MWUCBU->ValueComponent();
1653   StepBasic_Unit anUnit2 = MWUCBU->UnitComponent();
1654   if(anUnit2.CaseNum(anUnit2.Value())==1) {
1655     Handle(StepBasic_NamedUnit) NU2 = anUnit2.NamedUnit();
1656     if(NU2->IsKind(STANDARD_TYPE(StepBasic_SiUnit))) {
1657       Handle(StepBasic_SiUnit) SU = Handle(StepBasic_SiUnit)::DownCast(NU2);
1658       if(SU->Name()==StepBasic_sunRadian) {
1659         if(SU->HasPrefix()) 
1660           afact *= STEPConstruct_UnitContext::ConvertSiPrefix (SU->Prefix());
1661         // convert radian to deg
1662         afact *= 180/M_PI;
1663       }
1664     }
1665   }
1666   return Standard_True;
1667 }
1668
1669 //=======================================================================
1670 //function : GetMassConversionFactor
1671 //purpose  : 
1672 //=======================================================================
1673 static Standard_Boolean GetMassConversionFactor(Handle(StepBasic_NamedUnit)& NU,
1674                                                 Standard_Real& afact)
1675 {
1676   afact=1.;
1677   if( !NU->IsKind(STANDARD_TYPE(StepBasic_ConversionBasedUnitAndMassUnit)) ) return Standard_False;
1678   Handle(StepBasic_ConversionBasedUnitAndMassUnit) CBUMU =
1679     Handle(StepBasic_ConversionBasedUnitAndMassUnit)::DownCast(NU);
1680   Handle(StepBasic_MeasureWithUnit) MWUCBU = CBUMU->ConversionFactor();
1681   afact = MWUCBU->ValueComponent();
1682   StepBasic_Unit anUnit2 = MWUCBU->UnitComponent();
1683   if(anUnit2.CaseNum(anUnit2.Value())==1) {
1684     Handle(StepBasic_NamedUnit) NU2 = anUnit2.NamedUnit();
1685     if(NU2->IsKind(STANDARD_TYPE(StepBasic_SiUnit))) {
1686       Handle(StepBasic_SiUnit) SU = Handle(StepBasic_SiUnit)::DownCast(NU2);
1687       if(SU->Name()==StepBasic_sunGram) {
1688         if(SU->HasPrefix())
1689           afact *= STEPConstruct_UnitContext::ConvertSiPrefix (SU->Prefix());
1690       }
1691     }
1692   }
1693   return Standard_True;
1694 }
1695
1696
1697 //=======================================================================
1698 //function : ReadDatums
1699 //purpose  : auxilary
1700 //=======================================================================
1701 static Standard_Boolean ReadDatums(const Handle(XCAFDoc_ShapeTool) &STool,
1702                                    const Handle(XCAFDoc_DimTolTool) &DGTTool,
1703                                    const Interface_Graph &graph,
1704                                    Handle(Transfer_TransientProcess) &TP,
1705                                    const TDF_Label TolerL,
1706                                    const Handle(StepDimTol_GeometricToleranceWithDatumReference) GTWDR)
1707 {
1708   if(GTWDR.IsNull()) return Standard_False;
1709   Handle(StepDimTol_HArray1OfDatumReference) HADR = GTWDR->DatumSystem();
1710   if(HADR.IsNull()) return Standard_False;
1711   for(Standard_Integer idr=1; idr<=HADR->Length(); idr++) {
1712     Handle(StepDimTol_DatumReference) DR = HADR->Value(idr);
1713     Handle(StepDimTol_Datum) aDatum = DR->ReferencedDatum();
1714     if(aDatum.IsNull()) continue;
1715     Interface_EntityIterator subs4 = graph.Sharings(aDatum);
1716     for(subs4.Start(); subs4.More(); subs4.Next()) {
1717       Handle(StepRepr_ShapeAspectRelationship) SAR = 
1718         Handle(StepRepr_ShapeAspectRelationship)::DownCast(subs4.Value());
1719       if(SAR.IsNull()) continue;
1720       Handle(StepDimTol_DatumFeature) DF = 
1721         Handle(StepDimTol_DatumFeature)::DownCast(SAR->RelatingShapeAspect());
1722       if(DF.IsNull()) continue;
1723       Interface_EntityIterator subs5 = graph.Sharings(DF);
1724       Handle(StepRepr_PropertyDefinition) PropDef;
1725       for(subs5.Start(); subs5.More() && PropDef.IsNull(); subs5.Next()) {
1726         PropDef = Handle(StepRepr_PropertyDefinition)::DownCast(subs5.Value());
1727       }
1728       if(PropDef.IsNull()) continue;
1729       Handle(StepShape_AdvancedFace) AF;
1730       subs5 = graph.Sharings(PropDef);
1731       for(subs5.Start(); subs5.More(); subs5.Next()) {
1732         Handle(StepShape_ShapeDefinitionRepresentation) SDR = 
1733           Handle(StepShape_ShapeDefinitionRepresentation)::DownCast(subs5.Value());
1734         if(!SDR.IsNull()) {
1735           Handle(StepRepr_Representation) Repr = SDR->UsedRepresentation();
1736           if( !Repr.IsNull() && Repr->NbItems()>0 ) {
1737             Handle(StepRepr_RepresentationItem) RI = Repr->ItemsValue(1);
1738             AF = Handle(StepShape_AdvancedFace)::DownCast(RI);
1739           }
1740         }
1741       }
1742       if(AF.IsNull()) return Standard_False;
1743       Standard_Integer index = TP->MapIndex(AF);
1744       TopoDS_Shape aSh;
1745       if(index >0) {
1746         Handle(Transfer_Binder) binder = TP->MapItem(index);
1747         aSh = TransferBRep::ShapeResult(binder);
1748       }
1749       if(aSh.IsNull()) continue; 
1750       TDF_Label shL;
1751       if( !STool->Search(aSh, shL, Standard_True, Standard_True, Standard_True) ) continue;
1752       DGTTool->SetDatum(shL,TolerL,PropDef->Name(),PropDef->Description(),aDatum->Identification());
1753     }
1754   }
1755   return Standard_True;
1756 }
1757
1758
1759 //=======================================================================
1760 //function : setDatumToXCAF
1761 //purpose  : 
1762 //=======================================================================
1763 static Standard_Boolean setDatumToXCAF(const Handle(StepDimTol_Datum)& theDat,
1764                                         const TDF_Label theGDTL,
1765                                         const Standard_Integer thePositionCounter,
1766                                         const XCAFDimTolObjects_DatumModifiersSequence& aXCAFModifiers,
1767                                         const XCAFDimTolObjects_DatumModifWithValue aXCAFModifWithVal,
1768                                         const Standard_Real aModifValue,
1769                                         const Handle(TDocStd_Document)& theDoc,
1770                                         const Handle(XSControl_WorkSession)& theWS)
1771 {
1772   Handle(XCAFDoc_ShapeTool) aSTool = XCAFDoc_DocumentTool::ShapeTool( theDoc->Main() );
1773   Handle(XCAFDoc_DimTolTool) aDGTTool = XCAFDoc_DocumentTool::DimTolTool( theDoc->Main() );
1774   Handle(XSControl_TransferReader) aTR = theWS->TransferReader();
1775   Handle(Transfer_TransientProcess) aTP = aTR->TransientProcess();
1776   const Interface_Graph& aGraph = aTP->Graph();
1777   Handle(XCAFDoc_Datum) aDat;
1778   TDF_Label aShL;
1779   Standard_Boolean aRefShapeIsFound = Standard_False;
1780   Standard_Boolean aFirstStep = Standard_True;
1781   Interface_EntityIterator anIterD = aGraph.Sharings(theDat);
1782   for(anIterD.Start(); anIterD.More(); anIterD.Next()) {
1783     Handle(StepRepr_ShapeAspectRelationship) aSAR = 
1784       Handle(StepRepr_ShapeAspectRelationship)::DownCast(anIterD.Value());
1785     if(aSAR.IsNull()) continue;
1786
1787     Handle(StepRepr_ShapeAspect) aSA = aSAR->RelatingShapeAspect();
1788     if (aSA.IsNull()) continue;
1789     Handle(StepAP242_GeometricItemSpecificUsage) aPGISU;
1790     if(aSA->IsKind(STANDARD_TYPE(StepRepr_CompShAspAndDatumFeatAndShAsp)))
1791     {
1792       //processing for complex entity
1793       Interface_EntityIterator anIterC = aGraph.Sharings(aSA);
1794       for(anIterC.Start(); anIterC.More(); anIterC.Next()) {
1795         Handle(StepRepr_ShapeAspectRelationship) SAR = 
1796           Handle(StepRepr_ShapeAspectRelationship)::DownCast(anIterC.Value());
1797         if(SAR.IsNull()) continue;
1798         Handle(StepRepr_ShapeAspect) aS = 
1799           Handle(StepRepr_ShapeAspect)::DownCast(SAR->RelatedShapeAspect());
1800         if(aS.IsNull()) continue;
1801         Interface_EntityIterator anIterSA = aGraph.Sharings(aS);
1802         for(anIterSA.Start(); anIterSA.More() && aPGISU.IsNull(); anIterSA.Next()) {
1803           aPGISU = Handle(StepAP242_GeometricItemSpecificUsage)::DownCast(anIterSA.Value());
1804         }
1805         if(!aPGISU.IsNull()){
1806           aSA = aS;
1807           break;
1808         }
1809       }
1810     }
1811     else if(aSA->IsKind(STANDARD_TYPE(StepDimTol_PlacedDatumTargetFeature)))
1812     {
1813       //processing for datum target
1814       Interface_EntityIterator anIterDTF = aGraph.Sharings(aSA);
1815       for(anIterDTF.Start(); anIterDTF.More(); anIterDTF.Next()) {
1816         if(anIterDTF.Value()->IsKind(STANDARD_TYPE(StepRepr_FeatureForDatumTargetRelationship)))
1817         {
1818           Handle(StepRepr_FeatureForDatumTargetRelationship) aFFDTR =
1819             Handle(StepRepr_FeatureForDatumTargetRelationship)::DownCast(anIterDTF.Value());
1820           Handle(StepRepr_ShapeAspect) aTmpSA = aFFDTR->RelatedShapeAspect();
1821           Interface_EntityIterator anIterDSWP = aGraph.Sharings(aTmpSA);
1822           for(anIterDSWP.Start(); anIterDSWP.More() && aPGISU.IsNull(); anIterDSWP.Next()) {
1823             aPGISU = Handle(StepAP242_GeometricItemSpecificUsage)::DownCast(anIterDSWP.Value());
1824     }
1825         }
1826       }
1827     }
1828     if (aSA.IsNull()) continue;
1829     Interface_EntityIterator anIterDSWP = aGraph.Sharings(aSA);
1830     for(anIterDSWP.Start(); anIterDSWP.More() && aPGISU.IsNull(); anIterDSWP.Next()) {
1831       aPGISU = Handle(StepAP242_GeometricItemSpecificUsage)::DownCast(anIterDSWP.Value());
1832     }
1833     if(aPGISU.IsNull()) continue;
1834     // get representation item
1835     Handle(StepRepr_RepresentationItem) aRI;
1836     for(Standard_Integer i = 1 ; i <= aPGISU->NbIdentifiedItem() && aRI.IsNull(); i++)
1837     {
1838       aRI = Handle(StepRepr_RepresentationItem)::DownCast(aPGISU->IdentifiedItemValue(i));
1839     }
1840     if(aRI.IsNull()) continue;
1841     Standard_Integer index = aTP->MapIndex(aRI);
1842     TopoDS_Shape aSh;
1843     if(index >0) {
1844       Handle(Transfer_Binder) binder = aTP->MapItem(index);
1845       aSh = TransferBRep::ShapeResult(binder);
1846     }
1847     if(aSh.IsNull()) continue; 
1848     if( !aSTool->Search(aSh, aShL, Standard_True, Standard_True, Standard_True) ) continue;
1849     Handle(TDataStd_TreeNode) aNode;
1850     if(aFirstStep && aShL.FindAttribute(XCAFDoc::DatumRefGUID(),aNode) && aNode->HasFirst() &&
1851        aNode->First()->Label().FindAttribute(XCAFDoc_Datum::GetID(),aDat))
1852     {
1853       //if datums already attached, not need add datum target
1854       aRefShapeIsFound = Standard_True;
1855     }
1856     aFirstStep = Standard_False;
1857     Handle(XCAFDimTolObjects_DatumObject) aDatObj;
1858     if(aSA->IsKind(STANDARD_TYPE(StepDimTol_PlacedDatumTargetFeature)))
1859     {
1860       if(!aRefShapeIsFound)
1861       {
1862         //if datum targers not yet added
1863         TDF_Label aDatL = aDGTTool->AddDatum();
1864         aDat = XCAFDoc_Datum::Set(aDatL);
1865         aDGTTool->SetDatum(aShL, aDatL);
1866         aDatObj = aDat->GetObject();
1867         aDatObj->SetName(theDat->Identification());
1868         aDatObj->SetPosition (thePositionCounter);
1869         if(!aXCAFModifiers.IsEmpty())
1870           aDatObj->SetModifiers(aXCAFModifiers);
1871         if (aXCAFModifWithVal != XCAFDimTolObjects_DatumModifWithValue_None) 
1872           aDatObj->SetModifierWithValue(aXCAFModifWithVal, aModifValue);
1873         aDGTTool->SetDatumToGeomTol(aDatL, theGDTL);
1874         Handle(StepDimTol_PlacedDatumTargetFeature) aPDTF = Handle(StepDimTol_PlacedDatumTargetFeature)::DownCast(aSA);
1875         if (aPDTF->TargetId()->IsIntegerValue())
1876           aDatObj->SetDatumTargetNumber(aPDTF->TargetId()->IntegerValue());
1877         else
1878           aDatObj->SetDatumTargetNumber(0);
1879         aDatObj->IsDatumTarget(Standard_True);
1880         XCAFDimTolObjects_DatumTargetType aType;
1881         if(STEPConstruct_GDTProperty::GetDatumTargetType(aSA->Description(),aType))
1882         {
1883           aDatObj->SetDatumTargetType(aType);
1884           if(aType == XCAFDimTolObjects_DatumTargetType_Area)
1885           {
1886             Interface_EntityIterator anIterDTF = aGraph.Shareds(aSA);
1887             for(anIterDTF.Start(); anIterDTF.More(); anIterDTF.Next()) {
1888               if(anIterDTF.Value()->IsKind(STANDARD_TYPE(StepAP242_GeometricItemSpecificUsage)))
1889               {
1890                 Handle(StepAP242_GeometricItemSpecificUsage) aGISU
1891                   = Handle(StepAP242_GeometricItemSpecificUsage)::DownCast(anIterDSWP.Value());
1892                 Handle(StepRepr_RepresentationItem) anItem;
1893                 if(aPGISU->NbIdentifiedItem() > 0) {
1894                   anItem = Handle(StepRepr_RepresentationItem)::DownCast(aPGISU->IdentifiedItemValue(1));
1895                 }
1896                 if(anItem.IsNull()) continue;
1897                 Standard_Integer anItemIndex = aTP->MapIndex(anItem);
1898                 if(anItemIndex >0) {
1899                   Handle(Transfer_Binder) binder = aTP->MapItem(anItemIndex);
1900                   TopoDS_Shape anItemShape = TransferBRep::ShapeResult(binder);
1901                   aDatObj->SetDatumTarget(anItemShape);
1902                 }
1903               }
1904             }
1905           }
1906           else
1907           {
1908             Interface_EntityIterator anIterDTF = aGraph.Shareds(aSA);
1909             for(anIterDTF.Start(); anIterDTF.More(); anIterDTF.Next()) {
1910               if(anIterDTF.Value()->IsKind(STANDARD_TYPE(StepRepr_PropertyDefinition)))
1911               {
1912                 Interface_EntityIterator anIterPD = aGraph.Shareds(anIterDTF.Value());
1913                 for(anIterPD.Start(); anIterPD.More(); anIterPD.Next()) {
1914                   if(anIterPD.Value()->IsKind(STANDARD_TYPE(StepShape_ShapeDefinitionRepresentation)))
1915                   {
1916                     Interface_EntityIterator anIterSDR = aGraph.Sharings(anIterPD.Value());
1917                     for(anIterSDR.Start(); anIterSDR.More(); anIterSDR.Next()) {
1918                       if(anIterSDR.Value()->IsKind(STANDARD_TYPE(StepShape_ShapeRepresentationWithParameters)))
1919                       {
1920                         Handle(StepShape_ShapeRepresentationWithParameters) aSRWP
1921                           = Handle(StepShape_ShapeRepresentationWithParameters)::DownCast(anIterSDR.Value());
1922                         for(Standard_Integer r = aSRWP->Items()->Lower(); r <= aSRWP->Items()->Upper(); r++)
1923                         {
1924                           if(aSRWP->ItemsValue(r)->IsKind(STANDARD_TYPE(StepGeom_Axis2Placement3d)))
1925                           {
1926                             Handle(StepGeom_Axis2Placement3d) anAx
1927                               = Handle(StepGeom_Axis2Placement3d)::DownCast(aSRWP->ItemsValue(r));
1928                             Handle(TColStd_HArray1OfReal) aDirArr = anAx->Axis()->DirectionRatios();
1929                             Handle(TColStd_HArray1OfReal) aDirRArr = anAx->RefDirection()->DirectionRatios();
1930                             Handle(TColStd_HArray1OfReal) aLocArr = anAx->Location()->Coordinates();
1931                             gp_Dir aDir;
1932                             gp_Dir aDirR;
1933                             gp_Pnt aPnt;
1934                             if(!aDirArr.IsNull() && aDirArr->Length() > 2 &&
1935                               !aDirRArr.IsNull() && aDirRArr->Length() > 2 && 
1936                               !aLocArr.IsNull() && aLocArr->Length() > 2)
1937                             {
1938                               aDir.SetCoord(aDirArr->Lower(), aDirArr->Lower()+1, aDirArr->Lower()+2);
1939                               aDirR.SetCoord(aDirRArr->Lower(), aDirRArr->Lower()+1, aDirRArr->Lower()+2);
1940                               aPnt.SetCoord(aLocArr->Lower(), aLocArr->Lower()+1, aLocArr->Lower()+2);
1941                               gp_Ax2 anA(aPnt, aDir, aDirR);
1942                               aDatObj->SetDatumTargetAxis(anA);
1943                             }
1944                           }
1945                           else if(aSRWP->ItemsValue(r)->IsKind(STANDARD_TYPE(StepRepr_ReprItemAndLengthMeasureWithUnit)))
1946                           {
1947                             Handle(StepRepr_ReprItemAndLengthMeasureWithUnit) aM =
1948                               Handle(StepRepr_ReprItemAndLengthMeasureWithUnit)::DownCast(aSRWP->ItemsValue(r)); 
1949                             Standard_Real aVal = aM->GetMeasureWithUnit()->ValueComponent();
1950                             StepBasic_Unit anUnit = aM->GetMeasureWithUnit()->UnitComponent();
1951                             Standard_Real aFact=1.;
1952                             if(anUnit.IsNull()) continue;
1953                             if( !(anUnit.CaseNum(anUnit.Value())==1) ) continue;
1954                             Handle(StepBasic_NamedUnit) NU = anUnit.NamedUnit();
1955                             if(GetLengthConversionFactor(NU,aFact)) aVal=aVal*aFact;
1956                             if(aM->Name()->String().IsEqual("target length") ||
1957                               aM->Name()->String().IsEqual("target diameter"))
1958                               aDatObj->SetDatumTargetLength(aVal);
1959                             else
1960                               aDatObj->SetDatumTargetWidth(aVal);
1961                           }
1962                         }
1963                       }
1964                     }
1965                   }
1966                 }
1967               }
1968             }
1969           }
1970         }
1971       }
1972     }
1973     else
1974     {
1975       //processing for darum feature
1976       TDF_Label aDatL = aDGTTool->AddDatum();
1977       aDat = XCAFDoc_Datum::Set(aDatL);
1978       aDGTTool->SetDatum(aShL, aDatL);
1979       aDatObj = aDat->GetObject();
1980       aDatObj->SetName(theDat->Identification());
1981       aDatObj->SetPosition (thePositionCounter);
1982       if(!aXCAFModifiers.IsEmpty())
1983         aDatObj->SetModifiers(aXCAFModifiers);
1984       if (aXCAFModifWithVal != XCAFDimTolObjects_DatumModifWithValue_None) 
1985         aDatObj->SetModifierWithValue(aXCAFModifWithVal, aModifValue);
1986       aDGTTool->SetDatumToGeomTol(aDatL, theGDTL);
1987     }
1988     if(!aDatObj.IsNull())
1989       aDat->SetObject(aDatObj);
1990   }
1991   return !aDat.IsNull();
1992 }
1993
1994
1995 //=======================================================================
1996 //function : ReadDatums
1997 //purpose  : auxilary
1998 //=======================================================================
1999 static Standard_Boolean readDatumsAP242(const Handle(Standard_Transient)& theEnt,
2000                                         const TDF_Label theGDTL,
2001                                         const Handle(TDocStd_Document)& theDoc,
2002                                         const Handle(XSControl_WorkSession)& theWS)
2003 {
2004   Handle(XCAFDoc_ShapeTool) aSTool = XCAFDoc_DocumentTool::ShapeTool( theDoc->Main() );
2005   Handle(XCAFDoc_DimTolTool) aDGTTool = XCAFDoc_DocumentTool::DimTolTool( theDoc->Main() );
2006   Handle(XSControl_TransferReader) aTR = theWS->TransferReader();
2007   Handle(Transfer_TransientProcess) aTP = aTR->TransientProcess();
2008   const Interface_Graph& aGraph = aTP->Graph();
2009
2010   Interface_EntityIterator anIter = aGraph.Shareds(theEnt);
2011   for(anIter.Start(); anIter.More(); anIter.Next()) {
2012     Handle(Standard_Transient) anAtr = anIter.Value();
2013     if(anAtr->IsKind(STANDARD_TYPE(StepDimTol_DatumSystem)))
2014     {
2015       Standard_Integer aPositionCounter = 0;//position on frame 
2016       Handle(StepDimTol_DatumSystem) aDS = Handle(StepDimTol_DatumSystem)::DownCast(anAtr);
2017       Interface_EntityIterator anIterDS = aGraph.Sharings(aDS);
2018       for(anIterDS.Start(); anIterDS.More(); anIterDS.Next()) {
2019         Handle(Standard_Transient) anAtrDS = anIterDS.Value();
2020         if(anAtrDS->IsKind(STANDARD_TYPE(StepAP242_GeometricItemSpecificUsage)))
2021         {
2022           //get axis
2023           Handle(StepAP242_GeometricItemSpecificUsage)aAxGISUI
2024             = Handle(StepAP242_GeometricItemSpecificUsage)::DownCast(anAtrDS);
2025           if (aAxGISUI->IdentifiedItemValue(1)->IsKind(STANDARD_TYPE(StepGeom_Axis2Placement3d)))
2026           {
2027             Handle(StepGeom_Axis2Placement3d) anAx
2028               = Handle(StepGeom_Axis2Placement3d)::DownCast(aAxGISUI->IdentifiedItemValue(1));
2029             Handle(XCAFDoc_GeomTolerance) aTol;
2030             if(theGDTL.FindAttribute(XCAFDoc_GeomTolerance::GetID(), aTol))
2031             {
2032               Handle(XCAFDimTolObjects_GeomToleranceObject) anObj = aTol->GetObject();
2033               Handle(TColStd_HArray1OfReal) aDirArr = anAx->Axis()->DirectionRatios();
2034               Handle(TColStd_HArray1OfReal) aDirRArr = anAx->RefDirection()->DirectionRatios();
2035               Handle(TColStd_HArray1OfReal) aLocArr = anAx->Location()->Coordinates();
2036               gp_Dir aDir;
2037               gp_Dir aDirR;
2038               gp_Pnt aPnt;
2039               if(!aDirArr.IsNull() && aDirArr->Length() > 2 &&
2040                 !aDirRArr.IsNull() && aDirRArr->Length() > 2 && 
2041                 !aLocArr.IsNull() && aLocArr->Length() > 2)
2042               {
2043                 aDir.SetCoord(aDirArr->Lower(), aDirArr->Lower()+1, aDirArr->Lower()+2);
2044                 aDirR.SetCoord(aDirRArr->Lower(), aDirRArr->Lower()+1, aDirRArr->Lower()+2);
2045                 aPnt.SetCoord(aLocArr->Lower(), aLocArr->Lower()+1, aLocArr->Lower()+2);
2046                 gp_Ax2 anA(aPnt, aDir, aDirR);
2047                 anObj->SetAxis(anA);
2048                 aTol->SetObject(anObj);
2049               }
2050             }
2051           }
2052         }
2053       }
2054       if (aDS->NbConstituents() > 0)
2055       {
2056         //get datum feature and datum target from datum system
2057         Handle(StepDimTol_HArray1OfDatumReferenceCompartment) aDRCA = aDS->Constituents();
2058         if(!aDRCA.IsNull())
2059         {
2060           for(Standard_Integer i = aDRCA->Lower(); i <= aDRCA->Upper(); i++)
2061           {
2062             Handle(StepDimTol_DatumReferenceCompartment) aDRC
2063               = Handle(StepDimTol_DatumReferenceCompartment)::DownCast(aDRCA->Value(i));
2064             //gete modifiers
2065             Handle(StepDimTol_HArray1OfDatumReferenceModifier) aModif = aDRC->Modifiers();
2066             XCAFDimTolObjects_DatumModifiersSequence aXCAFModifiers;
2067             XCAFDimTolObjects_DatumModifWithValue aXCAFModifWithVal = XCAFDimTolObjects_DatumModifWithValue_None;
2068             Standard_Real aModifValue = 0;
2069             if(!aModif.IsNull())
2070             {
2071               for(Standard_Integer m = aModif->Lower(); m <= aModif->Upper(); m++)
2072               {
2073                 if(aModif->Value(m).CaseNumber() == 2)
2074                   aXCAFModifiers.Append(
2075                   (XCAFDimTolObjects_DatumSingleModif)aModif->Value(m).
2076                   SimpleDatumReferenceModifierMember()->Value());
2077                 else if (aModif->Value(m).CaseNumber() == 1)
2078                 {
2079                   aXCAFModifWithVal = (XCAFDimTolObjects_DatumModifWithValue)(aModif->Value(m).DatumReferenceModifierWithValue()->ModifierType() + 1);
2080                   Standard_Real aVal = aModif->Value(m).DatumReferenceModifierWithValue()->ModifierValue()->ValueComponent();
2081                   StepBasic_Unit anUnit = aModif->Value(m).DatumReferenceModifierWithValue()->ModifierValue()->UnitComponent();
2082                   Standard_Real aFact=1.;
2083                   if(anUnit.IsNull()) continue;
2084                   if( !(anUnit.CaseNum(anUnit.Value())==1) ) continue;
2085                   Handle(StepBasic_NamedUnit) NU = anUnit.NamedUnit();
2086                   if(GetLengthConversionFactor(NU,aFact)) aVal=aVal*aFact;
2087                   aModifValue = aVal;
2088                 }
2089               }
2090             }
2091             aPositionCounter++;
2092             Interface_EntityIterator anIterDRC = aGraph.Shareds(aDRC);
2093             for(anIterDRC.Start(); anIterDRC.More(); anIterDRC.Next()) {
2094               
2095               if(anIterDRC.Value()->IsKind(STANDARD_TYPE(StepDimTol_Datum)))
2096               {
2097                 Handle(StepDimTol_Datum) aD = Handle(StepDimTol_Datum)::DownCast(anIterDRC.Value());
2098                 setDatumToXCAF(aD, theGDTL, aPositionCounter, aXCAFModifiers, aXCAFModifWithVal, aModifValue,theDoc, theWS);
2099               }
2100               else if(anIterDRC.Value()->IsKind(STANDARD_TYPE(StepDimTol_DatumReferenceElement)))
2101               {
2102                 Handle(StepDimTol_DatumReferenceElement) aDRE 
2103                   = Handle(StepDimTol_DatumReferenceElement)::DownCast(anIterDRC.Value());
2104                 //get modifiers from group of datums
2105                 Handle(StepDimTol_HArray1OfDatumReferenceModifier) aModifE = aDRE->Modifiers();
2106                 if(!aModifE.IsNull())
2107                 {
2108                   for(Standard_Integer k = aModifE->Lower(); k <= aModifE->Upper(); k++)
2109                   {
2110                     if(aModifE->Value(k).CaseNumber() == 1)
2111                       aXCAFModifiers.Append(
2112                       (XCAFDimTolObjects_DatumSingleModif)aModifE->Value(k).
2113                       SimpleDatumReferenceModifierMember()->Value());
2114                     else if (aModifE->Value(k).CaseNumber() == 2)
2115                     {
2116                       aXCAFModifWithVal = (XCAFDimTolObjects_DatumModifWithValue)(aModifE->Value(k).DatumReferenceModifierWithValue()->ModifierType() + 1);
2117                       Standard_Real aVal = aModifE->Value(k).DatumReferenceModifierWithValue()->ModifierValue()->ValueComponent();
2118                       StepBasic_Unit anUnit = aModifE->Value(k).DatumReferenceModifierWithValue()->ModifierValue()->UnitComponent();
2119                       Standard_Real aFact=1.;
2120                       if(anUnit.IsNull()) continue;
2121                       if( !(anUnit.CaseNum(anUnit.Value())==1) ) continue;
2122                       Handle(StepBasic_NamedUnit) NU = anUnit.NamedUnit();
2123                       if(GetLengthConversionFactor(NU,aFact)) aVal=aVal*aFact;
2124                       aModifValue = aVal;
2125                     }
2126                   }
2127                 }
2128                 Interface_EntityIterator anIterDRE = aGraph.Shareds(aDRE);
2129                 for(anIterDRE.Start(); anIterDRE.More(); anIterDRE.Next()) {
2130                   if(anIterDRE.Value()->IsKind(STANDARD_TYPE(StepDimTol_Datum)))
2131                   {
2132                     Handle(StepDimTol_Datum) aD = Handle(StepDimTol_Datum)::DownCast(anIterDRE.Value());
2133                     setDatumToXCAF(aD, theGDTL, aPositionCounter, aXCAFModifiers, aXCAFModifWithVal, aModifValue,theDoc, theWS);
2134                   }
2135                 }
2136               }
2137             }
2138           }
2139         }
2140       }
2141     }
2142   }
2143   return Standard_True;
2144 }
2145
2146 //=======================================================================
2147 //function : craeteGeomTolObjectInXCAF
2148 //purpose  : 
2149 //=======================================================================
2150 static TDF_Label createGDTObjectInXCAF(const Handle(Standard_Transient)& theEnt,
2151                                                const Handle(TDocStd_Document)& theDoc,
2152                                                const Handle(XSControl_WorkSession)& theWS)
2153 {
2154   TDF_Label aGDTL;
2155   if(!theEnt->IsKind(STANDARD_TYPE(StepShape_DimensionalSize)) &&
2156      !theEnt->IsKind(STANDARD_TYPE(StepShape_DimensionalLocation)) && 
2157      !theEnt->IsKind(STANDARD_TYPE(StepDimTol_GeometricTolerance)))
2158   {
2159     return aGDTL;
2160   }
2161
2162   Handle(XCAFDoc_ShapeTool) aSTool = XCAFDoc_DocumentTool::ShapeTool( theDoc->Main() );
2163   Handle(XCAFDoc_DimTolTool) aDGTTool = XCAFDoc_DocumentTool::DimTolTool( theDoc->Main() );
2164   Handle(XSControl_TransferReader) aTR = theWS->TransferReader();
2165   Handle(Transfer_TransientProcess) aTP = aTR->TransientProcess();
2166   const Interface_Graph& aGraph = aTP->Graph();
2167   Standard_Boolean isAllAround = Standard_False;
2168   Standard_Boolean isAllOver = Standard_False;
2169   
2170   // find RepresentationItem for current Ent
2171   NCollection_Sequence<Handle(Standard_Transient)> aSeqRI1, aSeqRI2;
2172
2173   Interface_EntityIterator anIter = aGraph.Shareds(theEnt);
2174   for(anIter.Start(); anIter.More(); anIter.Next()) {
2175     Handle(Standard_Transient) anAtr = anIter.Value();
2176     NCollection_Sequence<Handle(StepRepr_ShapeAspect)> aSAs;
2177     if(anAtr->IsKind(STANDARD_TYPE(StepRepr_ProductDefinitionShape)))
2178     {
2179       //if associating tolerances with part (All-Over)
2180       Interface_EntityIterator anIterSDR = aGraph.Sharings(anAtr);
2181       for(anIterSDR.Start(); anIterSDR.More(); anIterSDR.Next()) 
2182       {
2183         Handle(Standard_Transient) anAtrSDR = anIterSDR.Value();
2184         if(anAtrSDR->IsKind(STANDARD_TYPE(StepShape_ShapeDefinitionRepresentation)))
2185         {
2186           isAllOver = Standard_True;
2187           Interface_EntityIterator anIterABSR = aGraph.Shareds(anAtrSDR);
2188           for(anIterABSR.Start(); anIterABSR.More(); anIterABSR.Next()) 
2189           {
2190             Handle(Standard_Transient) anAtrABSR = anIterABSR.Value();
2191             if(anAtrABSR->IsKind(STANDARD_TYPE(StepShape_AdvancedBrepShapeRepresentation)))
2192             {
2193               aSeqRI1.Append(anAtrABSR);
2194             }
2195           }
2196         }
2197       }
2198     }
2199     else if(anAtr->IsKind(STANDARD_TYPE(StepShape_DimensionalLocation)) || 
2200       anAtr->IsKind(STANDARD_TYPE(StepShape_DimensionalSize)))
2201     {
2202       //if tolerance attached to dimension
2203       Interface_EntityIterator anIterDim = aGraph.Shareds(anAtr);
2204       for(anIterDim.Start(); anIterDim.More(); anIterDim.Next()) 
2205       {
2206         Handle(Standard_Transient) anAtrDim = anIterDim.Value();
2207         if(anAtrDim->IsKind(STANDARD_TYPE(StepRepr_CompositeShapeAspect)) || 
2208           anAtrDim->IsKind(STANDARD_TYPE(StepRepr_CompShAspAndDatumFeatAndShAsp)))
2209         {
2210           Interface_EntityIterator anIterCSA = aGraph.Sharings(anAtrDim);
2211           for(anIterCSA.Start(); anIterCSA.More(); anIterCSA.Next()) {
2212             if (anIterCSA.Value()->IsKind(STANDARD_TYPE(StepRepr_ShapeAspectRelationship))){
2213               Interface_EntityIterator anIterSAR = aGraph.Shareds(anIterCSA.Value());
2214               for(anIterSAR.Start(); anIterSAR.More(); anIterSAR.Next()) {
2215                 if (anIterSAR.Value()->IsKind(STANDARD_TYPE(StepRepr_ShapeAspect))){
2216                   aSAs.Append(Handle(StepRepr_ShapeAspect)::DownCast(anIterSAR.Value()));
2217                 }
2218               }
2219             }
2220           }
2221         }
2222         else if(anAtrDim->IsKind(STANDARD_TYPE(StepRepr_ShapeAspect)))
2223         {
2224           aSAs.Append( Handle(StepRepr_ShapeAspect)::DownCast(anAtrDim));
2225         }
2226       }
2227     }
2228     else if(anAtr->IsKind(STANDARD_TYPE(StepRepr_CompositeShapeAspect)) || 
2229       anAtr->IsKind(STANDARD_TYPE(StepRepr_CompShAspAndDatumFeatAndShAsp)))
2230     {
2231       //processing for composite entity
2232       Handle(Standard_Transient) anAtrTmp = anAtr;
2233       if(anAtr->IsKind(STANDARD_TYPE(StepRepr_AllAroundShapeAspect)))
2234       {
2235         // if applyed AllAround Modifier
2236         isAllAround = Standard_True;
2237         Interface_EntityIterator anIterAASA = aGraph.Sharings(anAtrTmp);
2238         for(anIterAASA.Start(); anIterAASA.More(); anIterAASA.Next()) {
2239           if (anIterAASA.Value()->IsKind(STANDARD_TYPE(StepRepr_ShapeAspectRelationship))){
2240             Interface_EntityIterator anIterSAR = aGraph.Shareds(anIterAASA.Value());
2241             for(anIterSAR.Start(); anIterSAR.More(); anIterSAR.Next()) {
2242               if ((anIterSAR.Value()->IsKind(STANDARD_TYPE(StepRepr_CompositeShapeAspect)) || 
2243                 anIterSAR.Value()->IsKind(STANDARD_TYPE(StepRepr_CompShAspAndDatumFeatAndShAsp))) &&
2244                 anAtrTmp != anIterSAR.Value()){
2245                   anAtrTmp = anIterSAR.Value();
2246                   break;
2247               }
2248             }
2249           }
2250         }
2251       }
2252       Interface_EntityIterator anIterCSA = aGraph.Sharings(anAtrTmp);
2253       for(anIterCSA.Start(); anIterCSA.More(); anIterCSA.Next()) {
2254         if (anIterCSA.Value()->IsKind(STANDARD_TYPE(StepRepr_ShapeAspectRelationship))){
2255           Interface_EntityIterator anIterSAR = aGraph.Shareds(anIterCSA.Value());
2256           for(anIterSAR.Start(); anIterSAR.More(); anIterSAR.Next()) {
2257             if (anIterSAR.Value()->IsKind(STANDARD_TYPE(StepRepr_ShapeAspect))){
2258               aSAs.Append(Handle(StepRepr_ShapeAspect)::DownCast(anIterSAR.Value()));
2259             }
2260           }
2261         }
2262       }
2263     }
2264     else if(anAtr->IsKind(STANDARD_TYPE(StepRepr_ShapeAspect)))
2265     {
2266       //default
2267       aSAs.Append( Handle(StepRepr_ShapeAspect)::DownCast(anAtr));
2268     }
2269     if(!aSAs.IsEmpty())
2270     {
2271       //get representation items
2272       NCollection_Sequence<Handle(Standard_Transient)> aSeqRI;
2273       for(Standard_Integer i = aSAs.Lower(); i <= aSAs.Upper(); i++)
2274       {
2275         Interface_EntityIterator anIterSA = aGraph.Sharings(aSAs.Value(i));
2276         Handle(StepAP242_GeometricItemSpecificUsage) aGISU;
2277         Handle(StepRepr_PropertyDefinition) PropD;
2278         for(anIterSA.Start(); anIterSA.More() && aGISU.IsNull() && PropD.IsNull(); anIterSA.Next()) {
2279           aGISU = Handle(StepAP242_GeometricItemSpecificUsage)::DownCast(anIterSA.Value());
2280           PropD = Handle(StepRepr_PropertyDefinition)::DownCast(anIterSA.Value());
2281         }
2282         if(!PropD.IsNull())//for old version
2283         {
2284           Handle(StepRepr_RepresentationItem) RI;
2285           Interface_EntityIterator subs4 = aGraph.Sharings(PropD);
2286           for(subs4.Start(); subs4.More(); subs4.Next()) {
2287             Handle(StepShape_ShapeDefinitionRepresentation) SDR = 
2288               Handle(StepShape_ShapeDefinitionRepresentation)::DownCast(subs4.Value());
2289             if(!SDR.IsNull()) {
2290               Handle(StepRepr_Representation) Repr = SDR->UsedRepresentation();
2291               if( !Repr.IsNull() && Repr->NbItems()>0 ) {
2292                 RI = Repr->ItemsValue(1);
2293               }
2294             }
2295           }
2296           if(RI.IsNull()) continue;
2297
2298           if(theEnt->IsKind(STANDARD_TYPE(StepShape_DimensionalSize))) {
2299             // read dimensions
2300             Handle(StepShape_EdgeCurve) EC = Handle(StepShape_EdgeCurve)::DownCast(RI);
2301             if(EC.IsNull()) continue;
2302             Handle(TCollection_HAsciiString) aName;
2303             Handle(StepShape_DimensionalSize) DimSize = 
2304               Handle(StepShape_DimensionalSize)::DownCast(theEnt);
2305             Standard_Real dim1=-1.,dim2=-1.;
2306             subs4 = aGraph.Sharings(DimSize);
2307             for(subs4.Start(); subs4.More(); subs4.Next()) {
2308               Handle(StepShape_DimensionalCharacteristicRepresentation) DimCharR = 
2309                 Handle(StepShape_DimensionalCharacteristicRepresentation)::DownCast(subs4.Value());
2310               if(!DimCharR.IsNull()) {
2311                 Handle(StepShape_ShapeDimensionRepresentation) SDimR = DimCharR->Representation();
2312                 if(!SDimR.IsNull() && SDimR->NbItems()>0) {
2313                   Handle(StepRepr_RepresentationItem) anItem = SDimR->ItemsValue(1);
2314                   Handle(StepRepr_ValueRange) VR = Handle(StepRepr_ValueRange)::DownCast(anItem);
2315                   if(!VR.IsNull()) {
2316                     aName = VR->Name();
2317                     //StepRepr_CompoundItemDefinition CID = VR->ItemElement();
2318                     //if(CID.IsNull()) continue;
2319                     //Handle(StepRepr_CompoundItemDefinitionMember) CIDM = 
2320                     //  Handle(StepRepr_CompoundItemDefinitionMember)::DownCast(CID.Value());
2321                     //if(CIDM.IsNull()) continue;
2322                     //if(CIDM->ArrTransient().IsNull()) continue;
2323                     //Handle(StepRepr_HArray1OfRepresentationItem) HARI;
2324                     //if(CID.CaseMem(CIDM)==1)
2325                     //  HARI = CID.ListRepresentationItem();
2326                     //if(CID.CaseMem(CIDM)==2)
2327                     //  HARI = CID.SetRepresentationItem();
2328                     Handle(StepRepr_HArray1OfRepresentationItem) HARI = VR->ItemElement();
2329                     if(HARI.IsNull()) continue;
2330                     if(HARI->Length()>0) {
2331                       Handle(StepRepr_RepresentationItem) RI1 =
2332                         Handle(StepRepr_RepresentationItem)::DownCast(HARI->Value(1));
2333                       if(RI1.IsNull()) continue;
2334                       if(RI1->IsKind(STANDARD_TYPE(StepRepr_ReprItemAndLengthMeasureWithUnit))) {
2335                         Handle(StepRepr_ReprItemAndLengthMeasureWithUnit) RILMWU =
2336                           Handle(StepRepr_ReprItemAndLengthMeasureWithUnit)::DownCast(RI1);
2337                         dim1 = RILMWU->GetMeasureWithUnit()->ValueComponent();
2338                         StepBasic_Unit anUnit = RILMWU->GetMeasureWithUnit()->UnitComponent();
2339                         Standard_Real afact=1.;
2340                         if(anUnit.IsNull()) continue;
2341                         if( !(anUnit.CaseNum(anUnit.Value())==1) ) continue;
2342                         Handle(StepBasic_NamedUnit) NU = anUnit.NamedUnit();
2343                         if(GetLengthConversionFactor(NU,afact)) dim1=dim1*afact;
2344                       }
2345                     }
2346                     if(HARI->Length()>1) {
2347                       Handle(StepRepr_RepresentationItem) RI2 =
2348                         Handle(StepRepr_RepresentationItem)::DownCast(HARI->Value(2));
2349                       if(RI2.IsNull()) continue;
2350                       if(RI2->IsKind(STANDARD_TYPE(StepRepr_ReprItemAndLengthMeasureWithUnit))) {
2351                         Handle(StepRepr_ReprItemAndLengthMeasureWithUnit) RILMWU =
2352                           Handle(StepRepr_ReprItemAndLengthMeasureWithUnit)::DownCast(RI2);
2353                         dim2 = RILMWU->GetMeasureWithUnit()->ValueComponent();
2354                         StepBasic_Unit anUnit = RILMWU->GetMeasureWithUnit()->UnitComponent();
2355                         Standard_Real afact=1.;
2356                         if(anUnit.IsNull()) continue;
2357                         if( !(anUnit.CaseNum(anUnit.Value())==1) ) continue;
2358                         Handle(StepBasic_NamedUnit) NU = anUnit.NamedUnit();
2359                         if(GetLengthConversionFactor(NU,afact)) dim2 = dim2*afact;
2360                       }
2361                     }
2362                   }
2363                 }
2364               }
2365             }
2366             if(dim1<0) continue;
2367             if(dim2<0) dim2=dim1;
2368             //cout<<"DimensionalSize: dim1="<<dim1<<"  dim2="<<dim2<<endl;
2369             // now we know edge_curve and value range therefore
2370             // we can create corresponding D&GT labels
2371             Standard_Integer index = aTP->MapIndex(EC);
2372             TopoDS_Shape aSh;
2373             if(index >0) {
2374               Handle(Transfer_Binder) binder = aTP->MapItem(index);
2375               aSh = TransferBRep::ShapeResult(binder);
2376             }
2377             if(aSh.IsNull()) continue; 
2378             TDF_Label shL;
2379             if( !aSTool->Search(aSh, shL, Standard_True, Standard_True, Standard_True) ) continue;
2380             Handle(TColStd_HArray1OfReal) arr = new TColStd_HArray1OfReal(1,2);
2381             arr->SetValue(1,dim1);
2382             arr->SetValue(2,dim2);
2383             aDGTTool->SetDimTol(shL,1,arr,aName,DimSize->Name());
2384           }
2385           // read tolerances and datums
2386           else if(theEnt->IsKind(STANDARD_TYPE(StepDimTol_GeometricTolerance))) {
2387             Handle(StepDimTol_GeometricTolerance) GT =
2388               Handle(StepDimTol_GeometricTolerance)::DownCast(theEnt);
2389             // read common data for tolerance
2390             //Standard_Real dim = GT->Magnitude()->ValueComponent();
2391             Handle (StepBasic_MeasureWithUnit) dim3 = GT->Magnitude();
2392             if(dim3.IsNull()) continue;
2393             Standard_Real dim = dim3->ValueComponent();
2394             StepBasic_Unit anUnit = GT->Magnitude()->UnitComponent();
2395             Standard_Real afact=1.;
2396             if(anUnit.IsNull()) continue;
2397             if( !(anUnit.CaseNum(anUnit.Value())==1) ) continue;
2398             Handle(StepBasic_NamedUnit) NU = anUnit.NamedUnit();
2399             if(GetLengthConversionFactor(NU,afact)) dim = dim*afact;
2400             //cout<<"GeometricTolerance: Magnitude = "<<dim<<endl;
2401             Handle(TColStd_HArray1OfReal) arr = new TColStd_HArray1OfReal(1,1);
2402             arr->SetValue(1,dim);
2403             Handle(TCollection_HAsciiString) aName = GT->Name();
2404             Handle(TCollection_HAsciiString) aDescription = GT->Description();
2405             Handle(StepShape_AdvancedFace) AF = Handle(StepShape_AdvancedFace)::DownCast(RI);
2406             if(AF.IsNull()) continue;
2407             Standard_Integer index = aTP->MapIndex(AF);
2408             TopoDS_Shape aSh;
2409             if(index >0) {
2410               Handle(Transfer_Binder) binder = aTP->MapItem(index);
2411               aSh = TransferBRep::ShapeResult(binder);
2412             }
2413             if(aSh.IsNull()) continue; 
2414             TDF_Label shL;
2415             if( !aSTool->Search(aSh, shL, Standard_True, Standard_True, Standard_True) ) continue;
2416             // read specific data for tolerance
2417             if(GT->IsKind(STANDARD_TYPE(StepDimTol_GeoTolAndGeoTolWthDatRefAndModGeoTolAndPosTol))) {
2418               Handle(StepDimTol_GeoTolAndGeoTolWthDatRefAndModGeoTolAndPosTol) GTComplex =
2419                 Handle(StepDimTol_GeoTolAndGeoTolWthDatRefAndModGeoTolAndPosTol)::DownCast(theEnt);
2420               Standard_Integer kind=20;
2421               Handle(StepDimTol_ModifiedGeometricTolerance) MGT = 
2422                 GTComplex->GetModifiedGeometricTolerance();
2423               if(!MGT.IsNull()) {
2424                 kind = kind + MGT->Modifier()+1;
2425               }
2426               TDF_Label TolerL = aDGTTool->SetDimTol(shL,kind,arr,aName,aDescription);
2427               // translate datums connected with this tolerance
2428               Handle(StepDimTol_GeometricToleranceWithDatumReference) GTWDR =
2429                 GTComplex->GetGeometricToleranceWithDatumReference();
2430               if(!GTWDR.IsNull()) {
2431                 ReadDatums(aSTool,aDGTTool,aGraph,aTP,TolerL,GTWDR);
2432               }
2433             }
2434             else if(GT->IsKind(STANDARD_TYPE(StepDimTol_GeometricToleranceWithDatumReference))) {
2435               Handle(StepDimTol_GeometricToleranceWithDatumReference) GTWDR =
2436                 Handle(StepDimTol_GeometricToleranceWithDatumReference)::DownCast(theEnt);
2437               if(GTWDR.IsNull()) continue;
2438               Standard_Integer kind = 0;
2439               if     (GTWDR->IsKind(STANDARD_TYPE(StepDimTol_AngularityTolerance)))       kind = 24;
2440               else if(GTWDR->IsKind(STANDARD_TYPE(StepDimTol_CircularRunoutTolerance)))   kind = 25;
2441               else if(GTWDR->IsKind(STANDARD_TYPE(StepDimTol_CoaxialityTolerance)))       kind = 26;
2442               else if(GTWDR->IsKind(STANDARD_TYPE(StepDimTol_ConcentricityTolerance)))    kind = 27;
2443               else if(GTWDR->IsKind(STANDARD_TYPE(StepDimTol_ParallelismTolerance)))      kind = 28;
2444               else if(GTWDR->IsKind(STANDARD_TYPE(StepDimTol_PerpendicularityTolerance))) kind = 29;
2445               else if(GTWDR->IsKind(STANDARD_TYPE(StepDimTol_SymmetryTolerance)))         kind = 30;
2446               else if(GTWDR->IsKind(STANDARD_TYPE(StepDimTol_TotalRunoutTolerance)))      kind = 31;
2447               //cout<<"GTWDR: kind="<<kind<<endl;
2448               TDF_Label TolerL = aDGTTool->SetDimTol(shL,kind,arr,aName,aDescription);
2449               ReadDatums(aSTool,aDGTTool,aGraph,aTP,TolerL,GTWDR);
2450             }
2451             else if(GT->IsKind(STANDARD_TYPE(StepDimTol_ModifiedGeometricTolerance))) {
2452               Handle(StepDimTol_ModifiedGeometricTolerance) MGT =
2453                 Handle(StepDimTol_ModifiedGeometricTolerance)::DownCast(theEnt);
2454               Standard_Integer kind = 35 + MGT->Modifier();
2455               aDGTTool->SetDimTol(shL,kind,arr,aName,aDescription);
2456             }
2457             else if(GT->IsKind(STANDARD_TYPE(StepDimTol_CylindricityTolerance))) {
2458               aDGTTool->SetDimTol(shL,38,arr,aName,aDescription);
2459             }
2460             else if(GT->IsKind(STANDARD_TYPE(StepDimTol_FlatnessTolerance))) {
2461               aDGTTool->SetDimTol(shL,39,arr,aName,aDescription);
2462             }
2463             else if(GT->IsKind(STANDARD_TYPE(StepDimTol_LineProfileTolerance))) {
2464               aDGTTool->SetDimTol(shL,40,arr,aName,aDescription);
2465             }
2466             else if(GT->IsKind(STANDARD_TYPE(StepDimTol_PositionTolerance))) {
2467               aDGTTool->SetDimTol(shL,41,arr,aName,aDescription);
2468             }
2469             else if(GT->IsKind(STANDARD_TYPE(StepDimTol_RoundnessTolerance))) {
2470               aDGTTool->SetDimTol(shL,42,arr,aName,aDescription);
2471             }
2472             else if(GT->IsKind(STANDARD_TYPE(StepDimTol_StraightnessTolerance))) {
2473               aDGTTool->SetDimTol(shL,43,arr,aName,aDescription);
2474             }
2475             else if(GT->IsKind(STANDARD_TYPE(StepDimTol_SurfaceProfileTolerance))) {
2476               aDGTTool->SetDimTol(shL,44,arr,aName,aDescription);
2477             }
2478           }
2479         }
2480         else
2481         {
2482           if(aGISU.IsNull()) continue;
2483           if(aGISU->NbIdentifiedItem() > 0) {
2484             aSeqRI.Append(aGISU->IdentifiedItemValue(1));
2485           }
2486         }
2487       }
2488       if(!aSeqRI.IsEmpty())
2489       {
2490         if(aSeqRI1.IsEmpty())
2491           aSeqRI1 = aSeqRI;
2492         else 
2493           aSeqRI2 = aSeqRI;
2494       }
2495     }
2496   }
2497   if(aSeqRI1.IsEmpty()) return aGDTL;
2498
2499   TDF_LabelSequence aShLS1, aShLS2;
2500
2501   for(Standard_Integer i = aSeqRI1.Lower(); i <= aSeqRI1.Upper() ;i++)
2502   {
2503     Standard_Integer anIndex = aTP->MapIndex(aSeqRI1.Value(i));
2504     TopoDS_Shape aSh;
2505     if(anIndex >0) {
2506       Handle(Transfer_Binder) aBinder = aTP->MapItem(anIndex);
2507       aSh = TransferBRep::ShapeResult(aBinder);
2508     }
2509     if(!aSh.IsNull())
2510     {
2511       TDF_Label aShL;
2512       aSTool->Search(aSh, aShL, Standard_True, Standard_True, Standard_True);
2513       aShLS1.Append(aShL);
2514     }
2515   }
2516   if (!aSeqRI2.IsEmpty())
2517   {
2518     //for dimensional location
2519     for(Standard_Integer i = aSeqRI2.Lower(); i <= aSeqRI2.Upper() ;i++)
2520     {
2521       Standard_Integer anIndex = aTP->MapIndex(aSeqRI2.Value(i));
2522       TopoDS_Shape aSh;
2523       if(anIndex >0) {
2524         Handle(Transfer_Binder) aBinder = aTP->MapItem(anIndex);
2525         aSh = TransferBRep::ShapeResult(aBinder);
2526       }
2527       if(!aSh.IsNull())
2528       {
2529         TDF_Label aShL;
2530         aSTool->Search(aSh, aShL, Standard_True, Standard_True, Standard_True);
2531         aShLS2.Append(aShL);
2532       }
2533     }
2534   }
2535
2536   if(!aShLS1.IsEmpty())
2537   {
2538     // add to XCAF
2539     if(!theEnt->IsKind(STANDARD_TYPE(StepDimTol_GeometricTolerance)))
2540     {
2541       aGDTL = aDGTTool->AddDimension();
2542       Handle(XCAFDoc_Dimension) aDim = XCAFDoc_Dimension::Set(aGDTL);
2543       TCollection_AsciiString aStr("DGT:Dimensional_");
2544       if(theEnt->IsKind(STANDARD_TYPE(StepShape_DimensionalSize)))
2545       {
2546         aStr.AssignCat("Size");
2547       }
2548       else if(theEnt->IsKind(STANDARD_TYPE(StepShape_DimensionalLocation)))
2549       {
2550         aStr.AssignCat("Location");
2551       }
2552       TDataStd_Name::Set(aGDTL, aStr);
2553
2554       if(!aShLS2.IsEmpty())
2555       {
2556         aDGTTool->SetDimension(aShLS1, aShLS2, aGDTL);
2557       }
2558       else
2559       {
2560         TDF_LabelSequence aEmptySeq;
2561         aDGTTool->SetDimension(aShLS1, aEmptySeq, aGDTL);
2562       }
2563     }
2564     else
2565     {
2566       aGDTL = aDGTTool->AddGeomTolerance();
2567       Handle(XCAFDoc_GeomTolerance) aGTol = XCAFDoc_GeomTolerance::Set(aGDTL);
2568       TCollection_AsciiString aStr("DGT:GeomTolerance");
2569       TDataStd_Name::Set(aGDTL, aStr);
2570       aDGTTool->SetGeomTolerance(aShLS1, aGDTL);
2571       Handle(XCAFDimTolObjects_GeomToleranceObject) anObj = aGTol->GetObject();
2572       if(isAllAround)
2573         anObj->AddModifier(XCAFDimTolObjects_GeomToleranceModif_All_Around);
2574       else if(isAllOver)
2575         anObj->AddModifier(XCAFDimTolObjects_GeomToleranceModif_All_Over);
2576       aGTol->SetObject(anObj);
2577     }
2578     readDatumsAP242(theEnt, aGDTL, theDoc, theWS);
2579   }
2580   return aGDTL;
2581 }
2582
2583 //=======================================================================
2584 //function : setDimObjectToXCAF
2585 //purpose  : 
2586 //=======================================================================
2587 static void setDimObjectToXCAF(const Handle(Standard_Transient)& theEnt,
2588                                            const TDF_Label& aDimL,
2589                                            const Handle(TDocStd_Document)& theDoc,
2590                                            const Handle(XSControl_WorkSession)& theWS)
2591 {
2592   Handle(XCAFDoc_ShapeTool) aSTool = XCAFDoc_DocumentTool::ShapeTool( theDoc->Main() );
2593   Handle(XCAFDoc_DimTolTool) aDGTTool = XCAFDoc_DocumentTool::DimTolTool( theDoc->Main() );
2594   Handle(XSControl_TransferReader) aTR = theWS->TransferReader();
2595   Handle(Transfer_TransientProcess) aTP = aTR->TransientProcess();
2596   const Interface_Graph& aGraph = aTP->Graph();
2597   Handle(XCAFDimTolObjects_DimensionObject) aDimObj;
2598   if(!theEnt->IsKind(STANDARD_TYPE(StepShape_DimensionalSize)) &&
2599     !theEnt->IsKind(STANDARD_TYPE(StepShape_DimensionalLocation))) 
2600     return;
2601
2602   Handle(StepShape_DimensionalSize) aDimSize = 
2603     Handle(StepShape_DimensionalSize)::DownCast(theEnt);
2604   Handle(StepShape_DimensionalLocation) aDimLocation = 
2605     Handle(StepShape_DimensionalLocation)::DownCast(theEnt);
2606
2607   Standard_Real aDim1=-1.,aDim2=-1.,aDim3=-1.;
2608   Handle(StepShape_TypeQualifier) aTQ;
2609   Handle(StepShape_ValueFormatTypeQualifier) aVFTQ;
2610   Handle(StepShape_ToleranceValue) aTV;
2611   Handle(StepShape_LimitsAndFits) aLAF;
2612   Handle(StepRepr_CompoundRepresentationItem) aCRI;
2613   Handle(StepGeom_Axis2Placement3d) anAP;
2614
2615   Interface_EntityIterator anIterDim;
2616   if(!aDimSize.IsNull())
2617   {
2618     anIterDim = aGraph.Sharings(aDimSize);
2619   }
2620   else
2621   {
2622     anIterDim = aGraph.Sharings(aDimLocation);
2623   }
2624   for(anIterDim.Start(); anIterDim.More(); anIterDim.Next()) {
2625     Handle(StepShape_DimensionalCharacteristicRepresentation) aDCR = 
2626       Handle(StepShape_DimensionalCharacteristicRepresentation)::DownCast(anIterDim.Value());
2627     Handle(StepShape_PlusMinusTolerance) aPMT = 
2628       Handle(StepShape_PlusMinusTolerance)::DownCast(anIterDim.Value());
2629     if(!aDCR.IsNull()) {
2630       Handle(StepShape_ShapeDimensionRepresentation) aSDR = aDCR->Representation();
2631       if(!aSDR.IsNull()) {
2632         Handle(StepRepr_HArray1OfRepresentationItem) aHARI = aSDR->Items();
2633
2634         if(!aHARI.IsNull())
2635         {
2636           for(Standard_Integer nr = aHARI->Lower(); nr <= aHARI->Upper(); nr++)
2637           {
2638             Handle(StepRepr_RepresentationItem) aDRI =
2639               Handle(StepRepr_RepresentationItem)::DownCast(aHARI->Value(nr));
2640             if(aDRI.IsNull()) continue;
2641
2642             if(aDRI->IsKind(STANDARD_TYPE(StepRepr_ReprItemAndLengthMeasureWithUnit))) {
2643               //get values
2644               Handle(StepRepr_ReprItemAndLengthMeasureWithUnit) aRILMWU =
2645                 Handle(StepRepr_ReprItemAndLengthMeasureWithUnit)::DownCast(aDRI);
2646               Standard_Real aVal = aRILMWU->GetMeasureWithUnit()->ValueComponent();
2647               StepBasic_Unit anUnit = aRILMWU->GetMeasureWithUnit()->UnitComponent();
2648               Standard_Real aFact=1.;
2649               if(anUnit.IsNull()) continue;
2650               if( !(anUnit.CaseNum(anUnit.Value())==1) ) continue;
2651               Handle(StepBasic_NamedUnit) NU = anUnit.NamedUnit();
2652               if(GetLengthConversionFactor(NU,aFact)) aVal=aVal*aFact;
2653               if(aDim2 > 0)
2654               {
2655                 if(aDim1 > aDim2)
2656                 {
2657                   Standard_Real aTmp = aDim1;
2658                   aDim1 = aDim2;
2659                   aDim2 = aTmp;
2660                 }
2661                 if(aVal < aDim1)
2662                   aDim1 = aVal;
2663                 else if(aVal > aDim2)
2664                   aDim2 = aVal;
2665               }
2666               else if(aDim1 > 0)
2667               {
2668                 if (aVal > aDim1)
2669                   aDim2 = aVal;
2670                 else
2671                 {
2672                   aDim2 = aDim1;
2673                   aDim1 = aVal;
2674                 }
2675               }
2676               else
2677                 aDim1 = aVal;
2678             }
2679             else if(aDRI->IsKind(STANDARD_TYPE(StepRepr_ReprItemAndPlaneAngleMeasureWithUnit))) {
2680               //get values
2681               Handle(StepRepr_ReprItemAndPlaneAngleMeasureWithUnit) aRIPAMWU =
2682                 Handle(StepRepr_ReprItemAndPlaneAngleMeasureWithUnit)::DownCast(aDRI);
2683               Standard_Real aVal = aRIPAMWU->GetMeasureWithUnit()->ValueComponent();
2684               StepBasic_Unit anUnit = aRIPAMWU->GetMeasureWithUnit()->UnitComponent();
2685               Standard_Real aFact=1.;
2686               if(anUnit.IsNull()) continue;
2687               if( !(anUnit.CaseNum(anUnit.Value())==1) ) continue;
2688               Handle(StepBasic_NamedUnit) NU = anUnit.NamedUnit();
2689               if(GetAngleConversionFactor(NU,aFact)) aVal=aVal*aFact;
2690               if(aDim2 > 0)
2691               {
2692                 if(aDim1 > aDim2)
2693                 {
2694                   Standard_Real aTmp = aDim1;
2695                   aDim1 = aDim2;
2696                   aDim2 = aTmp;
2697                 }
2698                 if(aVal < aDim1)
2699                   aDim1 = aVal;
2700                 else if(aVal > aDim2)
2701                   aDim2 = aVal;
2702               }
2703               else if(aDim1 > 0)
2704               {
2705                 if (aVal > aDim1)
2706                   aDim2 = aVal;
2707                 else
2708                 {
2709                   aDim2 = aDim1;
2710                   aDim1 = aVal;
2711                 }
2712               }
2713               else
2714                 aDim1 = aVal;
2715             }
2716             else if(aDRI->IsKind(STANDARD_TYPE(StepShape_QualifiedRepresentationItem))) {
2717               //get qualifier
2718               Handle(StepShape_QualifiedRepresentationItem) aQRI =
2719                 Handle(StepShape_QualifiedRepresentationItem)::DownCast(aDRI);
2720               for(Standard_Integer l = 1; l <= aQRI->NbQualifiers(); l++)
2721               {
2722                 aTQ = aQRI->Qualifiers()->Value(l).TypeQualifier();
2723                 aVFTQ = aQRI->Qualifiers()->Value(l).ValueFormatTypeQualifier();
2724               }
2725             }
2726             else if(aDRI->IsKind(STANDARD_TYPE(StepRepr_CompoundRepresentationItem))) {
2727               aCRI = Handle(StepRepr_CompoundRepresentationItem)::DownCast(aDRI);
2728             }
2729             else if(aDRI->IsKind(STANDARD_TYPE(StepGeom_Axis2Placement3d)))
2730             {
2731               anAP = Handle(StepGeom_Axis2Placement3d)::DownCast(aDRI);
2732             }
2733           }
2734         }
2735       }
2736     }
2737     else if (!aPMT.IsNull())
2738     {
2739       StepShape_ToleranceMethodDefinition aTMD = aPMT->Range();
2740       if(aPMT.IsNull()) continue;
2741       if(aTMD.CaseNumber() == 1)
2742         //! 1 -> ToleranceValue from StepShape
2743         //! 2 -> LimitsAndFits from StepShape
2744       {
2745         //plus minus tolerance 
2746         aTV = aTMD.ToleranceValue();
2747         if (aTV.IsNull()) continue;
2748
2749         Standard_Real aVal = aTV->UpperBound()->ValueComponent();
2750         StepBasic_Unit anUnit = aTV->UpperBound()->UnitComponent();
2751         Standard_Real aFact=1.;
2752         if(anUnit.IsNull()) continue;
2753         if( !(anUnit.CaseNum(anUnit.Value())==1) ) continue;
2754         Handle(StepBasic_NamedUnit) NU = anUnit.NamedUnit();
2755         if(GetLengthConversionFactor(NU,aFact)) aVal=aVal*aFact;
2756         aDim3 = aVal;
2757
2758         aVal = aTV->LowerBound()->ValueComponent();
2759         anUnit = aTV->LowerBound()->UnitComponent();
2760         aFact=1.;
2761         if(anUnit.IsNull()) continue;
2762         if( !(anUnit.CaseNum(anUnit.Value())==1) ) continue;
2763         NU = anUnit.NamedUnit();
2764         if(GetLengthConversionFactor(NU,aFact)) aVal=aVal*aFact;
2765         aDim2 = Abs(aVal);
2766       }
2767       else
2768       {
2769         // calss of tolerance
2770         aLAF = aTMD.LimitsAndFits();
2771       }
2772     }
2773   }
2774
2775   if(aDim1<0) return;
2776
2777   aDimObj = new XCAFDimTolObjects_DimensionObject();
2778   if(aDim2 < 0)
2779     aDimObj->SetValue(aDim1);
2780   else if (aDim3 < 0)
2781   {
2782     Handle(TColStd_HArray1OfReal) anArr = new TColStd_HArray1OfReal(1,2);
2783     anArr->SetValue(1,aDim1);
2784     anArr->SetValue(2,aDim2);
2785     aDimObj->SetValues(anArr);
2786   }
2787   else
2788   {
2789     Handle(TColStd_HArray1OfReal) anArr = new TColStd_HArray1OfReal(1,3);
2790     anArr->SetValue(1,aDim1);
2791     anArr->SetValue(2,aDim2);
2792     anArr->SetValue(3,aDim3);
2793     aDimObj->SetValues(anArr);
2794   }
2795   if(!aTQ.IsNull()) 
2796   {
2797     XCAFDimTolObjects_DimensionQualifier aQ;
2798     if (STEPConstruct_GDTProperty::GetDimQualifierType(aTQ->Name(), aQ))
2799     {
2800         aDimObj->SetQualifier(aQ);
2801     }
2802   }
2803
2804   if (!aVFTQ.IsNull())
2805   {
2806     //A typical value would be ‘NR2 2.2’
2807     TCollection_HAsciiString aFormat = aVFTQ->FormatType();
2808     Standard_Integer i = aFormat.Location(1,' ',1,aFormat.Length());
2809     aFormat.SubString(i+1,i+1)->IntegerValue();
2810     aDimObj->SetNbOfDecimalPlaces(aFormat.SubString(i+1,i+1)->IntegerValue(),
2811       aFormat.SubString(i+3,i+3)->IntegerValue());
2812   }
2813
2814   if (!aLAF.IsNull())
2815   { 
2816     //get class of tolerance
2817     Standard_Boolean aHolle = Standard_False;
2818     XCAFDimTolObjects_DimensionFormVariance aFV = XCAFDimTolObjects_DimensionFormVariance_None;
2819     XCAFDimTolObjects_DimensionGrade aG = XCAFDimTolObjects_DimensionGrade_IT01;
2820     STEPConstruct_GDTProperty::GetDimClassOfTolerance(aLAF, aHolle, aFV, aG);
2821     aDimObj->SetClassOfTolerance(aHolle, aFV, aG);
2822   }
2823
2824   if (!aCRI.IsNull() && aCRI->ItemElement()->Length() > 0)
2825   {
2826     //get modifiers
2827     XCAFDimTolObjects_DimensionModifiersSequence aModifiers;
2828     STEPConstruct_GDTProperty::GetDimModifiers(aCRI, aModifiers);
2829     if(aModifiers.Length() > 0)
2830       aDimObj->SetModifiers(aModifiers);
2831   }
2832
2833   Handle(TCollection_HAsciiString) aName;
2834   if(!aDimSize.IsNull())
2835   {
2836     aName = aDimSize->Name();
2837   }
2838   else
2839   {
2840     aName = aDimLocation->Name();
2841   }
2842   XCAFDimTolObjects_DimensionType aType = XCAFDimTolObjects_DimensionType_Location_None;
2843   if (!STEPConstruct_GDTProperty::GetDimType(aName, aType))
2844   {
2845     if(!aDimSize.IsNull())
2846     {
2847       Handle(StepShape_AngularSize) anAngSize= 
2848         Handle(StepShape_AngularSize)::DownCast(aDimSize);
2849       if(!anAngSize.IsNull())
2850       {
2851         //get qualifier for angular value
2852         aType = XCAFDimTolObjects_DimensionType_Size_Angular;
2853         if(anAngSize->AngleSelection() == StepShape_Equal)
2854           aDimObj->SetQualifier(XCAFDimTolObjects_DimensionQualifier_Avg);
2855         else if (anAngSize->AngleSelection() == StepShape_Large)
2856           aDimObj->SetQualifier(XCAFDimTolObjects_DimensionQualifier_Max);
2857         else if (anAngSize->AngleSelection() == StepShape_Small)
2858           aDimObj->SetQualifier(XCAFDimTolObjects_DimensionQualifier_Min);
2859       }
2860     }
2861     else
2862     {
2863       Handle(StepShape_AngularLocation) anAngLoc= 
2864         Handle(StepShape_AngularLocation)::DownCast(aDimLocation);
2865       if(!anAngLoc.IsNull())
2866       {
2867         //get qualifier for angular value
2868         aType = XCAFDimTolObjects_DimensionType_Location_Angular;
2869         if(anAngLoc->AngleSelection() == StepShape_Equal)
2870           aDimObj->SetQualifier(XCAFDimTolObjects_DimensionQualifier_Avg);
2871         else if (anAngLoc->AngleSelection() == StepShape_Large)
2872           aDimObj->SetQualifier(XCAFDimTolObjects_DimensionQualifier_Max);
2873         else if (anAngLoc->AngleSelection() == StepShape_Small)
2874           aDimObj->SetQualifier(XCAFDimTolObjects_DimensionQualifier_Min);
2875       }
2876     }
2877     if(aType == XCAFDimTolObjects_DimensionType_Location_None)
2878     {
2879       Handle(StepRepr_ShapeAspect) aPSA;
2880       if(!aDimSize.IsNull())
2881       {
2882         Handle(StepShape_DimensionalSizeWithPath) aDimSizeWithPath= 
2883           Handle(StepShape_DimensionalSizeWithPath)::DownCast(aDimSize);
2884         if (!aDimSizeWithPath.IsNull())
2885         {
2886           aType = XCAFDimTolObjects_DimensionType_Size_WithPath;
2887           aPSA = aDimSizeWithPath->Path();
2888         }
2889       }
2890       else
2891       {
2892         Handle(StepShape_DimensionalLocationWithPath) aDimLocWithPath= 
2893           Handle(StepShape_DimensionalLocationWithPath)::DownCast(aDimLocation);
2894         if (!aDimLocWithPath.IsNull())
2895         {
2896           aType = XCAFDimTolObjects_DimensionType_Location_WithPath;
2897           aPSA = aDimLocWithPath->Path();
2898         }
2899       }
2900
2901       if(!aPSA.IsNull())
2902       {
2903         //for DimensionalLocationWithPath
2904         Handle(StepGeom_GeometricRepresentationItem) aGRI;
2905         Handle(StepAP242_GeometricItemSpecificUsage) aPGISU;
2906         Interface_EntityIterator anIterDSWP = aGraph.Sharings(aPSA);
2907         for(anIterDSWP.Start(); anIterDSWP.More() && aPGISU.IsNull(); anIterDSWP.Next()) {
2908           aPGISU = Handle(StepAP242_GeometricItemSpecificUsage)::DownCast(anIterDSWP.Value());
2909         }
2910         if(aPGISU.IsNull()) return;
2911         if(aPGISU->NbIdentifiedItem() > 0) {
2912           aGRI = Handle(StepGeom_GeometricRepresentationItem)::DownCast(aPGISU->IdentifiedItemValue(1));
2913         }
2914         if(aGRI.IsNull()) return;
2915         Handle(StepRepr_RepresentationItem) aPRI;
2916         Interface_EntityIterator anIterGRI = aGraph.Sharings(aGRI);
2917         for(anIterGRI.Start(); anIterGRI.More() && aPGISU.IsNull(); anIterGRI.Next()) {
2918           aPRI = Handle(StepRepr_RepresentationItem)::DownCast(anIterGRI.Value());
2919         }
2920         Standard_Integer anIndex = aTP->MapIndex(aPRI);
2921         TopoDS_Edge aSh;
2922         if(anIndex >0) {
2923           Handle(Transfer_Binder) aBinder = aTP->MapItem(anIndex);
2924           aSh = TopoDS::Edge(TransferBRep::ShapeResult(aBinder));
2925         }
2926         if(aSh.IsNull()) return; 
2927         aDimObj->SetPath(aSh);
2928       }
2929       else if(!anAP.IsNull())
2930       {
2931         if(anAP->Name()->String().IsEqual("orientation") && !anAP->Axis().IsNull())
2932         {
2933           //for Oriented Dimensional Location
2934           Handle(TColStd_HArray1OfReal) aDirArr = anAP->RefDirection()->DirectionRatios();
2935           gp_Dir aDir;
2936           if(!aDirArr.IsNull() && aDirArr->Length() > 2)
2937           {
2938             aDir.SetCoord(aDirArr->Lower(), aDirArr->Lower()+1, aDirArr->Lower()+2);
2939             aDimObj->SetDirection(aDir);
2940           }
2941           else if(aDirArr->Length() > 1)
2942           {
2943             aDir.SetCoord(aDirArr->Lower(), aDirArr->Lower()+1, 0);
2944             aDimObj->SetDirection(aDir);
2945           }
2946         }
2947       }
2948     }
2949   }
2950   aDimObj->SetType(aType);
2951
2952   if(!aDimObj.IsNull())
2953   {
2954     Handle(XCAFDoc_Dimension) aDim;
2955     if(aDimL.FindAttribute(XCAFDoc_Dimension::GetID(),aDim))
2956     {
2957       aDim->SetObject(aDimObj);
2958     }
2959   }
2960 }
2961
2962 //=======================================================================
2963 //function : getTolType
2964 //purpose  : 
2965 //=======================================================================
2966 static Standard_Boolean getTolType(const Handle(Standard_Transient)& theEnt,
2967                        XCAFDimTolObjects_GeomToleranceType& theType)
2968 {
2969   if(!theEnt->IsKind(STANDARD_TYPE(StepDimTol_GeometricTolerance))) 
2970     return Standard_False;
2971   theType = XCAFDimTolObjects_GeomToleranceType_None;
2972   if (theEnt->IsKind(STANDARD_TYPE(StepDimTol_GeoTolAndGeoTolWthDatRef)))
2973   {
2974     Handle(StepDimTol_GeoTolAndGeoTolWthDatRef) anE = Handle(StepDimTol_GeoTolAndGeoTolWthDatRef)::DownCast(theEnt);
2975     theType = STEPConstruct_GDTProperty::GetGeomToleranceType(anE->GetToleranceType());
2976   }
2977   else if (theEnt->IsKind(STANDARD_TYPE(StepDimTol_GeoTolAndGeoTolWthDatRefAndGeoTolWthMaxTol)))
2978   {
2979     Handle(StepDimTol_GeoTolAndGeoTolWthDatRefAndGeoTolWthMaxTol) anE = 
2980       Handle(StepDimTol_GeoTolAndGeoTolWthDatRefAndGeoTolWthMaxTol)::DownCast(theEnt);
2981     theType = STEPConstruct_GDTProperty::GetGeomToleranceType(anE->GetToleranceType());
2982   }
2983   else if (theEnt->IsKind(STANDARD_TYPE(StepDimTol_GeoTolAndGeoTolWthDatRefAndGeoTolWthMod)))
2984   {
2985     Handle(StepDimTol_GeoTolAndGeoTolWthDatRefAndGeoTolWthMod) anE = 
2986       Handle(StepDimTol_GeoTolAndGeoTolWthDatRefAndGeoTolWthMod)::DownCast(theEnt);
2987     theType = STEPConstruct_GDTProperty::GetGeomToleranceType(anE->GetToleranceType());
2988   }
2989   else if (theEnt->IsKind(STANDARD_TYPE(StepDimTol_GeoTolAndGeoTolWthMaxTol)))
2990   {
2991     Handle(StepDimTol_GeoTolAndGeoTolWthMaxTol) anE = 
2992       Handle(StepDimTol_GeoTolAndGeoTolWthMaxTol)::DownCast(theEnt);
2993     theType = STEPConstruct_GDTProperty::GetGeomToleranceType(anE->GetToleranceType());
2994   }
2995   else if (theEnt->IsKind(STANDARD_TYPE(StepDimTol_GeoTolAndGeoTolWthMod)))
2996   {
2997     Handle(StepDimTol_GeoTolAndGeoTolWthMod) anE = 
2998       Handle(StepDimTol_GeoTolAndGeoTolWthMod)::DownCast(theEnt);
2999     theType = STEPConstruct_GDTProperty::GetGeomToleranceType(anE->GetToleranceType());
3000   }
3001   else if (theEnt->IsKind(STANDARD_TYPE(StepDimTol_GeoTolAndGeoTolWthDatRefAndUneqDisGeoTol)))
3002   {
3003     Handle(StepDimTol_GeoTolAndGeoTolWthDatRefAndUneqDisGeoTol) anE = 
3004       Handle(StepDimTol_GeoTolAndGeoTolWthDatRefAndUneqDisGeoTol)::DownCast(theEnt);
3005     theType = STEPConstruct_GDTProperty::GetGeomToleranceType(anE->GetToleranceType());
3006   }
3007   else if(theEnt->IsKind(STANDARD_TYPE(StepDimTol_AngularityTolerance)))
3008   {
3009     theType = XCAFDimTolObjects_GeomToleranceType_Angularity;
3010   }
3011   else if(theEnt->IsKind(STANDARD_TYPE(StepDimTol_CircularRunoutTolerance)))
3012   {
3013     theType = XCAFDimTolObjects_GeomToleranceType_CircularRunout;
3014   }
3015   else if(theEnt->IsKind(STANDARD_TYPE(StepDimTol_RoundnessTolerance)))
3016   {
3017     theType = XCAFDimTolObjects_GeomToleranceType_CircularityOrRoundness;
3018   }
3019   else if(theEnt->IsKind(STANDARD_TYPE(StepDimTol_CoaxialityTolerance)))
3020   {
3021     theType = XCAFDimTolObjects_GeomToleranceType_Coaxiality;
3022   }
3023   else if(theEnt->IsKind(STANDARD_TYPE(StepDimTol_ConcentricityTolerance)))
3024   {
3025     theType = XCAFDimTolObjects_GeomToleranceType_Concentricity;
3026   }
3027   else if(theEnt->IsKind(STANDARD_TYPE(StepDimTol_CylindricityTolerance)))
3028   {
3029     theType = XCAFDimTolObjects_GeomToleranceType_Cylindricity;
3030   }
3031   else if(theEnt->IsKind(STANDARD_TYPE(StepDimTol_FlatnessTolerance)))
3032   {
3033     theType = XCAFDimTolObjects_GeomToleranceType_Flatness;
3034   }
3035   else if(theEnt->IsKind(STANDARD_TYPE(StepDimTol_ParallelismTolerance)))
3036   {
3037     theType = XCAFDimTolObjects_GeomToleranceType_Parallelism;
3038   }
3039   else if(theEnt->IsKind(STANDARD_TYPE(StepDimTol_PerpendicularityTolerance)))
3040   {
3041     theType = XCAFDimTolObjects_GeomToleranceType_Perpendicularity;
3042   }
3043   else if(theEnt->IsKind(STANDARD_TYPE(StepDimTol_PositionTolerance)) || 
3044      theEnt->IsKind(STANDARD_TYPE(StepDimTol_GeoTolAndGeoTolWthDatRefAndModGeoTolAndPosTol)))
3045   {
3046     theType = XCAFDimTolObjects_GeomToleranceType_Position;
3047   }
3048   else if(theEnt->IsKind(STANDARD_TYPE(StepDimTol_LineProfileTolerance)))
3049   {
3050     theType = XCAFDimTolObjects_GeomToleranceType_ProfileOfLine;
3051   }
3052   else if(theEnt->IsKind(STANDARD_TYPE(StepDimTol_SurfaceProfileTolerance)))
3053   {
3054     theType = XCAFDimTolObjects_GeomToleranceType_ProfileOfSurface;
3055   }
3056   else if(theEnt->IsKind(STANDARD_TYPE(StepDimTol_StraightnessTolerance)))
3057   {
3058     theType = XCAFDimTolObjects_GeomToleranceType_Straightness;
3059   }
3060   else if(theEnt->IsKind(STANDARD_TYPE(StepDimTol_SymmetryTolerance)))
3061   {
3062     theType = XCAFDimTolObjects_GeomToleranceType_Symmetry;
3063   }
3064   else if(theEnt->IsKind(STANDARD_TYPE(StepDimTol_TotalRunoutTolerance)))
3065   {
3066     theType = XCAFDimTolObjects_GeomToleranceType_TotalRunout;
3067   }
3068   return Standard_True;
3069 }
3070 //=======================================================================
3071 //function : setGeomTolObjectToXCAF
3072 //purpose  : 
3073 //=======================================================================
3074 static void setGeomTolObjectToXCAF(const Handle(Standard_Transient)& theEnt,
3075                                            const TDF_Label& theTolL,
3076                                            const Handle(TDocStd_Document)& theDoc,
3077                                            const Handle(XSControl_WorkSession)& theWS)
3078 {
3079   Handle(XCAFDoc_ShapeTool) aSTool = XCAFDoc_DocumentTool::ShapeTool( theDoc->Main() );
3080   Handle(XCAFDoc_DimTolTool) aDGTTool = XCAFDoc_DocumentTool::DimTolTool( theDoc->Main() );
3081   Handle(XSControl_TransferReader) aTR = theWS->TransferReader();
3082   Handle(Transfer_TransientProcess) aTP = aTR->TransientProcess();
3083   const Interface_Graph& aGraph = aTP->Graph();
3084   Handle(XCAFDoc_GeomTolerance) aGTol;
3085   if(!theTolL.FindAttribute(XCAFDoc_GeomTolerance::GetID(), aGTol))
3086   {
3087     return;
3088   }
3089   Handle(XCAFDimTolObjects_GeomToleranceObject) aTolObj = aGTol->GetObject();
3090   Handle(StepDimTol_GeometricTolerance) aTolEnt = Handle(StepDimTol_GeometricTolerance)::DownCast(theEnt);
3091
3092   XCAFDimTolObjects_GeomToleranceType aType = XCAFDimTolObjects_GeomToleranceType_None;
3093   getTolType(theEnt, aType);
3094   aTolObj->SetType(aType);
3095   if(!aTolEnt->Magnitude().IsNull()){
3096     //get value
3097     Standard_Real aVal = aTolEnt->Magnitude()->ValueComponent();
3098     StepBasic_Unit anUnit = aTolEnt->Magnitude()->UnitComponent();
3099     Standard_Real aFact=1.;
3100     if(anUnit.IsNull()) return;
3101     if( !(anUnit.CaseNum(anUnit.Value())==1) ) return;
3102     Handle(StepBasic_NamedUnit) NU = anUnit.NamedUnit();
3103     if(GetLengthConversionFactor(NU,aFact))
3104       aVal=aVal*aFact;
3105     aTolObj->SetValue(aVal);
3106   }
3107   //get modifiers
3108   XCAFDimTolObjects_GeomToleranceTypeValue aTypeV = XCAFDimTolObjects_GeomToleranceTypeValue_None;
3109   Interface_EntityIterator anIter = aGraph.Sharings(aTolEnt);
3110   for(anIter.Start(); anIter.More(); anIter.Next()) {
3111     if(anIter.Value()->IsKind(STANDARD_TYPE(StepDimTol_ToleranceZone))){
3112       Handle(StepDimTol_ToleranceZoneForm) aForm 
3113                             = Handle(StepDimTol_ToleranceZone)::DownCast(anIter.Value())->Form();
3114       STEPConstruct_GDTProperty::GetTolValueType(aForm->Name(), aTypeV);
3115       Interface_EntityIterator anIt = aGraph.Sharings(anIter.Value());
3116       for(anIt.Start(); anIt.More(); anIt.Next()) {
3117         if(anIt.Value()->IsKind(STANDARD_TYPE(StepDimTol_ProjectedZoneDefinition))){
3118           Handle(StepDimTol_ProjectedZoneDefinition) aPZone
3119                               = Handle(StepDimTol_ProjectedZoneDefinition)::DownCast(anIt.Value());
3120           if(!aPZone->ProjectionLength().IsNull())
3121           {
3122             Standard_Real aVal = aPZone->ProjectionLength()->ValueComponent();
3123             StepBasic_Unit anUnit = aPZone->ProjectionLength()->UnitComponent();
3124             Standard_Real aFact=1.;
3125             if(anUnit.IsNull()) return;
3126             if( !(anUnit.CaseNum(anUnit.Value())==1) ) return;
3127             Handle(StepBasic_NamedUnit) NU = anUnit.NamedUnit();
3128             if(GetLengthConversionFactor(NU,aFact))
3129               aVal=aVal*aFact;
3130             aTolObj->SetValueOfZoneModifier(aVal);
3131             aTolObj->SetZoneModifier(XCAFDimTolObjects_GeomToleranceZoneModif_Projected);
3132           }
3133         }
3134         else if (anIt.Value()->IsKind(STANDARD_TYPE(StepDimTol_RunoutZoneDefinition)))
3135         {
3136           Handle(StepDimTol_RunoutZoneDefinition) aRZone
3137                                 = Handle(StepDimTol_RunoutZoneDefinition)::DownCast(anIt.Value());
3138           if(!aRZone->Orientation().IsNull())
3139           {
3140             Standard_Real aVal = aRZone->Orientation()->Angle()->ValueComponent();
3141             StepBasic_Unit anUnit = aRZone->Orientation()->Angle()->UnitComponent();
3142             Standard_Real aFact=1.;
3143             if(anUnit.IsNull()) continue;
3144             if( !(anUnit.CaseNum(anUnit.Value())==1) ) continue;
3145             Handle(StepBasic_NamedUnit) NU = anUnit.NamedUnit();
3146             if(GetAngleConversionFactor(NU,aFact)) aVal=aVal*aFact;
3147             aTolObj->SetValueOfZoneModifier(aVal);
3148             aTolObj->SetZoneModifier(XCAFDimTolObjects_GeomToleranceZoneModif_Runout);
3149           }
3150         }
3151       }
3152       aTolObj->SetTypeOfValue(aTypeV);
3153     }
3154   }
3155   Handle(StepDimTol_HArray1OfGeometricToleranceModifier) aModifiers;
3156   if (aTolEnt->IsKind(STANDARD_TYPE(StepDimTol_GeometricToleranceWithModifiers)))
3157   {
3158     aModifiers = Handle(StepDimTol_GeometricToleranceWithModifiers)::DownCast(aTolEnt)->Modifiers();
3159   }
3160   else if (aTolEnt->IsKind(STANDARD_TYPE(StepDimTol_GeoTolAndGeoTolWthDatRefAndGeoTolWthMod)))
3161   {
3162     aModifiers = Handle(StepDimTol_GeoTolAndGeoTolWthDatRefAndGeoTolWthMod)
3163                             ::DownCast(aTolEnt)->GetGeometricToleranceWithModifiers()->Modifiers();
3164   }
3165   else if (aTolEnt->IsKind(STANDARD_TYPE(StepDimTol_GeoTolAndGeoTolWthMod)))
3166   {
3167     aModifiers = Handle(StepDimTol_GeoTolAndGeoTolWthMod)
3168                              ::DownCast(aTolEnt)->GetGeometricToleranceWithModifiers()->Modifiers();
3169   }
3170   else if (aTolEnt->IsKind(STANDARD_TYPE(StepDimTol_GeoTolAndGeoTolWthMaxTol)))
3171   {
3172     aModifiers = Handle(StepDimTol_GeoTolAndGeoTolWthMaxTol)
3173                              ::DownCast(aTolEnt)->GetGeometricToleranceWithModifiers()->Modifiers();
3174   }
3175   else if (aTolEnt->IsKind(STANDARD_TYPE(StepDimTol_GeoTolAndGeoTolWthDatRefAndGeoTolWthMaxTol)))
3176   {
3177     aModifiers = Handle(StepDimTol_GeoTolAndGeoTolWthDatRefAndGeoTolWthMaxTol)
3178                              ::DownCast(aTolEnt)->GetGeometricToleranceWithModifiers()->Modifiers();
3179   }
3180   if(!aModifiers.IsNull())
3181   {
3182     for(Standard_Integer i = aModifiers->Lower(); i <= aModifiers->Upper(); i++)
3183     {
3184       if(aModifiers->Value(i) == StepDimTol_GTMLeastMaterialRequirement)
3185         aTolObj->SetMaterialRequirementModifier(XCAFDimTolObjects_GeomToleranceMatReqModif_L);
3186       else if (aModifiers->Value(i) == StepDimTol_GTMMaximumMaterialRequirement)
3187         aTolObj->SetMaterialRequirementModifier(XCAFDimTolObjects_GeomToleranceMatReqModif_M);
3188       else
3189         aTolObj->AddModifier((XCAFDimTolObjects_GeomToleranceModif)aModifiers->Value(i));
3190     }
3191   }
3192   Standard_Real aVal = 0;
3193   StepBasic_Unit anUnit;
3194   if (aTolEnt->IsKind(STANDARD_TYPE(StepDimTol_GeometricToleranceWithMaximumTolerance)))
3195   {
3196     Handle(StepDimTol_GeometricToleranceWithMaximumTolerance) aMax = Handle(StepDimTol_GeometricToleranceWithMaximumTolerance)::DownCast(aTolEnt);
3197     aVal = aMax->MaximumUpperTolerance()->ValueComponent();
3198     anUnit = aMax->MaximumUpperTolerance()->UnitComponent();
3199   }
3200   else if (aTolEnt->IsKind(STANDARD_TYPE(StepDimTol_GeoTolAndGeoTolWthMaxTol)))
3201   {
3202     Handle(StepDimTol_GeoTolAndGeoTolWthMaxTol) aMax = 
3203       Handle(StepDimTol_GeoTolAndGeoTolWthMaxTol)::DownCast(aTolEnt);
3204     aVal = aMax->GetMaxTolerance()->ValueComponent();
3205     anUnit = aMax->GetMaxTolerance()->UnitComponent();
3206   }
3207   else if (aTolEnt->IsKind(STANDARD_TYPE(StepDimTol_GeoTolAndGeoTolWthDatRefAndGeoTolWthMaxTol)))
3208   {
3209     Handle(StepDimTol_GeoTolAndGeoTolWthDatRefAndGeoTolWthMaxTol) aMax = 
3210       Handle(StepDimTol_GeoTolAndGeoTolWthDatRefAndGeoTolWthMaxTol)::DownCast(aTolEnt);
3211     aVal = aMax->GetMaxTolerance()->ValueComponent();
3212     anUnit = aMax->GetMaxTolerance()->UnitComponent();
3213   }
3214   if (!anUnit.IsNull() && (anUnit.CaseNum(anUnit.Value()) == 1))
3215   {
3216     Standard_Real aFact=1.;
3217     Handle(StepBasic_NamedUnit) NU = anUnit.NamedUnit();
3218     if(GetAngleConversionFactor(NU,aFact)) aVal=aVal*aFact;
3219     aTolObj->SetMaxValueModifier(aVal);
3220   }
3221
3222   aGTol->SetObject(aTolObj);
3223 }
3224
3225 //=======================================================================
3226 //function : ReadGDTs
3227 //purpose  : 
3228 //=======================================================================
3229
3230 Standard_Boolean STEPCAFControl_Reader::ReadGDTs(const Handle(XSControl_WorkSession)& theWS,
3231                                                  Handle(TDocStd_Document)& theDoc) const
3232 {
3233   Handle(Interface_InterfaceModel) aModel = theWS->Model();
3234   Handle(StepData_StepModel) aSM = Handle(StepData_StepModel)::DownCast(aModel);
3235   Interface_EntityIterator anI = aSM->Header();
3236   Handle(HeaderSection_FileSchema) aH;
3237   for(anI.Start(); anI.More() && aH.IsNull();anI.Next())
3238     aH = Handle(HeaderSection_FileSchema)::DownCast(anI.Value());