1 // Copyright (C) 2014-2023 CEA, EDF
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "SketchPlugin_ConstraintMiddle.h"
22 #include "SketcherPrs_Factory.h"
24 #include <GeomDataAPI_Point2D.h>
26 #include <ModelAPI_Validator.h>
27 #include <ModelAPI_AttributeString.h>
29 #include <ModelAPI_Events.h>
30 #include <SketchPlugin_Point.h>
31 #include <SketchPlugin_Tools.h>
33 SketchPlugin_ConstraintMiddle::SketchPlugin_ConstraintMiddle()
37 // Create new point for Middle constraint
38 void SketchPlugin_ConstraintMiddle::CreatePoint()
40 // Wait all objects being created, then send update events
41 static Events_ID anUpdateEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
42 bool isUpdateFlushed = Events_Loop::loop()->isFlushed(anUpdateEvent);
44 Events_Loop::loop()->setFlushed(anUpdateEvent, false);
46 auto aTrPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(data()->attribute(POINT_REF_ID()));
50 // Get last subfeature (constraintMiddle) for set as parent
51 FeaturePtr aCurrentFeature = sketch()->subFeature(sketch()->numberOfSubs() - 1);
53 myPoint = sketch()->addFeature(SketchPlugin_Point::ID());
54 myPoint->boolean(SketchPlugin_SketchEntity::AUXILIARY_ID())->setValue(true);
55 restoreCurrentFeature();
57 myPoint->reference(SketchPlugin_Point::PARENT_ID())->setValue(aCurrentFeature);
60 AttributePoint2DPtr aCoord = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
61 myPoint->attribute(SketchPlugin_Point::COORD_ID()));
62 aCoord->setValue(aTrPnt->pnt());
66 // Init second attr for constraint
67 refattr(SketchPlugin_Constraint::ENTITY_B())->setObject(myPoint);
71 Events_Loop::loop()->setFlushed(anUpdateEvent, true);
74 void SketchPlugin_ConstraintMiddle::initAttributes()
76 data()->addAttribute(SketchPlugin_ConstraintMiddle::MIDDLE_TYPE(), ModelAPI_AttributeString::typeId());
78 data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefAttr::typeId());
79 data()->addAttribute(SketchPlugin_Constraint::ENTITY_B(), ModelAPI_AttributeRefAttr::typeId());
81 data()->addAttribute(POINT_REF_ID(), GeomDataAPI_Point2D::typeId());
83 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), POINT_REF_ID());
86 void SketchPlugin_ConstraintMiddle::execute()
88 if (string(MIDDLE_TYPE())->value() == MIDDLE_TYPE_BY_LINE())
90 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(data()->attribute(POINT_REF_ID()))->setValue(1., 1.);
91 AttributeRefAttrPtr aPointRes = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
92 data()->attribute(SketchPlugin_Constraint::ENTITY_B()));
94 auto aRefAttr = data()->refattr(SketchPlugin_Constraint::ENTITY_A())->object();
99 if (!attribute(ENTITY_B())->isInitialized())
100 CreatePoint(); // Create new point
104 void SketchPlugin_ConstraintMiddle::attributeChanged(const std::string& theID)
106 if (theID == MIDDLE_TYPE())
108 SketchPlugin_Tools::resetAttribute(this, ENTITY_A());
109 SketchPlugin_Tools::resetAttribute(this, ENTITY_B());
111 else if (theID == POINT_REF_ID())
116 auto aTrPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(data()->attribute(POINT_REF_ID()));
117 AttributePoint2DPtr aCoord = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
118 myPoint->attribute(SketchPlugin_Point::COORD_ID()));
119 aCoord->setValue(aTrPnt->pnt());
125 AISObjectPtr SketchPlugin_ConstraintMiddle::getAISObject(AISObjectPtr thePrevious)
129 AISObjectPtr anAIS = SketcherPrs_Factory::middleConstraint(this, sketch(),