1 // Copyright (C) 2014-2017 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
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #include <ModelAPI_Tools.h>
22 #include <ModelAPI_AttributeString.h>
24 #include "PartSet_OverconstraintListener.h"
25 #include <PartSet_Module.h>
26 #include <PartSet_SketcherMgr.h>
27 #include <PartSet_SketcherReentrantMgr.h>
29 #include "XGUI_CustomPrs.h"
30 #include "XGUI_Displayer.h"
31 #include "XGUI_ModuleConnector.h"
32 #include "XGUI_OperationMgr.h"
33 #include "XGUI_Tools.h"
34 #include "XGUI_Workshop.h"
36 #include "SketcherPrs_SymbolPrs.h"
37 #include "SketchPlugin_SketchEntity.h"
38 #include "SketchPlugin_MacroArcReentrantMessage.h"
39 #include "SketchPlugin_Sketch.h"
41 #include "Events_Loop.h"
43 #include <GeomAPI_IPresentable.h>
44 #include <ModelAPI_Events.h>
45 #include <ModelAPI_EventReentrantMessage.h>
46 #include <ModuleBase_Tools.h>
50 //#define DEBUG_FEATURE_OVERCONSTRAINT_LISTENER
52 PartSet_OverconstraintListener::PartSet_OverconstraintListener(ModuleBase_IWorkshop* theWorkshop)
53 : myWorkshop(theWorkshop), myIsActive(false), myIsFullyConstrained(false)
55 Events_Loop* aLoop = Events_Loop::loop();
56 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_SOLVER_FAILED));
57 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_SOLVER_REPAIRED));
59 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_SKETCH_UNDER_CONSTRAINED));
60 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_SKETCH_FULLY_CONSTRAINED));
62 aLoop->registerListener(this, ModelAPI_EventReentrantMessage::eventId());
63 aLoop->registerListener(this, SketchPlugin_MacroArcReentrantMessage::eventId());
66 void PartSet_OverconstraintListener::setActive(const bool& theActive)
68 if (myIsActive == theActive)
71 myIsActive = theActive;
72 myIsFullyConstrained = false; /// returned to default state, no custom color for it
75 PartSet_Module* aModule = module();
76 CompositeFeaturePtr aSketch = aModule->sketchMgr()->activeSketch();
78 std::string aDOFMessage = aSketch->string(SketchPlugin_Sketch::SOLVER_DOF())->value();
79 myIsFullyConstrained = QString(aDOFMessage.c_str()).contains("DoF = 0");
84 void PartSet_OverconstraintListener::getCustomColor(const ObjectPtr& theObject,
85 std::vector<int>& theColor)
90 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
91 std::string aFeatureName = aFeature->data()->name();
93 if (myConflictingObjects.find(theObject) != myConflictingObjects.end()) {
94 theColor = Config_PropManager::color("Visualization", "sketch_overconstraint_color");
96 if (myIsFullyConstrained) {
97 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
98 // only entity features has custom color when sketch is fully constrained
99 if (aFeature.get() && PartSet_SketcherMgr::isEntity(aFeature->getKind()) &&
100 !PartSet_SketcherMgr::isExternalFeature(aFeature)) {
101 PartSet_Module* aModule = module();
102 CompositeFeaturePtr aSketch = aModule->sketchMgr()->activeSketch();
103 // the given object is sub feature of the current sketch(created or edited)
104 if (ModelAPI_Tools::compositeOwner(aFeature) == aSketch)
105 theColor = Config_PropManager::color("Visualization", "sketch_fully_constrained_color");
110 void PartSet_OverconstraintListener::processEvent(
111 const std::shared_ptr<Events_Message>& theMessage)
113 // #2271 open document: if sketch has confilcting elements, solver sends message with the
114 // elements by opening the document. Sketch is not active, but an internal container should
115 // be updated. So, we should not check whether the listener is active here
119 #ifdef DEBUG_FEATURE_OVERCONSTRAINT_LISTENER
120 bool isRepaired = theMessage->eventID() == Events_Loop::eventByName(EVENT_SOLVER_REPAIRED);
123 std::shared_ptr<ModelAPI_SolverFailedMessage> anErrorMsg =
124 std::dynamic_pointer_cast<ModelAPI_SolverFailedMessage>(theMessage);
126 if (anErrorMsg.get()) {
127 const std::set<ObjectPtr>& aConflictingObjects = anErrorMsg->objects();
128 aCount = aConflictingObjects.size();
129 anInfoStr = getObjectsInfo(aConflictingObjects);
132 QString aCurrentInfoStr = getObjectsInfo(myConflictingObjects);
134 QString aMsg("PartSet_OverconstraintListener::processEvent: %1,\nobjects "
135 "count = %2:%3\ncurrent objects count = %4:%5");
136 qDebug(aMsg.arg(isRepaired ? "REPAIRED" : "FAILED")
137 .arg(aCount).arg(anInfoStr).arg(myConflictingObjects.size())
138 .arg(aCurrentInfoStr).toStdString().c_str());
141 Events_ID anEventID = theMessage->eventID();
142 if (anEventID == Events_Loop::eventByName(EVENT_SOLVER_FAILED) ||
143 anEventID == Events_Loop::eventByName(EVENT_SOLVER_REPAIRED)) {
144 std::shared_ptr<ModelAPI_SolverFailedMessage> anErrorMsg =
145 std::dynamic_pointer_cast<ModelAPI_SolverFailedMessage>(theMessage);
146 bool anUpdated = false;
147 if (anErrorMsg.get()) {
148 const std::set<ObjectPtr>& aConflictingObjects = anErrorMsg->objects();
149 if (anEventID == Events_Loop::eventByName(EVENT_SOLVER_FAILED))
150 anUpdated = appendConflictingObjects(aConflictingObjects);
152 anUpdated = repairConflictingObjects(aConflictingObjects);
155 else if (anEventID == Events_Loop::eventByName(EVENT_SKETCH_UNDER_CONSTRAINED) ||
156 anEventID == Events_Loop::eventByName(EVENT_SKETCH_FULLY_CONSTRAINED)) {
157 bool aPrevFullyConstrained = myIsFullyConstrained;
158 myIsFullyConstrained = anEventID == Events_Loop::eventByName(EVENT_SKETCH_FULLY_CONSTRAINED);
160 if (aPrevFullyConstrained != myIsFullyConstrained) {
161 std::set<ObjectPtr> aModifiedObjects;
162 PartSet_Module* aModule = dynamic_cast<PartSet_Module*>(myWorkshop->module());
163 CompositeFeaturePtr aSketch = aModule->sketchMgr()->activeSketch();
165 int aNumberOfSubs = aSketch->numberOfSubs();
166 for (int i = 0; i < aNumberOfSubs; i++) {
167 FeaturePtr aFeature = aSketch->subFeature(i);
168 aModifiedObjects.insert(aFeature); // is necessary to redisplay presentations
169 std::list<ResultPtr> aResults = aFeature->results();
170 for (std::list<ResultPtr>::const_iterator aIt = aResults.begin();
171 aIt != aResults.end(); ++aIt) {
172 aModifiedObjects.insert(*aIt);
175 redisplayObjects(aModifiedObjects);
179 else if (anEventID == ModelAPI_EventReentrantMessage::eventId() ||
180 anEventID == SketchPlugin_MacroArcReentrantMessage::eventId()) {
181 // the message is sent to sketcher reentrant manager only if the name of feature
182 // sender is equal to feature name of the current operation. E.g. Horizontal create operation
183 // is active. Sketch Line feature is changed, so execute is called, it will send message
184 // This Line's message should not be processed, as the reentrant operation is not for Line
185 // It is not enoght of kind, the name should be used, e.g. restarted Lines on auxiliary
186 // cirlce sometimes causes previous line change, kind the same, but feature line is different
187 std::string aCurrentFeatureName;
188 ModuleBase_Operation* anOperation =
189 XGUI_Tools::workshop(myWorkshop)->operationMgr()->currentOperation();
191 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
194 FeaturePtr aFeature = aFOperation->feature();
195 // data valid is necessary if the feature has been already deleted
196 // (e.g. Esc of Lenght if lenght value is modified)
197 if (aFeature.get() && aFeature->data()->isValid())
198 aCurrentFeatureName = aFeature->data()->name();
201 if (theMessage->sender()) {
202 ModelAPI_Object* aSender = static_cast<ModelAPI_Object*>(theMessage->sender());
204 FeaturePtr aFeatureSender =
205 std::dynamic_pointer_cast<ModelAPI_Feature>(aSender->data()->owner());
206 if (aFeatureSender.get() && aFeatureSender->data()->name() == aCurrentFeatureName) {
207 PartSet_Module* aModule = dynamic_cast<PartSet_Module*>(myWorkshop->module());
208 PartSet_SketcherReentrantMgr* aReentrantMgr = aModule->sketchReentranceMgr();
209 aReentrantMgr->setReentrantMessage(theMessage);
215 #ifdef DEBUG_FEATURE_OVERCONSTRAINT_LISTENER
216 aCurrentInfoStr = getObjectsInfo(myConflictingObjects);
217 qDebug(QString("RESULT: current objects count = %1:%2\n")
218 .arg(myConflictingObjects.size()).arg(aCurrentInfoStr).toStdString().c_str());
222 bool PartSet_OverconstraintListener::appendConflictingObjects(
223 const std::set<ObjectPtr>& theConflictingObjects)
225 std::set<ObjectPtr> aModifiedObjects;
227 // set error state for new objects and append them in the internal map of objects
228 std::set<ObjectPtr>::const_iterator
229 anIt = theConflictingObjects.begin(), aLast = theConflictingObjects.end();
230 for (; anIt != aLast; anIt++) {
231 ObjectPtr anObject = *anIt;
232 if (myConflictingObjects.find(anObject) == myConflictingObjects.end()) { // it is not found
233 aModifiedObjects.insert(anObject);
234 myConflictingObjects.insert(anObject);
237 bool isUpdated = !aModifiedObjects.empty();
239 redisplayObjects(aModifiedObjects);
244 bool PartSet_OverconstraintListener::repairConflictingObjects(
245 const std::set<ObjectPtr>& theConflictingObjects)
247 std::set<ObjectPtr> aModifiedObjects;
248 // erase error state of absent current objects in the parameter list
249 std::set<ObjectPtr>::const_iterator anIt, aLast;
250 for (anIt = theConflictingObjects.begin(), aLast = theConflictingObjects.end();
251 anIt != aLast; anIt++) {
252 ObjectPtr anObject = *anIt;
253 if (theConflictingObjects.find(anObject) != theConflictingObjects.end()) { // it is found
254 myConflictingObjects.erase(anObject);
256 aModifiedObjects.insert(anObject);
259 bool isUpdated = !aModifiedObjects.empty();
261 redisplayObjects(aModifiedObjects);
266 void PartSet_OverconstraintListener::redisplayObjects(
267 const std::set<ObjectPtr>& theObjects)
269 static Events_Loop* aLoop = Events_Loop::loop();
271 static Events_ID EVENT_DISP = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
272 static const ModelAPI_EventCreator* aECreator = ModelAPI_EventCreator::get();
274 std::set<ObjectPtr>::const_iterator anIt = theObjects.begin(), aLast = theObjects.end();
275 for (; anIt != aLast; anIt++)
276 aECreator->sendUpdated(*anIt, EVENT_DISP);
278 aLoop->flush(EVENT_DISP);
281 PartSet_Module* PartSet_OverconstraintListener::module() const
283 return dynamic_cast<PartSet_Module*>(myWorkshop->module());
287 QString PartSet_OverconstraintListener::getObjectsInfo(const std::set<ObjectPtr>& theObjects)
289 std::set<ObjectPtr>::const_iterator anIt = theObjects.begin(),
290 aLast = theObjects.end();
292 for (; anIt != aLast; ++anIt)
293 anInfo.append(ModuleBase_Tools::objectInfo((*anIt)));
295 return anInfo.join(";\n");