0027104: DownCast() cannot return null for mismatched handle
[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 <STEPCAFControl_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 = subSHUO->NextUsage();
1419   if (NUNAUO.IsNull())
1420     return Standard_False;
1421 //   Handle(Interface_InterfaceModel) Model = WS->Model();
1422 //   Handle(XSControl_TransferReader) TR = WS->TransferReader();
1423 //   Handle(Transfer_TransientProcess) TP = TR->TransientProcess();
1424 //   Handle(Transfer_Binder) binder = TP->Find(NUNAUO);
1425 //   if ( binder.IsNull() || ! binder->HasResult() )
1426 //     return Standard_False;
1427 //   TopoDS_Shape NUSh = TransferBRep::ShapeResult ( TP, binder );
1428   // get label of NAUO next level
1429   TDF_Label NULab;
1430   STEPConstruct_Tool Tool( WS );
1431   NULab = STEPCAFControl_Reader::FindInstance ( NUNAUO, STool, Tool, PDFileMap, ShapeLabelMap ); 
1432 //   STool->Search(NUSh, NUlab);
1433   if (NULab.IsNull())
1434     return Standard_False;
1435   aLabels.Append( NULab );
1436   // and check by recurse.
1437   findNextSHUOlevel( WS, subSHUO, STool, PDFileMap, ShapeLabelMap, aLabels );
1438   return Standard_True;
1439 }
1440
1441
1442 //=======================================================================
1443 //function : setSHUOintoDoc
1444 //purpose  : auxilary
1445 //=======================================================================
1446 static TDF_Label setSHUOintoDoc (const Handle(XSControl_WorkSession) &WS,
1447                                  const Handle(StepRepr_SpecifiedHigherUsageOccurrence)& SHUO,
1448                                  const Handle(XCAFDoc_ShapeTool)& STool,
1449                                  const STEPCAFControl_DataMapOfPDExternFile &PDFileMap,
1450                                  const XCAFDoc_DataMapOfShapeLabel &ShapeLabelMap)
1451 {
1452   TDF_Label aMainLabel;
1453   // get upper usage NAUO from SHUO.
1454   Handle(StepRepr_NextAssemblyUsageOccurrence) UUNAUO =
1455     Handle(StepRepr_NextAssemblyUsageOccurrence)::DownCast(SHUO->UpperUsage());
1456   Handle(StepRepr_NextAssemblyUsageOccurrence) NUNAUO = SHUO->NextUsage();
1457   if ( UUNAUO.IsNull() || NUNAUO.IsNull() ) {
1458 #ifdef OCCT_DEBUG
1459     cout << "Warning: " << __FILE__ <<": Upper_usage or Next_usage of styled SHUO is null. Skip it" << endl;
1460 #endif
1461     return aMainLabel;
1462   }
1463 //   Handle(Interface_InterfaceModel) Model = WS->Model();
1464 //   Handle(XSControl_TransferReader) TR = WS->TransferReader();
1465 //   Handle(Transfer_TransientProcess) TP = TR->TransientProcess();
1466 //   TopoDS_Shape UUSh, NUSh;
1467 //   Handle(Transfer_Binder) binder = TP->Find(UUNAUO);
1468 //   if ( binder.IsNull() || ! binder->HasResult() )
1469 //     return aMainLabel;
1470 //   UUSh = TransferBRep::ShapeResult ( TP, binder );
1471 //   binder = TP->Find(NUNAUO);
1472 //   if ( binder.IsNull() || ! binder->HasResult() )
1473 //     return aMainLabel;
1474 //   NUSh = TransferBRep::ShapeResult ( TP, binder );
1475
1476   // get first labels for first SHUO attribute
1477   TDF_Label UULab, NULab;
1478   STEPConstruct_Tool Tool( WS );
1479   UULab = STEPCAFControl_Reader::FindInstance ( UUNAUO, STool, Tool, PDFileMap, ShapeLabelMap ); 
1480   NULab = STEPCAFControl_Reader::FindInstance ( NUNAUO, STool, Tool, PDFileMap, ShapeLabelMap ); 
1481   
1482 //   STool->Search(UUSh, UULab);
1483 //   STool->Search(NUSh, NULab);
1484   if (UULab.IsNull() || NULab.IsNull()) return aMainLabel;
1485   //create sequence fo labels to set SHUO structure into the document
1486   TDF_LabelSequence ShuoLabels;
1487   ShuoLabels.Append( UULab );
1488   ShuoLabels.Append( NULab );
1489   // add all other labels of sub SHUO entities
1490   findNextSHUOlevel( WS, SHUO, STool, PDFileMap, ShapeLabelMap, ShuoLabels );
1491   // last accord for SHUO
1492   Handle(XCAFDoc_GraphNode) anSHUOAttr;
1493   if ( STool->SetSHUO( ShuoLabels, anSHUOAttr ) )
1494     aMainLabel = anSHUOAttr->Label();
1495   
1496   return aMainLabel;
1497 }
1498
1499
1500 //=======================================================================
1501 //function : ReadSHUOs
1502 //purpose  : 
1503 //=======================================================================
1504
1505 Standard_Boolean STEPCAFControl_Reader::ReadSHUOs (const Handle(XSControl_WorkSession) &WS,
1506                                                    Handle(TDocStd_Document)& Doc,
1507                                                    const STEPCAFControl_DataMapOfPDExternFile &PDFileMap,
1508                                                    const XCAFDoc_DataMapOfShapeLabel &ShapeLabelMap) const
1509 {
1510   // the big part code duplication from ReadColors.
1511   // It is possible to share this code functionality, just to decide how ???
1512   Handle(XCAFDoc_ColorTool) CTool = XCAFDoc_DocumentTool::ColorTool( Doc->Main() );
1513   Handle(XCAFDoc_ShapeTool) STool = CTool->ShapeTool();
1514   
1515   STEPConstruct_Styles Styles ( WS );
1516   if ( ! Styles.LoadStyles() ) {
1517 #ifdef OCCT_DEBUG
1518     cout << "Warning: no styles are found in the model" << endl;
1519 #endif
1520     return Standard_False;
1521   }
1522   // searching for invisible items in the model
1523   Handle(TColStd_HSequenceOfTransient) aHSeqOfInvisStyle = new TColStd_HSequenceOfTransient;
1524   Styles.LoadInvisStyles( aHSeqOfInvisStyle );
1525   // parse and search for color attributes
1526   Standard_Integer nb = Styles.NbStyles();
1527   for ( Standard_Integer i=1; i <= nb; i++ ) {
1528     Handle(StepVisual_StyledItem) style = Styles.Style ( i );
1529     if ( style.IsNull() ) continue;
1530     
1531     Standard_Boolean IsVisible = Standard_True;
1532     // check the visibility of styled item.
1533     for (Standard_Integer si = 1; si <= aHSeqOfInvisStyle->Length(); si++ ) {
1534       if ( style != aHSeqOfInvisStyle->Value( si ) )
1535         continue;
1536       // found that current style is invisible.
1537 #ifdef OCCT_DEBUG
1538       cout << "Warning: item No " << i << "(" << style->Item()->DynamicType()->Name() << ") is invisible" << endl;
1539 #endif
1540       IsVisible = Standard_False;
1541       break;
1542     }
1543
1544     Handle(StepVisual_Colour) SurfCol, BoundCol, CurveCol;
1545     // check if it is component style
1546     Standard_Boolean IsComponent = Standard_False;
1547     if ( ! Styles.GetColors ( style, SurfCol, BoundCol, CurveCol, IsComponent ) && IsVisible )
1548       continue;
1549     if (!IsComponent)
1550       continue;
1551     Handle(StepShape_ShapeRepresentation) aSR;
1552     findStyledSR( style, aSR );
1553     // search for SR along model
1554     if ( aSR.IsNull() )
1555       continue;
1556     Interface_EntityIterator subs = WS->HGraph()->Graph().Sharings( aSR );
1557     Handle(StepShape_ShapeDefinitionRepresentation) aSDR;
1558     for (subs.Start(); subs.More(); subs.Next()) {
1559       aSDR = Handle(StepShape_ShapeDefinitionRepresentation)::DownCast(subs.Value());
1560       if ( aSDR.IsNull() )
1561         continue;
1562       StepRepr_RepresentedDefinition aPDSselect = aSDR->Definition();
1563       Handle(StepRepr_ProductDefinitionShape) PDS = 
1564         Handle(StepRepr_ProductDefinitionShape)::DownCast(aPDSselect.PropertyDefinition());
1565       if ( PDS.IsNull() )
1566         continue;
1567       StepRepr_CharacterizedDefinition aCharDef = PDS->Definition();
1568       Handle(StepRepr_SpecifiedHigherUsageOccurrence) SHUO =
1569         Handle(StepRepr_SpecifiedHigherUsageOccurrence)::DownCast(aCharDef.ProductDefinitionRelationship());
1570       if ( SHUO.IsNull() )
1571         continue;
1572       
1573       // set the SHUO structure to the document
1574       TDF_Label aLabelForStyle = setSHUOintoDoc( WS, SHUO, STool, PDFileMap, ShapeLabelMap );
1575       if ( aLabelForStyle.IsNull() ) {
1576 #ifdef OCCT_DEBUG
1577         cout << "Warning: " << __FILE__ <<": coudnot create SHUO structure in the document" << endl;
1578 #endif
1579         continue;
1580       }
1581       // now set the style to the SHUO main label.
1582       if ( ! SurfCol.IsNull() ) {
1583         Quantity_Color col;
1584         Styles.DecodeColor ( SurfCol, col );
1585         CTool->SetColor ( aLabelForStyle, col, XCAFDoc_ColorSurf );
1586       }
1587       if ( ! BoundCol.IsNull() ) {
1588         Quantity_Color col;
1589         Styles.DecodeColor ( BoundCol, col );
1590         CTool->SetColor ( aLabelForStyle, col, XCAFDoc_ColorCurv );
1591       }
1592       if ( ! CurveCol.IsNull() ) {
1593         Quantity_Color col;
1594         Styles.DecodeColor ( CurveCol, col );
1595         CTool->SetColor ( aLabelForStyle, col, XCAFDoc_ColorCurv );
1596       }
1597       if ( !IsVisible )
1598         // sets the invisibility for shape.
1599         CTool->SetVisibility( aLabelForStyle, Standard_False );
1600       
1601     } // end search SHUO by SDR
1602   } // end iterates on styles
1603       
1604   return Standard_True;
1605 }
1606
1607
1608 //=======================================================================
1609 //function : GetLengthConversionFactor
1610 //purpose  : 
1611 //=======================================================================
1612 static Standard_Boolean GetLengthConversionFactor(Handle(StepBasic_NamedUnit)& NU,
1613                                                   Standard_Real& afact)
1614 {
1615   afact=1.;
1616   if( !NU->IsKind(STANDARD_TYPE(StepBasic_ConversionBasedUnitAndLengthUnit)) ) return Standard_False;
1617   Handle(StepBasic_ConversionBasedUnitAndLengthUnit) CBULU =
1618     Handle(StepBasic_ConversionBasedUnitAndLengthUnit)::DownCast(NU);
1619   Handle(StepBasic_MeasureWithUnit) MWUCBU = CBULU->ConversionFactor();
1620   afact = MWUCBU->ValueComponent();
1621   StepBasic_Unit anUnit2 = MWUCBU->UnitComponent();
1622   if(anUnit2.CaseNum(anUnit2.Value())==1) {
1623     Handle(StepBasic_NamedUnit) NU2 = anUnit2.NamedUnit();
1624     if(NU2->IsKind(STANDARD_TYPE(StepBasic_SiUnit))) {
1625       Handle(StepBasic_SiUnit) SU = Handle(StepBasic_SiUnit)::DownCast(NU2);
1626       if(SU->Name()==StepBasic_sunMetre) {
1627         if(SU->HasPrefix()) 
1628           afact *= STEPConstruct_UnitContext::ConvertSiPrefix (SU->Prefix());
1629         // convert m to mm
1630         afact *= 1000.;
1631       }
1632     }
1633   }
1634   return Standard_True;
1635 }
1636
1637
1638 //=======================================================================
1639 //function : GetAngleConversionFactor
1640 //purpose  : 
1641 //=======================================================================
1642 static Standard_Boolean GetAngleConversionFactor(Handle(StepBasic_NamedUnit)& NU,
1643                                                   Standard_Real& afact)
1644 {
1645   afact=1.;
1646   if( !NU->IsKind(STANDARD_TYPE(StepBasic_ConversionBasedUnitAndPlaneAngleUnit)) ) return Standard_False;
1647   Handle(StepBasic_ConversionBasedUnitAndPlaneAngleUnit) CBULU =
1648     Handle(StepBasic_ConversionBasedUnitAndPlaneAngleUnit)::DownCast(NU);
1649   Handle(StepBasic_MeasureWithUnit) MWUCBU = CBULU->ConversionFactor();
1650   afact = MWUCBU->ValueComponent();
1651   StepBasic_Unit anUnit2 = MWUCBU->UnitComponent();
1652   if(anUnit2.CaseNum(anUnit2.Value())==1) {
1653     Handle(StepBasic_NamedUnit) NU2 = anUnit2.NamedUnit();
1654     if(NU2->IsKind(STANDARD_TYPE(StepBasic_SiUnit))) {
1655       Handle(StepBasic_SiUnit) SU = Handle(StepBasic_SiUnit)::DownCast(NU2);
1656       if(SU->Name()==StepBasic_sunRadian) {
1657         if(SU->HasPrefix()) 
1658           afact *= STEPConstruct_UnitContext::ConvertSiPrefix (SU->Prefix());
1659         // convert radian to deg
1660         afact *= 180/M_PI;
1661       }
1662     }
1663   }
1664   return Standard_True;
1665 }
1666
1667 //=======================================================================
1668 //function : GetMassConversionFactor
1669 //purpose  : 
1670 //=======================================================================
1671 static Standard_Boolean GetMassConversionFactor(Handle(StepBasic_NamedUnit)& NU,
1672                                                 Standard_Real& afact)
1673 {
1674   afact=1.;
1675   if( !NU->IsKind(STANDARD_TYPE(StepBasic_ConversionBasedUnitAndMassUnit)) ) return Standard_False;
1676   Handle(StepBasic_ConversionBasedUnitAndMassUnit) CBUMU =
1677     Handle(StepBasic_ConversionBasedUnitAndMassUnit)::DownCast(NU);
1678   Handle(StepBasic_MeasureWithUnit) MWUCBU = CBUMU->ConversionFactor();
1679   afact = MWUCBU->ValueComponent();
1680   StepBasic_Unit anUnit2 = MWUCBU->UnitComponent();
1681   if(anUnit2.CaseNum(anUnit2.Value())==1) {
1682     Handle(StepBasic_NamedUnit) NU2 = anUnit2.NamedUnit();
1683     if(NU2->IsKind(STANDARD_TYPE(StepBasic_SiUnit))) {
1684       Handle(StepBasic_SiUnit) SU = Handle(StepBasic_SiUnit)::DownCast(NU2);
1685       if(SU->Name()==StepBasic_sunGram) {
1686         if(SU->HasPrefix())
1687           afact *= STEPConstruct_UnitContext::ConvertSiPrefix (SU->Prefix());
1688       }
1689     }
1690   }
1691   return Standard_True;
1692 }
1693
1694
1695 //=======================================================================
1696 //function : ReadDatums
1697 //purpose  : auxilary
1698 //=======================================================================
1699 static Standard_Boolean ReadDatums(const Handle(XCAFDoc_ShapeTool) &STool,
1700                                    const Handle(XCAFDoc_DimTolTool) &DGTTool,
1701                                    const Interface_Graph &graph,
1702                                    Handle(Transfer_TransientProcess) &TP,
1703                                    const TDF_Label TolerL,
1704                                    const Handle(StepDimTol_GeometricToleranceWithDatumReference) GTWDR)
1705 {
1706   if(GTWDR.IsNull()) return Standard_False;
1707   Handle(StepDimTol_HArray1OfDatumReference) HADR = GTWDR->DatumSystem();
1708   if(HADR.IsNull()) return Standard_False;
1709   for(Standard_Integer idr=1; idr<=HADR->Length(); idr++) {
1710     Handle(StepDimTol_DatumReference) DR = HADR->Value(idr);
1711     Handle(StepDimTol_Datum) aDatum = DR->ReferencedDatum();
1712     if(aDatum.IsNull()) continue;
1713     Interface_EntityIterator subs4 = graph.Sharings(aDatum);
1714     for(subs4.Start(); subs4.More(); subs4.Next()) {
1715       Handle(StepRepr_ShapeAspectRelationship) SAR = 
1716         Handle(StepRepr_ShapeAspectRelationship)::DownCast(subs4.Value());
1717       if(SAR.IsNull()) continue;
1718       Handle(StepDimTol_DatumFeature) DF = 
1719         Handle(StepDimTol_DatumFeature)::DownCast(SAR->RelatingShapeAspect());
1720       if(DF.IsNull()) continue;
1721       Interface_EntityIterator subs5 = graph.Sharings(DF);
1722       Handle(StepRepr_PropertyDefinition) PropDef;
1723       for(subs5.Start(); subs5.More() && PropDef.IsNull(); subs5.Next()) {
1724         PropDef = Handle(StepRepr_PropertyDefinition)::DownCast(subs5.Value());
1725       }
1726       if(PropDef.IsNull()) continue;
1727       Handle(StepShape_AdvancedFace) AF;
1728       subs5 = graph.Sharings(PropDef);
1729       for(subs5.Start(); subs5.More(); subs5.Next()) {
1730         Handle(StepShape_ShapeDefinitionRepresentation) SDR = 
1731           Handle(StepShape_ShapeDefinitionRepresentation)::DownCast(subs5.Value());
1732         if(!SDR.IsNull()) {
1733           Handle(StepRepr_Representation) Repr = SDR->UsedRepresentation();
1734           if( !Repr.IsNull() && Repr->NbItems()>0 ) {
1735             Handle(StepRepr_RepresentationItem) RI = Repr->ItemsValue(1);
1736             AF = Handle(StepShape_AdvancedFace)::DownCast(RI);
1737           }
1738         }
1739       }
1740       if(AF.IsNull()) return Standard_False;
1741       Standard_Integer index = TP->MapIndex(AF);
1742       TopoDS_Shape aSh;
1743       if(index >0) {
1744         Handle(Transfer_Binder) binder = TP->MapItem(index);
1745         aSh = TransferBRep::ShapeResult(binder);
1746       }
1747       if(aSh.IsNull()) continue; 
1748       TDF_Label shL;
1749       if( !STool->Search(aSh, shL, Standard_True, Standard_True, Standard_True) ) continue;
1750       DGTTool->SetDatum(shL,TolerL,PropDef->Name(),PropDef->Description(),aDatum->Identification());
1751     }
1752   }
1753   return Standard_True;
1754 }
1755
1756
1757 //=======================================================================
1758 //function : setDatumToXCAF
1759 //purpose  : 
1760 //=======================================================================
1761 static Standard_Boolean setDatumToXCAF(const Handle(StepDimTol_Datum)& theDat,
1762                                         const TDF_Label theGDTL,
1763                                         const Standard_Integer thePositionCounter,
1764                                         const XCAFDimTolObjects_DatumModifiersSequence& aXCAFModifiers,
1765                                         const XCAFDimTolObjects_DatumModifWithValue aXCAFModifWithVal,
1766                                         const Standard_Real aModifValue,
1767                                         const Handle(TDocStd_Document)& theDoc,
1768                                         const Handle(XSControl_WorkSession)& theWS)
1769 {
1770   Handle(XCAFDoc_ShapeTool) aSTool = XCAFDoc_DocumentTool::ShapeTool( theDoc->Main() );
1771   Handle(XCAFDoc_DimTolTool) aDGTTool = XCAFDoc_DocumentTool::DimTolTool( theDoc->Main() );
1772   Handle(XSControl_TransferReader) aTR = theWS->TransferReader();
1773   Handle(Transfer_TransientProcess) aTP = aTR->TransientProcess();
1774   const Interface_Graph& aGraph = aTP->Graph();
1775   Handle(XCAFDoc_Datum) aDat;
1776   TDF_Label aShL;
1777   Standard_Boolean aRefShapeIsFound = Standard_False;
1778   Standard_Boolean aFirstStep = Standard_True;
1779   Interface_EntityIterator anIterD = aGraph.Sharings(theDat);
1780   for(anIterD.Start(); anIterD.More(); anIterD.Next()) {
1781     Handle(StepRepr_ShapeAspectRelationship) aSAR = 
1782       Handle(StepRepr_ShapeAspectRelationship)::DownCast(anIterD.Value());
1783     if(aSAR.IsNull()) continue;
1784
1785     Handle(StepRepr_ShapeAspect) aSA = aSAR->RelatingShapeAspect();
1786     if (aSA.IsNull()) continue;
1787     Handle(StepAP242_GeometricItemSpecificUsage) aPGISU;
1788     if(aSA->IsKind(STANDARD_TYPE(StepRepr_CompShAspAndDatumFeatAndShAsp)))
1789     {
1790       //processing for complex entity
1791       Interface_EntityIterator anIterC = aGraph.Sharings(aSA);
1792       for(anIterC.Start(); anIterC.More(); anIterC.Next()) {
1793         Handle(StepRepr_ShapeAspectRelationship) SAR = 
1794           Handle(StepRepr_ShapeAspectRelationship)::DownCast(anIterC.Value());
1795         if(SAR.IsNull()) continue;
1796         Handle(StepRepr_ShapeAspect) aS = SAR->RelatedShapeAspect();
1797         if(aS.IsNull()) continue;
1798         Interface_EntityIterator anIterSA = aGraph.Sharings(aS);
1799         for(anIterSA.Start(); anIterSA.More() && aPGISU.IsNull(); anIterSA.Next()) {
1800           aPGISU = Handle(StepAP242_GeometricItemSpecificUsage)::DownCast(anIterSA.Value());
1801         }
1802         if(!aPGISU.IsNull()){
1803           aSA = aS;
1804           break;
1805         }
1806       }
1807     }
1808     else if(aSA->IsKind(STANDARD_TYPE(StepDimTol_PlacedDatumTargetFeature)))
1809     {
1810       //processing for datum target
1811       Interface_EntityIterator anIterDTF = aGraph.Sharings(aSA);
1812       for(anIterDTF.Start(); anIterDTF.More(); anIterDTF.Next()) {
1813         if(anIterDTF.Value()->IsKind(STANDARD_TYPE(StepRepr_FeatureForDatumTargetRelationship)))
1814         {
1815           Handle(StepRepr_FeatureForDatumTargetRelationship) aFFDTR =
1816             Handle(StepRepr_FeatureForDatumTargetRelationship)::DownCast(anIterDTF.Value());
1817           Handle(StepRepr_ShapeAspect) aTmpSA = aFFDTR->RelatedShapeAspect();
1818           Interface_EntityIterator anIterDSWP = aGraph.Sharings(aTmpSA);
1819           for(anIterDSWP.Start(); anIterDSWP.More() && aPGISU.IsNull(); anIterDSWP.Next()) {
1820             aPGISU = Handle(StepAP242_GeometricItemSpecificUsage)::DownCast(anIterDSWP.Value());
1821     }
1822         }
1823       }
1824     }
1825     if (aSA.IsNull()) continue;
1826     Interface_EntityIterator anIterDSWP = aGraph.Sharings(aSA);
1827     for(anIterDSWP.Start(); anIterDSWP.More() && aPGISU.IsNull(); anIterDSWP.Next()) {
1828       aPGISU = Handle(StepAP242_GeometricItemSpecificUsage)::DownCast(anIterDSWP.Value());
1829     }
1830     if(aPGISU.IsNull()) continue;
1831     // get representation item
1832     Handle(StepRepr_RepresentationItem) aRI;
1833     for(Standard_Integer i = 1 ; i <= aPGISU->NbIdentifiedItem() && aRI.IsNull(); i++)
1834     {
1835       aRI = aPGISU->IdentifiedItemValue(i);
1836     }
1837     if(aRI.IsNull()) continue;
1838     Standard_Integer index = aTP->MapIndex(aRI);
1839     TopoDS_Shape aSh;
1840     if(index >0) {
1841       Handle(Transfer_Binder) binder = aTP->MapItem(index);
1842       aSh = TransferBRep::ShapeResult(binder);
1843     }
1844     if(aSh.IsNull()) continue; 
1845     if( !aSTool->Search(aSh, aShL, Standard_True, Standard_True, Standard_True) ) continue;
1846     Handle(TDataStd_TreeNode) aNode;
1847     if(aFirstStep && aShL.FindAttribute(XCAFDoc::DatumRefGUID(),aNode) && aNode->HasFirst() &&
1848        aNode->First()->Label().FindAttribute(XCAFDoc_Datum::GetID(),aDat))
1849     {
1850       //if datums already attached, not need add datum target
1851       aRefShapeIsFound = Standard_True;
1852     }
1853     aFirstStep = Standard_False;
1854     Handle(XCAFDimTolObjects_DatumObject) aDatObj;
1855     if(aSA->IsKind(STANDARD_TYPE(StepDimTol_PlacedDatumTargetFeature)))
1856     {
1857       if(!aRefShapeIsFound)
1858       {
1859         //if datum targers not yet added
1860         TDF_Label aDatL = aDGTTool->AddDatum();
1861         aDat = XCAFDoc_Datum::Set(aDatL);
1862         aDGTTool->SetDatum(aShL, aDatL);
1863         aDatObj = aDat->GetObject();
1864         aDatObj->SetName(theDat->Identification());
1865         aDatObj->SetPosition (thePositionCounter);
1866         if(!aXCAFModifiers.IsEmpty())
1867           aDatObj->SetModifiers(aXCAFModifiers);
1868         if (aXCAFModifWithVal != XCAFDimTolObjects_DatumModifWithValue_None) 
1869           aDatObj->SetModifierWithValue(aXCAFModifWithVal, aModifValue);
1870         aDGTTool->SetDatumToGeomTol(aDatL, theGDTL);
1871         Handle(StepDimTol_PlacedDatumTargetFeature) aPDTF = Handle(StepDimTol_PlacedDatumTargetFeature)::DownCast(aSA);
1872         if (aPDTF->TargetId()->IsIntegerValue())
1873           aDatObj->SetDatumTargetNumber(aPDTF->TargetId()->IntegerValue());
1874         else
1875           aDatObj->SetDatumTargetNumber(0);
1876         aDatObj->IsDatumTarget(Standard_True);
1877         XCAFDimTolObjects_DatumTargetType aType;
1878         if(STEPCAFControl_GDTProperty::GetDatumTargetType(aSA->Description(),aType))
1879         {
1880           aDatObj->SetDatumTargetType(aType);
1881           if(aType == XCAFDimTolObjects_DatumTargetType_Area)
1882           {
1883             Interface_EntityIterator anIterDTF = aGraph.Shareds(aSA);
1884             for(anIterDTF.Start(); anIterDTF.More(); anIterDTF.Next()) {
1885               if(anIterDTF.Value()->IsKind(STANDARD_TYPE(StepAP242_GeometricItemSpecificUsage)))
1886               {
1887                 Handle(StepAP242_GeometricItemSpecificUsage) aGISU
1888                   = Handle(StepAP242_GeometricItemSpecificUsage)::DownCast(anIterDSWP.Value());
1889                 Handle(StepRepr_RepresentationItem) anItem;
1890                 if(aPGISU->NbIdentifiedItem() > 0) {
1891                   anItem = aPGISU->IdentifiedItemValue(1);
1892                 }
1893                 if(anItem.IsNull()) continue;
1894                 Standard_Integer anItemIndex = aTP->MapIndex(anItem);
1895                 if(anItemIndex >0) {
1896                   Handle(Transfer_Binder) binder = aTP->MapItem(anItemIndex);
1897                   TopoDS_Shape anItemShape = TransferBRep::ShapeResult(binder);
1898                   aDatObj->SetDatumTarget(anItemShape);
1899                 }
1900               }
1901             }
1902           }
1903           else
1904           {
1905             Interface_EntityIterator anIterDTF = aGraph.Shareds(aSA);
1906             for(anIterDTF.Start(); anIterDTF.More(); anIterDTF.Next()) {
1907               if(anIterDTF.Value()->IsKind(STANDARD_TYPE(StepRepr_PropertyDefinition)))
1908               {
1909                 Interface_EntityIterator anIterPD = aGraph.Shareds(anIterDTF.Value());
1910                 for(anIterPD.Start(); anIterPD.More(); anIterPD.Next()) {
1911                   if(anIterPD.Value()->IsKind(STANDARD_TYPE(StepShape_ShapeDefinitionRepresentation)))
1912                   {
1913                     Interface_EntityIterator anIterSDR = aGraph.Sharings(anIterPD.Value());
1914                     for(anIterSDR.Start(); anIterSDR.More(); anIterSDR.Next()) {
1915                       if(anIterSDR.Value()->IsKind(STANDARD_TYPE(StepShape_ShapeRepresentationWithParameters)))
1916                       {
1917                         Handle(StepShape_ShapeRepresentationWithParameters) aSRWP
1918                           = Handle(StepShape_ShapeRepresentationWithParameters)::DownCast(anIterSDR.Value());
1919                         for(Standard_Integer r = aSRWP->Items()->Lower(); r <= aSRWP->Items()->Upper(); r++)
1920                         {
1921                           if(aSRWP->ItemsValue(r)->IsKind(STANDARD_TYPE(StepGeom_Axis2Placement3d)))
1922                           {
1923                             Handle(StepGeom_Axis2Placement3d) anAx
1924                               = Handle(StepGeom_Axis2Placement3d)::DownCast(aSRWP->ItemsValue(r));
1925                             Handle(TColStd_HArray1OfReal) aDirArr = anAx->Axis()->DirectionRatios();
1926                             Handle(TColStd_HArray1OfReal) aDirRArr = anAx->RefDirection()->DirectionRatios();
1927                             Handle(TColStd_HArray1OfReal) aLocArr = anAx->Location()->Coordinates();
1928                             gp_Dir aDir;
1929                             gp_Dir aDirR;
1930                             gp_Pnt aPnt;
1931                             if(!aDirArr.IsNull() && aDirArr->Length() > 2 &&
1932                               !aDirRArr.IsNull() && aDirRArr->Length() > 2 && 
1933                               !aLocArr.IsNull() && aLocArr->Length() > 2)
1934                             {
1935                               aDir.SetCoord(aDirArr->Lower(), aDirArr->Lower()+1, aDirArr->Lower()+2);
1936                               aDirR.SetCoord(aDirRArr->Lower(), aDirRArr->Lower()+1, aDirRArr->Lower()+2);
1937                               aPnt.SetCoord(aLocArr->Lower(), aLocArr->Lower()+1, aLocArr->Lower()+2);
1938                               gp_Ax2 anA(aPnt, aDir, aDirR);
1939                               aDatObj->SetDatumTargetAxis(anA);
1940                             }
1941                           }
1942                           else if(aSRWP->ItemsValue(r)->IsKind(STANDARD_TYPE(StepRepr_ReprItemAndLengthMeasureWithUnit)))
1943                           {
1944                             Handle(StepRepr_ReprItemAndLengthMeasureWithUnit) aM =
1945                               Handle(StepRepr_ReprItemAndLengthMeasureWithUnit)::DownCast(aSRWP->ItemsValue(r)); 
1946                             Standard_Real aVal = aM->GetMeasureWithUnit()->ValueComponent();
1947                             StepBasic_Unit anUnit = aM->GetMeasureWithUnit()->UnitComponent();
1948                             Standard_Real aFact=1.;
1949                             if(anUnit.IsNull()) continue;
1950                             if( !(anUnit.CaseNum(anUnit.Value())==1) ) continue;
1951                             Handle(StepBasic_NamedUnit) NU = anUnit.NamedUnit();
1952                             if(GetLengthConversionFactor(NU,aFact)) aVal=aVal*aFact;
1953                             if(aM->Name()->String().IsEqual("target length") ||
1954                               aM->Name()->String().IsEqual("target diameter"))
1955                               aDatObj->SetDatumTargetLength(aVal);
1956                             else
1957                               aDatObj->SetDatumTargetWidth(aVal);
1958                           }
1959                         }
1960                       }
1961                     }
1962                   }
1963                 }
1964               }
1965             }
1966           }
1967         }
1968       }
1969     }
1970     else
1971     {
1972       //processing for darum feature
1973       TDF_Label aDatL = aDGTTool->AddDatum();
1974       aDat = XCAFDoc_Datum::Set(aDatL);
1975       aDGTTool->SetDatum(aShL, aDatL);
1976       aDatObj = aDat->GetObject();
1977       aDatObj->SetName(theDat->Identification());
1978       aDatObj->SetPosition (thePositionCounter);
1979       if(!aXCAFModifiers.IsEmpty())
1980         aDatObj->SetModifiers(aXCAFModifiers);
1981       if (aXCAFModifWithVal != XCAFDimTolObjects_DatumModifWithValue_None) 
1982         aDatObj->SetModifierWithValue(aXCAFModifWithVal, aModifValue);
1983       aDGTTool->SetDatumToGeomTol(aDatL, theGDTL);
1984     }
1985     if(!aDatObj.IsNull())
1986       aDat->SetObject(aDatObj);
1987   }
1988   return !aDat.IsNull();
1989 }
1990
1991
1992 //=======================================================================
1993 //function : ReadDatums
1994 //purpose  : auxilary
1995 //=======================================================================
1996 static Standard_Boolean readDatumsAP242(const Handle(Standard_Transient)& theEnt,
1997                                         const TDF_Label theGDTL,
1998                                         const Handle(TDocStd_Document)& theDoc,
1999                                         const Handle(XSControl_WorkSession)& theWS)
2000 {
2001   Handle(XCAFDoc_ShapeTool) aSTool = XCAFDoc_DocumentTool::ShapeTool( theDoc->Main() );
2002   Handle(XCAFDoc_DimTolTool) aDGTTool = XCAFDoc_DocumentTool::DimTolTool( theDoc->Main() );
2003   Handle(XSControl_TransferReader) aTR = theWS->TransferReader();
2004   Handle(Transfer_TransientProcess) aTP = aTR->TransientProcess();
2005   const Interface_Graph& aGraph = aTP->Graph();
2006
2007   Interface_EntityIterator anIter = aGraph.Shareds(theEnt);
2008   for(anIter.Start(); anIter.More(); anIter.Next()) {
2009     Handle(Standard_Transient) anAtr = anIter.Value();
2010     if(anAtr->IsKind(STANDARD_TYPE(StepDimTol_DatumSystem)))
2011     {
2012       Standard_Integer aPositionCounter = 0;//position on frame 
2013       Handle(StepDimTol_DatumSystem) aDS = Handle(StepDimTol_DatumSystem)::DownCast(anAtr);
2014       Interface_EntityIterator anIterDS = aGraph.Sharings(aDS);
2015       for(anIterDS.Start(); anIterDS.More(); anIterDS.Next()) {
2016         Handle(Standard_Transient) anAtrDS = anIterDS.Value();
2017         if(anAtrDS->IsKind(STANDARD_TYPE(StepAP242_GeometricItemSpecificUsage)))
2018         {
2019           //get axis
2020           Handle(StepAP242_GeometricItemSpecificUsage)aAxGISUI
2021             = Handle(StepAP242_GeometricItemSpecificUsage)::DownCast(anAtrDS);
2022           if (aAxGISUI->IdentifiedItemValue(1)->IsKind(STANDARD_TYPE(StepGeom_Axis2Placement3d)))
2023           {
2024             Handle(StepGeom_Axis2Placement3d) anAx
2025               = Handle(StepGeom_Axis2Placement3d)::DownCast(aAxGISUI->IdentifiedItemValue(1));
2026             Handle(XCAFDoc_GeomTolerance) aTol;
2027             if(theGDTL.FindAttribute(XCAFDoc_GeomTolerance::GetID(), aTol))
2028             {
2029               Handle(XCAFDimTolObjects_GeomToleranceObject) anObj = aTol->GetObject();
2030               Handle(TColStd_HArray1OfReal) aDirArr = anAx->Axis()->DirectionRatios();
2031               Handle(TColStd_HArray1OfReal) aDirRArr = anAx->RefDirection()->DirectionRatios();
2032               Handle(TColStd_HArray1OfReal) aLocArr = anAx->Location()->Coordinates();
2033               gp_Dir aDir;
2034               gp_Dir aDirR;
2035               gp_Pnt aPnt;
2036               if(!aDirArr.IsNull() && aDirArr->Length() > 2 &&
2037                 !aDirRArr.IsNull() && aDirRArr->Length() > 2 && 
2038                 !aLocArr.IsNull() && aLocArr->Length() > 2)
2039               {
2040                 aDir.SetCoord(aDirArr->Lower(), aDirArr->Lower()+1, aDirArr->Lower()+2);
2041                 aDirR.SetCoord(aDirRArr->Lower(), aDirRArr->Lower()+1, aDirRArr->Lower()+2);
2042                 aPnt.SetCoord(aLocArr->Lower(), aLocArr->Lower()+1, aLocArr->Lower()+2);
2043                 gp_Ax2 anA(aPnt, aDir, aDirR);
2044                 anObj->SetAxis(anA);
2045                 aTol->SetObject(anObj);
2046               }
2047             }
2048           }
2049         }
2050       }
2051       if (aDS->NbConstituents() > 0)
2052       {
2053         //get datum feature and datum target from datum system
2054         Handle(StepDimTol_HArray1OfDatumReferenceCompartment) aDRCA = aDS->Constituents();
2055         if(!aDRCA.IsNull())
2056         {
2057           for(Standard_Integer i = aDRCA->Lower(); i <= aDRCA->Upper(); i++)
2058           {
2059             Handle(StepDimTol_DatumReferenceCompartment) aDRC = aDRCA->Value(i);
2060             //gete modifiers
2061             Handle(StepDimTol_HArray1OfDatumReferenceModifier) aModif = aDRC->Modifiers();
2062             XCAFDimTolObjects_DatumModifiersSequence aXCAFModifiers;
2063             XCAFDimTolObjects_DatumModifWithValue aXCAFModifWithVal = XCAFDimTolObjects_DatumModifWithValue_None;
2064             Standard_Real aModifValue = 0;
2065             if(!aModif.IsNull())
2066             {
2067               for(Standard_Integer m = aModif->Lower(); m <= aModif->Upper(); m++)
2068               {
2069                 if(aModif->Value(m).CaseNumber() == 2)
2070                   aXCAFModifiers.Append(
2071                   (XCAFDimTolObjects_DatumSingleModif)aModif->Value(m).
2072                   SimpleDatumReferenceModifierMember()->Value());
2073                 else if (aModif->Value(m).CaseNumber() == 1)
2074                 {
2075                   aXCAFModifWithVal = (XCAFDimTolObjects_DatumModifWithValue)(aModif->Value(m).DatumReferenceModifierWithValue()->ModifierType() + 1);
2076                   Standard_Real aVal = aModif->Value(m).DatumReferenceModifierWithValue()->ModifierValue()->ValueComponent();
2077                   StepBasic_Unit anUnit = aModif->Value(m).DatumReferenceModifierWithValue()->ModifierValue()->UnitComponent();
2078                   Standard_Real aFact=1.;
2079                   if(anUnit.IsNull()) continue;
2080                   if( !(anUnit.CaseNum(anUnit.Value())==1) ) continue;
2081                   Handle(StepBasic_NamedUnit) NU = anUnit.NamedUnit();
2082                   if(GetLengthConversionFactor(NU,aFact)) aVal=aVal*aFact;
2083                   aModifValue = aVal;
2084                 }
2085               }
2086             }
2087             aPositionCounter++;
2088             Interface_EntityIterator anIterDRC = aGraph.Shareds(aDRC);
2089             for(anIterDRC.Start(); anIterDRC.More(); anIterDRC.Next()) {
2090               
2091               if(anIterDRC.Value()->IsKind(STANDARD_TYPE(StepDimTol_Datum)))
2092               {
2093                 Handle(StepDimTol_Datum) aD = Handle(StepDimTol_Datum)::DownCast(anIterDRC.Value());
2094                 setDatumToXCAF(aD, theGDTL, aPositionCounter, aXCAFModifiers, aXCAFModifWithVal, aModifValue,theDoc, theWS);
2095               }
2096               else if(anIterDRC.Value()->IsKind(STANDARD_TYPE(StepDimTol_DatumReferenceElement)))
2097               {
2098                 Handle(StepDimTol_DatumReferenceElement) aDRE 
2099                   = Handle(StepDimTol_DatumReferenceElement)::DownCast(anIterDRC.Value());
2100                 //get modifiers from group of datums
2101                 Handle(StepDimTol_HArray1OfDatumReferenceModifier) aModifE = aDRE->Modifiers();
2102                 if(!aModifE.IsNull())
2103                 {
2104                   for(Standard_Integer k = aModifE->Lower(); k <= aModifE->Upper(); k++)
2105                   {
2106                     if(aModifE->Value(k).CaseNumber() == 1)
2107                       aXCAFModifiers.Append(
2108                       (XCAFDimTolObjects_DatumSingleModif)aModifE->Value(k).
2109                       SimpleDatumReferenceModifierMember()->Value());
2110                     else if (aModifE->Value(k).CaseNumber() == 2)
2111                     {
2112                       aXCAFModifWithVal = (XCAFDimTolObjects_DatumModifWithValue)(aModifE->Value(k).DatumReferenceModifierWithValue()->ModifierType() + 1);
2113                       Standard_Real aVal = aModifE->Value(k).DatumReferenceModifierWithValue()->ModifierValue()->ValueComponent();
2114                       StepBasic_Unit anUnit = aModifE->Value(k).DatumReferenceModifierWithValue()->ModifierValue()->UnitComponent();
2115                       Standard_Real aFact=1.;
2116                       if(anUnit.IsNull()) continue;
2117                       if( !(anUnit.CaseNum(anUnit.Value())==1) ) continue;
2118                       Handle(StepBasic_NamedUnit) NU = anUnit.NamedUnit();
2119                       if(GetLengthConversionFactor(NU,aFact)) aVal=aVal*aFact;
2120                       aModifValue = aVal;
2121                     }
2122                   }
2123                 }
2124                 Interface_EntityIterator anIterDRE = aGraph.Shareds(aDRE);
2125                 for(anIterDRE.Start(); anIterDRE.More(); anIterDRE.Next()) {
2126                   if(anIterDRE.Value()->IsKind(STANDARD_TYPE(StepDimTol_Datum)))
2127                   {
2128                     Handle(StepDimTol_Datum) aD = Handle(StepDimTol_Datum)::DownCast(anIterDRE.Value());
2129                     setDatumToXCAF(aD, theGDTL, aPositionCounter, aXCAFModifiers, aXCAFModifWithVal, aModifValue,theDoc, theWS);
2130                   }
2131                 }
2132               }
2133             }
2134           }
2135         }
2136       }
2137     }
2138   }
2139   return Standard_True;
2140 }
2141
2142 //=======================================================================
2143 //function : craeteGeomTolObjectInXCAF
2144 //purpose  : 
2145 //=======================================================================
2146 static TDF_Label createGDTObjectInXCAF(const Handle(Standard_Transient)& theEnt,
2147                                                const Handle(TDocStd_Document)& theDoc,
2148                                                const Handle(XSControl_WorkSession)& theWS)
2149 {
2150   TDF_Label aGDTL;
2151   if(!theEnt->IsKind(STANDARD_TYPE(StepShape_DimensionalSize)) &&
2152      !theEnt->IsKind(STANDARD_TYPE(StepShape_DimensionalLocation)) && 
2153      !theEnt->IsKind(STANDARD_TYPE(StepDimTol_GeometricTolerance)))
2154   {
2155     return aGDTL;
2156   }
2157
2158   Handle(XCAFDoc_ShapeTool) aSTool = XCAFDoc_DocumentTool::ShapeTool( theDoc->Main() );
2159   Handle(XCAFDoc_DimTolTool) aDGTTool = XCAFDoc_DocumentTool::DimTolTool( theDoc->Main() );
2160   Handle(XSControl_TransferReader) aTR = theWS->TransferReader();
2161   Handle(Transfer_TransientProcess) aTP = aTR->TransientProcess();
2162   const Interface_Graph& aGraph = aTP->Graph();
2163   Standard_Boolean isAllAround = Standard_False;
2164   Standard_Boolean isAllOver = Standard_False;
2165   
2166   // find RepresentationItem for current Ent
2167   NCollection_Sequence<Handle(Standard_Transient)> aSeqRI1, aSeqRI2;
2168
2169   Interface_EntityIterator anIter = aGraph.Shareds(theEnt);
2170   for(anIter.Start(); anIter.More(); anIter.Next()) {
2171     Handle(Standard_Transient) anAtr = anIter.Value();
2172     NCollection_Sequence<Handle(StepRepr_ShapeAspect)> aSAs;
2173     if(anAtr->IsKind(STANDARD_TYPE(StepRepr_ProductDefinitionShape)))
2174     {
2175       //if associating tolerances with part (All-Over)
2176       Interface_EntityIterator anIterSDR = aGraph.Sharings(anAtr);
2177       for(anIterSDR.Start(); anIterSDR.More(); anIterSDR.Next()) 
2178       {
2179         Handle(Standard_Transient) anAtrSDR = anIterSDR.Value();
2180         if(anAtrSDR->IsKind(STANDARD_TYPE(StepShape_ShapeDefinitionRepresentation)))
2181         {
2182           isAllOver = Standard_True;
2183           Interface_EntityIterator anIterABSR = aGraph.Shareds(anAtrSDR);
2184           for(anIterABSR.Start(); anIterABSR.More(); anIterABSR.Next()) 
2185           {
2186             Handle(Standard_Transient) anAtrABSR = anIterABSR.Value();
2187             if(anAtrABSR->IsKind(STANDARD_TYPE(StepShape_AdvancedBrepShapeRepresentation)))
2188             {
2189               aSeqRI1.Append(anAtrABSR);
2190             }
2191           }
2192         }
2193       }
2194     }
2195     else if(anAtr->IsKind(STANDARD_TYPE(StepShape_DimensionalLocation)) || 
2196       anAtr->IsKind(STANDARD_TYPE(StepShape_DimensionalSize)))
2197     {
2198       //if tolerance attached to dimension
2199       Interface_EntityIterator anIterDim = aGraph.Shareds(anAtr);
2200       for(anIterDim.Start(); anIterDim.More(); anIterDim.Next()) 
2201       {
2202         Handle(Standard_Transient) anAtrDim = anIterDim.Value();
2203         if(anAtrDim->IsKind(STANDARD_TYPE(StepRepr_CompositeShapeAspect)) || 
2204           anAtrDim->IsKind(STANDARD_TYPE(StepRepr_CompShAspAndDatumFeatAndShAsp)))
2205         {
2206           Interface_EntityIterator anIterCSA = aGraph.Sharings(anAtrDim);
2207           for(anIterCSA.Start(); anIterCSA.More(); anIterCSA.Next()) {
2208             if (anIterCSA.Value()->IsKind(STANDARD_TYPE(StepRepr_ShapeAspectRelationship))){
2209               Interface_EntityIterator anIterSAR = aGraph.Shareds(anIterCSA.Value());
2210               for(anIterSAR.Start(); anIterSAR.More(); anIterSAR.Next()) {
2211                 if (anIterSAR.Value()->IsKind(STANDARD_TYPE(StepRepr_ShapeAspect))){
2212                   aSAs.Append(Handle(StepRepr_ShapeAspect)::DownCast(anIterSAR.Value()));
2213                 }
2214               }
2215             }
2216           }
2217         }
2218         else if(anAtrDim->IsKind(STANDARD_TYPE(StepRepr_ShapeAspect)))
2219         {
2220           aSAs.Append( Handle(StepRepr_ShapeAspect)::DownCast(anAtrDim));
2221         }
2222       }
2223     }
2224     else if(anAtr->IsKind(STANDARD_TYPE(StepRepr_CompositeShapeAspect)) || 
2225       anAtr->IsKind(STANDARD_TYPE(StepRepr_CompShAspAndDatumFeatAndShAsp)))
2226     {
2227       //processing for composite entity
2228       Handle(Standard_Transient) anAtrTmp = anAtr;
2229       if(anAtr->IsKind(STANDARD_TYPE(StepRepr_AllAroundShapeAspect)))
2230       {
2231         // if applyed AllAround Modifier
2232         isAllAround = Standard_True;
2233         Interface_EntityIterator anIterAASA = aGraph.Sharings(anAtrTmp);
2234         for(anIterAASA.Start(); anIterAASA.More(); anIterAASA.Next()) {
2235           if (anIterAASA.Value()->IsKind(STANDARD_TYPE(StepRepr_ShapeAspectRelationship))){
2236             Interface_EntityIterator anIterSAR = aGraph.Shareds(anIterAASA.Value());
2237             for(anIterSAR.Start(); anIterSAR.More(); anIterSAR.Next()) {
2238               if ((anIterSAR.Value()->IsKind(STANDARD_TYPE(StepRepr_CompositeShapeAspect)) || 
2239                 anIterSAR.Value()->IsKind(STANDARD_TYPE(StepRepr_CompShAspAndDatumFeatAndShAsp))) &&
2240                 anAtrTmp != anIterSAR.Value()){
2241                   anAtrTmp = anIterSAR.Value();
2242                   break;
2243               }
2244             }
2245           }
2246         }
2247       }
2248       Interface_EntityIterator anIterCSA = aGraph.Sharings(anAtrTmp);
2249       for(anIterCSA.Start(); anIterCSA.More(); anIterCSA.Next()) {
2250         if (anIterCSA.Value()->IsKind(STANDARD_TYPE(StepRepr_ShapeAspectRelationship))){
2251           Interface_EntityIterator anIterSAR = aGraph.Shareds(anIterCSA.Value());
2252           for(anIterSAR.Start(); anIterSAR.More(); anIterSAR.Next()) {
2253             if (anIterSAR.Value()->IsKind(STANDARD_TYPE(StepRepr_ShapeAspect))){
2254               aSAs.Append(Handle(StepRepr_ShapeAspect)::DownCast(anIterSAR.Value()));
2255             }
2256           }
2257         }
2258       }
2259     }
2260     else if(anAtr->IsKind(STANDARD_TYPE(StepRepr_ShapeAspect)))
2261     {
2262       //default
2263       aSAs.Append( Handle(StepRepr_ShapeAspect)::DownCast(anAtr));
2264     }
2265     if(!aSAs.IsEmpty())
2266     {
2267       //get representation items
2268       NCollection_Sequence<Handle(Standard_Transient)> aSeqRI;
2269       for(Standard_Integer i = aSAs.Lower(); i <= aSAs.Upper(); i++)
2270       {
2271         Interface_EntityIterator anIterSA = aGraph.Sharings(aSAs.Value(i));
2272         Handle(StepAP242_GeometricItemSpecificUsage) aGISU;
2273         Handle(StepRepr_PropertyDefinition) PropD;
2274         for(anIterSA.Start(); anIterSA.More() && aGISU.IsNull() && PropD.IsNull(); anIterSA.Next()) {
2275           aGISU = Handle(StepAP242_GeometricItemSpecificUsage)::DownCast(anIterSA.Value());
2276           PropD = Handle(StepRepr_PropertyDefinition)::DownCast(anIterSA.Value());
2277         }
2278         if(!PropD.IsNull())//for old version
2279         {
2280           Handle(StepRepr_RepresentationItem) RI;
2281           Interface_EntityIterator subs4 = aGraph.Sharings(PropD);
2282           for(subs4.Start(); subs4.More(); subs4.Next()) {
2283             Handle(StepShape_ShapeDefinitionRepresentation) SDR = 
2284               Handle(StepShape_ShapeDefinitionRepresentation)::DownCast(subs4.Value());
2285             if(!SDR.IsNull()) {
2286               Handle(StepRepr_Representation) Repr = SDR->UsedRepresentation();
2287               if( !Repr.IsNull() && Repr->NbItems()>0 ) {
2288                 RI = Repr->ItemsValue(1);
2289               }
2290             }
2291           }
2292           if(RI.IsNull()) continue;
2293
2294           if(theEnt->IsKind(STANDARD_TYPE(StepShape_DimensionalSize))) {
2295             // read dimensions
2296             Handle(StepShape_EdgeCurve) EC = Handle(StepShape_EdgeCurve)::DownCast(RI);
2297             if(EC.IsNull()) continue;
2298             Handle(TCollection_HAsciiString) aName;
2299             Handle(StepShape_DimensionalSize) DimSize = 
2300               Handle(StepShape_DimensionalSize)::DownCast(theEnt);
2301             Standard_Real dim1=-1.,dim2=-1.;
2302             subs4 = aGraph.Sharings(DimSize);
2303             for(subs4.Start(); subs4.More(); subs4.Next()) {
2304               Handle(StepShape_DimensionalCharacteristicRepresentation) DimCharR = 
2305                 Handle(StepShape_DimensionalCharacteristicRepresentation)::DownCast(subs4.Value());
2306               if(!DimCharR.IsNull()) {
2307                 Handle(StepShape_ShapeDimensionRepresentation) SDimR = DimCharR->Representation();
2308                 if(!SDimR.IsNull() && SDimR->NbItems()>0) {
2309                   Handle(StepRepr_RepresentationItem) anItem = SDimR->ItemsValue(1);
2310                   Handle(StepRepr_ValueRange) VR = Handle(StepRepr_ValueRange)::DownCast(anItem);
2311                   if(!VR.IsNull()) {
2312                     aName = VR->Name();
2313                     //StepRepr_CompoundItemDefinition CID = VR->ItemElement();
2314                     //if(CID.IsNull()) continue;
2315                     //Handle(StepRepr_CompoundItemDefinitionMember) CIDM = 
2316                     //  Handle(StepRepr_CompoundItemDefinitionMember)::DownCast(CID.Value());
2317                     //if(CIDM.IsNull()) continue;
2318                     //if(CIDM->ArrTransient().IsNull()) continue;
2319                     //Handle(StepRepr_HArray1OfRepresentationItem) HARI;
2320                     //if(CID.CaseMem(CIDM)==1)
2321                     //  HARI = CID.ListRepresentationItem();
2322                     //if(CID.CaseMem(CIDM)==2)
2323                     //  HARI = CID.SetRepresentationItem();
2324                     Handle(StepRepr_HArray1OfRepresentationItem) HARI = VR->ItemElement();
2325                     if(HARI.IsNull()) continue;
2326                     if(HARI->Length()>0) {
2327                       Handle(StepRepr_RepresentationItem) RI1 = HARI->Value(1);
2328                       if(RI1.IsNull()) continue;
2329                       if(RI1->IsKind(STANDARD_TYPE(StepRepr_ReprItemAndLengthMeasureWithUnit))) {
2330                         Handle(StepRepr_ReprItemAndLengthMeasureWithUnit) RILMWU =
2331                           Handle(StepRepr_ReprItemAndLengthMeasureWithUnit)::DownCast(RI1);
2332                         dim1 = RILMWU->GetMeasureWithUnit()->ValueComponent();
2333                         StepBasic_Unit anUnit = RILMWU->GetMeasureWithUnit()->UnitComponent();
2334                         Standard_Real afact=1.;
2335                         if(anUnit.IsNull()) continue;
2336                         if( !(anUnit.CaseNum(anUnit.Value())==1) ) continue;
2337                         Handle(StepBasic_NamedUnit) NU = anUnit.NamedUnit();
2338                         if(GetLengthConversionFactor(NU,afact)) dim1=dim1*afact;
2339                       }
2340                     }
2341                     if(HARI->Length()>1) {
2342                       Handle(StepRepr_RepresentationItem) RI2 = HARI->Value(2);
2343                       if(RI2.IsNull()) continue;
2344                       if(RI2->IsKind(STANDARD_TYPE(StepRepr_ReprItemAndLengthMeasureWithUnit))) {
2345                         Handle(StepRepr_ReprItemAndLengthMeasureWithUnit) RILMWU =
2346                           Handle(StepRepr_ReprItemAndLengthMeasureWithUnit)::DownCast(RI2);
2347                         dim2 = RILMWU->GetMeasureWithUnit()->ValueComponent();
2348                         StepBasic_Unit anUnit = RILMWU->GetMeasureWithUnit()->UnitComponent();
2349                         Standard_Real afact=1.;
2350                         if(anUnit.IsNull()) continue;
2351                         if( !(anUnit.CaseNum(anUnit.Value())==1) ) continue;
2352                         Handle(StepBasic_NamedUnit) NU = anUnit.NamedUnit();
2353                         if(GetLengthConversionFactor(NU,afact)) dim2 = dim2*afact;
2354                       }
2355                     }
2356                   }
2357                 }
2358               }
2359             }
2360             if(dim1<0) continue;
2361             if(dim2<0) dim2=dim1;
2362             //cout<<"DimensionalSize: dim1="<<dim1<<"  dim2="<<dim2<<endl;
2363             // now we know edge_curve and value range therefore
2364             // we can create corresponding D&GT labels
2365             Standard_Integer index = aTP->MapIndex(EC);
2366             TopoDS_Shape aSh;
2367             if(index >0) {
2368               Handle(Transfer_Binder) binder = aTP->MapItem(index);
2369               aSh = TransferBRep::ShapeResult(binder);
2370             }
2371             if(aSh.IsNull()) continue; 
2372             TDF_Label shL;
2373             if( !aSTool->Search(aSh, shL, Standard_True, Standard_True, Standard_True) ) continue;
2374             Handle(TColStd_HArray1OfReal) arr = new TColStd_HArray1OfReal(1,2);
2375             arr->SetValue(1,dim1);
2376             arr->SetValue(2,dim2);
2377             aDGTTool->SetDimTol(shL,1,arr,aName,DimSize->Name());
2378           }
2379           // read tolerances and datums
2380           else if(theEnt->IsKind(STANDARD_TYPE(StepDimTol_GeometricTolerance))) {
2381             Handle(StepDimTol_GeometricTolerance) GT =
2382               Handle(StepDimTol_GeometricTolerance)::DownCast(theEnt);
2383             // read common data for tolerance
2384             //Standard_Real dim = GT->Magnitude()->ValueComponent();
2385             Handle (StepBasic_MeasureWithUnit) dim3 = GT->Magnitude();
2386             if(dim3.IsNull()) continue;
2387             Standard_Real dim = dim3->ValueComponent();
2388             StepBasic_Unit anUnit = GT->Magnitude()->UnitComponent();
2389             Standard_Real afact=1.;
2390             if(anUnit.IsNull()) continue;
2391             if( !(anUnit.CaseNum(anUnit.Value())==1) ) continue;
2392             Handle(StepBasic_NamedUnit) NU = anUnit.NamedUnit();
2393             if(GetLengthConversionFactor(NU,afact)) dim = dim*afact;
2394             //cout<<"GeometricTolerance: Magnitude = "<<dim<<endl;
2395             Handle(TColStd_HArray1OfReal) arr = new TColStd_HArray1OfReal(1,1);
2396             arr->SetValue(1,dim);
2397             Handle(TCollection_HAsciiString) aName = GT->Name();
2398             Handle(TCollection_HAsciiString) aDescription = GT->Description();
2399             Handle(StepShape_AdvancedFace) AF = Handle(StepShape_AdvancedFace)::DownCast(RI);
2400             if(AF.IsNull()) continue;
2401             Standard_Integer index = aTP->MapIndex(AF);
2402             TopoDS_Shape aSh;
2403             if(index >0) {
2404               Handle(Transfer_Binder) binder = aTP->MapItem(index);
2405               aSh = TransferBRep::ShapeResult(binder);
2406             }
2407             if(aSh.IsNull()) continue; 
2408             TDF_Label shL;
2409             if( !aSTool->Search(aSh, shL, Standard_True, Standard_True, Standard_True) ) continue;
2410             // read specific data for tolerance
2411             if(GT->IsKind(STANDARD_TYPE(StepDimTol_GeoTolAndGeoTolWthDatRefAndModGeoTolAndPosTol))) {
2412               Handle(StepDimTol_GeoTolAndGeoTolWthDatRefAndModGeoTolAndPosTol) GTComplex =
2413                 Handle(StepDimTol_GeoTolAndGeoTolWthDatRefAndModGeoTolAndPosTol)::DownCast(theEnt);
2414               Standard_Integer kind=20;
2415               Handle(StepDimTol_ModifiedGeometricTolerance) MGT = 
2416                 GTComplex->GetModifiedGeometricTolerance();
2417               if(!MGT.IsNull()) {
2418                 kind = kind + MGT->Modifier()+1;
2419               }
2420               TDF_Label TolerL = aDGTTool->SetDimTol(shL,kind,arr,aName,aDescription);
2421               // translate datums connected with this tolerance
2422               Handle(StepDimTol_GeometricToleranceWithDatumReference) GTWDR =
2423                 GTComplex->GetGeometricToleranceWithDatumReference();
2424               if(!GTWDR.IsNull()) {
2425                 ReadDatums(aSTool,aDGTTool,aGraph,aTP,TolerL,GTWDR);
2426               }
2427             }
2428             else if(GT->IsKind(STANDARD_TYPE(StepDimTol_GeometricToleranceWithDatumReference))) {
2429               Handle(StepDimTol_GeometricToleranceWithDatumReference) GTWDR =
2430                 Handle(StepDimTol_GeometricToleranceWithDatumReference)::DownCast(theEnt);
2431               if(GTWDR.IsNull()) continue;
2432               Standard_Integer kind = 0;
2433               if     (GTWDR->IsKind(STANDARD_TYPE(StepDimTol_AngularityTolerance)))       kind = 24;
2434               else if(GTWDR->IsKind(STANDARD_TYPE(StepDimTol_CircularRunoutTolerance)))   kind = 25;
2435               else if(GTWDR->IsKind(STANDARD_TYPE(StepDimTol_CoaxialityTolerance)))       kind = 26;
2436               else if(GTWDR->IsKind(STANDARD_TYPE(StepDimTol_ConcentricityTolerance)))    kind = 27;
2437               else if(GTWDR->IsKind(STANDARD_TYPE(StepDimTol_ParallelismTolerance)))      kind = 28;
2438               else if(GTWDR->IsKind(STANDARD_TYPE(StepDimTol_PerpendicularityTolerance))) kind = 29;
2439               else if(GTWDR->IsKind(STANDARD_TYPE(StepDimTol_SymmetryTolerance)))         kind = 30;
2440               else if(GTWDR->IsKind(STANDARD_TYPE(StepDimTol_TotalRunoutTolerance)))      kind = 31;
2441               //cout<<"GTWDR: kind="<<kind<<endl;
2442               TDF_Label TolerL = aDGTTool->SetDimTol(shL,kind,arr,aName,aDescription);
2443               ReadDatums(aSTool,aDGTTool,aGraph,aTP,TolerL,GTWDR);
2444             }
2445             else if(GT->IsKind(STANDARD_TYPE(StepDimTol_ModifiedGeometricTolerance))) {
2446               Handle(StepDimTol_ModifiedGeometricTolerance) MGT =
2447                 Handle(StepDimTol_ModifiedGeometricTolerance)::DownCast(theEnt);
2448               Standard_Integer kind = 35 + MGT->Modifier();
2449               aDGTTool->SetDimTol(shL,kind,arr,aName,aDescription);
2450             }
2451             else if(GT->IsKind(STANDARD_TYPE(StepDimTol_CylindricityTolerance))) {
2452               aDGTTool->SetDimTol(shL,38,arr,aName,aDescription);
2453             }
2454             else if(GT->IsKind(STANDARD_TYPE(StepDimTol_FlatnessTolerance))) {
2455               aDGTTool->SetDimTol(shL,39,arr,aName,aDescription);
2456             }
2457             else if(GT->IsKind(STANDARD_TYPE(StepDimTol_LineProfileTolerance))) {
2458               aDGTTool->SetDimTol(shL,40,arr,aName,aDescription);
2459             }
2460             else if(GT->IsKind(STANDARD_TYPE(StepDimTol_PositionTolerance))) {
2461               aDGTTool->SetDimTol(shL,41,arr,aName,aDescription);
2462             }
2463             else if(GT->IsKind(STANDARD_TYPE(StepDimTol_RoundnessTolerance))) {
2464               aDGTTool->SetDimTol(shL,42,arr,aName,aDescription);
2465             }
2466             else if(GT->IsKind(STANDARD_TYPE(StepDimTol_StraightnessTolerance))) {
2467               aDGTTool->SetDimTol(shL,43,arr,aName,aDescription);
2468             }
2469             else if(GT->IsKind(STANDARD_TYPE(StepDimTol_SurfaceProfileTolerance))) {
2470               aDGTTool->SetDimTol(shL,44,arr,aName,aDescription);
2471             }
2472           }
2473         }
2474         else
2475         {
2476           if(aGISU.IsNull()) continue;
2477           if(aGISU->NbIdentifiedItem() > 0) {
2478             aSeqRI.Append(aGISU->IdentifiedItemValue(1));
2479           }
2480         }
2481       }
2482       if(!aSeqRI.IsEmpty())
2483       {
2484         if(aSeqRI1.IsEmpty())
2485           aSeqRI1 = aSeqRI;
2486         else 
2487           aSeqRI2 = aSeqRI;
2488       }
2489     }
2490   }
2491   if(aSeqRI1.IsEmpty()) return aGDTL;
2492
2493   TDF_LabelSequence aShLS1, aShLS2;
2494
2495   for(Standard_Integer i = aSeqRI1.Lower(); i <= aSeqRI1.Upper() ;i++)
2496   {
2497     Standard_Integer anIndex = aTP->MapIndex(aSeqRI1.Value(i));
2498     TopoDS_Shape aSh;
2499     if(anIndex >0) {
2500       Handle(Transfer_Binder) aBinder = aTP->MapItem(anIndex);
2501       aSh = TransferBRep::ShapeResult(aBinder);
2502     }
2503     if(!aSh.IsNull())
2504     {
2505       TDF_Label aShL;
2506       aSTool->Search(aSh, aShL, Standard_True, Standard_True, Standard_True);
2507       aShLS1.Append(aShL);
2508     }
2509   }
2510   if (!aSeqRI2.IsEmpty())
2511   {
2512     //for dimensional location
2513     for(Standard_Integer i = aSeqRI2.Lower(); i <= aSeqRI2.Upper() ;i++)
2514     {
2515       Standard_Integer anIndex = aTP->MapIndex(aSeqRI2.Value(i));
2516       TopoDS_Shape aSh;
2517       if(anIndex >0) {
2518         Handle(Transfer_Binder) aBinder = aTP->MapItem(anIndex);
2519         aSh = TransferBRep::ShapeResult(aBinder);
2520       }
2521       if(!aSh.IsNull())
2522       {
2523         TDF_Label aShL;
2524         aSTool->Search(aSh, aShL, Standard_True, Standard_True, Standard_True);
2525         aShLS2.Append(aShL);
2526       }
2527     }
2528   }
2529
2530   if(!aShLS1.IsEmpty())
2531   {
2532     // add to XCAF
2533     if(!theEnt->IsKind(STANDARD_TYPE(StepDimTol_GeometricTolerance)))
2534     {
2535       aGDTL = aDGTTool->AddDimension();
2536       Handle(XCAFDoc_Dimension) aDim = XCAFDoc_Dimension::Set(aGDTL);
2537       TCollection_AsciiString aStr("DGT:Dimensional_");
2538       if(theEnt->IsKind(STANDARD_TYPE(StepShape_DimensionalSize)))
2539       {
2540         aStr.AssignCat("Size");
2541       }
2542       else if(theEnt->IsKind(STANDARD_TYPE(StepShape_DimensionalLocation)))
2543       {
2544         aStr.AssignCat("Location");
2545       }
2546       TDataStd_Name::Set(aGDTL, aStr);
2547
2548       if(!aShLS2.IsEmpty())
2549       {
2550         aDGTTool->SetDimension(aShLS1, aShLS2, aGDTL);
2551       }
2552       else
2553       {
2554         TDF_LabelSequence aEmptySeq;
2555         aDGTTool->SetDimension(aShLS1, aEmptySeq, aGDTL);
2556       }
2557     }
2558     else
2559     {
2560       aGDTL = aDGTTool->AddGeomTolerance();
2561       Handle(XCAFDoc_GeomTolerance) aGTol = XCAFDoc_GeomTolerance::Set(aGDTL);
2562       TCollection_AsciiString aStr("DGT:GeomTolerance");
2563       TDataStd_Name::Set(aGDTL, aStr);
2564       aDGTTool->SetGeomTolerance(aShLS1, aGDTL);
2565       Handle(XCAFDimTolObjects_GeomToleranceObject) anObj = aGTol->GetObject();
2566       if(isAllAround)
2567         anObj->AddModifier(XCAFDimTolObjects_GeomToleranceModif_All_Around);
2568       else if(isAllOver)
2569         anObj->AddModifier(XCAFDimTolObjects_GeomToleranceModif_All_Over);
2570       aGTol->SetObject(anObj);
2571     }
2572     readDatumsAP242(theEnt, aGDTL, theDoc, theWS);
2573   }
2574   return aGDTL;
2575 }
2576
2577 //=======================================================================
2578 //function : setDimObjectToXCAF
2579 //purpose  : 
2580 //=======================================================================
2581 static void setDimObjectToXCAF(const Handle(Standard_Transient)& theEnt,
2582                                            const TDF_Label& aDimL,
2583                                            const Handle(TDocStd_Document)& theDoc,
2584                                            const Handle(XSControl_WorkSession)& theWS)
2585 {
2586   Handle(XCAFDoc_ShapeTool) aSTool = XCAFDoc_DocumentTool::ShapeTool( theDoc->Main() );
2587   Handle(XCAFDoc_DimTolTool) aDGTTool = XCAFDoc_DocumentTool::DimTolTool( theDoc->Main() );
2588   Handle(XSControl_TransferReader) aTR = theWS->TransferReader();
2589   Handle(Transfer_TransientProcess) aTP = aTR->TransientProcess();
2590   const Interface_Graph& aGraph = aTP->Graph();
2591   Handle(XCAFDimTolObjects_DimensionObject) aDimObj;
2592   if(!theEnt->IsKind(STANDARD_TYPE(StepShape_DimensionalSize)) &&
2593     !theEnt->IsKind(STANDARD_TYPE(StepShape_DimensionalLocation))) 
2594     return;
2595
2596   Handle(StepShape_DimensionalSize) aDimSize = 
2597     Handle(StepShape_DimensionalSize)::DownCast(theEnt);
2598   Handle(StepShape_DimensionalLocation) aDimLocation = 
2599     Handle(StepShape_DimensionalLocation)::DownCast(theEnt);
2600
2601   Standard_Real aDim1=-1.,aDim2=-1.,aDim3=-1.;
2602   Handle(StepShape_TypeQualifier) aTQ;
2603   Handle(StepShape_ValueFormatTypeQualifier) aVFTQ;
2604   Handle(StepShape_ToleranceValue) aTV;
2605   Handle(StepShape_LimitsAndFits) aLAF;
2606   Handle(StepRepr_CompoundRepresentationItem) aCRI;
2607   Handle(StepGeom_Axis2Placement3d) anAP;
2608
2609   Interface_EntityIterator anIterDim;
2610   if(!aDimSize.IsNull())
2611   {
2612     anIterDim = aGraph.Sharings(aDimSize);
2613   }
2614   else
2615   {
2616     anIterDim = aGraph.Sharings(aDimLocation);
2617   }
2618   for(anIterDim.Start(); anIterDim.More(); anIterDim.Next()) {
2619     Handle(StepShape_DimensionalCharacteristicRepresentation) aDCR = 
2620       Handle(StepShape_DimensionalCharacteristicRepresentation)::DownCast(anIterDim.Value());
2621     Handle(StepShape_PlusMinusTolerance) aPMT = 
2622       Handle(StepShape_PlusMinusTolerance)::DownCast(anIterDim.Value());
2623     if(!aDCR.IsNull()) {
2624       Handle(StepShape_ShapeDimensionRepresentation) aSDR = aDCR->Representation();
2625       if(!aSDR.IsNull()) {
2626         Handle(StepRepr_HArray1OfRepresentationItem) aHARI = aSDR->Items();
2627
2628         if(!aHARI.IsNull())
2629         {
2630           for(Standard_Integer nr = aHARI->Lower(); nr <= aHARI->Upper(); nr++)
2631           {
2632             Handle(StepRepr_RepresentationItem) aDRI = aHARI->Value(nr);
2633             if(aDRI.IsNull()) continue;
2634
2635             if(aDRI->IsKind(STANDARD_TYPE(StepRepr_ReprItemAndLengthMeasureWithUnit))) {
2636               //get values
2637               Handle(StepRepr_ReprItemAndLengthMeasureWithUnit) aRILMWU =
2638                 Handle(StepRepr_ReprItemAndLengthMeasureWithUnit)::DownCast(aDRI);
2639               Standard_Real aVal = aRILMWU->GetMeasureWithUnit()->ValueComponent();
2640               StepBasic_Unit anUnit = aRILMWU->GetMeasureWithUnit()->UnitComponent();
2641               Standard_Real aFact=1.;
2642               if(anUnit.IsNull()) continue;
2643               if( !(anUnit.CaseNum(anUnit.Value())==1) ) continue;
2644               Handle(StepBasic_NamedUnit) NU = anUnit.NamedUnit();
2645               if(GetLengthConversionFactor(NU,aFact)) aVal=aVal*aFact;
2646               if(aDim2 > 0)
2647               {
2648                 if(aDim1 > aDim2)
2649                 {
2650                   Standard_Real aTmp = aDim1;
2651                   aDim1 = aDim2;
2652                   aDim2 = aTmp;
2653                 }
2654                 if(aVal < aDim1)
2655                   aDim1 = aVal;
2656                 else if(aVal > aDim2)
2657                   aDim2 = aVal;
2658               }
2659               else if(aDim1 > 0)
2660               {
2661                 if (aVal > aDim1)
2662                   aDim2 = aVal;
2663                 else
2664                 {
2665                   aDim2 = aDim1;
2666                   aDim1 = aVal;
2667                 }
2668               }
2669               else
2670                 aDim1 = aVal;
2671             }
2672             else if(aDRI->IsKind(STANDARD_TYPE(StepRepr_ReprItemAndPlaneAngleMeasureWithUnit))) {
2673               //get values
2674               Handle(StepRepr_ReprItemAndPlaneAngleMeasureWithUnit) aRIPAMWU =
2675                 Handle(StepRepr_ReprItemAndPlaneAngleMeasureWithUnit)::DownCast(aDRI);
2676               Standard_Real aVal = aRIPAMWU->GetMeasureWithUnit()->ValueComponent();
2677               StepBasic_Unit anUnit = aRIPAMWU->GetMeasureWithUnit()->UnitComponent();
2678               Standard_Real aFact=1.;
2679               if(anUnit.IsNull()) continue;
2680               if( !(anUnit.CaseNum(anUnit.Value())==1) ) continue;
2681               Handle(StepBasic_NamedUnit) NU = anUnit.NamedUnit();
2682               if(GetAngleConversionFactor(NU,aFact)) aVal=aVal*aFact;
2683               if(aDim2 > 0)
2684               {
2685                 if(aDim1 > aDim2)
2686                 {
2687                   Standard_Real aTmp = aDim1;
2688                   aDim1 = aDim2;
2689                   aDim2 = aTmp;
2690                 }
2691                 if(aVal < aDim1)
2692                   aDim1 = aVal;
2693                 else if(aVal > aDim2)
2694                   aDim2 = aVal;
2695               }
2696               else if(aDim1 > 0)
2697               {
2698                 if (aVal > aDim1)
2699                   aDim2 = aVal;
2700                 else
2701                 {
2702                   aDim2 = aDim1;
2703                   aDim1 = aVal;
2704                 }
2705               }
2706               else
2707                 aDim1 = aVal;
2708             }
2709             else if(aDRI->IsKind(STANDARD_TYPE(StepShape_QualifiedRepresentationItem))) {
2710               //get qualifier
2711               Handle(StepShape_QualifiedRepresentationItem) aQRI =
2712                 Handle(StepShape_QualifiedRepresentationItem)::DownCast(aDRI);
2713               for(Standard_Integer l = 1; l <= aQRI->NbQualifiers(); l++)
2714               {
2715                 aTQ = aQRI->Qualifiers()->Value(l).TypeQualifier();
2716                 aVFTQ = aQRI->Qualifiers()->Value(l).ValueFormatTypeQualifier();
2717               }
2718             }
2719             else if(aDRI->IsKind(STANDARD_TYPE(StepRepr_CompoundRepresentationItem))) {
2720               aCRI = Handle(StepRepr_CompoundRepresentationItem)::DownCast(aDRI);
2721             }
2722             else if(aDRI->IsKind(STANDARD_TYPE(StepGeom_Axis2Placement3d)))
2723             {
2724               anAP = Handle(StepGeom_Axis2Placement3d)::DownCast(aDRI);
2725             }
2726           }
2727         }
2728       }
2729     }
2730     else if (!aPMT.IsNull())
2731     {
2732       StepShape_ToleranceMethodDefinition aTMD = aPMT->Range();
2733       if(aPMT.IsNull()) continue;
2734       if(aTMD.CaseNumber() == 1)
2735         //! 1 -> ToleranceValue from StepShape
2736         //! 2 -> LimitsAndFits from StepShape
2737       {
2738         //plus minus tolerance 
2739         aTV = aTMD.ToleranceValue();
2740         if (aTV.IsNull()) continue;
2741
2742         Standard_Real aVal = aTV->UpperBound()->ValueComponent();
2743         StepBasic_Unit anUnit = aTV->UpperBound()->UnitComponent();
2744         Standard_Real aFact=1.;
2745         if(anUnit.IsNull()) continue;
2746         if( !(anUnit.CaseNum(anUnit.Value())==1) ) continue;
2747         Handle(StepBasic_NamedUnit) NU = anUnit.NamedUnit();
2748         if(GetLengthConversionFactor(NU,aFact)) aVal=aVal*aFact;
2749         aDim3 = aVal;
2750
2751         aVal = aTV->LowerBound()->ValueComponent();
2752         anUnit = aTV->LowerBound()->UnitComponent();
2753         aFact=1.;
2754         if(anUnit.IsNull()) continue;
2755         if( !(anUnit.CaseNum(anUnit.Value())==1) ) continue;
2756         NU = anUnit.NamedUnit();
2757         if(GetLengthConversionFactor(NU,aFact)) aVal=aVal*aFact;
2758         aDim2 = Abs(aVal);
2759       }
2760       else
2761       {
2762         // calss of tolerance
2763         aLAF = aTMD.LimitsAndFits();
2764       }
2765     }
2766   }
2767
2768   if(aDim1<0) return;
2769
2770   aDimObj = new XCAFDimTolObjects_DimensionObject();
2771   if(aDim2 < 0)
2772     aDimObj->SetValue(aDim1);
2773   else if (aDim3 < 0)
2774   {
2775     Handle(TColStd_HArray1OfReal) anArr = new TColStd_HArray1OfReal(1,2);
2776     anArr->SetValue(1,aDim1);
2777     anArr->SetValue(2,aDim2);
2778     aDimObj->SetValues(anArr);
2779   }
2780   else
2781   {
2782     Handle(TColStd_HArray1OfReal) anArr = new TColStd_HArray1OfReal(1,3);
2783     anArr->SetValue(1,aDim1);
2784     anArr->SetValue(2,aDim2);
2785     anArr->SetValue(3,aDim3);
2786     aDimObj->SetValues(anArr);
2787   }
2788   if(!aTQ.IsNull()) 
2789   {
2790     XCAFDimTolObjects_DimensionQualifier aQ;
2791     if (STEPCAFControl_GDTProperty::GetDimQualifierType(aTQ->Name(), aQ))
2792     {
2793         aDimObj->SetQualifier(aQ);
2794     }
2795   }
2796
2797   if (!aVFTQ.IsNull())
2798   {
2799     //A typical value would be 'NR2 2.2'
2800     TCollection_HAsciiString aFormat = aVFTQ->FormatType();
2801     Standard_Integer i = aFormat.Location(1,' ',1,aFormat.Length());
2802     aFormat.SubString(i+1,i+1)->IntegerValue();
2803     aDimObj->SetNbOfDecimalPlaces(aFormat.SubString(i+1,i+1)->IntegerValue(),
2804       aFormat.SubString(i+3,i+3)->IntegerValue());
2805   }
2806
2807   if (!aLAF.IsNull())
2808   { 
2809     //get class of tolerance
2810     Standard_Boolean aHolle = Standard_False;
2811     XCAFDimTolObjects_DimensionFormVariance aFV = XCAFDimTolObjects_DimensionFormVariance_None;
2812     XCAFDimTolObjects_DimensionGrade aG = XCAFDimTolObjects_DimensionGrade_IT01;
2813     STEPCAFControl_GDTProperty::GetDimClassOfTolerance(aLAF, aHolle, aFV, aG);
2814     aDimObj->SetClassOfTolerance(aHolle, aFV, aG);
2815   }
2816
2817   if (!aCRI.IsNull() && aCRI->ItemElement()->Length() > 0)
2818   {
2819     //get modifiers
2820     XCAFDimTolObjects_DimensionModifiersSequence aModifiers;
2821     STEPCAFControl_GDTProperty::GetDimModifiers(aCRI, aModifiers);
2822     if(aModifiers.Length() > 0)
2823       aDimObj->SetModifiers(aModifiers);
2824   }
2825
2826   Handle(TCollection_HAsciiString) aName;
2827   if(!aDimSize.IsNull())
2828   {
2829     aName = aDimSize->Name();
2830   }
2831   else
2832   {
2833     aName = aDimLocation->Name();
2834   }
2835   XCAFDimTolObjects_DimensionType aType = XCAFDimTolObjects_DimensionType_Location_None;
2836   if (!STEPCAFControl_GDTProperty::GetDimType(aName, aType))
2837   {
2838     if(!aDimSize.IsNull())
2839     {
2840       Handle(StepShape_AngularSize) anAngSize= 
2841         Handle(StepShape_AngularSize)::DownCast(aDimSize);
2842       if(!anAngSize.IsNull())
2843       {
2844         //get qualifier for angular value
2845         aType = XCAFDimTolObjects_DimensionType_Size_Angular;
2846         if(anAngSize->AngleSelection() == StepShape_Equal)
2847           aDimObj->SetQualifier(XCAFDimTolObjects_DimensionQualifier_Avg);
2848         else if (anAngSize->AngleSelection() == StepShape_Large)
2849           aDimObj->SetQualifier(XCAFDimTolObjects_DimensionQualifier_Max);
2850         else if (anAngSize->AngleSelection() == StepShape_Small)
2851           aDimObj->SetQualifier(XCAFDimTolObjects_DimensionQualifier_Min);
2852       }
2853     }
2854     else
2855     {
2856       Handle(StepShape_AngularLocation) anAngLoc= 
2857         Handle(StepShape_AngularLocation)::DownCast(aDimLocation);
2858       if(!anAngLoc.IsNull())
2859       {
2860         //get qualifier for angular value
2861         aType = XCAFDimTolObjects_DimensionType_Location_Angular;
2862         if(anAngLoc->AngleSelection() == StepShape_Equal)
2863           aDimObj->SetQualifier(XCAFDimTolObjects_DimensionQualifier_Avg);
2864         else if (anAngLoc->AngleSelection() == StepShape_Large)
2865           aDimObj->SetQualifier(XCAFDimTolObjects_DimensionQualifier_Max);
2866         else if (anAngLoc->AngleSelection() == StepShape_Small)
2867           aDimObj->SetQualifier(XCAFDimTolObjects_DimensionQualifier_Min);
2868       }
2869     }
2870     if(aType == XCAFDimTolObjects_DimensionType_Location_None)
2871     {
2872       Handle(StepRepr_ShapeAspect) aPSA;
2873       if(!aDimSize.IsNull())
2874       {
2875         Handle(StepShape_DimensionalSizeWithPath) aDimSizeWithPath= 
2876           Handle(StepShape_DimensionalSizeWithPath)::DownCast(aDimSize);
2877         if (!aDimSizeWithPath.IsNull())
2878         {
2879           aType = XCAFDimTolObjects_DimensionType_Size_WithPath;
2880           aPSA = aDimSizeWithPath->Path();
2881         }
2882       }
2883       else
2884       {
2885         Handle(StepShape_DimensionalLocationWithPath) aDimLocWithPath= 
2886           Handle(StepShape_DimensionalLocationWithPath)::DownCast(aDimLocation);
2887         if (!aDimLocWithPath.IsNull())
2888         {
2889           aType = XCAFDimTolObjects_DimensionType_Location_WithPath;
2890           aPSA = aDimLocWithPath->Path();
2891         }
2892       }
2893
2894       if(!aPSA.IsNull())
2895       {
2896         //for DimensionalLocationWithPath
2897         Handle(StepGeom_GeometricRepresentationItem) aGRI;
2898         Handle(StepAP242_GeometricItemSpecificUsage) aPGISU;
2899         Interface_EntityIterator anIterDSWP = aGraph.Sharings(aPSA);
2900         for(anIterDSWP.Start(); anIterDSWP.More() && aPGISU.IsNull(); anIterDSWP.Next()) {
2901           aPGISU = Handle(StepAP242_GeometricItemSpecificUsage)::DownCast(anIterDSWP.Value());
2902         }
2903         if(aPGISU.IsNull()) return;
2904         if(aPGISU->NbIdentifiedItem() > 0) {
2905           aGRI = Handle(StepGeom_GeometricRepresentationItem)::DownCast(aPGISU->IdentifiedItemValue(1));
2906         }
2907         if(aGRI.IsNull()) return;
2908         Handle(StepRepr_RepresentationItem) aPRI;
2909         Interface_EntityIterator anIterGRI = aGraph.Sharings(aGRI);
2910         for(anIterGRI.Start(); anIterGRI.More() && aPGISU.IsNull(); anIterGRI.Next()) {
2911           aPRI = Handle(StepRepr_RepresentationItem)::DownCast(anIterGRI.Value());
2912         }
2913         Standard_Integer anIndex = aTP->MapIndex(aPRI);
2914         TopoDS_Edge aSh;
2915         if(anIndex >0) {
2916           Handle(Transfer_Binder) aBinder = aTP->MapItem(anIndex);
2917           aSh = TopoDS::Edge(TransferBRep::ShapeResult(aBinder));
2918         }
2919         if(aSh.IsNull()) return; 
2920         aDimObj->SetPath(aSh);
2921       }
2922       else if(!anAP.IsNull())
2923       {
2924         if(anAP->Name()->String().IsEqual("orientation") && !anAP->Axis().IsNull())
2925         {
2926           //for Oriented Dimensional Location
2927           Handle(TColStd_HArray1OfReal) aDirArr = anAP->RefDirection()->DirectionRatios();
2928           gp_Dir aDir;
2929           if(!aDirArr.IsNull() && aDirArr->Length() > 2)
2930           {
2931             aDir.SetCoord(aDirArr->Lower(), aDirArr->Lower()+1, aDirArr->Lower()+2);
2932             aDimObj->SetDirection(aDir);
2933           }
2934           else if(aDirArr->Length() > 1)
2935           {
2936             aDir.SetCoord(aDirArr->Lower(), aDirArr->Lower()+1, 0);
2937             aDimObj->SetDirection(aDir);
2938           }
2939         }
2940       }
2941     }
2942   }
2943   aDimObj->SetType(aType);
2944
2945   if(!aDimObj.IsNull())
2946   {
2947     Handle(XCAFDoc_Dimension) aDim;
2948     if(aDimL.FindAttribute(XCAFDoc_Dimension::GetID(),aDim))
2949     {
2950       aDim->SetObject(aDimObj);
2951     }
2952   }
2953 }
2954
2955 //=======================================================================
2956 //function : getTolType
2957 //purpose  : 
2958 //=======================================================================
2959 static Standard_Boolean getTolType(const Handle(Standard_Transient)& theEnt,
2960                        XCAFDimTolObjects_GeomToleranceType& theType)
2961 {
2962   if(!theEnt->IsKind(STANDARD_TYPE(StepDimTol_GeometricTolerance))) 
2963     return Standard_False;
2964   theType = XCAFDimTolObjects_GeomToleranceType_None;
2965   if (theEnt->IsKind(STANDARD_TYPE(StepDimTol_GeoTolAndGeoTolWthDatRef)))
2966   {
2967     Handle(StepDimTol_GeoTolAndGeoTolWthDatRef) anE = Handle(StepDimTol_GeoTolAndGeoTolWthDatRef)::DownCast(theEnt);
2968     theType = STEPCAFControl_GDTProperty::GetGeomToleranceType(anE->GetToleranceType());
2969   }
2970   else if (theEnt->IsKind(STANDARD_TYPE(StepDimTol_GeoTolAndGeoTolWthDatRefAndGeoTolWthMaxTol)))
2971   {
2972     Handle(StepDimTol_GeoTolAndGeoTolWthDatRefAndGeoTolWthMaxTol) anE = 
2973       Handle(StepDimTol_GeoTolAndGeoTolWthDatRefAndGeoTolWthMaxTol)::DownCast(theEnt);
2974     theType = STEPCAFControl_GDTProperty::GetGeomToleranceType(anE->GetToleranceType());
2975   }
2976   else if (theEnt->IsKind(STANDARD_TYPE(StepDimTol_GeoTolAndGeoTolWthDatRefAndGeoTolWthMod)))
2977   {
2978     Handle(StepDimTol_GeoTolAndGeoTolWthDatRefAndGeoTolWthMod) anE = 
2979       Handle(StepDimTol_GeoTolAndGeoTolWthDatRefAndGeoTolWthMod)::DownCast(theEnt);
2980     theType = STEPCAFControl_GDTProperty::GetGeomToleranceType(anE->GetToleranceType());
2981   }
2982   else if (theEnt->IsKind(STANDARD_TYPE(StepDimTol_GeoTolAndGeoTolWthMaxTol)))
2983   {
2984     Handle(StepDimTol_GeoTolAndGeoTolWthMaxTol) anE = 
2985       Handle(StepDimTol_GeoTolAndGeoTolWthMaxTol)::DownCast(theEnt);
2986     theType = STEPCAFControl_GDTProperty::GetGeomToleranceType(anE->GetToleranceType());
2987   }
2988   else if (theEnt->IsKind(STANDARD_TYPE(StepDimTol_GeoTolAndGeoTolWthMod)))
2989   {
2990     Handle(StepDimTol_GeoTolAndGeoTolWthMod) anE = 
2991       Handle(StepDimTol_GeoTolAndGeoTolWthMod)::DownCast(theEnt);
2992     theType = STEPCAFControl_GDTProperty::GetGeomToleranceType(anE->GetToleranceType());
2993   }
2994   else if (theEnt->IsKind(STANDARD_TYPE(StepDimTol_GeoTolAndGeoTolWthDatRefAndUneqDisGeoTol)))
2995   {
2996     Handle(StepDimTol_GeoTolAndGeoTolWthDatRefAndUneqDisGeoTol) anE = 
2997       Handle(StepDimTol_GeoTolAndGeoTolWthDatRefAndUneqDisGeoTol)::DownCast(theEnt);
2998     theType = STEPCAFControl_GDTProperty::GetGeomToleranceType(anE->GetToleranceType());
2999   }
3000   else if(theEnt->IsKind(STANDARD_TYPE(StepDimTol_AngularityTolerance)))
3001   {
3002     theType = XCAFDimTolObjects_GeomToleranceType_Angularity;
3003   }
3004   else if(theEnt->IsKind(STANDARD_TYPE(StepDimTol_CircularRunoutTolerance)))
3005   {
3006     theType = XCAFDimTolObjects_GeomToleranceType_CircularRunout;
3007   }
3008   else if(theEnt->IsKind(STANDARD_TYPE(StepDimTol_RoundnessTolerance)))
3009   {
3010     theType = XCAFDimTolObjects_GeomToleranceType_CircularityOrRoundness;
3011   }
3012   else if(theEnt->IsKind(STANDARD_TYPE(StepDimTol_CoaxialityTolerance)))
3013   {
3014     theType = XCAFDimTolObjects_GeomToleranceType_Coaxiality;
3015   }
3016   else if(theEnt->IsKind(STANDARD_TYPE(StepDimTol_ConcentricityTolerance)))
3017   {
3018     theType = XCAFDimTolObjects_GeomToleranceType_Concentricity;
3019   }
3020   else if(theEnt->IsKind(STANDARD_TYPE(StepDimTol_CylindricityTolerance)))
3021   {
3022     theType = XCAFDimTolObjects_GeomToleranceType_Cylindricity;
3023   }
3024   else if(theEnt->IsKind(STANDARD_TYPE(StepDimTol_FlatnessTolerance)))
3025   {
3026     theType = XCAFDimTolObjects_GeomToleranceType_Flatness;
3027   }
3028   else if(theEnt->IsKind(STANDARD_TYPE(StepDimTol_ParallelismTolerance)))
3029   {
3030     theType = XCAFDimTolObjects_GeomToleranceType_Parallelism;
3031   }
3032   else if(theEnt->IsKind(STANDARD_TYPE(StepDimTol_PerpendicularityTolerance)))
3033   {
3034     theType = XCAFDimTolObjects_GeomToleranceType_Perpendicularity;
3035   }
3036   else if(theEnt->IsKind(STANDARD_TYPE(StepDimTol_PositionTolerance)) || 
3037      theEnt->IsKind(STANDARD_TYPE(StepDimTol_GeoTolAndGeoTolWthDatRefAndModGeoTolAndPosTol)))
3038   {
3039     theType = XCAFDimTolObjects_GeomToleranceType_Position;
3040   }
3041   else if(theEnt->IsKind(STANDARD_TYPE(StepDimTol_LineProfileTolerance)))
3042   {
3043     theType = XCAFDimTolObjects_GeomToleranceType_ProfileOfLine;
3044   }
3045   else if(theEnt->IsKind(STANDARD_TYPE(StepDimTol_SurfaceProfileTolerance)))
3046   {
3047     theType = XCAFDimTolObjects_GeomToleranceType_ProfileOfSurface;
3048   }
3049   else if(theEnt->IsKind(STANDARD_TYPE(StepDimTol_StraightnessTolerance)))
3050   {
3051     theType = XCAFDimTolObjects_GeomToleranceType_Straightness;
3052   }
3053   else if(theEnt->IsKind(STANDARD_TYPE(StepDimTol_SymmetryTolerance)))
3054   {
3055     theType = XCAFDimTolObjects_GeomToleranceType_Symmetry;
3056   }
3057   else if(theEnt->IsKind(STANDARD_TYPE(StepDimTol_TotalRunoutTolerance)))
3058   {
3059     theType = XCAFDimTolObjects_GeomToleranceType_TotalRunout;
3060   }
3061   return Standard_True;
3062 }
3063 //=======================================================================
3064 //function : setGeomTolObjectToXCAF
3065 //purpose  : 
3066 //=======================================================================
3067 static void setGeomTolObjectToXCAF(const Handle(Standard_Transient)& theEnt,
3068                                            const TDF_Label& theTolL,
3069                                            const Handle(TDocStd_Document)& theDoc,
3070                                            const Handle(XSControl_WorkSession)& theWS)
3071 {
3072   Handle(XCAFDoc_ShapeTool) aSTool = XCAFDoc_DocumentTool::ShapeTool( theDoc->Main() );
3073   Handle(XCAFDoc_DimTolTool) aDGTTool = XCAFDoc_DocumentTool::DimTolTool( theDoc->Main() );
3074   Handle(XSControl_TransferReader) aTR = theWS->TransferReader();
3075   Handle(Transfer_TransientProcess) aTP = aTR->TransientProcess();
3076   const Interface_Graph& aGraph = aTP->Graph();
3077   Handle(XCAFDoc_GeomTolerance) aGTol;
3078   if(!theTolL.FindAttribute(XCAFDoc_GeomTolerance::GetID(), aGTol))
3079   {
3080     return;
3081   }
3082   Handle(XCAFDimTolObjects_GeomToleranceObject) aTolObj = aGTol->GetObject();
3083   Handle(StepDimTol_GeometricTolerance) aTolEnt = Handle(StepDimTol_GeometricTolerance)::DownCast(theEnt);
3084
3085   XCAFDimTolObjects_GeomToleranceType aType = XCAFDimTolObjects_GeomToleranceType_None;
3086   getTolType(theEnt, aType);
3087   aTolObj->SetType(aType);
3088   if(!aTolEnt->Magnitude().IsNull()){
3089     //get value
3090     Standard_Real aVal = aTolEnt->Magnitude()->ValueComponent();
3091     StepBasic_Unit anUnit = aTolEnt->Magnitude()->UnitComponent();
3092     Standard_Real aFact=1.;
3093     if(anUnit.IsNull()) return;
3094     if( !(anUnit.CaseNum(anUnit.Value())==1) ) return;
3095     Handle(StepBasic_NamedUnit) NU = anUnit.NamedUnit();
3096     if(GetLengthConversionFactor(NU,aFact))
3097       aVal=aVal*aFact;
3098     aTolObj->SetValue(aVal);
3099   }
3100   //get modifiers
3101   XCAFDimTolObjects_GeomToleranceTypeValue aTypeV = XCAFDimTolObjects_GeomToleranceTypeValue_None;
3102   Interface_EntityIterator anIter = aGraph.Sharings(aTolEnt);
3103   for(anIter.Start(); anIter.More(); anIter.Next()) {
3104     if(anIter.Value()->IsKind(STANDARD_TYPE(StepDimTol_ToleranceZone))){
3105       Handle(StepDimTol_ToleranceZoneForm) aForm 
3106                             = Handle(StepDimTol_ToleranceZone)::DownCast(anIter.Value())->Form();
3107       STEPCAFControl_GDTProperty::GetTolValueType(aForm->Name(), aTypeV);
3108       Interface_EntityIterator anIt = aGraph.Sharings(anIter.Value());
3109       for(anIt.Start(); anIt.More(); anIt.Next()) {
3110         if(anIt.Value()->IsKind(STANDARD_TYPE(StepDimTol_ProjectedZoneDefinition))){
3111           Handle(StepDimTol_ProjectedZoneDefinition) aPZone
3112                               = Handle(StepDimTol_ProjectedZoneDefinition)::DownCast(anIt.Value());
3113           if(!aPZone->ProjectionLength().IsNull())
3114           {
3115             Standard_Real aVal = aPZone->ProjectionLength()->ValueComponent();
3116             StepBasic_Unit anUnit = aPZone->ProjectionLength()->UnitComponent();
3117             Standard_Real aFact=1.;
3118             if(anUnit.IsNull()) return;
3119             if( !(anUnit.CaseNum(anUnit.Value())==1) ) return;
3120             Handle(StepBasic_NamedUnit) NU = anUnit.NamedUnit();
3121             if(GetLengthConversionFactor(NU,aFact))
3122               aVal=aVal*aFact;
3123             aTolObj->SetValueOfZoneModifier(aVal);
3124             aTolObj->SetZoneModifier(XCAFDimTolObjects_GeomToleranceZoneModif_Projected);
3125           }
3126         }
3127         else if (anIt.Value()->IsKind(STANDARD_TYPE(StepDimTol_RunoutZoneDefinition)))
3128         {
3129           Handle(StepDimTol_RunoutZoneDefinition) aRZone
3130                                 = Handle(StepDimTol_RunoutZoneDefinition)::DownCast(anIt.Value());
3131           if(!aRZone->Orientation().IsNull())
3132           {
3133             Standard_Real aVal = aRZone->Orientation()->Angle()->ValueComponent();
3134             StepBasic_Unit anUnit = aRZone->Orientation()->Angle()->UnitComponent();
3135             Standard_Real aFact=1.;
3136             if(anUnit.IsNull()) continue;
3137             if( !(anUnit.CaseNum(anUnit.Value())==1) ) continue;
3138             Handle(StepBasic_NamedUnit) NU = anUnit.NamedUnit();
3139             if(GetAngleConversionFactor(NU,aFact)) aVal=aVal*aFact;
3140             aTolObj->SetValueOfZoneModifier(aVal);
3141             aTolObj->SetZoneModifier(XCAFDimTolObjects_GeomToleranceZoneModif_Runout);
3142           }
3143         }
3144       }
3145       aTolObj->SetTypeOfValue(aTypeV);
3146     }
3147   }
3148   Handle(StepDimTol_HArray1OfGeometricToleranceModifier) aModifiers;
3149   if (aTolEnt->IsKind(STANDARD_TYPE(StepDimTol_GeometricToleranceWithModifiers)))
3150   {
3151     aModifiers = Handle(StepDimTol_GeometricToleranceWithModifiers)::DownCast(aTolEnt)->Modifiers();
3152   }
3153   else if (aTolEnt->IsKind(STANDARD_TYPE(StepDimTol_GeoTolAndGeoTolWthDatRefAndGeoTolWthMod)))
3154   {
3155     aModifiers = Handle(StepDimTol_GeoTolAndGeoTolWthDatRefAndGeoTolWthMod)
3156                             ::DownCast(aTolEnt)->GetGeometricToleranceWithModifiers()->Modifiers();
3157   }
3158   else if (aTolEnt->IsKind(STANDARD_TYPE(StepDimTol_GeoTolAndGeoTolWthMod)))
3159   {
3160     aModifiers = Handle(StepDimTol_GeoTolAndGeoTolWthMod)
3161                              ::DownCast(aTolEnt)->GetGeometricToleranceWithModifiers()->Modifiers();
3162   }
3163   else if (aTolEnt->IsKind(STANDARD_TYPE(StepDimTol_GeoTolAndGeoTolWthMaxTol)))
3164   {
3165     aModifiers = Handle(StepDimTol_GeoTolAndGeoTolWthMaxTol)
3166                              ::DownCast(aTolEnt)->GetGeometricToleranceWithModifiers()->Modifiers();
3167   }
3168   else if (aTolEnt->IsKind(STANDARD_TYPE(StepDimTol_GeoTolAndGeoTolWthDatRefAndGeoTolWthMaxTol)))
3169   {
3170     aModifiers = Handle(StepDimTol_GeoTolAndGeoTolWthDatRefAndGeoTolWthMaxTol)
3171                              ::DownCast(aTolEnt)->GetGeometricToleranceWithModifiers()->Modifiers();
3172   }
3173   if(!aModifiers.IsNull())
3174   {
3175     for(Standard_Integer i = aModifiers->Lower(); i <= aModifiers->Upper(); i++)
3176     {
3177       if(aModifiers->Value(i) == StepDimTol_GTMLeastMaterialRequirement)
3178         aTolObj->SetMaterialRequirementModifier(XCAFDimTolObjects_GeomToleranceMatReqModif_L);
3179       else if (aModifiers->Value(i) == StepDimTol_GTMMaximumMaterialRequirement)
3180         aTolObj->SetMaterialRequirementModifier(XCAFDimTolObjects_GeomToleranceMatReqModif_M);
3181       else
3182         aTolObj->AddModifier((XCAFDimTolObjects_GeomToleranceModif)aModifiers->Value(i));
3183     }
3184   }
3185   Standard_Real aVal = 0;
3186   StepBasic_Unit anUnit;
3187   if (aTolEnt->IsKind(STANDARD_TYPE(StepDimTol_GeometricToleranceWithMaximumTolerance)))
3188   {
3189     Handle(StepDimTol_GeometricToleranceWithMaximumTolerance) aMax = Handle(StepDimTol_GeometricToleranceWithMaximumTolerance)::DownCast(aTolEnt);
3190     aVal = aMax->MaximumUpperTolerance()->ValueComponent();
3191     anUnit = aMax->MaximumUpperTolerance()->UnitComponent();
3192   }
3193   else if (aTolEnt->IsKind(STANDARD_TYPE(StepDimTol_GeoTolAndGeoTolWthMaxTol)))
3194   {
3195     Handle(StepDimTol_GeoTolAndGeoTolWthMaxTol) aMax = 
3196       Handle(StepDimTol_GeoTolAndGeoTolWthMaxTol)::DownCast(aTolEnt);
3197     aVal = aMax->GetMaxTolerance()->ValueComponent();
3198     anUnit = aMax->GetMaxTolerance()->UnitComponent();
3199   }
3200   else if (aTolEnt->IsKind(STANDARD_TYPE(StepDimTol_GeoTolAndGeoTolWthDatRefAndGeoTolWthMaxTol)))
3201   {
3202     Handle(StepDimTol_GeoTolAndGeoTolWthDatRefAndGeoTolWthMaxTol) aMax = 
3203       Handle(StepDimTol_GeoTolAndGeoTolWthDatRefAndGeoTolWthMaxTol)::DownCast(aTolEnt);
3204     aVal = aMax->GetMaxTolerance()->ValueComponent();
3205     anUnit = aMax->GetMaxTolerance()->UnitComponent();
3206   }
3207   if (!anUnit.IsNull() && (anUnit.CaseNum(anUnit.Value()) == 1))
3208   {
3209     Standard_Real aFact=1.;
3210     Handle(StepBasic_NamedUnit) NU = anUnit.NamedUnit();
3211     if(GetAngleConversionFactor(NU,aFact)) aVal=aVal*aFact;
3212     aTolObj->SetMaxValueModifier(aVal);
3213   }
3214
3215   aGTol->SetObject(aTolObj);
3216 }
3217
3218 //=======================================================================
3219 //function : ReadGDTs
3220 //purpose  : 
3221 //=======================================================================
3222
3223 Standard_Boolean STEPCAFControl_Reader::ReadGDTs(const Handle(XSControl_WorkSession)& theWS,
3224                                                  Handle(TDocStd_Document)& theDoc) const
3225 {
3226   Handle(Interface_InterfaceModel) aModel = theWS->Model();
3227   Handle(StepData_StepModel) aSM = Handle(StepData_StepModel)::DownCast(aModel);
3228   Interface_EntityIterator anI = aSM->Header();
3229   Handle(HeaderSection_FileSchema) aH;
3230   for(anI.Start(); anI.More() && aH.IsNull();anI.Next())
3231     aH = Handle(HeaderSection_FileSchema)::DownCast(anI.Value());
3232   Handle(XCAFDoc_ShapeTool) aSTool = XCAFDoc_DocumentTool::ShapeTool( theDoc->Main() );
3233   Handle(XSControl_TransferReader) aTR = theWS->TransferReader();
3234   Handle(Transfer_TransientProcess) aTP = aTR->TransientProcess();
3235   Handle(XCAFDoc_DimTolTool) aDGTTool = XCAFDoc_DocumentTool::DimTolTool( theDoc->Main() );
3236   if ( aDGTTool.IsNull() ) return Standard_False;
3237
3238   Standard_Integer nb = aModel->NbEntities();
3239   for(Standard_Integer i=1; i<=nb; i++) {