1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: SketcherPrs_Angle.cpp
4 // Created: 20 August 2015
5 // Author: Vitaly SMETANNIKOV
7 #include <ModelAPI_Tools.h>
8 #include <ModelAPI_AttributeString.h>
10 #include "PartSet_OverconstraintListener.h"
11 #include <PartSet_Module.h>
12 #include <PartSet_SketcherMgr.h>
13 #include <PartSet_SketcherReentrantMgr.h>
15 #include "XGUI_CustomPrs.h"
16 #include "XGUI_Displayer.h"
17 #include "XGUI_ModuleConnector.h"
18 #include "XGUI_OperationMgr.h"
19 #include "XGUI_Tools.h"
20 #include "XGUI_Workshop.h"
22 #include "SketcherPrs_SymbolPrs.h"
23 #include "SketchPlugin_SketchEntity.h"
24 #include "SketchPlugin_MacroArcReentrantMessage.h"
25 #include "SketchPlugin_Sketch.h"
27 #include "Events_Loop.h"
29 #include <GeomAPI_IPresentable.h>
30 #include <ModelAPI_Events.h>
31 #include <ModelAPI_EventReentrantMessage.h>
32 #include <ModuleBase_Tools.h>
36 //#define DEBUG_FEATURE_OVERCONSTRAINT_LISTENER
38 PartSet_OverconstraintListener::PartSet_OverconstraintListener(ModuleBase_IWorkshop* theWorkshop)
39 : myWorkshop(theWorkshop), myIsActive(false), myIsFullyConstrained(false)
41 Events_Loop* aLoop = Events_Loop::loop();
42 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_SOLVER_FAILED));
43 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_SOLVER_REPAIRED));
45 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_SKETCH_UNDER_CONSTRAINED));
46 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_SKETCH_FULLY_CONSTRAINED));
48 aLoop->registerListener(this, ModelAPI_EventReentrantMessage::eventId());
49 aLoop->registerListener(this, SketchPlugin_MacroArcReentrantMessage::eventId());
52 void PartSet_OverconstraintListener::setActive(const bool& theActive)
54 if (myIsActive == theActive)
57 myIsActive = theActive;
58 myIsFullyConstrained = false; /// returned to default state, no custom color for it
61 PartSet_Module* aModule = module();
62 CompositeFeaturePtr aSketch = aModule->sketchMgr()->activeSketch();
64 std::string aDOFMessage = aSketch->string(SketchPlugin_Sketch::SOLVER_DOF())->value();
65 myIsFullyConstrained = QString(aDOFMessage.c_str()).contains("DoF = 0");
70 void PartSet_OverconstraintListener::getCustomColor(const ObjectPtr& theObject,
71 std::vector<int>& theColor)
76 if (myConflictingObjects.find(theObject) != myConflictingObjects.end()) {
77 theColor = Config_PropManager::color("Visualization", "sketch_overconstraint_color");
79 if (myIsFullyConstrained) {
80 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
81 // only entity features has custom color when sketch is fully constrained
82 if (aFeature.get() && PartSet_SketcherMgr::isEntity(aFeature->getKind())) {
83 PartSet_Module* aModule = module();
84 CompositeFeaturePtr aSketch = aModule->sketchMgr()->activeSketch();
85 // the given object is sub feature of the current sketch(created or edited)
86 if (ModelAPI_Tools::compositeOwner(aFeature) == aSketch)
87 theColor = Config_PropManager::color("Visualization", "sketch_fully_constrained_color");
92 void PartSet_OverconstraintListener::processEvent(
93 const std::shared_ptr<Events_Message>& theMessage)
98 #ifdef DEBUG_FEATURE_OVERCONSTRAINT_LISTENER
99 bool isRepaired = theMessage->eventID() == Events_Loop::eventByName(EVENT_SOLVER_REPAIRED);
102 std::shared_ptr<ModelAPI_SolverFailedMessage> anErrorMsg =
103 std::dynamic_pointer_cast<ModelAPI_SolverFailedMessage>(theMessage);
105 if (anErrorMsg.get()) {
106 const std::set<ObjectPtr>& aConflictingObjects = anErrorMsg->objects();
107 aCount = aConflictingObjects.size();
108 anInfoStr = getObjectsInfo(aConflictingObjects);
111 QString aCurrentInfoStr = getObjectsInfo(myConflictingObjects);
113 QString aMsg("PartSet_OverconstraintListener::processEvent: %1,\nobjects "
114 "count = %2:%3\ncurrent objects count = %4:%5");
115 qDebug(aMsg.arg(isRepaired ? "REPAIRED" : "FAILED")
116 .arg(aCount).arg(anInfoStr).arg(myConflictingObjects.size())
117 .arg(aCurrentInfoStr).toStdString().c_str());
120 Events_ID anEventID = theMessage->eventID();
121 if (anEventID == Events_Loop::eventByName(EVENT_SOLVER_FAILED) ||
122 anEventID == Events_Loop::eventByName(EVENT_SOLVER_REPAIRED)) {
123 std::shared_ptr<ModelAPI_SolverFailedMessage> anErrorMsg =
124 std::dynamic_pointer_cast<ModelAPI_SolverFailedMessage>(theMessage);
125 bool anUpdated = false;
126 if (anErrorMsg.get()) {
127 const std::set<ObjectPtr>& aConflictingObjects = anErrorMsg->objects();
128 if (anEventID == Events_Loop::eventByName(EVENT_SOLVER_FAILED))
129 anUpdated = appendConflictingObjects(aConflictingObjects);
131 anUpdated = repairConflictingObjects(aConflictingObjects);
134 else if (anEventID == Events_Loop::eventByName(EVENT_SKETCH_UNDER_CONSTRAINED) ||
135 anEventID == Events_Loop::eventByName(EVENT_SKETCH_FULLY_CONSTRAINED)) {
136 bool aPrevFullyConstrained = myIsFullyConstrained;
137 myIsFullyConstrained = anEventID == Events_Loop::eventByName(EVENT_SKETCH_FULLY_CONSTRAINED);
139 if (aPrevFullyConstrained != myIsFullyConstrained) {
140 std::set<ObjectPtr> aModifiedObjects;
141 PartSet_Module* aModule = dynamic_cast<PartSet_Module*>(myWorkshop->module());
142 CompositeFeaturePtr aSketch = aModule->sketchMgr()->activeSketch();
144 for (int i = 0; i < aSketch->numberOfSubs(); i++) {
145 FeaturePtr aFeature = aSketch->subFeature(i);
146 aModifiedObjects.insert(aFeature); // is necessary to redisplay presentations
147 std::list<ResultPtr> aResults = aFeature->results();
148 for (std::list<ResultPtr>::const_iterator aIt = aResults.begin();
149 aIt != aResults.end(); ++aIt) {
150 aModifiedObjects.insert(*aIt);
153 redisplayObjects(aModifiedObjects);
157 else if (anEventID == ModelAPI_EventReentrantMessage::eventId() ||
158 anEventID == SketchPlugin_MacroArcReentrantMessage::eventId()) {
159 // the message is sent to sketcher reentrant manager only if the kind of feature
160 // sender is equal to kind of the current operation. E.g. Horizontal create operation
161 // is active. Sketch Line feature is changed, so execute is called, it will send message
162 // This Line's message should not be processed, as the reentrant operation is not for Line
163 std::string aCurrentFeatureKind;
164 ModuleBase_Operation* anOperation =
165 XGUI_Tools::workshop(myWorkshop)->operationMgr()->currentOperation();
167 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
170 FeaturePtr aFeature = aFOperation->feature();
172 aCurrentFeatureKind = aFeature->getKind();
175 if (theMessage->sender()) {
176 ModelAPI_Object* aSender = static_cast<ModelAPI_Object*>(theMessage->sender());
178 FeaturePtr aFeatureSender =
179 std::dynamic_pointer_cast<ModelAPI_Feature>(aSender->data()->owner());
180 if (aFeatureSender.get() && aFeatureSender->getKind() == aCurrentFeatureKind) {
181 PartSet_Module* aModule = dynamic_cast<PartSet_Module*>(myWorkshop->module());
182 PartSet_SketcherReentrantMgr* aReentrantMgr = aModule->sketchReentranceMgr();
183 aReentrantMgr->setReentrantMessage(theMessage);
189 #ifdef DEBUG_FEATURE_OVERCONSTRAINT_LISTENER
190 aCurrentInfoStr = getObjectsInfo(myConflictingObjects);
191 qDebug(QString("RESULT: current objects count = %1:%2\n")
192 .arg(myConflictingObjects.size()).arg(aCurrentInfoStr).toStdString().c_str());
196 bool PartSet_OverconstraintListener::appendConflictingObjects(
197 const std::set<ObjectPtr>& theConflictingObjects)
199 std::set<ObjectPtr> aModifiedObjects;
201 // set error state for new objects and append them in the internal map of objects
202 std::set<ObjectPtr>::const_iterator
203 anIt = theConflictingObjects.begin(), aLast = theConflictingObjects.end();
204 for (; anIt != aLast; anIt++) {
205 ObjectPtr anObject = *anIt;
206 if (myConflictingObjects.find(anObject) == myConflictingObjects.end()) { // it is not found
207 aModifiedObjects.insert(anObject);
208 myConflictingObjects.insert(anObject);
211 bool isUpdated = !aModifiedObjects.empty();
213 redisplayObjects(aModifiedObjects);
218 bool PartSet_OverconstraintListener::repairConflictingObjects(
219 const std::set<ObjectPtr>& theConflictingObjects)
221 std::set<ObjectPtr> aModifiedObjects;
222 // erase error state of absent current objects in the parameter list
223 std::set<ObjectPtr>::const_iterator anIt, aLast;
224 for (anIt = theConflictingObjects.begin(), aLast = theConflictingObjects.end();
225 anIt != aLast; anIt++) {
226 ObjectPtr anObject = *anIt;
227 if (theConflictingObjects.find(anObject) != theConflictingObjects.end()) { // it is found
228 myConflictingObjects.erase(anObject);
230 aModifiedObjects.insert(anObject);
233 bool isUpdated = !aModifiedObjects.empty();
235 redisplayObjects(aModifiedObjects);
240 void PartSet_OverconstraintListener::redisplayObjects(
241 const std::set<ObjectPtr>& theObjects)
243 static Events_Loop* aLoop = Events_Loop::loop();
245 static Events_ID EVENT_DISP = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
246 static const ModelAPI_EventCreator* aECreator = ModelAPI_EventCreator::get();
248 std::set<ObjectPtr>::const_iterator anIt = theObjects.begin(), aLast = theObjects.end();
249 for (; anIt != aLast; anIt++)
250 aECreator->sendUpdated(*anIt, EVENT_DISP);
252 aLoop->flush(EVENT_DISP);
255 PartSet_Module* PartSet_OverconstraintListener::module() const
257 return dynamic_cast<PartSet_Module*>(myWorkshop->module());
261 QString PartSet_OverconstraintListener::getObjectsInfo(const std::set<ObjectPtr>& theObjects)
263 std::set<ObjectPtr>::const_iterator anIt = theObjects.begin(),
264 aLast = theObjects.end();
266 for (; anIt != aLast; ++anIt)
267 anInfo.append(ModuleBase_Tools::objectInfo((*anIt)));
269 return anInfo.join(";\n");