]> SALOME platform Git repositories - modules/shaper.git/commitdiff
Salome HOME
Fixed edge positioning issue of [EDF] (2023-T1) Sketch middle point constrain should... eksu/CR35150_fixMiddlePoint 58/head
authorEkaterina Sukhareva <ekaterina.sukhareva@opencascade.com>
Mon, 6 May 2024 16:09:09 +0000 (17:09 +0100)
committerEsukhareva <ekaterina.sukhareva@opencascade.com>
Fri, 18 Oct 2024 16:02:30 +0000 (17:02 +0100)
src/SketchPlugin/SketchPlugin_ConstraintMiddle.cpp
src/SketchPlugin/SketchPlugin_ConstraintMiddle.h
src/SketchPlugin/doc/middleFeature.rst
src/SketchPlugin/icons/middlepoint_edge.png [new file with mode: 0755]
src/SketchPlugin/icons/middlepoint_obj.png [deleted file]
src/SketchPlugin/plugin-Sketch.xml

index ecb882bbc4ddf64240e7c0118fb7d39d3ac729cf..3d083c79bc642df625683d200f3e42eb42a1d2f1 100644 (file)
 
 #include <ModelAPI_Events.h>
 #include <SketchPlugin_Point.h>
+#include <SketchPlugin_Line.h>
+#include <SketchPlugin_Arc.h>
 #include <SketchPlugin_Tools.h>
 
+#include <GeomAPI_Edge.h>
 
 class RaiiSetBoolFlag {
 public:
@@ -47,49 +50,13 @@ private:
   bool *myBoolPtr;
 };
 
+const double TOL = 1.e-5;
 
 SketchPlugin_ConstraintMiddle::SketchPlugin_ConstraintMiddle()
 {
   myBlockAttribInit = false;
 }
 
-// Create new point for Middle constraint
-void SketchPlugin_ConstraintMiddle::CreatePoint()
-{
-  // Wait all objects being created, then send update events
-  static Events_ID anUpdateEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
-  bool isUpdateFlushed = Events_Loop::loop()->isFlushed(anUpdateEvent);
-  if (isUpdateFlushed)
-    Events_Loop::loop()->setFlushed(anUpdateEvent, false);
-
-  auto aTrPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(data()->attribute(POINT_REF_ID()));
-
-  if (!myPoint)
-  {
-    // Get last subfeature (constraintMiddle) for set as parent
-    FeaturePtr aCurrentFeature = sketch()->subFeature(sketch()->numberOfSubs() - 1);
-    keepCurrentFeature();
-    myPoint = sketch()->addFeature(SketchPlugin_Point::ID());
-    myPoint->boolean(SketchPlugin_SketchEntity::AUXILIARY_ID())->setValue(true);
-    restoreCurrentFeature();
-
-    myPoint->reference(SketchPlugin_Point::PARENT_ID())->setValue(aCurrentFeature);
-  }
-
-  AttributePoint2DPtr aCoord = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
-    myPoint->attribute(SketchPlugin_Point::COORD_ID()));
-  aCoord->setValue(aTrPnt->pnt());
-
-  myPoint->execute();
-
-  // Init second attr for constraint
-  refattr(SketchPlugin_Constraint::ENTITY_B())->setObject(myPoint);
-
-
-  if (isUpdateFlushed)
-    Events_Loop::loop()->setFlushed(anUpdateEvent, true);
-}
-
 void SketchPlugin_ConstraintMiddle::initAttributes()
 {
   // To maintain compatibility with older study versions, keep the order of all previously existing attributes:
@@ -120,6 +87,7 @@ void SketchPlugin_ConstraintMiddle::initAttributes()
     aMethodAttr->setValue(MIDDLE_TYPE_BY_LINE_AND_POINT());
   }
 
+  myX = 1., myY = 1.;
   data()->addAttribute(POINT_REF_ID(), GeomDataAPI_Point2D::typeId());
 
   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), POINT_REF_ID());
@@ -129,20 +97,87 @@ void SketchPlugin_ConstraintMiddle::execute()
 {
   if (string(MIDDLE_TYPE())->value() == MIDDLE_TYPE_BY_LINE())
   {
-    std::dynamic_pointer_cast<GeomDataAPI_Point2D>(data()->attribute(POINT_REF_ID()))->setValue(1., 1.);
-    AttributeRefAttrPtr aPointRes = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
-      data()->attribute(SketchPlugin_Constraint::ENTITY_B()));
-
     auto aRefAttr = data()->refattr(SketchPlugin_Constraint::ENTITY_A())->object();
     if (!aRefAttr.get())
       return;
 
-
     if (!attribute(ENTITY_B())->isInitialized())
-      CreatePoint(); // Create new point
+    {
+      computePointCoordinates();
+      createPoint(); // Create new point
+    }
   }
 }
 
+void SketchPlugin_ConstraintMiddle::computePointCoordinates()
+{
+  if (string(MIDDLE_TYPE())->value() != MIDDLE_TYPE_BY_LINE() 
+      || attribute(ENTITY_B())->isInitialized())
+    return;
+
+  auto aRefAttr = data()->refattr(SketchPlugin_Constraint::ENTITY_A())->object();
+  if (!aRefAttr.get())
+    return;
+
+  // calculate position of middle point(poor placement of a point can greatly
+  // deform the position of the original shape in the sketch)
+  FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttr);
+  if(aFeature->getKind() == SketchPlugin_Line::ID() || aFeature->getKind() == SketchPlugin_Arc::ID())
+  {
+    GeomShapePtr aShape = aFeature->lastResult()->shape();
+    if (aShape && aShape->shapeType() == GeomAPI_Shape::EDGE) 
+    {
+      GeomAPI_Edge anEdge(aShape->edge());
+      GeomPointPtr aMiddle = anEdge.middlePoint();
+      GeomPnt2dPtr aMiddle2d = sketch()->to2D(aMiddle);
+      myX = aMiddle2d->x();
+      myY = aMiddle2d->y();
+
+      // Workaround: The exact midpoint causes an exception in the solver when attempting to use this point in subsequent features.
+      auto deltaX = sketch()->to2D(anEdge.firstPoint())->x() - sketch()->to2D(anEdge.lastPoint())->x();
+      (deltaX < TOL) ? myX += TOL : myY += TOL;
+    }
+  }
+  std::dynamic_pointer_cast<GeomDataAPI_Point2D>(data()->attribute(POINT_REF_ID()))->setValue(myX, myY);
+}
+
+// Create new point for Middle constraint
+void SketchPlugin_ConstraintMiddle::createPoint()
+{
+  // Wait all objects being created, then send update events
+  static Events_ID anUpdateEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
+  bool isUpdateFlushed = Events_Loop::loop()->isFlushed(anUpdateEvent);
+  if (isUpdateFlushed)
+    Events_Loop::loop()->setFlushed(anUpdateEvent, false);
+
+  auto aTrPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(data()->attribute(POINT_REF_ID()));
+
+  if (!myPoint)
+  {
+    // Get last subfeature (constraintMiddle) for set as parent
+    FeaturePtr aCurrentFeature = sketch()->subFeature(sketch()->numberOfSubs() - 1);
+    keepCurrentFeature();
+    myPoint = sketch()->addFeature(SketchPlugin_Point::ID());
+    myPoint->boolean(SketchPlugin_SketchEntity::AUXILIARY_ID())->setValue(true);
+    restoreCurrentFeature();
+
+    myPoint->reference(SketchPlugin_Point::PARENT_ID())->setValue(aCurrentFeature);
+  }
+
+  AttributePoint2DPtr aCoord = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
+    myPoint->attribute(SketchPlugin_Point::COORD_ID()));
+  aCoord->setValue(aTrPnt->pnt());
+
+  myPoint->execute();
+
+  // Init second attr for constraint
+  refattr(SketchPlugin_Constraint::ENTITY_B())->setObject(myPoint);
+
+
+  if (isUpdateFlushed)
+    Events_Loop::loop()->setFlushed(anUpdateEvent, true);
+}
+
 void SketchPlugin_ConstraintMiddle::attributeChanged(const std::string& theID)
 {
   if (theID == MIDDLE_TYPE())
index fa8c77ec699b34dce12aa807a20b558fd544b71c..2aa6e485e8bfc1e97d8f7abd39fe4f08ac02e9bf 100644 (file)
@@ -94,9 +94,12 @@ class SketchPlugin_ConstraintMiddle : public SketchPlugin_ConstraintBase
   SketchPlugin_ConstraintMiddle();
 
 private:
-  void CreatePoint();
+  void createPoint();
+  void computePointCoordinates();
 
   std::shared_ptr<ModelAPI_Feature> myPoint;
+  double myX;
+  double myY;
   bool  myBlockAttribInit;
 };
 
index a6d0142f81a810bb34152d9c8551e108e2a11abe..18ab8f42fbf60524de05a3b1bdd087d7898f4fef 100644 (file)
@@ -18,7 +18,7 @@ There are 2 algorithms for creation of a middle constraint:
 
 **By object and point** create a middle constraint by object (segment or arc) and point
 
-.. figure:: images/MiddlePoint_obj.png
+.. figure:: images/MiddlePoint_edge.png
    :align: left
    :height: 24px
 
diff --git a/src/SketchPlugin/icons/middlepoint_edge.png b/src/SketchPlugin/icons/middlepoint_edge.png
new file mode 100755 (executable)
index 0000000..6488c05
Binary files /dev/null and b/src/SketchPlugin/icons/middlepoint_edge.png differ
diff --git a/src/SketchPlugin/icons/middlepoint_obj.png b/src/SketchPlugin/icons/middlepoint_obj.png
deleted file mode 100644 (file)
index af12521..0000000
Binary files a/src/SketchPlugin/icons/middlepoint_obj.png and /dev/null differ
index 270c40f7ff78bae9f7412e9e77027c2cffdecf44..bca6759a9c04c32010d0744026bf1f93d7f9da8b 100644 (file)
             </sketch_shape_selector>
           </box>
           <box id="middle_type_by_line"
-               icon="icons/Sketch/middlepoint_obj.png"
+               icon="icons/Sketch/middlepoint_edge.png"
                title="Line">
             <sketch_shape_selector id="ConstraintEntityA"
              label="Object"