Salome HOME
Issue #2027 Sketcher Trim Feature
[modules/shaper.git] / src / ModelGeomAlgo / ModelGeomAlgo_Point2D.cpp
index f9fa6030de2a65e6ce75afe4a8b6e59ed1f7ce19..e65a2786e312b2fc8f47d068cfe3f12fb5a207f3 100755 (executable)
@@ -9,6 +9,10 @@
 #include <ModelAPI_Feature.h>
 #include <ModelAPI_Result.h>
 #include <ModelAPI_AttributeRefAttr.h>
+#include <ModelAPI_CompositeFeature.h>
+#include <ModelAPI_Tools.h>
+
+#include <GeomAPI_ShapeIterator.h>
 
 #include <GeomAlgoAPI_ShapeTools.h>
 #include <GeomDataAPI_Point2D.h>
@@ -119,7 +123,119 @@ namespace ModelGeomAlgo_Point2D {
     }
   }
 
+  void appendPoint(const std::shared_ptr<GeomAPI_Pnt>& thePoint,
+                   const std::shared_ptr<ModelAPI_Result>& theResult,
+                   PointToRefsMap& thePointToAttributeOrObject)
+  {
+    if (thePointToAttributeOrObject.find(thePoint) != thePointToAttributeOrObject.end())
+      thePointToAttributeOrObject.at(thePoint).second.push_back(theResult);
+    else {
+      std::list<std::shared_ptr<GeomDataAPI_Point2D> > anAttributes;
+      std::list<std::shared_ptr<ModelAPI_Object> > anObjects;
+      anObjects.push_back(theResult);
+      thePointToAttributeOrObject[thePoint] = std::make_pair(anAttributes, anObjects);
+    }
+  }
+
+  void appendShapePoints(GeomShapePtr& theShape,
+                         const std::shared_ptr<ModelAPI_Result>& theResult,
+                         PointToRefsMap& thePointToAttributeOrObject)
+  {
+    if (!theShape.get())
+      return;
+
+    switch (theShape->shapeType()) {
+      case GeomAPI_Shape::VERTEX: {
+        std::shared_ptr<GeomAPI_Vertex> aVertex =
+          std::shared_ptr<GeomAPI_Vertex>(new GeomAPI_Vertex(theShape));
+        std::shared_ptr<GeomAPI_Pnt> aPnt = aVertex->point();
+        appendPoint(aPnt, theResult, thePointToAttributeOrObject);
+      }
+      break;
+      case GeomAPI_Shape::EDGE: {
+        std::shared_ptr<GeomAPI_Edge> anEdge =
+          std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(theShape));
+        appendPoint(anEdge->firstPoint(), theResult, thePointToAttributeOrObject);
+        appendPoint(anEdge->lastPoint(), theResult, thePointToAttributeOrObject);
+      }
+      break;
+      case GeomAPI_Shape::COMPOUND: {
+        for(GeomAPI_ShapeIterator anIt(theShape); anIt.more(); anIt.next()) {
+          appendShapePoints(anIt.current(), theResult, thePointToAttributeOrObject);
+        }
+      }
+      break;
+      default: break;
+    }
+  }
+
+  void getPointsIntersectedShape(const std::shared_ptr<ModelAPI_Feature>& theBaseFeature,
+                        const std::list<std::shared_ptr<ModelAPI_Feature> >& theFeatures,
+                        PointToRefsMap& thePointToAttributeOrObject)
+  {
+    GeomShapePtr aFeatureShape;
+    {
+      std::set<ResultPtr> anEdgeShapes;
+      ModelAPI_Tools::shapesOfType(theBaseFeature, GeomAPI_Shape::EDGE, anEdgeShapes);
+      if (anEdgeShapes.empty())
+        return;
+      aFeatureShape = (*anEdgeShapes.begin())->shape();
+    }
+
+    std::list<std::shared_ptr<ModelAPI_Feature> >::const_iterator anIt = theFeatures.begin(),
+                                                                  aLast = theFeatures.end();
+    for (; anIt != aLast; anIt++) {
+      FeaturePtr aFeature = *anIt;
+      if (aFeature.get() == theBaseFeature.get())
+        continue;
+      if (aFeature.get()) {
+        std::set<ResultPtr> anEdgeShapes;
+        ModelAPI_Tools::shapesOfType(aFeature, GeomAPI_Shape::EDGE, anEdgeShapes);
+        if (anEdgeShapes.empty())
+          continue;
+        ResultPtr aResult = *anEdgeShapes.begin();
+        GeomShapePtr aShape = aResult->shape();
+
+        GeomShapePtr aShapeOfIntersection = aFeatureShape->intersect(aShape);
+        appendShapePoints(aShapeOfIntersection, aResult, thePointToAttributeOrObject);
+      }
+    }
+  }
+
   void getPointsInsideShape(const std::shared_ptr<GeomAPI_Shape> theBaseShape,
+                        const std::set<std::shared_ptr<GeomDataAPI_Point2D> >& theAttributes,
+                        const std::shared_ptr<GeomAPI_Pnt>& theOrigin,
+                        const std::shared_ptr<GeomAPI_Dir>& theDirX,
+                        const std::shared_ptr<GeomAPI_Dir>& theDirY,
+                        PointToRefsMap& thePointToAttributeOrObject)
+  {
+    std::set<std::shared_ptr<GeomDataAPI_Point2D> >::const_iterator anIt = theAttributes.begin(),
+                                                            aLast = theAttributes.end();
+    for (; anIt != aLast; anIt++) {
+      std::shared_ptr<GeomDataAPI_Point2D> anAttribute = *anIt;
+      std::shared_ptr<GeomAPI_Pnt2d> aPnt2d = anAttribute->pnt();
+      std::shared_ptr<GeomAPI_Pnt> aPoint = aPnt2d->to3D(theOrigin, theDirX, theDirY);
+      std::shared_ptr<GeomAPI_Pnt> aProjectedPoint;
+      if (isPointOnEdge(theBaseShape, aPoint, aProjectedPoint)) {
+        if (thePointToAttributeOrObject.find(aProjectedPoint) != thePointToAttributeOrObject.end())
+          thePointToAttributeOrObject.at(aProjectedPoint).first.push_back(anAttribute);
+        else {
+          //std::list< std::shared_ptr<GeomDataAPI_Point2D> > anAttributes;
+          //anAttributes.push_back(anAttribute);
+          //thePointToAttributeOrObject.insert(aProjectedPoint, anAttributes);
+
+          std::list<std::shared_ptr<GeomDataAPI_Point2D> > anAttributes;
+          std::list<std::shared_ptr<ModelAPI_Object> > anObjects;
+          anAttributes.push_back(anAttribute);
+          thePointToAttributeOrObject[aProjectedPoint] = std::make_pair(anAttributes, anObjects);
+        }
+        //thePoints.push_back(aProjectedPoint);
+        //theAttributeToPoint[anAttribute] = aProjectedPoint;
+      }
+    }
+  }
+
+  void getPointsInsideShape_p(const std::shared_ptr<GeomAPI_Shape> theBaseShape,
                             const std::set<std::shared_ptr<GeomDataAPI_Point2D> >& theAttributes,
                             const std::shared_ptr<GeomAPI_Pnt>& theOrigin,
                             const std::shared_ptr<GeomAPI_Dir>& theDirX,