1 // Copyright (C) 2014-2019 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_CustomPrs.h"
29 #include "XGUI_Displayer.h"
30 #include "XGUI_ModuleConnector.h"
31 #include "XGUI_OperationMgr.h"
32 #include "XGUI_Tools.h"
33 #include "XGUI_Workshop.h"
35 #include "SketcherPrs_SymbolPrs.h"
36 #include "SketchPlugin_SketchEntity.h"
37 #include "SketchPlugin_MacroArcReentrantMessage.h"
38 #include "SketchPlugin_Sketch.h"
40 #include "Events_Loop.h"
42 #include <GeomAPI_IPresentable.h>
43 #include <ModelAPI_Events.h>
44 #include <ModelAPI_EventReentrantMessage.h>
45 #include <ModuleBase_Tools.h>
49 //#define DEBUG_FEATURE_OVERCONSTRAINT_LISTENER
51 PartSet_OverconstraintListener::PartSet_OverconstraintListener(ModuleBase_IWorkshop* theWorkshop)
52 : myWorkshop(theWorkshop), myIsActive(false), myIsFullyConstrained(false)
54 Events_Loop* aLoop = Events_Loop::loop();
55 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_SOLVER_FAILED));
56 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_SOLVER_REPAIRED));
58 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_SKETCH_UNDER_CONSTRAINED));
59 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_SKETCH_FULLY_CONSTRAINED));
61 aLoop->registerListener(this, ModelAPI_EventReentrantMessage::eventId());
62 aLoop->registerListener(this, SketchPlugin_MacroArcReentrantMessage::eventId());
65 void PartSet_OverconstraintListener::setActive(const bool& theActive)
67 if (myIsActive == theActive)
70 myIsActive = theActive;
71 myIsFullyConstrained = false; /// returned to default state, no custom color for it
74 PartSet_Module* aModule = module();
75 CompositeFeaturePtr aSketch = aModule->sketchMgr()->activeSketch();
77 std::string aDOFMessage = aSketch->string(SketchPlugin_Sketch::SOLVER_DOF())->value();
78 myIsFullyConstrained = QString(aDOFMessage.c_str()).contains("DoF = 0");
83 void PartSet_OverconstraintListener::getCustomColor(const ObjectPtr& theObject,
84 std::vector<int>& theColor)
89 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
90 std::string aFeatureName = aFeature->data()->name();
92 if (myConflictingObjects.find(theObject) != myConflictingObjects.end()) {
93 theColor = Config_PropManager::color("Visualization", "sketch_overconstraint_color");
95 if (myIsFullyConstrained) {
96 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
97 // only entity features has custom color when sketch is fully constrained
98 if (aFeature.get() && PartSet_SketcherMgr::isEntity(aFeature->getKind()) &&
99 !PartSet_SketcherMgr::isExternalFeature(aFeature)) {
100 PartSet_Module* aModule = module();
101 CompositeFeaturePtr aSketch = aModule->sketchMgr()->activeSketch();
102 // the given object is sub feature of the current sketch(created or edited)
103 if (ModelAPI_Tools::compositeOwner(aFeature) == aSketch)
104 theColor = Config_PropManager::color("Visualization", "sketch_fully_constrained_color");
109 void PartSet_OverconstraintListener::processEvent(
110 const std::shared_ptr<Events_Message>& theMessage)
112 // #2271 open document: if sketch has confilcting elements, solver sends message with the
113 // elements by opening the document. Sketch is not active, but an internal container should
114 // be updated. So, we should not check whether the listener is active here
118 #ifdef DEBUG_FEATURE_OVERCONSTRAINT_LISTENER
119 bool isRepaired = theMessage->eventID() == Events_Loop::eventByName(EVENT_SOLVER_REPAIRED);
122 std::shared_ptr<ModelAPI_SolverFailedMessage> anErrorMsg =
123 std::dynamic_pointer_cast<ModelAPI_SolverFailedMessage>(theMessage);
125 if (anErrorMsg.get()) {
126 const std::set<ObjectPtr>& aConflictingObjects = anErrorMsg->objects();
127 aCount = aConflictingObjects.size();
128 anInfoStr = getObjectsInfo(aConflictingObjects);
131 QString aCurrentInfoStr = getObjectsInfo(myConflictingObjects);
133 QString aMsg("PartSet_OverconstraintListener::processEvent: %1,\nobjects "
134 "count = %2:%3\ncurrent objects count = %4:%5");
135 qDebug(aMsg.arg(isRepaired ? "REPAIRED" : "FAILED")
136 .arg(aCount).arg(anInfoStr).arg(myConflictingObjects.size())
137 .arg(aCurrentInfoStr).toStdString().c_str());
140 Events_ID anEventID = theMessage->eventID();
141 if (anEventID == Events_Loop::eventByName(EVENT_SOLVER_FAILED) ||
142 anEventID == Events_Loop::eventByName(EVENT_SOLVER_REPAIRED)) {
143 std::shared_ptr<ModelAPI_SolverFailedMessage> anErrorMsg =
144 std::dynamic_pointer_cast<ModelAPI_SolverFailedMessage>(theMessage);
145 bool anUpdated = false;
146 if (anErrorMsg.get()) {
147 const std::set<ObjectPtr>& aConflictingObjects = anErrorMsg->objects();
148 if (anEventID == Events_Loop::eventByName(EVENT_SOLVER_FAILED))
149 anUpdated = appendConflictingObjects(aConflictingObjects);
151 anUpdated = repairConflictingObjects(aConflictingObjects);
154 else if (anEventID == Events_Loop::eventByName(EVENT_SKETCH_UNDER_CONSTRAINED) ||
155 anEventID == Events_Loop::eventByName(EVENT_SKETCH_FULLY_CONSTRAINED)) {
156 bool aPrevFullyConstrained = myIsFullyConstrained;
157 myIsFullyConstrained = anEventID == Events_Loop::eventByName(EVENT_SKETCH_FULLY_CONSTRAINED);
159 if (aPrevFullyConstrained != myIsFullyConstrained) {
160 std::set<ObjectPtr> aModifiedObjects;
161 PartSet_Module* aModule = dynamic_cast<PartSet_Module*>(myWorkshop->module());
162 CompositeFeaturePtr aSketch = aModule->sketchMgr()->activeSketch();
164 // check the sketch in the message and the active sketch are the same
165 std::shared_ptr<ModelAPI_SolverFailedMessage> anErrorMsg =
166 std::dynamic_pointer_cast<ModelAPI_SolverFailedMessage>(theMessage);
167 if (aSketch && anErrorMsg && !anErrorMsg->objects().empty()) {
168 ObjectPtr anObject = *anErrorMsg->objects().begin();
169 CompositeFeaturePtr aSketchFromMsg =
170 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(anObject);
171 if (!aSketchFromMsg || aSketchFromMsg != aSketch)
172 aSketch = CompositeFeaturePtr();
176 int aNumberOfSubs = aSketch->numberOfSubs();
177 for (int i = 0; i < aNumberOfSubs; i++) {
178 FeaturePtr aFeature = aSketch->subFeature(i);
179 aModifiedObjects.insert(aFeature); // is necessary to redisplay presentations
180 std::list<ResultPtr> aResults = aFeature->results();
181 for (std::list<ResultPtr>::const_iterator aIt = aResults.begin();
182 aIt != aResults.end(); ++aIt) {
183 aModifiedObjects.insert(*aIt);
186 redisplayObjects(aModifiedObjects);
190 else if (anEventID == ModelAPI_EventReentrantMessage::eventId() ||
191 anEventID == SketchPlugin_MacroArcReentrantMessage::eventId()) {
192 // the message is sent to sketcher reentrant manager only if the name of feature
193 // sender is equal to feature name of the current operation. E.g. Horizontal create operation
194 // is active. Sketch Line feature is changed, so execute is called, it will send message
195 // This Line's message should not be processed, as the reentrant operation is not for Line
196 // It is not enoght of kind, the name should be used, e.g. restarted Lines on auxiliary
197 // cirlce sometimes causes previous line change, kind the same, but feature line is different
198 std::string aCurrentFeatureName;
199 ModuleBase_Operation* anOperation =
200 XGUI_Tools::workshop(myWorkshop)->operationMgr()->currentOperation();
202 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
205 FeaturePtr aFeature = aFOperation->feature();
206 // data valid is necessary if the feature has been already deleted
207 // (e.g. Esc of Lenght if lenght value is modified)
208 if (aFeature.get() && aFeature->data()->isValid())
209 aCurrentFeatureName = aFeature->data()->name();
212 if (theMessage->sender()) {
213 ModelAPI_Object* aSender = static_cast<ModelAPI_Object*>(theMessage->sender());
215 FeaturePtr aFeatureSender =
216 std::dynamic_pointer_cast<ModelAPI_Feature>(aSender->data()->owner());
217 if (aFeatureSender.get() && aFeatureSender->data()->name() == aCurrentFeatureName) {
218 PartSet_Module* aModule = dynamic_cast<PartSet_Module*>(myWorkshop->module());
219 PartSet_SketcherReentrantMgr* aReentrantMgr = aModule->sketchReentranceMgr();
220 aReentrantMgr->setReentrantMessage(theMessage);
226 #ifdef DEBUG_FEATURE_OVERCONSTRAINT_LISTENER
227 aCurrentInfoStr = getObjectsInfo(myConflictingObjects);
228 qDebug(QString("RESULT: current objects count = %1:%2\n")
229 .arg(myConflictingObjects.size()).arg(aCurrentInfoStr).toStdString().c_str());
233 bool PartSet_OverconstraintListener::appendConflictingObjects(
234 const std::set<ObjectPtr>& theConflictingObjects)
236 std::set<ObjectPtr> aModifiedObjects;
238 // set error state for new objects and append them in the internal map of objects
239 std::set<ObjectPtr>::const_iterator
240 anIt = theConflictingObjects.begin(), aLast = theConflictingObjects.end();
241 for (; anIt != aLast; anIt++) {
242 ObjectPtr anObject = *anIt;
243 if (myConflictingObjects.find(anObject) == myConflictingObjects.end()) { // it is not found
244 aModifiedObjects.insert(anObject);
245 myConflictingObjects.insert(anObject);
248 bool isUpdated = !aModifiedObjects.empty();
250 redisplayObjects(aModifiedObjects);
255 bool PartSet_OverconstraintListener::repairConflictingObjects(
256 const std::set<ObjectPtr>& theConflictingObjects)
258 std::set<ObjectPtr> aModifiedObjects;
259 // erase error state of absent current objects in the parameter list
260 std::set<ObjectPtr>::const_iterator anIt, aLast;
261 for (anIt = theConflictingObjects.begin(), aLast = theConflictingObjects.end();
262 anIt != aLast; anIt++) {
263 ObjectPtr anObject = *anIt;
264 if (theConflictingObjects.find(anObject) != theConflictingObjects.end()) { // it is found
265 myConflictingObjects.erase(anObject);
267 aModifiedObjects.insert(anObject);
270 bool isUpdated = !aModifiedObjects.empty();
272 redisplayObjects(aModifiedObjects);
277 void PartSet_OverconstraintListener::redisplayObjects(
278 const std::set<ObjectPtr>& theObjects)
280 static Events_Loop* aLoop = Events_Loop::loop();
282 static Events_ID EVENT_DISP = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
283 static const ModelAPI_EventCreator* aECreator = ModelAPI_EventCreator::get();
285 std::set<ObjectPtr>::const_iterator anIt = theObjects.begin(), aLast = theObjects.end();
286 for (; anIt != aLast; anIt++)
287 aECreator->sendUpdated(*anIt, EVENT_DISP);
289 aLoop->flush(EVENT_DISP);
292 PartSet_Module* PartSet_OverconstraintListener::module() const
294 return dynamic_cast<PartSet_Module*>(myWorkshop->module());
298 QString PartSet_OverconstraintListener::getObjectsInfo(const std::set<ObjectPtr>& theObjects)
300 std::set<ObjectPtr>::const_iterator anIt = theObjects.begin(),
301 aLast = theObjects.end();
303 for (; anIt != aLast; ++anIt)
304 anInfo.append(ModuleBase_Tools::objectInfo((*anIt)));
306 return anInfo.join(";\n");