Salome HOME
Issue #355 Delete: elements of sketch and constraints
[modules/shaper.git] / src / PartSet / PartSet_Tools.cpp
index 78df67edb61f3a0f9a98e7400a2e967329eb67e8..0b07da7e75db47c670207a7138059a3b9048c0fe 100644 (file)
@@ -308,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)
@@ -338,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);
@@ -353,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;