1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: SketchSolver_ConstraintManager.cpp
4 // Created: 08 May 2014
5 // Author: Artem ZHIDKOV
7 #include "SketchSolver_ConstraintManager.h"
9 #include <Events_Loop.h>
10 #include <ModelAPI_AttributeDouble.h>
11 #include <ModelAPI_AttributeRefList.h>
12 #include <ModelAPI_Data.h>
13 #include <ModelAPI_Events.h>
14 #include <ModelAPI_Object.h>
15 #include <ModelAPI_ResultConstruction.h>
16 #include <ModelAPI_Attribute.h>
18 #include <SketchPlugin_Constraint.h>
19 #include <SketchPlugin_ConstraintCoincidence.h>
20 #include <SketchPlugin_ConstraintFillet.h>
21 #include <SketchPlugin_ConstraintMirror.h>
22 #include <SketchPlugin_ConstraintTangent.h>
24 #include <SketchPlugin_Arc.h>
25 #include <SketchPlugin_Circle.h>
26 #include <SketchPlugin_Line.h>
27 #include <SketchPlugin_Point.h>
28 #include <SketchPlugin_Sketch.h>
29 #include <SketchPlugin_Feature.h>
35 // Initialization of constraint manager self pointer
36 SketchSolver_ConstraintManager* SketchSolver_ConstraintManager::_self = 0;
38 /// Global constraint manager object
39 SketchSolver_ConstraintManager* myManager = SketchSolver_ConstraintManager::Instance();
41 // ========================================================
42 // ========= SketchSolver_ConstraintManager ===============
43 // ========================================================
44 SketchSolver_ConstraintManager* SketchSolver_ConstraintManager::Instance()
47 _self = new SketchSolver_ConstraintManager();
51 SketchSolver_ConstraintManager::SketchSolver_ConstraintManager()
56 // Register in event loop
57 Events_Loop::loop()->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_CREATED));
58 Events_Loop::loop()->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
59 Events_Loop::loop()->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_DELETED));
60 Events_Loop::loop()->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_MOVED));
63 SketchSolver_ConstraintManager::~SketchSolver_ConstraintManager()
68 // ============================================================================
69 // Function: processEvent
70 // Class: SketchSolver_Session
71 // Purpose: listen the event loop and process the message
72 // ============================================================================
73 void SketchSolver_ConstraintManager::processEvent(
74 const std::shared_ptr<Events_Message>& theMessage)
78 if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_CREATED)
79 || theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_UPDATED)
80 || theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_MOVED)) {
81 std::shared_ptr<ModelAPI_ObjectUpdatedMessage> anUpdateMsg =
82 std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
83 std::set<ObjectPtr> aFeatures = anUpdateMsg->objects();
85 bool isMovedEvt = theMessage->eventID()
86 == Events_Loop::loop()->eventByName(EVENT_OBJECT_MOVED);
88 std::set<ObjectPtr>::iterator aFeatIter;
89 for (aFeatIter = aFeatures.begin(); aFeatIter != aFeatures.end(); aFeatIter++) {
90 std::shared_ptr<SketchPlugin_Feature> aSFeature =
91 std::dynamic_pointer_cast<SketchPlugin_Feature>(*aFeatIter);
93 moveEntity(aSFeature);
96 std::set<ObjectPtr>::iterator aFeatIter;
97 // iterate sketchers fisrt to create all sketches before (on load may exist several sketches)
98 for (aFeatIter = aFeatures.begin(); aFeatIter != aFeatures.end(); aFeatIter++) {
99 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*aFeatIter);
102 const std::string& aFeatureKind = aFeature->getKind();
103 if (aFeatureKind.compare(SketchPlugin_Sketch::ID()) == 0) {
104 std::shared_ptr<ModelAPI_CompositeFeature> aSketch = std::dynamic_pointer_cast<
105 ModelAPI_CompositeFeature>(aFeature);
106 changeWorkplane(aSketch);
109 // then get anything but not the sketch
110 std::set<ObjectPtr> aComplexConstraints;
111 // fillet and mirror an tangency constraints will be processed later
112 for (aFeatIter = aFeatures.begin(); aFeatIter != aFeatures.end(); aFeatIter++) {
113 std::shared_ptr<SketchPlugin_Feature> aFeature =
114 std::dynamic_pointer_cast<SketchPlugin_Feature>(*aFeatIter);
117 if (aFeature->getKind() == SketchPlugin_ConstraintFillet::ID() ||
118 aFeature->getKind() == SketchPlugin_ConstraintMirror::ID() ||
119 aFeature->getKind() == SketchPlugin_ConstraintTangent::ID()) {
120 aComplexConstraints.insert(aFeature);
123 changeConstraintOrEntity(aFeature);
125 // processing remain constraints
126 aFeatIter = aComplexConstraints.begin();
127 for (; aFeatIter != aComplexConstraints.end(); aFeatIter++) {
128 std::shared_ptr<SketchPlugin_Feature> aFeature =
129 std::dynamic_pointer_cast<SketchPlugin_Feature>(*aFeatIter);
132 changeConstraintOrEntity(aFeature);
136 // Solve the set of constraints
137 resolveConstraints(isMovedEvt); // send update for movement in any case
138 } else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_DELETED)) {
139 std::shared_ptr<ModelAPI_ObjectDeletedMessage> aDeleteMsg =
140 std::dynamic_pointer_cast<ModelAPI_ObjectDeletedMessage>(theMessage);
141 const std::set<std::string>& aFeatureGroups = aDeleteMsg->groups();
143 // Find SketchPlugin_Sketch::ID() in groups. The constraint groups should be updated when an object removed from Sketch
144 std::set<std::string>::const_iterator aFGrIter;
145 for (aFGrIter = aFeatureGroups.begin(); aFGrIter != aFeatureGroups.end(); aFGrIter++)
146 if (aFGrIter->compare(ModelAPI_ResultConstruction::group()) == 0 ||
147 aFGrIter->compare(ModelAPI_Feature::group()) == 0)
150 if (aFGrIter != aFeatureGroups.end()) {
151 std::vector<SketchSolver_Group*>::iterator aGroupIter = myGroups.begin();
152 std::vector<SketchSolver_Group*> aSeparatedGroups;
153 while (aGroupIter != myGroups.end()) {
154 if (!(*aGroupIter)->isWorkplaneValid()) { // the group should be removed
156 int aShift = aGroupIter - myGroups.begin();
157 myGroups.erase(aGroupIter);
158 aGroupIter = myGroups.begin() + aShift;
161 if (!(*aGroupIter)->isConsistent()) { // some constraints were removed, try to split the group
162 (*aGroupIter)->splitGroup(aSeparatedGroups);
166 if (aSeparatedGroups.size() > 0)
167 myGroups.insert(myGroups.end(), aSeparatedGroups.begin(), aSeparatedGroups.end());
172 // ============================================================================
173 // Function: changeWorkplane
174 // Class: SketchSolver_Session
175 // Purpose: update workplane by given parameters of the sketch
176 // ============================================================================
177 bool SketchSolver_ConstraintManager::changeWorkplane(CompositeFeaturePtr theSketch)
179 bool aResult = true; // changed when a workplane wrongly updated
180 bool isUpdated = false;
181 // Try to update specified workplane in all groups
182 std::vector<SketchSolver_Group*>::iterator aGroupIter;
183 for (aGroupIter = myGroups.begin(); aGroupIter != myGroups.end(); aGroupIter++)
184 if ((*aGroupIter)->isBaseWorkplane(theSketch)) {
186 if (!(*aGroupIter)->updateWorkplane())
189 // If the workplane is not updated, so this is a new workplane
191 SketchSolver_Group* aNewGroup = new SketchSolver_Group(theSketch);
192 // Verify that the group is created successfully
193 if (!aNewGroup->isBaseWorkplane(theSketch) || !aNewGroup->isWorkplaneValid()) {
197 myGroups.push_back(aNewGroup);
202 // ============================================================================
203 // Function: changeConstraintOrEntity
204 // Class: SketchSolver_Session
205 // Purpose: create/update the constraint or the feature and place it into appropriate group
206 // ============================================================================
207 bool SketchSolver_ConstraintManager::changeConstraintOrEntity(
208 std::shared_ptr<SketchPlugin_Feature> theFeature)
210 // Search the groups which this feature touches
211 std::set<Slvs_hGroup> aGroups;
212 findGroups(theFeature, aGroups);
214 std::shared_ptr<SketchPlugin_Constraint> aConstraint =
215 std::dynamic_pointer_cast<SketchPlugin_Constraint>(theFeature);
217 // Process the groups list
218 if (aGroups.size() == 0) {
219 // There are no groups applicable for this constraint => create new one
220 // The group will be created only for constraints, not for features
221 if (!aConstraint) return false;
222 std::shared_ptr<ModelAPI_CompositeFeature> aWP = findWorkplane(aConstraint);
225 SketchSolver_Group* aGroup = new SketchSolver_Group(aWP);
226 if (!aGroup->changeConstraint(aConstraint)) {
230 myGroups.push_back(aGroup);
232 } else if (aGroups.size() == 1) { // Only one group => add feature into it
233 Slvs_hGroup aGroupId = *(aGroups.begin());
234 std::vector<SketchSolver_Group*>::iterator aGroupIter;
235 for (aGroupIter = myGroups.begin(); aGroupIter != myGroups.end(); aGroupIter++)
236 if ((*aGroupIter)->getId() == aGroupId) {
237 // If the group is empty, the feature is not added (the constraint only)
238 if (!aConstraint && !(*aGroupIter)->isEmpty())
239 return (*aGroupIter)->updateFeature(theFeature);
240 return (*aGroupIter)->changeConstraint(aConstraint);
242 } else if (aGroups.size() > 1) { // Several groups applicable for this feature => need to merge them
243 std::set<Slvs_hGroup>::const_iterator aGroupsIter = aGroups.begin();
245 // Search first group
246 std::vector<SketchSolver_Group*>::iterator aFirstGroupIter;
247 for (aFirstGroupIter = myGroups.begin(); aFirstGroupIter != myGroups.end(); aFirstGroupIter++)
248 if ((*aFirstGroupIter)->getId() == *aGroupsIter)
250 if (aFirstGroupIter == myGroups.end())
253 // Append other groups to the first one
254 std::vector<SketchSolver_Group*>::iterator anOtherGroupIter = aFirstGroupIter + 1;
255 for (aGroupsIter++; aGroupsIter != aGroups.end(); aGroupsIter++) {
256 for (; anOtherGroupIter != myGroups.end(); anOtherGroupIter++)
257 if ((*anOtherGroupIter)->getId() == *aGroupsIter)
259 if (anOtherGroupIter == myGroups.end()) { // Group disappears
260 anOtherGroupIter = aFirstGroupIter + 1;
264 (*aFirstGroupIter)->mergeGroups(**anOtherGroupIter);
265 int aShiftFirst = aFirstGroupIter - myGroups.begin();
266 int aShiftOther = anOtherGroupIter - myGroups.begin();
267 delete *anOtherGroupIter;
268 myGroups.erase(anOtherGroupIter);
269 aFirstGroupIter = myGroups.begin() + aShiftFirst;
270 anOtherGroupIter = myGroups.begin() + aShiftOther;
274 return (*aFirstGroupIter)->changeConstraint(aConstraint);
275 return (*aFirstGroupIter)->updateFeature(theFeature);
278 // Something goes wrong
282 // ============================================================================
283 // Function: moveEntity
284 // Class: SketchSolver_Session
285 // Purpose: update element moved on the sketch, which is used by constraints
286 // ============================================================================
287 void SketchSolver_ConstraintManager::moveEntity(
288 std::shared_ptr<SketchPlugin_Feature> theFeature)
290 std::vector<SketchSolver_Group*>::iterator aGroupIt = myGroups.begin();
291 for (; aGroupIt != myGroups.end(); aGroupIt++)
292 if (!(*aGroupIt)->isEmpty() && (*aGroupIt)->isInteract(theFeature))
293 (*aGroupIt)->moveFeature(theFeature);
296 // ============================================================================
297 // Function: findGroups
298 // Class: SketchSolver_Session
299 // Purpose: search groups of entities interacting with given feature
300 // ============================================================================
301 void SketchSolver_ConstraintManager::findGroups(
302 std::shared_ptr<SketchPlugin_Feature> theFeature,
303 std::set<Slvs_hGroup>& theGroupIDs) const
305 std::shared_ptr<ModelAPI_CompositeFeature> aWP = findWorkplane(theFeature);
307 SketchSolver_Group* anEmptyGroup = 0; // appropriate empty group for specified constraint
308 std::vector<SketchSolver_Group*>::const_iterator aGroupIter;
309 for (aGroupIter = myGroups.begin(); aGroupIter != myGroups.end(); aGroupIter++)
310 if (aWP == (*aGroupIter)->getWorkplane() && (*aGroupIter)->isInteract(theFeature)) {
311 if (!(*aGroupIter)->isEmpty())
312 theGroupIDs.insert((*aGroupIter)->getId());
313 else if (!anEmptyGroup)
314 anEmptyGroup = *aGroupIter;
317 // When only empty group is found, use it
318 if (anEmptyGroup && theGroupIDs.empty())
319 theGroupIDs.insert(anEmptyGroup->getId());
322 // ============================================================================
323 // Function: findWorkplane
324 // Class: SketchSolver_Session
325 // Purpose: search workplane containing given feature
326 // ============================================================================
327 std::shared_ptr<ModelAPI_CompositeFeature> SketchSolver_ConstraintManager
328 ::findWorkplane(std::shared_ptr<SketchPlugin_Feature> theFeature) const
330 // Already verified workplanes
331 std::set<std::shared_ptr<ModelAPI_CompositeFeature> > aVerified;
333 std::vector<SketchSolver_Group*>::const_iterator aGroupIter;
334 for (aGroupIter = myGroups.begin(); aGroupIter != myGroups.end(); aGroupIter++) {
335 std::shared_ptr<ModelAPI_CompositeFeature> aWP = (*aGroupIter)->getWorkplane();
336 if (aVerified.find(aWP) != aVerified.end())
339 DataPtr aData = aWP->data();
341 std::shared_ptr<ModelAPI_AttributeRefList> aWPFeatures = std::dynamic_pointer_cast<
342 ModelAPI_AttributeRefList>(aData->attribute(SketchPlugin_Sketch::FEATURES_ID()));
343 std::list<ObjectPtr> aFeaturesList = aWPFeatures->list();
344 std::list<ObjectPtr>::const_iterator anIter;
345 for (anIter = aFeaturesList.begin(); anIter != aFeaturesList.end(); anIter++)
346 if (*anIter == theFeature)
347 return aWP; // workplane is found
349 aVerified.insert(aWP);
352 return std::shared_ptr<ModelAPI_CompositeFeature>();
355 // ============================================================================
356 // Function: resolveConstraints
357 // Class: SketchSolver_Session
358 // Purpose: change entities according to available constraints
359 // ============================================================================
360 void SketchSolver_ConstraintManager::resolveConstraints(const bool theForceUpdate)
363 bool needToUpdate = false;
364 static Events_ID anUpdateEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
365 // to avoid redisplay of each segment on update by solver one by one in the viewer
366 bool isUpdateFlushed = Events_Loop::loop()->isFlushed(anUpdateEvent);
367 if (isUpdateFlushed) {
368 Events_Loop::loop()->setFlushed(anUpdateEvent, false);
371 std::vector<SketchSolver_Group*>::iterator aGroupIter;
372 for (aGroupIter = myGroups.begin(); aGroupIter != myGroups.end(); aGroupIter++)
373 if ((*aGroupIter)->resolveConstraints())
376 // Features may be updated => now send events, but for all changed at once
377 if (isUpdateFlushed) {
378 Events_Loop::loop()->setFlushed(anUpdateEvent, true);
380 // Must be before flush because on "Updated" flush the results may be produced
381 // and the creation event is appeared with many new objects. If myIsComputed these
382 // events are missed in processEvents and some elements are not added.
383 myIsComputed = false;
384 if (needToUpdate || theForceUpdate)
385 Events_Loop::loop()->flush(anUpdateEvent);