From 05884516451f57d3310c5bf22a951dffd4915d4e Mon Sep 17 00:00:00 2001 From: Ekaterina Sukhareva Date: Mon, 6 May 2024 17:09:09 +0100 Subject: [PATCH] Fixed edge positioning issue of [EDF] (2023-T1) Sketch middle point constrain should create point if missing --- .../SketchPlugin_ConstraintMiddle.cpp | 121 +++++++++++------- .../SketchPlugin_ConstraintMiddle.h | 5 +- src/SketchPlugin/doc/middleFeature.rst | 2 +- src/SketchPlugin/icons/middlepoint_edge.png | Bin 0 -> 671 bytes src/SketchPlugin/icons/middlepoint_obj.png | Bin 312 -> 0 bytes src/SketchPlugin/plugin-Sketch.xml | 2 +- 6 files changed, 84 insertions(+), 46 deletions(-) create mode 100755 src/SketchPlugin/icons/middlepoint_edge.png delete mode 100644 src/SketchPlugin/icons/middlepoint_obj.png 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 0000000000000000000000000000000000000000..6488c05101d4e8bf1e49c0a0a6c379a888543c24 GIT binary patch literal 671 zcmV;Q0$}}#P)EX>4Tx04R}tkv&MmKpe$iQ)@*k9qb??n4vn^MMVUcQpF-zC~bvS9ZW9$f+h_~ zii@M*T5#}VvFhOBtgC~oAP9bdI6F8gx=4xtOA0MwJvi>iyL*qjcYx5SFxBjd0;*;i zsd!Ax=2k`jD}3lh5OWxnn5ieyiy3qsU-$6v^)AA*x}W=V3@CY%0UnV!%5=jb-XNaY zv~|nWrS; zby?xO#aXS?SnHnrg@L@blIA+i5yY{C1dUyLke5pMfi_?XNa~*-z5z zZ7qBR1h;{U>$WEE0hc?#(3371k|TL(3WWmjen#Jv0|sw_{x!F^);>-jfDCoDd;=UD z0^>!>UhnemzRuqMJ=5y%2j5y{D4^006^DL_t(I z%k7b&3c_F%g}>j4f<-KwAh;qdSp>ysFbRV1;7b^N2Oq*@HEZ_)EJi^Pu^2?1$28r> zf@9b`aKpV^?m71yNC|-i3i=!wVbU-s%`+?DCmska!9=hVoCOO(Jq^n=U<%b%P>S#7 zo&IPesDy{-_0p}}Z}DAk+-&b`yLL5(^80|G6;2~3QGEJ|$_i2y6OAKpuo@Bh1|G(m zfj@}7b?Ke(gJx&QzG002ovPDHLk FV1n;UEII%H literal 0 HcmV?d00001 diff --git a/src/SketchPlugin/icons/middlepoint_obj.png b/src/SketchPlugin/icons/middlepoint_obj.png deleted file mode 100644 index af1252161e0d28dbd966b5dd5bee2f4a4fe33314..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 312 zcmeAS@N?(olHy`uVBq!ia0vp^0wB!61|;P_|4#%`EX7WqAsj$Z!;#Vf2?p zUk71ECym(^Ktah8*NBqf{Irtt#G+J&^73-M%)IR4-%f*x^(K7$MC~J4lO;MKZ9J6(rl1yf>9JH?BTwMM^Ky7v8ZuSj1A}kYi**@m` zy!xf6!*-i*PwI^F8ImVH$aBaXj1b^VIv5l1`)GpFgi>k8lh>a8y;q=HFgayU6wAA( zIoXE;7mHTLeNEwcI=yiL=M43UlQr(;$4?er!o@blC!ye#{72rHUgOs2`>RUd$ZdNY z`QUw!7(=6agxat7tj$;4PaolMa(sTfLh@b(LyMC`