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_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"
38 #include "SketchPlugin_ConstraintHorizontal.h"
39 #include "SketchPlugin_ConstraintVertical.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>
51 //#define DEBUG_FEATURE_OVERCONSTRAINT_LISTENER
53 PartSet_OverconstraintListener::PartSet_OverconstraintListener(ModuleBase_IWorkshop* theWorkshop)
54 : myWorkshop(theWorkshop), myIsActive(false), myIsFullyConstrained(false)
56 Events_Loop* aLoop = Events_Loop::loop();
57 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_SOLVER_FAILED));
58 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_SOLVER_REPAIRED));
60 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_SKETCH_UNDER_CONSTRAINED));
61 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_SKETCH_FULLY_CONSTRAINED));
63 aLoop->registerListener(this, ModelAPI_EventReentrantMessage::eventId());
64 aLoop->registerListener(this, SketchPlugin_MacroArcReentrantMessage::eventId());
67 void PartSet_OverconstraintListener::setActive(const bool& theActive)
69 if (myIsActive == theActive)
72 myIsActive = theActive;
73 myIsFullyConstrained = false; /// returned to default state, no custom color for it
76 PartSet_Module* aModule = module();
77 CompositeFeaturePtr aSketch = aModule->sketchMgr()->activeSketch();
79 QString aDOFMessage(aSketch->string(SketchPlugin_Sketch::SOLVER_DOF())->value().c_str());
80 if (aDOFMessage.contains('=')) {
81 // to support old data
83 aDOFMessage.right(aDOFMessage.length() - aDOFMessage.lastIndexOf('=')).trimmed();
85 myIsFullyConstrained = (aDOFMessage == "0");
90 void PartSet_OverconstraintListener::getCustomColor(const ObjectPtr& theObject,
91 std::vector<int>& theColor)
96 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
98 if (myConflictingObjects.find(theObject) != myConflictingObjects.end()) {
99 theColor = Config_PropManager::color("Visualization", "sketch_overconstraint_color");
101 if (myIsFullyConstrained) {
102 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
103 // only entity features has custom color when sketch is fully constrained
104 if (aFeature.get() && PartSet_SketcherMgr::isEntity(aFeature->getKind()) &&
105 !PartSet_SketcherMgr::isExternalFeature(aFeature)) {
106 PartSet_Module* aModule = module();
107 CompositeFeaturePtr aSketch = aModule->sketchMgr()->activeSketch();
108 // the given object is sub feature of the current sketch(created or edited)
109 if (ModelAPI_Tools::compositeOwner(aFeature) == aSketch)
110 theColor = Config_PropManager::color("Visualization", "sketch_fully_constrained_color");
115 void PartSet_OverconstraintListener::processEvent(
116 const std::shared_ptr<Events_Message>& theMessage)
118 // #2271 open document: if sketch has confilcting elements, solver sends message with the
119 // elements by opening the document. Sketch is not active, but an internal container should
120 // be updated. So, we should not check whether the listener is active here
124 #ifdef DEBUG_FEATURE_OVERCONSTRAINT_LISTENER
125 bool isRepaired = theMessage->eventID() == Events_Loop::eventByName(EVENT_SOLVER_REPAIRED);
128 std::shared_ptr<ModelAPI_SolverFailedMessage> anErrorMsg =
129 std::dynamic_pointer_cast<ModelAPI_SolverFailedMessage>(theMessage);
131 if (anErrorMsg.get()) {
132 const std::set<ObjectPtr>& aConflictingObjects = anErrorMsg->objects();
133 aCount = aConflictingObjects.size();
134 anInfoStr = getObjectsInfo(aConflictingObjects);
137 QString aCurrentInfoStr = getObjectsInfo(myConflictingObjects);
139 QString aMsg("PartSet_OverconstraintListener::processEvent: %1,\nobjects "
140 "count = %2:%3\ncurrent objects count = %4:%5");
141 qDebug(aMsg.arg(isRepaired ? "REPAIRED" : "FAILED")
142 .arg(aCount).arg(anInfoStr).arg(myConflictingObjects.size())
143 .arg(aCurrentInfoStr).toStdString().c_str());
146 Events_ID anEventID = theMessage->eventID();
147 if (anEventID == Events_Loop::eventByName(EVENT_SOLVER_FAILED) ||
148 anEventID == Events_Loop::eventByName(EVENT_SOLVER_REPAIRED)) {
149 std::shared_ptr<ModelAPI_SolverFailedMessage> anErrorMsg =
150 std::dynamic_pointer_cast<ModelAPI_SolverFailedMessage>(theMessage);
151 bool anUpdated = false;
152 if (anErrorMsg.get()) {
153 const std::set<ObjectPtr>& aConflictingObjects = anErrorMsg->objects();
154 if (anEventID == Events_Loop::eventByName(EVENT_SOLVER_FAILED))
155 anUpdated = appendConflictingObjects(aConflictingObjects);
157 anUpdated = repairConflictingObjects(aConflictingObjects);
160 else if (anEventID == Events_Loop::eventByName(EVENT_SKETCH_UNDER_CONSTRAINED) ||
161 anEventID == Events_Loop::eventByName(EVENT_SKETCH_FULLY_CONSTRAINED)) {
162 bool aPrevFullyConstrained = myIsFullyConstrained;
163 myIsFullyConstrained = anEventID == Events_Loop::eventByName(EVENT_SKETCH_FULLY_CONSTRAINED);
165 if (aPrevFullyConstrained != myIsFullyConstrained) {
166 std::set<ObjectPtr> aModifiedObjects;
167 PartSet_Module* aModule = dynamic_cast<PartSet_Module*>(myWorkshop->module());
168 CompositeFeaturePtr aSketch = aModule->sketchMgr()->activeSketch();
170 // check the sketch in the message and the active sketch are the same
171 std::shared_ptr<ModelAPI_SolverFailedMessage> anErrorMsg =
172 std::dynamic_pointer_cast<ModelAPI_SolverFailedMessage>(theMessage);
173 if (aSketch && anErrorMsg && !anErrorMsg->objects().empty()) {
174 ObjectPtr anObject = *anErrorMsg->objects().begin();
175 CompositeFeaturePtr aSketchFromMsg =
176 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(anObject);
177 if (!aSketchFromMsg || aSketchFromMsg != aSketch)
178 aSketch = CompositeFeaturePtr();
182 int aNumberOfSubs = aSketch->numberOfSubs();
183 for (int i = 0; i < aNumberOfSubs; i++) {
184 FeaturePtr aFeature = aSketch->subFeature(i);
185 aModifiedObjects.insert(aFeature); // is necessary to redisplay presentations
186 std::list<ResultPtr> aResults = aFeature->results();
187 for (std::list<ResultPtr>::const_iterator aIt = aResults.begin();
188 aIt != aResults.end(); ++aIt) {
189 aModifiedObjects.insert(*aIt);
192 redisplayObjects(aModifiedObjects);
196 else if (anEventID == ModelAPI_EventReentrantMessage::eventId() ||
197 anEventID == SketchPlugin_MacroArcReentrantMessage::eventId()) {
198 // the message is sent to sketcher reentrant manager only if the name of feature
199 // sender is equal to feature name of the current operation. E.g. Horizontal create operation
200 // is active. Sketch Line feature is changed, so execute is called, it will send message
201 // This Line's message should not be processed, as the reentrant operation is not for Line
202 // It is not enoght of kind, the name should be used, e.g. restarted Lines on auxiliary
203 // cirlce sometimes causes previous line change, kind the same, but feature line is different
204 std::wstring aCurrentFeatureName;
205 ModuleBase_Operation* anOperation =
206 XGUI_Tools::workshop(myWorkshop)->operationMgr()->currentOperation();
208 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
211 FeaturePtr aFeature = aFOperation->feature();
212 // data valid is necessary if the feature has been already deleted
213 // (e.g. Esc of Lenght if lenght value is modified)
214 if (aFeature.get() && aFeature->data()->isValid())
215 aCurrentFeatureName = aFeature->data()->name();
218 if (theMessage->sender()) {
219 ModelAPI_Object* aSender = static_cast<ModelAPI_Object*>(theMessage->sender());
221 FeaturePtr aFeatureSender =
222 std::dynamic_pointer_cast<ModelAPI_Feature>(aSender->data()->owner());
223 if (aFeatureSender.get() && aFeatureSender->data()->name() == aCurrentFeatureName) {
224 PartSet_Module* aModule = dynamic_cast<PartSet_Module*>(myWorkshop->module());
225 PartSet_SketcherReentrantMgr* aReentrantMgr = aModule->sketchReentranceMgr();
226 aReentrantMgr->setReentrantMessage(theMessage);
232 #ifdef DEBUG_FEATURE_OVERCONSTRAINT_LISTENER
233 aCurrentInfoStr = getObjectsInfo(myConflictingObjects);
234 qDebug(QString("RESULT: current objects count = %1:%2\n")
235 .arg(myConflictingObjects.size()).arg(aCurrentInfoStr).toStdString().c_str());
239 bool PartSet_OverconstraintListener::appendConflictingObjects(
240 const std::set<ObjectPtr>& theConflictingObjects)
242 std::set<ObjectPtr> aModifiedObjects;
244 // set error state for new objects and append them in the internal map of objects
245 std::set<ObjectPtr>::const_iterator
246 anIt = theConflictingObjects.begin(), aLast = theConflictingObjects.end();
248 bool isHVConstraint = false;
249 for (; anIt != aLast; anIt++) {
250 ObjectPtr anObject = *anIt;
251 if (myConflictingObjects.find(anObject) == myConflictingObjects.end()) { // it is not found
252 aModifiedObjects.insert(anObject);
253 myConflictingObjects.insert(anObject);
255 if (!isHVConstraint) {
256 aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(anObject);
258 std::string aType = aFeature->getKind();
259 isHVConstraint = (aType == SketchPlugin_ConstraintHorizontal::ID()) ||
260 (aType == SketchPlugin_ConstraintVertical::ID());
264 bool isUpdated = !aModifiedObjects.empty();
266 redisplayObjects(aModifiedObjects);
268 // If the conflicting object is an automatic constraint caused the conflict
269 // then it has to be deleted
270 if (isHVConstraint) {
271 PartSet_Module* aModule = dynamic_cast<PartSet_Module*>(myWorkshop->module());
272 QTimer::singleShot(5, aModule, SLOT(onConflictingConstraints()));
278 bool PartSet_OverconstraintListener::repairConflictingObjects(
279 const std::set<ObjectPtr>& theConflictingObjects)
281 std::set<ObjectPtr> aModifiedObjects;
282 // erase error state of absent current objects in the parameter list
283 std::set<ObjectPtr>::const_iterator anIt, aLast;
284 for (anIt = theConflictingObjects.begin(), aLast = theConflictingObjects.end();
285 anIt != aLast; anIt++) {
286 ObjectPtr anObject = *anIt;
287 if (theConflictingObjects.find(anObject) != theConflictingObjects.end()) { // it is found
288 myConflictingObjects.erase(anObject);
289 aModifiedObjects.insert(anObject);
292 bool isUpdated = !aModifiedObjects.empty();
294 redisplayObjects(aModifiedObjects);
299 void PartSet_OverconstraintListener::redisplayObjects(
300 const std::set<ObjectPtr>& theObjects)
302 static Events_Loop* aLoop = Events_Loop::loop();
304 static Events_ID EVENT_DISP = aLoop->eventByName(EVENT_VISUAL_ATTRIBUTES);
305 static const ModelAPI_EventCreator* aECreator = ModelAPI_EventCreator::get();
307 std::set<ObjectPtr>::const_iterator anIt = theObjects.begin(), aLast = theObjects.end();
308 for (; anIt != aLast; anIt++)
309 aECreator->sendUpdated(*anIt, EVENT_DISP);
311 aLoop->flush(EVENT_DISP);
314 PartSet_Module* PartSet_OverconstraintListener::module() const
316 return dynamic_cast<PartSet_Module*>(myWorkshop->module());
320 QString PartSet_OverconstraintListener::getObjectsInfo(const std::set<ObjectPtr>& theObjects)
322 std::set<ObjectPtr>::const_iterator anIt = theObjects.begin(),
323 aLast = theObjects.end();
325 for (; anIt != aLast; ++anIt)
326 anInfo.append(ModuleBase_Tools::objectInfo((*anIt)));
328 return anInfo.join(";\n");