Salome HOME
Merge branch 'Dev_0.7.1' of newgeom:newgeom into Dev_0.7.1
[modules/shaper.git] / src / PartSet / PartSet_Tools.cpp
index f6e8ae84fe8645128b6b020f056cf3e409b41207..0b07da7e75db47c670207a7138059a3b9048c0fe 100644 (file)
@@ -1,3 +1,5 @@
+// Copyright (C) 2014-20xx CEA/DEN, EDF R&D
+
 // File:        PartSet_Tools.h
 // Created:     28 Apr 2014
 // Author:      Natalia ERMOLAEVA
@@ -18,6 +20,7 @@
 #include <GeomAPI_Pnt2d.h>
 #include <GeomAPI_Pnt.h>
 #include <GeomAPI_Edge.h>
+#include <GeomAPI_Vertex.h>
 
 #include <GeomAPI_Dir.h>
 #include <GeomAPI_XYZ.h>
@@ -33,6 +36,7 @@
 #include <SketchPlugin_Circle.h>
 #include <SketchPlugin_Arc.h>
 #include <SketchPlugin_Line.h>
+#include <SketchPlugin_Point.h>
 
 #include <ModuleBase_ViewerPrs.h>
 
@@ -304,6 +308,35 @@ void PartSet_Tools::createConstraint(CompositeFeaturePtr theSketch,
     aFeature->execute();
 }
 
+std::shared_ptr<GeomDataAPI_Point2D> PartSet_Tools::
+  findAttributePoint(CompositeFeaturePtr theSketch, double theX, double theY,
+  double theTolerance, const QList<FeaturePtr>& theIgnore)
+{
+  std::shared_ptr<GeomAPI_Pnt2d> aClickedPoint = std::shared_ptr<GeomAPI_Pnt2d>(
+      new GeomAPI_Pnt2d(theX, theY));
+
+  std::list<std::shared_ptr<ModelAPI_Attribute> > anAttiributes;
+  for (int i = 0; i < theSketch->numberOfSubs(); i++) {
+    FeaturePtr aFeature = theSketch->subFeature(i);
+    if (!theIgnore.contains(aFeature)) {
+      anAttiributes = aFeature->data()->attributes(GeomDataAPI_Point2D::type());
+
+      std::list<std::shared_ptr<ModelAPI_Attribute> >::const_iterator anIt;
+      for (anIt = anAttiributes.cbegin(); anIt != anAttiributes.cend(); ++anIt) {
+        std::shared_ptr<GeomDataAPI_Point2D> aCurPoint = 
+          std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*anIt);
+        double x = aCurPoint->x();
+        double y = aCurPoint->y();
+        if (aCurPoint && (aCurPoint->pnt()->distance(aClickedPoint) < theTolerance)) {
+          return aCurPoint;
+        }
+      }
+    }
+  }
+  return std::shared_ptr<GeomDataAPI_Point2D>();
+}
+
+
 void PartSet_Tools::setConstraints(CompositeFeaturePtr theSketch, FeaturePtr theFeature,
                                    const std::string& theAttribute, double theClickedX,
                                    double theClickedY)
@@ -334,10 +367,12 @@ void PartSet_Tools::setConstraints(CompositeFeaturePtr theSketch, FeaturePtr the
         aLast = anAttiributes.end();
     std::shared_ptr<GeomDataAPI_Point2D> aFPoint;
     for (; anIt != aLast && !aFPoint; anIt++) {
-      std::shared_ptr<GeomDataAPI_Point2D> aCurPoint = std::dynamic_pointer_cast<
-          GeomDataAPI_Point2D>(*anIt);
-      if (aCurPoint && aCurPoint->pnt()->distance(aClickedPoint) < Precision::Confusion())
+      std::shared_ptr<GeomDataAPI_Point2D> aCurPoint = 
+        std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*anIt);
+      if (aCurPoint && (aCurPoint->pnt()->distance(aClickedPoint) < Precision::Confusion())) {
         aFPoint = aCurPoint;
+        break;
+      }
     }
     if (aFPoint)
       PartSet_Tools::createConstraint(theSketch, aFPoint, aFeaturePoint);
@@ -349,23 +384,25 @@ std::shared_ptr<GeomAPI_Pln> PartSet_Tools::sketchPlane(CompositeFeaturePtr theS
   std::shared_ptr<GeomAPI_Pln> aPlane;
 
   std::shared_ptr<ModelAPI_Data> aData = theSketch->data();
-  std::shared_ptr<GeomDataAPI_Point> anOrigin = std::dynamic_pointer_cast<GeomDataAPI_Point>(
-      aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
-  std::shared_ptr<GeomDataAPI_Dir> aNormal = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
-      aData->attribute(SketchPlugin_Sketch::NORM_ID()));
-  if (aNormal && anOrigin) {
-    double adX = aNormal->x();
-    double adY = aNormal->y();
-    double adZ = aNormal->z();
-
-    if ( (adX != 0) || (adY != 0) || (adZ != 0) ) { // Plane is valid
-      double aX = anOrigin->x();
-      double aY = anOrigin->y();
-      double aZ = anOrigin->z();
-      gp_Pln aPln(gp_Pnt(aX, aY, aZ), gp_Dir(adX, adY, adZ));
-      double aA, aB, aC, aD;
-      aPln.Coefficients(aA, aB, aC, aD);
-      aPlane = std::shared_ptr<GeomAPI_Pln>(new GeomAPI_Pln(aA, aB, aC, aD));
+  if (aData) {
+    std::shared_ptr<GeomDataAPI_Point> anOrigin = std::dynamic_pointer_cast<GeomDataAPI_Point>(
+        aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
+    std::shared_ptr<GeomDataAPI_Dir> aNormal = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
+        aData->attribute(SketchPlugin_Sketch::NORM_ID()));
+    if (aNormal && anOrigin) {
+      double adX = aNormal->x();
+      double adY = aNormal->y();
+      double adZ = aNormal->z();
+
+      if ( (adX != 0) || (adY != 0) || (adZ != 0) ) { // Plane is valid
+        double aX = anOrigin->x();
+        double aY = anOrigin->y();
+        double aZ = anOrigin->z();
+        gp_Pln aPln(gp_Pnt(aX, aY, aZ), gp_Dir(adX, adY, adZ));
+        double aA, aB, aC, aD;
+        aPln.Coefficients(aA, aB, aC, aD);
+        aPlane = std::shared_ptr<GeomAPI_Pln>(new GeomAPI_Pln(aA, aB, aC, aD));
+      }
     }
   }
   return aPlane;
@@ -400,56 +437,87 @@ ResultPtr PartSet_Tools::createFixedObjectByEdge(const TopoDS_Shape& theShape,
                                                  const ObjectPtr& theObject, 
                                                  CompositeFeaturePtr theSketch)
 {
-  if (theShape.ShapeType() != TopAbs_EDGE)
-    return ResultPtr();
-
-  // Check that we already have such external edge
-  std::shared_ptr<GeomAPI_Edge> aInEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge());
-  aInEdge->setImpl(new TopoDS_Shape(theShape));
-  ResultPtr aResult = findExternalEdge(theSketch, aInEdge);
-  if (aResult)
-    return aResult;
-
-  // If not found then we have to create new
-  Standard_Real aStart, aEnd;
-  Handle(V3d_View) aNullView;
-  FeaturePtr aMyFeature;
-
-  Handle(Geom_Curve) aCurve = BRep_Tool::Curve(TopoDS::Edge(theShape), aStart, aEnd);
-  GeomAdaptor_Curve aAdaptor(aCurve);
-  if (aAdaptor.GetType() == GeomAbs_Line) {
-    // Create line
-    aMyFeature = theSketch->addFeature(SketchPlugin_Line::ID());
-  } else if (aAdaptor.GetType() == GeomAbs_Circle) {
-    if (aAdaptor.IsClosed()) {
-      // Create circle
-      aMyFeature = theSketch->addFeature(SketchPlugin_Circle::ID());
-    } else {
-      // Create arc
-      aMyFeature = theSketch->addFeature(SketchPlugin_Arc::ID());
+  if (theShape.ShapeType() == TopAbs_EDGE) {
+    // Check that we already have such external edge
+    std::shared_ptr<GeomAPI_Edge> aInEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge());
+    aInEdge->setImpl(new TopoDS_Shape(theShape));
+    ResultPtr aResult = findExternalEdge(theSketch, aInEdge);
+    if (aResult)
+      return aResult;
+
+    // If not found then we have to create new
+    Standard_Real aStart, aEnd;
+    Handle(V3d_View) aNullView;
+    FeaturePtr aMyFeature;
+
+    Handle(Geom_Curve) aCurve = BRep_Tool::Curve(TopoDS::Edge(theShape), aStart, aEnd);
+    GeomAdaptor_Curve aAdaptor(aCurve);
+    if (aAdaptor.GetType() == GeomAbs_Line) {
+      // Create line
+      aMyFeature = theSketch->addFeature(SketchPlugin_Line::ID());
+    } else if (aAdaptor.GetType() == GeomAbs_Circle) {
+      if (aAdaptor.IsClosed()) {
+        // Create circle
+        aMyFeature = theSketch->addFeature(SketchPlugin_Circle::ID());
+      } else {
+        // Create arc
+        aMyFeature = theSketch->addFeature(SketchPlugin_Arc::ID());
+      }
     }
-  }
-  if (aMyFeature) {
-    DataPtr aData = aMyFeature->data();
-    AttributeSelectionPtr anAttr = 
-      std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
-      (aData->attribute(SketchPlugin_Feature::EXTERNAL_ID()));
+    if (aMyFeature) {
+      DataPtr aData = aMyFeature->data();
+      AttributeSelectionPtr anAttr = 
+        std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
+        (aData->attribute(SketchPlugin_Feature::EXTERNAL_ID()));
 
-    ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
-    if (anAttr && aRes) {
-      std::shared_ptr<GeomAPI_Shape> anEdge(new GeomAPI_Shape);
-      anEdge->setImpl(new TopoDS_Shape(theShape));
+      ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
+      if (anAttr && aRes) {
+        std::shared_ptr<GeomAPI_Shape> anEdge(new GeomAPI_Shape);
+        anEdge->setImpl(new TopoDS_Shape(theShape));
 
-      anAttr->setValue(aRes, anEdge);
+        anAttr->setValue(aRes, anEdge);
 
-      aMyFeature->execute();
+        aMyFeature->execute();
 
-      // fix this edge
-      FeaturePtr aFix = theSketch->addFeature(SketchPlugin_ConstraintRigid::ID());
-      aFix->data()->refattr(SketchPlugin_Constraint::ENTITY_A())->
-        setObject(aMyFeature->lastResult());
+        // fix this edge
+        FeaturePtr aFix = theSketch->addFeature(SketchPlugin_ConstraintRigid::ID());
+        aFix->data()->refattr(SketchPlugin_Constraint::ENTITY_A())->
+          setObject(aMyFeature->lastResult());
 
-      return aMyFeature->lastResult();
+        return aMyFeature->lastResult();
+      }
+    }
+  }
+  if (theShape.ShapeType() == TopAbs_VERTEX) {
+    std::shared_ptr<GeomAPI_Vertex> aInVert = std::shared_ptr<GeomAPI_Vertex>(new GeomAPI_Vertex());
+    aInVert->setImpl(new TopoDS_Shape(theShape));
+    ResultPtr aResult = findExternalVertex(theSketch, aInVert);
+    if (aResult)
+      return aResult;
+
+    FeaturePtr aMyFeature = theSketch->addFeature(SketchPlugin_Point::ID());
+
+    if (aMyFeature) {
+      DataPtr aData = aMyFeature->data();
+      AttributeSelectionPtr anAttr = 
+        std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
+        (aData->attribute(SketchPlugin_Feature::EXTERNAL_ID()));
+
+      ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
+      if (anAttr && aRes) {
+        std::shared_ptr<GeomAPI_Shape> aVert(new GeomAPI_Shape);
+        aVert->setImpl(new TopoDS_Shape(theShape));
+
+        anAttr->setValue(aRes, aVert);
+        aMyFeature->execute();
+
+        // fix this edge
+        FeaturePtr aFix = theSketch->addFeature(SketchPlugin_ConstraintRigid::ID());
+        aFix->data()->refattr(SketchPlugin_Constraint::ENTITY_A())->
+          setObject(aMyFeature->lastResult());
+
+        return aMyFeature->lastResult();
+      }
     }
   }
   return ResultPtr();
@@ -492,6 +560,35 @@ ResultPtr PartSet_Tools::findExternalEdge(CompositeFeaturePtr theSketch, std::sh
   return ResultPtr();
 }
 
+
+ResultPtr PartSet_Tools::findExternalVertex(CompositeFeaturePtr theSketch, std::shared_ptr<GeomAPI_Vertex> theVert)
+{
+  for (int i = 0; i < theSketch->numberOfSubs(); i++) {
+    FeaturePtr aFeature = theSketch->subFeature(i);
+    std::shared_ptr<SketchPlugin_Feature> aSketchFea = 
+      std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
+    if (aSketchFea) {
+      if (aSketchFea->isExternal()) {
+        std::list<ResultPtr> aResults = aSketchFea->results();
+        std::list<ResultPtr>::const_iterator aIt;
+        for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
+          ResultConstructionPtr aRes = 
+            std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aIt);
+          if (aRes) {
+            std::shared_ptr<GeomAPI_Shape> aShape = aRes->shape();
+            if (aShape) {
+              if (theVert->isEqual(aShape))
+                return aRes;
+            }
+          }
+        }
+      }
+    }
+  }
+  return ResultPtr();
+}
+
+
 bool PartSet_Tools::hasVertexShape(const ModuleBase_ViewerPrs& thePrs, FeaturePtr theSketch,
                                    Handle_V3d_View theView, double& theX, double& theY)
 {