Salome HOME
Task 2.11. Ability to impose a midpoint on an arc (refers issue #3002)
[modules/shaper.git] / src / SketcherPrs / SketcherPrs_PositionMgr.cpp
index 65b1d045166f3044044e2917d7f1119f52af0e71..0a546233d9b33060c9ba1257475d3cd95622f06d 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 "SketcherPrs_PositionMgr.h"
@@ -27,6 +26,7 @@
 #include <GeomAPI_Dir.h>
 #include <GeomAPI_Ax3.h>
 #include <GeomAPI_Circ.h>
+#include <GeomAPI_Lin2d.h>
 
 #include <GeomDataAPI_Point2D.h>
 
@@ -36,9 +36,9 @@
 #include <SketchPlugin_ConstraintTangent.h>
 #include <SketchPlugin_ConstraintPerpendicular.h>
 
-#include <BRepExtrema_ExtPC.hxx>
 #include <TopoDS_Vertex.hxx>
 #include <Geom_Curve.hxx>
+#include <GeomAPI_ProjectPointOnCurve.hxx>
 #include <TColGeom_SequenceOfCurve.hxx>
 #include <gp_Dir.hxx>
 
@@ -60,7 +60,6 @@ SketcherPrs_PositionMgr::SketcherPrs_PositionMgr()
 {
 }
 
-
 int SketcherPrs_PositionMgr::getPositionIndex(ObjectPtr theLine,
                                               const SketcherPrs_SymbolPrs* thePrs)
 {
@@ -88,17 +87,8 @@ int SketcherPrs_PositionMgr::getPositionIndex(ObjectPtr theLine,
 
 bool SketcherPrs_PositionMgr::isPntConstraint(const std::string& theName)
 {
-  static std::list<std::string> aConstraints;
-  if (aConstraints.size() == 0) {
-    aConstraints.push_back(SketchPlugin_ConstraintTangent::ID());
-    aConstraints.push_back(SketchPlugin_ConstraintPerpendicular::ID());
-  }
-  std::list<std::string>::const_iterator aIt;
-  for (aIt = aConstraints.cbegin(); aIt != aConstraints.cend(); ++aIt) {
-    if ((*aIt) == theName)
-      return true;
-  }
-  return false;
+  return ((theName == SketchPlugin_ConstraintTangent::ID()) ||
+    (theName == SketchPlugin_ConstraintPerpendicular::ID()));
 }
 
 bool containsPoint(const FeaturePtr& theFeature, GeomPnt2dPtr thePnt2d, GeomPointPtr thePos)
@@ -155,31 +145,30 @@ const std::array<int, 2>& SketcherPrs_PositionMgr::getPositionIndex(GeomPointPtr
     for (int i = 0; i < aNbSubs; i++) {
       FeaturePtr aFeature = aOwner->subFeature(i);
 
-      if (myPntShapes.count(aFeature.get()) == 1) {
-        myPntShapes[aFeature.get()][0] = aId;
-        aId++;
-        aFeaList.push_back(aFeature.get());
-      } else {
-        if (isPntConstraint(aFeature->getKind())) {
-          DataPtr aData = aFeature->data();
-          AttributeRefAttrPtr aObjRef = aData->refattr(SketchPlugin_Constraint::ENTITY_A());
-          FeaturePtr aObj = ModelAPI_Feature::feature(aObjRef->object());
-          bool aContains = false;
-          if (containsPoint(aObj, aPnt2d, thePos)) {
-            aContains = true;
-          } else {
-            aObjRef = aData->refattr(SketchPlugin_Constraint::ENTITY_B());
+      bool aUseFeature = ((myPntShapes.count(aFeature.get()) == 1) ||
+                         (isPntConstraint(aFeature->getKind())));
+      if (aUseFeature) {
+        DataPtr aData = aFeature->data();
+        AttributeRefAttrPtr aObjRef = aData->refattr(SketchPlugin_Constraint::ENTITY_A());
+        FeaturePtr aObj;
+        if (aObjRef)
+          aObj = ModelAPI_Feature::feature(aObjRef->object());
+        bool aContains = false;
+        if (aObj && containsPoint(aObj, aPnt2d, thePos)) {
+          aContains = true;
+        } else {
+          aObjRef = aData->refattr(SketchPlugin_Constraint::ENTITY_B());
+          if (aObjRef)
             aObj = ModelAPI_Feature::feature(aObjRef->object());
-            if (containsPoint(aObj, aPnt2d, thePos)) {
-              aContains = true;
-            }
-          }
-          if (aContains) {
-            myPntShapes[aFeature.get()][0] = aId;
-            aId++;
-            aFeaList.push_back(aFeature.get());
+          if (aObj && containsPoint(aObj, aPnt2d, thePos)) {
+            aContains = true;
           }
         }
+        if (aContains) {
+          myPntShapes[aFeature.get()][0] = aId;
+          aId++;
+          aFeaList.push_back(aFeature.get());
+        }
       }
     }
     int aSize = (int) aFeaList.size();
@@ -201,21 +190,24 @@ gp_Vec getVector(ObjectPtr theShape, GeomDirPtr theDir, gp_Pnt theP)
       std::shared_ptr<GeomAPI_Curve>(new GeomAPI_Curve(aShape));
 
     if (aCurve->isCircle()) {
-      GeomEdgePtr aEdgePtr(new GeomAPI_Edge(aShape));
-      GeomVertexPtr aVertexPtr(new GeomAPI_Vertex(theP.X(), theP.Y(), theP.Z()));
-      BRepExtrema_ExtPC aExtrema(aVertexPtr->impl<TopoDS_Vertex>(),
-                                 aEdgePtr->impl<TopoDS_Edge>());
-      int aNb = aExtrema.NbExt();
-      if (aNb > 0) {
-        for (int i = 1; i <= aNb; i++) {
-          if (aExtrema.IsMin(i)) {
-            double aParam = aExtrema.Parameter(i);
-            Handle(Geom_Curve) aCurv = aCurve->impl<Handle_Geom_Curve>();
-            gp_Pnt aP;
-            aCurv->D1(aParam, aP, aVec);
-            break;
-          }
-        }
+      Handle(Geom_Curve) aCurv = aCurve->impl<Handle_Geom_Curve>();
+      GeomAPI_ProjectPointOnCurve anExtr(theP, aCurv);
+      double aParam = anExtr.LowerDistanceParameter();
+      gp_Pnt aP;
+      aCurv->D1(aParam, aP, aVec);
+      // 2458: check correct orientation of the vector
+      if (aVec.SquareMagnitude() > Precision::Confusion()) {
+        std::shared_ptr<GeomAPI_Edge> aCircEdge(new GeomAPI_Edge(aShape));
+        double aFirstParam, aLastParam;
+        aCircEdge->getRange(aFirstParam, aLastParam);
+        // if parameter is near the LastParam, make the vector go inside (reverse)
+        double aDelta = aLastParam - aParam;
+        while (aDelta < -Precision::Confusion())
+          aDelta += 2. * M_PI;
+        while (aDelta > 2. * M_PI - Precision::Confusion())
+          aDelta -= 2. * M_PI;
+        if (fabs(aDelta) < Precision::Confusion())
+          aVec.Reverse();
       }
     } else {
       GeomPointPtr aPnt1 = aCurve->getPoint(aCurve->endParam());
@@ -311,8 +303,8 @@ std::list<ObjectPtr> getCurves(const GeomPointPtr& thePnt, const SketcherPrs_Sym
   int aNbSubs = aOwner->numberOfSubs();
   for (int i = 0; i < aNbSubs; i++) {
     FeaturePtr aFeature = aOwner->subFeature(i);
-    if (!aFeature->firstResult().get()) // There is no result
-      continue;
+    if (!aFeature->firstResult().get() || aFeature->firstResult()->isDisabled())
+      continue;  // There is no result
 
     if (aFeature->getKind() == SketchPlugin_Line::ID()) {
       AttributePoint2DPtr aSPnt1 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
@@ -323,10 +315,18 @@ std::list<ObjectPtr> getCurves(const GeomPointPtr& thePnt, const SketcherPrs_Sym
       GeomPnt2dPtr aPnt1 = aSPnt1->pnt();
       GeomPnt2dPtr aPnt2 = aSPnt2->pnt();
 
-      if (aPnt1->isEqual(aPnt2d) || aPnt2->isEqual(aPnt2d)) {
+      if (aPnt1->distance(aPnt2d) <= Precision::Confusion() ||
+          aPnt2->distance(aPnt2d) <= Precision::Confusion()) {
+        // a point corresponds to one of the line end
         GeomShapePtr aShp = SketcherPrs_Tools::getShape(aFeature->firstResult());
         GeomCurvePtr aCurv = std::shared_ptr<GeomAPI_Curve>(new GeomAPI_Curve(aShp));
         aList.push_back(aFeature->firstResult());
+      } else {
+        // Check that a point belongs to the curve
+        GeomAPI_Lin2d aLin2d(aPnt1, aPnt2);
+        double aDist = aLin2d.distance(aPnt2d);
+        if (aDist <= Precision::Confusion())
+          aList.push_back(aFeature->firstResult());
       }
     } else if ((aFeature->getKind() == SketchPlugin_Circle::ID()) ||
               (aFeature->getKind() == SketchPlugin_Arc::ID())) {
@@ -346,8 +346,8 @@ std::list<ObjectPtr> getCurves(const GeomPointPtr& thePnt, const SketcherPrs_Sym
         double aStart = aCurve->startParam();
         double aEnd = aCurve->endParam();
         GeomCirclePtr  aCircle = GeomCirclePtr(new GeomAPI_Circ(aCurve));
-        double aParam;
-        if (aCircle->parameter(thePnt, 1.e-4, aParam) && (aParam >= aStart) && (aParam <= aEnd))
+        GeomPointPtr aProjPnt = aCircle->project(thePnt);
+        if (aProjPnt && thePnt->distance(aProjPnt) <= Precision::Confusion())
           aList.push_back(aResObj);
       }
     }
@@ -361,6 +361,8 @@ gp_Pnt SketcherPrs_PositionMgr::getPointPosition(
   double theStep, GeomPointPtr thePnt)
 {
   gp_Pnt aP = thePnt->impl<gp_Pnt>();
+  if (!thePrs->plane().get())
+    return aP;
   GeomDirPtr aNormal = thePrs->plane()->normal();
   gp_Dir aNormDir = aNormal->impl<gp_Dir>();
 
@@ -376,7 +378,8 @@ gp_Pnt SketcherPrs_PositionMgr::getPointPosition(
   const std::array<int, 2>& aPos = getPositionIndex(thePnt, thePrs);
 
   // Angle size of a symbol
-  double aAngleStep = PI * 50./180.;
+  //double aAngleStep = PI * 50./180.;
+  double aAngleStep = PI/4.;
 
   std::list<gp_Vec>::const_iterator aItVec;
   std::list<double> aAngles;
@@ -399,18 +402,34 @@ gp_Pnt SketcherPrs_PositionMgr::getPointPosition(
         }
       }
     }
-    aVectors.push_back(aVec);
-    aAngles.push_back(aMinAng);
+    if (aMinAng >= aAngleStep) {
+      aVectors.push_back(aVec);
+      aAngles.push_back(aMinAng);
+    }
+  }
+
+  gp_Ax1 aRotAx(aP, aNormDir);
+  gp_Vec aVecPos;
+  // If number of angle less then number of symbols then each symbol can be placed
+  // directly inside of the angle
+  if (aAngles.size() >= aPos[1] && !aVectors.empty()) {
+    int aId = aPos[0];
+    aVecPos = *(std::next(aVectors.begin(), aId));
+
+    gp_Vec aShift = aVecPos.Rotated(aRotAx, aAngleStep);
+    aShift.Normalize();
+    aShift.Multiply(theStep * 1.5);
+    return aP.Translated(aShift);
   }
 
+  // A case when there are a lot of symbols
   int aPosCount = 0;
   double aAng;
   std::list<double>::const_iterator aItAng;
 
   double aAngPos;
-  gp_Vec aVecPos;
   bool aHasPlace = false;
-  int aIntId = 0; // a position inside a one sector
+  //int aIntId = 0; // a position inside a one sector
   while (aPosCount < aPos[1]) {
     for (aItAng = aAngles.cbegin(), aItVec = aVectors.cbegin();
          aItAng != aAngles.cend(); ++aItAng, ++aItVec) {
@@ -422,7 +441,7 @@ gp_Pnt SketcherPrs_PositionMgr::getPointPosition(
         aHasPlace = true;
         aAngPos = (*aItAng);
         aVecPos = (*aItVec);
-        aIntId = aPos[0] - (aPosCount - Nb);
+        //aIntId = aPos[0] - (aPosCount - Nb);
       }
     }
     if (aPosCount < aPos[1]) {
@@ -430,12 +449,13 @@ gp_Pnt SketcherPrs_PositionMgr::getPointPosition(
       aHasPlace = false;
       aPosCount = 0;
     }
+    if (aAngleStep <= 0)
+      break;
   }
 
-  gp_Ax1 aRotAx(aP, aNormDir);
   if (aHasPlace) {
     // rotate base vector on a necessary angle
-    gp_Vec aShift = aVecPos.Rotated(aRotAx, aAngleStep + aAngleStep * aIntId);
+    gp_Vec aShift = aVecPos.Rotated(aRotAx, aAngleStep + aAngleStep * aPos[0]);
     aShift.Normalize();
     aShift.Multiply(theStep * 1.5);
     return aP.Translated(aShift);