Salome HOME
Fix for the issue #2528 : No more results after parameters edition canceled
[modules/shaper.git] / src / Model / Model_ResultBody.cpp
1 // Copyright (C) 2014-2017  CEA/DEN, EDF R&D
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #include <Model_ResultBody.h>
22
23 #include <Model_Document.h>
24 #include <Model_Objects.h>
25 #include <Model_BodyBuilder.h>
26 #include <Model_Document.h>
27 #include <ModelAPI_Object.h>
28 #include <ModelAPI_Events.h>
29 #include <ModelAPI_Tools.h>
30 #include <Events_Loop.h>
31
32 #include <TopoDS_Shape.hxx>
33 #include <TopExp_Explorer.hxx>
34
35
36 Model_ResultBody::Model_ResultBody() : ModelAPI_ResultBody()
37 {
38   myBuilder = new Model_BodyBuilder(this);
39   myLastConcealed = false;
40   updateSubs(shape()); // in case of open, etc.
41 }
42
43 Model_ResultBody::~Model_ResultBody()
44 {
45   updateSubs(std::shared_ptr<GeomAPI_Shape>()); // erase sub-results
46   delete myBuilder;
47 }
48
49 void Model_ResultBody::loadAndOrientModifiedShapes(GeomAlgoAPI_MakeShape* theMS,
50     std::shared_ptr<GeomAPI_Shape>  theShapeIn, const int  theKindOfShape, const int  theTag,
51     const std::string& theName, GeomAPI_DataMapOfShapeShape& theSubShapes,
52     const bool theIsStoreSeparate,
53     const bool theIsStoreAsGenerated,
54     const bool theSplitInSubs)
55 {
56   if (theSplitInSubs && mySubs.size()) { // consists of subs
57     std::vector<ResultBodyPtr>::const_iterator aSubIter = mySubs.cbegin();
58     for(; aSubIter != mySubs.cend(); aSubIter++) {
59       // check that sub-shape was also created as modification of ShapeIn
60       /* to find when it is needed later to enable: to store modification of sub-bodies not only as primitives
61       GeomShapePtr aSubGeomShape = (*aSubIter)->shape();
62       if (!theIsStoreAsGenerated && aSubGeomShape.get() && !aSubGeomShape->isNull()) {
63         TopoDS_Shape aSubShape = aSubGeomShape->impl<TopoDS_Shape>();
64         TopoDS_Shape aWholeIn = theShapeIn->impl<TopoDS_Shape>();
65         for(TopExp_Explorer anExp(aWholeIn, aSubShape.ShapeType()); anExp.More(); anExp.Next()) {
66           ListOfShape aHistory;
67           std::shared_ptr<GeomAPI_Shape> aSubIn(new GeomAPI_Shape());
68           aSubIn->setImpl((new TopoDS_Shape(anExp.Current())));
69           theMS->modified(aSubIn, aHistory);
70           std::list<std::shared_ptr<GeomAPI_Shape> >::const_iterator anIt = aHistory.begin();
71           for (; anIt != aHistory.end(); anIt++) {
72             if ((*anIt)->isSame(aSubGeomShape)) {
73               (*aSubIter)->storeModified(aSubIn, aSubGeomShape, -2); // -2 is to avoid clearing
74             }
75           }
76         }
77       }*/
78       (*aSubIter)->loadAndOrientModifiedShapes(
79         theMS, theShapeIn, theKindOfShape, theTag, theName, theSubShapes, theIsStoreSeparate,
80         theIsStoreAsGenerated, theSplitInSubs);
81     }
82   } else { // do for this directly
83     myBuilder->loadAndOrientModifiedShapes(
84       theMS, theShapeIn, theKindOfShape, theTag, theName, theSubShapes, theIsStoreSeparate,
85         theIsStoreAsGenerated);
86   }
87 }
88
89 int Model_ResultBody::numberOfSubs(bool forTree) const
90 {
91   return int(mySubs.size());
92 }
93
94 ResultBodyPtr Model_ResultBody::subResult(const int theIndex, bool forTree) const
95 {
96   return mySubs.at(theIndex);
97 }
98
99 bool Model_ResultBody::isSub(ObjectPtr theObject, int& theIndex) const
100 {
101   std::map<ObjectPtr, int>::const_iterator aFound = mySubsMap.find(theObject);
102   if (aFound != mySubsMap.end()) {
103     theIndex = aFound->second;
104     return true;
105   }
106   return false;
107 }
108
109 void Model_ResultBody::colorConfigInfo(std::string& theSection, std::string& theName,
110   std::string& theDefault)
111 {
112   theSection = "Visualization";
113   theName = "result_body_color";
114   theDefault = DEFAULT_COLOR();
115 }
116
117 bool Model_ResultBody::setDisabled(std::shared_ptr<ModelAPI_Result> theThis, const bool theFlag)
118 {
119   bool aChanged = ModelAPI_ResultBody::setDisabled(theThis, theFlag);
120   if (aChanged) { // state is changed, so modifications are needed
121     myBuilder->evolutionToSelection(theFlag);
122     updateSubs(shape()); // to set disabled/enabled
123   }
124   return aChanged;
125 }
126
127 bool Model_ResultBody::isConcealed()
128 {
129   return myLastConcealed;
130 }
131
132 void Model_ResultBody::setIsConcealed(const bool theValue)
133 {
134   if (ModelAPI_ResultBody::isConcealed() != theValue) {
135     ModelAPI_ResultBody::setIsConcealed(theValue);
136     updateConcealment();
137   }
138 }
139
140 // recursively check all subs for concealment flag, returns true if everybody have "flag" state,
141 // in theAll returns results with "flag" state
142 static bool checkAllSubs(ResultBodyPtr theParent, bool theFlag, std::list<ResultBodyPtr>& theAll)
143 {
144   if (theParent->isConcealed() != theFlag)
145     theAll.push_back(theParent);
146   bool aResult = theParent->ModelAPI_ResultBody::isConcealed() == theFlag;
147   for(int a = 0; a < theParent->numberOfSubs(); a++) {
148     bool aSubRes = checkAllSubs(theParent->subResult(a), theFlag, theAll);
149     if (theFlag)
150       aResult = aResult || aSubRes; // concealed: one makes concealed everyone
151     else
152       aResult = aResult && aSubRes; // not concealed: all must be not concealed
153   }
154   return aResult;
155 }
156
157 void Model_ResultBody::updateConcealment()
158 {
159   if (myLastConcealed != ModelAPI_ResultBody::isConcealed()) {
160     // check the whole tree of results: if one is concealed, everybody are concealed
161     ResultBodyPtr anOwner = std::dynamic_pointer_cast<ModelAPI_ResultBody>(data()->owner());
162     if (!anOwner.get())
163       return; // "this" is invalid
164     ResultBodyPtr aParent = ModelAPI_Tools::bodyOwner(anOwner);
165     while(aParent.get()) {
166       anOwner = aParent;
167       aParent = ModelAPI_Tools::bodyOwner(anOwner);
168     }
169     // iterate all results and collect all results whose state may be updated
170     std::list<ResultBodyPtr> anUpdated;
171     bool aNewFlag = !myLastConcealed;
172     if (checkAllSubs(anOwner, aNewFlag, anUpdated)) { // state of everyone must be updated
173       std::list<ResultBodyPtr>::iterator aRes = anUpdated.begin();
174       for(; aRes != anUpdated.end(); aRes++) {
175         bool aLastConcealed = (*aRes)->isConcealed();
176         if (aNewFlag != aLastConcealed) {
177           std::dynamic_pointer_cast<Model_ResultBody>(*aRes)->myLastConcealed = aNewFlag;
178           if (aNewFlag) { // become concealed, behaves like removed
179             ModelAPI_EventCreator::get()->sendDeleted(document(), groupName());
180           } else { // become not-concealed, behaves like created
181             static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_CREATED);
182             ModelAPI_EventCreator::get()->sendUpdated(*aRes, anEvent);
183           }
184           static Events_ID EVENT_DISP = // must be redisplayed in any case
185             Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
186           ModelAPI_EventCreator::get()->sendUpdated(*aRes, EVENT_DISP);
187         }
188       }
189     }
190   }
191 }
192
193 void Model_ResultBody::updateSubs(const std::shared_ptr<GeomAPI_Shape>& theThisShape)
194 {
195   static Events_Loop* aLoop = Events_Loop::loop();
196   static Events_ID EVENT_DISP = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
197   static Events_ID EVENT_UPD = aLoop->eventByName(EVENT_OBJECT_UPDATED);
198   static const ModelAPI_EventCreator* aECreator = ModelAPI_EventCreator::get();
199   // iterate all sub-solids of compsolid to make sub-results synchronized with them
200   TopoDS_Shape aThisShape;
201   if (theThisShape.get()) aThisShape = theThisShape->impl<TopoDS_Shape>();
202   if (!aThisShape.IsNull() && (aThisShape.ShapeType() == TopAbs_COMPSOLID ||
203        aThisShape.ShapeType() == TopAbs_COMPOUND)) {
204     bool aWasEmpty = mySubs.empty();
205     Model_Objects* anObjects = std::dynamic_pointer_cast<Model_Document>(document())->objects();
206     unsigned int aSubIndex = 0;
207     TopoDS_Iterator aShapesIter(aThisShape);
208     for(; aShapesIter.More(); aShapesIter.Next(), aSubIndex++) {
209       std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape);
210       aShape->setImpl(new TopoDS_Shape(aShapesIter.Value()));
211       ResultBodyPtr aSub;
212       if (mySubs.size() <= aSubIndex) { // it is needed to create a new sub-result
213         aSub = anObjects->createBody(this->data(), aSubIndex);
214         mySubs.push_back(aSub);
215         mySubsMap[aSub] = int(mySubs.size() - 1);
216       } else { // just update shape of this result
217         aSub = mySubs[aSubIndex];
218       }
219       if (!aShape->isEqual(aSub->shape())) {
220         aSub->store(aShape, false);
221         aECreator->sendUpdated(aSub, EVENT_DISP);
222         aECreator->sendUpdated(aSub, EVENT_UPD);
223       }
224       aSub->setDisabled(aSub, isDisabled());
225     }
226     // erase left, unused results
227     while(mySubs.size() > aSubIndex) {
228       ResultBodyPtr anErased = *(mySubs.rbegin());
229       if (anErased->ModelAPI_ResultBody::isConcealed()) {
230         anErased->ModelAPI_ResultBody::setIsConcealed(false);
231         std::dynamic_pointer_cast<Model_ResultBody>(anErased)->updateConcealment();
232       }
233       anErased->setDisabled(anErased, true);
234       mySubsMap.erase(anErased);
235       mySubs.pop_back();
236     }
237     if (aWasEmpty) { // erase all subs
238       // redisplay this because result with and without subs are displayed differently
239       aECreator->sendUpdated(data()->owner(), EVENT_DISP);
240     }
241   } else if (!mySubs.empty()) { // erase all subs
242     while(!mySubs.empty()) {
243       ResultBodyPtr anErased = *(mySubs.rbegin());
244       if (anErased->ModelAPI_ResultBody::isConcealed()) {
245         anErased->ModelAPI_ResultBody::setIsConcealed(false);
246         std::dynamic_pointer_cast<Model_ResultBody>(anErased)->updateConcealment();
247       }
248       anErased->setDisabled(anErased, true); // even if it is invalid (to erase subs on abort/undo)
249       mySubs.pop_back();
250     }
251     mySubsMap.clear();
252     // redisplay this because result with and without subs are displayed differently
253     aECreator->sendUpdated(data()->owner(), EVENT_DISP);
254   }
255 }
256
257 bool Model_ResultBody::isLatestEqual(const std::shared_ptr<GeomAPI_Shape>& theShape)
258 {
259   if (myBuilder->isLatestEqual(theShape))
260     return true;
261   // also check that it is asked for sub-elements
262   std::vector<ResultBodyPtr>::const_iterator aSubIter = mySubs.cbegin();
263   for(; aSubIter != mySubs.cend(); aSubIter++) {
264     if (aSubIter->get() && (*aSubIter)->isLatestEqual(theShape)) {
265       return true;
266     }
267   }
268   return false;
269 }