]> OCCT Git - occt-copy.git/commitdiff
0031388: Data Exchange - support kinematics data in STEP format
authordpasukhi <dpasukhi@opencascade.com>
Sun, 30 Aug 2020 15:44:33 +0000 (18:44 +0300)
committerdpasukhi <dpasukhi@opencascade.com>
Sun, 30 Aug 2020 15:44:33 +0000 (18:44 +0300)
# Reorganize STEP Write Kinematics

src/STEPCAFControl/STEPCAFControl_Writer.cxx
src/XCAFKinematics/XCAFKinematics_HighOrderPairObject.cxx

index f4716771095ff829a09dbd48d07d9ae8431e9be8..3f082b4567e9455ab06c35be9cf0cf1582c0357e 100644 (file)
 #include <XCAFDoc_KinematicPairValue.hxx>
 #include <XCAFDoc_KinematicTool.hxx>
 #include <XCAFDoc_KinematicPair.hxx>
+
+#include <StepRepr_HSequenceOfRepresentationItem.hxx>
 // End Added for kinematics implementation
 
 #include <TCollection_AsciiString.hxx>
@@ -2188,2977 +2190,2963 @@ Standard_Boolean STEPCAFControl_Writer::WriteSHUOs (const Handle(XSControl_WorkS
 }
 
 //=======================================================================
-//function : createKinematicLink
-//purpose  : auxilary
+//function : FindPDSforDGT
+//purpose  : auxilary: find PDS for AdvancedFace or EdgeCurve for creation
+//                     needed ShapeAspect in D&GT structure
 //=======================================================================
-static Standard_Boolean createKinematicLink(const Handle(XSControl_WorkSession)& theWS,
-                                            const Handle(XCAFDoc_KinematicTool)& theKTool,
-                                            const Interface_Graph& theGraph,
-                                            const TDF_Label& theLabelLink,
-                                            NCollection_IndexedDataMap<TDF_Label, Handle(StepKinematics_KinematicLinkRepresentation), TDF_LabelMapHasher>& theMapOfLinks,
-                                            Handle(StepKinematics_RigidLinkRepresentation)& theBaseLink,
-                                            Handle(StepRepr_PropertyDefinition)& theGeneralPD)
+static Standard_Boolean FindPDSforDGT(const Interface_Graph &aGraph,
+                                      const Handle(Standard_Transient) &ent,
+                                      Handle(StepRepr_ProductDefinitionShape) &PDS,
+                                      Handle(StepRepr_RepresentationContext) &RC,
+                                      Handle(StepShape_AdvancedFace) &AF,
+                                      Handle(StepShape_EdgeCurve) &EC)
 {
-  const Handle(Interface_InterfaceModel)& Model = theWS->Model();
-  const Handle(XSControl_TransferWriter)& TW = theWS->TransferWriter();
-  const Handle(Transfer_FinderProcess)& FP = TW->FinderProcess();
-  Handle(StepKinematics_KinematicLink) aLink = new StepKinematics_KinematicLink;
-
-  Handle(TDataStd_Name) aNameLink;
-  Handle(TCollection_HAsciiString) aHNameLink;
-  if (theLabelLink.FindAttribute(TDataStd_Name::GetID(), aNameLink))
-    aHNameLink = new TCollection_HAsciiString(aNameLink->Get());
-  aLink->Init(aHNameLink);
-
-  //Choose type of the representation link
-  Standard_Boolean isLinear = Standard_True;
-  TDF_LabelSequence aJoints = theKTool->GetJointsOfLink(theLabelLink, Standard_True, Standard_False);
-  if (aJoints.IsEmpty())
-    isLinear = Standard_False;
-  for (TDF_LabelSequence::Iterator anItJoint(aJoints); anItJoint.More() && isLinear; anItJoint.Next())
-  {
-    Handle(XCAFDoc_KinematicPair) aKPairAttr;
-    if (!anItJoint.Value().FindAttribute(XCAFDoc_KinematicPair::GetID(), aKPairAttr))
-      continue;
-    Handle(XCAFKinematics_PairObject) aPairObject = aKPairAttr->GetObject();
-    if (aPairObject->Type() == XCAFKinematics_PairType_NoType)
-      continue;
-    if (aPairObject->Type() != XCAFKinematics_PairType_LinearFlexibleAndPinion &&
-        aPairObject->Type() != XCAFKinematics_PairType_LinearFlexibleAndPlanarCurve)
-      isLinear = Standard_False;
-  }
-  aJoints = theKTool->GetJointsOfLink(theLabelLink, Standard_False, Standard_True);
-  if (!aJoints.IsEmpty())
-    isLinear = Standard_False;
-
-  Handle(StepKinematics_KinematicLinkRepresentation) aLinkRepr;
-  Handle(TCollection_HAsciiString) aNameOfLinkRepr = new TCollection_HAsciiString("");
-  // create empty collection for updating during creating a referenced pairs
-  Handle(StepRepr_HArray1OfRepresentationItem) aPlacementsOfPairs = new StepRepr_HArray1OfRepresentationItem();
-
-  TDF_LabelSequence aShapesL = theKTool->GetRefShapes(theLabelLink);
-  if (aShapesL.IsEmpty())
+  if (ent.IsNull())
+    return Standard_False;
+  if( !ent->IsKind(STANDARD_TYPE(StepShape_EdgeCurve)) && 
+      !ent->IsKind(STANDARD_TYPE(StepShape_AdvancedFace)) ) 
     return Standard_False;
 
-  // find ref shapes
-  for (TDF_LabelSequence::Iterator anItShapes(aShapesL); anItShapes.More(); anItShapes.Next()) {
-    TopoDS_Shape aTopoShape = XCAFDoc_ShapeTool::GetShape(anItShapes.Value());
-    TopLoc_Location aLoc;
-    TColStd_SequenceOfTransient seqRI;
-    FindEntities(FP, aTopoShape, aLoc, seqRI);
-    if (seqRI.Length() <= 0) {
-      FP->Messenger()->SendInfo() << "Warning: Cannot find RI for " << aTopoShape.TShape()->DynamicType()->Name() << std::endl;
-      continue;
+  AF = Handle(StepShape_AdvancedFace)::DownCast(ent);
+  if( AF.IsNull() ) {
+    EC = Handle(StepShape_EdgeCurve)::DownCast(ent);
+    Interface_EntityIterator subs = aGraph.Sharings(EC);
+    for(subs.Start(); subs.More() && AF.IsNull(); subs.Next()) {
+      Handle(StepShape_OrientedEdge) OE = Handle(StepShape_OrientedEdge)::DownCast(subs.Value());
+      if(OE.IsNull()) continue;
+      Interface_EntityIterator subs1 = aGraph.Sharings(OE);
+      for(subs1.Start(); subs1.More() && AF.IsNull(); subs1.Next()) {
+        Handle(StepShape_EdgeLoop) EL = Handle(StepShape_EdgeLoop)::DownCast(subs1.Value());
+        if(EL.IsNull()) continue;
+        Interface_EntityIterator subs2 = aGraph.Sharings(EL);
+        for(subs2.Start(); subs2.More() && AF.IsNull(); subs2.Next()) {
+          Handle(StepShape_FaceBound) FB = Handle(StepShape_FaceBound)::DownCast(subs2.Value());
+          if(FB.IsNull()) continue;
+          Interface_EntityIterator subs3 = aGraph.Sharings(FB);
+          for(subs3.Start(); subs3.More() && AF.IsNull(); subs3.Next()) {
+            AF = Handle(StepShape_AdvancedFace)::DownCast(subs3.Value());
+          }
+        }
+      }
     }
+  }
+  if(AF.IsNull()) return Standard_False;
 
-    // get PDS of Shape
-    Handle(StepShape_ShapeRepresentation) aShapeRepr;
-    Handle(StepRepr_PropertyDefinition) aPD;
-    Interface_EntityIterator anItRI = theGraph.Sharings(seqRI.Value(1));
-    for (anItRI.Start(); anItRI.More() && aPD.IsNull(); anItRI.Next()) {
-      aShapeRepr = Handle(StepShape_ShapeRepresentation)::DownCast(anItRI.Value());
-      if (aShapeRepr.IsNull())
-        continue;
-      Interface_EntityIterator aSDRIt = theGraph.Sharings(aShapeRepr);
-      for (aSDRIt.Start(); aSDRIt.More() && aPD.IsNull(); aSDRIt.Next()) {
-        Handle(StepShape_ShapeDefinitionRepresentation) aSDR =
-          Handle(StepShape_ShapeDefinitionRepresentation)::DownCast(aSDRIt.Value());
-        if (aSDR.IsNull()) continue;
-        aPD = aSDR->Definition().PropertyDefinition();
+  Interface_EntityIterator subs = aGraph.Sharings(AF);
+  for(subs.Start(); subs.More() && PDS.IsNull(); subs.Next()) {
+    Handle(StepShape_ConnectedFaceSet) CFS =
+      Handle(StepShape_ConnectedFaceSet)::DownCast(subs.Value());
+    if(CFS.IsNull()) continue;
+    Interface_EntityIterator subs1 = aGraph.Sharings(CFS);
+    for(subs1.Start(); subs1.More() && PDS.IsNull(); subs1.Next()) {
+      Handle(StepRepr_RepresentationItem) RI = 
+        Handle(StepRepr_RepresentationItem)::DownCast(subs1.Value());
+      if(RI.IsNull()) continue;
+      Interface_EntityIterator subs2 = aGraph.Sharings(RI);
+      for(subs2.Start(); subs2.More() && PDS.IsNull(); subs2.Next()) {
+        Handle(StepShape_ShapeRepresentation) SR = 
+          Handle(StepShape_ShapeRepresentation)::DownCast(subs2.Value());
+        if(SR.IsNull()) continue;
+        RC = SR->ContextOfItems();
+        Interface_EntityIterator subs3 = aGraph.Sharings(SR);
+        for(subs3.Start(); subs3.More() && PDS.IsNull(); subs3.Next()) {
+          Handle(StepShape_ShapeDefinitionRepresentation) SDR = 
+            Handle(StepShape_ShapeDefinitionRepresentation)::DownCast(subs3.Value());
+          if(SDR.IsNull()) continue;
+          Handle(StepRepr_PropertyDefinition) PropD = SDR->Definition().PropertyDefinition();
+          if(PropD.IsNull()) continue;
+          PDS = Handle(StepRepr_ProductDefinitionShape)::DownCast(PropD);
+        }
       }
     }
-    if (aPD.IsNull())
-      return Standard_False;
+  }
+  
+  return Standard_True;
+}
 
-    if (theGeneralPD.IsNull() && !isLinear)
-      theGeneralPD = aPD;
+//=======================================================================
+//function : FindPDS
+//purpose  : auxilary: find Product_definition_shape entity for given entity
+//=======================================================================
+static Handle(StepRepr_ProductDefinitionShape) FindPDS(const Interface_Graph &theGraph,
+                                                       const Handle(Standard_Transient) &theEnt,
+                                                       Handle(StepRepr_RepresentationContext) &theRC)
+{
+  if (theEnt.IsNull())
+    return NULL;
+  Handle(StepRepr_ProductDefinitionShape) aPDS;
 
-    Handle(TDataStd_Integer) aBase;
-    if (theBaseLink.IsNull() && theLabelLink.FindAttribute(TDataStd_Integer::GetID(), aBase)) {
-      theGeneralPD = aPD;
-      theBaseLink = Handle(StepKinematics_RigidLinkRepresentation)::DownCast(aLinkRepr);
+  // try to find shape_representation in sharings
+  Interface_EntityIterator anIter = theGraph.Sharings(theEnt);
+  for (anIter.Start(); anIter.More() && aPDS.IsNull(); anIter.Next()) {
+    Handle(StepShape_ShapeRepresentation) aSR = Handle(StepShape_ShapeRepresentation)::DownCast(anIter.Value());
+    if (aSR.IsNull())
+      continue;
+    theRC = aSR->ContextOfItems();
+    Interface_EntityIterator aSDRIt = theGraph.Sharings(aSR);
+    for (aSDRIt.Start(); aSDRIt.More() && aPDS.IsNull(); aSDRIt.Next()) {
+      Handle(StepShape_ShapeDefinitionRepresentation) aSDR =
+        Handle(StepShape_ShapeDefinitionRepresentation)::DownCast(aSDRIt.Value());
+      if (aSDR.IsNull()) continue;
+      Handle(StepRepr_PropertyDefinition) aPropD = aSDR->Definition().PropertyDefinition();
+      if (aPropD.IsNull()) continue;
+      aPDS = Handle(StepRepr_ProductDefinitionShape)::DownCast(aPropD);
     }
+  }
+  if (!aPDS.IsNull())
+    return aPDS;
 
-    if (aLinkRepr.IsNull())
+  anIter = theGraph.Sharings(theEnt);
+  for (anIter.Start(); anIter.More(); anIter.Next()) {
+    if (anIter.Value()->IsKind(STANDARD_TYPE(StepShape_TopologicalRepresentationItem)) ||
+      anIter.Value()->IsKind(STANDARD_TYPE(StepGeom_GeometricRepresentationItem)))
     {
-      if (isLinear)
-        aLinkRepr = new StepKinematics_LinearFlexibleLinkRepresentation;
-      else
-        aLinkRepr = new StepKinematics_RigidLinkRepresentation;
-      aLinkRepr->Init(aNameOfLinkRepr, aPlacementsOfPairs, aShapeRepr->ContextOfItems(), aLink);
-      theMapOfLinks.Add(theLabelLink, aLinkRepr);
-      Model->AddEntity(aLink);
-      Model->AddEntity(aLinkRepr);
+      aPDS = FindPDS(theGraph, anIter.Value(), theRC);
+      if (!aPDS.IsNull())
+        return aPDS;
     }
-
-    Handle(StepKinematics_KinematicLinkRepresentationAssociation) aLinkRepresentationAssociation = new StepKinematics_KinematicLinkRepresentationAssociation;
-    aLinkRepresentationAssociation->Init(aNameOfLinkRepr, aNameOfLinkRepr, aLinkRepr, aShapeRepr);
-    Handle(StepKinematics_ContextDependentKinematicLinkRepresentation) aCDKLRS = new StepKinematics_ContextDependentKinematicLinkRepresentation;
-    Handle(StepKinematics_ProductDefinitionRelationshipKinematics) aPDRK = new StepKinematics_ProductDefinitionRelationshipKinematics;
-    aPDRK->Init(aNameOfLinkRepr, Standard_False, aLinkRepresentationAssociation->Description(), aPD->Definition());
-    aCDKLRS->Init(aLinkRepresentationAssociation, aPDRK);
-    Model->AddEntity(aLinkRepresentationAssociation);
-    Model->AddEntity(aPDRK);
-    Model->AddEntity(aCDKLRS);
   }
 
-  return Standard_True;
+  return aPDS;
 }
 
 //=======================================================================
-//function : createKinematicJoint
-//purpose  : auxilary
+//function : GetUnit
+//purpose  : auxiliary
 //=======================================================================
-static Standard_Boolean createKinematicJoint(const Handle(XCAFDoc_KinematicTool)& theKTool,
-                                             const TDF_Label& theLabelJoint,
-                                             const Handle(XCAFKinematics_PairObject)& theKinPairObj,
-                                             const NCollection_IndexedDataMap<TDF_Label, Handle(StepKinematics_KinematicLinkRepresentation), TDF_LabelMapHasher>& theMapOfLinks,
-                                             Handle(StepKinematics_KinematicLinkRepresentation)& theLinkRepr1,
-                                             Handle(StepKinematics_KinematicLinkRepresentation)& theLinkRepr2,
-                                             Handle(StepKinematics_KinematicJoint)& theJoint)
+static StepBasic_Unit GetUnit(const Handle(StepRepr_RepresentationContext)& theRC,
+                              const Standard_Boolean isAngle = Standard_False)
 {
-  TDF_Label aLinkStartL, aLinkEndL;
-  theJoint = new StepKinematics_KinematicJoint;
-  theKTool->GetLinksOfJoint(theLabelJoint, aLinkStartL, aLinkEndL);
-  Handle(TCollection_HAsciiString) aNameJoint = new TCollection_HAsciiString(theKinPairObj->Name());
-
-  Handle(StepShape_Vertex) anEdgeStart;
-  Handle(StepShape_Vertex) anEdgeEnd;
-
-  if (!theMapOfLinks.FindFromKey(aLinkStartL, theLinkRepr1))
-    return Standard_False;
-  if (!theMapOfLinks.FindFromKey(aLinkEndL, theLinkRepr2))
-    return Standard_False;
-
-  anEdgeStart = theLinkRepr1->RepresentedLink();
-  anEdgeEnd = theLinkRepr2->RepresentedLink();
-
-  if (anEdgeEnd.IsNull() || anEdgeStart.IsNull())
-    return Standard_False;
+  StepBasic_Unit aUnit;
+  Handle(StepBasic_NamedUnit) aCurrentUnit;
+  if (isAngle) {
+    Handle(StepGeom_GeometricRepresentationContextAndGlobalUnitAssignedContext) aCtx =
+      Handle(StepGeom_GeometricRepresentationContextAndGlobalUnitAssignedContext)::DownCast(theRC);
+    if(!aCtx.IsNull()) {
+      for(Standard_Integer j = 1; j <= aCtx->NbUnits(); j++) {
+        if (aCtx->UnitsValue(j)->IsKind(STANDARD_TYPE(StepBasic_SiUnitAndPlaneAngleUnit)) ||
+            aCtx->UnitsValue(j)->IsKind(STANDARD_TYPE(StepBasic_ConversionBasedUnitAndPlaneAngleUnit))) {
+          aCurrentUnit = aCtx->UnitsValue(j);
+          break;
+        }
+      }
+    }
+    if (aCurrentUnit.IsNull()) {
+      Handle(StepGeom_GeomRepContextAndGlobUnitAssCtxAndGlobUncertaintyAssCtx) aCtx1 =
+        Handle(StepGeom_GeomRepContextAndGlobUnitAssCtxAndGlobUncertaintyAssCtx)::DownCast(theRC);
+      if(!aCtx1.IsNull()) {
+        for(Standard_Integer j = 1; j <= aCtx1->NbUnits(); j++) {
+          if (aCtx1->UnitsValue(j)->IsKind(STANDARD_TYPE(StepBasic_SiUnitAndPlaneAngleUnit)) ||
+              aCtx1->UnitsValue(j)->IsKind(STANDARD_TYPE(StepBasic_ConversionBasedUnitAndPlaneAngleUnit))) {
+            aCurrentUnit = aCtx1->UnitsValue(j);
+            break;
+          }
+        }
+      }
+    }
+    if (aCurrentUnit.IsNull())
+      aCurrentUnit = new StepBasic_SiUnitAndPlaneAngleUnit;
+  }
+  else {
+    Handle(StepGeom_GeometricRepresentationContextAndGlobalUnitAssignedContext) aCtx =
+      Handle(StepGeom_GeometricRepresentationContextAndGlobalUnitAssignedContext)::DownCast(theRC);
+    if(!aCtx.IsNull()) {
+      for(Standard_Integer j = 1; j <= aCtx->NbUnits(); j++) {
+        if (aCtx->UnitsValue(j)->IsKind(STANDARD_TYPE(StepBasic_SiUnitAndLengthUnit)) ||
+            aCtx->UnitsValue(j)->IsKind(STANDARD_TYPE(StepBasic_ConversionBasedUnitAndLengthUnit))) {
+          aCurrentUnit = aCtx->UnitsValue(j);
+          break;
+        }
+      }
+    }
+    if (aCurrentUnit.IsNull()) {
+      Handle(StepGeom_GeomRepContextAndGlobUnitAssCtxAndGlobUncertaintyAssCtx) aCtx1 =
+        Handle(StepGeom_GeomRepContextAndGlobUnitAssCtxAndGlobUncertaintyAssCtx)::DownCast(theRC);
+      if(!aCtx1.IsNull()) {
+        for(Standard_Integer j = 1; j <= aCtx1->NbUnits(); j++) {
+          if (aCtx1->UnitsValue(j)->IsKind(STANDARD_TYPE(StepBasic_SiUnitAndLengthUnit)) ||
+              aCtx1->UnitsValue(j)->IsKind(STANDARD_TYPE(StepBasic_ConversionBasedUnitAndLengthUnit))) {
+            aCurrentUnit = aCtx1->UnitsValue(j);
+            break;
+          }
+        }
+      }
+    }
+    if (aCurrentUnit.IsNull())
+      aCurrentUnit = new StepBasic_SiUnitAndLengthUnit;
+  }
 
-  theJoint->Init(aNameJoint, anEdgeStart, anEdgeEnd);
-  return Standard_True;
+  aUnit.SetValue(aCurrentUnit);
+  return aUnit;
 }
 
 //=======================================================================
-//function : createKinematicPair
-//purpose  : auxilary
-//=======================================================================
-static Standard_Boolean createKinematicPair(const Handle(XCAFKinematics_PairObject)& theKinPairObj,
-                                            const Handle(StepKinematics_KinematicJoint)& theJoint,
-                                            const Handle(StepKinematics_KinematicLinkRepresentation)& theLinkRepr1,
-                                            const Handle(StepKinematics_KinematicLinkRepresentation)& theLinkRepr2,
-                                            Handle(StepKinematics_KinematicPair)& theKinematicPair)
+//function : CreateDimValue
+//purpose  : auxiliary
+//======================================================================
+static Handle(StepRepr_ReprItemAndMeasureWithUnit) CreateDimValue(const Standard_Real theValue,
+                                                                  const StepBasic_Unit theUnit,
+                                                                  const Handle(TCollection_HAsciiString)& theName,
+                                                                  const Standard_CString theMeasureName,
+                                                                  const Standard_Boolean isAngle,
+                                                                  const Standard_Boolean isQualified = Standard_False,
+                                                                  const Handle(StepShape_QualifiedRepresentationItem)& theQRI = NULL)
 {
-  if (theKinPairObj->Type() == XCAFKinematics_PairType_NoType)
-    return Standard_False;
-  Handle(StepRepr_RepresentationItem) aTransformItem1 = GeomToStep_MakeAxis2Placement3d(theKinPairObj->FirstTransformation()).Value();
-  Handle(StepRepr_RepresentationItem) aTransformItem2 = GeomToStep_MakeAxis2Placement3d(theKinPairObj->SecondTransformation()).Value();
-  if (aTransformItem1.IsNull() || aTransformItem2.IsNull())
-    return Standard_False;
-
-  Standard_Boolean hasDescription = Standard_False;
-  Handle(TCollection_HAsciiString) aPairName;
-  if (!theKinPairObj->Name().Search("Joint"))
-    aPairName = new TCollection_HAsciiString(theKinPairObj->Name());
-  else
-    aPairName = new TCollection_HAsciiString("");
-  Handle(TCollection_HAsciiString) aDescription;
-  Standard_Boolean isRanged = theKinPairObj->HasLimits();
-  if (theKinPairObj->IsKind(STANDARD_TYPE(XCAFKinematics_LowOrderPairObject)))
-  {
-    Handle(XCAFKinematics_LowOrderPairObject) aLowOrderPairObj = Handle(XCAFKinematics_LowOrderPairObject)::DownCast(theKinPairObj);
-    Standard_Boolean aTX = Standard_True;
-    Standard_Boolean aTY = Standard_True;
-    Standard_Boolean aTZ = Standard_True;
-    Standard_Boolean aRX = Standard_True;
-    Standard_Boolean aRY = Standard_True;
-    Standard_Boolean aRZ = Standard_True;
-    switch (theKinPairObj->Type())
-    {
-    case(XCAFKinematics_PairType_Revolute):
-    {
-      aTX = Standard_False;
-      aTY = Standard_False;
-      aTZ = Standard_False;
-      aRX = Standard_False;
-      aRY = Standard_False;
-      if (isRanged)
-      {
-        theKinematicPair = new StepKinematics_RevolutePairWithRange;
-        Standard_Real aLowerLimitActualRotation = aLowOrderPairObj->MinRotationZ();
-        Standard_Real aUpperLimitActualRotation = aLowOrderPairObj->MaxRotationZ();
-        aRZ = Standard_True;
-        Handle(StepKinematics_RevolutePairWithRange) aRevolutePairWithRange = Handle(StepKinematics_RevolutePairWithRange)::DownCast(theKinematicPair);
-        aRevolutePairWithRange->Init(aPairName, aPairName, hasDescription,
-          aDescription, aTransformItem1, aTransformItem2, theJoint,
-          aTX, aTY, aTZ, aRX, aRY, aRZ, Standard_True, aLowerLimitActualRotation, Standard_True, aUpperLimitActualRotation);
-      }
-      else
-      {
-        theKinematicPair = new StepKinematics_RevolutePair;
-        Handle(StepKinematics_RevolutePair) aRevolutePair = Handle(StepKinematics_RevolutePair)::DownCast(theKinematicPair);
-        aRevolutePair->Init(aPairName, aPairName, hasDescription,
-          aDescription, aTransformItem1, aTransformItem2, theJoint,
-          aTX, aTY, aTZ, aRX, aRY, aRZ);
-      }
-      break;
-    }
-    case(XCAFKinematics_PairType_Prismatic):
-    {
-      aTY = Standard_False;
-      aTZ = Standard_False;
-      aRX = Standard_False;
-      aRY = Standard_False;
-      aRZ = Standard_False;
-      if (isRanged)
-      {
-        theKinematicPair = new StepKinematics_PrismaticPairWithRange;
-        Handle(StepKinematics_PrismaticPairWithRange) aPrismaticPairWithRange = Handle(StepKinematics_PrismaticPairWithRange)::DownCast(theKinematicPair);
-        Standard_Real aUpperLimitActualTranslation = aLowOrderPairObj->MaxTranslationX();
-        Standard_Real aLowerLimitActualTranslation = aLowOrderPairObj->MinTranslationX();
-        aPrismaticPairWithRange->Init(aPairName, aPairName, hasDescription,
-          aDescription, aTransformItem1, aTransformItem2, theJoint,
-          aTX, aTY, aTZ, aRX, aRY, aRZ, Standard_True, aLowerLimitActualTranslation, Standard_True, aUpperLimitActualTranslation);
-      }
-      else
-      {
-        theKinematicPair = new StepKinematics_PrismaticPair;
-        Handle(StepKinematics_PrismaticPair) aPrismaticPair = Handle(StepKinematics_PrismaticPair)::DownCast(theKinematicPair);
-        aPrismaticPair->Init(aPairName, aPairName, hasDescription,
-          aDescription, aTransformItem1, aTransformItem2, theJoint,
-          aTX, aTY, aTZ, aRX, aRY, aRZ);
-      }
-      break;
-    }
-    case(XCAFKinematics_PairType_Cylindrical):
-    {
-      aTX = Standard_False;
-      aTY = Standard_False;
-      aRX = Standard_False;
-      aRY = Standard_False;
-      if (isRanged)
-      {
-        theKinematicPair = new StepKinematics_CylindricalPairWithRange;
-        Handle(StepKinematics_CylindricalPairWithRange) aCylindricalPairWithRange = Handle(StepKinematics_CylindricalPairWithRange)::DownCast(theKinematicPair);
-        Standard_Real aUpperLimitActualTranslation = aLowOrderPairObj->MaxTranslationZ();
-        Standard_Real aLowerLimitActualTranslation = aLowOrderPairObj->MinTranslationZ();
-        Standard_Real aLowerLimitActualRotation = aLowOrderPairObj->MinRotationZ();
-        Standard_Real aUpperLimitActualRotation = aLowOrderPairObj->MaxRotationZ();
-        aCylindricalPairWithRange->Init(aPairName, aPairName, hasDescription,
-          aDescription, aTransformItem1, aTransformItem2, theJoint,
-          aTX, aTY, aTZ, aRX, aRY, aRZ, Standard_True, aLowerLimitActualTranslation, Standard_True,
-          aUpperLimitActualTranslation, Standard_True, aLowerLimitActualRotation, Standard_True, aUpperLimitActualRotation);
-      }
-      else
-      {
-        theKinematicPair = new StepKinematics_CylindricalPair;
-        Handle(StepKinematics_CylindricalPair) aCylindricalPair = Handle(StepKinematics_CylindricalPair)::DownCast(theKinematicPair);
-        aCylindricalPair->Init(aPairName, aPairName, hasDescription,
-          aDescription, aTransformItem1, aTransformItem2, theJoint,
-          aTX, aTY, aTZ, aRX, aRY, aRZ);
-      }
-      break;
+  Handle(StepRepr_RepresentationItem) aReprItem = new StepRepr_RepresentationItem();
+  aReprItem->Init(new TCollection_HAsciiString(theName));
+  Handle(StepBasic_MeasureWithUnit) aMWU = new StepBasic_MeasureWithUnit();
+  Handle(StepBasic_MeasureValueMember) aValueMember = new StepBasic_MeasureValueMember();
+  aValueMember->SetName(theMeasureName);
+  aValueMember->SetReal(theValue);
+  aMWU->Init(aValueMember, theUnit);
+  if (isQualified) {
+    if (isAngle) {
+      // Angle & with qualifiers
+      Handle(StepRepr_ReprItemAndPlaneAngleMeasureWithUnitAndQRI) anItem = 
+        new StepRepr_ReprItemAndPlaneAngleMeasureWithUnitAndQRI();
+      anItem->Init(aMWU, aReprItem, theQRI);
+      return anItem;
     }
-    case(XCAFKinematics_PairType_Universal):
-    {
-      aTX = Standard_False;
-      aTY = Standard_False;
-      aTZ = Standard_False;
-      aRY = Standard_False;
-      Standard_Real theUniversalPair_InputSkewAngle = aLowOrderPairObj->SkewAngle();
-      if (isRanged)
-      {
-        theKinematicPair = new StepKinematics_UniversalPairWithRange;
-        Handle(StepKinematics_UniversalPairWithRange) anUniversalPairWithRange = Handle(StepKinematics_UniversalPairWithRange)::DownCast(theKinematicPair);
-        Standard_Real aUpperLimitActualRotationX = aLowOrderPairObj->MaxRotationX();
-        Standard_Real aLowerLimitActualRotationX = aLowOrderPairObj->MinRotationX();
-        Standard_Real aUpperLimitActualRotationZ = aLowOrderPairObj->MaxRotationZ();
-        Standard_Real aLowerLimitActualRotationZ = aLowOrderPairObj->MinRotationZ();
-        anUniversalPairWithRange->Init(aPairName, aPairName, hasDescription,
-          aDescription, aTransformItem1, aTransformItem2, theJoint,
-          aTX, aTY, aTZ, aRX, aRY, aRZ, Standard_True, theUniversalPair_InputSkewAngle, Standard_True, aLowerLimitActualRotationX, Standard_True,
-          aUpperLimitActualRotationX, Standard_True, aLowerLimitActualRotationZ, Standard_True, aUpperLimitActualRotationZ);
-      }
-      else
-      {
-        theKinematicPair = new StepKinematics_UniversalPair;
-        Handle(StepKinematics_UniversalPair) anUniversalPair = Handle(StepKinematics_UniversalPair)::DownCast(theKinematicPair);
-        anUniversalPair->Init(aPairName, aPairName, hasDescription,
-          aDescription, aTransformItem1, aTransformItem2, theJoint,
-          aTX, aTY, aTZ, aRX, aRY, aRZ, Standard_True, theUniversalPair_InputSkewAngle);
-      }
-      break;
+    else {
+      // Length & with qualifiers
+      Handle(StepRepr_ReprItemAndLengthMeasureWithUnitAndQRI) anItem = 
+        new StepRepr_ReprItemAndLengthMeasureWithUnitAndQRI();
+      anItem->Init(aMWU, aReprItem, theQRI);
+      return anItem;
     }
-    case(XCAFKinematics_PairType_SphericalWithPin):
-    {
-      aTX = Standard_False;
-      aTY = Standard_False;
-      aTZ = Standard_False;
-      aRX = Standard_False;
-      aRY = Standard_False;
-      if (isRanged)
-      {
-        Standard_Real aUpperLimitYaw = aLowOrderPairObj->MaxRotationZ();
-        Standard_Real aLowerLimitYaw = aLowOrderPairObj->MinRotationZ();
-        Standard_Real aUpperLimitRoll = aLowOrderPairObj->MaxRotationX();
-        Standard_Real aLowerLimitRoll = aLowOrderPairObj->MinRotationX();
-        theKinematicPair = new StepKinematics_SphericalPairWithPinAndRange;
-        Handle(StepKinematics_SphericalPairWithPinAndRange) aSphericalPairWithPinAndRange = Handle(StepKinematics_SphericalPairWithPinAndRange)::DownCast(theKinematicPair);
-        aSphericalPairWithPinAndRange->Init(aPairName, aPairName, hasDescription,
-          aDescription, aTransformItem1, aTransformItem2, theJoint,
-          aTX, aTY, aTZ, aRX, aRY, aRZ, Standard_True, aLowerLimitYaw, Standard_True,
-          aUpperLimitYaw, Standard_True, aLowerLimitRoll, Standard_True, aUpperLimitRoll);
-      }
-      else
-      {
-        theKinematicPair = new StepKinematics_SphericalPairWithPin;
-        Handle(StepKinematics_SphericalPairWithPin) aSphericalPairWithPin = Handle(StepKinematics_SphericalPairWithPin)::DownCast(theKinematicPair);
-        aSphericalPairWithPin->Init(aPairName, aPairName, hasDescription,
-          aDescription, aTransformItem1, aTransformItem2, theJoint,
-          aTX, aTY, aTZ, aRX, aRY, aRZ);
-      }
-      break;
+  }
+  else {
+    if (isAngle) {
+      // Angle & without qualifiers
+      Handle(StepRepr_ReprItemAndPlaneAngleMeasureWithUnit) anItem = 
+        new StepRepr_ReprItemAndPlaneAngleMeasureWithUnit();
+      anItem->Init(aMWU, aReprItem);
+      return anItem;
     }
-    case(XCAFKinematics_PairType_Spherical):
-    {
-      aTX = Standard_False;
-      aTY = Standard_False;
-      aTZ = Standard_False;
-      if (isRanged)
-      {
-        Standard_Real aUpperLimitYaw = aLowOrderPairObj->MaxRotationZ();
-        Standard_Real aLowerLimitYaw = aLowOrderPairObj->MinRotationZ();
-        Standard_Real aUpperLimitRoll = aLowOrderPairObj->MaxRotationX();
-        Standard_Real aLowerLimitRoll = aLowOrderPairObj->MinRotationX();
-        Standard_Real aUpperLimitPitch = aLowOrderPairObj->MaxRotationY();
-        Standard_Real aLowerLimitPitch= aLowOrderPairObj->MinRotationY();
-        theKinematicPair = new StepKinematics_SphericalPairWithRange;
-        Handle(StepKinematics_SphericalPairWithRange) aSphericalPairWithRange = Handle(StepKinematics_SphericalPairWithRange)::DownCast(theKinematicPair);
-        aSphericalPairWithRange->Init(aPairName, aPairName, hasDescription,
-          aDescription, aTransformItem1, aTransformItem2, theJoint,
-          aTX, aTY, aTZ, aRX, aRY, aRZ, Standard_True, aLowerLimitYaw, Standard_True,
-          aUpperLimitYaw, Standard_True, aLowerLimitRoll, Standard_True, aUpperLimitRoll, Standard_True, aLowerLimitPitch, Standard_True, aUpperLimitPitch);
-      }
-      else
-      {
-        theKinematicPair = new StepKinematics_SphericalPair;
-        Handle(StepKinematics_SphericalPair) aSphericalPair = Handle(StepKinematics_SphericalPair)::DownCast(theKinematicPair);
-        aSphericalPair->Init(aPairName, aPairName, hasDescription,
-          aDescription, aTransformItem1, aTransformItem2, theJoint,
-          aTX, aTY, aTZ, aRX, aRY, aRZ);
-      }
-      break;
+    else {
+      // Length & without qualifiers
+      Handle(StepRepr_ReprItemAndLengthMeasureWithUnit) anItem = 
+        new StepRepr_ReprItemAndLengthMeasureWithUnit();
+      anItem->Init(aMWU, aReprItem);
+      return anItem;
     }
-    case(XCAFKinematics_PairType_Planar):
-    {
-      aTZ = Standard_False;
-      aRX = Standard_False;
-      aRY = Standard_False;
-      if (isRanged)
-      {
-        theKinematicPair = new StepKinematics_PlanarPairWithRange;
-        Handle(StepKinematics_PlanarPairWithRange) aPlanarPairWithRange = Handle(StepKinematics_PlanarPairWithRange)::DownCast(theKinematicPair);
-        Standard_Real aUpperLimitActualTranslationX = aLowOrderPairObj->MaxTranslationX();
-        Standard_Real aLowerLimitActualTranslationX = aLowOrderPairObj->MinTranslationX();
-        Standard_Real aUpperLimitActualTranslationY = aLowOrderPairObj->MaxTranslationY();
-        Standard_Real aLowerLimitActualTranslationY = aLowOrderPairObj->MinTranslationY();
-        Standard_Real aLowerLimitActualRotation = aLowOrderPairObj->MinRotationZ();
-        Standard_Real aUpperLimitActualRotation = aLowOrderPairObj->MaxRotationZ();
-        aPlanarPairWithRange->Init(aPairName, aPairName, hasDescription,
-          aDescription, aTransformItem1, aTransformItem2, theJoint,
-          aTX, aTY, aTZ, aRX, aRY, aRZ, Standard_True, aLowerLimitActualTranslationX, Standard_True, aUpperLimitActualTranslationX, 
-          Standard_True, aLowerLimitActualTranslationY, Standard_True, aUpperLimitActualTranslationY,
-          Standard_True, aLowerLimitActualRotation, Standard_True, aUpperLimitActualRotation);
-      }
-      else
-      {
-        theKinematicPair = new StepKinematics_PlanarPair;
-        Handle(StepKinematics_PlanarPair) aPlanarPair = Handle(StepKinematics_PlanarPair)::DownCast(theKinematicPair);
-        aPlanarPair->Init(aPairName, aPairName, hasDescription,
-          aDescription, aTransformItem1, aTransformItem2, theJoint,
-          aTX, aTY, aTZ, aRX, aRY, aRZ);
-      }
-      break;
-    }
-    case(XCAFKinematics_PairType_Unconstrained):
-    {
-      theKinematicPair = new StepKinematics_LowOrderKinematicPairWithRange;
-
-      Standard_Real aUpperLimitActualTranslationX = aLowOrderPairObj->MaxTranslationX();
-      Standard_Real aLowerLimitActualTranslationX = aLowOrderPairObj->MinTranslationX();
-      Standard_Real aUpperLimitActualTranslationY = aLowOrderPairObj->MaxTranslationY();
-      Standard_Real aLowerLimitActualTranslationY = aLowOrderPairObj->MinTranslationY();
-      Standard_Real aUpperLimitActualTranslationZ = aLowOrderPairObj->MaxTranslationZ();
-      Standard_Real aLowerLimitActualTranslationZ = aLowOrderPairObj->MinTranslationZ();
-      Standard_Real aLowerLimitActualRotationX = aLowOrderPairObj->MinRotationX();
-      Standard_Real aUpperLimitActualRotationX = aLowOrderPairObj->MaxRotationX();
-      Standard_Real aLowerLimitActualRotationY = aLowOrderPairObj->MinRotationY();
-      Standard_Real aUpperLimitActualRotationY = aLowOrderPairObj->MaxRotationY();
-      Standard_Real aLowerLimitActualRotationZ = aLowOrderPairObj->MinRotationZ();
-      Standard_Real aUpperLimitActualRotationZ = aLowOrderPairObj->MaxRotationZ();
-      Handle(StepKinematics_LowOrderKinematicPairWithRange) anUnconstrainedPairWithRange = Handle(StepKinematics_LowOrderKinematicPairWithRange)::DownCast(theKinematicPair);
-      anUnconstrainedPairWithRange->Init(aPairName, aPairName, hasDescription,
-        aDescription, aTransformItem1, aTransformItem2, theJoint,
-        aTX, aTY, aTZ, aRX, aRY, aRZ, Standard_True, aLowerLimitActualRotationX, Standard_True, aUpperLimitActualRotationX,
-        Standard_True, aLowerLimitActualRotationY, Standard_True, aUpperLimitActualRotationY,
-        Standard_True, aLowerLimitActualRotationZ, Standard_True, aUpperLimitActualRotationZ,
-        Standard_True, aLowerLimitActualTranslationX, Standard_True, aUpperLimitActualTranslationX,
-        Standard_True, aLowerLimitActualTranslationY, Standard_True, aUpperLimitActualTranslationY,
-        Standard_True, aLowerLimitActualTranslationZ, Standard_True, aUpperLimitActualTranslationZ);
-      break;
-    }
-    case(XCAFKinematics_PairType_FullyConstrained):
-    {
-      theKinematicPair = new StepKinematics_FullyConstrainedPair;
-      Handle(StepKinematics_FullyConstrainedPair) aFullyConstrainedPair = Handle(StepKinematics_FullyConstrainedPair)::DownCast(theKinematicPair);
-      aFullyConstrainedPair->Init(aPairName, aPairName, hasDescription,
-        aDescription, aTransformItem1, aTransformItem2, theJoint,
-        aTX, aTY, aTZ, aRX, aRY, aRZ);
-      break;
-    }
-    case(XCAFKinematics_PairType_Homokinetic):
-    {
-      aTX = Standard_False;
-      aTY = Standard_False;
-      aTZ = Standard_False;
-      aRY = Standard_False;
-      Standard_Real InputSkewAngle = aLowOrderPairObj->SkewAngle();
-      theKinematicPair = new StepKinematics_HomokineticPair;
-      Handle(StepKinematics_HomokineticPair) aHomokineticPair = Handle(StepKinematics_HomokineticPair)::DownCast(theKinematicPair);
-      aHomokineticPair->Init(aPairName, aPairName, hasDescription,
-        aDescription, aTransformItem1, aTransformItem2, theJoint,
-        aTX, aTY, aTZ, aRX, aRY, aRZ, Standard_True, InputSkewAngle);
-      break;
-    }
-    default:
-      return Standard_False;
-    }
-  }
-  else if (theKinPairObj->IsKind(STANDARD_TYPE(XCAFKinematics_LowOrderPairObjectWithCoupling)))
-  {
-  Handle(XCAFKinematics_LowOrderPairObjectWithCoupling) aLowOrderPairObjectWithCoupling = Handle(XCAFKinematics_LowOrderPairObjectWithCoupling)::DownCast(theKinPairObj);
-    switch (theKinPairObj->Type())
-    {
-    case(XCAFKinematics_PairType_Screw):
-    {
-      Standard_Real aPitch = aLowOrderPairObjectWithCoupling->Pitch();
-      if (isRanged)
-      {
-        theKinematicPair = new StepKinematics_ScrewPairWithRange;
-        Handle(StepKinematics_ScrewPairWithRange) aScrewPairWithRange = Handle(StepKinematics_ScrewPairWithRange)::DownCast(theKinematicPair);
-        Standard_Real aLowLimit = aLowOrderPairObjectWithCoupling->LowLimit();
-        Standard_Real aUpperLimit = aLowOrderPairObjectWithCoupling->UpperLimit();
-        aScrewPairWithRange->Init(aPairName, aPairName, hasDescription,
-          aDescription, aTransformItem1, aTransformItem2, theJoint,aPitch,
-          Standard_True, aLowLimit, Standard_True, aUpperLimit);
-      }
-      else
-      {
-        theKinematicPair = new StepKinematics_ScrewPair;
-        Handle(StepKinematics_ScrewPair) aScrewPair = Handle(StepKinematics_ScrewPair)::DownCast(theKinematicPair);
-        aScrewPair->Init(aPairName, aPairName, hasDescription,
-          aDescription, aTransformItem1, aTransformItem2, theJoint,aPitch);
-      }
-      break;
-    }
-    case(XCAFKinematics_PairType_RackAndPinion):
-    {
-      Standard_Real aPinionRadius = aLowOrderPairObjectWithCoupling->PinionRadius();
-      if (isRanged)
-      {
-        theKinematicPair = new StepKinematics_RackAndPinionPairWithRange;
-        Standard_Real aLowLimit = aLowOrderPairObjectWithCoupling->LowLimit();
-        Standard_Real aUpperLimit = aLowOrderPairObjectWithCoupling->UpperLimit();
-        Handle(StepKinematics_RackAndPinionPairWithRange) aRackAndPinionPairWithRange = Handle(StepKinematics_RackAndPinionPairWithRange)::DownCast(theKinematicPair);
-        aRackAndPinionPairWithRange->Init(aPairName, aPairName, hasDescription,
-          aDescription, aTransformItem1, aTransformItem2, theJoint, aPinionRadius,
-          Standard_True, aLowLimit, Standard_True, aUpperLimit);
-      }
-      else
-      { 
-        theKinematicPair = new StepKinematics_RackAndPinionPair;
-        Handle(StepKinematics_RackAndPinionPair) aRackAndPinionPair = Handle(StepKinematics_RackAndPinionPair)::DownCast(theKinematicPair);
-        aRackAndPinionPair->Init(aPairName, aPairName, hasDescription,
-          aDescription, aTransformItem1, aTransformItem2, theJoint, aPinionRadius);
-      }
-      break;
-    }
-    case(XCAFKinematics_PairType_Gear):
-    {
-      Standard_Real aBevel = aLowOrderPairObjectWithCoupling->Bevel();
-      Standard_Real aGearRatio = aLowOrderPairObjectWithCoupling->GearRatio();
-      Standard_Real aHelicalAngle = aLowOrderPairObjectWithCoupling->HelicalAngle();
-      Standard_Real aRadiusFirstLink = aLowOrderPairObjectWithCoupling->RadiusFirstLink();;
-      Standard_Real aRadiusSecondLink = aLowOrderPairObjectWithCoupling->RadiusSecondLink();;
-      if (isRanged)
-      {
-        Standard_Real aLowLimit = aLowOrderPairObjectWithCoupling->LowLimit();
-        Standard_Real aUpperLimit = aLowOrderPairObjectWithCoupling->UpperLimit();
-        theKinematicPair = new StepKinematics_GearPairWithRange;
-        Handle(StepKinematics_GearPairWithRange) aGearPairWithRange = Handle(StepKinematics_GearPairWithRange)::DownCast(theKinematicPair);
-        aGearPairWithRange->Init(aPairName, aPairName, hasDescription,
-          aDescription, aTransformItem1, aTransformItem2, theJoint, aRadiusFirstLink, aRadiusSecondLink, aBevel, aHelicalAngle, aGearRatio,
-          Standard_True, aLowLimit, Standard_True, aUpperLimit);
-      }
-      else
-      {
-        theKinematicPair = new StepKinematics_GearPair;
-        Handle(StepKinematics_GearPair) aGearPair = Handle(StepKinematics_GearPair)::DownCast(theKinematicPair);
-        aGearPair->Init(aPairName, aPairName, hasDescription,
-          aDescription, aTransformItem1, aTransformItem2, theJoint,aRadiusFirstLink,aRadiusSecondLink,aBevel,aHelicalAngle, aGearRatio);
-      }
-      break;
-    }
-    case(XCAFKinematics_PairType_LinearFlexibleAndPinion):
-    {
-      Standard_Real aPinionRadius = aLowOrderPairObjectWithCoupling->PinionRadius();
-      theKinematicPair = new StepKinematics_LinearFlexibleAndPinionPair;
-      Handle(StepKinematics_LinearFlexibleAndPinionPair) aLinearFlexibleAndPinionPair = Handle(StepKinematics_LinearFlexibleAndPinionPair)::DownCast(theKinematicPair);
-      aLinearFlexibleAndPinionPair->Init(aPairName, aPairName, hasDescription,
-        aDescription, aTransformItem1, aTransformItem2, theJoint, aPinionRadius);
-      break;
-    }
-    default:
-      return Standard_False;
-    }
-  }
-  else if (theKinPairObj->IsKind(STANDARD_TYPE(XCAFKinematics_HighOrderPairObject)))
-  {
-  Handle(XCAFKinematics_HighOrderPairObject) aHighOrderPairObject = Handle(XCAFKinematics_HighOrderPairObject)::DownCast(theKinPairObj);
-    switch (theKinPairObj->Type())
-    {
-    case(XCAFKinematics_PairType_PointOnSurface):
-    {
-      GeomToStep_MakeSurface aMaker(aHighOrderPairObject->Surface());
-      Handle(StepGeom_Surface) aPairSurface = aMaker.Value();
-      if (isRanged && !aPairSurface.IsNull() &&
-        aPairSurface->IsKind(STANDARD_TYPE(StepGeom_RectangularTrimmedSurface)))
-      {
-        Standard_Real aLowerLimitYaw = aHighOrderPairObject->LowLimitYaw();
-        Standard_Real aUpperLimitYaw = aHighOrderPairObject->UpperLimitYaw();
-        Standard_Real aLowerLimitPitch = aHighOrderPairObject->LowLimitPitch();
-        Standard_Real aUpperLimitPitch = aHighOrderPairObject->UpperLimitPitch();
-        Standard_Real aLowerLimitRoll = aHighOrderPairObject->LowLimitRoll();
-        Standard_Real aUpperLimitRoll = aHighOrderPairObject->UpperLimitRoll();
-        GeomToStep_MakeSurface aMakerSurface(aHighOrderPairObject->Surface());
-        Handle(StepGeom_RectangularTrimmedSurface) aRangeOnPairSurface = Handle(StepGeom_RectangularTrimmedSurface)::DownCast(aMakerSurface.Value());
-        theKinematicPair = new StepKinematics_PointOnSurfacePairWithRange;
-        Handle(StepKinematics_PointOnSurfacePairWithRange) aPointOnSurfacePairWithRange = Handle(StepKinematics_PointOnSurfacePairWithRange)::DownCast(theKinematicPair);
-        aPointOnSurfacePairWithRange->Init(aPairName, aPairName, hasDescription,
-          aDescription, aTransformItem1, aTransformItem2, theJoint, aPairSurface,aRangeOnPairSurface, Standard_True, aLowerLimitYaw, Standard_True, aUpperLimitYaw, Standard_True,
-          aLowerLimitPitch, Standard_True, aUpperLimitPitch, Standard_True, aLowerLimitRoll, Standard_True, aUpperLimitRoll);
-      }
-      else 
-      {
-        theKinematicPair = new StepKinematics_PointOnSurfacePair;
-        Handle(StepKinematics_PointOnSurfacePair) aPointOnSurface = Handle(StepKinematics_PointOnSurfacePair)::DownCast(theKinematicPair);
-        aPointOnSurface->Init(aPairName, aPairName, hasDescription,
-          aDescription, aTransformItem1, aTransformItem2, theJoint, aPairSurface);
-      }
-      break;
-    }
-    case(XCAFKinematics_PairType_SlidingSurface):
-    {
-      theKinematicPair = new StepKinematics_SlidingSurfacePair;
-      GeomToStep_MakeSurface aMaker(aHighOrderPairObject->FirstSurface());
-      Handle(StepGeom_Surface) aPairSurfac1 = aMaker.Value();
-      GeomToStep_MakeSurface aMaker2(aHighOrderPairObject->SecondSurface());
-      Handle(StepGeom_Surface) aPairSurface2 = aMaker2.Value();
-      Standard_Boolean anOrientation = aHighOrderPairObject->Orientation();
-      Handle(StepKinematics_SlidingSurfacePair) aSlidingSurfacePair = Handle(StepKinematics_SlidingSurfacePair)::DownCast(theKinematicPair);
-      aSlidingSurfacePair->Init(aPairName, aPairName, hasDescription,
-        aDescription, aTransformItem1, aTransformItem2, theJoint, aPairSurfac1, aPairSurface2, anOrientation);
-      break;
-    }
-    case(XCAFKinematics_PairType_RollingSurface):
-    {
-      theKinematicPair = new StepKinematics_RollingSurfacePair;
-      GeomToStep_MakeSurface aMaker(aHighOrderPairObject->FirstSurface());
-      Handle(StepGeom_Surface) aPairSurfac1 = aMaker.Value();
-      GeomToStep_MakeSurface aMaker2(aHighOrderPairObject->SecondSurface());
-      Handle(StepGeom_Surface) aPairSurface2 = aMaker2.Value();
-      Standard_Boolean anOrientation = aHighOrderPairObject->Orientation();
-      Handle(StepKinematics_RollingSurfacePair) aRollingSurfacePair = Handle(StepKinematics_RollingSurfacePair)::DownCast(theKinematicPair);
-      aRollingSurfacePair->Init(aPairName, aPairName, hasDescription,
-        aDescription, aTransformItem1, aTransformItem2, theJoint, aPairSurfac1, aPairSurface2, anOrientation);
-      break;
-    }
-    case(XCAFKinematics_PairType_PointOnPlanarCurve):
-    {
-      GeomToStep_MakeCurve aMaker(aHighOrderPairObject->Curve());
-      Handle(StepGeom_Curve) aPairCurve = aMaker.Value();
-      Standard_Boolean anOrientation = aHighOrderPairObject->Orientation();
-      if (isRanged && !aPairCurve.IsNull() &&
-        aPairCurve->IsKind(STANDARD_TYPE(StepGeom_TrimmedCurve)))
-      {
-        Standard_Real aLowerLimitYaw = aHighOrderPairObject->LowLimitYaw();
-        Standard_Real aUpperLimitYaw = aHighOrderPairObject->UpperLimitYaw();
-        Standard_Real aLowerLimitPitch = aHighOrderPairObject->LowLimitPitch();
-        Standard_Real aUpperLimitPitch = aHighOrderPairObject->UpperLimitPitch();
-        Standard_Real aLowerLimitRoll = aHighOrderPairObject->LowLimitRoll();
-        Standard_Real aUpperLimitRoll = aHighOrderPairObject->UpperLimitRoll();
-        Handle(StepGeom_TrimmedCurve) aRangeOnPairCurve = Handle(StepGeom_TrimmedCurve)::DownCast(aPairCurve);
-        theKinematicPair = new StepKinematics_PointOnPlanarCurvePairWithRange;
-        Handle(StepKinematics_PointOnPlanarCurvePairWithRange) aPointOnPlanarCurvePairWithRange = Handle(StepKinematics_PointOnPlanarCurvePairWithRange)::DownCast(theKinematicPair);
-        aPointOnPlanarCurvePairWithRange->Init(aPairName, aPairName, hasDescription,
-          aDescription, aTransformItem1, aTransformItem2, theJoint, aPairCurve, anOrientation, aRangeOnPairCurve, Standard_True, aLowerLimitYaw, Standard_True, aUpperLimitYaw, Standard_True,
-          aLowerLimitPitch, Standard_True, aUpperLimitPitch, Standard_True, aLowerLimitRoll, Standard_True, aUpperLimitRoll);
-      }
-      else
-      {
-        theKinematicPair = new StepKinematics_PointOnPlanarCurvePair;
-        Handle(StepKinematics_PointOnPlanarCurvePair) aPointOnPlanarCurvePair = Handle(StepKinematics_PointOnPlanarCurvePair)::DownCast(theKinematicPair);
-        aPointOnPlanarCurvePair->Init(aPairName, aPairName, hasDescription,
-          aDescription, aTransformItem1, aTransformItem2, theJoint, aPairCurve, anOrientation);
-      }
-      break;
-    }
-    case(XCAFKinematics_PairType_SlidingCurve): 
-    {
-      GeomToStep_MakeCurve aMaker(aHighOrderPairObject->FirstCurve());
-      Handle(StepGeom_Curve) aPairCurve = aMaker.Value();
-      Standard_Boolean anOrientation = aHighOrderPairObject->Orientation();
-      GeomToStep_MakeCurve aMaker2(aHighOrderPairObject->SecondCurve());
-      Handle(StepGeom_Curve) aPairCurve2 = aMaker2.Value();
-      theKinematicPair = new StepKinematics_SlidingCurvePair;
-      Handle(StepKinematics_SlidingCurvePair) aSlidingCurvePair = Handle(StepKinematics_SlidingCurvePair)::DownCast(theKinematicPair);
-      aSlidingCurvePair->Init(aPairName, aPairName, hasDescription,
-        aDescription, aTransformItem1, aTransformItem2, theJoint, aPairCurve, aPairCurve2, anOrientation);
-      break;
-    }
-    case(XCAFKinematics_PairType_RollingCurve):
-    {
-      GeomToStep_MakeCurve aMaker(aHighOrderPairObject->FirstCurve());
-      Handle(StepGeom_Curve) aPairCurve = aMaker.Value();
-      Standard_Boolean anOrientation = aHighOrderPairObject->Orientation();
-      GeomToStep_MakeCurve aMaker2(aHighOrderPairObject->SecondCurve());
-      Handle(StepGeom_Curve) aPairCurve2 = aMaker2.Value();
-      theKinematicPair = new StepKinematics_RollingCurvePair;
-      Handle(StepKinematics_RollingCurvePair) aRollingCurvePair = Handle(StepKinematics_RollingCurvePair)::DownCast(theKinematicPair);
-      aRollingCurvePair->Init(aPairName, aPairName, hasDescription,
-        aDescription, aTransformItem1, aTransformItem2, theJoint, aPairCurve, aPairCurve2, anOrientation);
-      break;
-    }
-    case(XCAFKinematics_PairType_LinearFlexibleAndPlanarCurve):
-    {
-      theKinematicPair = new StepKinematics_LinearFlexibleAndPlanarCurvePair;
-      GeomToStep_MakeCurve aMaker(aHighOrderPairObject->Curve());
-      Handle(StepGeom_Curve) aPairCurve = aMaker.Value();
-      Standard_Boolean anOrientation = aHighOrderPairObject->Orientation();
-      Handle(StepKinematics_LinearFlexibleAndPlanarCurvePair) aLinearFlexibleAndPlanarCurvePair = Handle(StepKinematics_LinearFlexibleAndPlanarCurvePair)::DownCast(theKinematicPair);
-      aLinearFlexibleAndPlanarCurvePair->Init(aPairName, aPairName, hasDescription,
-        aDescription, aTransformItem1, aTransformItem2, theJoint, aPairCurve, anOrientation);
-      break;
-    }
-    default:
-      return Standard_False;
-    }
-  }
-
-  // updating placements of the both link representations
-  theLinkRepr1->Items()->Resize(theLinkRepr1->Items()->Lower(), theLinkRepr1->Items()->Upper() + 1, Standard_True);
-  theLinkRepr1->Items()->ChangeLast() = aTransformItem1;
-  theLinkRepr2->Items()->Resize(theLinkRepr2->Items()->Lower(), theLinkRepr2->Items()->Upper() + 1, Standard_True);
-  theLinkRepr2->Items()->ChangeLast() = aTransformItem2;
-
-  return Standard_True;
-}
-
-//=======================================================================
-//function : createKinematicPairValue
-//purpose  : 
-//=======================================================================
-static Standard_Boolean createKinematicPairValue(const Handle(XCAFKinematics_PairValueObject)& theKinPairValueObj,
-                                                 const Handle(StepKinematics_PairRepresentationRelationship)& thePairReprRelationship,
-                                                 Handle(StepKinematics_PairValue)& thePairValue)
-{
-  if (theKinPairValueObj->Type() == XCAFKinematics_PairType_NoType)
-    return Standard_False;
-
-  switch (theKinPairValueObj->Type())
-  {
-  case(XCAFKinematics_PairType_Revolute):
-  {
-    thePairValue = new StepKinematics_RevolutePairValue;
-    Handle(StepKinematics_RevolutePairValue) aRevolutePairValue =
-      Handle(StepKinematics_RevolutePairValue)::DownCast(thePairValue);
-    aRevolutePairValue->SetActualRotation(theKinPairValueObj->GetRotation());
-    break;
-  }
-  case(XCAFKinematics_PairType_Prismatic):
-  {
-    thePairValue = new StepKinematics_PrismaticPairValue;
-    Handle(StepKinematics_PrismaticPairValue) aPrismaticPairValue =
-      Handle(StepKinematics_PrismaticPairValue)::DownCast(thePairValue);
-    aPrismaticPairValue->SetActualTranslation(theKinPairValueObj->GetTranslation());
-    break;
-  }
-  case(XCAFKinematics_PairType_Cylindrical):
-  {
-    thePairValue = new StepKinematics_CylindricalPairValue;
-    Handle(StepKinematics_CylindricalPairValue) aCylindricalPairValue =
-      Handle(StepKinematics_CylindricalPairValue)::DownCast(thePairValue);
-    aCylindricalPairValue->SetActualTranslation(theKinPairValueObj->GetTranslation());
-    aCylindricalPairValue->SetActualRotation(theKinPairValueObj->GetRotation());
-    break;
-  }
-  case(XCAFKinematics_PairType_Universal):
-  case(XCAFKinematics_PairType_Homokinetic):
-  {
-    thePairValue = new StepKinematics_UniversalPairValue;
-    Handle(StepKinematics_UniversalPairValue) aUniversalPairValue =
-      Handle(StepKinematics_UniversalPairValue)::DownCast(thePairValue);
-    aUniversalPairValue->SetFirstRotationAngle(theKinPairValueObj->GetFirstRotation());
-    aUniversalPairValue->SetSecondRotationAngle(theKinPairValueObj->GetSecondRotation());
-    break;
-  }
-  case(XCAFKinematics_PairType_Spherical):
-  case(XCAFKinematics_PairType_SphericalWithPin):
-  {
-    thePairValue = new StepKinematics_SphericalPairValue;
-    Handle(StepKinematics_SphericalPairValue) aSphericalPairValue =
-      Handle(StepKinematics_SphericalPairValue)::DownCast(thePairValue);
-    StepKinematics_SpatialRotation InputRotation;
-    InputRotation.SetValue(theKinPairValueObj->GetAllValues());
-    aSphericalPairValue->SetInputOrientation(InputRotation);
-    break;
-  }
-  case(XCAFKinematics_PairType_Planar):
-  {
-    thePairValue = new StepKinematics_PlanarPairValue;
-    Handle(StepKinematics_PlanarPairValue) aPlanarPairValue =
-      Handle(StepKinematics_PlanarPairValue)::DownCast(thePairValue);
-    aPlanarPairValue->SetActualRotation(theKinPairValueObj->GetRotation());
-    aPlanarPairValue->SetActualTranslationX(theKinPairValueObj->GetFirstTranslation());
-    aPlanarPairValue->SetActualTranslationY(theKinPairValueObj->GetSecondTranslation());
-    break;
-  }
-  case(XCAFKinematics_PairType_Unconstrained):
-  {
-    thePairValue = new StepKinematics_UnconstrainedPairValue;
-    Handle(StepKinematics_UnconstrainedPairValue) anUnconstrainedPairValue = 
-      Handle(StepKinematics_UnconstrainedPairValue)::DownCast(thePairValue);
-    GeomToStep_MakeAxis2Placement3d anActualPlacement(theKinPairValueObj->GetTransformation());
-    anUnconstrainedPairValue->SetActualPlacement(anActualPlacement.Value());
-    break;
-  }
-  case(XCAFKinematics_PairType_Screw):
-  {
-    thePairValue = new StepKinematics_ScrewPairValue;
-    Handle(StepKinematics_ScrewPairValue) aScrewPairValue = 
-      Handle(StepKinematics_ScrewPairValue)::DownCast(thePairValue);
-    aScrewPairValue->SetActualRotation(theKinPairValueObj->GetRotation());
-    break;
-  }
-  case(XCAFKinematics_PairType_RackAndPinion):
-  {
-    thePairValue = new StepKinematics_RackAndPinionPairValue;
-    Handle(StepKinematics_RackAndPinionPairValue) aRackAndPinionPairValue = 
-      Handle(StepKinematics_RackAndPinionPairValue)::DownCast(thePairValue);
-    aRackAndPinionPairValue->SetActualDisplacement(theKinPairValueObj->GetTranslation());
-    break;
-  }
-  case(XCAFKinematics_PairType_Gear):
-  {
-    thePairValue = new StepKinematics_GearPairValue;
-    Handle(StepKinematics_GearPairValue) aGearPairValue = 
-      Handle(StepKinematics_GearPairValue)::DownCast(thePairValue);
-    aGearPairValue->SetActualRotation1(theKinPairValueObj->GetRotation());
-    break;
-  }
-  case(XCAFKinematics_PairType_PointOnSurface):
-  {
-    thePairValue = new StepKinematics_PointOnSurfacePairValue;
-    Handle(StepKinematics_PointOnSurfacePairValue) aPointOnSurfacePairValue = 
-      Handle(StepKinematics_PointOnSurfacePairValue)::DownCast(thePairValue);
-    Handle(StepGeom_PointOnSurface) aActualPointOnSurface = new StepGeom_PointOnSurface;
-    Handle(StepKinematics_PointOnSurfacePair) aPointOnSurfacePair = 
-      Handle(StepKinematics_PointOnSurfacePair)::DownCast(thePairReprRelationship->RepresentationRelationshipWithTransformation()->TransformationOperator().KinematicPair());
-    Standard_Real theU;
-    Standard_Real theV;
-    theKinPairValueObj->GetPointOnSurface(theU, theV);
-    aActualPointOnSurface->Init(new TCollection_HAsciiString, aPointOnSurfacePair->PairSurface(), theU, theV);
-    aPointOnSurfacePairValue->SetActualPointOnSurface(aActualPointOnSurface);
-    StepKinematics_SpatialRotation aYRP;
-    Handle(TColStd_HArray1OfReal) anArrayYRP = new TColStd_HArray1OfReal(1,3);
-    Standard_Real aYaw;
-    Standard_Real aPitch;
-    Standard_Real aRoll;
-    theKinPairValueObj->GetYPR(aYaw, aPitch, aRoll);
-    anArrayYRP->SetValue(1, aYaw);
-    anArrayYRP->SetValue(2, aPitch);
-    anArrayYRP->SetValue(3, aRoll);
-    aYRP.SetValue(anArrayYRP);
-    aPointOnSurfacePairValue->SetInputOrientation(aYRP);
-    break;
-  }
-  case(XCAFKinematics_PairType_SlidingSurface):
-  {
-    thePairValue = new StepKinematics_SlidingSurfacePairValue;
-    Handle(StepKinematics_SlidingSurfacePairValue) aSlidingSurfacePairValue =
-      Handle(StepKinematics_SlidingSurfacePairValue)::DownCast(thePairValue);
-    Handle(StepGeom_PointOnSurface) aActualPointOnSurface1 = new StepGeom_PointOnSurface;
-    Handle(StepKinematics_SlidingSurfacePair) SlidingSurfacePair =
-      Handle(StepKinematics_SlidingSurfacePair)::DownCast(thePairReprRelationship->RepresentationRelationshipWithTransformation()->TransformationOperator().KinematicPair());
-    Standard_Real theU1;
-    Standard_Real theV1;
-    theKinPairValueObj->GetFirstPointOnSurface(theU1, theV1);
-    aActualPointOnSurface1->Init(new TCollection_HAsciiString, SlidingSurfacePair->Surface1(), theU1, theV1);
-    aSlidingSurfacePairValue->SetActualPointOnSurface1(aActualPointOnSurface1);
-    Handle(StepGeom_PointOnSurface) aActualPointOnSurface2 = new StepGeom_PointOnSurface;
-    Standard_Real theU2;
-    Standard_Real theV2;
-    theKinPairValueObj->GetSecondPointOnSurface(theU2, theV2);
-    aActualPointOnSurface2->Init(new TCollection_HAsciiString, SlidingSurfacePair->Surface2(), theU2, theV2);
-    aSlidingSurfacePairValue->SetActualPointOnSurface2(aActualPointOnSurface2);
-    aSlidingSurfacePairValue->SetActualRotation(theKinPairValueObj->GetRotation());
-    break;
-  }
-  case(XCAFKinematics_PairType_RollingSurface):
-  {
-    thePairValue = new StepKinematics_RollingSurfacePairValue;
-    Handle(StepKinematics_RollingSurfacePairValue) aRollingSurfacePairValue =
-      Handle(StepKinematics_RollingSurfacePairValue)::DownCast(thePairValue);
-    aRollingSurfacePairValue->SetActualRotation(theKinPairValueObj->GetRotation());
-    Handle(StepGeom_PointOnSurface) aActualPointOnSurface = new StepGeom_PointOnSurface;
-    Handle(StepKinematics_RollingSurfacePair) aRollingSurfacePair =
-      Handle(StepKinematics_RollingSurfacePair)::DownCast(thePairReprRelationship->RepresentationRelationshipWithTransformation()->TransformationOperator().KinematicPair());
-    Standard_Real theU;
-    Standard_Real theV;
-    theKinPairValueObj->GetPointOnSurface(theU, theV);
-    aActualPointOnSurface->Init(new TCollection_HAsciiString, aRollingSurfacePair->Surface1(), theU, theV);
-    aRollingSurfacePairValue->SetActualPointOnSurface(aActualPointOnSurface);
-    break;
-  }
-  case(XCAFKinematics_PairType_PointOnPlanarCurve):
-  {
-    thePairValue = new StepKinematics_PointOnPlanarCurvePairValue;
-    Handle(StepKinematics_PointOnPlanarCurvePairValue) aPointOnPlanarCurvePairValue =
-      Handle(StepKinematics_PointOnPlanarCurvePairValue)::DownCast(thePairValue);
-    StepKinematics_SpatialRotation aYRP;
-    Handle(TColStd_HArray1OfReal) anArrayYRP = new TColStd_HArray1OfReal(1, 3);
-    Standard_Real aYaw;
-    Standard_Real aPitch;
-    Standard_Real aRoll;
-    theKinPairValueObj->GetYPR(aYaw, aPitch, aRoll);
-    anArrayYRP->SetValue(1, aYaw);
-    anArrayYRP->SetValue(2, aPitch);
-    anArrayYRP->SetValue(3, aRoll);
-    aYRP.SetValue(anArrayYRP);
-    aPointOnPlanarCurvePairValue->SetInputOrientation(aYRP);
-    Handle(StepGeom_PointOnCurve) anActualPointOnCurve = new StepGeom_PointOnCurve;
-    Handle(StepKinematics_PointOnPlanarCurvePair) aPointOnPlanarCurvePair =
-      Handle(StepKinematics_PointOnPlanarCurvePair)::DownCast(thePairReprRelationship->RepresentationRelationshipWithTransformation()->TransformationOperator().KinematicPair());
-    Standard_Real aParametr = theKinPairValueObj->GetPointOnCurve();
-    anActualPointOnCurve->Init(aPointOnPlanarCurvePair->Name(), aPointOnPlanarCurvePair->PairCurve(), aParametr);
-    aPointOnPlanarCurvePairValue->SetActualPointOnCurve(anActualPointOnCurve);
-    break;
-  }
-  case(XCAFKinematics_PairType_SlidingCurve):
-  {
-    thePairValue = new StepKinematics_SlidingCurvePairValue;
-    Handle(StepKinematics_SlidingCurvePairValue) aSlidingCurvePairValue = 
-      Handle(StepKinematics_SlidingCurvePairValue)::DownCast(thePairValue);
-    Handle(StepGeom_PointOnCurve) anActualPointOnCurve1 = new StepGeom_PointOnCurve;
-    Handle(StepKinematics_SlidingCurvePair) aSlidingCurvePair =
-      Handle(StepKinematics_SlidingCurvePair)::DownCast(thePairReprRelationship->RepresentationRelationshipWithTransformation()->TransformationOperator().KinematicPair());
-    Standard_Real aParametr1 = theKinPairValueObj->GetFirstPointOnCurve();
-    anActualPointOnCurve1->Init(aSlidingCurvePair->Name(), aSlidingCurvePair->Curve1(), aParametr1);
-    aSlidingCurvePairValue->SetActualPointOnCurve1(anActualPointOnCurve1);
-    Handle(StepGeom_PointOnCurve) anActualPointOnCurve2 = new StepGeom_PointOnCurve;
-    Standard_Real aParametr2 = theKinPairValueObj->GetSecondPointOnCurve();
-    anActualPointOnCurve2->Init(aSlidingCurvePair->Name(), aSlidingCurvePair->Curve2(), aParametr2);
-    aSlidingCurvePairValue->SetActualPointOnCurve2(anActualPointOnCurve2);
-    break;
-  }
-  case(XCAFKinematics_PairType_RollingCurve):
-  {
-    thePairValue = new StepKinematics_RollingCurvePairValue;
-    Handle(StepKinematics_RollingCurvePairValue) aRollingCurvePairValue =
-      Handle(StepKinematics_RollingCurvePairValue)::DownCast(thePairValue);
-    Handle(StepGeom_PointOnCurve) anActualPointOnCurve = new StepGeom_PointOnCurve;
-    Handle(StepKinematics_RollingCurvePair) aRollingCurvePair =
-      Handle(StepKinematics_RollingCurvePair)::DownCast(thePairReprRelationship->RepresentationRelationshipWithTransformation()->TransformationOperator().KinematicPair());
-    Standard_Real aParametr = theKinPairValueObj->GetPointOnCurve();
-    anActualPointOnCurve->Init(aRollingCurvePair->Name(), aRollingCurvePair->Curve1(), aParametr);
-    aRollingCurvePairValue->SetActualPointOnCurve1(anActualPointOnCurve);
-    break; 
-  }
-  default:
-    return Standard_False;
-    break;
   }
-  Handle(StepKinematics_KinematicPair) aAppliesToPair =
-    thePairReprRelationship->RepresentationRelationshipWithTransformation()->TransformationOperator().KinematicPair();
-  thePairValue->SetAppliesToPair(aAppliesToPair);
-  thePairValue->SetName(aAppliesToPair->Name());
-  return Standard_True;
 }
 
 //=======================================================================
-//function : WriteKinematics
-//purpose  : 
+//function : WriteShapeAspect
+//purpose  : auxiliary (write Shape_Aspect entity for given shape)
 //=======================================================================
-Standard_EXPORT Standard_Boolean STEPCAFControl_Writer::WriteKinematics(const Handle(XSControl_WorkSession)& theWS, const TDF_LabelSequence& theLabels)
-{
 
-  if (theLabels.Length() <= 0) return Standard_False;
-
-  // get working data
-  const Handle(Interface_InterfaceModel)& Model = theWS->Model();
-
-  const Handle(Interface_HGraph) aHGraph = theWS->HGraph();
+Handle(StepRepr_ShapeAspect) STEPCAFControl_Writer::WriteShapeAspect (const Handle(XSControl_WorkSession) &WS,
+                                                                      const TDF_Label theLabel,
+                                                                      const TopoDS_Shape theShape,
+                                                                      Handle(StepRepr_RepresentationContext)& theRC,
+                                                                      Handle(StepAP242_GeometricItemSpecificUsage)& theGISU)
+{
+  // Get working data
+  const Handle(Interface_InterfaceModel) &Model = WS->Model();
+  const Handle(XSControl_TransferWriter) &TW = WS->TransferWriter();
+  const Handle(Transfer_FinderProcess) &FP = TW->FinderProcess();
+  const Handle(Interface_HGraph) aHGraph = WS->HGraph();
   if (aHGraph.IsNull())
-    return Standard_False;
-
+    return NULL;
   Interface_Graph aGraph = aHGraph->Graph();
-  Handle(XCAFDoc_KinematicTool) aKTool = XCAFDoc_DocumentTool::KinematicTool(theLabels(1));
-  if (aKTool.IsNull()) 
-    return Standard_False;
-
-  TDF_LabelSequence aMechanismsL;
-  aMechanismsL = aKTool->GetMechanisms();
-  if (aMechanismsL.Length() <= 0) return Standard_False;
-
-  for (TDF_LabelSequence::Iterator anItMech(aMechanismsL); anItMech.More(); anItMech.Next())
-  {
-    // write Links
-    Handle(StepKinematics_RigidLinkRepresentation) aBaseLinkOfMech;
-    Handle(StepRepr_PropertyDefinition) aGeneralDefinition;
-    NCollection_IndexedDataMap<TDF_Label, Handle(StepKinematics_KinematicLinkRepresentation), TDF_LabelMapHasher> aMapOfLinks;
-
-    TDF_LabelSequence aSeqOfLinskL = aKTool->GetLinks(anItMech.Value());
-    if (aSeqOfLinskL.Length() <= 0)
-      continue;
-
-    for (TDF_LabelSequence::Iterator anItLink(aSeqOfLinskL); anItLink.More(); anItLink.Next())
-    {
-      TDF_Label aLinkL = anItLink.Value();
-      if (!createKinematicLink(theWS, aKTool, aGraph, aLinkL, aMapOfLinks, aBaseLinkOfMech, aGeneralDefinition))
-        continue;
-    }
-    if (aGeneralDefinition.IsNull())
-      continue;
 
-    // if not found base link in OCAF
-    // first rigid link became base of the mechanism
-    for (Standard_Integer anIndOfLink = 1; anIndOfLink <= aMapOfLinks.Size() && aBaseLinkOfMech.IsNull(); ++anIndOfLink)
-      aBaseLinkOfMech = Handle(StepKinematics_RigidLinkRepresentation)::DownCast(aMapOfLinks.FindFromIndex(anIndOfLink));
-    if (aBaseLinkOfMech.IsNull())
-      continue;
-
-    // write joints
-    TDF_LabelSequence aSeqOfJointsL = aKTool->GetJoints(anItMech.Value());
-    if (aSeqOfJointsL.Length() <= 0) 
-      continue;
-    NCollection_IndexedDataMap<TDF_Label, Handle(StepKinematics_KinematicJoint), TDF_LabelMapHasher> aMapofJoints;
-    Handle(StepRepr_HArray1OfRepresentationItem) anArrayOfPairs = new StepRepr_HArray1OfRepresentationItem(1, aSeqOfJointsL.Length());
-
-    Handle(StepKinematics_KinematicPropertyMechanismRepresentation) aPropertyMechanismRepr = new StepKinematics_KinematicPropertyMechanismRepresentation;
-    Handle(StepKinematics_MechanismRepresentation) aMechanism = new StepKinematics_MechanismRepresentation;
-    StepRepr_RepresentedDefinition aPDK;
-    Handle(StepKinematics_KinematicTopologyStructure) aKTopoStruct = new StepKinematics_KinematicTopologyStructure;
-    Standard_Integer aJointInd = 1;
-    for (TDF_LabelSequence::Iterator anItJoint(aSeqOfJointsL); anItJoint.More(); anItJoint.Next())
-    {
-      TDF_Label aJointL = anItJoint.Value();
-      Handle(StepKinematics_KinematicJoint) aJoint;
-      Handle(XCAFDoc_KinematicPair) aKPairAttr;
-      Handle(StepKinematics_KinematicPair) aKinematicPair;
-      if (!aJointL.FindAttribute(XCAFDoc_KinematicPair::GetID(), aKPairAttr))
-        continue;
-      Handle(XCAFKinematics_PairObject) aPairObject = aKPairAttr->GetObject();
-      Handle(StepKinematics_KinematicLinkRepresentation) aLinkRepr1;
-      Handle(StepKinematics_KinematicLinkRepresentation) aLinkRepr2;
-      if (!createKinematicJoint(aKTool, aJointL, aPairObject, aMapOfLinks, aLinkRepr1, aLinkRepr2, aJoint))
-        continue;
-      if (!createKinematicPair(aPairObject, aJoint, aLinkRepr1, aLinkRepr2, aKinematicPair))
-        continue;
-      aMapofJoints.Add(anItJoint.Value(), aJoint);
-      Handle(StepKinematics_PairRepresentationRelationship) aPairReprRelationship = new StepKinematics_PairRepresentationRelationship;
-      StepRepr_RepresentationOrRepresentationReference aDataLinkStart;
-      aDataLinkStart.SetValue(aLinkRepr1);
-      StepRepr_RepresentationOrRepresentationReference aDataLinkEnd;
-      aDataLinkEnd.SetValue(aLinkRepr2);
-      StepRepr_Transformation aPair;
-      aPair.SetValue(aKinematicPair);
-      Handle(TCollection_HAsciiString) aDescription;
-      aPairReprRelationship->Init(aKinematicPair->Name(), aKinematicPair->Name(), Standard_False, aDescription, aDataLinkStart, aDataLinkEnd, aPair);
-      anArrayOfPairs->SetValue(aJointInd++, aPairReprRelationship);
+  TopLoc_Location aLoc;
+  TColStd_SequenceOfTransient aSeqRI;
+  FindEntities( FP, theShape, aLoc, aSeqRI );
+  if ( aSeqRI.Length() <= 0 ) {
+    FP->Messenger()->SendInfo() << "Warning: Cannot find RI for "<<theShape.TShape()->DynamicType()->Name()<<std::endl;
+    return NULL;
+  }
 
-      Model->AddEntity(aJoint);
-      Model->AddWithRefs(aPairReprRelationship);
-    }
+  Handle(StepRepr_ProductDefinitionShape) aPDS;
+  Handle(StepRepr_RepresentationContext) aRC;
+  Handle(Standard_Transient) anEnt = aSeqRI.Value(1);
+  aPDS = FindPDS(aGraph, anEnt, aRC);
+  if(aPDS.IsNull()) 
+    return NULL;
 
-    // write mechanism
-    Handle(StepKinematics_ProductDefinitionKinematics) aProductDefKin = new StepKinematics_ProductDefinitionKinematics;
-    aProductDefKin->Init(new TCollection_HAsciiString, Standard_False, new TCollection_HAsciiString, aGeneralDefinition->Definition());
-    aKTopoStruct->Init(new TCollection_HAsciiString, anArrayOfPairs, aBaseLinkOfMech->ContextOfItems());
-    StepKinematics_KinematicTopologyRepresentationSelect aTopoSelect;
-    aTopoSelect.SetValue(aKTopoStruct);
-    Handle(TDataStd_Name) aAttrName;
-    anItMech.Value().FindAttribute(TDataStd_Name::GetID(), aAttrName);
-    Handle(TCollection_HAsciiString) aHNameMechanism = new TCollection_HAsciiString(aAttrName->Get());
-    aPDK.SetValue(aProductDefKin);
-    aMechanism->Init(aHNameMechanism, anArrayOfPairs, aBaseLinkOfMech->ContextOfItems(), aTopoSelect);
-    aPropertyMechanismRepr->Init(aPDK, aMechanism, aBaseLinkOfMech);
-    Model->AddWithRefs(aPropertyMechanismRepr);
+  theRC = aRC;
+  // Shape_Aspect
+  Handle(TCollection_HAsciiString) aName = new TCollection_HAsciiString();
+  Handle(TDataStd_Name) aNameAttr;
+  if (theLabel.FindAttribute(TDataStd_Name::GetID(), aNameAttr)) {
+    aName = new TCollection_HAsciiString(TCollection_AsciiString(aNameAttr->Get(), '?'));
+    Standard_Integer aFirstSpace = aName->Search(" ");
+    if (aFirstSpace != -1)
+      aName = aName->SubString(aFirstSpace + 1, aName->Length());
+    else
+      aName = new TCollection_HAsciiString();
+  }
+  Handle(TCollection_HAsciiString) aDescription = new TCollection_HAsciiString();
+  Handle(StepRepr_ShapeAspect) aSA = new StepRepr_ShapeAspect;
+  aSA->Init(aName, aDescription, aPDS, StepData_LTrue);
 
-    // (optional) write States
-    TDF_LabelSequence aSeqOfStates = aKTool->GetStates(anItMech.Value());
-    for (TDF_LabelSequence::Iterator anItState(aSeqOfStates); anItState.More(); anItState.Next())
-    {
-      Handle(StepKinematics_MechanismStateRepresentation) aStateRepr = new StepKinematics_MechanismStateRepresentation;
-      TDF_LabelSequence aSeqOfValues = aKTool->GetValuesOfState(anItState.Value());
-      if (aSeqOfValues.IsEmpty())
-        continue;
-      Handle(StepRepr_HArray1OfRepresentationItem) aItems = new StepRepr_HArray1OfRepresentationItem(1, aSeqOfValues.Length());
-      Standard_Integer aValueInd = 1;
-      for (TDF_LabelSequence::Iterator anItValue(aSeqOfValues); anItValue.More(); anItValue.Next())
-      {
-        TDF_Label aJointL = aKTool->GetJointOfValue(anItValue.Value());
-        Handle(StepKinematics_KinematicJoint) aJoint;
-        Handle(StepKinematics_PairRepresentationRelationship) aPairReprRelationship;
-        if (!aMapofJoints.FindFromKey(aJointL, aJoint))
-          continue;
-        aPairReprRelationship = Handle(StepKinematics_PairRepresentationRelationship)::DownCast(anArrayOfPairs->Value(aMapofJoints.FindIndex(aJointL)));
-        if (aPairReprRelationship.IsNull())
-          continue;
-        Handle(XCAFDoc_KinematicPairValue) aKPairValueAttr;
-        if (!anItValue.Value().FindAttribute(XCAFDoc_KinematicPairValue::GetID(), aKPairValueAttr))
-          continue;
-        Handle(TCollection_HAsciiString) aNameOfValue;
-        Handle(TDataStd_Name) aNameAttr;
-        if (anItValue.Value().FindAttribute(TDataStd_Name::GetID(), aNameAttr))
-          aNameOfValue = new TCollection_HAsciiString(aNameAttr->Get());
-        Handle(XCAFKinematics_PairValueObject) aPairValueObject = aKPairValueAttr->GetObject();
-        Handle(StepKinematics_PairValue) aPairValue;
-        if (!createKinematicPairValue(aPairValueObject, aPairReprRelationship, aPairValue))
-          continue;
-        aPairValue->SetName(aNameOfValue);
-        aItems->SetValue(aValueInd++, aPairValue);
-      }
-      if (aValueInd == 1)
-        continue;
-      Handle(TCollection_HAsciiString) aNameOfState;
-      Handle(TDataStd_Name) aNameAttr;
-      if (anItState.Value().FindAttribute(TDataStd_Name::GetID(), aNameAttr))
-        aNameOfState = new TCollection_HAsciiString(aNameAttr->Get());
-      aStateRepr->Init(aNameOfState, aItems, aMechanism->ContextOfItems(), aMechanism);
-      Model->AddWithRefs(aStateRepr);
-    }
+  // Geometric_Item_Specific_Usage
+  Handle(StepAP242_GeometricItemSpecificUsage) aGISU = new StepAP242_GeometricItemSpecificUsage();
+  StepAP242_ItemIdentifiedRepresentationUsageDefinition aDefinition;
+  aDefinition.SetValue(aSA);
+  Handle(StepRepr_HArray1OfRepresentationItem) anReprItems = new StepRepr_HArray1OfRepresentationItem(1, 1);
+  Handle(StepRepr_RepresentationItem) anIdentifiedItem = Handle(StepRepr_RepresentationItem)::DownCast(anEnt);
+  anReprItems->SetValue(1, anIdentifiedItem);
+  Interface_EntityIterator subs = aGraph.Sharings(aPDS);
+  Handle(StepShape_ShapeDefinitionRepresentation) aSDR;
+  for (subs.Start(); subs.More() && aSDR.IsNull(); subs.Next()) {
+    Handle(Standard_Transient) anEntity = subs.Value();
+    aSDR = Handle(StepShape_ShapeDefinitionRepresentation)::DownCast(anEntity);
   }
+  if (aSDR.IsNull())
+    return NULL;
 
-  return Standard_True;
+  // Set entities to model
+  aGISU->Init(aName, aDescription, aDefinition, aSDR->UsedRepresentation(), anReprItems);
+  Model->AddWithRefs(aSA);
+  Model->AddWithRefs(aGISU);
+  theGISU = aGISU;
+  return aSA;
 }
 
 //=======================================================================
-//function : FindPDSforDGT
-//purpose  : auxilary: find PDS for AdvancedFace or EdgeCurve for creation
-//                     needed ShapeAspect in D&GT structure
-//=======================================================================
-static Standard_Boolean FindPDSforDGT(const Interface_Graph &aGraph,
-                                      const Handle(Standard_Transient) &ent,
-                                      Handle(StepRepr_ProductDefinitionShape) &PDS,
-                                      Handle(StepRepr_RepresentationContext) &RC,
-                                      Handle(StepShape_AdvancedFace) &AF,
-                                      Handle(StepShape_EdgeCurve) &EC)
+//function : WritePresentation
+//purpose  : auxiliary (write annotation plane and presentation)
+//======================================================================
+void STEPCAFControl_Writer::WritePresentation(const Handle(XSControl_WorkSession) &WS,
+                                              const TopoDS_Shape thePresentation,
+                                              const Handle(TCollection_HAsciiString)& thePrsName,
+                                              const Standard_Boolean hasSemantic,
+                                              const Standard_Boolean hasPlane,
+                                              const gp_Ax2 theAnnotationPlane,
+                                              const gp_Pnt theTextPosition,
+                                              const Handle(Standard_Transient) theDimension)
 {
-  if (ent.IsNull())
-    return Standard_False;
-  if( !ent->IsKind(STANDARD_TYPE(StepShape_EdgeCurve)) && 
-      !ent->IsKind(STANDARD_TYPE(StepShape_AdvancedFace)) ) 
-    return Standard_False;
+  if (thePresentation.IsNull())
+    return;
+  // Get working data
+  const Handle(Interface_InterfaceModel) &aModel = WS->Model();
 
-  AF = Handle(StepShape_AdvancedFace)::DownCast(ent);
-  if( AF.IsNull() ) {
-    EC = Handle(StepShape_EdgeCurve)::DownCast(ent);
-    Interface_EntityIterator subs = aGraph.Sharings(EC);
-    for(subs.Start(); subs.More() && AF.IsNull(); subs.Next()) {
-      Handle(StepShape_OrientedEdge) OE = Handle(StepShape_OrientedEdge)::DownCast(subs.Value());
-      if(OE.IsNull()) continue;
-      Interface_EntityIterator subs1 = aGraph.Sharings(OE);
-      for(subs1.Start(); subs1.More() && AF.IsNull(); subs1.Next()) {
-        Handle(StepShape_EdgeLoop) EL = Handle(StepShape_EdgeLoop)::DownCast(subs1.Value());
-        if(EL.IsNull()) continue;
-        Interface_EntityIterator subs2 = aGraph.Sharings(EL);
-        for(subs2.Start(); subs2.More() && AF.IsNull(); subs2.Next()) {
-          Handle(StepShape_FaceBound) FB = Handle(StepShape_FaceBound)::DownCast(subs2.Value());
-          if(FB.IsNull()) continue;
-          Interface_EntityIterator subs3 = aGraph.Sharings(FB);
-          for(subs3.Start(); subs3.More() && AF.IsNull(); subs3.Next()) {
-            AF = Handle(StepShape_AdvancedFace)::DownCast(subs3.Value());
-          }
-        }
-      }
-    }
-  }
-  if(AF.IsNull()) return Standard_False;
+  // Presentation
+  Handle(StepVisual_TessellatedGeometricSet) aGeomSet = STEPCAFControl_GDTProperty::GetTessellation(thePresentation);
+  Handle(StepVisual_TessellatedAnnotationOccurrence) aTAO = new StepVisual_TessellatedAnnotationOccurrence();
+  aTAO->Init(new TCollection_HAsciiString(), myGDTPrsCurveStyle, aGeomSet);
+  StepVisual_DraughtingCalloutElement aDCElement;
+  aDCElement.SetValue(aTAO);
+  Handle(StepVisual_HArray1OfDraughtingCalloutElement) aTAOs = new StepVisual_HArray1OfDraughtingCalloutElement(1, 1);
+  aTAOs->SetValue(1, aDCElement);
+  Handle(StepVisual_DraughtingCallout) aDCallout = new StepVisual_DraughtingCallout();
+  Handle(TCollection_HAsciiString) aPrsName = thePrsName.IsNull() ? new TCollection_HAsciiString() : thePrsName;
+  aDCallout->Init(aPrsName, aTAOs);
+  Handle(StepRepr_HArray1OfRepresentationItem) aDCsForDMIA = new StepRepr_HArray1OfRepresentationItem(1, 1);
+  aDCsForDMIA->SetValue(1, aDCallout);
+  myGDTAnnotations.Append(aDCallout);
+  StepAP242_ItemIdentifiedRepresentationUsageDefinition aDimension;
+  aDimension.SetValue(theDimension);
+  Handle(TCollection_HAsciiString) aDMIAName;
+  if (hasSemantic)
+    aDMIAName = new TCollection_HAsciiString("PMI representation to presentation link");
+  else
+    aDMIAName = new TCollection_HAsciiString();
+  Handle(StepAP242_DraughtingModelItemAssociation) aDMIA =
+    new StepAP242_DraughtingModelItemAssociation();
+  aDMIA->Init(aDMIAName, new TCollection_HAsciiString(), aDimension, myGDTPresentationDM, aDCsForDMIA);
+  aModel->AddWithRefs(aDMIA);
 
-  Interface_EntityIterator subs = aGraph.Sharings(AF);
-  for(subs.Start(); subs.More() && PDS.IsNull(); subs.Next()) {
-    Handle(StepShape_ConnectedFaceSet) CFS =
-      Handle(StepShape_ConnectedFaceSet)::DownCast(subs.Value());
-    if(CFS.IsNull()) continue;
-    Interface_EntityIterator subs1 = aGraph.Sharings(CFS);
-    for(subs1.Start(); subs1.More() && PDS.IsNull(); subs1.Next()) {
-      Handle(StepRepr_RepresentationItem) RI = 
-        Handle(StepRepr_RepresentationItem)::DownCast(subs1.Value());
-      if(RI.IsNull()) continue;
-      Interface_EntityIterator subs2 = aGraph.Sharings(RI);
-      for(subs2.Start(); subs2.More() && PDS.IsNull(); subs2.Next()) {
-        Handle(StepShape_ShapeRepresentation) SR = 
-          Handle(StepShape_ShapeRepresentation)::DownCast(subs2.Value());
-        if(SR.IsNull()) continue;
-        RC = SR->ContextOfItems();
-        Interface_EntityIterator subs3 = aGraph.Sharings(SR);
-        for(subs3.Start(); subs3.More() && PDS.IsNull(); subs3.Next()) {
-          Handle(StepShape_ShapeDefinitionRepresentation) SDR = 
-            Handle(StepShape_ShapeDefinitionRepresentation)::DownCast(subs3.Value());
-          if(SDR.IsNull()) continue;
-          Handle(StepRepr_PropertyDefinition) PropD = SDR->Definition().PropertyDefinition();
-          if(PropD.IsNull()) continue;
-          PDS = Handle(StepRepr_ProductDefinitionShape)::DownCast(PropD);
-        }
-      }
-    }
-  }
-  
-  return Standard_True;
+  if (!hasPlane)
+    return;
+
+  // Annotation plane
+  // Presentation Style
+  Handle(StepVisual_NullStyleMember) aNullStyle = new StepVisual_NullStyleMember();
+  aNullStyle->SetEnumText(0, ".NULL.");
+  StepVisual_PresentationStyleSelect aStyleItem;
+  aStyleItem.SetValue(aNullStyle);
+  Handle(StepVisual_HArray1OfPresentationStyleSelect) aStyles = new StepVisual_HArray1OfPresentationStyleSelect(1, 1);
+  aStyles->SetValue(1, aStyleItem);
+  Handle(StepVisual_PresentationStyleAssignment) aPrsStyle = new StepVisual_PresentationStyleAssignment();
+  aPrsStyle->Init(aStyles);
+  Handle(StepVisual_HArray1OfPresentationStyleAssignment) aPrsStyles =
+    new StepVisual_HArray1OfPresentationStyleAssignment(1, 1);
+  aPrsStyles->SetValue(1, aPrsStyle);
+  // Plane
+  Handle(StepGeom_Plane) aPlane = new StepGeom_Plane();
+  GeomToStep_MakeAxis2Placement3d anAxisMaker(theAnnotationPlane);
+  Handle(StepGeom_Axis2Placement3d) anAxis = anAxisMaker.Value();
+  // Set text position to plane origin
+  Handle(StepGeom_CartesianPoint) aTextPos = new StepGeom_CartesianPoint();
+  Handle(TColStd_HArray1OfReal) aCoords = new TColStd_HArray1OfReal(1, 3);
+  for (Standard_Integer i = 1; i <= 3; i++)
+    aCoords->SetValue(i, theTextPosition.Coord(i));
+  aTextPos->Init(new TCollection_HAsciiString(), aCoords);
+  anAxis->SetLocation(aTextPos);
+  aPlane->Init(new TCollection_HAsciiString(), anAxis);
+  // Annotation plane element
+  StepVisual_AnnotationPlaneElement aPlaneElement;
+  aPlaneElement.SetValue(aDCallout);
+  Handle(StepVisual_HArray1OfAnnotationPlaneElement) aDCsForAnnPln = new StepVisual_HArray1OfAnnotationPlaneElement(1, 1);
+  aDCsForAnnPln->SetValue(1, aPlaneElement);
+  // Init AnnotationPlane entity
+  Handle(StepVisual_AnnotationPlane) anAnnPlane = new StepVisual_AnnotationPlane();
+  anAnnPlane->Init(new TCollection_HAsciiString(), aPrsStyles, aPlane, aDCsForAnnPln);
+  myGDTAnnotations.Append(anAnnPlane);
+  aModel->AddWithRefs(anAnnPlane);
 }
 
 //=======================================================================
-//function : FindPDS
-//purpose  : auxilary: find Product_definition_shape entity for given entity
+//function : WriteDatumAP242
+//purpose  : auxiliary (write Datum entity for given shape or write all 
+//           necessary entities and link them to already written datum 
+//           in case of multiple features association)
 //=======================================================================
-static Handle(StepRepr_ProductDefinitionShape) FindPDS(const Interface_Graph &theGraph,
-                                                       const Handle(Standard_Transient) &theEnt,
-                                                       Handle(StepRepr_RepresentationContext) &theRC)
+Handle(StepDimTol_Datum) STEPCAFControl_Writer::WriteDatumAP242(const Handle(XSControl_WorkSession) &WS,
+                                                                const TDF_LabelSequence theShapeL,
+                                                                const TDF_Label theDatumL,
+                                                                const Standard_Boolean isFirstDTarget,
+                                                                const Handle(StepDimTol_Datum) theWrittenDatum)
 {
-  if (theEnt.IsNull())
+  // Get working data
+  const Handle(Interface_InterfaceModel) &Model = WS->Model();
+  const Handle(XSControl_TransferWriter) &TW = WS->TransferWriter();
+  const Handle(Transfer_FinderProcess) &FP = TW->FinderProcess();
+  const Handle(Interface_HGraph) aHGraph = WS->HGraph();
+  if (aHGraph.IsNull())
     return NULL;
+  Interface_Graph aGraph = aHGraph->Graph();
+
+  Handle(StepRepr_ShapeAspect) aSA;
+  Handle(StepRepr_RepresentationContext) aRC;
   Handle(StepRepr_ProductDefinitionShape) aPDS;
+  NCollection_Sequence<Handle(StepRepr_ShapeAspect)> aSASeq;
+  Handle(StepAP242_GeometricItemSpecificUsage) aGISU;
+  Standard_Integer aSANum = 0, aGISUNum = 0;
+  // Link with datum feature
+  for (Standard_Integer i = 1; i <= theShapeL.Length(); i++) {
+    Handle(Standard_Transient) anEnt;
+    TopoDS_Shape aShape;
+    TopLoc_Location aLoc;
+    TColStd_SequenceOfTransient aSeqRI;
 
-  // try to find shape_representation in sharings
-  Interface_EntityIterator anIter = theGraph.Sharings(theEnt);
-  for (anIter.Start(); anIter.More() && aPDS.IsNull(); anIter.Next()) {
-    Handle(StepShape_ShapeRepresentation) aSR = Handle(StepShape_ShapeRepresentation)::DownCast(anIter.Value());
-    if (aSR.IsNull())
+    aShape = XCAFDoc_ShapeTool::GetShape(theShapeL.Value(i));
+    FindEntities(FP, aShape, aLoc, aSeqRI);
+    if (aSeqRI.Length() <= 0) {
+      FP->Messenger()->SendInfo() << "Warning: Cannot find RI for " << aShape.TShape()->DynamicType()->Name() << std::endl;
       continue;
-    theRC = aSR->ContextOfItems();
-    Interface_EntityIterator aSDRIt = theGraph.Sharings(aSR);
-    for (aSDRIt.Start(); aSDRIt.More() && aPDS.IsNull(); aSDRIt.Next()) {
-      Handle(StepShape_ShapeDefinitionRepresentation) aSDR =
-        Handle(StepShape_ShapeDefinitionRepresentation)::DownCast(aSDRIt.Value());
-      if (aSDR.IsNull()) continue;
-      Handle(StepRepr_PropertyDefinition) aPropD = aSDR->Definition().PropertyDefinition();
-      if (aPropD.IsNull()) continue;
-      aPDS = Handle(StepRepr_ProductDefinitionShape)::DownCast(aPropD);
     }
-  }
-  if (!aPDS.IsNull())
-    return aPDS;
+    anEnt = aSeqRI.Value(1);
+    aPDS = FindPDS(aGraph, anEnt, aRC);
+    if (aPDS.IsNull())
+      continue;
 
-  anIter = theGraph.Sharings(theEnt);
-  for (anIter.Start(); anIter.More(); anIter.Next()) {
-    if (anIter.Value()->IsKind(STANDARD_TYPE(StepShape_TopologicalRepresentationItem)) ||
-      anIter.Value()->IsKind(STANDARD_TYPE(StepGeom_GeometricRepresentationItem)))
-    {
-      aPDS = FindPDS(theGraph, anIter.Value(), theRC);
-      if (!aPDS.IsNull())
-        return aPDS;
+    Handle(StepRepr_ShapeAspect) aCurrentSA = WriteShapeAspect(WS, theDatumL, aShape, aRC, aGISU);
+    if (aCurrentSA.IsNull())
+      continue;
+    aSASeq.Append(aCurrentSA);
+    aSANum = Model->Number(aCurrentSA);
+    aGISUNum = Model->Number(aGISU);
+  }
+  if (aPDS.IsNull()) {
+    // Workaround for datums without shape
+    aPDS = myGDTCommonPDS;
+    Interface_EntityIterator aSDRIt = aGraph.Sharings(aPDS);
+    Handle(StepShape_ShapeDefinitionRepresentation) aSDR;
+    for (aSDRIt.Start(); aSDRIt.More() && aSDR.IsNull(); aSDRIt.Next())
+      aSDR = Handle(StepShape_ShapeDefinitionRepresentation)::DownCast(aSDRIt.Value());
+    if (!aSDR.IsNull()) {
+      Handle(StepRepr_Representation) aRepr = aSDR->UsedRepresentation();
+      if (!aRepr.IsNull())
+        aRC = aRepr->ContextOfItems();
     }
   }
 
-  return aPDS;
-}
 
-//=======================================================================
-//function : GetUnit
-//purpose  : auxiliary
-//=======================================================================
-static StepBasic_Unit GetUnit(const Handle(StepRepr_RepresentationContext)& theRC,
-                              const Standard_Boolean isAngle = Standard_False)
-{
-  StepBasic_Unit aUnit;
-  Handle(StepBasic_NamedUnit) aCurrentUnit;
-  if (isAngle) {
-    Handle(StepGeom_GeometricRepresentationContextAndGlobalUnitAssignedContext) aCtx =
-      Handle(StepGeom_GeometricRepresentationContextAndGlobalUnitAssignedContext)::DownCast(theRC);
-    if(!aCtx.IsNull()) {
-      for(Standard_Integer j = 1; j <= aCtx->NbUnits(); j++) {
-        if (aCtx->UnitsValue(j)->IsKind(STANDARD_TYPE(StepBasic_SiUnitAndPlaneAngleUnit)) ||
-            aCtx->UnitsValue(j)->IsKind(STANDARD_TYPE(StepBasic_ConversionBasedUnitAndPlaneAngleUnit))) {
-          aCurrentUnit = aCtx->UnitsValue(j);
-          break;
-        }
-      }
+  // Find if datum has datum targets and get common datum attributes
+  Handle(XCAFDoc_Datum) aDatumAttr;
+  if (!theDatumL.FindAttribute(XCAFDoc_Datum::GetID(), aDatumAttr)) 
+    return NULL;
+  Handle(XCAFDimTolObjects_DatumObject) anObject = aDatumAttr->GetObject();
+  if (anObject.IsNull())
+    return NULL;
+  Standard_Boolean isSimpleDatum = !anObject->IsDatumTarget();
+  Handle(TCollection_HAsciiString) anIdentifier = anObject->GetName();
+  Handle(TCollection_HAsciiString) aTargetId = (anObject->GetDatumTargetNumber() == 0 ? 
+    new TCollection_HAsciiString() : new TCollection_HAsciiString(anObject->GetDatumTargetNumber()));
+
+  // If datum type is area, but there is no area in object, write as simple datum
+  if (anObject->GetDatumTargetType() == XCAFDimTolObjects_DatumTargetType_Area &&
+    anObject->GetDatumTarget().IsNull())
+    isSimpleDatum = Standard_True;
+
+  // Simple datum
+  if (isSimpleDatum) {
+    if (aSASeq.Length() == 0) {
+      // Create empty datum with name and presentation only
+      Handle(StepDimTol_DatumFeature) aDF = new StepDimTol_DatumFeature();
+      aDF->Init(new TCollection_HAsciiString(), new TCollection_HAsciiString(), aPDS, StepData_LTrue);
+      aSA = aDF;
+      Model->AddWithRefs(aDF);
     }
-    if (aCurrentUnit.IsNull()) {
-      Handle(StepGeom_GeomRepContextAndGlobUnitAssCtxAndGlobUncertaintyAssCtx) aCtx1 =
-        Handle(StepGeom_GeomRepContextAndGlobUnitAssCtxAndGlobUncertaintyAssCtx)::DownCast(theRC);
-      if(!aCtx1.IsNull()) {
-        for(Standard_Integer j = 1; j <= aCtx1->NbUnits(); j++) {
-          if (aCtx1->UnitsValue(j)->IsKind(STANDARD_TYPE(StepBasic_SiUnitAndPlaneAngleUnit)) ||
-              aCtx1->UnitsValue(j)->IsKind(STANDARD_TYPE(StepBasic_ConversionBasedUnitAndPlaneAngleUnit))) {
-            aCurrentUnit = aCtx1->UnitsValue(j);
-            break;
-          }
-        }
+    else if (aSASeq.Length() == 1) {
+      Handle(StepDimTol_DatumFeature) aDF = new StepDimTol_DatumFeature();
+      aDF->Init(new TCollection_HAsciiString(), new TCollection_HAsciiString(), aPDS, StepData_LTrue);
+      Model->ReplaceEntity(aSANum, aDF);
+      aSA = aDF;
+      StepAP242_ItemIdentifiedRepresentationUsageDefinition aDefinition;
+      aDefinition.SetValue(aDF);
+      aGISU->SetDefinition(aDefinition);
+      Model->ReplaceEntity(aGISUNum, aGISU);
+    }
+    else if (aSASeq.Length() > 1) {
+      Handle(StepRepr_CompShAspAndDatumFeatAndShAsp) aDF = new StepRepr_CompShAspAndDatumFeatAndShAsp();
+      aDF->Init(new TCollection_HAsciiString(), new TCollection_HAsciiString(), aPDS, StepData_LTrue);
+      for (Standard_Integer i = 1; i <= aSASeq.Length(); i++) {
+        Handle(StepRepr_ShapeAspectRelationship) aSAR = new StepRepr_ShapeAspectRelationship();
+        aSAR->Init(new TCollection_HAsciiString(), Standard_False, new TCollection_HAsciiString(), aDF, aSASeq.Value(i));
+        Model->AddWithRefs(aSAR);
       }
+      aSA = aDF;
+      Model->AddWithRefs(aDF);
     }
-    if (aCurrentUnit.IsNull())
-      aCurrentUnit = new StepBasic_SiUnitAndPlaneAngleUnit;
   }
+  // Datum with datum targets
   else {
-    Handle(StepGeom_GeometricRepresentationContextAndGlobalUnitAssignedContext) aCtx =
-      Handle(StepGeom_GeometricRepresentationContextAndGlobalUnitAssignedContext)::DownCast(theRC);
-    if(!aCtx.IsNull()) {
-      for(Standard_Integer j = 1; j <= aCtx->NbUnits(); j++) {
-        if (aCtx->UnitsValue(j)->IsKind(STANDARD_TYPE(StepBasic_SiUnitAndLengthUnit)) ||
-            aCtx->UnitsValue(j)->IsKind(STANDARD_TYPE(StepBasic_ConversionBasedUnitAndLengthUnit))) {
-          aCurrentUnit = aCtx->UnitsValue(j);
-          break;
-        }
-      }
+    XCAFDimTolObjects_DatumTargetType aDatumType = anObject->GetDatumTargetType();
+    Handle(StepDimTol_DatumTarget) aDatumTarget;
+    // Note: the given way to write such datum type may be incorrect (too little information)
+    if (aDatumType == XCAFDimTolObjects_DatumTargetType_Area) {
+      TopoDS_Shape aDTShape = anObject->GetDatumTarget();
+      Handle(StepAP242_GeometricItemSpecificUsage) anAreaGISU;
+      Handle(StepRepr_ShapeAspect) anAreaSA = WriteShapeAspect(WS, theDatumL, aDTShape, aRC, anAreaGISU);
+      aSANum = Model->Number(anAreaSA);
+      aGISUNum = Model->Number(anAreaGISU);
+      Handle(StepDimTol_DatumTarget) aDT = new StepDimTol_DatumTarget();
+      aDT->Init(new TCollection_HAsciiString(), new TCollection_HAsciiString("area"), anAreaSA->OfShape(),
+        StepData_LTrue, aTargetId);
+      Model->ReplaceEntity(aSANum, aDT);
+      StepAP242_ItemIdentifiedRepresentationUsageDefinition aDefinition;
+      aDefinition.SetValue(aDT);
+      anAreaGISU->SetDefinition(aDefinition);
+      Model->ReplaceEntity(aGISUNum, anAreaGISU);
     }
-    if (aCurrentUnit.IsNull()) {
-      Handle(StepGeom_GeomRepContextAndGlobUnitAssCtxAndGlobUncertaintyAssCtx) aCtx1 =
-        Handle(StepGeom_GeomRepContextAndGlobUnitAssCtxAndGlobUncertaintyAssCtx)::DownCast(theRC);
-      if(!aCtx1.IsNull()) {
-        for(Standard_Integer j = 1; j <= aCtx1->NbUnits(); j++) {
-          if (aCtx1->UnitsValue(j)->IsKind(STANDARD_TYPE(StepBasic_SiUnitAndLengthUnit)) ||
-              aCtx1->UnitsValue(j)->IsKind(STANDARD_TYPE(StepBasic_ConversionBasedUnitAndLengthUnit))) {
-            aCurrentUnit = aCtx1->UnitsValue(j);
-            break;
+    else {
+      Handle(StepDimTol_PlacedDatumTargetFeature) aPDTF = new StepDimTol_PlacedDatumTargetFeature();
+      aPDTF->Init(new TCollection_HAsciiString(), STEPCAFControl_GDTProperty::GetDatumTargetName(aDatumType),
+        aPDS, StepData_LTrue, aTargetId);
+      Model->AddWithRefs(aPDTF);
+      aDatumTarget = aPDTF;
+      // Datum targets
+      Handle(StepRepr_PropertyDefinition) aPD = new StepRepr_PropertyDefinition();
+      StepRepr_CharacterizedDefinition aCDefinition;
+      aCDefinition.SetValue(aPDTF);
+      aPD->Init(new TCollection_HAsciiString(), Standard_False, NULL, aCDefinition);
+      if (anObject->HasDatumTargetParams()) {
+        // write all parameters of datum target
+        Handle(StepShape_ShapeRepresentationWithParameters) aSRWP = new StepShape_ShapeRepresentationWithParameters();
+        // Common for all datum targets
+        StepBasic_Unit aUnit = GetUnit(aRC);
+        gp_Ax2 aDTAxis = anObject->GetDatumTargetAxis();
+        GeomToStep_MakeAxis2Placement3d anAxisMaker(aDTAxis);
+        Handle(StepGeom_Axis2Placement3d) anA2P3D = anAxisMaker.Value();
+        anA2P3D->SetName(new TCollection_HAsciiString("orientation"));
+        Handle(StepRepr_HArray1OfRepresentationItem) anItems;
+        // Process each datum target type
+        if (aDatumType == XCAFDimTolObjects_DatumTargetType_Point) {
+          anItems = new StepRepr_HArray1OfRepresentationItem(1, 1);
+        }
+        else  {
+          Handle(TCollection_HAsciiString) aTargetValueName;
+          if (aDatumType == XCAFDimTolObjects_DatumTargetType_Line) {
+            anItems = new StepRepr_HArray1OfRepresentationItem(1, 2);
+            aTargetValueName = new TCollection_HAsciiString("target length");
+          }
+          else if (aDatumType == XCAFDimTolObjects_DatumTargetType_Rectangle) {
+            anItems = new StepRepr_HArray1OfRepresentationItem(1, 3);
+            aTargetValueName = new TCollection_HAsciiString("target length");
+            // Additional value
+            Handle(StepRepr_ReprItemAndMeasureWithUnit) aTargetValue = CreateDimValue(anObject->GetDatumTargetWidth(),
+              aUnit, new TCollection_HAsciiString("target width"), "POSITIVE_LENGTH_MEASURE", Standard_False);
+            anItems->SetValue(2, aTargetValue);
+            Model->AddWithRefs(aTargetValue);
+          }
+          else if (aDatumType == XCAFDimTolObjects_DatumTargetType_Circle) {
+            anItems = new StepRepr_HArray1OfRepresentationItem(1, 2);
+            aTargetValueName = new TCollection_HAsciiString("target diameter");
           }
+          // Value
+          Handle(StepRepr_ReprItemAndMeasureWithUnit) aTargetValue = CreateDimValue(anObject->GetDatumTargetLength(),
+            aUnit, aTargetValueName, "POSITIVE_LENGTH_MEASURE", Standard_False);
+          anItems->SetValue(1, aTargetValue);
+          Model->AddWithRefs(aTargetValue);
         }
+        anItems->SetValue(anItems->Length(), anA2P3D);
+        aSRWP->Init(new TCollection_HAsciiString(), anItems, aRC);
+        // Create and write auxiliary entities
+        Handle(StepShape_ShapeDefinitionRepresentation) aSDR = new StepShape_ShapeDefinitionRepresentation();
+        StepRepr_RepresentedDefinition aRDefinition;
+        aRDefinition.SetValue(aPD);
+        aSDR->Init(aRDefinition, aSRWP);
+        Model->AddWithRefs(aPD);
+        Model->AddWithRefs(aSRWP);
+        Model->AddWithRefs(aSDR);
       }
     }
-    if (aCurrentUnit.IsNull())
-      aCurrentUnit = new StepBasic_SiUnitAndLengthUnit;
+    // Link datum target to datum feature
+    // if aSASeq.Length() == 0 nothing to do
+    if (aSASeq.Length() == 1) {
+      Handle(StepRepr_FeatureForDatumTargetRelationship) aFFDTR = new StepRepr_FeatureForDatumTargetRelationship();
+      aFFDTR->Init(new TCollection_HAsciiString(), Standard_False, NULL, aSASeq.Value(1), aDatumTarget);
+      Model->AddWithRefs(aFFDTR);
+    }
+    else if (aSASeq.Length() > 1) {
+      Handle(StepRepr_CompositeShapeAspect) aCompSA = new StepRepr_CompositeShapeAspect();
+      aCompSA->Init(new TCollection_HAsciiString(), new TCollection_HAsciiString(), aPDS, aSASeq.Value(1)->ProductDefinitional());
+      for (Standard_Integer i = 1; i <= aSASeq.Length(); i++) {
+        Handle(StepRepr_ShapeAspectRelationship) aSAR = new StepRepr_ShapeAspectRelationship();
+        aSAR->Init(new TCollection_HAsciiString(), Standard_False, new TCollection_HAsciiString(), aCompSA, aSASeq.Value(i));
+        Model->AddWithRefs(aSAR);
+      }
+      Handle(StepRepr_FeatureForDatumTargetRelationship) aFFDTR = new StepRepr_FeatureForDatumTargetRelationship();
+      aFFDTR->Init(new TCollection_HAsciiString(), Standard_False, NULL, aCompSA, aDatumTarget);
+      Model->AddWithRefs(aFFDTR);
+    }
+    aSA = aDatumTarget;
   }
 
-  aUnit.SetValue(aCurrentUnit);
-  return aUnit;
+  // Datum
+  Handle(StepDimTol_Datum) aDatum = theWrittenDatum;
+  if (isFirstDTarget) {
+    aDatum = new StepDimTol_Datum();
+    aDatum->Init(new TCollection_HAsciiString(), new TCollection_HAsciiString(), aPDS, StepData_LFalse, anIdentifier);
+    Model->AddWithRefs(aDatum);
+  }
+
+  // Shape_Aspect_Relationship
+  if (!aSA.IsNull()) {
+    Handle(StepRepr_ShapeAspectRelationship) aSAR = new StepRepr_ShapeAspectRelationship();
+    aSAR->Init(new TCollection_HAsciiString(), Standard_False, NULL, aSA, aDatum);
+    Model->AddWithRefs(aSAR);
+  }
+
+  //Annotation plane and Presentation
+  WritePresentation(WS, anObject->GetPresentation(), anObject->GetPresentationName(), Standard_True, anObject->HasPlane(),
+    anObject->GetPlane(), anObject->GetPointTextAttach(), aSA);
+
+  return aDatum;
 }
 
 //=======================================================================
-//function : CreateDimValue
-//purpose  : auxiliary
+//function : WriteDimValues
+//purpose  : auxiliary (write all data for given dimension: values, 
+//           qualifiers, modifiers, orientation and tolerance class)
 //======================================================================
-static Handle(StepRepr_ReprItemAndMeasureWithUnit) CreateDimValue(const Standard_Real theValue,
-                                                                  const StepBasic_Unit theUnit,
-                                                                  const Handle(TCollection_HAsciiString)& theName,
-                                                                  const Standard_CString theMeasureName,
-                                                                  const Standard_Boolean isAngle,
-                                                                  const Standard_Boolean isQualified = Standard_False,
-                                                                  const Handle(StepShape_QualifiedRepresentationItem)& theQRI = NULL)
+static void WriteDimValues(const Handle(XSControl_WorkSession) &WS,
+                           const Handle(XCAFDimTolObjects_DimensionObject) theObject,
+                           const Handle(StepRepr_RepresentationContext) theRC,
+                           const StepShape_DimensionalCharacteristic theDimension)
 {
-  Handle(StepRepr_RepresentationItem) aReprItem = new StepRepr_RepresentationItem();
-  aReprItem->Init(new TCollection_HAsciiString(theName));
-  Handle(StepBasic_MeasureWithUnit) aMWU = new StepBasic_MeasureWithUnit();
-  Handle(StepBasic_MeasureValueMember) aValueMember = new StepBasic_MeasureValueMember();
-  aValueMember->SetName(theMeasureName);
-  aValueMember->SetReal(theValue);
-  aMWU->Init(aValueMember, theUnit);
-  if (isQualified) {
-    if (isAngle) {
-      // Angle & with qualifiers
-      Handle(StepRepr_ReprItemAndPlaneAngleMeasureWithUnitAndQRI) anItem = 
-        new StepRepr_ReprItemAndPlaneAngleMeasureWithUnitAndQRI();
-      anItem->Init(aMWU, aReprItem, theQRI);
-      return anItem;
+  // Get working data
+  const Handle(Interface_InterfaceModel) &aModel = WS->Model();
+  XCAFDimTolObjects_DimensionModifiersSequence aModifiers = theObject->GetModifiers();
+  Handle(Standard_Transient) aDim = theDimension.Value();
+  Standard_Boolean isAngle = aDim->IsKind(STANDARD_TYPE(StepShape_AngularLocation)) ||
+                             aDim->IsKind(STANDARD_TYPE(StepShape_AngularSize));
+
+  // Unit
+  StepBasic_Unit aUnit = GetUnit(theRC, isAngle);
+  Standard_CString aMeasureName;
+  if (isAngle)
+    aMeasureName = "POSITIVE_PLANE_ANGLE_MEASURE";
+  else
+    aMeasureName = "POSITIVE_LENGTH_MEASURE";
+
+  // Values
+  Handle(StepRepr_HArray1OfRepresentationItem) aValues;
+  Standard_Integer aNbItems = 1, aValIt = 1;
+  if (theObject->IsDimWithRange())
+    aNbItems += 2;
+  if (aModifiers.Length() > 0)
+    aNbItems++;
+  if (theObject->GetType() == XCAFDimTolObjects_DimensionType_Location_Oriented)
+    aNbItems++;
+  aNbItems += theObject->NbDescriptions();
+  aValues = new StepRepr_HArray1OfRepresentationItem(1, aNbItems);
+
+  // Nominal value
+  Standard_Real aNominal = theObject->GetValue();
+  Standard_Integer aLeftNbDigits, aRightNbDigits;
+  theObject->GetNbOfDecimalPlaces(aLeftNbDigits, aRightNbDigits);
+  Standard_Integer aNbQualifiers = 0;
+  if (theObject->HasQualifier() && !isAngle)
+    aNbQualifiers++;
+  if (aLeftNbDigits > 0 || aRightNbDigits > 0)
+    aNbQualifiers++;
+  // With qualifiers
+  if (aNbQualifiers > 0) {
+    Handle(StepShape_QualifiedRepresentationItem) aQRI = new StepShape_QualifiedRepresentationItem();
+    Handle(StepShape_HArray1OfValueQualifier) aQualifiers = new StepShape_HArray1OfValueQualifier(1, aNbQualifiers);
+    // Type qualifier
+    if (theObject->HasQualifier() && !isAngle) {
+      StepShape_ValueQualifier anItem;
+      Handle(StepShape_TypeQualifier) aType = new StepShape_TypeQualifier();
+      XCAFDimTolObjects_DimensionQualifier aQualifier = theObject->GetQualifier();
+      aType->Init(STEPCAFControl_GDTProperty::GetDimQualifierName(aQualifier));
+      aModel->AddWithRefs(aType);
+      anItem.SetValue(aType);
+      aQualifiers->SetValue(1, anItem);
     }
-    else {
-      // Length & with qualifiers
-      Handle(StepRepr_ReprItemAndLengthMeasureWithUnitAndQRI) anItem = 
-        new StepRepr_ReprItemAndLengthMeasureWithUnitAndQRI();
-      anItem->Init(aMWU, aReprItem, theQRI);
-      return anItem;
+    // Number of decimal places
+    if (aLeftNbDigits > 0 || aRightNbDigits > 0) {
+      StepShape_ValueQualifier anItem;
+      Handle(StepShape_ValueFormatTypeQualifier) aType = new StepShape_ValueFormatTypeQualifier();
+      Handle(TCollection_HAsciiString) aFormatType = new TCollection_HAsciiString("NR2 ");
+      aFormatType->AssignCat(new TCollection_HAsciiString(aLeftNbDigits));
+      aFormatType->AssignCat(new TCollection_HAsciiString("."));
+      aFormatType->AssignCat(new TCollection_HAsciiString(aRightNbDigits));
+      aType->Init(aFormatType);
+      aModel->AddWithRefs(aType);
+      anItem.SetValue(aType);
+      aQualifiers->SetValue(aNbQualifiers, anItem);
     }
+    // Set qualifiers
+    aQRI->SetQualifiers(aQualifiers);
+    Handle(StepRepr_ReprItemAndMeasureWithUnit) anItem = CreateDimValue(aNominal, aUnit,
+      new TCollection_HAsciiString("nominal value"), aMeasureName, isAngle, Standard_True, aQRI);
+    aValues->SetValue(aValIt, anItem);
+    aValIt++;
   }
+  // Without qualifiers
   else {
-    if (isAngle) {
-      // Angle & without qualifiers
-      Handle(StepRepr_ReprItemAndPlaneAngleMeasureWithUnit) anItem = 
-        new StepRepr_ReprItemAndPlaneAngleMeasureWithUnit();
-      anItem->Init(aMWU, aReprItem);
-      return anItem;
-    }
-    else {
-      // Length & without qualifiers
-      Handle(StepRepr_ReprItemAndLengthMeasureWithUnit) anItem = 
-        new StepRepr_ReprItemAndLengthMeasureWithUnit();
-      anItem->Init(aMWU, aReprItem);
-      return anItem;
-    }
+    Handle(StepRepr_ReprItemAndMeasureWithUnit) anItem = CreateDimValue(aNominal, aUnit,
+      new TCollection_HAsciiString("nominal value"), aMeasureName, isAngle);
+    aValues->SetValue(aValIt, anItem);
+    aValIt++;
   }
-}
 
-//=======================================================================
-//function : WriteShapeAspect
-//purpose  : auxiliary (write Shape_Aspect entity for given shape)
-//=======================================================================
+  // Ranges
+  if (theObject->IsDimWithRange()) {
+    Handle(StepRepr_ReprItemAndMeasureWithUnit) aLowerItem = CreateDimValue(theObject->GetLowerBound(), aUnit,
+      new TCollection_HAsciiString("lower limit"), aMeasureName, isAngle);
+    Handle(StepRepr_ReprItemAndMeasureWithUnit) anUpperItem = CreateDimValue(theObject->GetUpperBound(), aUnit,
+      new TCollection_HAsciiString("upper limit"), aMeasureName, isAngle);
+    aValues->SetValue(aValIt, aLowerItem);
+    aValIt++;
+    aValues->SetValue(aValIt, anUpperItem);
+    aValIt++;
+  }
 
-Handle(StepRepr_ShapeAspect) STEPCAFControl_Writer::WriteShapeAspect (const Handle(XSControl_WorkSession) &WS,
-                                                                      const TDF_Label theLabel,
-                                                                      const TopoDS_Shape theShape,
-                                                                      Handle(StepRepr_RepresentationContext)& theRC,
-                                                                      Handle(StepAP242_GeometricItemSpecificUsage)& theGISU)
-{
-  // Get working data
-  const Handle(Interface_InterfaceModel) &Model = WS->Model();
-  const Handle(XSControl_TransferWriter) &TW = WS->TransferWriter();
-  const Handle(Transfer_FinderProcess) &FP = TW->FinderProcess();
-  const Handle(Interface_HGraph) aHGraph = WS->HGraph();
-  if (aHGraph.IsNull())
-    return NULL;
-  Interface_Graph aGraph = aHGraph->Graph();
+  // Modifiers
+  if (aModifiers.Length() > 0) {
+    Handle(StepRepr_CompoundRepresentationItem) aCompoundRI = new StepRepr_CompoundRepresentationItem();
+    Handle (StepRepr_HArray1OfRepresentationItem) aModifItems = 
+      new StepRepr_HArray1OfRepresentationItem(1, aModifiers.Length());
+    for (Standard_Integer i = 1; i <= aModifiers.Length(); i++) {
+      XCAFDimTolObjects_DimensionModif aModif = aModifiers.Value(i);
+      Handle(StepRepr_DescriptiveRepresentationItem) aModifItem = 
+        new StepRepr_DescriptiveRepresentationItem();
+      aModifItem->Init(new TCollection_HAsciiString(), STEPCAFControl_GDTProperty::GetDimModifierName(aModif));
+      aModel->AddWithRefs(aModifItem);
+      aModifItems->SetValue(i, aModifItem);
+    }
+    aCompoundRI->Init(new TCollection_HAsciiString(), aModifItems);
+    aValues->SetValue(aValIt, aCompoundRI);
+    aValIt++;
+  }
 
-  TopLoc_Location aLoc;
-  TColStd_SequenceOfTransient aSeqRI;
-  FindEntities( FP, theShape, aLoc, aSeqRI );
-  if ( aSeqRI.Length() <= 0 ) {
-    FP->Messenger()->SendInfo() << "Warning: Cannot find RI for "<<theShape.TShape()->DynamicType()->Name()<<std::endl;
-    return NULL;
+  // Orientation
+  if (theObject->GetType() == XCAFDimTolObjects_DimensionType_Location_Oriented) {
+    Handle(StepGeom_Axis2Placement3d) anOrientation = new StepGeom_Axis2Placement3d();
+    gp_Dir aDir;
+    theObject->GetDirection(aDir);
+    GeomToStep_MakeCartesianPoint MkPoint(gp_Pnt(0, 0, 0));
+    Handle(StepGeom_CartesianPoint) aLoc = MkPoint.Value();
+    Handle(StepGeom_Direction) anAxis = new StepGeom_Direction();
+    Handle(TColStd_HArray1OfReal) aCoords = new TColStd_HArray1OfReal(1, 3);
+    aCoords->SetValue(1, aDir.X());
+    aCoords->SetValue(2, aDir.Y());
+    aCoords->SetValue(3, aDir.Z());
+    anAxis->Init(new TCollection_HAsciiString(), aCoords);
+    anOrientation->Init(new TCollection_HAsciiString("orientation"), aLoc, Standard_True, anAxis, Standard_False, NULL);
+    aValues->SetValue(aValIt, anOrientation);
+    aValIt++;
   }
 
-  Handle(StepRepr_ProductDefinitionShape) aPDS;
-  Handle(StepRepr_RepresentationContext) aRC;
-  Handle(Standard_Transient) anEnt = aSeqRI.Value(1);
-  aPDS = FindPDS(aGraph, anEnt, aRC);
-  if(aPDS.IsNull()) 
-    return NULL;
+  // Descriptions
+  if (theObject->HasDescriptions()) {
+    for (Standard_Integer i = 0; i < theObject->NbDescriptions(); i++) {
+      Handle(StepRepr_DescriptiveRepresentationItem) aDRI = new StepRepr_DescriptiveRepresentationItem();
+      aDRI->Init(theObject->GetDescriptionName(i), theObject->GetDescription(i));
+      aValues->SetValue(aValIt, aDRI);
+      aValIt++;
+    }
+  }
 
-  theRC = aRC;
-  // Shape_Aspect
-  Handle(TCollection_HAsciiString) aName = new TCollection_HAsciiString();
-  Handle(TDataStd_Name) aNameAttr;
-  if (theLabel.FindAttribute(TDataStd_Name::GetID(), aNameAttr)) {
-    aName = new TCollection_HAsciiString(TCollection_AsciiString(aNameAttr->Get(), '?'));
-    Standard_Integer aFirstSpace = aName->Search(" ");
-    if (aFirstSpace != -1)
-      aName = aName->SubString(aFirstSpace + 1, aName->Length());
-    else
-      aName = new TCollection_HAsciiString();
+  for (Standard_Integer i = 1; i <= aValues->Length(); i++)
+    aModel->AddWithRefs(aValues->Value(i));
+
+  // Create resulting Shape_Dimension_Representation
+  Handle(StepShape_ShapeDimensionRepresentation) aSDR = new StepShape_ShapeDimensionRepresentation();
+  aSDR->Init(new TCollection_HAsciiString(), aValues, theRC);
+  aModel->AddWithRefs(aSDR);
+  Handle(StepShape_DimensionalCharacteristicRepresentation) aDCR = new StepShape_DimensionalCharacteristicRepresentation();
+  aDCR->Init(theDimension, aSDR);
+  aModel->AddWithRefs(aDCR);
+
+  // Plus_Minus_Tolerance
+  if (theObject->IsDimWithPlusMinusTolerance()) {
+    Handle(TCollection_HAsciiString) aDummyName = new TCollection_HAsciiString(aMeasureName);
+    aDummyName = aDummyName->SubString(9, aDummyName->Length()); //delete "POSITIVE_"
+    aMeasureName = aDummyName->ToCString();
+    Standard_Real aLowerTolValue = -theObject->GetLowerTolValue(),
+                  anUpperTolValue = theObject->GetUpperTolValue();
+    // Upper
+    Handle(StepBasic_MeasureWithUnit) anUpperMWU = new StepBasic_MeasureWithUnit();
+    Handle(StepBasic_MeasureValueMember) anUpperValue = new StepBasic_MeasureValueMember();
+    anUpperValue->SetName(aMeasureName);
+    anUpperValue->SetReal(anUpperTolValue);
+    anUpperMWU->Init(anUpperValue, aUnit);
+    aModel->AddWithRefs(anUpperMWU);
+    // Lower
+    Handle(StepBasic_MeasureWithUnit) aLowerMWU = new StepBasic_MeasureWithUnit();
+    Handle(StepBasic_MeasureValueMember) aLowerValue = new StepBasic_MeasureValueMember();
+    aLowerValue->SetName(aMeasureName);
+    aLowerValue->SetReal(aLowerTolValue);
+    aLowerMWU->Init(aLowerValue, aUnit);
+    aModel->AddWithRefs(aLowerMWU);
+    // Tolerance
+    Handle(StepShape_ToleranceValue) aTolValue = new StepShape_ToleranceValue();
+    aTolValue->Init(aLowerMWU, anUpperMWU);
+    aModel->AddWithRefs(aTolValue);
+    StepShape_ToleranceMethodDefinition aMethod;
+    aMethod.SetValue(aTolValue);
+    Handle(StepShape_PlusMinusTolerance) aPlusMinusTol = new StepShape_PlusMinusTolerance();
+    aPlusMinusTol->Init(aMethod, theDimension);
+    aModel->AddWithRefs(aPlusMinusTol);
   }
-  Handle(TCollection_HAsciiString) aDescription = new TCollection_HAsciiString();
-  Handle(StepRepr_ShapeAspect) aSA = new StepRepr_ShapeAspect;
-  aSA->Init(aName, aDescription, aPDS, StepData_LTrue);
+  // Tolerance class
+  if (theObject->IsDimWithClassOfTolerance()) {
+    Standard_Boolean isHole;
+    XCAFDimTolObjects_DimensionFormVariance aFormVariance;
+    XCAFDimTolObjects_DimensionGrade aGrade;
+    if (!theObject->GetClassOfTolerance(isHole, aFormVariance, aGrade))
+      return;
+    Handle(StepShape_LimitsAndFits) aLAF = STEPCAFControl_GDTProperty::GetLimitsAndFits(isHole, aFormVariance, aGrade);
+    aModel->AddWithRefs(aLAF);
+    StepShape_ToleranceMethodDefinition aMethod;
+    aMethod.SetValue(aLAF);
+    Handle(StepShape_PlusMinusTolerance) aPlusMinusTol = new StepShape_PlusMinusTolerance();
+    aPlusMinusTol->Init(aMethod, theDimension);
+    aModel->AddWithRefs(aPlusMinusTol);
+  }
+}
 
-  // Geometric_Item_Specific_Usage
-  Handle(StepAP242_GeometricItemSpecificUsage) aGISU = new StepAP242_GeometricItemSpecificUsage();
-  StepAP242_ItemIdentifiedRepresentationUsageDefinition aDefinition;
-  aDefinition.SetValue(aSA);
-  Handle(StepRepr_HArray1OfRepresentationItem) anReprItems = new StepRepr_HArray1OfRepresentationItem(1, 1);
-  Handle(StepRepr_RepresentationItem) anIdentifiedItem = Handle(StepRepr_RepresentationItem)::DownCast(anEnt);
-  anReprItems->SetValue(1, anIdentifiedItem);
-  Interface_EntityIterator subs = aGraph.Sharings(aPDS);
-  Handle(StepShape_ShapeDefinitionRepresentation) aSDR;
-  for (subs.Start(); subs.More() && aSDR.IsNull(); subs.Next()) {
-    Handle(Standard_Transient) anEntity = subs.Value();
-    aSDR = Handle(StepShape_ShapeDefinitionRepresentation)::DownCast(anEntity);
+//=======================================================================
+//function : WriteDerivedGeometry
+//purpose  : auxiliary (write connection point for dimensions)
+//======================================================================
+static void WriteDerivedGeometry (const Handle(XSControl_WorkSession) &WS,
+                                  const Handle(XCAFDimTolObjects_DimensionObject)& theObject,
+                                  const Handle(StepRepr_ConstructiveGeometryRepresentation) theRepr,
+                                  Handle(StepRepr_ShapeAspect)& theFirstSA,
+                                  Handle(StepRepr_ShapeAspect)& theSecondSA,
+                                  NCollection_Vector<Handle(StepGeom_CartesianPoint)>& thePnts)
+{
+  const Handle(Interface_InterfaceModel) &aModel = WS->Model();
+  // First point
+  if (theObject->HasPoint()) {
+    GeomToStep_MakeCartesianPoint aPointMaker(theObject->GetPoint());
+    Handle(StepGeom_CartesianPoint) aPoint = aPointMaker.Value();
+    thePnts.Append(aPoint);
+    Handle(StepRepr_DerivedShapeAspect) aDSA = new StepRepr_DerivedShapeAspect();
+    aDSA->Init(new TCollection_HAsciiString(), new TCollection_HAsciiString(), theFirstSA->OfShape(), StepData_LFalse);
+    Handle(StepAP242_GeometricItemSpecificUsage) aGISU = new StepAP242_GeometricItemSpecificUsage();
+    StepAP242_ItemIdentifiedRepresentationUsageDefinition aDefinition;
+    aDefinition.SetValue(aDSA);
+    Handle(StepRepr_HArray1OfRepresentationItem) anItem = new StepRepr_HArray1OfRepresentationItem(1, 1);
+    anItem->SetValue(1, aPoint);
+    aGISU->Init(new TCollection_HAsciiString(), new TCollection_HAsciiString(), aDefinition, theRepr, anItem);
+    Handle(StepRepr_ShapeAspectDerivingRelationship) aSADR = new StepRepr_ShapeAspectDerivingRelationship();
+    aSADR->Init(new TCollection_HAsciiString(), Standard_False, new TCollection_HAsciiString(), aDSA, theFirstSA);
+    theFirstSA = aDSA;
+    aModel->AddWithRefs(aGISU);
+    aModel->AddWithRefs(aSADR);
+  }
+  
+  // Second point (for locations)
+  if (theObject->HasPoint2()) {
+    GeomToStep_MakeCartesianPoint aPointMaker(theObject->GetPoint2());
+    Handle(StepGeom_CartesianPoint) aPoint = aPointMaker.Value();
+    thePnts.Append(aPoint);
+    Handle(StepRepr_DerivedShapeAspect) aDSA = new StepRepr_DerivedShapeAspect();
+    aDSA->Init(new TCollection_HAsciiString(), new TCollection_HAsciiString(), theFirstSA->OfShape(), StepData_LFalse);
+    Handle(StepAP242_GeometricItemSpecificUsage) aGISU = new StepAP242_GeometricItemSpecificUsage();
+    StepAP242_ItemIdentifiedRepresentationUsageDefinition aDefinition;
+    aDefinition.SetValue(aDSA);
+    Handle(StepRepr_HArray1OfRepresentationItem) anItem = new StepRepr_HArray1OfRepresentationItem(1, 1);
+    anItem->SetValue(1, aPoint);
+    aGISU->Init(new TCollection_HAsciiString(), new TCollection_HAsciiString(), aDefinition, theRepr, anItem);
+    Handle(StepRepr_ShapeAspectDerivingRelationship) aSADR = new StepRepr_ShapeAspectDerivingRelationship();
+    aSADR->Init(new TCollection_HAsciiString(), Standard_False, new TCollection_HAsciiString(), aDSA, theSecondSA);
+    theSecondSA = aDSA;
+    aModel->AddWithRefs(aGISU);
+    aModel->AddWithRefs(aSADR);
   }
-  if (aSDR.IsNull())
-    return NULL;
-
-  // Set entities to model
-  aGISU->Init(aName, aDescription, aDefinition, aSDR->UsedRepresentation(), anReprItems);
-  Model->AddWithRefs(aSA);
-  Model->AddWithRefs(aGISU);
-  theGISU = aGISU;
-  return aSA;
 }
 
 //=======================================================================
-//function : WritePresentation
-//purpose  : auxiliary (write annotation plane and presentation)
+//function : WriteDatumSystem
+//purpose  : auxiliary (write Write datum system for given
+//           geometric_tolerance)
 //======================================================================
-void STEPCAFControl_Writer::WritePresentation(const Handle(XSControl_WorkSession) &WS,
-                                              const TopoDS_Shape thePresentation,
-                                              const Handle(TCollection_HAsciiString)& thePrsName,
-                                              const Standard_Boolean hasSemantic,
-                                              const Standard_Boolean hasPlane,
-                                              const gp_Ax2 theAnnotationPlane,
-                                              const gp_Pnt theTextPosition,
-                                              const Handle(Standard_Transient) theDimension)
+static Handle(StepDimTol_HArray1OfDatumSystemOrReference) WriteDatumSystem(const Handle(XSControl_WorkSession) &WS,
+                                                                           const TDF_Label theGeomTolL,
+                                                                           const TDF_LabelSequence theDatumSeq,
+                                                                           const STEPConstruct_DataMapOfAsciiStringTransient theDatumMap,
+                                                                           const Handle(StepRepr_RepresentationContext)& theRC)
 {
-  if (thePresentation.IsNull())
-    return;
   // Get working data
-  const Handle(Interface_InterfaceModel) &aModel = WS->Model();
+  const Handle(Interface_InterfaceModel) &Model = WS->Model();
+  const Handle(Interface_HGraph) aHGraph = WS->HGraph();
+  if (aHGraph.IsNull())
+    return NULL;
+  Interface_Graph aGraph = aHGraph->Graph();
+  Handle(XCAFDoc_GeomTolerance) aGTAttr;
+  if (!theGeomTolL.FindAttribute(XCAFDoc_GeomTolerance::GetID(), aGTAttr)) 
+    return NULL;
+  Handle(XCAFDimTolObjects_GeomToleranceObject) anObject = aGTAttr->GetObject();
+  if (anObject.IsNull())
+    return NULL;
 
-  // Presentation
-  Handle(StepVisual_TessellatedGeometricSet) aGeomSet = STEPCAFControl_GDTProperty::GetTessellation(thePresentation);
-  Handle(StepVisual_TessellatedAnnotationOccurrence) aTAO = new StepVisual_TessellatedAnnotationOccurrence();
-  aTAO->Init(new TCollection_HAsciiString(), myGDTPrsCurveStyle, aGeomSet);
-  StepVisual_DraughtingCalloutElement aDCElement;
-  aDCElement.SetValue(aTAO);
-  Handle(StepVisual_HArray1OfDraughtingCalloutElement) aTAOs = new StepVisual_HArray1OfDraughtingCalloutElement(1, 1);
-  aTAOs->SetValue(1, aDCElement);
-  Handle(StepVisual_DraughtingCallout) aDCallout = new StepVisual_DraughtingCallout();
-  Handle(TCollection_HAsciiString) aPrsName = thePrsName.IsNull() ? new TCollection_HAsciiString() : thePrsName;
-  aDCallout->Init(aPrsName, aTAOs);
-  Handle(StepRepr_HArray1OfRepresentationItem) aDCsForDMIA = new StepRepr_HArray1OfRepresentationItem(1, 1);
-  aDCsForDMIA->SetValue(1, aDCallout);
-  myGDTAnnotations.Append(aDCallout);
-  StepAP242_ItemIdentifiedRepresentationUsageDefinition aDimension;
-  aDimension.SetValue(theDimension);
-  Handle(TCollection_HAsciiString) aDMIAName;
-  if (hasSemantic)
-    aDMIAName = new TCollection_HAsciiString("PMI representation to presentation link");
-  else
-    aDMIAName = new TCollection_HAsciiString();
-  Handle(StepAP242_DraughtingModelItemAssociation) aDMIA =
-    new StepAP242_DraughtingModelItemAssociation();
-  aDMIA->Init(aDMIAName, new TCollection_HAsciiString(), aDimension, myGDTPresentationDM, aDCsForDMIA);
-  aModel->AddWithRefs(aDMIA);
+  // Unit
+  StepBasic_Unit aUnit = GetUnit(theRC);
 
-  if (!hasPlane)
-    return;
+  XCAFDimTolObjects_DatumObjectSequence aDatums;
+  Standard_Integer aMaxDatumNum = 0;
+  for (Standard_Integer i = 1; i <= theDatumSeq.Length(); i++) {
+    Handle(XCAFDoc_Datum) aDatumAttr;
+    if (!theDatumSeq.Value(i).FindAttribute(XCAFDoc_Datum::GetID(), aDatumAttr)) 
+      continue;
+    Handle(XCAFDimTolObjects_DatumObject) aDatumObj = aDatumAttr->GetObject();
+    if (aDatumObj.IsNull())
+      continue;
+    aDatums.Append(aDatumObj);
+    aMaxDatumNum = Max(aMaxDatumNum, aDatumObj->GetPosition());
+  }
+  if (aMaxDatumNum == 0)
+    return NULL;
 
-  // Annotation plane
-  // Presentation Style
-  Handle(StepVisual_NullStyleMember) aNullStyle = new StepVisual_NullStyleMember();
-  aNullStyle->SetEnumText(0, ".NULL.");
-  StepVisual_PresentationStyleSelect aStyleItem;
-  aStyleItem.SetValue(aNullStyle);
-  Handle(StepVisual_HArray1OfPresentationStyleSelect) aStyles = new StepVisual_HArray1OfPresentationStyleSelect(1, 1);
-  aStyles->SetValue(1, aStyleItem);
-  Handle(StepVisual_PresentationStyleAssignment) aPrsStyle = new StepVisual_PresentationStyleAssignment();
-  aPrsStyle->Init(aStyles);
-  Handle(StepVisual_HArray1OfPresentationStyleAssignment) aPrsStyles =
-    new StepVisual_HArray1OfPresentationStyleAssignment(1, 1);
-  aPrsStyles->SetValue(1, aPrsStyle);
-  // Plane
-  Handle(StepGeom_Plane) aPlane = new StepGeom_Plane();
-  GeomToStep_MakeAxis2Placement3d anAxisMaker(theAnnotationPlane);
-  Handle(StepGeom_Axis2Placement3d) anAxis = anAxisMaker.Value();
-  // Set text position to plane origin
-  Handle(StepGeom_CartesianPoint) aTextPos = new StepGeom_CartesianPoint();
-  Handle(TColStd_HArray1OfReal) aCoords = new TColStd_HArray1OfReal(1, 3);
-  for (Standard_Integer i = 1; i <= 3; i++)
-    aCoords->SetValue(i, theTextPosition.Coord(i));
-  aTextPos->Init(new TCollection_HAsciiString(), aCoords);
-  anAxis->SetLocation(aTextPos);
-  aPlane->Init(new TCollection_HAsciiString(), anAxis);
-  // Annotation plane element
-  StepVisual_AnnotationPlaneElement aPlaneElement;
-  aPlaneElement.SetValue(aDCallout);
-  Handle(StepVisual_HArray1OfAnnotationPlaneElement) aDCsForAnnPln = new StepVisual_HArray1OfAnnotationPlaneElement(1, 1);
-  aDCsForAnnPln->SetValue(1, aPlaneElement);
-  // Init AnnotationPlane entity
-  Handle(StepVisual_AnnotationPlane) anAnnPlane = new StepVisual_AnnotationPlane();
-  anAnnPlane->Init(new TCollection_HAsciiString(), aPrsStyles, aPlane, aDCsForAnnPln);
-  myGDTAnnotations.Append(anAnnPlane);
-  aModel->AddWithRefs(anAnnPlane);
+  Handle(StepDimTol_HArray1OfDatumReferenceCompartment) aConstituents =
+    new StepDimTol_HArray1OfDatumReferenceCompartment(1, aMaxDatumNum);
+  // Auxiliary datum to initialize attributes in Datum_System
+  Handle(StepDimTol_Datum) aFirstDatum;
+  Standard_Integer aConstituentsNum = 0;
+  for (Standard_Integer i = 1; i <= aMaxDatumNum; i++) {
+    // Collect datums with i-th position
+    XCAFDimTolObjects_DatumObjectSequence aDatumSeqPos;
+    for (Standard_Integer j = 1; j <= aDatums.Length(); j++)
+      if (aDatums.Value(j)->GetPosition() == i)
+        aDatumSeqPos.Append(aDatums.Value(j));
+    if (aDatumSeqPos.Length() < 1)
+      continue;
+
+    aConstituentsNum++;
+    // Initialize Datum_Reference_Compartment
+    StepDimTol_DatumOrCommonDatum aDatumRef;
+    Handle(StepDimTol_DatumReferenceCompartment) aCompartment =
+      new StepDimTol_DatumReferenceCompartment();
+    Handle(StepDimTol_HArray1OfDatumReferenceModifier) aModifiers;
+    if (aDatumSeqPos.Length() == 1) {
+      // Datum entity
+      Handle(Standard_Transient) aFDValue;
+      if (theDatumMap.Find(aDatumSeqPos.Value(1)->GetName()->String(), aFDValue) && !aFDValue.IsNull())
+        aFirstDatum = Handle(StepDimTol_Datum)::DownCast (aFDValue);
+      aDatumRef.SetValue(aFirstDatum);
+      // Modifiers
+      XCAFDimTolObjects_DatumModifiersSequence aSimpleModifiers = aDatumSeqPos.Value(1)->GetModifiers();
+      XCAFDimTolObjects_DatumModifWithValue aModifWithVal;
+      Standard_Real aValue = 0;
+      aDatumSeqPos.Value(1)->GetModifierWithValue(aModifWithVal, aValue);
+      aModifiers = STEPCAFControl_GDTProperty::GetDatumRefModifiers(aSimpleModifiers, aModifWithVal, aValue, aUnit);
+      // Add Datum_Reference_Modifier_With_Value
+      if (!aModifiers.IsNull()) {
+        Handle(StepDimTol_DatumReferenceModifierWithValue) aDRMWV = 
+          aModifiers->Value(aModifiers->Length()).DatumReferenceModifierWithValue();
+        if (!aDRMWV.IsNull()) {
+          Model->AddWithRefs(aDRMWV);
+        }
+      }
+    }
+    else {
+      Handle(StepDimTol_HArray1OfDatumReferenceElement) aCommonDatumList = new StepDimTol_HArray1OfDatumReferenceElement(1, aDatumSeqPos.Length());
+      for (Standard_Integer j = 1; j <= aDatumSeqPos.Length(); j++) {
+        // Datum entity
+        Handle(StepDimTol_Datum) aDatum;
+        Handle(Standard_Transient) aDValue;
+        if (theDatumMap.Find(aDatumSeqPos.Value(j)->GetName()->String(), aDValue))
+          aDatum = Handle(StepDimTol_Datum)::DownCast (aDValue);
+        StepDimTol_DatumOrCommonDatum anElemDatumRef;
+        anElemDatumRef.SetValue(aDatum);
+        if (aFirstDatum.IsNull())
+          aFirstDatum = aDatum;
+        // Modifiers
+        XCAFDimTolObjects_DatumModifiersSequence aSimpleModifiers = aDatumSeqPos.Value(j)->GetModifiers();
+        XCAFDimTolObjects_DatumModifWithValue aModifWithVal;
+        Standard_Real aValue = 0;
+        aDatumSeqPos.Value(j)->GetModifierWithValue(aModifWithVal, aValue);
+        Handle(StepDimTol_HArray1OfDatumReferenceModifier) anElemModifiers =
+          STEPCAFControl_GDTProperty::GetDatumRefModifiers(aSimpleModifiers, aModifWithVal, aValue, aUnit);
+        // Add Datum_Reference_Modifier_With_Value
+        if (!anElemModifiers.IsNull()) {
+          Handle(StepDimTol_DatumReferenceModifierWithValue) aDRMWV = 
+            anElemModifiers->Value(anElemModifiers->Length()).DatumReferenceModifierWithValue();
+          if (!aDRMWV.IsNull()) {
+            Model->AddWithRefs(aDRMWV);
+          }
+        }
+        // Datum_Reference_Element
+        Handle(StepDimTol_DatumReferenceElement) anElement = new StepDimTol_DatumReferenceElement();
+        anElement->Init(new TCollection_HAsciiString(), new TCollection_HAsciiString(), aDatum->OfShape(),
+          aDatum->ProductDefinitional(), anElemDatumRef, !anElemModifiers.IsNull(), anElemModifiers);
+        Model->AddWithRefs(anElement);
+        aCommonDatumList->SetValue(j, anElement);
+      }
+      aDatumRef.SetValue(aCommonDatumList);
+    }
+    aCompartment->Init(new TCollection_HAsciiString(), new TCollection_HAsciiString(), aFirstDatum->OfShape(),
+      aFirstDatum->ProductDefinitional(), aDatumRef, !aModifiers.IsNull(), aModifiers);
+    Model->AddWithRefs(aCompartment);
+    aConstituents->SetValue(aConstituentsNum, aCompartment);
+  }
+  // Remove null elements from aConstituents
+  Standard_Integer aNbConstituents = 0;
+  for (Standard_Integer i = 1; i <= aConstituents->Length(); i++)
+    if (!aConstituents->Value(i).IsNull())
+      aNbConstituents++;
+  Handle(StepDimTol_HArray1OfDatumReferenceCompartment) aResConstituents =
+    new StepDimTol_HArray1OfDatumReferenceCompartment(1, aNbConstituents);
+  Standard_Integer aConstituentsIt = 0;
+  for (Standard_Integer i = 1; i <= aConstituents->Length(); i++)
+    if (!aConstituents->Value(i).IsNull()) {
+      aConstituentsIt++;
+      aResConstituents->SetValue(aConstituentsIt, aConstituents->Value(i));
+    }
+
+  Handle(StepDimTol_HArray1OfDatumSystemOrReference) aDatumSystem;
+  Handle(StepDimTol_DatumSystem) aDS = new StepDimTol_DatumSystem();
+  aDS->Init(new TCollection_HAsciiString(), new TCollection_HAsciiString(), aFirstDatum->OfShape(),
+    aFirstDatum->ProductDefinitional(), aResConstituents);
+  Model->AddWithRefs(aDS);
+  StepDimTol_DatumSystemOrReference anArrayValue;
+  anArrayValue.SetValue(aDS);
+  aDatumSystem = new StepDimTol_HArray1OfDatumSystemOrReference(1, 1);
+  aDatumSystem->SetValue(1, anArrayValue);
+
+  // Axis
+  if (anObject->HasAxis()) {
+    GeomToStep_MakeAxis2Placement3d anAxisMaker(anObject->GetAxis());
+    Handle(StepGeom_Axis2Placement3d) anAxis = anAxisMaker.Value();
+    anAxis->SetName(new TCollection_HAsciiString("orientation"));
+    Handle(StepAP242_GeometricItemSpecificUsage) aGISU = new StepAP242_GeometricItemSpecificUsage();
+    StepAP242_ItemIdentifiedRepresentationUsageDefinition aDefinition;
+    aDefinition.SetValue(aDS);
+    Handle(StepRepr_HArray1OfRepresentationItem) anReprItems = new StepRepr_HArray1OfRepresentationItem(1, 1);
+    Handle(StepRepr_RepresentationItem) anIdentifiedItem = anAxis;
+    anReprItems->SetValue(1, anIdentifiedItem);
+    Interface_EntityIterator subs = aGraph.Sharings(aFirstDatum->OfShape());
+    Handle(StepShape_ShapeDefinitionRepresentation) aSDR;
+    for (subs.Start(); subs.More() && aSDR.IsNull(); subs.Next()) {
+      Handle(Standard_Transient) anEntity = subs.Value();
+      aSDR = Handle(StepShape_ShapeDefinitionRepresentation)::DownCast(anEntity);
+    }
+    if (aSDR.IsNull())
+      return aDatumSystem;
+    
+    aGISU->Init(new TCollection_HAsciiString(), new TCollection_HAsciiString(),
+      aDefinition, aSDR->UsedRepresentation(), anReprItems);
+    Model->AddWithRefs(anAxis);
+    Model->AddWithRefs(aGISU);
+  }
+
+  return aDatumSystem;
 }
 
 //=======================================================================
-//function : WriteDatumAP242
-//purpose  : auxiliary (write Datum entity for given shape or write all 
-//           necessary entities and link them to already written datum 
-//           in case of multiple features association)
+//function : WriteToleranceZone
+//purpose  : auxiliary (write tolerace zones)
 //=======================================================================
-Handle(StepDimTol_Datum) STEPCAFControl_Writer::WriteDatumAP242(const Handle(XSControl_WorkSession) &WS,
-                                                                const TDF_LabelSequence theShapeL,
-                                                                const TDF_Label theDatumL,
-                                                                const Standard_Boolean isFirstDTarget,
-                                                                const Handle(StepDimTol_Datum) theWrittenDatum)
+void STEPCAFControl_Writer::WriteToleranceZone (const Handle(XSControl_WorkSession) &WS,
+                                                const Handle(XCAFDimTolObjects_GeomToleranceObject)& theObject,
+                                                const Handle(StepDimTol_GeometricTolerance)& theEntity,
+                                                const Handle(StepRepr_RepresentationContext)& theRC)
 {
   // Get working data
   const Handle(Interface_InterfaceModel) &Model = WS->Model();
-  const Handle(XSControl_TransferWriter) &TW = WS->TransferWriter();
-  const Handle(Transfer_FinderProcess) &FP = TW->FinderProcess();
-  const Handle(Interface_HGraph) aHGraph = WS->HGraph();
-  if (aHGraph.IsNull())
-    return NULL;
-  Interface_Graph aGraph = aHGraph->Graph();
+  if (theEntity.IsNull() || theObject.IsNull())
+    return;
 
-  Handle(StepRepr_ShapeAspect) aSA;
-  Handle(StepRepr_RepresentationContext) aRC;
-  Handle(StepRepr_ProductDefinitionShape) aPDS;
-  NCollection_Sequence<Handle(StepRepr_ShapeAspect)> aSASeq;
-  Handle(StepAP242_GeometricItemSpecificUsage) aGISU;
-  Standard_Integer aSANum = 0, aGISUNum = 0;
-  // Link with datum feature
-  for (Standard_Integer i = 1; i <= theShapeL.Length(); i++) {
-    Handle(Standard_Transient) anEnt;
-    TopoDS_Shape aShape;
-    TopLoc_Location aLoc;
-    TColStd_SequenceOfTransient aSeqRI;
+  // Return if there is no tolerance zones
+  if (theObject->GetTypeOfValue() == XCAFDimTolObjects_GeomToleranceTypeValue_None &&
+      theObject->GetZoneModifier() != XCAFDimTolObjects_GeomToleranceZoneModif_Runout)
+    return;
 
-    aShape = XCAFDoc_ShapeTool::GetShape(theShapeL.Value(i));
-    FindEntities(FP, aShape, aLoc, aSeqRI);
-    if (aSeqRI.Length() <= 0) {
-      FP->Messenger()->SendInfo() << "Warning: Cannot find RI for " << aShape.TShape()->DynamicType()->Name() << std::endl;
-      continue;
-    }
-    anEnt = aSeqRI.Value(1);
-    aPDS = FindPDS(aGraph, anEnt, aRC);
-    if (aPDS.IsNull())
-      continue;
+  // Create Tolerance_Zone
+  Handle(StepDimTol_ToleranceZoneForm) aForm = new StepDimTol_ToleranceZoneForm();
+  Model->AddWithRefs(aForm);
+  aForm->Init(STEPCAFControl_GDTProperty::GetTolValueType(theObject->GetTypeOfValue()));
+  Handle(StepDimTol_HArray1OfToleranceZoneTarget) aZoneTargetArray = new StepDimTol_HArray1OfToleranceZoneTarget(1, 1);
+  StepDimTol_ToleranceZoneTarget aTarget;
+  aTarget.SetValue(theEntity);
+  aZoneTargetArray->SetValue(1, aTarget);
+  Handle(StepDimTol_ToleranceZone) aZone = new StepDimTol_ToleranceZone();
+  aZone->Init(new TCollection_HAsciiString(), new TCollection_HAsciiString(),
+    theEntity->TolerancedShapeAspect().ShapeAspect()->OfShape(), StepData_LFalse,
+    aZoneTargetArray, aForm);
+  Model->AddWithRefs(aZone);
 
-    Handle(StepRepr_ShapeAspect) aCurrentSA = WriteShapeAspect(WS, theDatumL, aShape, aRC, aGISU);
-    if (aCurrentSA.IsNull())
-      continue;
-    aSASeq.Append(aCurrentSA);
-    aSANum = Model->Number(aCurrentSA);
-    aGISUNum = Model->Number(aGISU);
+  // Runout_Tolerance_Zone
+  Handle(StepBasic_PlaneAngleMeasureWithUnit) aPAMWU = new StepBasic_PlaneAngleMeasureWithUnit();
+  Handle(StepBasic_MeasureValueMember) aValueMember = new StepBasic_MeasureValueMember();
+  aValueMember->SetName("PLANE_ANGLE_MEASURE");
+  aValueMember->SetReal(theObject->GetValueOfZoneModifier());
+  aPAMWU->Init(aValueMember, GetUnit(theRC, Standard_True));
+  Handle(StepDimTol_RunoutZoneOrientation) anOrientation = new StepDimTol_RunoutZoneOrientation();
+  anOrientation->Init(aPAMWU);
+  Handle(StepDimTol_RunoutZoneDefinition) aDefinition = new StepDimTol_RunoutZoneDefinition();
+  aDefinition->Init(aZone, NULL, anOrientation);
+  Model->AddWithRefs(aDefinition);
+  Model->AddWithRefs(anOrientation);
+  Model->AddWithRefs(aPAMWU);
+}
+
+//=======================================================================
+//function : WriteGeomTolerance
+//purpose  : auxiliary (write Geometric_Tolerance entity for given shapes,
+//           label and datum system)
+//======================================================================
+void STEPCAFControl_Writer::WriteGeomTolerance (const Handle(XSControl_WorkSession) &WS,
+                                                const TDF_LabelSequence theShapeSeqL,
+                                                const TDF_Label theGeomTolL,
+                                                const Handle(StepDimTol_HArray1OfDatumSystemOrReference)& theDatumSystem,
+                                                const Handle(StepRepr_RepresentationContext)& theRC)
+{
+  // Get working data
+  const Handle(Interface_InterfaceModel) &Model = WS->Model();
+  Handle(XCAFDoc_GeomTolerance) aGTAttr;
+  if (!theGeomTolL.FindAttribute(XCAFDoc_GeomTolerance::GetID(), aGTAttr)) 
+    return;
+  Handle(XCAFDimTolObjects_GeomToleranceObject) anObject = aGTAttr->GetObject();
+  if (anObject.IsNull())
+    return;
+
+  // Value
+  Handle(StepBasic_LengthMeasureWithUnit) aLMWU = new StepBasic_LengthMeasureWithUnit();
+  StepBasic_Unit aUnit = GetUnit(theRC);
+  Handle(StepBasic_MeasureValueMember) aValueMember = new StepBasic_MeasureValueMember();
+  aValueMember->SetName("LENGTH_MEASURE");
+  aValueMember->SetReal(anObject->GetValue());
+  aLMWU->Init(aValueMember, aUnit);
+  Model->AddWithRefs(aLMWU);
+
+  // Geometric_Tolerance target
+  Handle(StepRepr_ShapeAspect) aMainSA;
+  Handle(StepRepr_RepresentationContext) dummyRC;
+  Handle(StepAP242_GeometricItemSpecificUsage) dummyGISU;
+  if (theShapeSeqL.Length() == 1) {
+    TopoDS_Shape aShape = XCAFDoc_ShapeTool::GetShape(theShapeSeqL.Value(1));
+    aMainSA = WriteShapeAspect(WS, theGeomTolL, aShape, dummyRC, dummyGISU);
+    Model->AddWithRefs(aMainSA);
   }
-  if (aPDS.IsNull()) {
-    // Workaround for datums without shape
-    aPDS = myGDTCommonPDS;
-    Interface_EntityIterator aSDRIt = aGraph.Sharings(aPDS);
-    Handle(StepShape_ShapeDefinitionRepresentation) aSDR;
-    for (aSDRIt.Start(); aSDRIt.More() && aSDR.IsNull(); aSDRIt.Next())
-      aSDR = Handle(StepShape_ShapeDefinitionRepresentation)::DownCast(aSDRIt.Value());
-    if (!aSDR.IsNull()) {
-      Handle(StepRepr_Representation) aRepr = aSDR->UsedRepresentation();
-      if (!aRepr.IsNull())
-        aRC = aRepr->ContextOfItems();
+  else {
+    Handle(StepRepr_CompositeShapeAspect) aCSA;
+    for (Standard_Integer i = 1; i <= theShapeSeqL.Length(); i++) {
+      TopoDS_Shape aShape = XCAFDoc_ShapeTool::GetShape(theShapeSeqL.Value(i));
+      Handle(StepRepr_ShapeAspect) aSA = WriteShapeAspect(WS, theGeomTolL, aShape, dummyRC, dummyGISU);
+      if (aSA.IsNull())
+        continue;
+      if (aCSA.IsNull()) {
+        aCSA = new StepRepr_CompositeShapeAspect();
+        aCSA->Init(aSA->Name(), aSA->Description(), aSA->OfShape(), aSA->ProductDefinitional());
+        Model->AddWithRefs(aCSA);
+      }
+      Handle(StepRepr_ShapeAspectRelationship) aSAR = new StepRepr_ShapeAspectRelationship();
+      aSAR->Init(new TCollection_HAsciiString(), Standard_False, NULL, aCSA, aSA);
+      Model->AddWithRefs(aSAR);
     }
+    aMainSA = aCSA;
   }
+  StepDimTol_GeometricToleranceTarget aGTTarget;
+  aGTTarget.SetValue(aMainSA);
 
+  Standard_Boolean isWithModif = Standard_False,
+                   isWithDatRef = Standard_False,
+                   isWithMaxTol = Standard_False;
+  // Modifiers
+  // Simple modifiers
+  XCAFDimTolObjects_GeomToleranceModifiersSequence aModifiers = anObject->GetModifiers();
+  Handle(StepDimTol_HArray1OfGeometricToleranceModifier) aModifArray;
+  Handle(StepBasic_LengthMeasureWithUnit) aMaxLMWU;
+  Standard_Integer aModifNb = aModifiers.Length();
+  if (anObject->GetMaterialRequirementModifier() != XCAFDimTolObjects_GeomToleranceMatReqModif_None)
+      aModifNb++;
+  for (Standard_Integer i = 1; i <= aModifiers.Length(); i++)
+    if (aModifiers.Value(i) == XCAFDimTolObjects_GeomToleranceModif_All_Around ||
+        aModifiers.Value(i) == XCAFDimTolObjects_GeomToleranceModif_All_Over)
+        aModifNb--;
+  if (aModifNb > 0) {
+    isWithModif = Standard_True;
+    aModifArray = new StepDimTol_HArray1OfGeometricToleranceModifier(1, aModifNb);
+    Standard_Integer k = 1;
+    for (Standard_Integer i = 1; i <= aModifiers.Length(); i++) {
+      if (aModifiers.Value(i) == XCAFDimTolObjects_GeomToleranceModif_All_Around ||
+        aModifiers.Value(i) == XCAFDimTolObjects_GeomToleranceModif_All_Over)
+        continue;
+      StepDimTol_GeometricToleranceModifier aModif = 
+        STEPCAFControl_GDTProperty::GetGeomToleranceModifier(aModifiers.Value(i));
+      aModifArray->SetValue(k, aModif);
+      k++;
+    }
+    if (anObject->GetMaterialRequirementModifier() == XCAFDimTolObjects_GeomToleranceMatReqModif_L) {
+      aModifArray->SetValue(aModifNb, StepDimTol_GTMLeastMaterialRequirement);
+    }
+    else if (anObject->GetMaterialRequirementModifier() == XCAFDimTolObjects_GeomToleranceMatReqModif_M) {
+      aModifArray->SetValue(aModifNb, StepDimTol_GTMMaximumMaterialRequirement);
+    }
+    // Modifier with value
+    if (anObject->GetMaxValueModifier() != 0) {
+      isWithMaxTol = Standard_True;
+      aMaxLMWU = new StepBasic_LengthMeasureWithUnit();
+      Handle(StepBasic_MeasureValueMember) aModifierValueMember = new StepBasic_MeasureValueMember();
+      aModifierValueMember->SetName("LENGTH_MEASURE");
+      aModifierValueMember->SetReal(anObject->GetMaxValueModifier());
+      aMaxLMWU->Init(aModifierValueMember, aUnit);
+      Model->AddWithRefs(aMaxLMWU);
+    }
+  }
 
-  // Find if datum has datum targets and get common datum attributes
-  Handle(XCAFDoc_Datum) aDatumAttr;
-  if (!theDatumL.FindAttribute(XCAFDoc_Datum::GetID(), aDatumAttr)) 
-    return NULL;
-  Handle(XCAFDimTolObjects_DatumObject) anObject = aDatumAttr->GetObject();
-  if (anObject.IsNull())
-    return NULL;
-  Standard_Boolean isSimpleDatum = !anObject->IsDatumTarget();
-  Handle(TCollection_HAsciiString) anIdentifier = anObject->GetName();
-  Handle(TCollection_HAsciiString) aTargetId = (anObject->GetDatumTargetNumber() == 0 ? 
-    new TCollection_HAsciiString() : new TCollection_HAsciiString(anObject->GetDatumTargetNumber()));
+  // Datum Reference
+  isWithDatRef = !theDatumSystem.IsNull();
 
-  // If datum type is area, but there is no area in object, write as simple datum
-  if (anObject->GetDatumTargetType() == XCAFDimTolObjects_DatumTargetType_Area &&
-    anObject->GetDatumTarget().IsNull())
-    isSimpleDatum = Standard_True;
+  // Collect all attributes
+  Handle(TCollection_HAsciiString) aName = new TCollection_HAsciiString(),
+                                  aDescription = new TCollection_HAsciiString();
+  Handle(StepDimTol_GeometricToleranceWithDatumReference) aGTWDR = 
+    new StepDimTol_GeometricToleranceWithDatumReference();
+  aGTWDR->SetDatumSystem(theDatumSystem);
+  Handle(StepDimTol_GeometricToleranceWithModifiers) aGTWM = 
+    new StepDimTol_GeometricToleranceWithModifiers();
+  aGTWM->SetModifiers(aModifArray);
+  StepDimTol_GeometricToleranceType aType = 
+    STEPCAFControl_GDTProperty::GetGeomToleranceType(anObject->GetType());
 
-  // Simple datum
-  if (isSimpleDatum) {
-    if (aSASeq.Length() == 0) {
-      // Create empty datum with name and presentation only
-      Handle(StepDimTol_DatumFeature) aDF = new StepDimTol_DatumFeature();
-      aDF->Init(new TCollection_HAsciiString(), new TCollection_HAsciiString(), aPDS, StepData_LTrue);
-      aSA = aDF;
-      Model->AddWithRefs(aDF);
-    }
-    else if (aSASeq.Length() == 1) {
-      Handle(StepDimTol_DatumFeature) aDF = new StepDimTol_DatumFeature();
-      aDF->Init(new TCollection_HAsciiString(), new TCollection_HAsciiString(), aPDS, StepData_LTrue);
-      Model->ReplaceEntity(aSANum, aDF);
-      aSA = aDF;
-      StepAP242_ItemIdentifiedRepresentationUsageDefinition aDefinition;
-      aDefinition.SetValue(aDF);
-      aGISU->SetDefinition(aDefinition);
-      Model->ReplaceEntity(aGISUNum, aGISU);
+  // Init and write necessary subtype of Geometric_Tolerance entity
+  Handle(StepDimTol_GeometricTolerance) aGeomTol;
+  if (isWithModif) {
+    if (isWithMaxTol) {
+      if (isWithDatRef) {
+        // Geometric_Tolerance & Geometric_Tolerance_With_Datum_Reference & 
+        //Geometric_Tolerance_With_Maximum_Tolerance & Geometric_Tolerance_With_Modifiers
+        Handle(StepDimTol_GeoTolAndGeoTolWthDatRefAndGeoTolWthMaxTol) aResult =
+          new StepDimTol_GeoTolAndGeoTolWthDatRefAndGeoTolWthMaxTol();
+        aResult->Init(aName, aDescription, aLMWU, aGTTarget, aGTWDR, aGTWM, aMaxLMWU, aType);
+        aGeomTol = aResult;
+      }
+      else {
+        // Geometric_Tolerance & Geometric_Tolerance_With_Maximum_Tolerance & Geometric_Tolerance_With_Modifiers
+        Handle(StepDimTol_GeoTolAndGeoTolWthMaxTol) aResult =
+          new StepDimTol_GeoTolAndGeoTolWthMaxTol();
+        aResult->Init(aName, aDescription, aLMWU, aGTTarget, aGTWM, aMaxLMWU, aType);
+        aGeomTol = aResult;
+      }
     }
-    else if (aSASeq.Length() > 1) {
-      Handle(StepRepr_CompShAspAndDatumFeatAndShAsp) aDF = new StepRepr_CompShAspAndDatumFeatAndShAsp();
-      aDF->Init(new TCollection_HAsciiString(), new TCollection_HAsciiString(), aPDS, StepData_LTrue);
-      for (Standard_Integer i = 1; i <= aSASeq.Length(); i++) {
-        Handle(StepRepr_ShapeAspectRelationship) aSAR = new StepRepr_ShapeAspectRelationship();
-        aSAR->Init(new TCollection_HAsciiString(), Standard_False, new TCollection_HAsciiString(), aDF, aSASeq.Value(i));
-        Model->AddWithRefs(aSAR);
+    else {
+      if (isWithDatRef) {
+        // Geometric_Tolerance & Geometric_Tolerance_With_Datum_Reference & Geometric_Tolerance_With_Modifiers
+        Handle(StepDimTol_GeoTolAndGeoTolWthDatRefAndGeoTolWthMod) aResult =
+          new StepDimTol_GeoTolAndGeoTolWthDatRefAndGeoTolWthMod();
+        aResult->Init(aName, aDescription, aLMWU, aGTTarget, aGTWDR, aGTWM, aType);
+        aGeomTol = aResult;
+      }
+      else {
+        // Geometric_Tolerance & Geometric_Tolerance_With_Modifiers
+        Handle(StepDimTol_GeoTolAndGeoTolWthMod) aResult =
+          new StepDimTol_GeoTolAndGeoTolWthMod();
+        aResult->Init(aName, aDescription, aLMWU, aGTTarget, aGTWM, aType);
+        aGeomTol = aResult;
       }
-      aSA = aDF;
-      Model->AddWithRefs(aDF);
     }
   }
-  // Datum with datum targets
   else {
-    XCAFDimTolObjects_DatumTargetType aDatumType = anObject->GetDatumTargetType();
-    Handle(StepDimTol_DatumTarget) aDatumTarget;
-    // Note: the given way to write such datum type may be incorrect (too little information)
-    if (aDatumType == XCAFDimTolObjects_DatumTargetType_Area) {
-      TopoDS_Shape aDTShape = anObject->GetDatumTarget();
-      Handle(StepAP242_GeometricItemSpecificUsage) anAreaGISU;
-      Handle(StepRepr_ShapeAspect) anAreaSA = WriteShapeAspect(WS, theDatumL, aDTShape, aRC, anAreaGISU);
-      aSANum = Model->Number(anAreaSA);
-      aGISUNum = Model->Number(anAreaGISU);
-      Handle(StepDimTol_DatumTarget) aDT = new StepDimTol_DatumTarget();
-      aDT->Init(new TCollection_HAsciiString(), new TCollection_HAsciiString("area"), anAreaSA->OfShape(),
-        StepData_LTrue, aTargetId);
-      Model->ReplaceEntity(aSANum, aDT);
-      StepAP242_ItemIdentifiedRepresentationUsageDefinition aDefinition;
-      aDefinition.SetValue(aDT);
-      anAreaGISU->SetDefinition(aDefinition);
-      Model->ReplaceEntity(aGISUNum, anAreaGISU);
+    if (isWithDatRef) {
+      // Geometric_Tolerance & Geometric_Tolerance_With_Datum_Reference
+      Handle(StepDimTol_GeoTolAndGeoTolWthDatRef) aResult =
+          new StepDimTol_GeoTolAndGeoTolWthDatRef();
+        aResult->Init(aName, aDescription, aLMWU, aGTTarget, aGTWDR, aType);
+        aGeomTol = aResult;
     }
     else {
-      Handle(StepDimTol_PlacedDatumTargetFeature) aPDTF = new StepDimTol_PlacedDatumTargetFeature();
-      aPDTF->Init(new TCollection_HAsciiString(), STEPCAFControl_GDTProperty::GetDatumTargetName(aDatumType),
-        aPDS, StepData_LTrue, aTargetId);
-      Model->AddWithRefs(aPDTF);
-      aDatumTarget = aPDTF;
-      // Datum targets
-      Handle(StepRepr_PropertyDefinition) aPD = new StepRepr_PropertyDefinition();
-      StepRepr_CharacterizedDefinition aCDefinition;
-      aCDefinition.SetValue(aPDTF);
-      aPD->Init(new TCollection_HAsciiString(), Standard_False, NULL, aCDefinition);
-      if (anObject->HasDatumTargetParams()) {
-        // write all parameters of datum target
-        Handle(StepShape_ShapeRepresentationWithParameters) aSRWP = new StepShape_ShapeRepresentationWithParameters();
-        // Common for all datum targets
-        StepBasic_Unit aUnit = GetUnit(aRC);
-        gp_Ax2 aDTAxis = anObject->GetDatumTargetAxis();
-        GeomToStep_MakeAxis2Placement3d anAxisMaker(aDTAxis);
-        Handle(StepGeom_Axis2Placement3d) anA2P3D = anAxisMaker.Value();
-        anA2P3D->SetName(new TCollection_HAsciiString("orientation"));
-        Handle(StepRepr_HArray1OfRepresentationItem) anItems;
-        // Process each datum target type
-        if (aDatumType == XCAFDimTolObjects_DatumTargetType_Point) {
-          anItems = new StepRepr_HArray1OfRepresentationItem(1, 1);
-        }
-        else  {
-          Handle(TCollection_HAsciiString) aTargetValueName;
-          if (aDatumType == XCAFDimTolObjects_DatumTargetType_Line) {
-            anItems = new StepRepr_HArray1OfRepresentationItem(1, 2);
-            aTargetValueName = new TCollection_HAsciiString("target length");
-          }
-          else if (aDatumType == XCAFDimTolObjects_DatumTargetType_Rectangle) {
-            anItems = new StepRepr_HArray1OfRepresentationItem(1, 3);
-            aTargetValueName = new TCollection_HAsciiString("target length");
-            // Additional value
-            Handle(StepRepr_ReprItemAndMeasureWithUnit) aTargetValue = CreateDimValue(anObject->GetDatumTargetWidth(),
-              aUnit, new TCollection_HAsciiString("target width"), "POSITIVE_LENGTH_MEASURE", Standard_False);
-            anItems->SetValue(2, aTargetValue);
-            Model->AddWithRefs(aTargetValue);
-          }
-          else if (aDatumType == XCAFDimTolObjects_DatumTargetType_Circle) {
-            anItems = new StepRepr_HArray1OfRepresentationItem(1, 2);
-            aTargetValueName = new TCollection_HAsciiString("target diameter");
-          }
-          // Value
-          Handle(StepRepr_ReprItemAndMeasureWithUnit) aTargetValue = CreateDimValue(anObject->GetDatumTargetLength(),
-            aUnit, aTargetValueName, "POSITIVE_LENGTH_MEASURE", Standard_False);
-          anItems->SetValue(1, aTargetValue);
-          Model->AddWithRefs(aTargetValue);
-        }
-        anItems->SetValue(anItems->Length(), anA2P3D);
-        aSRWP->Init(new TCollection_HAsciiString(), anItems, aRC);
-        // Create and write auxiliary entities
-        Handle(StepShape_ShapeDefinitionRepresentation) aSDR = new StepShape_ShapeDefinitionRepresentation();
-        StepRepr_RepresentedDefinition aRDefinition;
-        aRDefinition.SetValue(aPD);
-        aSDR->Init(aRDefinition, aSRWP);
-        Model->AddWithRefs(aPD);
-        Model->AddWithRefs(aSRWP);
-        Model->AddWithRefs(aSDR);
-      }
-    }
-    // Link datum target to datum feature
-    // if aSASeq.Length() == 0 nothing to do
-    if (aSASeq.Length() == 1) {
-      Handle(StepRepr_FeatureForDatumTargetRelationship) aFFDTR = new StepRepr_FeatureForDatumTargetRelationship();
-      aFFDTR->Init(new TCollection_HAsciiString(), Standard_False, NULL, aSASeq.Value(1), aDatumTarget);
-      Model->AddWithRefs(aFFDTR);
-    }
-    else if (aSASeq.Length() > 1) {
-      Handle(StepRepr_CompositeShapeAspect) aCompSA = new StepRepr_CompositeShapeAspect();
-      aCompSA->Init(new TCollection_HAsciiString(), new TCollection_HAsciiString(), aPDS, aSASeq.Value(1)->ProductDefinitional());
-      for (Standard_Integer i = 1; i <= aSASeq.Length(); i++) {
-        Handle(StepRepr_ShapeAspectRelationship) aSAR = new StepRepr_ShapeAspectRelationship();
-        aSAR->Init(new TCollection_HAsciiString(), Standard_False, new TCollection_HAsciiString(), aCompSA, aSASeq.Value(i));
-        Model->AddWithRefs(aSAR);
+      // Geometric_Tolerance
+      Handle(StepDimTol_GeometricTolerance) aResult = 
+        STEPCAFControl_GDTProperty::GetGeomTolerance(anObject->GetType());
+      if (!aResult.IsNull()) {
+        aResult->Init(aName, aDescription, aLMWU, aGTTarget);
+        aGeomTol = aResult;
       }
-      Handle(StepRepr_FeatureForDatumTargetRelationship) aFFDTR = new StepRepr_FeatureForDatumTargetRelationship();
-      aFFDTR->Init(new TCollection_HAsciiString(), Standard_False, NULL, aCompSA, aDatumTarget);
-      Model->AddWithRefs(aFFDTR);
     }
-    aSA = aDatumTarget;
-  }
-
-  // Datum
-  Handle(StepDimTol_Datum) aDatum = theWrittenDatum;
-  if (isFirstDTarget) {
-    aDatum = new StepDimTol_Datum();
-    aDatum->Init(new TCollection_HAsciiString(), new TCollection_HAsciiString(), aPDS, StepData_LFalse, anIdentifier);
-    Model->AddWithRefs(aDatum);
-  }
-
-  // Shape_Aspect_Relationship
-  if (!aSA.IsNull()) {
-    Handle(StepRepr_ShapeAspectRelationship) aSAR = new StepRepr_ShapeAspectRelationship();
-    aSAR->Init(new TCollection_HAsciiString(), Standard_False, NULL, aSA, aDatum);
-    Model->AddWithRefs(aSAR);
   }
-
+  Model->AddWithRefs(aGeomTol);
+  WriteToleranceZone(WS, anObject, aGeomTol, theRC);
   //Annotation plane and Presentation
   WritePresentation(WS, anObject->GetPresentation(), anObject->GetPresentationName(), Standard_True, anObject->HasPlane(),
-    anObject->GetPlane(), anObject->GetPointTextAttach(), aSA);
-
-  return aDatum;
+    anObject->GetPlane(), anObject->GetPointTextAttach(), aGeomTol);
 }
 
 //=======================================================================
-//function : WriteDimValues
-//purpose  : auxiliary (write all data for given dimension: values, 
-//           qualifiers, modifiers, orientation and tolerance class)
-//======================================================================
-static void WriteDimValues(const Handle(XSControl_WorkSession) &WS,
-                           const Handle(XCAFDimTolObjects_DimensionObject) theObject,
-                           const Handle(StepRepr_RepresentationContext) theRC,
-                           const StepShape_DimensionalCharacteristic theDimension)
+//function : WriteDGTs
+//purpose  : 
+//=======================================================================
+Standard_Boolean STEPCAFControl_Writer::WriteDGTs (const Handle(XSControl_WorkSession) &WS,
+                                                   const TDF_LabelSequence  &labels ) const
 {
-  // Get working data
-  const Handle(Interface_InterfaceModel) &aModel = WS->Model();
-  XCAFDimTolObjects_DimensionModifiersSequence aModifiers = theObject->GetModifiers();
-  Handle(Standard_Transient) aDim = theDimension.Value();
-  Standard_Boolean isAngle = aDim->IsKind(STANDARD_TYPE(StepShape_AngularLocation)) ||
-                             aDim->IsKind(STANDARD_TYPE(StepShape_AngularSize));
-
-  // Unit
-  StepBasic_Unit aUnit = GetUnit(theRC, isAngle);
-  Standard_CString aMeasureName;
-  if (isAngle)
-    aMeasureName = "POSITIVE_PLANE_ANGLE_MEASURE";
-  else
-    aMeasureName = "POSITIVE_LENGTH_MEASURE";
-
-  // Values
-  Handle(StepRepr_HArray1OfRepresentationItem) aValues;
-  Standard_Integer aNbItems = 1, aValIt = 1;
-  if (theObject->IsDimWithRange())
-    aNbItems += 2;
-  if (aModifiers.Length() > 0)
-    aNbItems++;
-  if (theObject->GetType() == XCAFDimTolObjects_DimensionType_Location_Oriented)
-    aNbItems++;
-  aNbItems += theObject->NbDescriptions();
-  aValues = new StepRepr_HArray1OfRepresentationItem(1, aNbItems);
-
-  // Nominal value
-  Standard_Real aNominal = theObject->GetValue();
-  Standard_Integer aLeftNbDigits, aRightNbDigits;
-  theObject->GetNbOfDecimalPlaces(aLeftNbDigits, aRightNbDigits);
-  Standard_Integer aNbQualifiers = 0;
-  if (theObject->HasQualifier() && !isAngle)
-    aNbQualifiers++;
-  if (aLeftNbDigits > 0 || aRightNbDigits > 0)
-    aNbQualifiers++;
-  // With qualifiers
-  if (aNbQualifiers > 0) {
-    Handle(StepShape_QualifiedRepresentationItem) aQRI = new StepShape_QualifiedRepresentationItem();
-    Handle(StepShape_HArray1OfValueQualifier) aQualifiers = new StepShape_HArray1OfValueQualifier(1, aNbQualifiers);
-    // Type qualifier
-    if (theObject->HasQualifier() && !isAngle) {
-      StepShape_ValueQualifier anItem;
-      Handle(StepShape_TypeQualifier) aType = new StepShape_TypeQualifier();
-      XCAFDimTolObjects_DimensionQualifier aQualifier = theObject->GetQualifier();
-      aType->Init(STEPCAFControl_GDTProperty::GetDimQualifierName(aQualifier));
-      aModel->AddWithRefs(aType);
-      anItem.SetValue(aType);
-      aQualifiers->SetValue(1, anItem);
-    }
-    // Number of decimal places
-    if (aLeftNbDigits > 0 || aRightNbDigits > 0) {
-      StepShape_ValueQualifier anItem;
-      Handle(StepShape_ValueFormatTypeQualifier) aType = new StepShape_ValueFormatTypeQualifier();
-      Handle(TCollection_HAsciiString) aFormatType = new TCollection_HAsciiString("NR2 ");
-      aFormatType->AssignCat(new TCollection_HAsciiString(aLeftNbDigits));
-      aFormatType->AssignCat(new TCollection_HAsciiString("."));
-      aFormatType->AssignCat(new TCollection_HAsciiString(aRightNbDigits));
-      aType->Init(aFormatType);
-      aModel->AddWithRefs(aType);
-      anItem.SetValue(aType);
-      aQualifiers->SetValue(aNbQualifiers, anItem);
-    }
-    // Set qualifiers
-    aQRI->SetQualifiers(aQualifiers);
-    Handle(StepRepr_ReprItemAndMeasureWithUnit) anItem = CreateDimValue(aNominal, aUnit,
-      new TCollection_HAsciiString("nominal value"), aMeasureName, isAngle, Standard_True, aQRI);
-    aValues->SetValue(aValIt, anItem);
-    aValIt++;
-  }
-  // Without qualifiers
-  else {
-    Handle(StepRepr_ReprItemAndMeasureWithUnit) anItem = CreateDimValue(aNominal, aUnit,
-      new TCollection_HAsciiString("nominal value"), aMeasureName, isAngle);
-    aValues->SetValue(aValIt, anItem);
-    aValIt++;
-  }
+  
+  if ( labels.Length() <=0 ) return Standard_False;
+  
+  // get working data
+  const Handle(Interface_InterfaceModel) &Model = WS->Model();
+  const Handle(XSControl_TransferWriter) &TW = WS->TransferWriter();
+  const Handle(Transfer_FinderProcess) &FP = TW->FinderProcess();
 
-  // Ranges
-  if (theObject->IsDimWithRange()) {
-    Handle(StepRepr_ReprItemAndMeasureWithUnit) aLowerItem = CreateDimValue(theObject->GetLowerBound(), aUnit,
-      new TCollection_HAsciiString("lower limit"), aMeasureName, isAngle);
-    Handle(StepRepr_ReprItemAndMeasureWithUnit) anUpperItem = CreateDimValue(theObject->GetUpperBound(), aUnit,
-      new TCollection_HAsciiString("upper limit"), aMeasureName, isAngle);
-    aValues->SetValue(aValIt, aLowerItem);
-    aValIt++;
-    aValues->SetValue(aValIt, anUpperItem);
-    aValIt++;
-  }
+  const Handle(Interface_HGraph) aHGraph = WS->HGraph();
+  if(aHGraph.IsNull())
+    return Standard_False;
 
-  // Modifiers
-  if (aModifiers.Length() > 0) {
-    Handle(StepRepr_CompoundRepresentationItem) aCompoundRI = new StepRepr_CompoundRepresentationItem();
-    Handle (StepRepr_HArray1OfRepresentationItem) aModifItems = 
-      new StepRepr_HArray1OfRepresentationItem(1, aModifiers.Length());
-    for (Standard_Integer i = 1; i <= aModifiers.Length(); i++) {
-      XCAFDimTolObjects_DimensionModif aModif = aModifiers.Value(i);
-      Handle(StepRepr_DescriptiveRepresentationItem) aModifItem = 
-        new StepRepr_DescriptiveRepresentationItem();
-      aModifItem->Init(new TCollection_HAsciiString(), STEPCAFControl_GDTProperty::GetDimModifierName(aModif));
-      aModel->AddWithRefs(aModifItem);
-      aModifItems->SetValue(i, aModifItem);
+  Interface_Graph aGraph = aHGraph->Graph();
+  Handle(XCAFDoc_DimTolTool) DGTTool = XCAFDoc_DocumentTool::DimTolTool( labels(1) );
+  if(DGTTool.IsNull() ) return Standard_False;
+
+  TDF_LabelSequence DGTLabels;
+
+  STEPConstruct_DataMapOfAsciiStringTransient DatumMap;
+
+  // write Datums
+  DGTLabels.Clear();
+  DGTTool->GetDatumLabels(DGTLabels);
+  if(DGTLabels.Length()<=0) return Standard_False;
+  Standard_Integer i;
+  for(i=1; i<=DGTLabels.Length(); i++) {
+    TDF_Label DatumL = DGTLabels.Value(i);
+    TDF_LabelSequence ShapeL;
+    TDF_LabelSequence aNullSeq;
+    if(!DGTTool->GetRefShapeLabel(DatumL,ShapeL,aNullSeq)) continue;
+    // find target shape
+    TopoDS_Shape aShape = XCAFDoc_ShapeTool::GetShape(ShapeL.Value(1));
+    TopLoc_Location Loc;
+    TColStd_SequenceOfTransient seqRI;
+    FindEntities( FP, aShape, Loc, seqRI );
+    if ( seqRI.Length() <= 0 ) {
+      FP->Messenger()->SendInfo() << "Warning: Cannot find RI for "<<aShape.TShape()->DynamicType()->Name()<<std::endl;
+      continue;
     }
-    aCompoundRI->Init(new TCollection_HAsciiString(), aModifItems);
-    aValues->SetValue(aValIt, aCompoundRI);
-    aValIt++;
+    Handle(StepRepr_ProductDefinitionShape) PDS;
+    Handle(StepRepr_RepresentationContext) RC;
+    Handle(Standard_Transient) ent = seqRI.Value(1);
+    Handle(StepShape_AdvancedFace) AF;
+    Handle(StepShape_EdgeCurve) EC;
+    FindPDSforDGT(aGraph,ent,PDS,RC,AF,EC);
+    if(PDS.IsNull()) continue;
+    //std::cout<<"Model->Number(PDS)="<<Model->Number(PDS)<<std::endl;
+    Handle(XCAFDoc_Datum) DatumAttr;
+    if(!DatumL.FindAttribute(XCAFDoc_Datum::GetID(),DatumAttr)) continue;
+    Handle(TCollection_HAsciiString) aName = DatumAttr->GetName();
+    Handle(TCollection_HAsciiString) aDescription = DatumAttr->GetDescription();
+    Handle(TCollection_HAsciiString) anIdentification = DatumAttr->GetIdentification();
+    Handle(StepDimTol_DatumFeature) DF = new StepDimTol_DatumFeature;
+    Handle(StepDimTol_Datum) aDatum = new StepDimTol_Datum;
+    DF->Init(aName, new TCollection_HAsciiString, PDS, StepData_LTrue);
+    Model->AddWithRefs(DF);
+    aDatum->Init(aName, new TCollection_HAsciiString, PDS, StepData_LFalse, anIdentification);
+    Model->AddWithRefs(aDatum);
+    Handle(StepRepr_ShapeAspectRelationship) SAR = new StepRepr_ShapeAspectRelationship;
+    SAR->SetName(aName);
+    SAR->SetRelatingShapeAspect(DF);
+    SAR->SetRelatedShapeAspect(aDatum);
+    Model->AddWithRefs(SAR);
+    // write chain for DatumFeature
+    StepRepr_CharacterizedDefinition CD;
+    CD.SetValue(DF);
+    Handle(StepRepr_PropertyDefinition) PropD = new StepRepr_PropertyDefinition;
+    PropD->Init(aName,Standard_True,aDescription,CD);
+    Model->AddWithRefs(PropD);
+    StepRepr_RepresentedDefinition RD;
+    RD.SetValue(PropD);
+    Handle(StepShape_ShapeRepresentation) SR = new StepShape_ShapeRepresentation;
+    Handle(StepRepr_HArray1OfRepresentationItem) HARI =
+      new StepRepr_HArray1OfRepresentationItem(1,1);
+    HARI->SetValue(1,AF);
+    SR->Init(aName,HARI,RC);
+    Handle(StepShape_ShapeDefinitionRepresentation) SDR = new StepShape_ShapeDefinitionRepresentation;
+    SDR->Init(RD,SR);
+    Model->AddWithRefs(SDR);
+    // write chain for Datum 
+    StepRepr_CharacterizedDefinition CD1;
+    CD1.SetValue(aDatum);
+    Handle(StepRepr_PropertyDefinition) PropD1 = new StepRepr_PropertyDefinition;
+    PropD1->Init(aName,Standard_True,aDescription,CD1);
+    Model->AddWithRefs(PropD1);
+    StepRepr_RepresentedDefinition RD1;
+    RD1.SetValue(PropD1);
+    Handle(StepShape_ShapeRepresentation) SR1 = new StepShape_ShapeRepresentation;
+    Handle(StepRepr_HArray1OfRepresentationItem) HARI1 =
+      new StepRepr_HArray1OfRepresentationItem(1,1);
+    HARI1->SetValue(1,AF->FaceGeometry());
+    SR1->Init(aName,HARI1,RC);
+    Model->AddWithRefs(SR1);
+    Handle(StepShape_ShapeDefinitionRepresentation) SDR1 = new StepShape_ShapeDefinitionRepresentation;
+    SDR1->Init(RD1,SR1);
+    Model->AddWithRefs(SDR1);
+    // add created Datum into Map
+    TCollection_AsciiString stmp(aName->ToCString());
+    stmp.AssignCat(aDescription->ToCString());
+    stmp.AssignCat(anIdentification->ToCString());
+    DatumMap.Bind(stmp,aDatum);
   }
 
-  // Orientation
-  if (theObject->GetType() == XCAFDimTolObjects_DimensionType_Location_Oriented) {
-    Handle(StepGeom_Axis2Placement3d) anOrientation = new StepGeom_Axis2Placement3d();
-    gp_Dir aDir;
-    theObject->GetDirection(aDir);
-    GeomToStep_MakeCartesianPoint MkPoint(gp_Pnt(0, 0, 0));
-    Handle(StepGeom_CartesianPoint) aLoc = MkPoint.Value();
-    Handle(StepGeom_Direction) anAxis = new StepGeom_Direction();
-    Handle(TColStd_HArray1OfReal) aCoords = new TColStd_HArray1OfReal(1, 3);
-    aCoords->SetValue(1, aDir.X());
-    aCoords->SetValue(2, aDir.Y());
-    aCoords->SetValue(3, aDir.Z());
-    anAxis->Init(new TCollection_HAsciiString(), aCoords);
-    anOrientation->Init(new TCollection_HAsciiString("orientation"), aLoc, Standard_True, anAxis, Standard_False, NULL);
-    aValues->SetValue(aValIt, anOrientation);
-    aValIt++;
-  }
+  // write Tolerances and Dimensions
+  DGTLabels.Clear();
+  DGTTool->GetDimTolLabels(DGTLabels);
+  if(DGTLabels.Length()<=0) return Standard_False;
+  for(i=1; i<=DGTLabels.Length(); i++) {
+    TDF_Label DimTolL = DGTLabels.Value(i);
+    TDF_LabelSequence ShapeL;
+    TDF_LabelSequence aNullSeq;
+    if(!DGTTool->GetRefShapeLabel(DimTolL,ShapeL,aNullSeq)) continue;
+    // find target shape
+    TopoDS_Shape aShape = XCAFDoc_ShapeTool::GetShape(ShapeL.Value(1));
+    TopLoc_Location Loc;
+    TColStd_SequenceOfTransient seqRI;
+    FindEntities( FP, aShape, Loc, seqRI );
+    if ( seqRI.Length() <= 0 ) {
+      FP->Messenger()->SendInfo() << "Warning: Cannot find RI for "<<aShape.TShape()->DynamicType()->Name()<<std::endl;
+      continue;
+    }
+    Handle(StepRepr_ProductDefinitionShape) PDS;
+    Handle(StepRepr_RepresentationContext) RC;
+    Handle(Standard_Transient) ent = seqRI.Value(1);
+    Handle(StepShape_AdvancedFace) AF;
+    Handle(StepShape_EdgeCurve) EC;
+    FindPDSforDGT(aGraph,ent,PDS,RC,AF,EC);
+    if(PDS.IsNull()) continue;
+    //std::cout<<"Model->Number(PDS)="<<Model->Number(PDS)<<std::endl;
 
-  // Descriptions
-  if (theObject->HasDescriptions()) {
-    for (Standard_Integer i = 0; i < theObject->NbDescriptions(); i++) {
-      Handle(StepRepr_DescriptiveRepresentationItem) aDRI = new StepRepr_DescriptiveRepresentationItem();
-      aDRI->Init(theObject->GetDescriptionName(i), theObject->GetDescription(i));
-      aValues->SetValue(aValIt, aDRI);
-      aValIt++;
+    Handle(XCAFDoc_DimTol) DimTolAttr;
+    if(!DimTolL.FindAttribute(XCAFDoc_DimTol::GetID(),DimTolAttr)) continue;
+    Standard_Integer kind = DimTolAttr->GetKind();
+    Handle(TColStd_HArray1OfReal) aVal = DimTolAttr->GetVal();
+    Handle(TCollection_HAsciiString) aName = DimTolAttr->GetName();
+    Handle(TCollection_HAsciiString) aDescription = DimTolAttr->GetDescription();
+
+    // common part of writing D&GT entities
+    StepRepr_CharacterizedDefinition CD;
+    Handle(StepRepr_ShapeAspect) SA = new StepRepr_ShapeAspect;
+    SA->Init(aName, new TCollection_HAsciiString, PDS, StepData_LTrue);
+    Model->AddWithRefs(SA);
+    CD.SetValue(SA);
+    Handle(StepRepr_PropertyDefinition) PropD = new StepRepr_PropertyDefinition;
+    PropD->Init(aName,Standard_True,aDescription,CD);
+    Model->AddWithRefs(PropD);
+    StepRepr_RepresentedDefinition RD;
+    RD.SetValue(PropD);
+    Handle(StepShape_ShapeRepresentation) SR = new StepShape_ShapeRepresentation;
+    Handle(StepRepr_HArray1OfRepresentationItem) HARI =
+      new StepRepr_HArray1OfRepresentationItem(1,1);
+    if(kind<20) 
+      HARI->SetValue(1,EC);
+    else
+      HARI->SetValue(1,AF);
+    SR->Init(aName,HARI,RC);
+    Handle(StepShape_ShapeDefinitionRepresentation) SDR = new StepShape_ShapeDefinitionRepresentation;
+    SDR->Init(RD,SR);
+    Model->AddWithRefs(SDR);
+    // define aUnit for creation LengthMeasureWithUnit (common for all)
+    StepBasic_Unit aUnit;
+    aUnit = GetUnit(RC);
+
+    // specific part of writing D&GT entities
+    if(kind<20) { //dimension
+      Handle(StepShape_DimensionalSize) DimSize = new StepShape_DimensionalSize;
+      DimSize->Init(SA,aDescription);
+      Model->AddWithRefs(DimSize);
+      if(aVal->Length()>1) {
+        // create MeasureWithUnits
+        Handle(StepBasic_MeasureValueMember) MVM1 = new StepBasic_MeasureValueMember;
+        MVM1->SetName("POSITIVE_LENGTH_MEASURE");
+        MVM1->SetReal(aVal->Value(1));
+        Handle(StepBasic_MeasureWithUnit) MWU1 = new StepBasic_MeasureWithUnit;
+        MWU1->Init(MVM1,aUnit);
+        Handle(StepBasic_MeasureValueMember) MVM2 = new StepBasic_MeasureValueMember;
+        MVM2->SetName("POSITIVE_LENGTH_MEASURE");
+        MVM2->SetReal(aVal->Value(2));
+        Handle(StepBasic_MeasureWithUnit) MWU2 = new StepBasic_MeasureWithUnit;
+        MWU2->Init(MVM2,aUnit);
+        Handle(StepRepr_RepresentationItem) RI1 = new StepRepr_RepresentationItem;
+        RI1->Init(new TCollection_HAsciiString("lower limit"));
+        Handle(StepRepr_RepresentationItem) RI2 = new StepRepr_RepresentationItem;
+        RI2->Init(new TCollection_HAsciiString("upper limit"));
+        Handle(StepRepr_ReprItemAndLengthMeasureWithUnit) RILMU1 =
+          new StepRepr_ReprItemAndLengthMeasureWithUnit;
+        RILMU1->Init(MWU1,RI1);
+        Handle(StepRepr_ReprItemAndLengthMeasureWithUnit) RILMU2 =
+          new StepRepr_ReprItemAndLengthMeasureWithUnit;
+        RILMU2->Init(MWU2,RI2);
+        Model->AddWithRefs(RILMU1);
+        Model->AddWithRefs(RILMU2);
+        //Handle(StepRepr_CompoundItemDefinitionMember) CIDM =
+        //  new StepRepr_CompoundItemDefinitionMember;
+        //Handle(TColStd_HArray1OfTransient) ArrTr = new TColStd_HArray1OfTransient(1,2);
+        //ArrTr->SetValue(1,RILMU1);
+        //ArrTr->SetValue(2,RILMU2);
+        //CIDM->SetArrTransient(ArrTr);
+        //CIDM->SetName("SET_REPRESENTATION_ITEM");
+        //StepRepr_CompoundItemDefinition CID;
+        //CID.SetValue(CIDM);
+        Handle(StepRepr_HArray1OfRepresentationItem) HARIVR =
+          new StepRepr_HArray1OfRepresentationItem(1,2);
+        HARIVR->SetValue(1,RILMU1);
+        HARIVR->SetValue(2,RILMU2);
+        Handle(StepRepr_ValueRange) VR = new StepRepr_ValueRange;
+        //VR->Init(aName,CID);
+        VR->Init(aName,HARIVR);
+        Model->AddEntity(VR);
+        Handle(StepShape_ShapeDimensionRepresentation) SDimR =
+          new StepShape_ShapeDimensionRepresentation;
+        Handle(StepRepr_HArray1OfRepresentationItem) aHARI =
+          new StepRepr_HArray1OfRepresentationItem(1,1);
+        aHARI->SetValue(1,VR);
+        SDimR->Init(aName,aHARI,RC);
+        Model->AddWithRefs(SDimR);
+        Handle(StepShape_DimensionalCharacteristicRepresentation) DimCharR =
+          new StepShape_DimensionalCharacteristicRepresentation;
+        StepShape_DimensionalCharacteristic DimChar;
+        DimChar.SetValue(DimSize);
+        DimCharR->Init(DimChar,SDimR);
+        Model->AddEntity(DimCharR);
+      }
+    }
+    else if(kind<50) { //tolerance
+      if(kind<35) { // tolerance with datum system
+        TDF_LabelSequence DatumLabels;
+        DGTTool->GetDatumOfTolerLabels(DimTolL,DatumLabels);
+        Standard_Integer NbDR = DatumLabels.Length();
+        Handle(StepDimTol_HArray1OfDatumReference) HADR = new StepDimTol_HArray1OfDatumReference(1,NbDR);
+        for(Standard_Integer j=1; j<=NbDR; j++) {
+          Handle(XCAFDoc_Datum) DatumAttr;
+          TDF_Label DatumL = DatumLabels.Value(j);
+          if(!DatumL.FindAttribute(XCAFDoc_Datum::GetID(),DatumAttr)) continue;
+          Handle(TCollection_HAsciiString) aNameD = DatumAttr->GetName();
+          Handle(TCollection_HAsciiString) aDescriptionD = DatumAttr->GetDescription();
+          Handle(TCollection_HAsciiString) anIdentificationD = DatumAttr->GetIdentification();
+          TCollection_AsciiString stmp(aNameD->ToCString());
+          stmp.AssignCat(aDescriptionD->ToCString());
+          stmp.AssignCat(anIdentificationD->ToCString());
+          if(DatumMap.IsBound(stmp)) {
+            Handle(StepDimTol_Datum) aDatum = 
+              Handle(StepDimTol_Datum)::DownCast(DatumMap.Find(stmp));
+            Handle(StepDimTol_DatumReference) DR = new StepDimTol_DatumReference;
+            DR->Init(j,aDatum);
+            Model->AddWithRefs(DR);
+            HADR->SetValue(j,DR);
+          }
+        }
+        // create LengthMeasureWithUnit
+        Handle(StepBasic_MeasureValueMember) MVM = new StepBasic_MeasureValueMember;
+        MVM->SetName("LENGTH_MEASURE");
+        MVM->SetReal(aVal->Value(1));
+        Handle(StepBasic_LengthMeasureWithUnit) LMWU = new StepBasic_LengthMeasureWithUnit;
+        LMWU->Init(MVM,aUnit);
+        // create tolerance by it's type
+        if(kind<24) {
+          Handle(StepDimTol_GeometricToleranceWithDatumReference) GTWDR =
+            new StepDimTol_GeometricToleranceWithDatumReference;
+          GTWDR->SetDatumSystem(HADR);
+          Handle(StepDimTol_ModifiedGeometricTolerance) MGT =
+            new StepDimTol_ModifiedGeometricTolerance;
+          if(kind==21) MGT->SetModifier(StepDimTol_MaximumMaterialCondition);
+          else if(kind==22) MGT->SetModifier(StepDimTol_LeastMaterialCondition);
+          else if(kind==23) MGT->SetModifier(StepDimTol_RegardlessOfFeatureSize);
+          Handle(StepDimTol_GeoTolAndGeoTolWthDatRefAndModGeoTolAndPosTol) GTComplex =
+            new StepDimTol_GeoTolAndGeoTolWthDatRefAndModGeoTolAndPosTol;
+          GTComplex->Init(aName,aDescription,LMWU,SA,GTWDR,MGT);
+          Model->AddWithRefs(GTComplex);
+        }
+        else if(kind==24) {
+          Handle(StepDimTol_AngularityTolerance) aToler =
+            new StepDimTol_AngularityTolerance;
+          aToler->Init(aName,aDescription,LMWU,SA,HADR);
+          Model->AddWithRefs(aToler);
+        }
+        else if(kind==25) {
+          Handle(StepDimTol_CircularRunoutTolerance) aToler =
+            new StepDimTol_CircularRunoutTolerance;
+          aToler->Init(aName,aDescription,LMWU,SA,HADR);
+          Model->AddWithRefs(aToler);
+        }
+        else if(kind==26) {
+          Handle(StepDimTol_CoaxialityTolerance) aToler =
+            new StepDimTol_CoaxialityTolerance;
+          aToler->Init(aName,aDescription,LMWU,SA,HADR);
+          Model->AddWithRefs(aToler);
+        }
+        else if(kind==27) {
+          Handle(StepDimTol_ConcentricityTolerance) aToler =
+            new StepDimTol_ConcentricityTolerance;
+          aToler->Init(aName,aDescription,LMWU,SA,HADR);
+          Model->AddWithRefs(aToler);
+        }
+        else if(kind==28) {
+          Handle(StepDimTol_ParallelismTolerance) aToler =
+            new StepDimTol_ParallelismTolerance;
+          aToler->Init(aName,aDescription,LMWU,SA,HADR);
+          Model->AddWithRefs(aToler);
+        }
+        else if(kind==29) {
+          Handle(StepDimTol_PerpendicularityTolerance) aToler =
+            new StepDimTol_PerpendicularityTolerance;
+          aToler->Init(aName,aDescription,LMWU,SA,HADR);
+          Model->AddWithRefs(aToler);
+        }
+        else if(kind==30) {
+          Handle(StepDimTol_SymmetryTolerance) aToler =
+            new StepDimTol_SymmetryTolerance;
+          aToler->Init(aName,aDescription,LMWU,SA,HADR);
+          Model->AddWithRefs(aToler);
+        }
+        else if(kind==31) {
+          Handle(StepDimTol_TotalRunoutTolerance) aToler =
+            new StepDimTol_TotalRunoutTolerance;
+          aToler->Init(aName,aDescription,LMWU,SA,HADR);
+          Model->AddWithRefs(aToler);
+        }
+      }
     }
   }
 
-  for (Standard_Integer i = 1; i <= aValues->Length(); i++)
-    aModel->AddWithRefs(aValues->Value(i));
-
-  // Create resulting Shape_Dimension_Representation
-  Handle(StepShape_ShapeDimensionRepresentation) aSDR = new StepShape_ShapeDimensionRepresentation();
-  aSDR->Init(new TCollection_HAsciiString(), aValues, theRC);
-  aModel->AddWithRefs(aSDR);
-  Handle(StepShape_DimensionalCharacteristicRepresentation) aDCR = new StepShape_DimensionalCharacteristicRepresentation();
-  aDCR->Init(theDimension, aSDR);
-  aModel->AddWithRefs(aDCR);
-
-  // Plus_Minus_Tolerance
-  if (theObject->IsDimWithPlusMinusTolerance()) {
-    Handle(TCollection_HAsciiString) aDummyName = new TCollection_HAsciiString(aMeasureName);
-    aDummyName = aDummyName->SubString(9, aDummyName->Length()); //delete "POSITIVE_"
-    aMeasureName = aDummyName->ToCString();
-    Standard_Real aLowerTolValue = -theObject->GetLowerTolValue(),
-                  anUpperTolValue = theObject->GetUpperTolValue();
-    // Upper
-    Handle(StepBasic_MeasureWithUnit) anUpperMWU = new StepBasic_MeasureWithUnit();
-    Handle(StepBasic_MeasureValueMember) anUpperValue = new StepBasic_MeasureValueMember();
-    anUpperValue->SetName(aMeasureName);
-    anUpperValue->SetReal(anUpperTolValue);
-    anUpperMWU->Init(anUpperValue, aUnit);
-    aModel->AddWithRefs(anUpperMWU);
-    // Lower
-    Handle(StepBasic_MeasureWithUnit) aLowerMWU = new StepBasic_MeasureWithUnit();
-    Handle(StepBasic_MeasureValueMember) aLowerValue = new StepBasic_MeasureValueMember();
-    aLowerValue->SetName(aMeasureName);
-    aLowerValue->SetReal(aLowerTolValue);
-    aLowerMWU->Init(aLowerValue, aUnit);
-    aModel->AddWithRefs(aLowerMWU);
-    // Tolerance
-    Handle(StepShape_ToleranceValue) aTolValue = new StepShape_ToleranceValue();
-    aTolValue->Init(aLowerMWU, anUpperMWU);
-    aModel->AddWithRefs(aTolValue);
-    StepShape_ToleranceMethodDefinition aMethod;
-    aMethod.SetValue(aTolValue);
-    Handle(StepShape_PlusMinusTolerance) aPlusMinusTol = new StepShape_PlusMinusTolerance();
-    aPlusMinusTol->Init(aMethod, theDimension);
-    aModel->AddWithRefs(aPlusMinusTol);
-  }
-  // Tolerance class
-  if (theObject->IsDimWithClassOfTolerance()) {
-    Standard_Boolean isHole;
-    XCAFDimTolObjects_DimensionFormVariance aFormVariance;
-    XCAFDimTolObjects_DimensionGrade aGrade;
-    if (!theObject->GetClassOfTolerance(isHole, aFormVariance, aGrade))
-      return;
-    Handle(StepShape_LimitsAndFits) aLAF = STEPCAFControl_GDTProperty::GetLimitsAndFits(isHole, aFormVariance, aGrade);
-    aModel->AddWithRefs(aLAF);
-    StepShape_ToleranceMethodDefinition aMethod;
-    aMethod.SetValue(aLAF);
-    Handle(StepShape_PlusMinusTolerance) aPlusMinusTol = new StepShape_PlusMinusTolerance();
-    aPlusMinusTol->Init(aMethod, theDimension);
-    aModel->AddWithRefs(aPlusMinusTol);
-  }
+  return Standard_True;
 }
 
 //=======================================================================
-//function : WriteDerivedGeometry
-//purpose  : auxiliary (write connection point for dimensions)
-//======================================================================
-static void WriteDerivedGeometry (const Handle(XSControl_WorkSession) &WS,
-                                  const Handle(XCAFDimTolObjects_DimensionObject)& theObject,
-                                  const Handle(StepRepr_ConstructiveGeometryRepresentation) theRepr,
-                                  Handle(StepRepr_ShapeAspect)& theFirstSA,
-                                  Handle(StepRepr_ShapeAspect)& theSecondSA,
-                                  NCollection_Vector<Handle(StepGeom_CartesianPoint)>& thePnts)
-{
-  const Handle(Interface_InterfaceModel) &aModel = WS->Model();
-  // First point
-  if (theObject->HasPoint()) {
-    GeomToStep_MakeCartesianPoint aPointMaker(theObject->GetPoint());
-    Handle(StepGeom_CartesianPoint) aPoint = aPointMaker.Value();
-    thePnts.Append(aPoint);
-    Handle(StepRepr_DerivedShapeAspect) aDSA = new StepRepr_DerivedShapeAspect();
-    aDSA->Init(new TCollection_HAsciiString(), new TCollection_HAsciiString(), theFirstSA->OfShape(), StepData_LFalse);
-    Handle(StepAP242_GeometricItemSpecificUsage) aGISU = new StepAP242_GeometricItemSpecificUsage();
-    StepAP242_ItemIdentifiedRepresentationUsageDefinition aDefinition;
-    aDefinition.SetValue(aDSA);
-    Handle(StepRepr_HArray1OfRepresentationItem) anItem = new StepRepr_HArray1OfRepresentationItem(1, 1);
-    anItem->SetValue(1, aPoint);
-    aGISU->Init(new TCollection_HAsciiString(), new TCollection_HAsciiString(), aDefinition, theRepr, anItem);
-    Handle(StepRepr_ShapeAspectDerivingRelationship) aSADR = new StepRepr_ShapeAspectDerivingRelationship();
-    aSADR->Init(new TCollection_HAsciiString(), Standard_False, new TCollection_HAsciiString(), aDSA, theFirstSA);
-    theFirstSA = aDSA;
-    aModel->AddWithRefs(aGISU);
-    aModel->AddWithRefs(aSADR);
-  }
-  
-  // Second point (for locations)
-  if (theObject->HasPoint2()) {
-    GeomToStep_MakeCartesianPoint aPointMaker(theObject->GetPoint2());
-    Handle(StepGeom_CartesianPoint) aPoint = aPointMaker.Value();
-    thePnts.Append(aPoint);
-    Handle(StepRepr_DerivedShapeAspect) aDSA = new StepRepr_DerivedShapeAspect();
-    aDSA->Init(new TCollection_HAsciiString(), new TCollection_HAsciiString(), theFirstSA->OfShape(), StepData_LFalse);
-    Handle(StepAP242_GeometricItemSpecificUsage) aGISU = new StepAP242_GeometricItemSpecificUsage();
-    StepAP242_ItemIdentifiedRepresentationUsageDefinition aDefinition;
-    aDefinition.SetValue(aDSA);
-    Handle(StepRepr_HArray1OfRepresentationItem) anItem = new StepRepr_HArray1OfRepresentationItem(1, 1);
-    anItem->SetValue(1, aPoint);
-    aGISU->Init(new TCollection_HAsciiString(), new TCollection_HAsciiString(), aDefinition, theRepr, anItem);
-    Handle(StepRepr_ShapeAspectDerivingRelationship) aSADR = new StepRepr_ShapeAspectDerivingRelationship();
-    aSADR->Init(new TCollection_HAsciiString(), Standard_False, new TCollection_HAsciiString(), aDSA, theSecondSA);
-    theSecondSA = aDSA;
-    aModel->AddWithRefs(aGISU);
-    aModel->AddWithRefs(aSADR);
-  }
-}
-
+//function : WriteDGTsAP242
+//purpose  : 
 //=======================================================================
-//function : WriteDatumSystem
-//purpose  : auxiliary (write Write datum system for given
-//           geometric_tolerance)
-//======================================================================
-static Handle(StepDimTol_HArray1OfDatumSystemOrReference) WriteDatumSystem(const Handle(XSControl_WorkSession) &WS,
-                                                                           const TDF_Label theGeomTolL,
-                                                                           const TDF_LabelSequence theDatumSeq,
-                                                                           const STEPConstruct_DataMapOfAsciiStringTransient theDatumMap,
-                                                                           const Handle(StepRepr_RepresentationContext)& theRC)
+
+Standard_Boolean STEPCAFControl_Writer::WriteDGTsAP242 (const Handle(XSControl_WorkSession) &WS,
+                                                        const TDF_LabelSequence  &labels )
 {
   // Get working data
-  const Handle(Interface_InterfaceModel) &Model = WS->Model();
+  const Handle(Interface_InterfaceModel) &aModel = WS->Model();
+
   const Handle(Interface_HGraph) aHGraph = WS->HGraph();
-  if (aHGraph.IsNull())
-    return NULL;
+  if(aHGraph.IsNull())
+    return Standard_False;
+
   Interface_Graph aGraph = aHGraph->Graph();
-  Handle(XCAFDoc_GeomTolerance) aGTAttr;
-  if (!theGeomTolL.FindAttribute(XCAFDoc_GeomTolerance::GetID(), aGTAttr)) 
-    return NULL;
-  Handle(XCAFDimTolObjects_GeomToleranceObject) anObject = aGTAttr->GetObject();
-  if (anObject.IsNull())
-    return NULL;
+  Handle(XCAFDoc_DimTolTool) DGTTool = XCAFDoc_DocumentTool::DimTolTool(labels(1));
+  if(DGTTool.IsNull())
+    return Standard_False;
 
-  // Unit
-  StepBasic_Unit aUnit = GetUnit(theRC);
+  // Common entities for presentation
+  STEPConstruct_Styles aStyles (WS);
+  Handle(StepVisual_Colour) aCurvColor = aStyles.EncodeColor(Quantity_NOC_WHITE);
+  Handle(StepRepr_RepresentationItem) anItem = NULL;
+  myGDTPrsCurveStyle->SetValue(1, aStyles.MakeColorPSA(anItem, aCurvColor, aCurvColor, aCurvColor, 0.0));
+  Interface_EntityIterator aModelIter = aModel->Entities();
+  for (; aModelIter.More() && myGDTCommonPDS.IsNull(); aModelIter.Next())
+    myGDTCommonPDS = Handle(StepRepr_ProductDefinitionShape)::DownCast(aModelIter.Value());
+
+  TDF_LabelSequence aDGTLabels;
+  STEPConstruct_DataMapOfAsciiStringTransient aDatumMap;
+  Handle(StepRepr_RepresentationContext) aRC;
 
-  XCAFDimTolObjects_DatumObjectSequence aDatums;
-  Standard_Integer aMaxDatumNum = 0;
-  for (Standard_Integer i = 1; i <= theDatumSeq.Length(); i++) {
+  //------------- //
+  // write Datums //
+  //--------------//
+  DGTTool->GetDatumLabels(aDGTLabels);
+  // Find all shapes with datums
+  TColStd_MapOfAsciiString aNameIdMap;
+  for(Standard_Integer i = 1; i <= aDGTLabels.Length(); i++) {
+    TDF_Label aDatumL = aDGTLabels.Value(i);
+    TDF_LabelSequence aShapeL, aNullSeq;
+    DGTTool->GetRefShapeLabel(aDatumL, aShapeL, aNullSeq);
     Handle(XCAFDoc_Datum) aDatumAttr;
-    if (!theDatumSeq.Value(i).FindAttribute(XCAFDoc_Datum::GetID(), aDatumAttr)) 
-      continue;
-    Handle(XCAFDimTolObjects_DatumObject) aDatumObj = aDatumAttr->GetObject();
-    if (aDatumObj.IsNull())
+    aDatumL.FindAttribute(XCAFDoc_Datum::GetID(), aDatumAttr);
+    Handle(XCAFDimTolObjects_DatumObject) anObject = aDatumAttr->GetObject();
+    TCollection_AsciiString aDatumName = anObject->GetName()->String();
+    TCollection_AsciiString aDatumTargetId = TCollection_AsciiString(anObject->GetDatumTargetNumber());
+    if (!aNameIdMap.Add(aDatumName.Cat(aDatumTargetId)))
       continue;
-    aDatums.Append(aDatumObj);
-    aMaxDatumNum = Max(aMaxDatumNum, aDatumObj->GetPosition());
+    Handle(Standard_Transient) aWrittenDatum;
+    Standard_Boolean isFirstDT = !aDatumMap.Find(aDatumName, aWrittenDatum);
+    Handle(StepDimTol_Datum) aDatum = WriteDatumAP242(WS, aShapeL, aDatumL, isFirstDT, 
+                                                      Handle(StepDimTol_Datum)::DownCast (aWrittenDatum));
+    // Add created Datum into Map
+    aDatumMap.Bind(aDatumName, aDatum);
   }
-  if (aMaxDatumNum == 0)
-    return NULL;
 
-  Handle(StepDimTol_HArray1OfDatumReferenceCompartment) aConstituents =
-    new StepDimTol_HArray1OfDatumReferenceCompartment(1, aMaxDatumNum);
-  // Auxiliary datum to initialize attributes in Datum_System
-  Handle(StepDimTol_Datum) aFirstDatum;
-  Standard_Integer aConstituentsNum = 0;
-  for (Standard_Integer i = 1; i <= aMaxDatumNum; i++) {
-    // Collect datums with i-th position
-    XCAFDimTolObjects_DatumObjectSequence aDatumSeqPos;
-    for (Standard_Integer j = 1; j <= aDatums.Length(); j++)
-      if (aDatums.Value(j)->GetPosition() == i)
-        aDatumSeqPos.Append(aDatums.Value(j));
-    if (aDatumSeqPos.Length() < 1)
+  //----------------- //
+  // write Dimensions //
+  //------------------//
+  aDGTLabels.Clear();
+  DGTTool->GetDimensionLabels(aDGTLabels);
+  // Auxiliary entities for derived geometry
+  Handle(StepRepr_ConstructiveGeometryRepresentation) aCGRepr =
+    new StepRepr_ConstructiveGeometryRepresentation();
+  Handle(StepRepr_ConstructiveGeometryRepresentationRelationship) aCGReprRel =
+    new StepRepr_ConstructiveGeometryRepresentationRelationship();
+  NCollection_Vector<Handle(StepGeom_CartesianPoint)> aConnectionPnts;
+  Handle(StepRepr_RepresentationContext) dummyRC;
+  Handle(StepAP242_GeometricItemSpecificUsage) dummyGISU;
+  for (Standard_Integer i = 1; i <= aDGTLabels.Length(); i++) {
+    TDF_Label aDimensionL = aDGTLabels.Value(i);
+    TDF_LabelSequence aFirstShapeL, aSecondShapeL;
+    Handle(XCAFDoc_Dimension) aDimAttr;
+    if (!aDimensionL.FindAttribute(XCAFDoc_Dimension::GetID(),aDimAttr)) 
+      continue;
+    Handle(XCAFDimTolObjects_DimensionObject) anObject = aDimAttr->GetObject();
+    if (anObject.IsNull())
       continue;
+    if (anObject->GetType() == XCAFDimTolObjects_DimensionType_CommonLabel)
+    {
+      Handle(StepRepr_ShapeAspect) aSA = new StepRepr_ShapeAspect();
+      aSA->Init(new TCollection_HAsciiString(), new TCollection_HAsciiString(), myGDTCommonPDS, StepData_LTrue);
+      aModel->AddWithRefs(aSA);
+      WritePresentation(WS, anObject->GetPresentation(), anObject->GetPresentationName(), anObject->HasPlane(),
+        Standard_False, anObject->GetPlane(), anObject->GetPointTextAttach(), aSA);
+    }
 
-    aConstituentsNum++;
-    // Initialize Datum_Reference_Compartment
-    StepDimTol_DatumOrCommonDatum aDatumRef;
-    Handle(StepDimTol_DatumReferenceCompartment) aCompartment =
-      new StepDimTol_DatumReferenceCompartment();
-    Handle(StepDimTol_HArray1OfDatumReferenceModifier) aModifiers;
-    if (aDatumSeqPos.Length() == 1) {
-      // Datum entity
-      Handle(Standard_Transient) aFDValue;
-      if (theDatumMap.Find(aDatumSeqPos.Value(1)->GetName()->String(), aFDValue) && !aFDValue.IsNull())
-        aFirstDatum = Handle(StepDimTol_Datum)::DownCast (aFDValue);
-      aDatumRef.SetValue(aFirstDatum);
-      // Modifiers
-      XCAFDimTolObjects_DatumModifiersSequence aSimpleModifiers = aDatumSeqPos.Value(1)->GetModifiers();
-      XCAFDimTolObjects_DatumModifWithValue aModifWithVal;
-      Standard_Real aValue = 0;
-      aDatumSeqPos.Value(1)->GetModifierWithValue(aModifWithVal, aValue);
-      aModifiers = STEPCAFControl_GDTProperty::GetDatumRefModifiers(aSimpleModifiers, aModifWithVal, aValue, aUnit);
-      // Add Datum_Reference_Modifier_With_Value
-      if (!aModifiers.IsNull()) {
-        Handle(StepDimTol_DatumReferenceModifierWithValue) aDRMWV = 
-          aModifiers->Value(aModifiers->Length()).DatumReferenceModifierWithValue();
-        if (!aDRMWV.IsNull()) {
-          Model->AddWithRefs(aDRMWV);
+    if (!DGTTool->GetRefShapeLabel(aDimensionL, aFirstShapeL, aSecondShapeL))
+      continue;
+
+    // Write links with shapes
+    Handle(StepRepr_ShapeAspect) aFirstSA, aSecondSA;
+    if (aFirstShapeL.Length() == 1) {
+      TopoDS_Shape aShape = XCAFDoc_ShapeTool::GetShape(aFirstShapeL.Value(1));
+      aFirstSA = WriteShapeAspect(WS, aDimensionL, aShape, dummyRC, dummyGISU);
+      if (aRC.IsNull() && !dummyRC.IsNull())
+        aRC = dummyRC;
+    }
+    else if (aFirstShapeL.Length() > 1) {
+      Handle(StepRepr_CompositeShapeAspect) aCSA;
+      for (Standard_Integer shIt = 1; shIt <= aFirstShapeL.Length(); shIt++) {
+        TopoDS_Shape aShape = XCAFDoc_ShapeTool::GetShape(aFirstShapeL.Value(shIt));
+        Handle(StepRepr_ShapeAspect) aSA = WriteShapeAspect(WS, aDimensionL, aShape, dummyRC, dummyGISU);
+        if (aSA.IsNull())
+          continue;
+        if (aCSA.IsNull()) {
+          aCSA = new StepRepr_CompositeShapeAspect();
+          aCSA->Init(aSA->Name(), aSA->Description(), aSA->OfShape(), aSA->ProductDefinitional());
+          aModel->AddWithRefs(aCSA);
         }
+        Handle(StepRepr_ShapeAspectRelationship) aSAR = new StepRepr_ShapeAspectRelationship();
+        aSAR->Init(new TCollection_HAsciiString(), Standard_False, new TCollection_HAsciiString(), aCSA, aSA);
+        aModel->AddWithRefs(aSAR);
+        if (aRC.IsNull() && !dummyRC.IsNull())
+          aRC = dummyRC;
       }
+      aFirstSA = aCSA;
     }
-    else {
-      Handle(StepDimTol_HArray1OfDatumReferenceElement) aCommonDatumList = new StepDimTol_HArray1OfDatumReferenceElement(1, aDatumSeqPos.Length());
-      for (Standard_Integer j = 1; j <= aDatumSeqPos.Length(); j++) {
-        // Datum entity
-        Handle(StepDimTol_Datum) aDatum;
-        Handle(Standard_Transient) aDValue;
-        if (theDatumMap.Find(aDatumSeqPos.Value(j)->GetName()->String(), aDValue))
-          aDatum = Handle(StepDimTol_Datum)::DownCast (aDValue);
-        StepDimTol_DatumOrCommonDatum anElemDatumRef;
-        anElemDatumRef.SetValue(aDatum);
-        if (aFirstDatum.IsNull())
-          aFirstDatum = aDatum;
-        // Modifiers
-        XCAFDimTolObjects_DatumModifiersSequence aSimpleModifiers = aDatumSeqPos.Value(j)->GetModifiers();
-        XCAFDimTolObjects_DatumModifWithValue aModifWithVal;
-        Standard_Real aValue = 0;
-        aDatumSeqPos.Value(j)->GetModifierWithValue(aModifWithVal, aValue);
-        Handle(StepDimTol_HArray1OfDatumReferenceModifier) anElemModifiers =
-          STEPCAFControl_GDTProperty::GetDatumRefModifiers(aSimpleModifiers, aModifWithVal, aValue, aUnit);
-        // Add Datum_Reference_Modifier_With_Value
-        if (!anElemModifiers.IsNull()) {
-          Handle(StepDimTol_DatumReferenceModifierWithValue) aDRMWV = 
-            anElemModifiers->Value(anElemModifiers->Length()).DatumReferenceModifierWithValue();
-          if (!aDRMWV.IsNull()) {
-            Model->AddWithRefs(aDRMWV);
-          }
+    if (aSecondShapeL.Length() == 1) {
+      TopoDS_Shape aShape = XCAFDoc_ShapeTool::GetShape(aSecondShapeL.Value(1));
+      aSecondSA = WriteShapeAspect(WS, aDimensionL, aShape, dummyRC, dummyGISU);
+      if (aRC.IsNull() && !dummyRC.IsNull())
+        aRC = dummyRC;
+    }
+    else if (aSecondShapeL.Length() > 1) {
+      Handle(StepRepr_CompositeShapeAspect) aCSA;
+      for (Standard_Integer shIt = 1; shIt <= aSecondShapeL.Length(); shIt++) {
+        TopoDS_Shape aShape = XCAFDoc_ShapeTool::GetShape(aSecondShapeL.Value(shIt));
+        Handle(StepRepr_ShapeAspect) aSA = WriteShapeAspect(WS, aDimensionL, aShape, dummyRC, dummyGISU);
+        if (aCSA.IsNull() && !aSA.IsNull())
+        {
+          aCSA = new StepRepr_CompositeShapeAspect();
         }
-        // Datum_Reference_Element
-        Handle(StepDimTol_DatumReferenceElement) anElement = new StepDimTol_DatumReferenceElement();
-        anElement->Init(new TCollection_HAsciiString(), new TCollection_HAsciiString(), aDatum->OfShape(),
-          aDatum->ProductDefinitional(), anElemDatumRef, !anElemModifiers.IsNull(), anElemModifiers);
-        Model->AddWithRefs(anElement);
-        aCommonDatumList->SetValue(j, anElement);
+        aCSA->Init(aSA->Name(), aSA->Description(), aSA->OfShape(), aSA->ProductDefinitional());
+        if (!aSA.IsNull()) {
+          Handle(StepRepr_ShapeAspectRelationship) aSAR = new StepRepr_ShapeAspectRelationship();
+          aSAR->Init(new TCollection_HAsciiString(), Standard_False, new TCollection_HAsciiString(), aCSA, aSA);
+          aModel->AddWithRefs(aSAR);
+        }
+        if (aRC.IsNull() && !dummyRC.IsNull())
+          aRC = dummyRC;
       }
-      aDatumRef.SetValue(aCommonDatumList);
+      aSecondSA = aCSA;
     }
-    aCompartment->Init(new TCollection_HAsciiString(), new TCollection_HAsciiString(), aFirstDatum->OfShape(),
-      aFirstDatum->ProductDefinitional(), aDatumRef, !aModifiers.IsNull(), aModifiers);
-    Model->AddWithRefs(aCompartment);
-    aConstituents->SetValue(aConstituentsNum, aCompartment);
-  }
-  // Remove null elements from aConstituents
-  Standard_Integer aNbConstituents = 0;
-  for (Standard_Integer i = 1; i <= aConstituents->Length(); i++)
-    if (!aConstituents->Value(i).IsNull())
-      aNbConstituents++;
-  Handle(StepDimTol_HArray1OfDatumReferenceCompartment) aResConstituents =
-    new StepDimTol_HArray1OfDatumReferenceCompartment(1, aNbConstituents);
-  Standard_Integer aConstituentsIt = 0;
-  for (Standard_Integer i = 1; i <= aConstituents->Length(); i++)
-    if (!aConstituents->Value(i).IsNull()) {
-      aConstituentsIt++;
-      aResConstituents->SetValue(aConstituentsIt, aConstituents->Value(i));
+
+    if (anObject->GetType() == XCAFDimTolObjects_DimensionType_DimensionPresentation)
+    {
+      WritePresentation(WS, anObject->GetPresentation(), anObject->GetPresentationName(), anObject->HasPlane(),
+        Standard_False, anObject->GetPlane(), anObject->GetPointTextAttach(), aFirstSA);
+      continue;
+    }
+
+    // Write dimensions
+    StepShape_DimensionalCharacteristic aDimension;
+    if (anObject->HasPoint() || anObject->HasPoint2())
+      WriteDerivedGeometry(WS, anObject, aCGRepr, aFirstSA, aSecondSA, aConnectionPnts);
+    XCAFDimTolObjects_DimensionType aDimType = anObject->GetType();
+    if (STEPCAFControl_GDTProperty::IsDimensionalLocation(aDimType)) {
+      // Dimensional_Location
+      Handle(StepShape_DimensionalLocation) aDim = new StepShape_DimensionalLocation();
+      aDim->Init(STEPCAFControl_GDTProperty::GetDimTypeName(aDimType), Standard_False, NULL, aFirstSA, aSecondSA);
+      aDimension.SetValue(aDim);
+    }
+    else if (aDimType == XCAFDimTolObjects_DimensionType_Location_Angular) {
+      // Angular_Location
+      Handle(StepShape_AngularLocation) aDim = new StepShape_AngularLocation();
+      StepShape_AngleRelator aRelator = StepShape_Equal;
+      if (anObject->HasQualifier()) {
+        XCAFDimTolObjects_DimensionQualifier aQualifier = anObject->GetQualifier();
+        switch (aQualifier) {
+          case XCAFDimTolObjects_DimensionQualifier_Min: aRelator = StepShape_Small;
+            break;
+          case XCAFDimTolObjects_DimensionQualifier_Max: aRelator = StepShape_Large;
+            break;
+          default: aRelator = StepShape_Equal;
+        }
+      }
+      aDim->Init(new TCollection_HAsciiString(), Standard_False, NULL, aFirstSA, aSecondSA, aRelator);
+      aDimension.SetValue(aDim);
+    }
+    else if (aDimType == XCAFDimTolObjects_DimensionType_Location_WithPath) {
+      // Dimensional_Location_With_Path
+      Handle(StepShape_DimensionalLocationWithPath) aDim = new StepShape_DimensionalLocationWithPath();
+      Handle(StepRepr_ShapeAspect) aPathSA = WriteShapeAspect(WS, aDimensionL, anObject->GetPath(), dummyRC, dummyGISU);
+      aDim->Init(new TCollection_HAsciiString(), Standard_False, NULL, aFirstSA, aSecondSA, aPathSA);
+      aDimension.SetValue(aDim);
+    }
+    else if (STEPCAFControl_GDTProperty::IsDimensionalSize(aDimType)) {
+      // Dimensional_Size
+      Handle(StepShape_DimensionalSize) aDim = new StepShape_DimensionalSize();
+      aDim->Init(aFirstSA, STEPCAFControl_GDTProperty::GetDimTypeName(aDimType));
+      aDimension.SetValue(aDim);
+    }
+    else if (aDimType == XCAFDimTolObjects_DimensionType_Size_Angular) {
+      // Angular_Size
+      Handle(StepShape_AngularSize) aDim = new StepShape_AngularSize();
+      StepShape_AngleRelator aRelator = StepShape_Equal;
+      if (anObject->HasQualifier()) {
+        XCAFDimTolObjects_DimensionQualifier aQualifier = anObject->GetQualifier();
+        switch (aQualifier) {
+          case XCAFDimTolObjects_DimensionQualifier_Min: aRelator = StepShape_Small;
+            break;
+          case XCAFDimTolObjects_DimensionQualifier_Max: aRelator = StepShape_Large;
+            break;
+          default: aRelator = StepShape_Equal;
+        }
+      }
+      aDim->Init(aFirstSA, new TCollection_HAsciiString(), aRelator);
+      aDimension.SetValue(aDim);
+    }
+    else if (aDimType == XCAFDimTolObjects_DimensionType_Size_WithPath) {
+      // Dimensional_Size_With_Path
+      Handle(StepShape_DimensionalSizeWithPath) aDim = new StepShape_DimensionalSizeWithPath();
+      Handle(StepRepr_ShapeAspect) aPathSA = WriteShapeAspect(WS, aDimensionL, anObject->GetPath(), dummyRC, dummyGISU);
+      aDim->Init(aFirstSA, new TCollection_HAsciiString(), aPathSA);
+      aDimension.SetValue(aDim);
     }
 
-  Handle(StepDimTol_HArray1OfDatumSystemOrReference) aDatumSystem;
-  Handle(StepDimTol_DatumSystem) aDS = new StepDimTol_DatumSystem();
-  aDS->Init(new TCollection_HAsciiString(), new TCollection_HAsciiString(), aFirstDatum->OfShape(),
-    aFirstDatum->ProductDefinitional(), aResConstituents);
-  Model->AddWithRefs(aDS);
-  StepDimTol_DatumSystemOrReference anArrayValue;
-  anArrayValue.SetValue(aDS);
-  aDatumSystem = new StepDimTol_HArray1OfDatumSystemOrReference(1, 1);
-  aDatumSystem->SetValue(1, anArrayValue);
+    // Write values
+    WriteDimValues(WS, anObject, aRC, aDimension);
+    //Annotation plane and Presentation
+    WritePresentation(WS, anObject->GetPresentation(), anObject->GetPresentationName(), Standard_True, anObject->HasPlane(),
+      anObject->GetPlane(), anObject->GetPointTextAttach(), aDimension.Value());
+  }
+  // Write Derived geometry
+  if (aConnectionPnts.Length() > 0) {
+    Handle(StepRepr_HArray1OfRepresentationItem) anItems = new StepRepr_HArray1OfRepresentationItem(1, aConnectionPnts.Length());
+    for (Standard_Integer i = 0; i < aConnectionPnts.Length(); i++)
+      anItems->SetValue(i + 1, aConnectionPnts(i));
+    aCGRepr->Init(new TCollection_HAsciiString(), anItems, dummyRC);
+    aCGReprRel->Init(new TCollection_HAsciiString(), new TCollection_HAsciiString(), dummyGISU->UsedRepresentation(), aCGRepr);
+    aModel->AddWithRefs(aCGReprRel);
+  }
 
-  // Axis
-  if (anObject->HasAxis()) {
-    GeomToStep_MakeAxis2Placement3d anAxisMaker(anObject->GetAxis());
-    Handle(StepGeom_Axis2Placement3d) anAxis = anAxisMaker.Value();
-    anAxis->SetName(new TCollection_HAsciiString("orientation"));
-    Handle(StepAP242_GeometricItemSpecificUsage) aGISU = new StepAP242_GeometricItemSpecificUsage();
-    StepAP242_ItemIdentifiedRepresentationUsageDefinition aDefinition;
-    aDefinition.SetValue(aDS);
-    Handle(StepRepr_HArray1OfRepresentationItem) anReprItems = new StepRepr_HArray1OfRepresentationItem(1, 1);
-    Handle(StepRepr_RepresentationItem) anIdentifiedItem = anAxis;
-    anReprItems->SetValue(1, anIdentifiedItem);
-    Interface_EntityIterator subs = aGraph.Sharings(aFirstDatum->OfShape());
-    Handle(StepShape_ShapeDefinitionRepresentation) aSDR;
-    for (subs.Start(); subs.More() && aSDR.IsNull(); subs.Next()) {
-      Handle(Standard_Transient) anEntity = subs.Value();
-      aSDR = Handle(StepShape_ShapeDefinitionRepresentation)::DownCast(anEntity);
-    }
-    if (aSDR.IsNull())
-      return aDatumSystem;
-    
-    aGISU->Init(new TCollection_HAsciiString(), new TCollection_HAsciiString(),
-      aDefinition, aSDR->UsedRepresentation(), anReprItems);
-    Model->AddWithRefs(anAxis);
-    Model->AddWithRefs(aGISU);
+  //----------------------------//
+  // write Geometric Tolerances //
+  //----------------------------//
+  aDGTLabels.Clear();
+  DGTTool->GetGeomToleranceLabels(aDGTLabels);
+  for (Standard_Integer i = 1; i <= aDGTLabels.Length(); i++) {
+    TDF_Label aGeomTolL = aDGTLabels.Value(i);
+    TDF_LabelSequence aFirstShapeL, aNullSeqL;
+    if (!DGTTool->GetRefShapeLabel(aGeomTolL, aFirstShapeL, aNullSeqL))
+      continue;
+    TDF_LabelSequence aDatumSeq;
+    DGTTool->GetDatumWithObjectOfTolerLabels(aGeomTolL, aDatumSeq);
+    Handle(StepDimTol_HArray1OfDatumSystemOrReference) aDatumSystem;
+    if (aDatumSeq.Length() > 0)
+      aDatumSystem = WriteDatumSystem(WS, aGeomTolL, aDatumSeq, aDatumMap, aRC);
+    WriteGeomTolerance(WS, aFirstShapeL, aGeomTolL, aDatumSystem, aRC);
   }
 
-  return aDatumSystem;
+  // Write Draughting model for Annotation Planes
+  if (myGDTAnnotations.Length() == 0)
+    return Standard_True;
+
+  Handle(StepRepr_HArray1OfRepresentationItem) aItems =
+    new StepRepr_HArray1OfRepresentationItem(1, myGDTAnnotations.Length());
+  for (Standard_Integer i = 1; i <= aItems->Length(); i++) {
+    aItems->SetValue(i, myGDTAnnotations.Value(i - 1));
+  }
+  myGDTPresentationDM->Init(new TCollection_HAsciiString(), aItems, aRC);
+  aModel->AddWithRefs(myGDTPresentationDM);
+
+  return Standard_True;
 }
 
 //=======================================================================
-//function : WriteToleranceZone
-//purpose  : auxiliary (write tolerace zones)
+//function : createKinematicLink
+//purpose  : auxilary
 //=======================================================================
-void STEPCAFControl_Writer::WriteToleranceZone (const Handle(XSControl_WorkSession) &WS,
-                                                const Handle(XCAFDimTolObjects_GeomToleranceObject)& theObject,
-                                                const Handle(StepDimTol_GeometricTolerance)& theEntity,
-                                                const Handle(StepRepr_RepresentationContext)& theRC)
+static Standard_Boolean createKinematicLink(const Handle(XSControl_WorkSession)& theWS,
+                                            const Handle(XCAFDoc_KinematicTool)& theKTool,
+                                            const TDF_Label& theLabelOfLink,
+                                            NCollection_IndexedDataMap<TDF_Label, Handle(StepKinematics_KinematicLinkRepresentation), TDF_LabelMapHasher>& theMapOfLinks,
+                                            Handle(StepKinematics_KinematicLinkRepresentation)& theLinkRepr,
+                                            Handle(TColStd_HSequenceOfTransient)& theSeqOfContexLink,
+                                            Handle(StepKinematics_RigidLinkRepresentation)& theBaseLink,
+                                            Handle(StepRepr_PropertyDefinition)& theGeneralPD)
 {
-  // Get working data
-  const Handle(Interface_InterfaceModel) &Model = WS->Model();
-  if (theEntity.IsNull() || theObject.IsNull())
-    return;
-
-  // Return if there is no tolerance zones
-  if (theObject->GetTypeOfValue() == XCAFDimTolObjects_GeomToleranceTypeValue_None &&
-      theObject->GetZoneModifier() != XCAFDimTolObjects_GeomToleranceZoneModif_Runout)
-    return;
-
-  // Create Tolerance_Zone
-  Handle(StepDimTol_ToleranceZoneForm) aForm = new StepDimTol_ToleranceZoneForm();
-  Model->AddWithRefs(aForm);
-  aForm->Init(STEPCAFControl_GDTProperty::GetTolValueType(theObject->GetTypeOfValue()));
-  Handle(StepDimTol_HArray1OfToleranceZoneTarget) aZoneTargetArray = new StepDimTol_HArray1OfToleranceZoneTarget(1, 1);
-  StepDimTol_ToleranceZoneTarget aTarget;
-  aTarget.SetValue(theEntity);
-  aZoneTargetArray->SetValue(1, aTarget);
-  Handle(StepDimTol_ToleranceZone) aZone = new StepDimTol_ToleranceZone();
-  aZone->Init(new TCollection_HAsciiString(), new TCollection_HAsciiString(),
-    theEntity->TolerancedShapeAspect().ShapeAspect()->OfShape(), StepData_LFalse,
-    aZoneTargetArray, aForm);
-  Model->AddWithRefs(aZone);
 
-  // Runout_Tolerance_Zone
-  Handle(StepBasic_PlaneAngleMeasureWithUnit) aPAMWU = new StepBasic_PlaneAngleMeasureWithUnit();
-  Handle(StepBasic_MeasureValueMember) aValueMember = new StepBasic_MeasureValueMember();
-  aValueMember->SetName("PLANE_ANGLE_MEASURE");
-  aValueMember->SetReal(theObject->GetValueOfZoneModifier());
-  aPAMWU->Init(aValueMember, GetUnit(theRC, Standard_True));
-  Handle(StepDimTol_RunoutZoneOrientation) anOrientation = new StepDimTol_RunoutZoneOrientation();
-  anOrientation->Init(aPAMWU);
-  Handle(StepDimTol_RunoutZoneDefinition) aDefinition = new StepDimTol_RunoutZoneDefinition();
-  aDefinition->Init(aZone, NULL, anOrientation);
-  Model->AddWithRefs(aDefinition);
-  Model->AddWithRefs(anOrientation);
-  Model->AddWithRefs(aPAMWU);
-}
+  if (theMapOfLinks.FindFromKey(theLabelOfLink, theLinkRepr))
+    return Standard_True;
 
-//=======================================================================
-//function : WriteGeomTolerance
-//purpose  : auxiliary (write Geometric_Tolerance entity for given shapes,
-//           label and datum system)
-//======================================================================
-void STEPCAFControl_Writer::WriteGeomTolerance (const Handle(XSControl_WorkSession) &WS,
-                                                const TDF_LabelSequence theShapeSeqL,
-                                                const TDF_Label theGeomTolL,
-                                                const Handle(StepDimTol_HArray1OfDatumSystemOrReference)& theDatumSystem,
-                                                const Handle(StepRepr_RepresentationContext)& theRC)
-{
-  // Get working data
-  const Handle(Interface_InterfaceModel) &Model = WS->Model();
-  Handle(XCAFDoc_GeomTolerance) aGTAttr;
-  if (!theGeomTolL.FindAttribute(XCAFDoc_GeomTolerance::GetID(), aGTAttr)) 
-    return;
-  Handle(XCAFDimTolObjects_GeomToleranceObject) anObject = aGTAttr->GetObject();
-  if (anObject.IsNull())
-    return;
+  const Handle(XSControl_TransferWriter)& TW = theWS->TransferWriter();
+  const Handle(Transfer_FinderProcess)& FP = TW->FinderProcess();
+  Interface_Graph aGraph = theWS->HGraph()->Graph();
 
-  // Value
-  Handle(StepBasic_LengthMeasureWithUnit) aLMWU = new StepBasic_LengthMeasureWithUnit();
-  StepBasic_Unit aUnit = GetUnit(theRC);
-  Handle(StepBasic_MeasureValueMember) aValueMember = new StepBasic_MeasureValueMember();
-  aValueMember->SetName("LENGTH_MEASURE");
-  aValueMember->SetReal(anObject->GetValue());
-  aLMWU->Init(aValueMember, aUnit);
-  Model->AddWithRefs(aLMWU);
+  Handle(StepKinematics_KinematicLink) aLink = new StepKinematics_KinematicLink;
+  Handle(TDataStd_Name) aNameLink;
+  Handle(TCollection_HAsciiString) aHNameLink;
+  if (theLabelOfLink.FindAttribute(TDataStd_Name::GetID(), aNameLink))
+    aHNameLink = new TCollection_HAsciiString(aNameLink->Get());
+  else 
+    aHNameLink = new TCollection_HAsciiString("");
+  aLink->Init(aHNameLink);
 
-  // Geometric_Tolerance target
-  Handle(StepRepr_ShapeAspect) aMainSA;
-  Handle(StepRepr_RepresentationContext) dummyRC;
-  Handle(StepAP242_GeometricItemSpecificUsage) dummyGISU;
-  if (theShapeSeqL.Length() == 1) {
-    TopoDS_Shape aShape = XCAFDoc_ShapeTool::GetShape(theShapeSeqL.Value(1));
-    aMainSA = WriteShapeAspect(WS, theGeomTolL, aShape, dummyRC, dummyGISU);
-    Model->AddWithRefs(aMainSA);
-  }
-  else {
-    Handle(StepRepr_CompositeShapeAspect) aCSA;
-    for (Standard_Integer i = 1; i <= theShapeSeqL.Length(); i++) {
-      TopoDS_Shape aShape = XCAFDoc_ShapeTool::GetShape(theShapeSeqL.Value(i));
-      Handle(StepRepr_ShapeAspect) aSA = WriteShapeAspect(WS, theGeomTolL, aShape, dummyRC, dummyGISU);
-      if (aSA.IsNull())
-        continue;
-      if (aCSA.IsNull()) {
-        aCSA = new StepRepr_CompositeShapeAspect();
-        aCSA->Init(aSA->Name(), aSA->Description(), aSA->OfShape(), aSA->ProductDefinitional());
-        Model->AddWithRefs(aCSA);
-      }
-      Handle(StepRepr_ShapeAspectRelationship) aSAR = new StepRepr_ShapeAspectRelationship();
-      aSAR->Init(new TCollection_HAsciiString(), Standard_False, NULL, aCSA, aSA);
-      Model->AddWithRefs(aSAR);
-    }
-    aMainSA = aCSA;
+  // Choose type of the representation link
+  Standard_Boolean isLinear = Standard_True;
+  TDF_LabelSequence aJoints = theKTool->GetJointsOfLink(theLabelOfLink, Standard_True, Standard_False);
+  if (aJoints.IsEmpty())
+    isLinear = Standard_False;
+  for (TDF_LabelSequence::Iterator anItJoint(aJoints); anItJoint.More() && isLinear; anItJoint.Next())
+  {
+    Handle(XCAFDoc_KinematicPair) aKPairAttr;
+    if (!anItJoint.Value().FindAttribute(XCAFDoc_KinematicPair::GetID(), aKPairAttr))
+      continue;
+    Handle(XCAFKinematics_PairObject) aPairObject = aKPairAttr->GetObject();
+    if (aPairObject->Type() == XCAFKinematics_PairType_NoType)
+      continue;
+    if (aPairObject->Type() != XCAFKinematics_PairType_LinearFlexibleAndPinion &&
+        aPairObject->Type() != XCAFKinematics_PairType_LinearFlexibleAndPlanarCurve)
+      isLinear = Standard_False;
   }
-  StepDimTol_GeometricToleranceTarget aGTTarget;
-  aGTTarget.SetValue(aMainSA);
+  aJoints = theKTool->GetJointsOfLink(theLabelOfLink, Standard_False, Standard_True);
+  if (!aJoints.IsEmpty())
+    isLinear = Standard_False;
 
-  Standard_Boolean isWithModif = Standard_False,
-                   isWithDatRef = Standard_False,
-                   isWithMaxTol = Standard_False;
-  // Modifiers
-  // Simple modifiers
-  XCAFDimTolObjects_GeomToleranceModifiersSequence aModifiers = anObject->GetModifiers();
-  Handle(StepDimTol_HArray1OfGeometricToleranceModifier) aModifArray;
-  Handle(StepBasic_LengthMeasureWithUnit) aMaxLMWU;
-  Standard_Integer aModifNb = aModifiers.Length();
-  if (anObject->GetMaterialRequirementModifier() != XCAFDimTolObjects_GeomToleranceMatReqModif_None)
-      aModifNb++;
-  for (Standard_Integer i = 1; i <= aModifiers.Length(); i++)
-    if (aModifiers.Value(i) == XCAFDimTolObjects_GeomToleranceModif_All_Around ||
-        aModifiers.Value(i) == XCAFDimTolObjects_GeomToleranceModif_All_Over)
-        aModifNb--;
-  if (aModifNb > 0) {
-    isWithModif = Standard_True;
-    aModifArray = new StepDimTol_HArray1OfGeometricToleranceModifier(1, aModifNb);
-    Standard_Integer k = 1;
-    for (Standard_Integer i = 1; i <= aModifiers.Length(); i++) {
-      if (aModifiers.Value(i) == XCAFDimTolObjects_GeomToleranceModif_All_Around ||
-        aModifiers.Value(i) == XCAFDimTolObjects_GeomToleranceModif_All_Over)
-        continue;
-      StepDimTol_GeometricToleranceModifier aModif = 
-        STEPCAFControl_GDTProperty::GetGeomToleranceModifier(aModifiers.Value(i));
-      aModifArray->SetValue(k, aModif);
-      k++;
-    }
-    if (anObject->GetMaterialRequirementModifier() == XCAFDimTolObjects_GeomToleranceMatReqModif_L) {
-      aModifArray->SetValue(aModifNb, StepDimTol_GTMLeastMaterialRequirement);
+  Handle(TCollection_HAsciiString) aNameOfLinkRepr = new TCollection_HAsciiString("");
+  // Create empty collection for updating during creating a referenced pairs
+  Handle(StepRepr_HArray1OfRepresentationItem) aPlacementsOfPairs = new StepRepr_HArray1OfRepresentationItem();
+
+  TDF_LabelSequence aShapesL = theKTool->GetRefShapes(theLabelOfLink);
+  if (aShapesL.IsEmpty())
+    return Standard_False;
+
+  // Find ref shapes
+  for (TDF_LabelSequence::Iterator anItShapes(aShapesL); anItShapes.More(); anItShapes.Next()) {
+    TopoDS_Shape aTopoShape = XCAFDoc_ShapeTool::GetShape(anItShapes.Value());
+    TopLoc_Location aLoc;
+    TColStd_SequenceOfTransient seqRI;
+    FindEntities(FP, aTopoShape, aLoc, seqRI);
+    if (seqRI.Length() <= 0) {
+      FP->Messenger()->SendInfo() << "Warning: Cannot find RI for " << aTopoShape.TShape()->DynamicType()->Name() << std::endl;
+      continue;
     }
-    else if (anObject->GetMaterialRequirementModifier() == XCAFDimTolObjects_GeomToleranceMatReqModif_M) {
-      aModifArray->SetValue(aModifNb, StepDimTol_GTMMaximumMaterialRequirement);
+
+    // Get PDS of Shape
+    Handle(StepShape_ShapeRepresentation) aShapeRepr;
+    Handle(StepRepr_PropertyDefinition) aPD;
+    Handle(StepRepr_RepresentationContext) aRC;
+    aPD = FindPDS(aGraph, seqRI.Value(1), aRC);
+    if (aPD.IsNull())
+      continue;
+    Interface_EntityIterator aSRIt = aGraph.Sharings(aRC);
+    for (aSRIt.Start(); aSRIt.More() && aShapeRepr.IsNull(); aSRIt.Next())
+      aShapeRepr = Handle(StepShape_ShapeRepresentation)::DownCast(aSRIt.Value());
+    if (aShapeRepr.IsNull())
+      continue;
+
+    if (theLinkRepr.IsNull())
+    {
+      if (isLinear)
+        theLinkRepr = new StepKinematics_LinearFlexibleLinkRepresentation;
+      else
+        theLinkRepr = new StepKinematics_RigidLinkRepresentation;
+      theLinkRepr->Init(aNameOfLinkRepr, aPlacementsOfPairs, aRC, aLink);
     }
-    // Modifier with value
-    if (anObject->GetMaxValueModifier() != 0) {
-      isWithMaxTol = Standard_True;
-      aMaxLMWU = new StepBasic_LengthMeasureWithUnit();
-      Handle(StepBasic_MeasureValueMember) aModifierValueMember = new StepBasic_MeasureValueMember();
-      aModifierValueMember->SetName("LENGTH_MEASURE");
-      aModifierValueMember->SetReal(anObject->GetMaxValueModifier());
-      aMaxLMWU->Init(aModifierValueMember, aUnit);
-      Model->AddWithRefs(aMaxLMWU);
+
+    Handle(TDataStd_Integer) aBase;
+    if (theBaseLink.IsNull() || theLabelOfLink.FindAttribute(TDataStd_Integer::GetID(), aBase)) {
+      theGeneralPD = aPD;
+      theBaseLink = Handle(StepKinematics_RigidLinkRepresentation)::DownCast(theLinkRepr);
     }
+
+    Handle(StepKinematics_KinematicLinkRepresentationAssociation) aLinkRepresentationAssociation = new StepKinematics_KinematicLinkRepresentationAssociation;
+    aLinkRepresentationAssociation->Init(aNameOfLinkRepr, aNameOfLinkRepr, theLinkRepr, aShapeRepr);
+    Handle(StepKinematics_ContextDependentKinematicLinkRepresentation) aCDKLRS = new StepKinematics_ContextDependentKinematicLinkRepresentation;
+    Handle(StepKinematics_ProductDefinitionRelationshipKinematics) aPDRK = new StepKinematics_ProductDefinitionRelationshipKinematics;
+    aPDRK->Init(aNameOfLinkRepr, Standard_False, aLinkRepresentationAssociation->Description(), aPD->Definition());
+    aCDKLRS->Init(aLinkRepresentationAssociation, aPDRK);
+    theSeqOfContexLink->Append(aCDKLRS);
   }
 
-  // Datum Reference
-  isWithDatRef = !theDatumSystem.IsNull();
+  if (theLinkRepr.IsNull())
+    return Standard_False;
 
-  // Collect all attributes
-  Handle(TCollection_HAsciiString) aName = new TCollection_HAsciiString(),
-                                  aDescription = new TCollection_HAsciiString();
-  Handle(StepDimTol_GeometricToleranceWithDatumReference) aGTWDR = 
-    new StepDimTol_GeometricToleranceWithDatumReference();
-  aGTWDR->SetDatumSystem(theDatumSystem);
-  Handle(StepDimTol_GeometricToleranceWithModifiers) aGTWM = 
-    new StepDimTol_GeometricToleranceWithModifiers();
-  aGTWM->SetModifiers(aModifArray);
-  StepDimTol_GeometricToleranceType aType = 
-    STEPCAFControl_GDTProperty::GetGeomToleranceType(anObject->GetType());
+  theMapOfLinks.Add(theLabelOfLink, theLinkRepr);
+  return Standard_True;
+}
 
-  // Init and write necessary subtype of Geometric_Tolerance entity
-  Handle(StepDimTol_GeometricTolerance) aGeomTol;
-  if (isWithModif) {
-    if (isWithMaxTol) {
-      if (isWithDatRef) {
-        // Geometric_Tolerance & Geometric_Tolerance_With_Datum_Reference & 
-        //Geometric_Tolerance_With_Maximum_Tolerance & Geometric_Tolerance_With_Modifiers
-        Handle(StepDimTol_GeoTolAndGeoTolWthDatRefAndGeoTolWthMaxTol) aResult =
-          new StepDimTol_GeoTolAndGeoTolWthDatRefAndGeoTolWthMaxTol();
-        aResult->Init(aName, aDescription, aLMWU, aGTTarget, aGTWDR, aGTWM, aMaxLMWU, aType);
-        aGeomTol = aResult;
+//=======================================================================
+//function : createKinematicJoint
+//purpose  : auxilary
+//=======================================================================
+static Standard_Boolean createKinematicJoint(const Handle(XCAFKinematics_PairObject)& theKinPairObj,
+                                             Handle(StepKinematics_KinematicLinkRepresentation)& theLinkRepr1,
+                                             Handle(StepKinematics_KinematicLinkRepresentation)& theLinkRepr2,
+                                             Handle(StepKinematics_KinematicJoint)& theJoint)
+{
+  theJoint = new StepKinematics_KinematicJoint;
+  Handle(TCollection_HAsciiString) aNameJoint = new TCollection_HAsciiString(theKinPairObj->Name());
+
+  Handle(StepShape_Vertex) anEdgeStart;
+  Handle(StepShape_Vertex) anEdgeEnd;
+
+  anEdgeStart = theLinkRepr1->RepresentedLink();
+  anEdgeEnd = theLinkRepr2->RepresentedLink();
+
+  if (anEdgeEnd.IsNull() || anEdgeStart.IsNull())
+    return Standard_False;
+
+  theJoint->Init(aNameJoint, anEdgeStart, anEdgeEnd);
+  return Standard_True;
+}
+
+//=======================================================================
+//function : createKinematicPair
+//purpose  : auxilary
+//=======================================================================
+static Standard_Boolean createKinematicPair(const Handle(XCAFKinematics_PairObject)& theKinPairObj,
+                                            const Handle(StepKinematics_KinematicJoint)& theJoint,
+                                            const Handle(StepKinematics_KinematicLinkRepresentation)& theLinkRepr1,
+                                            const Handle(StepKinematics_KinematicLinkRepresentation)& theLinkRepr2,
+                                            Handle(StepKinematics_KinematicPair)& theKinematicPair)
+{
+  if (theKinPairObj->Type() == XCAFKinematics_PairType_NoType)
+    return Standard_False;
+  Handle(StepRepr_RepresentationItem) aTransformItem1 = GeomToStep_MakeAxis2Placement3d(theKinPairObj->FirstTransformation()).Value();
+  Handle(StepRepr_RepresentationItem) aTransformItem2 = GeomToStep_MakeAxis2Placement3d(theKinPairObj->SecondTransformation()).Value();
+  if (aTransformItem1.IsNull() || aTransformItem2.IsNull())
+    return Standard_False;
+
+  Standard_Boolean hasDescription = Standard_False;
+  Handle(TCollection_HAsciiString) aPairName = new TCollection_HAsciiString(theKinPairObj->Name());
+  Handle(TCollection_HAsciiString) aDescription;
+  Standard_Boolean isRanged = theKinPairObj->HasLimits();
+  if (theKinPairObj->IsKind(STANDARD_TYPE(XCAFKinematics_LowOrderPairObject)))
+  {
+    Handle(XCAFKinematics_LowOrderPairObject) aLowOrderPairObj = Handle(XCAFKinematics_LowOrderPairObject)::DownCast(theKinPairObj);
+    Standard_Boolean aTX = Standard_True;
+    Standard_Boolean aTY = Standard_True;
+    Standard_Boolean aTZ = Standard_True;
+    Standard_Boolean aRX = Standard_True;
+    Standard_Boolean aRY = Standard_True;
+    Standard_Boolean aRZ = Standard_True;
+    switch (theKinPairObj->Type())
+    {
+    case(XCAFKinematics_PairType_Revolute):
+    {
+      aTX = Standard_False;
+      aTY = Standard_False;
+      aTZ = Standard_False;
+      aRX = Standard_False;
+      aRY = Standard_False;
+      if (isRanged)
+      {
+        theKinematicPair = new StepKinematics_RevolutePairWithRange;
+        Standard_Real aLowerLimitActualRotation = aLowOrderPairObj->MinRotationZ();
+        Standard_Real aUpperLimitActualRotation = aLowOrderPairObj->MaxRotationZ();
+        aRZ = Standard_True;
+        Handle(StepKinematics_RevolutePairWithRange) aRevolutePairWithRange = Handle(StepKinematics_RevolutePairWithRange)::DownCast(theKinematicPair);
+        aRevolutePairWithRange->Init(aPairName, aPairName, hasDescription,
+          aDescription, aTransformItem1, aTransformItem2, theJoint,
+          aTX, aTY, aTZ, aRX, aRY, aRZ, Standard_True, aLowerLimitActualRotation, Standard_True, aUpperLimitActualRotation);
       }
-      else {
-        // Geometric_Tolerance & Geometric_Tolerance_With_Maximum_Tolerance & Geometric_Tolerance_With_Modifiers
-        Handle(StepDimTol_GeoTolAndGeoTolWthMaxTol) aResult =
-          new StepDimTol_GeoTolAndGeoTolWthMaxTol();
-        aResult->Init(aName, aDescription, aLMWU, aGTTarget, aGTWM, aMaxLMWU, aType);
-        aGeomTol = aResult;
+      else
+      {
+        theKinematicPair = new StepKinematics_RevolutePair;
+        Handle(StepKinematics_RevolutePair) aRevolutePair = Handle(StepKinematics_RevolutePair)::DownCast(theKinematicPair);
+        aRevolutePair->Init(aPairName, aPairName, hasDescription,
+          aDescription, aTransformItem1, aTransformItem2, theJoint,
+          aTX, aTY, aTZ, aRX, aRY, aRZ);
+      }
+      break;
+    }
+    case(XCAFKinematics_PairType_Prismatic):
+    {
+      aTY = Standard_False;
+      aTZ = Standard_False;
+      aRX = Standard_False;
+      aRY = Standard_False;
+      aRZ = Standard_False;
+      if (isRanged)
+      {
+        theKinematicPair = new StepKinematics_PrismaticPairWithRange;
+        Handle(StepKinematics_PrismaticPairWithRange) aPrismaticPairWithRange = Handle(StepKinematics_PrismaticPairWithRange)::DownCast(theKinematicPair);
+        Standard_Real aUpperLimitActualTranslation = aLowOrderPairObj->MaxTranslationX();
+        Standard_Real aLowerLimitActualTranslation = aLowOrderPairObj->MinTranslationX();
+        aPrismaticPairWithRange->Init(aPairName, aPairName, hasDescription,
+          aDescription, aTransformItem1, aTransformItem2, theJoint,
+          aTX, aTY, aTZ, aRX, aRY, aRZ, Standard_True, aLowerLimitActualTranslation, Standard_True, aUpperLimitActualTranslation);
+      }
+      else
+      {
+        theKinematicPair = new StepKinematics_PrismaticPair;
+        Handle(StepKinematics_PrismaticPair) aPrismaticPair = Handle(StepKinematics_PrismaticPair)::DownCast(theKinematicPair);
+        aPrismaticPair->Init(aPairName, aPairName, hasDescription,
+          aDescription, aTransformItem1, aTransformItem2, theJoint,
+          aTX, aTY, aTZ, aRX, aRY, aRZ);
+      }
+      break;
+    }
+    case(XCAFKinematics_PairType_Cylindrical):
+    {
+      aTX = Standard_False;
+      aTY = Standard_False;
+      aRX = Standard_False;
+      aRY = Standard_False;
+      if (isRanged)
+      {
+        theKinematicPair = new StepKinematics_CylindricalPairWithRange;
+        Handle(StepKinematics_CylindricalPairWithRange) aCylindricalPairWithRange = Handle(StepKinematics_CylindricalPairWithRange)::DownCast(theKinematicPair);
+        Standard_Real aUpperLimitActualTranslation = aLowOrderPairObj->MaxTranslationZ();
+        Standard_Real aLowerLimitActualTranslation = aLowOrderPairObj->MinTranslationZ();
+        Standard_Real aLowerLimitActualRotation = aLowOrderPairObj->MinRotationZ();
+        Standard_Real aUpperLimitActualRotation = aLowOrderPairObj->MaxRotationZ();
+        aCylindricalPairWithRange->Init(aPairName, aPairName, hasDescription,
+          aDescription, aTransformItem1, aTransformItem2, theJoint,
+          aTX, aTY, aTZ, aRX, aRY, aRZ, Standard_True, aLowerLimitActualTranslation, Standard_True,
+          aUpperLimitActualTranslation, Standard_True, aLowerLimitActualRotation, Standard_True, aUpperLimitActualRotation);
+      }
+      else
+      {
+        theKinematicPair = new StepKinematics_CylindricalPair;
+        Handle(StepKinematics_CylindricalPair) aCylindricalPair = Handle(StepKinematics_CylindricalPair)::DownCast(theKinematicPair);
+        aCylindricalPair->Init(aPairName, aPairName, hasDescription,
+          aDescription, aTransformItem1, aTransformItem2, theJoint,
+          aTX, aTY, aTZ, aRX, aRY, aRZ);
+      }
+      break;
+    }
+    case(XCAFKinematics_PairType_Universal):
+    {
+      aTX = Standard_False;
+      aTY = Standard_False;
+      aTZ = Standard_False;
+      aRY = Standard_False;
+      Standard_Real theUniversalPair_InputSkewAngle = aLowOrderPairObj->SkewAngle();
+      if (isRanged)
+      {
+        theKinematicPair = new StepKinematics_UniversalPairWithRange;
+        Handle(StepKinematics_UniversalPairWithRange) anUniversalPairWithRange = Handle(StepKinematics_UniversalPairWithRange)::DownCast(theKinematicPair);
+        Standard_Real aUpperLimitActualRotationX = aLowOrderPairObj->MaxRotationX();
+        Standard_Real aLowerLimitActualRotationX = aLowOrderPairObj->MinRotationX();
+        Standard_Real aUpperLimitActualRotationZ = aLowOrderPairObj->MaxRotationZ();
+        Standard_Real aLowerLimitActualRotationZ = aLowOrderPairObj->MinRotationZ();
+        anUniversalPairWithRange->Init(aPairName, aPairName, hasDescription,
+          aDescription, aTransformItem1, aTransformItem2, theJoint,
+          aTX, aTY, aTZ, aRX, aRY, aRZ, Standard_True, theUniversalPair_InputSkewAngle, Standard_True, aLowerLimitActualRotationX, Standard_True,
+          aUpperLimitActualRotationX, Standard_True, aLowerLimitActualRotationZ, Standard_True, aUpperLimitActualRotationZ);
+      }
+      else
+      {
+        theKinematicPair = new StepKinematics_UniversalPair;
+        Handle(StepKinematics_UniversalPair) anUniversalPair = Handle(StepKinematics_UniversalPair)::DownCast(theKinematicPair);
+        anUniversalPair->Init(aPairName, aPairName, hasDescription,
+          aDescription, aTransformItem1, aTransformItem2, theJoint,
+          aTX, aTY, aTZ, aRX, aRY, aRZ, Standard_True, theUniversalPair_InputSkewAngle);
+      }
+      break;
+    }
+    case(XCAFKinematics_PairType_SphericalWithPin):
+    {
+      aTX = Standard_False;
+      aTY = Standard_False;
+      aTZ = Standard_False;
+      aRX = Standard_False;
+      aRY = Standard_False;
+      if (isRanged)
+      {
+        Standard_Real aUpperLimitYaw = aLowOrderPairObj->MaxRotationZ();
+        Standard_Real aLowerLimitYaw = aLowOrderPairObj->MinRotationZ();
+        Standard_Real aUpperLimitRoll = aLowOrderPairObj->MaxRotationX();
+        Standard_Real aLowerLimitRoll = aLowOrderPairObj->MinRotationX();
+        theKinematicPair = new StepKinematics_SphericalPairWithPinAndRange;
+        Handle(StepKinematics_SphericalPairWithPinAndRange) aSphericalPairWithPinAndRange = Handle(StepKinematics_SphericalPairWithPinAndRange)::DownCast(theKinematicPair);
+        aSphericalPairWithPinAndRange->Init(aPairName, aPairName, hasDescription,
+          aDescription, aTransformItem1, aTransformItem2, theJoint,
+          aTX, aTY, aTZ, aRX, aRY, aRZ, Standard_True, aLowerLimitYaw, Standard_True,
+          aUpperLimitYaw, Standard_True, aLowerLimitRoll, Standard_True, aUpperLimitRoll);
+      }
+      else
+      {
+        theKinematicPair = new StepKinematics_SphericalPairWithPin;
+        Handle(StepKinematics_SphericalPairWithPin) aSphericalPairWithPin = Handle(StepKinematics_SphericalPairWithPin)::DownCast(theKinematicPair);
+        aSphericalPairWithPin->Init(aPairName, aPairName, hasDescription,
+          aDescription, aTransformItem1, aTransformItem2, theJoint,
+          aTX, aTY, aTZ, aRX, aRY, aRZ);
       }
+      break;
     }
-    else {
-      if (isWithDatRef) {
-        // Geometric_Tolerance & Geometric_Tolerance_With_Datum_Reference & Geometric_Tolerance_With_Modifiers
-        Handle(StepDimTol_GeoTolAndGeoTolWthDatRefAndGeoTolWthMod) aResult =
-          new StepDimTol_GeoTolAndGeoTolWthDatRefAndGeoTolWthMod();
-        aResult->Init(aName, aDescription, aLMWU, aGTTarget, aGTWDR, aGTWM, aType);
-        aGeomTol = aResult;
+    case(XCAFKinematics_PairType_Spherical):
+    {
+      aTX = Standard_False;
+      aTY = Standard_False;
+      aTZ = Standard_False;
+      if (isRanged)
+      {
+        Standard_Real aUpperLimitYaw = aLowOrderPairObj->MaxRotationZ();
+        Standard_Real aLowerLimitYaw = aLowOrderPairObj->MinRotationZ();
+        Standard_Real aUpperLimitRoll = aLowOrderPairObj->MaxRotationX();
+        Standard_Real aLowerLimitRoll = aLowOrderPairObj->MinRotationX();
+        Standard_Real aUpperLimitPitch = aLowOrderPairObj->MaxRotationY();
+        Standard_Real aLowerLimitPitch= aLowOrderPairObj->MinRotationY();
+        theKinematicPair = new StepKinematics_SphericalPairWithRange;
+        Handle(StepKinematics_SphericalPairWithRange) aSphericalPairWithRange = Handle(StepKinematics_SphericalPairWithRange)::DownCast(theKinematicPair);
+        aSphericalPairWithRange->Init(aPairName, aPairName, hasDescription,
+          aDescription, aTransformItem1, aTransformItem2, theJoint,
+          aTX, aTY, aTZ, aRX, aRY, aRZ, Standard_True, aLowerLimitYaw, Standard_True,
+          aUpperLimitYaw, Standard_True, aLowerLimitRoll, Standard_True, aUpperLimitRoll, Standard_True, aLowerLimitPitch, Standard_True, aUpperLimitPitch);
       }
-      else {
-        // Geometric_Tolerance & Geometric_Tolerance_With_Modifiers
-        Handle(StepDimTol_GeoTolAndGeoTolWthMod) aResult =
-          new StepDimTol_GeoTolAndGeoTolWthMod();
-        aResult->Init(aName, aDescription, aLMWU, aGTTarget, aGTWM, aType);
-        aGeomTol = aResult;
+      else
+      {
+        theKinematicPair = new StepKinematics_SphericalPair;
+        Handle(StepKinematics_SphericalPair) aSphericalPair = Handle(StepKinematics_SphericalPair)::DownCast(theKinematicPair);
+        aSphericalPair->Init(aPairName, aPairName, hasDescription,
+          aDescription, aTransformItem1, aTransformItem2, theJoint,
+          aTX, aTY, aTZ, aRX, aRY, aRZ);
       }
+      break;
     }
-  }
-  else {
-    if (isWithDatRef) {
-      // Geometric_Tolerance & Geometric_Tolerance_With_Datum_Reference
-      Handle(StepDimTol_GeoTolAndGeoTolWthDatRef) aResult =
-          new StepDimTol_GeoTolAndGeoTolWthDatRef();
-        aResult->Init(aName, aDescription, aLMWU, aGTTarget, aGTWDR, aType);
-        aGeomTol = aResult;
-    }
-    else {
-      // Geometric_Tolerance
-      Handle(StepDimTol_GeometricTolerance) aResult = 
-        STEPCAFControl_GDTProperty::GetGeomTolerance(anObject->GetType());
-      if (!aResult.IsNull()) {
-        aResult->Init(aName, aDescription, aLMWU, aGTTarget);
-        aGeomTol = aResult;
+    case(XCAFKinematics_PairType_Planar):
+    {
+      aTZ = Standard_False;
+      aRX = Standard_False;
+      aRY = Standard_False;
+      if (isRanged)
+      {
+        theKinematicPair = new StepKinematics_PlanarPairWithRange;
+        Handle(StepKinematics_PlanarPairWithRange) aPlanarPairWithRange = Handle(StepKinematics_PlanarPairWithRange)::DownCast(theKinematicPair);
+        Standard_Real aUpperLimitActualTranslationX = aLowOrderPairObj->MaxTranslationX();
+        Standard_Real aLowerLimitActualTranslationX = aLowOrderPairObj->MinTranslationX();
+        Standard_Real aUpperLimitActualTranslationY = aLowOrderPairObj->MaxTranslationY();
+        Standard_Real aLowerLimitActualTranslationY = aLowOrderPairObj->MinTranslationY();
+        Standard_Real aLowerLimitActualRotation = aLowOrderPairObj->MinRotationZ();
+        Standard_Real aUpperLimitActualRotation = aLowOrderPairObj->MaxRotationZ();
+        aPlanarPairWithRange->Init(aPairName, aPairName, hasDescription,
+          aDescription, aTransformItem1, aTransformItem2, theJoint,
+          aTX, aTY, aTZ, aRX, aRY, aRZ, Standard_True, aLowerLimitActualTranslationX, Standard_True, aUpperLimitActualTranslationX, 
+          Standard_True, aLowerLimitActualTranslationY, Standard_True, aUpperLimitActualTranslationY,
+          Standard_True, aLowerLimitActualRotation, Standard_True, aUpperLimitActualRotation);
+      }
+      else
+      {
+        theKinematicPair = new StepKinematics_PlanarPair;
+        Handle(StepKinematics_PlanarPair) aPlanarPair = Handle(StepKinematics_PlanarPair)::DownCast(theKinematicPair);
+        aPlanarPair->Init(aPairName, aPairName, hasDescription,
+          aDescription, aTransformItem1, aTransformItem2, theJoint,
+          aTX, aTY, aTZ, aRX, aRY, aRZ);
       }
+      break;
     }
-  }
-  Model->AddWithRefs(aGeomTol);
-  WriteToleranceZone(WS, anObject, aGeomTol, theRC);
-  //Annotation plane and Presentation
-  WritePresentation(WS, anObject->GetPresentation(), anObject->GetPresentationName(), Standard_True, anObject->HasPlane(),
-    anObject->GetPlane(), anObject->GetPointTextAttach(), aGeomTol);
-}
-
-//=======================================================================
-//function : WriteDGTs
-//purpose  : 
-//=======================================================================
-Standard_Boolean STEPCAFControl_Writer::WriteDGTs (const Handle(XSControl_WorkSession) &WS,
-                                                   const TDF_LabelSequence  &labels ) const
-{
-  
-  if ( labels.Length() <=0 ) return Standard_False;
-  
-  // get working data
-  const Handle(Interface_InterfaceModel) &Model = WS->Model();
-  const Handle(XSControl_TransferWriter) &TW = WS->TransferWriter();
-  const Handle(Transfer_FinderProcess) &FP = TW->FinderProcess();
-
-  const Handle(Interface_HGraph) aHGraph = WS->HGraph();
-  if(aHGraph.IsNull())
-    return Standard_False;
-
-  Interface_Graph aGraph = aHGraph->Graph();
-  Handle(XCAFDoc_DimTolTool) DGTTool = XCAFDoc_DocumentTool::DimTolTool( labels(1) );
-  if(DGTTool.IsNull() ) return Standard_False;
-
-  TDF_LabelSequence DGTLabels;
-
-  STEPConstruct_DataMapOfAsciiStringTransient DatumMap;
+    case(XCAFKinematics_PairType_Unconstrained):
+    {
+      theKinematicPair = new StepKinematics_LowOrderKinematicPairWithRange;
 
-  // write Datums
-  DGTLabels.Clear();
-  DGTTool->GetDatumLabels(DGTLabels);
-  if(DGTLabels.Length()<=0) return Standard_False;
-  Standard_Integer i;
-  for(i=1; i<=DGTLabels.Length(); i++) {
-    TDF_Label DatumL = DGTLabels.Value(i);
-    TDF_LabelSequence ShapeL;
-    TDF_LabelSequence aNullSeq;
-    if(!DGTTool->GetRefShapeLabel(DatumL,ShapeL,aNullSeq)) continue;
-    // find target shape
-    TopoDS_Shape aShape = XCAFDoc_ShapeTool::GetShape(ShapeL.Value(1));
-    TopLoc_Location Loc;
-    TColStd_SequenceOfTransient seqRI;
-    FindEntities( FP, aShape, Loc, seqRI );
-    if ( seqRI.Length() <= 0 ) {
-      FP->Messenger()->SendInfo() << "Warning: Cannot find RI for "<<aShape.TShape()->DynamicType()->Name()<<std::endl;
-      continue;
+      Standard_Real aUpperLimitActualTranslationX = aLowOrderPairObj->MaxTranslationX();
+      Standard_Real aLowerLimitActualTranslationX = aLowOrderPairObj->MinTranslationX();
+      Standard_Real aUpperLimitActualTranslationY = aLowOrderPairObj->MaxTranslationY();
+      Standard_Real aLowerLimitActualTranslationY = aLowOrderPairObj->MinTranslationY();
+      Standard_Real aUpperLimitActualTranslationZ = aLowOrderPairObj->MaxTranslationZ();
+      Standard_Real aLowerLimitActualTranslationZ = aLowOrderPairObj->MinTranslationZ();
+      Standard_Real aLowerLimitActualRotationX = aLowOrderPairObj->MinRotationX();
+      Standard_Real aUpperLimitActualRotationX = aLowOrderPairObj->MaxRotationX();
+      Standard_Real aLowerLimitActualRotationY = aLowOrderPairObj->MinRotationY();
+      Standard_Real aUpperLimitActualRotationY = aLowOrderPairObj->MaxRotationY();
+      Standard_Real aLowerLimitActualRotationZ = aLowOrderPairObj->MinRotationZ();
+      Standard_Real aUpperLimitActualRotationZ = aLowOrderPairObj->MaxRotationZ();
+      Handle(StepKinematics_LowOrderKinematicPairWithRange) anUnconstrainedPairWithRange = Handle(StepKinematics_LowOrderKinematicPairWithRange)::DownCast(theKinematicPair);
+      anUnconstrainedPairWithRange->Init(aPairName, aPairName, hasDescription,
+        aDescription, aTransformItem1, aTransformItem2, theJoint,
+        aTX, aTY, aTZ, aRX, aRY, aRZ, Standard_True, aLowerLimitActualRotationX, Standard_True, aUpperLimitActualRotationX,
+        Standard_True, aLowerLimitActualRotationY, Standard_True, aUpperLimitActualRotationY,
+        Standard_True, aLowerLimitActualRotationZ, Standard_True, aUpperLimitActualRotationZ,
+        Standard_True, aLowerLimitActualTranslationX, Standard_True, aUpperLimitActualTranslationX,
+        Standard_True, aLowerLimitActualTranslationY, Standard_True, aUpperLimitActualTranslationY,
+        Standard_True, aLowerLimitActualTranslationZ, Standard_True, aUpperLimitActualTranslationZ);
+      break;
+    }
+    case(XCAFKinematics_PairType_FullyConstrained):
+    {
+      theKinematicPair = new StepKinematics_FullyConstrainedPair;
+      Handle(StepKinematics_FullyConstrainedPair) aFullyConstrainedPair = Handle(StepKinematics_FullyConstrainedPair)::DownCast(theKinematicPair);
+      aFullyConstrainedPair->Init(aPairName, aPairName, hasDescription,
+        aDescription, aTransformItem1, aTransformItem2, theJoint,
+        aTX, aTY, aTZ, aRX, aRY, aRZ);
+      break;
+    }
+    case(XCAFKinematics_PairType_Homokinetic):
+    {
+      aTX = Standard_False;
+      aTY = Standard_False;
+      aTZ = Standard_False;
+      aRY = Standard_False;
+      Standard_Real InputSkewAngle = aLowOrderPairObj->SkewAngle();
+      theKinematicPair = new StepKinematics_HomokineticPair;
+      Handle(StepKinematics_HomokineticPair) aHomokineticPair = Handle(StepKinematics_HomokineticPair)::DownCast(theKinematicPair);
+      aHomokineticPair->Init(aPairName, aPairName, hasDescription,
+        aDescription, aTransformItem1, aTransformItem2, theJoint,
+        aTX, aTY, aTZ, aRX, aRY, aRZ, Standard_True, InputSkewAngle);
+      break;
+    }
+    default:
+      return Standard_False;
     }
-    Handle(StepRepr_ProductDefinitionShape) PDS;
-    Handle(StepRepr_RepresentationContext) RC;
-    Handle(Standard_Transient) ent = seqRI.Value(1);
-    Handle(StepShape_AdvancedFace) AF;
-    Handle(StepShape_EdgeCurve) EC;
-    FindPDSforDGT(aGraph,ent,PDS,RC,AF,EC);
-    if(PDS.IsNull()) continue;
-    //std::cout<<"Model->Number(PDS)="<<Model->Number(PDS)<<std::endl;
-    Handle(XCAFDoc_Datum) DatumAttr;
-    if(!DatumL.FindAttribute(XCAFDoc_Datum::GetID(),DatumAttr)) continue;
-    Handle(TCollection_HAsciiString) aName = DatumAttr->GetName();
-    Handle(TCollection_HAsciiString) aDescription = DatumAttr->GetDescription();
-    Handle(TCollection_HAsciiString) anIdentification = DatumAttr->GetIdentification();
-    Handle(StepDimTol_DatumFeature) DF = new StepDimTol_DatumFeature;
-    Handle(StepDimTol_Datum) aDatum = new StepDimTol_Datum;
-    DF->Init(aName, new TCollection_HAsciiString, PDS, StepData_LTrue);
-    Model->AddWithRefs(DF);
-    aDatum->Init(aName, new TCollection_HAsciiString, PDS, StepData_LFalse, anIdentification);
-    Model->AddWithRefs(aDatum);
-    Handle(StepRepr_ShapeAspectRelationship) SAR = new StepRepr_ShapeAspectRelationship;
-    SAR->SetName(aName);
-    SAR->SetRelatingShapeAspect(DF);
-    SAR->SetRelatedShapeAspect(aDatum);
-    Model->AddWithRefs(SAR);
-    // write chain for DatumFeature
-    StepRepr_CharacterizedDefinition CD;
-    CD.SetValue(DF);
-    Handle(StepRepr_PropertyDefinition) PropD = new StepRepr_PropertyDefinition;
-    PropD->Init(aName,Standard_True,aDescription,CD);
-    Model->AddWithRefs(PropD);
-    StepRepr_RepresentedDefinition RD;
-    RD.SetValue(PropD);
-    Handle(StepShape_ShapeRepresentation) SR = new StepShape_ShapeRepresentation;
-    Handle(StepRepr_HArray1OfRepresentationItem) HARI =
-      new StepRepr_HArray1OfRepresentationItem(1,1);
-    HARI->SetValue(1,AF);
-    SR->Init(aName,HARI,RC);
-    Handle(StepShape_ShapeDefinitionRepresentation) SDR = new StepShape_ShapeDefinitionRepresentation;
-    SDR->Init(RD,SR);
-    Model->AddWithRefs(SDR);
-    // write chain for Datum 
-    StepRepr_CharacterizedDefinition CD1;
-    CD1.SetValue(aDatum);
-    Handle(StepRepr_PropertyDefinition) PropD1 = new StepRepr_PropertyDefinition;
-    PropD1->Init(aName,Standard_True,aDescription,CD1);
-    Model->AddWithRefs(PropD1);
-    StepRepr_RepresentedDefinition RD1;
-    RD1.SetValue(PropD1);
-    Handle(StepShape_ShapeRepresentation) SR1 = new StepShape_ShapeRepresentation;
-    Handle(StepRepr_HArray1OfRepresentationItem) HARI1 =
-      new StepRepr_HArray1OfRepresentationItem(1,1);
-    HARI1->SetValue(1,AF->FaceGeometry());
-    SR1->Init(aName,HARI1,RC);
-    Model->AddWithRefs(SR1);
-    Handle(StepShape_ShapeDefinitionRepresentation) SDR1 = new StepShape_ShapeDefinitionRepresentation;
-    SDR1->Init(RD1,SR1);
-    Model->AddWithRefs(SDR1);
-    // add created Datum into Map
-    TCollection_AsciiString stmp(aName->ToCString());
-    stmp.AssignCat(aDescription->ToCString());
-    stmp.AssignCat(anIdentification->ToCString());
-    DatumMap.Bind(stmp,aDatum);
   }
-
-  // write Tolerances and Dimensions
-  DGTLabels.Clear();
-  DGTTool->GetDimTolLabels(DGTLabels);
-  if(DGTLabels.Length()<=0) return Standard_False;
-  for(i=1; i<=DGTLabels.Length(); i++) {
-    TDF_Label DimTolL = DGTLabels.Value(i);
-    TDF_LabelSequence ShapeL;
-    TDF_LabelSequence aNullSeq;
-    if(!DGTTool->GetRefShapeLabel(DimTolL,ShapeL,aNullSeq)) continue;
-    // find target shape
-    TopoDS_Shape aShape = XCAFDoc_ShapeTool::GetShape(ShapeL.Value(1));
-    TopLoc_Location Loc;
-    TColStd_SequenceOfTransient seqRI;
-    FindEntities( FP, aShape, Loc, seqRI );
-    if ( seqRI.Length() <= 0 ) {
-      FP->Messenger()->SendInfo() << "Warning: Cannot find RI for "<<aShape.TShape()->DynamicType()->Name()<<std::endl;
-      continue;
+  else if (theKinPairObj->IsKind(STANDARD_TYPE(XCAFKinematics_LowOrderPairObjectWithCoupling)))
+  {
+  Handle(XCAFKinematics_LowOrderPairObjectWithCoupling) aLowOrderPairObjectWithCoupling = Handle(XCAFKinematics_LowOrderPairObjectWithCoupling)::DownCast(theKinPairObj);
+    switch (theKinPairObj->Type())
+    {
+    case(XCAFKinematics_PairType_Screw):
+    {
+      Standard_Real aPitch = aLowOrderPairObjectWithCoupling->Pitch();
+      if (isRanged)
+      {
+        theKinematicPair = new StepKinematics_ScrewPairWithRange;
+        Handle(StepKinematics_ScrewPairWithRange) aScrewPairWithRange = Handle(StepKinematics_ScrewPairWithRange)::DownCast(theKinematicPair);
+        Standard_Real aLowLimit = aLowOrderPairObjectWithCoupling->LowLimit();
+        Standard_Real aUpperLimit = aLowOrderPairObjectWithCoupling->UpperLimit();
+        aScrewPairWithRange->Init(aPairName, aPairName, hasDescription,
+          aDescription, aTransformItem1, aTransformItem2, theJoint,aPitch,
+          Standard_True, aLowLimit, Standard_True, aUpperLimit);
+      }
+      else
+      {
+        theKinematicPair = new StepKinematics_ScrewPair;
+        Handle(StepKinematics_ScrewPair) aScrewPair = Handle(StepKinematics_ScrewPair)::DownCast(theKinematicPair);
+        aScrewPair->Init(aPairName, aPairName, hasDescription,
+          aDescription, aTransformItem1, aTransformItem2, theJoint,aPitch);
+      }
+      break;
     }
-    Handle(StepRepr_ProductDefinitionShape) PDS;
-    Handle(StepRepr_RepresentationContext) RC;
-    Handle(Standard_Transient) ent = seqRI.Value(1);
-    Handle(StepShape_AdvancedFace) AF;
-    Handle(StepShape_EdgeCurve) EC;
-    FindPDSforDGT(aGraph,ent,PDS,RC,AF,EC);
-    if(PDS.IsNull()) continue;
-    //std::cout<<"Model->Number(PDS)="<<Model->Number(PDS)<<std::endl;
-
-    Handle(XCAFDoc_DimTol) DimTolAttr;
-    if(!DimTolL.FindAttribute(XCAFDoc_DimTol::GetID(),DimTolAttr)) continue;
-    Standard_Integer kind = DimTolAttr->GetKind();
-    Handle(TColStd_HArray1OfReal) aVal = DimTolAttr->GetVal();
-    Handle(TCollection_HAsciiString) aName = DimTolAttr->GetName();
-    Handle(TCollection_HAsciiString) aDescription = DimTolAttr->GetDescription();
-
-    // common part of writing D&GT entities
-    StepRepr_CharacterizedDefinition CD;
-    Handle(StepRepr_ShapeAspect) SA = new StepRepr_ShapeAspect;
-    SA->Init(aName, new TCollection_HAsciiString, PDS, StepData_LTrue);
-    Model->AddWithRefs(SA);
-    CD.SetValue(SA);
-    Handle(StepRepr_PropertyDefinition) PropD = new StepRepr_PropertyDefinition;
-    PropD->Init(aName,Standard_True,aDescription,CD);
-    Model->AddWithRefs(PropD);
-    StepRepr_RepresentedDefinition RD;
-    RD.SetValue(PropD);
-    Handle(StepShape_ShapeRepresentation) SR = new StepShape_ShapeRepresentation;
-    Handle(StepRepr_HArray1OfRepresentationItem) HARI =
-      new StepRepr_HArray1OfRepresentationItem(1,1);
-    if(kind<20) 
-      HARI->SetValue(1,EC);
-    else
-      HARI->SetValue(1,AF);
-    SR->Init(aName,HARI,RC);
-    Handle(StepShape_ShapeDefinitionRepresentation) SDR = new StepShape_ShapeDefinitionRepresentation;
-    SDR->Init(RD,SR);
-    Model->AddWithRefs(SDR);
-    // define aUnit for creation LengthMeasureWithUnit (common for all)
-    StepBasic_Unit aUnit;
-    aUnit = GetUnit(RC);
-
-    // specific part of writing D&GT entities
-    if(kind<20) { //dimension
-      Handle(StepShape_DimensionalSize) DimSize = new StepShape_DimensionalSize;
-      DimSize->Init(SA,aDescription);
-      Model->AddWithRefs(DimSize);
-      if(aVal->Length()>1) {
-        // create MeasureWithUnits
-        Handle(StepBasic_MeasureValueMember) MVM1 = new StepBasic_MeasureValueMember;
-        MVM1->SetName("POSITIVE_LENGTH_MEASURE");
-        MVM1->SetReal(aVal->Value(1));
-        Handle(StepBasic_MeasureWithUnit) MWU1 = new StepBasic_MeasureWithUnit;
-        MWU1->Init(MVM1,aUnit);
-        Handle(StepBasic_MeasureValueMember) MVM2 = new StepBasic_MeasureValueMember;
-        MVM2->SetName("POSITIVE_LENGTH_MEASURE");
-        MVM2->SetReal(aVal->Value(2));
-        Handle(StepBasic_MeasureWithUnit) MWU2 = new StepBasic_MeasureWithUnit;
-        MWU2->Init(MVM2,aUnit);
-        Handle(StepRepr_RepresentationItem) RI1 = new StepRepr_RepresentationItem;
-        RI1->Init(new TCollection_HAsciiString("lower limit"));
-        Handle(StepRepr_RepresentationItem) RI2 = new StepRepr_RepresentationItem;
-        RI2->Init(new TCollection_HAsciiString("upper limit"));
-        Handle(StepRepr_ReprItemAndLengthMeasureWithUnit) RILMU1 =
-          new StepRepr_ReprItemAndLengthMeasureWithUnit;
-        RILMU1->Init(MWU1,RI1);
-        Handle(StepRepr_ReprItemAndLengthMeasureWithUnit) RILMU2 =
-          new StepRepr_ReprItemAndLengthMeasureWithUnit;
-        RILMU2->Init(MWU2,RI2);
-        Model->AddWithRefs(RILMU1);
-        Model->AddWithRefs(RILMU2);
-        //Handle(StepRepr_CompoundItemDefinitionMember) CIDM =
-        //  new StepRepr_CompoundItemDefinitionMember;
-        //Handle(TColStd_HArray1OfTransient) ArrTr = new TColStd_HArray1OfTransient(1,2);
-        //ArrTr->SetValue(1,RILMU1);
-        //ArrTr->SetValue(2,RILMU2);
-        //CIDM->SetArrTransient(ArrTr);
-        //CIDM->SetName("SET_REPRESENTATION_ITEM");
-        //StepRepr_CompoundItemDefinition CID;
-        //CID.SetValue(CIDM);
-        Handle(StepRepr_HArray1OfRepresentationItem) HARIVR =
-          new StepRepr_HArray1OfRepresentationItem(1,2);
-        HARIVR->SetValue(1,RILMU1);
-        HARIVR->SetValue(2,RILMU2);
-        Handle(StepRepr_ValueRange) VR = new StepRepr_ValueRange;
-        //VR->Init(aName,CID);
-        VR->Init(aName,HARIVR);
-        Model->AddEntity(VR);
-        Handle(StepShape_ShapeDimensionRepresentation) SDimR =
-          new StepShape_ShapeDimensionRepresentation;
-        Handle(StepRepr_HArray1OfRepresentationItem) aHARI =
-          new StepRepr_HArray1OfRepresentationItem(1,1);
-        aHARI->SetValue(1,VR);
-        SDimR->Init(aName,aHARI,RC);
-        Model->AddWithRefs(SDimR);
-        Handle(StepShape_DimensionalCharacteristicRepresentation) DimCharR =
-          new StepShape_DimensionalCharacteristicRepresentation;
-        StepShape_DimensionalCharacteristic DimChar;
-        DimChar.SetValue(DimSize);
-        DimCharR->Init(DimChar,SDimR);
-        Model->AddEntity(DimCharR);
+    case(XCAFKinematics_PairType_RackAndPinion):
+    {
+      Standard_Real aPinionRadius = aLowOrderPairObjectWithCoupling->PinionRadius();
+      if (isRanged)
+      {
+        theKinematicPair = new StepKinematics_RackAndPinionPairWithRange;
+        Standard_Real aLowLimit = aLowOrderPairObjectWithCoupling->LowLimit();
+        Standard_Real aUpperLimit = aLowOrderPairObjectWithCoupling->UpperLimit();
+        Handle(StepKinematics_RackAndPinionPairWithRange) aRackAndPinionPairWithRange = Handle(StepKinematics_RackAndPinionPairWithRange)::DownCast(theKinematicPair);
+        aRackAndPinionPairWithRange->Init(aPairName, aPairName, hasDescription,
+          aDescription, aTransformItem1, aTransformItem2, theJoint, aPinionRadius,
+          Standard_True, aLowLimit, Standard_True, aUpperLimit);
+      }
+      else
+      { 
+        theKinematicPair = new StepKinematics_RackAndPinionPair;
+        Handle(StepKinematics_RackAndPinionPair) aRackAndPinionPair = Handle(StepKinematics_RackAndPinionPair)::DownCast(theKinematicPair);
+        aRackAndPinionPair->Init(aPairName, aPairName, hasDescription,
+          aDescription, aTransformItem1, aTransformItem2, theJoint, aPinionRadius);
+      }
+      break;
+    }
+    case(XCAFKinematics_PairType_Gear):
+    {
+      Standard_Real aBevel = aLowOrderPairObjectWithCoupling->Bevel();
+      Standard_Real aGearRatio = aLowOrderPairObjectWithCoupling->GearRatio();
+      Standard_Real aHelicalAngle = aLowOrderPairObjectWithCoupling->HelicalAngle();
+      Standard_Real aRadiusFirstLink = aLowOrderPairObjectWithCoupling->RadiusFirstLink();;
+      Standard_Real aRadiusSecondLink = aLowOrderPairObjectWithCoupling->RadiusSecondLink();;
+      if (isRanged)
+      {
+        Standard_Real aLowLimit = aLowOrderPairObjectWithCoupling->LowLimit();
+        Standard_Real aUpperLimit = aLowOrderPairObjectWithCoupling->UpperLimit();
+        theKinematicPair = new StepKinematics_GearPairWithRange;
+        Handle(StepKinematics_GearPairWithRange) aGearPairWithRange = Handle(StepKinematics_GearPairWithRange)::DownCast(theKinematicPair);
+        aGearPairWithRange->Init(aPairName, aPairName, hasDescription,
+          aDescription, aTransformItem1, aTransformItem2, theJoint, aRadiusFirstLink, aRadiusSecondLink, aBevel, aHelicalAngle, aGearRatio,
+          Standard_True, aLowLimit, Standard_True, aUpperLimit);
+      }
+      else
+      {
+        theKinematicPair = new StepKinematics_GearPair;
+        Handle(StepKinematics_GearPair) aGearPair = Handle(StepKinematics_GearPair)::DownCast(theKinematicPair);
+        aGearPair->Init(aPairName, aPairName, hasDescription,
+          aDescription, aTransformItem1, aTransformItem2, theJoint,aRadiusFirstLink,aRadiusSecondLink,aBevel,aHelicalAngle, aGearRatio);
+      }
+      break;
+    }
+    case(XCAFKinematics_PairType_LinearFlexibleAndPinion):
+    {
+      Standard_Real aPinionRadius = aLowOrderPairObjectWithCoupling->PinionRadius();
+      theKinematicPair = new StepKinematics_LinearFlexibleAndPinionPair;
+      Handle(StepKinematics_LinearFlexibleAndPinionPair) aLinearFlexibleAndPinionPair = Handle(StepKinematics_LinearFlexibleAndPinionPair)::DownCast(theKinematicPair);
+      aLinearFlexibleAndPinionPair->Init(aPairName, aPairName, hasDescription,
+        aDescription, aTransformItem1, aTransformItem2, theJoint, aPinionRadius);
+      break;
+    }
+    default:
+      return Standard_False;
+    }
+  }
+  else if (theKinPairObj->IsKind(STANDARD_TYPE(XCAFKinematics_HighOrderPairObject)))
+  {
+  Handle(XCAFKinematics_HighOrderPairObject) aHighOrderPairObject = Handle(XCAFKinematics_HighOrderPairObject)::DownCast(theKinPairObj);
+    switch (theKinPairObj->Type())
+    {
+    case(XCAFKinematics_PairType_PointOnSurface):
+    {
+      GeomToStep_MakeSurface aMaker(aHighOrderPairObject->Surface());
+      Handle(StepGeom_Surface) aPairSurface = aMaker.Value();
+      if (isRanged && !aPairSurface.IsNull() &&
+        aPairSurface->IsKind(STANDARD_TYPE(StepGeom_RectangularTrimmedSurface)))
+      {
+        Standard_Real aLowerLimitYaw = aHighOrderPairObject->LowLimitYaw();
+        Standard_Real aUpperLimitYaw = aHighOrderPairObject->UpperLimitYaw();
+        Standard_Real aLowerLimitPitch = aHighOrderPairObject->LowLimitPitch();
+        Standard_Real aUpperLimitPitch = aHighOrderPairObject->UpperLimitPitch();
+        Standard_Real aLowerLimitRoll = aHighOrderPairObject->LowLimitRoll();
+        Standard_Real aUpperLimitRoll = aHighOrderPairObject->UpperLimitRoll();
+        GeomToStep_MakeSurface aMakerSurface(aHighOrderPairObject->Surface());
+        Handle(StepGeom_RectangularTrimmedSurface) aRangeOnPairSurface = Handle(StepGeom_RectangularTrimmedSurface)::DownCast(aMakerSurface.Value());
+        theKinematicPair = new StepKinematics_PointOnSurfacePairWithRange;
+        Handle(StepKinematics_PointOnSurfacePairWithRange) aPointOnSurfacePairWithRange = Handle(StepKinematics_PointOnSurfacePairWithRange)::DownCast(theKinematicPair);
+        aPointOnSurfacePairWithRange->Init(aPairName, aPairName, hasDescription,
+          aDescription, aTransformItem1, aTransformItem2, theJoint, aPairSurface,aRangeOnPairSurface, Standard_True, aLowerLimitYaw, Standard_True, aUpperLimitYaw, Standard_True,
+          aLowerLimitPitch, Standard_True, aUpperLimitPitch, Standard_True, aLowerLimitRoll, Standard_True, aUpperLimitRoll);
+      }
+      else 
+      {
+        theKinematicPair = new StepKinematics_PointOnSurfacePair;
+        Handle(StepKinematics_PointOnSurfacePair) aPointOnSurface = Handle(StepKinematics_PointOnSurfacePair)::DownCast(theKinematicPair);
+        aPointOnSurface->Init(aPairName, aPairName, hasDescription,
+          aDescription, aTransformItem1, aTransformItem2, theJoint, aPairSurface);
       }
+      break;
+    }
+    case(XCAFKinematics_PairType_SlidingSurface):
+    {
+      theKinematicPair = new StepKinematics_SlidingSurfacePair;
+      GeomToStep_MakeSurface aMaker(aHighOrderPairObject->FirstSurface());
+      Handle(StepGeom_Surface) aPairSurfac1 = aMaker.Value();
+      GeomToStep_MakeSurface aMaker2(aHighOrderPairObject->SecondSurface());
+      Handle(StepGeom_Surface) aPairSurface2 = aMaker2.Value();
+      Standard_Boolean anOrientation = aHighOrderPairObject->Orientation();
+      Handle(StepKinematics_SlidingSurfacePair) aSlidingSurfacePair = Handle(StepKinematics_SlidingSurfacePair)::DownCast(theKinematicPair);
+      aSlidingSurfacePair->Init(aPairName, aPairName, hasDescription,
+        aDescription, aTransformItem1, aTransformItem2, theJoint, aPairSurfac1, aPairSurface2, anOrientation);
+      break;
     }
-    else if(kind<50) { //tolerance
-      if(kind<35) { // tolerance with datum system
-        TDF_LabelSequence DatumLabels;
-        DGTTool->GetDatumOfTolerLabels(DimTolL,DatumLabels);
-        Standard_Integer NbDR = DatumLabels.Length();
-        Handle(StepDimTol_HArray1OfDatumReference) HADR = new StepDimTol_HArray1OfDatumReference(1,NbDR);
-        for(Standard_Integer j=1; j<=NbDR; j++) {
-          Handle(XCAFDoc_Datum) DatumAttr;
-          TDF_Label DatumL = DatumLabels.Value(j);
-          if(!DatumL.FindAttribute(XCAFDoc_Datum::GetID(),DatumAttr)) continue;
-          Handle(TCollection_HAsciiString) aNameD = DatumAttr->GetName();
-          Handle(TCollection_HAsciiString) aDescriptionD = DatumAttr->GetDescription();
-          Handle(TCollection_HAsciiString) anIdentificationD = DatumAttr->GetIdentification();
-          TCollection_AsciiString stmp(aNameD->ToCString());
-          stmp.AssignCat(aDescriptionD->ToCString());
-          stmp.AssignCat(anIdentificationD->ToCString());
-          if(DatumMap.IsBound(stmp)) {
-            Handle(StepDimTol_Datum) aDatum = 
-              Handle(StepDimTol_Datum)::DownCast(DatumMap.Find(stmp));
-            Handle(StepDimTol_DatumReference) DR = new StepDimTol_DatumReference;
-            DR->Init(j,aDatum);
-            Model->AddWithRefs(DR);
-            HADR->SetValue(j,DR);
-          }
-        }
-        // create LengthMeasureWithUnit
-        Handle(StepBasic_MeasureValueMember) MVM = new StepBasic_MeasureValueMember;
-        MVM->SetName("LENGTH_MEASURE");
-        MVM->SetReal(aVal->Value(1));
-        Handle(StepBasic_LengthMeasureWithUnit) LMWU = new StepBasic_LengthMeasureWithUnit;
-        LMWU->Init(MVM,aUnit);
-        // create tolerance by it's type
-        if(kind<24) {
-          Handle(StepDimTol_GeometricToleranceWithDatumReference) GTWDR =
-            new StepDimTol_GeometricToleranceWithDatumReference;
-          GTWDR->SetDatumSystem(HADR);
-          Handle(StepDimTol_ModifiedGeometricTolerance) MGT =
-            new StepDimTol_ModifiedGeometricTolerance;
-          if(kind==21) MGT->SetModifier(StepDimTol_MaximumMaterialCondition);
-          else if(kind==22) MGT->SetModifier(StepDimTol_LeastMaterialCondition);
-          else if(kind==23) MGT->SetModifier(StepDimTol_RegardlessOfFeatureSize);
-          Handle(StepDimTol_GeoTolAndGeoTolWthDatRefAndModGeoTolAndPosTol) GTComplex =
-            new StepDimTol_GeoTolAndGeoTolWthDatRefAndModGeoTolAndPosTol;
-          GTComplex->Init(aName,aDescription,LMWU,SA,GTWDR,MGT);
-          Model->AddWithRefs(GTComplex);
-        }
-        else if(kind==24) {
-          Handle(StepDimTol_AngularityTolerance) aToler =
-            new StepDimTol_AngularityTolerance;
-          aToler->Init(aName,aDescription,LMWU,SA,HADR);
-          Model->AddWithRefs(aToler);
-        }
-        else if(kind==25) {
-          Handle(StepDimTol_CircularRunoutTolerance) aToler =
-            new StepDimTol_CircularRunoutTolerance;
-          aToler->Init(aName,aDescription,LMWU,SA,HADR);
-          Model->AddWithRefs(aToler);
-        }
-        else if(kind==26) {
-          Handle(StepDimTol_CoaxialityTolerance) aToler =
-            new StepDimTol_CoaxialityTolerance;
-          aToler->Init(aName,aDescription,LMWU,SA,HADR);
-          Model->AddWithRefs(aToler);
-        }
-        else if(kind==27) {
-          Handle(StepDimTol_ConcentricityTolerance) aToler =
-            new StepDimTol_ConcentricityTolerance;
-          aToler->Init(aName,aDescription,LMWU,SA,HADR);
-          Model->AddWithRefs(aToler);
-        }
-        else if(kind==28) {
-          Handle(StepDimTol_ParallelismTolerance) aToler =
-            new StepDimTol_ParallelismTolerance;
-          aToler->Init(aName,aDescription,LMWU,SA,HADR);
-          Model->AddWithRefs(aToler);
-        }
-        else if(kind==29) {
-          Handle(StepDimTol_PerpendicularityTolerance) aToler =
-            new StepDimTol_PerpendicularityTolerance;
-          aToler->Init(aName,aDescription,LMWU,SA,HADR);
-          Model->AddWithRefs(aToler);
-        }
-        else if(kind==30) {
-          Handle(StepDimTol_SymmetryTolerance) aToler =
-            new StepDimTol_SymmetryTolerance;
-          aToler->Init(aName,aDescription,LMWU,SA,HADR);
-          Model->AddWithRefs(aToler);
-        }
-        else if(kind==31) {
-          Handle(StepDimTol_TotalRunoutTolerance) aToler =
-            new StepDimTol_TotalRunoutTolerance;
-          aToler->Init(aName,aDescription,LMWU,SA,HADR);
-          Model->AddWithRefs(aToler);
-        }
+    case(XCAFKinematics_PairType_RollingSurface):
+    {
+      theKinematicPair = new StepKinematics_RollingSurfacePair;
+      GeomToStep_MakeSurface aMaker(aHighOrderPairObject->FirstSurface());
+      Handle(StepGeom_Surface) aPairSurfac1 = aMaker.Value();
+      GeomToStep_MakeSurface aMaker2(aHighOrderPairObject->SecondSurface());
+      Handle(StepGeom_Surface) aPairSurface2 = aMaker2.Value();
+      Standard_Boolean anOrientation = aHighOrderPairObject->Orientation();
+      Handle(StepKinematics_RollingSurfacePair) aRollingSurfacePair = Handle(StepKinematics_RollingSurfacePair)::DownCast(theKinematicPair);
+      aRollingSurfacePair->Init(aPairName, aPairName, hasDescription,
+        aDescription, aTransformItem1, aTransformItem2, theJoint, aPairSurfac1, aPairSurface2, anOrientation);
+      break;
+    }
+    case(XCAFKinematics_PairType_PointOnPlanarCurve):
+    {
+      GeomToStep_MakeCurve aMaker(aHighOrderPairObject->Curve());
+      Handle(StepGeom_Curve) aPairCurve = aMaker.Value();
+      Standard_Boolean anOrientation = aHighOrderPairObject->Orientation();
+      if (isRanged && !aPairCurve.IsNull() &&
+        aPairCurve->IsKind(STANDARD_TYPE(StepGeom_TrimmedCurve)))
+      {
+        Standard_Real aLowerLimitYaw = aHighOrderPairObject->LowLimitYaw();
+        Standard_Real aUpperLimitYaw = aHighOrderPairObject->UpperLimitYaw();
+        Standard_Real aLowerLimitPitch = aHighOrderPairObject->LowLimitPitch();
+        Standard_Real aUpperLimitPitch = aHighOrderPairObject->UpperLimitPitch();
+        Standard_Real aLowerLimitRoll = aHighOrderPairObject->LowLimitRoll();
+        Standard_Real aUpperLimitRoll = aHighOrderPairObject->UpperLimitRoll();
+        Handle(StepGeom_TrimmedCurve) aRangeOnPairCurve = Handle(StepGeom_TrimmedCurve)::DownCast(aPairCurve);
+        theKinematicPair = new StepKinematics_PointOnPlanarCurvePairWithRange;
+        Handle(StepKinematics_PointOnPlanarCurvePairWithRange) aPointOnPlanarCurvePairWithRange = Handle(StepKinematics_PointOnPlanarCurvePairWithRange)::DownCast(theKinematicPair);
+        aPointOnPlanarCurvePairWithRange->Init(aPairName, aPairName, hasDescription,
+          aDescription, aTransformItem1, aTransformItem2, theJoint, aPairCurve, anOrientation, aRangeOnPairCurve, Standard_True, aLowerLimitYaw, Standard_True, aUpperLimitYaw, Standard_True,
+          aLowerLimitPitch, Standard_True, aUpperLimitPitch, Standard_True, aLowerLimitRoll, Standard_True, aUpperLimitRoll);
+      }
+      else
+      {
+        theKinematicPair = new StepKinematics_PointOnPlanarCurvePair;
+        Handle(StepKinematics_PointOnPlanarCurvePair) aPointOnPlanarCurvePair = Handle(StepKinematics_PointOnPlanarCurvePair)::DownCast(theKinematicPair);
+        aPointOnPlanarCurvePair->Init(aPairName, aPairName, hasDescription,
+          aDescription, aTransformItem1, aTransformItem2, theJoint, aPairCurve, anOrientation);
       }
+      break;
+    }
+    case(XCAFKinematics_PairType_SlidingCurve): 
+    {
+      GeomToStep_MakeCurve aMaker(aHighOrderPairObject->FirstCurve());
+      Handle(StepGeom_Curve) aPairCurve = aMaker.Value();
+      Standard_Boolean anOrientation = aHighOrderPairObject->Orientation();
+      GeomToStep_MakeCurve aMaker2(aHighOrderPairObject->SecondCurve());
+      Handle(StepGeom_Curve) aPairCurve2 = aMaker2.Value();
+      theKinematicPair = new StepKinematics_SlidingCurvePair;
+      Handle(StepKinematics_SlidingCurvePair) aSlidingCurvePair = Handle(StepKinematics_SlidingCurvePair)::DownCast(theKinematicPair);
+      aSlidingCurvePair->Init(aPairName, aPairName, hasDescription,
+        aDescription, aTransformItem1, aTransformItem2, theJoint, aPairCurve, aPairCurve2, anOrientation);
+      break;
+    }
+    case(XCAFKinematics_PairType_RollingCurve):
+    {
+      GeomToStep_MakeCurve aMaker(aHighOrderPairObject->FirstCurve());
+      Handle(StepGeom_Curve) aPairCurve = aMaker.Value();
+      Standard_Boolean anOrientation = aHighOrderPairObject->Orientation();
+      GeomToStep_MakeCurve aMaker2(aHighOrderPairObject->SecondCurve());
+      Handle(StepGeom_Curve) aPairCurve2 = aMaker2.Value();
+      theKinematicPair = new StepKinematics_RollingCurvePair;
+      Handle(StepKinematics_RollingCurvePair) aRollingCurvePair = Handle(StepKinematics_RollingCurvePair)::DownCast(theKinematicPair);
+      aRollingCurvePair->Init(aPairName, aPairName, hasDescription,
+        aDescription, aTransformItem1, aTransformItem2, theJoint, aPairCurve, aPairCurve2, anOrientation);
+      break;
+    }
+    case(XCAFKinematics_PairType_LinearFlexibleAndPlanarCurve):
+    {
+      theKinematicPair = new StepKinematics_LinearFlexibleAndPlanarCurvePair;
+      GeomToStep_MakeCurve aMaker(aHighOrderPairObject->Curve());
+      Handle(StepGeom_Curve) aPairCurve = aMaker.Value();
+      Standard_Boolean anOrientation = aHighOrderPairObject->Orientation();
+      Handle(StepKinematics_LinearFlexibleAndPlanarCurvePair) aLinearFlexibleAndPlanarCurvePair = Handle(StepKinematics_LinearFlexibleAndPlanarCurvePair)::DownCast(theKinematicPair);
+      aLinearFlexibleAndPlanarCurvePair->Init(aPairName, aPairName, hasDescription,
+        aDescription, aTransformItem1, aTransformItem2, theJoint, aPairCurve, anOrientation);
+      break;
+    }
+    default:
+      return Standard_False;
     }
   }
-
+
+  // Updating placements of the both link representations
+  theLinkRepr1->Items()->Resize(theLinkRepr1->Items()->Lower(), theLinkRepr1->Items()->Upper() + 1, Standard_True);
+  theLinkRepr1->Items()->ChangeLast() = aTransformItem1;
+  theLinkRepr2->Items()->Resize(theLinkRepr2->Items()->Lower(), theLinkRepr2->Items()->Upper() + 1, Standard_True);
+  theLinkRepr2->Items()->ChangeLast() = aTransformItem2;
+
+  return Standard_True;
+}
+
+//=======================================================================
+//function : createKinematicPairValue
+//purpose  : 
+//=======================================================================
+static Standard_Boolean createKinematicPairValue(const Handle(XCAFKinematics_PairValueObject)& theKinPairValueObj,
+                                                 const Handle(StepKinematics_PairRepresentationRelationship)& thePairReprRelationship,
+                                                 Handle(StepKinematics_PairValue)& thePairValue)
+{
+  if (theKinPairValueObj->Type() == XCAFKinematics_PairType_NoType)
+    return Standard_False;
+
+  switch (theKinPairValueObj->Type())
+  {
+  case(XCAFKinematics_PairType_Revolute):
+  {
+    thePairValue = new StepKinematics_RevolutePairValue;
+    Handle(StepKinematics_RevolutePairValue) aRevolutePairValue =
+      Handle(StepKinematics_RevolutePairValue)::DownCast(thePairValue);
+    aRevolutePairValue->SetActualRotation(theKinPairValueObj->GetRotation());
+    break;
+  }
+  case(XCAFKinematics_PairType_Prismatic):
+  {
+    thePairValue = new StepKinematics_PrismaticPairValue;
+    Handle(StepKinematics_PrismaticPairValue) aPrismaticPairValue =
+      Handle(StepKinematics_PrismaticPairValue)::DownCast(thePairValue);
+    aPrismaticPairValue->SetActualTranslation(theKinPairValueObj->GetTranslation());
+    break;
+  }
+  case(XCAFKinematics_PairType_Cylindrical):
+  {
+    thePairValue = new StepKinematics_CylindricalPairValue;
+    Handle(StepKinematics_CylindricalPairValue) aCylindricalPairValue =
+      Handle(StepKinematics_CylindricalPairValue)::DownCast(thePairValue);
+    aCylindricalPairValue->SetActualTranslation(theKinPairValueObj->GetTranslation());
+    aCylindricalPairValue->SetActualRotation(theKinPairValueObj->GetRotation());
+    break;
+  }
+  case(XCAFKinematics_PairType_Universal):
+  case(XCAFKinematics_PairType_Homokinetic):
+  {
+    thePairValue = new StepKinematics_UniversalPairValue;
+    Handle(StepKinematics_UniversalPairValue) aUniversalPairValue =
+      Handle(StepKinematics_UniversalPairValue)::DownCast(thePairValue);
+    aUniversalPairValue->SetFirstRotationAngle(theKinPairValueObj->GetFirstRotation());
+    aUniversalPairValue->SetSecondRotationAngle(theKinPairValueObj->GetSecondRotation());
+    break;
+  }
+  case(XCAFKinematics_PairType_Spherical):
+  case(XCAFKinematics_PairType_SphericalWithPin):
+  {
+    thePairValue = new StepKinematics_SphericalPairValue;
+    Handle(StepKinematics_SphericalPairValue) aSphericalPairValue =
+      Handle(StepKinematics_SphericalPairValue)::DownCast(thePairValue);
+    StepKinematics_SpatialRotation InputRotation;
+    InputRotation.SetValue(theKinPairValueObj->GetAllValues());
+    aSphericalPairValue->SetInputOrientation(InputRotation);
+    break;
+  }
+  case(XCAFKinematics_PairType_Planar):
+  {
+    thePairValue = new StepKinematics_PlanarPairValue;
+    Handle(StepKinematics_PlanarPairValue) aPlanarPairValue =
+      Handle(StepKinematics_PlanarPairValue)::DownCast(thePairValue);
+    aPlanarPairValue->SetActualRotation(theKinPairValueObj->GetRotation());
+    aPlanarPairValue->SetActualTranslationX(theKinPairValueObj->GetFirstTranslation());
+    aPlanarPairValue->SetActualTranslationY(theKinPairValueObj->GetSecondTranslation());
+    break;
+  }
+  case(XCAFKinematics_PairType_Unconstrained):
+  {
+    thePairValue = new StepKinematics_UnconstrainedPairValue;
+    Handle(StepKinematics_UnconstrainedPairValue) anUnconstrainedPairValue = 
+      Handle(StepKinematics_UnconstrainedPairValue)::DownCast(thePairValue);
+    GeomToStep_MakeAxis2Placement3d anActualPlacement(theKinPairValueObj->GetTransformation());
+    anUnconstrainedPairValue->SetActualPlacement(anActualPlacement.Value());
+    break;
+  }
+  case(XCAFKinematics_PairType_Screw):
+  {
+    thePairValue = new StepKinematics_ScrewPairValue;
+    Handle(StepKinematics_ScrewPairValue) aScrewPairValue = 
+      Handle(StepKinematics_ScrewPairValue)::DownCast(thePairValue);
+    aScrewPairValue->SetActualRotation(theKinPairValueObj->GetRotation());
+    break;
+  }
+  case(XCAFKinematics_PairType_RackAndPinion):
+  {
+    thePairValue = new StepKinematics_RackAndPinionPairValue;
+    Handle(StepKinematics_RackAndPinionPairValue) aRackAndPinionPairValue = 
+      Handle(StepKinematics_RackAndPinionPairValue)::DownCast(thePairValue);
+    aRackAndPinionPairValue->SetActualDisplacement(theKinPairValueObj->GetTranslation());
+    break;
+  }
+  case(XCAFKinematics_PairType_Gear):
+  {
+    thePairValue = new StepKinematics_GearPairValue;
+    Handle(StepKinematics_GearPairValue) aGearPairValue = 
+      Handle(StepKinematics_GearPairValue)::DownCast(thePairValue);
+    aGearPairValue->SetActualRotation1(theKinPairValueObj->GetRotation());
+    break;
+  }
+  case(XCAFKinematics_PairType_PointOnSurface):
+  {
+    thePairValue = new StepKinematics_PointOnSurfacePairValue;
+    Handle(StepKinematics_PointOnSurfacePairValue) aPointOnSurfacePairValue = 
+      Handle(StepKinematics_PointOnSurfacePairValue)::DownCast(thePairValue);
+    Handle(StepGeom_PointOnSurface) aActualPointOnSurface = new StepGeom_PointOnSurface;
+    Handle(StepKinematics_PointOnSurfacePair) aPointOnSurfacePair = 
+      Handle(StepKinematics_PointOnSurfacePair)::DownCast(thePairReprRelationship->RepresentationRelationshipWithTransformation()->TransformationOperator().KinematicPair());
+    Standard_Real theU;
+    Standard_Real theV;
+    theKinPairValueObj->GetPointOnSurface(theU, theV);
+    aActualPointOnSurface->Init(new TCollection_HAsciiString, aPointOnSurfacePair->PairSurface(), theU, theV);
+    aPointOnSurfacePairValue->SetActualPointOnSurface(aActualPointOnSurface);
+    StepKinematics_SpatialRotation aYRP;
+    Handle(TColStd_HArray1OfReal) anArrayYRP = new TColStd_HArray1OfReal(1,3);
+    Standard_Real aYaw;
+    Standard_Real aPitch;
+    Standard_Real aRoll;
+    theKinPairValueObj->GetYPR(aYaw, aPitch, aRoll);
+    anArrayYRP->SetValue(1, aYaw);
+    anArrayYRP->SetValue(2, aPitch);
+    anArrayYRP->SetValue(3, aRoll);
+    aYRP.SetValue(anArrayYRP);
+    aPointOnSurfacePairValue->SetInputOrientation(aYRP);
+    break;
+  }
+  case(XCAFKinematics_PairType_SlidingSurface):
+  {
+    thePairValue = new StepKinematics_SlidingSurfacePairValue;
+    Handle(StepKinematics_SlidingSurfacePairValue) aSlidingSurfacePairValue =
+      Handle(StepKinematics_SlidingSurfacePairValue)::DownCast(thePairValue);
+    Handle(StepGeom_PointOnSurface) aActualPointOnSurface1 = new StepGeom_PointOnSurface;
+    Handle(StepKinematics_SlidingSurfacePair) SlidingSurfacePair =
+      Handle(StepKinematics_SlidingSurfacePair)::DownCast(thePairReprRelationship->RepresentationRelationshipWithTransformation()->TransformationOperator().KinematicPair());
+    Standard_Real theU1;
+    Standard_Real theV1;
+    theKinPairValueObj->GetFirstPointOnSurface(theU1, theV1);
+    aActualPointOnSurface1->Init(new TCollection_HAsciiString, SlidingSurfacePair->Surface1(), theU1, theV1);
+    aSlidingSurfacePairValue->SetActualPointOnSurface1(aActualPointOnSurface1);
+    Handle(StepGeom_PointOnSurface) aActualPointOnSurface2 = new StepGeom_PointOnSurface;
+    Standard_Real theU2;
+    Standard_Real theV2;
+    theKinPairValueObj->GetSecondPointOnSurface(theU2, theV2);
+    aActualPointOnSurface2->Init(new TCollection_HAsciiString, SlidingSurfacePair->Surface2(), theU2, theV2);
+    aSlidingSurfacePairValue->SetActualPointOnSurface2(aActualPointOnSurface2);
+    aSlidingSurfacePairValue->SetActualRotation(theKinPairValueObj->GetRotation());
+    break;
+  }
+  case(XCAFKinematics_PairType_RollingSurface):
+  {
+    thePairValue = new StepKinematics_RollingSurfacePairValue;
+    Handle(StepKinematics_RollingSurfacePairValue) aRollingSurfacePairValue =
+      Handle(StepKinematics_RollingSurfacePairValue)::DownCast(thePairValue);
+    aRollingSurfacePairValue->SetActualRotation(theKinPairValueObj->GetRotation());
+    Handle(StepGeom_PointOnSurface) aActualPointOnSurface = new StepGeom_PointOnSurface;
+    Handle(StepKinematics_RollingSurfacePair) aRollingSurfacePair =
+      Handle(StepKinematics_RollingSurfacePair)::DownCast(thePairReprRelationship->RepresentationRelationshipWithTransformation()->TransformationOperator().KinematicPair());
+    Standard_Real theU;
+    Standard_Real theV;
+    theKinPairValueObj->GetPointOnSurface(theU, theV);
+    aActualPointOnSurface->Init(new TCollection_HAsciiString, aRollingSurfacePair->Surface1(), theU, theV);
+    aRollingSurfacePairValue->SetActualPointOnSurface(aActualPointOnSurface);
+    break;
+  }
+  case(XCAFKinematics_PairType_PointOnPlanarCurve):
+  {
+    thePairValue = new StepKinematics_PointOnPlanarCurvePairValue;
+    Handle(StepKinematics_PointOnPlanarCurvePairValue) aPointOnPlanarCurvePairValue =
+      Handle(StepKinematics_PointOnPlanarCurvePairValue)::DownCast(thePairValue);
+    StepKinematics_SpatialRotation aYRP;
+    Handle(TColStd_HArray1OfReal) anArrayYRP = new TColStd_HArray1OfReal(1, 3);
+    Standard_Real aYaw;
+    Standard_Real aPitch;
+    Standard_Real aRoll;
+    theKinPairValueObj->GetYPR(aYaw, aPitch, aRoll);
+    anArrayYRP->SetValue(1, aYaw);
+    anArrayYRP->SetValue(2, aPitch);
+    anArrayYRP->SetValue(3, aRoll);
+    aYRP.SetValue(anArrayYRP);
+    aPointOnPlanarCurvePairValue->SetInputOrientation(aYRP);
+    Handle(StepGeom_PointOnCurve) anActualPointOnCurve = new StepGeom_PointOnCurve;
+    Handle(StepKinematics_PointOnPlanarCurvePair) aPointOnPlanarCurvePair =
+      Handle(StepKinematics_PointOnPlanarCurvePair)::DownCast(thePairReprRelationship->RepresentationRelationshipWithTransformation()->TransformationOperator().KinematicPair());
+    Standard_Real aParametr = theKinPairValueObj->GetPointOnCurve();
+    anActualPointOnCurve->Init(aPointOnPlanarCurvePair->Name(), aPointOnPlanarCurvePair->PairCurve(), aParametr);
+    aPointOnPlanarCurvePairValue->SetActualPointOnCurve(anActualPointOnCurve);
+    break;
+  }
+  case(XCAFKinematics_PairType_SlidingCurve):
+  {
+    thePairValue = new StepKinematics_SlidingCurvePairValue;
+    Handle(StepKinematics_SlidingCurvePairValue) aSlidingCurvePairValue = 
+      Handle(StepKinematics_SlidingCurvePairValue)::DownCast(thePairValue);
+    Handle(StepGeom_PointOnCurve) anActualPointOnCurve1 = new StepGeom_PointOnCurve;
+    Handle(StepKinematics_SlidingCurvePair) aSlidingCurvePair =
+      Handle(StepKinematics_SlidingCurvePair)::DownCast(thePairReprRelationship->RepresentationRelationshipWithTransformation()->TransformationOperator().KinematicPair());
+    Standard_Real aParametr1 = theKinPairValueObj->GetFirstPointOnCurve();
+    anActualPointOnCurve1->Init(aSlidingCurvePair->Name(), aSlidingCurvePair->Curve1(), aParametr1);
+    aSlidingCurvePairValue->SetActualPointOnCurve1(anActualPointOnCurve1);
+    Handle(StepGeom_PointOnCurve) anActualPointOnCurve2 = new StepGeom_PointOnCurve;
+    Standard_Real aParametr2 = theKinPairValueObj->GetSecondPointOnCurve();
+    anActualPointOnCurve2->Init(aSlidingCurvePair->Name(), aSlidingCurvePair->Curve2(), aParametr2);
+    aSlidingCurvePairValue->SetActualPointOnCurve2(anActualPointOnCurve2);
+    break;
+  }
+  case(XCAFKinematics_PairType_RollingCurve):
+  {
+    thePairValue = new StepKinematics_RollingCurvePairValue;
+    Handle(StepKinematics_RollingCurvePairValue) aRollingCurvePairValue =
+      Handle(StepKinematics_RollingCurvePairValue)::DownCast(thePairValue);
+    Handle(StepGeom_PointOnCurve) anActualPointOnCurve = new StepGeom_PointOnCurve;
+    Handle(StepKinematics_RollingCurvePair) aRollingCurvePair =
+      Handle(StepKinematics_RollingCurvePair)::DownCast(thePairReprRelationship->RepresentationRelationshipWithTransformation()->TransformationOperator().KinematicPair());
+    Standard_Real aParametr = theKinPairValueObj->GetPointOnCurve();
+    anActualPointOnCurve->Init(aRollingCurvePair->Name(), aRollingCurvePair->Curve1(), aParametr);
+    aRollingCurvePairValue->SetActualPointOnCurve1(anActualPointOnCurve);
+    break; 
+  }
+  default:
+    return Standard_False;
+    break;
+  }
+  Handle(StepKinematics_KinematicPair) aAppliesToPair =
+    thePairReprRelationship->RepresentationRelationshipWithTransformation()->TransformationOperator().KinematicPair();
+  thePairValue->SetAppliesToPair(aAppliesToPair);
+  thePairValue->SetName(aAppliesToPair->Name());
   return Standard_True;
 }
 
 //=======================================================================
-//function : WriteDGTsAP242
+//function : WriteKinematics
 //purpose  : 
 //=======================================================================
-
-Standard_Boolean STEPCAFControl_Writer::WriteDGTsAP242 (const Handle(XSControl_WorkSession) &WS,
-                                                        const TDF_LabelSequence  &labels )
+Standard_EXPORT Standard_Boolean STEPCAFControl_Writer::WriteKinematics(const Handle(XSControl_WorkSession)& theWS, const TDF_LabelSequence& theLabels)
 {
-  // Get working data
-  const Handle(Interface_InterfaceModel) &aModel = WS->Model();
 
-  const Handle(Interface_HGraph) aHGraph = WS->HGraph();
-  if(aHGraph.IsNull())
+  if (theLabels.Length() <= 0) return Standard_False;
+
+  // Get working data
+  const Handle(Interface_InterfaceModel)& Model = theWS->Model();
+  const Handle(Interface_HGraph) aHGraph = theWS->HGraph();
+  if (aHGraph.IsNull())
     return Standard_False;
 
   Interface_Graph aGraph = aHGraph->Graph();
-  Handle(XCAFDoc_DimTolTool) DGTTool = XCAFDoc_DocumentTool::DimTolTool(labels(1));
-  if(DGTTool.IsNull())
+  Handle(XCAFDoc_KinematicTool) aKTool = XCAFDoc_DocumentTool::KinematicTool(theLabels(1));
+  if (aKTool.IsNull()) 
     return Standard_False;
 
-  // Common entities for presentation
-  STEPConstruct_Styles aStyles (WS);
-  Handle(StepVisual_Colour) aCurvColor = aStyles.EncodeColor(Quantity_NOC_WHITE);
-  Handle(StepRepr_RepresentationItem) anItem = NULL;
-  myGDTPrsCurveStyle->SetValue(1, aStyles.MakeColorPSA(anItem, aCurvColor, aCurvColor, aCurvColor, 0.0));
-  Interface_EntityIterator aModelIter = aModel->Entities();
-  for (; aModelIter.More() && myGDTCommonPDS.IsNull(); aModelIter.Next())
-    myGDTCommonPDS = Handle(StepRepr_ProductDefinitionShape)::DownCast(aModelIter.Value());
-
-  TDF_LabelSequence aDGTLabels;
-  STEPConstruct_DataMapOfAsciiStringTransient aDatumMap;
-  Handle(StepRepr_RepresentationContext) aRC;
-
-  //------------- //
-  // write Datums //
-  //--------------//
-  DGTTool->GetDatumLabels(aDGTLabels);
-  // Find all shapes with datums
-  TColStd_MapOfAsciiString aNameIdMap;
-  for(Standard_Integer i = 1; i <= aDGTLabels.Length(); i++) {
-    TDF_Label aDatumL = aDGTLabels.Value(i);
-    TDF_LabelSequence aShapeL, aNullSeq;
-    DGTTool->GetRefShapeLabel(aDatumL, aShapeL, aNullSeq);
-    Handle(XCAFDoc_Datum) aDatumAttr;
-    aDatumL.FindAttribute(XCAFDoc_Datum::GetID(), aDatumAttr);
-    Handle(XCAFDimTolObjects_DatumObject) anObject = aDatumAttr->GetObject();
-    TCollection_AsciiString aDatumName = anObject->GetName()->String();
-    TCollection_AsciiString aDatumTargetId = TCollection_AsciiString(anObject->GetDatumTargetNumber());
-    if (!aNameIdMap.Add(aDatumName.Cat(aDatumTargetId)))
-      continue;
-    Handle(Standard_Transient) aWrittenDatum;
-    Standard_Boolean isFirstDT = !aDatumMap.Find(aDatumName, aWrittenDatum);
-    Handle(StepDimTol_Datum) aDatum = WriteDatumAP242(WS, aShapeL, aDatumL, isFirstDT, 
-                                                      Handle(StepDimTol_Datum)::DownCast (aWrittenDatum));
-    // Add created Datum into Map
-    aDatumMap.Bind(aDatumName, aDatum);
-  }
-
-  //----------------- //
-  // write Dimensions //
-  //------------------//
-  aDGTLabels.Clear();
-  DGTTool->GetDimensionLabels(aDGTLabels);
-  // Auxiliary entities for derived geometry
-  Handle(StepRepr_ConstructiveGeometryRepresentation) aCGRepr =
-    new StepRepr_ConstructiveGeometryRepresentation();
-  Handle(StepRepr_ConstructiveGeometryRepresentationRelationship) aCGReprRel =
-    new StepRepr_ConstructiveGeometryRepresentationRelationship();
-  NCollection_Vector<Handle(StepGeom_CartesianPoint)> aConnectionPnts;
-  Handle(StepRepr_RepresentationContext) dummyRC;
-  Handle(StepAP242_GeometricItemSpecificUsage) dummyGISU;
-  for (Standard_Integer i = 1; i <= aDGTLabels.Length(); i++) {
-    TDF_Label aDimensionL = aDGTLabels.Value(i);
-    TDF_LabelSequence aFirstShapeL, aSecondShapeL;
-    Handle(XCAFDoc_Dimension) aDimAttr;
-    if (!aDimensionL.FindAttribute(XCAFDoc_Dimension::GetID(),aDimAttr)) 
-      continue;
-    Handle(XCAFDimTolObjects_DimensionObject) anObject = aDimAttr->GetObject();
-    if (anObject.IsNull())
-      continue;
-    if (anObject->GetType() == XCAFDimTolObjects_DimensionType_CommonLabel)
-    {
-      Handle(StepRepr_ShapeAspect) aSA = new StepRepr_ShapeAspect();
-      aSA->Init(new TCollection_HAsciiString(), new TCollection_HAsciiString(), myGDTCommonPDS, StepData_LTrue);
-      aModel->AddWithRefs(aSA);
-      WritePresentation(WS, anObject->GetPresentation(), anObject->GetPresentationName(), anObject->HasPlane(),
-        Standard_False, anObject->GetPlane(), anObject->GetPointTextAttach(), aSA);
-    }
+  TDF_LabelSequence aMechanismsL;
+  aMechanismsL = aKTool->GetMechanisms();
+  if (aMechanismsL.Length() <= 0) return Standard_False;
 
-    if (!DGTTool->GetRefShapeLabel(aDimensionL, aFirstShapeL, aSecondShapeL))
+  for (TDF_LabelSequence::Iterator anItMech(aMechanismsL); anItMech.More(); anItMech.Next())
+  {
+    TDF_LabelSequence aSeqOfJointsL = aKTool->GetJoints(anItMech.Value());
+    if (aSeqOfJointsL.Length() <= 0) 
       continue;
 
-    // Write links with shapes
-    Handle(StepRepr_ShapeAspect) aFirstSA, aSecondSA;
-    if (aFirstShapeL.Length() == 1) {
-      TopoDS_Shape aShape = XCAFDoc_ShapeTool::GetShape(aFirstShapeL.Value(1));
-      aFirstSA = WriteShapeAspect(WS, aDimensionL, aShape, dummyRC, dummyGISU);
-      if (aRC.IsNull() && !dummyRC.IsNull())
-        aRC = dummyRC;
-    }
-    else if (aFirstShapeL.Length() > 1) {
-      Handle(StepRepr_CompositeShapeAspect) aCSA;
-      for (Standard_Integer shIt = 1; shIt <= aFirstShapeL.Length(); shIt++) {
-        TopoDS_Shape aShape = XCAFDoc_ShapeTool::GetShape(aFirstShapeL.Value(shIt));
-        Handle(StepRepr_ShapeAspect) aSA = WriteShapeAspect(WS, aDimensionL, aShape, dummyRC, dummyGISU);
-        if (aSA.IsNull())
-          continue;
-        if (aCSA.IsNull()) {
-          aCSA = new StepRepr_CompositeShapeAspect();
-          aCSA->Init(aSA->Name(), aSA->Description(), aSA->OfShape(), aSA->ProductDefinitional());
-          aModel->AddWithRefs(aCSA);
-        }
-        Handle(StepRepr_ShapeAspectRelationship) aSAR = new StepRepr_ShapeAspectRelationship();
-        aSAR->Init(new TCollection_HAsciiString(), Standard_False, new TCollection_HAsciiString(), aCSA, aSA);
-        aModel->AddWithRefs(aSAR);
-        if (aRC.IsNull() && !dummyRC.IsNull())
-          aRC = dummyRC;
-      }
-      aFirstSA = aCSA;
-    }
-    if (aSecondShapeL.Length() == 1) {
-      TopoDS_Shape aShape = XCAFDoc_ShapeTool::GetShape(aSecondShapeL.Value(1));
-      aSecondSA = WriteShapeAspect(WS, aDimensionL, aShape, dummyRC, dummyGISU);
-      if (aRC.IsNull() && !dummyRC.IsNull())
-        aRC = dummyRC;
-    }
-    else if (aSecondShapeL.Length() > 1) {
-      Handle(StepRepr_CompositeShapeAspect) aCSA;
-      for (Standard_Integer shIt = 1; shIt <= aSecondShapeL.Length(); shIt++) {
-        TopoDS_Shape aShape = XCAFDoc_ShapeTool::GetShape(aSecondShapeL.Value(shIt));
-        Handle(StepRepr_ShapeAspect) aSA = WriteShapeAspect(WS, aDimensionL, aShape, dummyRC, dummyGISU);
-        if (aCSA.IsNull() && !aSA.IsNull())
-        {
-          aCSA = new StepRepr_CompositeShapeAspect();
-        }
-        aCSA->Init(aSA->Name(), aSA->Description(), aSA->OfShape(), aSA->ProductDefinitional());
-        if (!aSA.IsNull()) {
-          Handle(StepRepr_ShapeAspectRelationship) aSAR = new StepRepr_ShapeAspectRelationship();
-          aSAR->Init(new TCollection_HAsciiString(), Standard_False, new TCollection_HAsciiString(), aCSA, aSA);
-          aModel->AddWithRefs(aSAR);
-        }
-        if (aRC.IsNull() && !dummyRC.IsNull())
-          aRC = dummyRC;
-      }
-      aSecondSA = aCSA;
-    }
+    Handle(StepKinematics_RigidLinkRepresentation) aBaseLinkOfMech;
+    Handle(StepRepr_PropertyDefinition) aGeneralDefinition;
+    NCollection_IndexedDataMap<TDF_Label, Handle(StepKinematics_KinematicLinkRepresentation), TDF_LabelMapHasher> aMapOfLinks;
+    NCollection_IndexedDataMap<TDF_Label, Handle(StepKinematics_KinematicJoint), TDF_LabelMapHasher> aMapofJoints;
+    Handle(StepRepr_HSequenceOfRepresentationItem) aSeqOfPairs = new StepRepr_HSequenceOfRepresentationItem;
+    Handle(StepKinematics_KinematicPropertyMechanismRepresentation) aPropertyMechanismRepr = new StepKinematics_KinematicPropertyMechanismRepresentation;
+    Handle(StepKinematics_ProductDefinitionKinematics) aProductDefKin = new StepKinematics_ProductDefinitionKinematics;
+    Handle(StepKinematics_MechanismRepresentation) aMechanism = new StepKinematics_MechanismRepresentation;
+    Handle(StepKinematics_KinematicTopologyStructure) aKTopoStruct = new StepKinematics_KinematicTopologyStructure;
 
-    if (anObject->GetType() == XCAFDimTolObjects_DimensionType_DimensionPresentation)
+    for (TDF_LabelSequence::Iterator anItJoint(aSeqOfJointsL); anItJoint.More(); anItJoint.Next())
     {
-      WritePresentation(WS, anObject->GetPresentation(), anObject->GetPresentationName(), anObject->HasPlane(),
-        Standard_False, anObject->GetPlane(), anObject->GetPointTextAttach(), aFirstSA);
-      continue;
-    }
+      TDF_Label aJointL = anItJoint.Value();
+      Handle(StepKinematics_KinematicJoint) aJoint;
+      Handle(XCAFDoc_KinematicPair) aKPairAttr;
+      Handle(StepKinematics_KinematicPair) aKinematicPair;
+      if (!aJointL.FindAttribute(XCAFDoc_KinematicPair::GetID(), aKPairAttr))
+        continue;
+      Handle(XCAFKinematics_PairObject) aPairObject = aKPairAttr->GetObject();
+      TDF_Label aStartLink, aEndLink;
+      aKTool->GetLinksOfJoint(aJointL, aStartLink, aEndLink);
+      if (aStartLink.IsNull() || aEndLink.IsNull())
+        continue;
 
-    // Write dimensions
-    StepShape_DimensionalCharacteristic aDimension;
-    if (anObject->HasPoint() || anObject->HasPoint2())
-      WriteDerivedGeometry(WS, anObject, aCGRepr, aFirstSA, aSecondSA, aConnectionPnts);
-    XCAFDimTolObjects_DimensionType aDimType = anObject->GetType();
-    if (STEPCAFControl_GDTProperty::IsDimensionalLocation(aDimType)) {
-      // Dimensional_Location
-      Handle(StepShape_DimensionalLocation) aDim = new StepShape_DimensionalLocation();
-      aDim->Init(STEPCAFControl_GDTProperty::GetDimTypeName(aDimType), Standard_False, NULL, aFirstSA, aSecondSA);
-      aDimension.SetValue(aDim);
-    }
-    else if (aDimType == XCAFDimTolObjects_DimensionType_Location_Angular) {
-      // Angular_Location
-      Handle(StepShape_AngularLocation) aDim = new StepShape_AngularLocation();
-      StepShape_AngleRelator aRelator = StepShape_Equal;
-      if (anObject->HasQualifier()) {
-        XCAFDimTolObjects_DimensionQualifier aQualifier = anObject->GetQualifier();
-        switch (aQualifier) {
-          case XCAFDimTolObjects_DimensionQualifier_Min: aRelator = StepShape_Small;
-            break;
-          case XCAFDimTolObjects_DimensionQualifier_Max: aRelator = StepShape_Large;
-            break;
-          default: aRelator = StepShape_Equal;
-        }
-      }
-      aDim->Init(new TCollection_HAsciiString(), Standard_False, NULL, aFirstSA, aSecondSA, aRelator);
-      aDimension.SetValue(aDim);
-    }
-    else if (aDimType == XCAFDimTolObjects_DimensionType_Location_WithPath) {
-      // Dimensional_Location_With_Path
-      Handle(StepShape_DimensionalLocationWithPath) aDim = new StepShape_DimensionalLocationWithPath();
-      Handle(StepRepr_ShapeAspect) aPathSA = WriteShapeAspect(WS, aDimensionL, anObject->GetPath(), dummyRC, dummyGISU);
-      aDim->Init(new TCollection_HAsciiString(), Standard_False, NULL, aFirstSA, aSecondSA, aPathSA);
-      aDimension.SetValue(aDim);
-    }
-    else if (STEPCAFControl_GDTProperty::IsDimensionalSize(aDimType)) {
-      // Dimensional_Size
-      Handle(StepShape_DimensionalSize) aDim = new StepShape_DimensionalSize();
-      aDim->Init(aFirstSA, STEPCAFControl_GDTProperty::GetDimTypeName(aDimType));
-      aDimension.SetValue(aDim);
-    }
-    else if (aDimType == XCAFDimTolObjects_DimensionType_Size_Angular) {
-      // Angular_Size
-      Handle(StepShape_AngularSize) aDim = new StepShape_AngularSize();
-      StepShape_AngleRelator aRelator = StepShape_Equal;
-      if (anObject->HasQualifier()) {
-        XCAFDimTolObjects_DimensionQualifier aQualifier = anObject->GetQualifier();
-        switch (aQualifier) {
-          case XCAFDimTolObjects_DimensionQualifier_Min: aRelator = StepShape_Small;
-            break;
-          case XCAFDimTolObjects_DimensionQualifier_Max: aRelator = StepShape_Large;
-            break;
-          default: aRelator = StepShape_Equal;
-        }
-      }
-      aDim->Init(aFirstSA, new TCollection_HAsciiString(), aRelator);
-      aDimension.SetValue(aDim);
-    }
-    else if (aDimType == XCAFDimTolObjects_DimensionType_Size_WithPath) {
-      // Dimensional_Size_With_Path
-      Handle(StepShape_DimensionalSizeWithPath) aDim = new StepShape_DimensionalSizeWithPath();
-      Handle(StepRepr_ShapeAspect) aPathSA = WriteShapeAspect(WS, aDimensionL, anObject->GetPath(), dummyRC, dummyGISU);
-      aDim->Init(aFirstSA, new TCollection_HAsciiString(), aPathSA);
-      aDimension.SetValue(aDim);
-    }
+      Handle(StepKinematics_KinematicLinkRepresentation) aLinkRepr1;
+      Handle(StepKinematics_KinematicLinkRepresentation) aLinkRepr2;
+      Handle(TColStd_HSequenceOfTransient) aSeqOfContexLink = new TColStd_HSequenceOfTransient;
+      if (!createKinematicLink(theWS,aKTool, aStartLink,aMapOfLinks, aLinkRepr1, aSeqOfContexLink,aBaseLinkOfMech,aGeneralDefinition))
+        continue;
+      if (!createKinematicLink(theWS, aKTool, aEndLink, aMapOfLinks, aLinkRepr2, aSeqOfContexLink, aBaseLinkOfMech, aGeneralDefinition))
+        continue;
+      if (!createKinematicJoint(aPairObject, aLinkRepr1, aLinkRepr2, aJoint))
+        continue;
+      if (!createKinematicPair(aPairObject, aJoint, aLinkRepr1, aLinkRepr2, aKinematicPair))
+        continue;
+      aMapofJoints.Add(anItJoint.Value(), aJoint);
+      Handle(StepKinematics_PairRepresentationRelationship) aPairReprRelationship = new StepKinematics_PairRepresentationRelationship;
+      StepRepr_RepresentationOrRepresentationReference aDataLinkStart;
+      aDataLinkStart.SetValue(aLinkRepr1);
+      StepRepr_RepresentationOrRepresentationReference aDataLinkEnd;
+      aDataLinkEnd.SetValue(aLinkRepr2);
+      StepRepr_Transformation aPair;
+      aPair.SetValue(aKinematicPair);
+      Handle(TCollection_HAsciiString) aDescription;
+      aPairReprRelationship->Init(aKinematicPair->Name(), aKinematicPair->Name(), Standard_False, aDescription, aDataLinkStart, aDataLinkEnd, aPair);
+      aSeqOfPairs->Append(aPairReprRelationship);
 
-    // Write values
-    WriteDimValues(WS, anObject, aRC, aDimension);
-    //Annotation plane and Presentation
-    WritePresentation(WS, anObject->GetPresentation(), anObject->GetPresentationName(), Standard_True, anObject->HasPlane(),
-      anObject->GetPlane(), anObject->GetPointTextAttach(), aDimension.Value());
-  }
-  // Write Derived geometry
-  if (aConnectionPnts.Length() > 0) {
-    Handle(StepRepr_HArray1OfRepresentationItem) anItems = new StepRepr_HArray1OfRepresentationItem(1, aConnectionPnts.Length());
-    for (Standard_Integer i = 0; i < aConnectionPnts.Length(); i++)
-      anItems->SetValue(i + 1, aConnectionPnts(i));
-    aCGRepr->Init(new TCollection_HAsciiString(), anItems, dummyRC);
-    aCGReprRel->Init(new TCollection_HAsciiString(), new TCollection_HAsciiString(), dummyGISU->UsedRepresentation(), aCGRepr);
-    aModel->AddWithRefs(aCGReprRel);
-  }
+      // Write all entities describe mechanism kinematics
+      for (TColStd_HSequenceOfTransient::Iterator anItLinks(aSeqOfContexLink->Sequence()); anItLinks.More(); anItLinks.Next())
+        Model->AddWithRefs(anItLinks.Value());
+      Model->AddEntity(aJoint);
+      Model->AddWithRefs(aPairReprRelationship);
+    }
 
-  //----------------------------//
-  // write Geometric Tolerances //
-  //----------------------------//
-  aDGTLabels.Clear();
-  DGTTool->GetGeomToleranceLabels(aDGTLabels);
-  for (Standard_Integer i = 1; i <= aDGTLabels.Length(); i++) {
-    TDF_Label aGeomTolL = aDGTLabels.Value(i);
-    TDF_LabelSequence aFirstShapeL, aNullSeqL;
-    if (!DGTTool->GetRefShapeLabel(aGeomTolL, aFirstShapeL, aNullSeqL))
+    if (aBaseLinkOfMech.IsNull() || aSeqOfPairs->IsEmpty())
       continue;
-    TDF_LabelSequence aDatumSeq;
-    DGTTool->GetDatumWithObjectOfTolerLabels(aGeomTolL, aDatumSeq);
-    Handle(StepDimTol_HArray1OfDatumSystemOrReference) aDatumSystem;
-    if (aDatumSeq.Length() > 0)
-      aDatumSystem = WriteDatumSystem(WS, aGeomTolL, aDatumSeq, aDatumMap, aRC);
-    WriteGeomTolerance(WS, aFirstShapeL, aGeomTolL, aDatumSystem, aRC);
-  }
 
-  // Write Draughting model for Annotation Planes
-  if (myGDTAnnotations.Length() == 0)
-    return Standard_True;
+    Standard_Integer anPairInd = 1;
+    Handle(StepRepr_HArray1OfRepresentationItem) anArrayOfPairs = new StepRepr_HArray1OfRepresentationItem(1, aSeqOfPairs->Length());
+    for (StepRepr_HSequenceOfRepresentationItem::Iterator anPairIt(aSeqOfPairs->Sequence()); anPairIt.More();anPairIt.Next())
+      anArrayOfPairs->SetValue(anPairInd++,anPairIt.Value());
 
-  Handle(StepRepr_HArray1OfRepresentationItem) aItems =
-    new StepRepr_HArray1OfRepresentationItem(1, myGDTAnnotations.Length());
-  for (Standard_Integer i = 1; i <= aItems->Length(); i++) {
-    aItems->SetValue(i, myGDTAnnotations.Value(i - 1));
+    // Write mechanism
+    aProductDefKin->Init(new TCollection_HAsciiString, Standard_False, new TCollection_HAsciiString, aGeneralDefinition->Definition());
+    aKTopoStruct->Init(new TCollection_HAsciiString, anArrayOfPairs, aBaseLinkOfMech->ContextOfItems());
+    StepKinematics_KinematicTopologyRepresentationSelect aTopoSelect;
+    aTopoSelect.SetValue(aKTopoStruct);
+    Handle(TDataStd_Name) aAttrName;
+    Handle(TCollection_HAsciiString) aHNameMechanism;
+    if (anItMech.Value().FindAttribute(TDataStd_Name::GetID(), aAttrName))
+       aHNameMechanism = new TCollection_HAsciiString(aAttrName->Get());
+    else
+      aHNameMechanism = new TCollection_HAsciiString("");
+    StepRepr_RepresentedDefinition aPDK;
+    aPDK.SetValue(aProductDefKin);
+    aMechanism->Init(aHNameMechanism, anArrayOfPairs, aBaseLinkOfMech->ContextOfItems(), aTopoSelect);
+    aPropertyMechanismRepr->Init(aPDK, aMechanism, aBaseLinkOfMech);
+    Model->AddWithRefs(aPropertyMechanismRepr);
+
+    // (Optional) write states
+    TDF_LabelSequence aSeqOfStates = aKTool->GetStates(anItMech.Value());
+    for (TDF_LabelSequence::Iterator anItState(aSeqOfStates); anItState.More(); anItState.Next())
+    {
+      Handle(StepKinematics_MechanismStateRepresentation) aStateRepr = new StepKinematics_MechanismStateRepresentation;
+      TDF_LabelSequence aSeqOfValues = aKTool->GetValuesOfState(anItState.Value());
+      if (aSeqOfValues.IsEmpty())
+        continue;
+      Handle(StepRepr_HArray1OfRepresentationItem) aItems = new StepRepr_HArray1OfRepresentationItem(1, aSeqOfValues.Length());
+      Standard_Integer aValueInd = 1;
+      for (TDF_LabelSequence::Iterator anItValue(aSeqOfValues); anItValue.More(); anItValue.Next())
+      {
+        TDF_Label aJointL = aKTool->GetJointOfValue(anItValue.Value());
+        Handle(StepKinematics_KinematicJoint) aJoint;
+        Handle(StepKinematics_PairRepresentationRelationship) aPairReprRelationship;
+        if (!aMapofJoints.FindFromKey(aJointL, aJoint))
+          continue;
+        aPairReprRelationship = Handle(StepKinematics_PairRepresentationRelationship)::DownCast(anArrayOfPairs->Value(aMapofJoints.FindIndex(aJointL)));
+        if (aPairReprRelationship.IsNull())
+          continue;
+        Handle(XCAFDoc_KinematicPairValue) aKPairValueAttr;
+        if (!anItValue.Value().FindAttribute(XCAFDoc_KinematicPairValue::GetID(), aKPairValueAttr))
+          continue;
+        Handle(TCollection_HAsciiString) aNameOfValue;
+        Handle(TDataStd_Name) aNameAttr;
+        if (anItValue.Value().FindAttribute(TDataStd_Name::GetID(), aNameAttr))
+          aNameOfValue = new TCollection_HAsciiString(aNameAttr->Get());
+        else
+          aNameOfValue = new TCollection_HAsciiString("");
+        Handle(XCAFKinematics_PairValueObject) aPairValueObject = aKPairValueAttr->GetObject();
+        Handle(StepKinematics_PairValue) aPairValue;
+        if (!createKinematicPairValue(aPairValueObject, aPairReprRelationship, aPairValue))
+          continue;
+        aPairValue->SetName(aNameOfValue);
+        aItems->SetValue(aValueInd++, aPairValue);
+      }
+      if (aValueInd == 1)
+        continue;
+      Handle(TCollection_HAsciiString) aNameOfState;
+      Handle(TDataStd_Name) aNameAttr;
+      if (anItState.Value().FindAttribute(TDataStd_Name::GetID(), aNameAttr))
+        aNameOfState = new TCollection_HAsciiString(aNameAttr->Get());
+      else
+        aNameOfState = new TCollection_HAsciiString("");
+      aStateRepr->Init(aNameOfState, aItems, aMechanism->ContextOfItems(), aMechanism);
+      Model->AddWithRefs(aStateRepr);
+    }
   }
-  myGDTPresentationDM->Init(new TCollection_HAsciiString(), aItems, aRC);
-  aModel->AddWithRefs(myGDTPresentationDM);
 
   return Standard_True;
 }
index 8fa2944547937a47d0b89bb8e414cb85da7c752a..abcda5eb29e9d47752aadcf83ad97fa6875bf896 100644 (file)
@@ -63,12 +63,12 @@ void XCAFKinematics_HighOrderPairObject::SetType(const XCAFKinematics_PairType t
     break;
   }
   case XCAFKinematics_PairType_SlidingSurface: {
-    myLimits = NULL;
+    myLimits = new TColStd_HArray1OfReal(1, 2);
     myGeom = NCollection_Array1<Handle(Geom_Geometry)>(1, 2);
     break;
   }
   case XCAFKinematics_PairType_RollingSurface: {
-    myLimits = NULL;
+    myLimits = new TColStd_HArray1OfReal(1, 2);
     myGeom = NCollection_Array1<Handle(Geom_Geometry)>(1, 2);
     break;
   }