Salome HOME
Copyright update 2022
[modules/shaper.git] / src / SketchPlugin / SketchPlugin_MacroEllipticArc.cpp
index ab87ca57f7bb40ba1ecb49961d470f8e388824fa..639e2fb17e51a835009fcab9175f725eaa5e27c9 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2017-2019  CEA/DEN, EDF R&D
+// Copyright (C) 2017-2022  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
@@ -42,6 +42,7 @@
 #include <GeomAlgoAPI_PointBuilder.h>
 
 
+const double TOLERANCE = 1.e-7;
 const double paramTolerance = 1.e-4;
 const double PI = 3.141592653589793238463;
 
@@ -83,6 +84,7 @@ void SketchPlugin_MacroEllipticArc::initAttributes()
 void SketchPlugin_MacroEllipticArc::execute()
 {
   FeaturePtr anEllipse = createEllipticArcFeature();
+  constraintsForEllipticArc(anEllipse);
 
   // message to init reentrant operation
   static Events_ID anId = SketchPlugin_MacroArcReentrantMessage::eventId();
@@ -92,7 +94,7 @@ void SketchPlugin_MacroEllipticArc::execute()
   Events_Loop::loop()->send(aMessage);
 }
 
-void SketchPlugin_MacroEllipticArc::attributeChanged(const std::string& theID)
+void SketchPlugin_MacroEllipticArc::attributeChanged(const std::string& /*theID*/)
 {
   static const int NB_POINTS = 4;
   std::string aPointAttrName[NB_POINTS] = { CENTER_ID(),
@@ -131,6 +133,9 @@ void SketchPlugin_MacroEllipticArc::attributeChanged(const std::string& theID)
   myStartPnt  = anEllipsePoints[2];
   myEndPnt    = anEllipsePoints[3];
 
+  if (myCenter->distance(myMajorAxis) < TOLERANCE)
+    return; // ellipse is not valid
+
   std::shared_ptr<GeomAPI_Ellipse2d> anEllipse;
   if (aNbInitialized == 2) {
     GeomDir2dPtr aXDir(new GeomAPI_Dir2d(anEllipsePoints[1]->x() - anEllipsePoints[0]->x(),
@@ -301,6 +306,32 @@ FeaturePtr SketchPlugin_MacroEllipticArc::createEllipticArcFeature()
   return aEllipseFeature;
 }
 
+void SketchPlugin_MacroEllipticArc::constraintsForEllipticArc(FeaturePtr theEllipticArc)
+{
+  // tangency on-the-fly is not applicable for elliptic arcs
+  static const bool isTangencyApplicable = false;
+  // Create constraints.
+  SketchPlugin_Tools::createCoincidenceOrTangency(
+      this, CENTER_REF_ID(),
+      theEllipticArc->attribute(SketchPlugin_EllipticArc::CENTER_ID()),
+      ObjectPtr(), isTangencyApplicable);
+  // make coincidence only if PASSED_POINT_REF_ID() refers a point but not an object
+  if (!refattr(MAJOR_AXIS_POINT_REF_ID())->isObject()) {
+    SketchPlugin_Tools::createCoincidenceOrTangency(
+        this, MAJOR_AXIS_POINT_REF_ID(),
+        AttributePtr(),
+        theEllipticArc->lastResult(), isTangencyApplicable);
+  }
+  SketchPlugin_Tools::createCoincidenceOrTangency(
+      this, START_POINT_REF_ID(),
+      theEllipticArc->attribute(SketchPlugin_EllipticArc::START_POINT_ID()),
+      ObjectPtr(), isTangencyApplicable);
+  SketchPlugin_Tools::createCoincidenceOrTangency(
+      this, END_POINT_REF_ID(),
+      theEllipticArc->attribute(SketchPlugin_EllipticArc::END_POINT_ID()),
+      ObjectPtr(), isTangencyApplicable);
+}
+
 AISObjectPtr SketchPlugin_MacroEllipticArc::getAISObject(AISObjectPtr thePrevious)
 {
   SketchPlugin_Sketch* aSketch = sketch();
@@ -323,6 +354,7 @@ AISObjectPtr SketchPlugin_MacroEllipticArc::getAISObject(AISObjectPtr thePreviou
   if (!anAIS)
     anAIS.reset(new GeomAPI_AISObject());
   anAIS->createShape(aCompound);
+  SketchPlugin_Tools::customizeFeaturePrs(anAIS, boolean(AUXILIARY_ID())->value());
   return anAIS;
 }
 
@@ -332,7 +364,7 @@ GeomShapePtr SketchPlugin_MacroEllipticArc::getArcShape()
     return GeomShapePtr();
 
   SketchPlugin_Sketch* aSketch = sketch();
-  if (!aSketch)
+  if (!aSketch || myCenter->distance(myMajorAxis) < 1.e-7)
     return GeomShapePtr();
 
   GeomPointPtr aCenter(aSketch->to3D(myCenter->x(), myCenter->y()));