]> SALOME platform Git repositories - modules/geom.git/blobdiff - src/GEOMImpl/GEOMImpl_MeasureDriver.cxx
Salome HOME
Merge remote branch 'origin/gdd/translations'
[modules/geom.git] / src / GEOMImpl / GEOMImpl_MeasureDriver.cxx
index 69ffeabd874f4aa2a154569c45a43785ba6ed031..b004dfe9b52611f92f854a8534bd0adf2c34b3cf 100644 (file)
@@ -1,18 +1,20 @@
-// Copyright (C) 2005  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
+// Copyright (C) 2007-2015  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
-// 
+//
 // This library is free software; you can redistribute it and/or
 // modify it under the terms of the GNU Lesser General Public
-// License as published by the Free Software Foundation; either 
-// version 2.1 of the License.
-// 
-// This library is distributed in the hope that it will be useful 
-// but WITHOUT ANY WARRANTY; without even the implied warranty of 
-// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
+// License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 // Lesser General Public License for more details.
 //
-// You should have received a copy of the GNU Lesser General Public  
-// License along with this library; if not, write to the Free Software 
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 //
 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
 
 #include <GEOMImpl_MeasureDriver.hxx>
 #include <GEOMImpl_IMeasure.hxx>
-#include <GEOMImpl_IMeasureOperations.hxx>
 #include <GEOMImpl_Types.hxx>
+
 #include <GEOM_Function.hxx>
 
+#include <GEOMUtils.hxx>
+
 #include <BRep_Tool.hxx>
+#include <BRepTools.hxx>
 #include <BRepGProp.hxx>
-#include <BRepBuilderAPI_MakeVertex.hxx>
+#include <BRepBuilderAPI_Copy.hxx>
 #include <BRepBuilderAPI_MakeEdge.hxx>
+#include <BRepBuilderAPI_MakeVertex.hxx>
+#include <BRepPrimAPI_MakeBox.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>
@@ -47,6 +59,7 @@
 #include <gp_Pnt.hxx>
 #include <Precision.hxx>
 #include <Standard_NullObject.hxx>
+#include <StdFail_NotDone.hxx>
 
 //=======================================================================
 //function : GetID
@@ -82,6 +95,57 @@ Standard_Integer GEOMImpl_MeasureDriver::Execute(TFunction_Logbook& log) const
   TopoDS_Shape aShape;
 
   if (aType == CDG_MEASURE)
+  {
+    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");
+
+    gp_Ax3 aPos = GEOMUtils::GetPosition(aShapeBase);
+    gp_Pnt aCenterMass = aPos.Location();
+    aShape = BRepBuilderAPI_MakeVertex(aCenterMass).Shape();
+  }
+  else if (aType == BND_BOX_MEASURE || aType == BND_BOX_MEASURE_PRECISE)
+  {
+    Handle(GEOM_Function) aRefBase = aCI.GetBase();
+    TopoDS_Shape aShapeBase = aRefBase->GetValue();
+    if (aShapeBase.IsNull())
+      Standard_NullObject::Raise("Shape for bounding box calculation is null");
+
+    BRepBuilderAPI_Copy aCopyTool (aShapeBase);
+    if (!aCopyTool.IsDone())
+      Standard_NullObject::Raise("Shape for bounding box calculation is bad");
+
+    aShapeBase = aCopyTool.Shape();
+
+    // remove triangulation to obtain more exact boundaries
+    BRepTools::Clean(aShapeBase);
+
+    Bnd_Box B;
+    BRepBndLib::Add(aShapeBase, B);
+
+    if (aType == BND_BOX_MEASURE_PRECISE) {
+      if (!GEOMUtils::PreciseBoundingBox(aShapeBase, B)) {
+        Standard_NullObject::Raise("Bounding box cannot be precised");
+      }
+    }
+
+    Standard_Real Xmin, Ymin, Zmin, Xmax, Ymax, Zmax;
+    B.Get(Xmin, Ymin, Zmin, Xmax, Ymax, Zmax);
+
+    if (Xmax - Xmin < Precision::Confusion()) Xmax += Precision::Confusion();
+    if (Ymax - Ymin < Precision::Confusion()) Ymax += Precision::Confusion();
+    if (Zmax - Zmin < Precision::Confusion()) Zmax += Precision::Confusion();
+
+    gp_Pnt P1 (Xmin, Ymin, Zmin);
+    gp_Pnt P2 (Xmax, Ymax, Zmax);
+
+    BRepPrimAPI_MakeBox MB (P1, P2);
+    MB.Build();
+    if (!MB.IsDone()) StdFail_NotDone::Raise("Bounding box cannot be computed from the given shape");
+    aShape = MB.Shape();
+  }
+  else if (aType == VERTEX_BY_INDEX)
   {
     Handle(GEOM_Function) aRefBase = aCI.GetBase();
     TopoDS_Shape aShapeBase = aRefBase->GetValue();
@@ -89,23 +153,60 @@ Standard_Integer GEOMImpl_MeasureDriver::Execute(TFunction_Logbook& log) const
       Standard_NullObject::Raise("Shape for centre of mass calculation is null");
     }
 
-    GProp_GProps aSystem;
-    gp_Pnt aCenterMass;
+    int index = aCI.GetIndex();
+    gp_Pnt aVertex;
 
     if (aShapeBase.ShapeType() == TopAbs_VERTEX) {
-      aCenterMass = BRep_Tool::Pnt(TopoDS::Vertex(aShapeBase));
-    } else if (aShapeBase.ShapeType() == TopAbs_EDGE || aShapeBase.ShapeType() == TopAbs_WIRE) {
-      BRepGProp::LinearProperties(aShapeBase, aSystem);
-      aCenterMass = aSystem.CentreOfMass();
-    } else if (aShapeBase.ShapeType() == TopAbs_FACE || aShapeBase.ShapeType() == TopAbs_SHELL) {
-      BRepGProp::SurfaceProperties(aShapeBase, aSystem);
-      aCenterMass = aSystem.CentreOfMass();
+      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 {
-      BRepGProp::VolumeProperties(aShapeBase, aSystem);
-      aCenterMass = aSystem.CentreOfMass();
+      Standard_NullObject::Raise("Shape for vertex calculation is not an edge or wire");
     }
 
-    aShape = BRepBuilderAPI_MakeVertex(aCenterMass).Shape();
+    aShape = BRepBuilderAPI_MakeVertex(aVertex).Shape();
   }
   else if (aType == VECTOR_FACE_NORMALE)
   {
@@ -133,7 +234,7 @@ Standard_Integer GEOMImpl_MeasureDriver::Execute(TFunction_Logbook& log) const
     }
     else
     {
-      gp_Ax3 aPos = GEOMImpl_IMeasureOperations::GetPosition(aFace);
+      gp_Ax3 aPos = GEOMUtils::GetPosition(aFace);
       p1 = aPos.Location();
     }
 
@@ -146,6 +247,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())
@@ -158,7 +270,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;
         }
@@ -210,45 +322,48 @@ Standard_Integer GEOMImpl_MeasureDriver::Execute(TFunction_Logbook& log) const
   return 1;
 }
 
+//================================================================================
+/*!
+ * \brief Returns a name of creation operation and names and values of creation parameters
+ */
+//================================================================================
 
-//=======================================================================
-//function :  GEOMImpl_MeasureDriver_Type_
-//purpose  :
-//======================================================================= 
-Standard_EXPORT Handle_Standard_Type& GEOMImpl_MeasureDriver_Type_()
-{
-
-  static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
-  if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
-  static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
-  if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared); 
-  static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
-  if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
-
-  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);
-
-  return _aType;
-}
-
-//=======================================================================
-//function : DownCast
-//purpose  :
-//======================================================================= 
-const Handle(GEOMImpl_MeasureDriver) Handle(GEOMImpl_MeasureDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
+bool GEOMImpl_MeasureDriver::
+GetCreationInformation(std::string&             theOperationName,
+                       std::vector<GEOM_Param>& theParams)
 {
-  Handle(GEOMImpl_MeasureDriver) _anOtherObject;
-
-  if (!AnObject.IsNull()) {
-     if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_MeasureDriver))) {
-       _anOtherObject = Handle(GEOMImpl_MeasureDriver)((Handle(GEOMImpl_MeasureDriver)&)AnObject);
-     }
+  if (Label().IsNull()) return 0;
+  Handle(GEOM_Function) function = GEOM_Function::GetFunction(Label());
+
+  GEOMImpl_IMeasure aCI( function );
+  Standard_Integer aType = function->GetType();
+
+  switch ( aType ) {
+  case CDG_MEASURE:
+    theOperationName = "MASS_CENTER";
+    AddParam( theParams, "Object", aCI.GetBase() );
+    break;
+  case BND_BOX_MEASURE:
+  case BND_BOX_MEASURE_PRECISE:
+    theOperationName = "BND_BOX";
+    AddParam( theParams, "Object", aCI.GetBase() );
+    break;
+  case VERTEX_BY_INDEX:
+    theOperationName = "GetVertexByIndex";
+    AddParam( theParams, "Object", aCI.GetBase() );
+    AddParam( theParams, "Index", aCI.GetIndex() );
+    break;
+  case VECTOR_FACE_NORMALE:
+    theOperationName = "NORMALE";
+    AddParam( theParams, "Face", aCI.GetBase() );
+    AddParam( theParams, "Point", aCI.GetPoint(), "face center" );
+    break;
+  default:
+    return false;
   }
 
-  return _anOtherObject ;
+  return true;
 }
+IMPLEMENT_STANDARD_HANDLE (GEOMImpl_MeasureDriver,GEOM_BaseDriver);
+
+IMPLEMENT_STANDARD_RTTIEXT (GEOMImpl_MeasureDriver,GEOM_BaseDriver);