1 // Copyright (C) 2014-2020 CEA/DEN, EDF R&D
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 <ModelAPI_Tools.h>
21 #include <ModelAPI_AttributeString.h>
23 #include "PartSet_OverconstraintListener.h"
24 #include <PartSet_Module.h>
25 #include <PartSet_SketcherMgr.h>
26 #include <PartSet_SketcherReentrantMgr.h>
28 #include "XGUI_Displayer.h"
29 #include "XGUI_ModuleConnector.h"
30 #include "XGUI_OperationMgr.h"
31 #include "XGUI_Tools.h"
32 #include "XGUI_Workshop.h"
34 #include "SketcherPrs_SymbolPrs.h"
35 #include "SketchPlugin_SketchEntity.h"
36 #include "SketchPlugin_MacroArcReentrantMessage.h"
37 #include "SketchPlugin_Sketch.h"
39 #include "Events_Loop.h"
41 #include <GeomAPI_IPresentable.h>
42 #include <ModelAPI_Events.h>
43 #include <ModelAPI_EventReentrantMessage.h>
44 #include <ModuleBase_Tools.h>
48 //#define DEBUG_FEATURE_OVERCONSTRAINT_LISTENER
50 PartSet_OverconstraintListener::PartSet_OverconstraintListener(ModuleBase_IWorkshop* theWorkshop)
51 : myWorkshop(theWorkshop), myIsActive(false), myIsFullyConstrained(false)
53 Events_Loop* aLoop = Events_Loop::loop();
54 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_SOLVER_FAILED));
55 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_SOLVER_REPAIRED));
57 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_SKETCH_UNDER_CONSTRAINED));
58 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_SKETCH_FULLY_CONSTRAINED));
60 aLoop->registerListener(this, ModelAPI_EventReentrantMessage::eventId());
61 aLoop->registerListener(this, SketchPlugin_MacroArcReentrantMessage::eventId());
64 void PartSet_OverconstraintListener::setActive(const bool& theActive)
66 if (myIsActive == theActive)
69 myIsActive = theActive;
70 myIsFullyConstrained = false; /// returned to default state, no custom color for it
73 PartSet_Module* aModule = module();
74 CompositeFeaturePtr aSketch = aModule->sketchMgr()->activeSketch();
76 QString aDOFMessage(aSketch->string(SketchPlugin_Sketch::SOLVER_DOF())->value().c_str());
77 if (aDOFMessage.contains('=')) {
78 // to support old data
80 aDOFMessage.right(aDOFMessage.length() - aDOFMessage.lastIndexOf('=')).trimmed();
82 myIsFullyConstrained = (aDOFMessage == "0");
87 void PartSet_OverconstraintListener::getCustomColor(const ObjectPtr& theObject,
88 std::vector<int>& theColor)
93 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
94 std::string aFeatureName = aFeature->data()->name();
96 if (myConflictingObjects.find(theObject) != myConflictingObjects.end()) {
97 theColor = Config_PropManager::color("Visualization", "sketch_overconstraint_color");
99 if (myIsFullyConstrained) {
100 // only entity features has custom color when sketch is fully constrained
101 if (aFeature.get() && PartSet_SketcherMgr::isEntity(aFeature->getKind()) &&
102 !PartSet_SketcherMgr::isExternalFeature(aFeature)) {
103 PartSet_Module* aModule = module();
104 CompositeFeaturePtr aSketch = aModule->sketchMgr()->activeSketch();
105 // the given object is sub feature of the current sketch(created or edited)
106 if (ModelAPI_Tools::compositeOwner(aFeature) == aSketch)
107 theColor = Config_PropManager::color("Visualization", "sketch_fully_constrained_color");
112 void PartSet_OverconstraintListener::processEvent(
113 const std::shared_ptr<Events_Message>& theMessage)
115 // #2271 open document: if sketch has confilcting elements, solver sends message with the
116 // elements by opening the document. Sketch is not active, but an internal container should
117 // be updated. So, we should not check whether the listener is active here
121 #ifdef DEBUG_FEATURE_OVERCONSTRAINT_LISTENER
122 bool isRepaired = theMessage->eventID() == Events_Loop::eventByName(EVENT_SOLVER_REPAIRED);
125 std::shared_ptr<ModelAPI_SolverFailedMessage> anErrorMsg =
126 std::dynamic_pointer_cast<ModelAPI_SolverFailedMessage>(theMessage);
128 if (anErrorMsg.get()) {
129 const std::set<ObjectPtr>& aConflictingObjects = anErrorMsg->objects();
130 aCount = aConflictingObjects.size();
131 anInfoStr = getObjectsInfo(aConflictingObjects);
134 QString aCurrentInfoStr = getObjectsInfo(myConflictingObjects);
136 QString aMsg("PartSet_OverconstraintListener::processEvent: %1,\nobjects "
137 "count = %2:%3\ncurrent objects count = %4:%5");
138 qDebug(aMsg.arg(isRepaired ? "REPAIRED" : "FAILED")
139 .arg(aCount).arg(anInfoStr).arg(myConflictingObjects.size())
140 .arg(aCurrentInfoStr).toStdString().c_str());
143 Events_ID anEventID = theMessage->eventID();
144 if (anEventID == Events_Loop::eventByName(EVENT_SOLVER_FAILED) ||
145 anEventID == Events_Loop::eventByName(EVENT_SOLVER_REPAIRED)) {
146 std::shared_ptr<ModelAPI_SolverFailedMessage> anErrorMsg =
147 std::dynamic_pointer_cast<ModelAPI_SolverFailedMessage>(theMessage);
148 bool anUpdated = false;
149 if (anErrorMsg.get()) {
150 const std::set<ObjectPtr>& aConflictingObjects = anErrorMsg->objects();
151 if (anEventID == Events_Loop::eventByName(EVENT_SOLVER_FAILED))
152 anUpdated = appendConflictingObjects(aConflictingObjects);
154 anUpdated = repairConflictingObjects(aConflictingObjects);
157 else if (anEventID == Events_Loop::eventByName(EVENT_SKETCH_UNDER_CONSTRAINED) ||
158 anEventID == Events_Loop::eventByName(EVENT_SKETCH_FULLY_CONSTRAINED)) {
159 bool aPrevFullyConstrained = myIsFullyConstrained;
160 myIsFullyConstrained = anEventID == Events_Loop::eventByName(EVENT_SKETCH_FULLY_CONSTRAINED);
162 if (aPrevFullyConstrained != myIsFullyConstrained) {
163 std::set<ObjectPtr> aModifiedObjects;
164 PartSet_Module* aModule = dynamic_cast<PartSet_Module*>(myWorkshop->module());
165 CompositeFeaturePtr aSketch = aModule->sketchMgr()->activeSketch();
167 // check the sketch in the message and the active sketch are the same
168 std::shared_ptr<ModelAPI_SolverFailedMessage> anErrorMsg =
169 std::dynamic_pointer_cast<ModelAPI_SolverFailedMessage>(theMessage);
170 if (aSketch && anErrorMsg && !anErrorMsg->objects().empty()) {
171 ObjectPtr anObject = *anErrorMsg->objects().begin();
172 CompositeFeaturePtr aSketchFromMsg =
173 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(anObject);
174 if (!aSketchFromMsg || aSketchFromMsg != aSketch)
175 aSketch = CompositeFeaturePtr();
179 int aNumberOfSubs = aSketch->numberOfSubs();
180 for (int i = 0; i < aNumberOfSubs; i++) {
181 FeaturePtr aFeature = aSketch->subFeature(i);
182 aModifiedObjects.insert(aFeature); // is necessary to redisplay presentations
183 std::list<ResultPtr> aResults = aFeature->results();
184 for (std::list<ResultPtr>::const_iterator aIt = aResults.begin();
185 aIt != aResults.end(); ++aIt) {
186 aModifiedObjects.insert(*aIt);
189 redisplayObjects(aModifiedObjects);
193 else if (anEventID == ModelAPI_EventReentrantMessage::eventId() ||
194 anEventID == SketchPlugin_MacroArcReentrantMessage::eventId()) {
195 // the message is sent to sketcher reentrant manager only if the name of feature
196 // sender is equal to feature name of the current operation. E.g. Horizontal create operation
197 // is active. Sketch Line feature is changed, so execute is called, it will send message
198 // This Line's message should not be processed, as the reentrant operation is not for Line
199 // It is not enoght of kind, the name should be used, e.g. restarted Lines on auxiliary
200 // cirlce sometimes causes previous line change, kind the same, but feature line is different
201 std::string aCurrentFeatureName;
202 ModuleBase_Operation* anOperation =
203 XGUI_Tools::workshop(myWorkshop)->operationMgr()->currentOperation();
205 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
208 FeaturePtr aFeature = aFOperation->feature();
209 // data valid is necessary if the feature has been already deleted
210 // (e.g. Esc of Lenght if lenght value is modified)
211 if (aFeature.get() && aFeature->data()->isValid())
212 aCurrentFeatureName = aFeature->data()->name();
215 if (theMessage->sender()) {
216 ModelAPI_Object* aSender = static_cast<ModelAPI_Object*>(theMessage->sender());
218 FeaturePtr aFeatureSender =
219 std::dynamic_pointer_cast<ModelAPI_Feature>(aSender->data()->owner());
220 if (aFeatureSender.get() && aFeatureSender->data()->name() == aCurrentFeatureName) {
221 PartSet_Module* aModule = dynamic_cast<PartSet_Module*>(myWorkshop->module());
222 PartSet_SketcherReentrantMgr* aReentrantMgr = aModule->sketchReentranceMgr();
223 aReentrantMgr->setReentrantMessage(theMessage);
229 #ifdef DEBUG_FEATURE_OVERCONSTRAINT_LISTENER
230 aCurrentInfoStr = getObjectsInfo(myConflictingObjects);
231 qDebug(QString("RESULT: current objects count = %1:%2\n")
232 .arg(myConflictingObjects.size()).arg(aCurrentInfoStr).toStdString().c_str());
236 bool PartSet_OverconstraintListener::appendConflictingObjects(
237 const std::set<ObjectPtr>& theConflictingObjects)
239 std::set<ObjectPtr> aModifiedObjects;
241 // set error state for new objects and append them in the internal map of objects
242 std::set<ObjectPtr>::const_iterator
243 anIt = theConflictingObjects.begin(), aLast = theConflictingObjects.end();
244 for (; anIt != aLast; anIt++) {
245 ObjectPtr anObject = *anIt;
246 if (myConflictingObjects.find(anObject) == myConflictingObjects.end()) { // it is not found
247 aModifiedObjects.insert(anObject);
248 myConflictingObjects.insert(anObject);
251 bool isUpdated = !aModifiedObjects.empty();
253 redisplayObjects(aModifiedObjects);
258 bool PartSet_OverconstraintListener::repairConflictingObjects(
259 const std::set<ObjectPtr>& theConflictingObjects)
261 std::set<ObjectPtr> aModifiedObjects;
262 // erase error state of absent current objects in the parameter list
263 std::set<ObjectPtr>::const_iterator anIt, aLast;
264 for (anIt = theConflictingObjects.begin(), aLast = theConflictingObjects.end();
265 anIt != aLast; anIt++) {
266 ObjectPtr anObject = *anIt;
267 if (theConflictingObjects.find(anObject) != theConflictingObjects.end()) { // it is found
268 myConflictingObjects.erase(anObject);
270 aModifiedObjects.insert(anObject);
273 bool isUpdated = !aModifiedObjects.empty();
275 redisplayObjects(aModifiedObjects);
280 void PartSet_OverconstraintListener::redisplayObjects(
281 const std::set<ObjectPtr>& theObjects)
283 static Events_Loop* aLoop = Events_Loop::loop();
285 static Events_ID EVENT_DISP = aLoop->eventByName(EVENT_VISUAL_ATTRIBUTES);
286 static Events_ID EVENT_REDISP = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
287 static const ModelAPI_EventCreator* aECreator = ModelAPI_EventCreator::get();
289 std::set<ObjectPtr>::const_iterator anIt = theObjects.begin(), aLast = theObjects.end();
290 for (; anIt != aLast; anIt++) {
291 ObjectPtr aObj = *anIt;
292 aECreator->sendUpdated(aObj, EVENT_DISP);
293 aECreator->sendUpdated(aObj, EVENT_REDISP);
295 aLoop->flush(EVENT_DISP);
296 aLoop->flush(EVENT_REDISP);
299 PartSet_Module* PartSet_OverconstraintListener::module() const
301 return dynamic_cast<PartSet_Module*>(myWorkshop->module());
305 QString PartSet_OverconstraintListener::getObjectsInfo(const std::set<ObjectPtr>& theObjects)
307 std::set<ObjectPtr>::const_iterator anIt = theObjects.begin(),
308 aLast = theObjects.end();
310 for (; anIt != aLast; ++anIt)
311 anInfo.append(ModuleBase_Tools::objectInfo((*anIt)));
313 return anInfo.join(";\n");