Salome HOME
updated copyright message
[modules/shaper.git] / src / SketcherPrs / SketcherPrs_PositionMgr.cpp
index 49892641961967dae0d903f327d14dd20e6d9e15..282c68d2fa96366358be4a43fb9e2885ac08d53e 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2014-2017  CEA/DEN, EDF R&D
+// Copyright (C) 2014-2023  CEA, EDF
 //
 // 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"
 #include "SketcherPrs_Tools.h"
 
-#include <GeomAPI_Edge.h>
-#include <GeomAPI_Curve.h>
-#include <GeomAPI_Vertex.h>
-#include <GeomAPI_Dir.h>
 #include <GeomAPI_Ax3.h>
 #include <GeomAPI_Circ.h>
+#include <GeomAPI_Curve.h>
+#include <GeomAPI_Edge.h>
+#include <GeomAPI_Ellipse.h>
+#include <GeomAPI_Dir.h>
 #include <GeomAPI_Lin2d.h>
+#include <GeomAPI_Vertex.h>
 
 #include <GeomDataAPI_Point2D.h>
 
-#include <SketchPlugin_Line.h>
-#include <SketchPlugin_Circle.h>
 #include <SketchPlugin_Arc.h>
-#include <SketchPlugin_ConstraintTangent.h>
+#include <SketchPlugin_Circle.h>
+#include <SketchPlugin_Ellipse.h>
+#include <SketchPlugin_Line.h>
 #include <SketchPlugin_ConstraintPerpendicular.h>
+#include <SketchPlugin_ConstraintTangent.h>
 
 #include <TopoDS_Vertex.hxx>
 #include <Geom_Curve.hxx>
@@ -61,7 +62,6 @@ SketcherPrs_PositionMgr::SketcherPrs_PositionMgr()
 {
 }
 
-
 int SketcherPrs_PositionMgr::getPositionIndex(ObjectPtr theLine,
                                               const SketcherPrs_SymbolPrs* thePrs)
 {
@@ -152,14 +152,17 @@ const std::array<int, 2>& SketcherPrs_PositionMgr::getPositionIndex(GeomPointPtr
       if (aUseFeature) {
         DataPtr aData = aFeature->data();
         AttributeRefAttrPtr aObjRef = aData->refattr(SketchPlugin_Constraint::ENTITY_A());
-        FeaturePtr aObj = ModelAPI_Feature::feature(aObjRef->object());
+        FeaturePtr aObj;
+        if (aObjRef)
+          aObj = ModelAPI_Feature::feature(aObjRef->object());
         bool aContains = false;
-        if (containsPoint(aObj, aPnt2d, thePos)) {
+        if (aObj && containsPoint(aObj, aPnt2d, thePos)) {
           aContains = true;
         } else {
           aObjRef = aData->refattr(SketchPlugin_Constraint::ENTITY_B());
-          aObj = ModelAPI_Feature::feature(aObjRef->object());
-          if (containsPoint(aObj, aPnt2d, thePos)) {
+          if (aObjRef)
+            aObj = ModelAPI_Feature::feature(aObjRef->object());
+          if (aObj && containsPoint(aObj, aPnt2d, thePos)) {
             aContains = true;
           }
         }
@@ -185,10 +188,12 @@ gp_Vec getVector(ObjectPtr theShape, GeomDirPtr theDir, gp_Pnt theP)
   gp_Vec aVec;
   std::shared_ptr<GeomAPI_Shape> aShape = SketcherPrs_Tools::getShape(theShape);
   if (aShape->isEdge()) {
+    std::shared_ptr<GeomAPI_Edge> anEdge = aShape->edge();
     std::shared_ptr<GeomAPI_Curve> aCurve =
       std::shared_ptr<GeomAPI_Curve>(new GeomAPI_Curve(aShape));
 
-    if (aCurve->isCircle()) {
+    if (anEdge->isClosed()) {
+      double aPeriod = aCurve->endParam() - aCurve->startParam();
       Handle(Geom_Curve) aCurv = aCurve->impl<Handle_Geom_Curve>();
       GeomAPI_ProjectPointOnCurve anExtr(theP, aCurv);
       double aParam = anExtr.LowerDistanceParameter();
@@ -202,9 +207,9 @@ gp_Vec getVector(ObjectPtr theShape, GeomDirPtr theDir, gp_Pnt theP)
         // 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;
+          aDelta += aPeriod;
+        while (aDelta > aPeriod + Precision::Confusion())
+          aDelta -= aPeriod;
         if (fabs(aDelta) < Precision::Confusion())
           aVec.Reverse();
       }
@@ -255,7 +260,8 @@ gp_Pnt SketcherPrs_PositionMgr::getPosition(ObjectPtr theShape,
 
   // Compute shifting vector for a one symbol
   gp_Vec aShift = aVec1.Crossed(thePrs->plane()->normal()->impl<gp_Dir>());
-  aShift.Normalize();
+  if (aShift.SquareMagnitude() > Precision::SquareConfusion())
+    aShift.Normalize();
   aShift.Multiply(theStep * 0.8);
 
   // Shift the position coordinate according to position index
@@ -327,8 +333,7 @@ std::list<ObjectPtr> getCurves(const GeomPointPtr& thePnt, const SketcherPrs_Sym
         if (aDist <= Precision::Confusion())
           aList.push_back(aFeature->firstResult());
       }
-    } else if ((aFeature->getKind() == SketchPlugin_Circle::ID()) ||
-              (aFeature->getKind() == SketchPlugin_Arc::ID())) {
+    } else {
       GeomCurvePtr aCurve;
       ObjectPtr aResObj;
       std::list<ResultPtr> aResults = aFeature->results();
@@ -342,10 +347,18 @@ std::list<ObjectPtr> getCurves(const GeomPointPtr& thePnt, const SketcherPrs_Sym
         }
       }
       if (aCurve.get()) {
-        double aStart = aCurve->startParam();
-        double aEnd = aCurve->endParam();
-        GeomCirclePtr  aCircle = GeomCirclePtr(new GeomAPI_Circ(aCurve));
-        GeomPointPtr aProjPnt = aCircle->project(thePnt);
+        GeomPointPtr aProjPnt;
+        if (aFeature->getKind() == SketchPlugin_Circle::ID() ||
+          aFeature->getKind() == SketchPlugin_Arc::ID()) {
+          GeomCirclePtr aCircle = GeomCirclePtr(new GeomAPI_Circ(aCurve));
+          aProjPnt = aCircle->project(thePnt);
+        }
+        else if (aFeature->getKind() == SketchPlugin_Ellipse::ID()) {
+          GeomEllipsePtr anEllipse = GeomEllipsePtr(new GeomAPI_Ellipse(aCurve));
+          aProjPnt = anEllipse->project(thePnt);
+        }
+        else
+          aProjPnt = aCurve->project(thePnt);
         if (aProjPnt && thePnt->distance(aProjPnt) <= Precision::Confusion())
           aList.push_back(aResObj);
       }
@@ -360,6 +373,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>();
 
@@ -409,7 +424,7 @@ gp_Pnt SketcherPrs_PositionMgr::getPointPosition(
   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()) {
+  if ((int)aAngles.size() >= aPos[1] && !aVectors.empty()) {
     int aId = aPos[0];
     aVecPos = *(std::next(aVectors.begin(), aId));
 
@@ -424,9 +439,7 @@ gp_Pnt SketcherPrs_PositionMgr::getPointPosition(
   double aAng;
   std::list<double>::const_iterator aItAng;
 
-  double aAngPos;
   bool aHasPlace = false;
-  //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) {
@@ -436,9 +449,7 @@ gp_Pnt SketcherPrs_PositionMgr::getPointPosition(
 
       if ((!aHasPlace) && (aPosCount >= (aPos[0] + 1))) {
         aHasPlace = true;
-        aAngPos = (*aItAng);
         aVecPos = (*aItVec);
-        //aIntId = aPos[0] - (aPosCount - Nb);
       }
     }
     if (aPosCount < aPos[1]) {
@@ -476,10 +487,10 @@ void SketcherPrs_PositionMgr::deleteConstraint(const SketcherPrs_SymbolPrs* theP
         aToDel.push_back(aIt->first);
       else {
         // Reindex objects positions in order to avoid spaces
-        PositionsMap::iterator aIt;
+        PositionsMap::iterator aPMIt;
         int i = 0;
-        for (aIt = aPosMap.begin(); aIt != aPosMap.end(); aIt++, i++)
-          aIt->second = i;
+        for (aPMIt = aPosMap.begin(); aPMIt != aPosMap.end(); aPMIt++, i++)
+          aPMIt->second = i;
       }
     }
   }