Salome HOME
IMP 0021064: correct implementation
[modules/geom.git] / src / GEOMImpl / GEOMImpl_MeasureDriver.cxx
index b9dff3e14f03911794749499f0215316a24a04ae..6d8698259ab39d1f5ceba9867268e84ed529e599 100644 (file)
@@ -1,4 +1,4 @@
-//  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
+//  Copyright (C) 2007-2010  CEA/DEN, EDF R&D, OPEN CASCADE
 //
 //  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
@@ -19,6 +19,7 @@
 //
 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
 //
+
 #include <Standard_Stream.hxx>
 
 #include <GEOMImpl_MeasureDriver.hxx>
 #include <TopAbs.hxx>
 #include <TopoDS.hxx>
 #include <TopoDS_Shape.hxx>
+#include <TopoDS_Edge.hxx>
+#include <TopoDS_Wire.hxx>
+#include <TopExp.hxx>
+#include <TopExp_Explorer.hxx>
+#include <TopTools_IndexedMapOfShape.hxx>
 
 #include <GProp_GProps.hxx>
 #include <GeomLProp_SLProps.hxx>
@@ -108,6 +114,69 @@ Standard_Integer GEOMImpl_MeasureDriver::Execute(TFunction_Logbook& log) const
 
     aShape = BRepBuilderAPI_MakeVertex(aCenterMass).Shape();
   }
+  else if (aType == VERTEX_BY_INDEX)
+  {
+    Handle(GEOM_Function) aRefBase = aCI.GetBase();
+    TopoDS_Shape aShapeBase = aRefBase->GetValue();
+    if (aShapeBase.IsNull()) {
+      Standard_NullObject::Raise("Shape for centre of mass calculation is null");
+    }
+
+    int index = aCI.GetIndex();
+    gp_Pnt aVertex;
+
+    if (aShapeBase.ShapeType() == TopAbs_VERTEX) {
+      if ( index != 1 )
+        Standard_NullObject::Raise("Vertex index is out of range");
+      else
+        aVertex = BRep_Tool::Pnt(TopoDS::Vertex(aShapeBase));
+    } else if (aShapeBase.ShapeType() == TopAbs_EDGE) {
+      TopoDS_Vertex aV1, aV2;
+      TopoDS_Edge anEdgeE = TopoDS::Edge(aShapeBase);
+      
+      TopExp::Vertices(anEdgeE, aV1, aV2);
+      gp_Pnt aP1 = BRep_Tool::Pnt(aV1);
+      gp_Pnt aP2 = BRep_Tool::Pnt(aV2);
+
+      if (index < 0 || index > 1)
+        Standard_NullObject::Raise("Vertex index is out of range");
+
+      if ( ( anEdgeE.Orientation() == TopAbs_FORWARD && index == 0 ) ||
+           ( anEdgeE.Orientation() == TopAbs_REVERSED && index == 1 ) )
+        aVertex = aP1;
+      else
+      aVertex = aP2;
+    } else if (aShapeBase.ShapeType() == TopAbs_WIRE) {
+      TopTools_IndexedMapOfShape anEdgeShapes;
+      TopTools_IndexedMapOfShape aVertexShapes;
+      TopoDS_Vertex aV1, aV2;
+      TopoDS_Wire aWire = TopoDS::Wire(aShapeBase);
+      TopExp_Explorer exp (aWire, TopAbs_EDGE);
+      for (; exp.More(); exp.Next()) {
+        anEdgeShapes.Add(exp.Current());
+        TopoDS_Edge E = TopoDS::Edge(exp.Current());
+        TopExp::Vertices(E, aV1, aV2);
+        if ( aVertexShapes.Extent() == 0)
+          aVertexShapes.Add(aV1);
+        if ( !aV1.IsSame( aVertexShapes(aVertexShapes.Extent()) ) )
+          aVertexShapes.Add(aV1);
+        if ( !aV2.IsSame( aVertexShapes(aVertexShapes.Extent()) ) )
+          aVertexShapes.Add(aV2);
+      }
+
+      if (index < 0 || index > aVertexShapes.Extent())
+        Standard_NullObject::Raise("Vertex index is out of range");
+
+      if (aWire.Orientation() == TopAbs_FORWARD)
+        aVertex = BRep_Tool::Pnt(TopoDS::Vertex(aVertexShapes(index+1)));
+      else
+        aVertex = BRep_Tool::Pnt(TopoDS::Vertex(aVertexShapes(aVertexShapes.Extent() - index)));
+    } else {
+      Standard_NullObject::Raise("Shape for vertex calculation is not an edge or wire");
+    }
+
+    aShape = BRepBuilderAPI_MakeVertex(aVertex).Shape();
+  }
   else if (aType == VECTOR_FACE_NORMALE)
   {
     // Face
@@ -147,6 +216,17 @@ Standard_Integer GEOMImpl_MeasureDriver::Execute(TFunction_Logbook& log) const
     gp_Vec Vec1,Vec2;
     BRepAdaptor_Surface SF (aFace);
     SF.D1(pUV.X(), pUV.Y(), p1, Vec1, Vec2);
+    if (Vec1.Magnitude() < Precision::Confusion()) {
+      gp_Vec tmpV;
+      gp_Pnt tmpP;
+      SF.D1(pUV.X(), pUV.Y()-0.1, tmpP, Vec1, tmpV);
+    }
+    else if (Vec2.Magnitude() < Precision::Confusion()) {
+      gp_Vec tmpV;
+      gp_Pnt tmpP;
+      SF.D1(pUV.X()-0.1, pUV.Y(), tmpP, tmpV, Vec2);
+    }
+
     gp_Vec V = Vec1.Crossed(Vec2);
     Standard_Real mod = V.Magnitude();
     if (mod < Precision::Confusion())
@@ -159,7 +239,7 @@ Standard_Integer GEOMImpl_MeasureDriver::Execute(TFunction_Logbook& log) const
       Standard_Real radius1 = Abs(aProperties.MinCurvature());
       Standard_Real radius2 = Abs(aProperties.MaxCurvature());
       if (Abs(radius1) > Precision::Confusion()) {
-       radius = 1.0 / radius1;
+        radius = 1.0 / radius1;
         if (Abs(radius2) > Precision::Confusion()) {
           radius = (radius + 1.0 / radius2) / 2.0;
         }
@@ -229,10 +309,10 @@ Standard_EXPORT Handle_Standard_Type& GEOMImpl_MeasureDriver_Type_()
 
   static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
   static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_MeasureDriver",
-                                                        sizeof(GEOMImpl_MeasureDriver),
-                                                        1,
-                                                        (Standard_Address)_Ancestors,
-                                                        (Standard_Address)NULL);
+                                                         sizeof(GEOMImpl_MeasureDriver),
+                                                         1,
+                                                         (Standard_Address)_Ancestors,
+                                                         (Standard_Address)NULL);
 
   return _aType;
 }