From: Ekaterina Sukhareva Date: Mon, 6 May 2024 16:09:09 +0000 (+0100) Subject: Fixed edge positioning issue of [EDF] (2023-T1) Sketch middle point constrain should... X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=refs%2Ftlpr%2F58%2Fhead;p=modules%2Fshaper.git Fixed edge positioning issue of [EDF] (2023-T1) Sketch middle point constrain should create point if missing --- diff --git a/src/SketchPlugin/SketchPlugin_ConstraintMiddle.cpp b/src/SketchPlugin/SketchPlugin_ConstraintMiddle.cpp index ecb882bbc..3d083c79b 100644 --- a/src/SketchPlugin/SketchPlugin_ConstraintMiddle.cpp +++ b/src/SketchPlugin/SketchPlugin_ConstraintMiddle.cpp @@ -28,8 +28,11 @@ #include #include +#include +#include #include +#include 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(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( - 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(data()->attribute(POINT_REF_ID()))->setValue(1., 1.); - AttributeRefAttrPtr aPointRes = std::dynamic_pointer_cast( - 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(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(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( + 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()) diff --git a/src/SketchPlugin/SketchPlugin_ConstraintMiddle.h b/src/SketchPlugin/SketchPlugin_ConstraintMiddle.h index fa8c77ec6..2aa6e485e 100644 --- a/src/SketchPlugin/SketchPlugin_ConstraintMiddle.h +++ b/src/SketchPlugin/SketchPlugin_ConstraintMiddle.h @@ -94,9 +94,12 @@ class SketchPlugin_ConstraintMiddle : public SketchPlugin_ConstraintBase SketchPlugin_ConstraintMiddle(); private: - void CreatePoint(); + void createPoint(); + void computePointCoordinates(); std::shared_ptr myPoint; + double myX; + double myY; bool myBlockAttribInit; }; diff --git a/src/SketchPlugin/doc/middleFeature.rst b/src/SketchPlugin/doc/middleFeature.rst index a6d0142f8..18ab8f42f 100644 --- a/src/SketchPlugin/doc/middleFeature.rst +++ b/src/SketchPlugin/doc/middleFeature.rst @@ -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 index 000000000..6488c0510 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 index af1252161..000000000 Binary files a/src/SketchPlugin/icons/middlepoint_obj.png and /dev/null differ diff --git a/src/SketchPlugin/plugin-Sketch.xml b/src/SketchPlugin/plugin-Sketch.xml index 270c40f7f..bca6759a9 100644 --- a/src/SketchPlugin/plugin-Sketch.xml +++ b/src/SketchPlugin/plugin-Sketch.xml @@ -1460,7 +1460,7 @@