Salome HOME
Change icons for chamfer
[modules/shaper.git] / src / SketchPlugin / SketchPlugin_Tools.cpp
index 7a3895a34e38990d561099810e75f8fe9f1b4981..4f55be24d485ffb1d9997890f99f5da42e708fcd 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2014-2017  CEA/DEN, EDF R&D
+// Copyright (C) 2014-2019  CEA/DEN, EDF R&D
 //
 // This library is free software; you can redistribute it and/or
 // modify it under the terms of the GNU Lesser General Public
 //
 // 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
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 //
-// See http://www.salome-platform.org/ or
-// email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
+// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
 //
 
 #include "SketchPlugin_Tools.h"
 
 #include "SketchPlugin_ConstraintCoincidence.h"
+#include "SketchPlugin_ConstraintLength.h"
 #include "SketchPlugin_ConstraintTangent.h"
+#include "SketchPlugin_Line.h"
 #include "SketchPlugin_Point.h"
 #include "SketchPlugin_SketchEntity.h"
 
 
 #include <ModelAPI_AttributeDouble.h>
 
+#include <GeomAPI_Dir2d.h>
+#include <GeomAPI_Pnt2d.h>
+#include <GeomAPI_XY.h>
+
 #include <GeomDataAPI_Point.h>
 #include <GeomDataAPI_Point2D.h>
 
@@ -100,7 +105,7 @@ std::set<FeaturePtr> findCoincidentConstraints(const FeaturePtr& theFeature)
   std::set<AttributePtr>::const_iterator aIt;
   for (aIt = aRefsList.cbegin(); aIt != aRefsList.cend(); ++aIt) {
     FeaturePtr aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>((*aIt)->owner());
-    if (aConstrFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID())
+    if (aConstrFeature && aConstrFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID())
       aCoincident.insert(aConstrFeature);
   }
   return aCoincident;
@@ -185,11 +190,14 @@ public:
     std::list< std::set<AttributePoint2DPtr> >::iterator aFound1 = find(thePoint1);
     std::list< std::set<AttributePoint2DPtr> >::iterator aFound2 = find(thePoint2);
     if (aFound1 == myCoincidentPoints.end()) {
-      std::set<AttributePoint2DPtr> aNewSet;
-      aNewSet.insert(thePoint1);
-      if (thePoint2)
-        aNewSet.insert(thePoint2);
-      myCoincidentPoints.push_back(aNewSet);
+      if (aFound2 == myCoincidentPoints.end()) {
+        std::set<AttributePoint2DPtr> aNewSet;
+        aNewSet.insert(thePoint1);
+        if (thePoint2)
+          aNewSet.insert(thePoint2);
+        myCoincidentPoints.push_back(aNewSet);
+      } else
+        aFound2->insert(thePoint1);
     } else if (aFound2 == myCoincidentPoints.end()) {
       if (thePoint2)
         aFound1->insert(thePoint2);
@@ -201,6 +209,8 @@ public:
 
   std::set<AttributePoint2DPtr> coincidentPoints(const AttributePoint2DPtr& thePoint)
   {
+    collectCoincidentPoints(thePoint);
+
     std::list< std::set<AttributePoint2DPtr> >::iterator aFound = find(thePoint);
     if (aFound == myCoincidentPoints.end())
       return std::set<AttributePoint2DPtr>();
@@ -208,6 +218,55 @@ public:
   }
 
 private:
+  void coincidences(const FeaturePtr& theFeature,
+                    std::set<FeaturePtr>& theCoincidences) const
+  {
+    // iterate through coincideces for the given feature
+    std::set<FeaturePtr> aCoincidences = SketchPlugin_Tools::findCoincidentConstraints(theFeature);
+    std::set<FeaturePtr>::const_iterator aCIt = aCoincidences.begin();
+    for (; aCIt != aCoincidences.end(); ++aCIt)
+    {
+      if (theCoincidences.find(*aCIt) != theCoincidences.end())
+        continue; // already processed
+      theCoincidences.insert(*aCIt);
+      // iterate on coincident attributes
+      for (int i = 0, aPtInd = 0; i < CONSTRAINT_ATTR_SIZE; ++i) {
+        AttributeRefAttrPtr aRefAttr = (*aCIt)->refattr(SketchPlugin_Constraint::ATTRIBUTE(i));
+        if (aRefAttr && !aRefAttr->isObject())
+        {
+          FeaturePtr anOwner = ModelAPI_Feature::feature(aRefAttr->attr()->owner());
+          if (anOwner != theFeature)
+            coincidences(anOwner, theCoincidences);
+        }
+      }
+    }
+  }
+
+  // Iteratively search points coincident to the given point
+  // (two points may be coincident through the third point)
+  void collectCoincidentPoints(const AttributePoint2DPtr& thePoint)
+  {
+    AttributePoint2DPtr aPoints[2];
+
+    FeaturePtr anOwner = ModelAPI_Feature::feature(thePoint->owner());
+    std::set<FeaturePtr> aCoincidences;
+    coincidences(anOwner, aCoincidences);
+
+    std::set<FeaturePtr>::const_iterator aCIt = aCoincidences.begin();
+    for (; aCIt != aCoincidences.end(); ++aCIt) {
+      aPoints[0] = AttributePoint2DPtr();
+      aPoints[1] = AttributePoint2DPtr();
+      for (int i = 0, aPtInd = 0; i < CONSTRAINT_ATTR_SIZE; ++i) {
+        AttributeRefAttrPtr aRefAttr = (*aCIt)->refattr(SketchPlugin_Constraint::ATTRIBUTE(i));
+        if (aRefAttr && !aRefAttr->isObject())
+          aPoints[aPtInd++] = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aRefAttr->attr());
+      }
+
+      if (aPoints[0] && aPoints[1])
+        addCoincidence(aPoints[0], aPoints[1]);
+    }
+  }
+
   std::list< std::set<AttributePoint2DPtr> >::iterator find(const AttributePoint2DPtr& thePoint)
   {
     std::list< std::set<AttributePoint2DPtr> >::iterator aSeek = myCoincidentPoints.begin();
@@ -224,26 +283,6 @@ private:
 std::set<AttributePoint2DPtr> findPointsCoincidentToPoint(const AttributePoint2DPtr& thePoint)
 {
   CoincidentPoints aCoincidentPoints;
-  AttributePoint2DPtr aPoints[2];
-
-  FeaturePtr anOwner = ModelAPI_Feature::feature(thePoint->owner());
-  std::set<FeaturePtr> aCoincidences = findCoincidentConstraints(anOwner);
-  std::set<FeaturePtr>::const_iterator aCIt = aCoincidences.begin();
-  for (; aCIt != aCoincidences.end(); ++aCIt) {
-    aPoints[0] = AttributePoint2DPtr();
-    aPoints[1] = AttributePoint2DPtr();
-    for (int i = 0, aPtInd = 0; i < CONSTRAINT_ATTR_SIZE; ++i) {
-      AttributeRefAttrPtr aRefAttr = (*aCIt)->refattr(SketchPlugin_Constraint::ATTRIBUTE(i));
-      if (!aRefAttr)
-        continue;
-      if (!aRefAttr->isObject())
-        aPoints[aPtInd++] = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aRefAttr->attr());
-    }
-
-    if (aPoints[0])
-      aCoincidentPoints.addCoincidence(aPoints[0], aPoints[1]);
-  }
-
   return aCoincidentPoints.coincidentPoints(thePoint);
 }
 
@@ -318,10 +357,10 @@ void convertRefAttrToPointOrTangentCurve(const AttributeRefAttrPtr&      theRefA
 }
 
 
-FeaturePtr createConstraint(SketchPlugin_Sketch* theSketch,
-                            const std::string& theConstraintId,
-                            const AttributePtr& theFirstAttribute,
-                            const AttributePtr& theSecondAttribute)
+FeaturePtr createConstraintAttrAttr(SketchPlugin_Sketch* theSketch,
+                                    const std::string& theConstraintId,
+                                    const AttributePtr& theFirstAttribute,
+                                    const AttributePtr& theSecondAttribute)
 {
   FeaturePtr aConstraint = theSketch->addFeature(theConstraintId);
   AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
@@ -342,10 +381,10 @@ FeaturePtr createConstraint(SketchPlugin_Sketch* theSketch,
   return aConstraint;
 }
 
-FeaturePtr createConstraint(SketchPlugin_Sketch* theSketch,
-                            const std::string& theConstraintId,
-                            const AttributePtr& theFirstAttribute,
-                            const ObjectPtr& theSecondObject)
+FeaturePtr createConstraintAttrObject(SketchPlugin_Sketch* theSketch,
+                                      const std::string& theConstraintId,
+                                      const AttributePtr& theFirstAttribute,
+                                      const ObjectPtr& theSecondObject)
 {
   FeaturePtr aConstraint = theSketch->addFeature(theConstraintId);
   AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
@@ -366,10 +405,10 @@ FeaturePtr createConstraint(SketchPlugin_Sketch* theSketch,
   return aConstraint;
 }
 
-FeaturePtr createConstraint(SketchPlugin_Sketch* theSketch,
-                            const std::string& theConstraintId,
-                            const ObjectPtr& theFirstObject,
-                            const ObjectPtr& theSecondObject)
+FeaturePtr createConstraintObjectObject(SketchPlugin_Sketch* theSketch,
+                                        const std::string& theConstraintId,
+                                        const ObjectPtr& theFirstObject,
+                                        const ObjectPtr& theSecondObject)
 {
   FeaturePtr aConstraint = theSketch->addFeature(theConstraintId);
   AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
@@ -390,4 +429,35 @@ FeaturePtr createConstraint(SketchPlugin_Sketch* theSketch,
   return aConstraint;
 }
 
+GeomPnt2dPtr flyoutPointCoordinates(const ConstraintPtr& theConstraint)
+{
+  // currently process Length constraints only
+  if (theConstraint->getKind() != SketchPlugin_ConstraintLength::ID())
+    return GeomPnt2dPtr();
+
+  AttributeRefAttrPtr aLineAttr = theConstraint->refattr(SketchPlugin_Constraint::ENTITY_A());
+  if (!aLineAttr || !aLineAttr->isObject())
+    return GeomPnt2dPtr();
+  FeaturePtr aLine = ModelAPI_Feature::feature(aLineAttr->object());
+  if (!aLine || aLine->getKind() != SketchPlugin_Line::ID())
+    return GeomPnt2dPtr();
+
+  std::shared_ptr<GeomAPI_XY> aStartPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
+      aLine->attribute(SketchPlugin_Line::START_ID()))->pnt()->xy();
+  std::shared_ptr<GeomAPI_XY> aEndPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
+      aLine->attribute(SketchPlugin_Line::END_ID()))->pnt()->xy();
+
+  std::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr =
+      std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
+      theConstraint->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
+  std::shared_ptr<GeomAPI_Pnt2d> aFltPnt = aFlyoutAttr->pnt();
+
+  std::shared_ptr<GeomAPI_Dir2d> aLineDir(new GeomAPI_Dir2d(aEndPnt->decreased(aStartPnt)));
+
+  double X = aStartPnt->x() + aFltPnt->x() * aLineDir->x() - aFltPnt->y() * aLineDir->y();
+  double Y = aStartPnt->y() + aFltPnt->x() * aLineDir->y() + aFltPnt->y() * aLineDir->x();
+
+  return GeomPnt2dPtr(new GeomAPI_Pnt2d(X, Y));
+}
+
 } // namespace SketchPlugin_Tools