Salome HOME
Issue #2612 : Re-calculate model after modification of parameters
[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   if (theIndex >= int(mySubs.size()))
97     return ResultBodyPtr();
98   return mySubs.at(theIndex);
99 }
100
101 bool Model_ResultBody::isSub(ObjectPtr theObject, int& theIndex) const
102 {
103   std::map<ObjectPtr, int>::const_iterator aFound = mySubsMap.find(theObject);
104   if (aFound != mySubsMap.end()) {
105     theIndex = aFound->second;
106     return true;
107   }
108   return false;
109 }
110
111 void Model_ResultBody::colorConfigInfo(std::string& theSection, std::string& theName,
112   std::string& theDefault)
113 {
114   theSection = "Visualization";
115   theName = "result_body_color";
116   theDefault = DEFAULT_COLOR();
117 }
118
119 bool Model_ResultBody::setDisabled(std::shared_ptr<ModelAPI_Result> theThis, const bool theFlag)
120 {
121   bool aChanged = ModelAPI_ResultBody::setDisabled(theThis, theFlag);
122   if (aChanged) { // state is changed, so modifications are needed
123     myBuilder->evolutionToSelection(theFlag);
124     updateSubs(shape()); // to set disabled/enabled
125   }
126   return aChanged;
127 }
128
129 bool Model_ResultBody::isConcealed()
130 {
131   return myLastConcealed;
132 }
133
134 void Model_ResultBody::setIsConcealed(const bool theValue)
135 {
136   if (ModelAPI_ResultBody::isConcealed() != theValue) {
137     ModelAPI_ResultBody::setIsConcealed(theValue);
138     updateConcealment();
139   }
140 }
141
142 // recursively check all subs for concealment flag, returns true if everybody have "flag" state,
143 // in theAll returns results with "flag" state
144 static bool checkAllSubs(ResultBodyPtr theParent, bool theFlag, std::list<ResultBodyPtr>& theAll)
145 {
146   if (theParent->isConcealed() != theFlag)
147     theAll.push_back(theParent);
148   bool aResult = theParent->ModelAPI_ResultBody::isConcealed() == theFlag;
149   for(int a = 0; a < theParent->numberOfSubs(); a++) {
150     bool aSubRes = checkAllSubs(theParent->subResult(a), theFlag, theAll);
151     if (theFlag)
152       aResult = aResult || aSubRes; // concealed: one makes concealed everyone
153     else
154       aResult = aResult && aSubRes; // not concealed: all must be not concealed
155   }
156   return aResult;
157 }
158
159 void Model_ResultBody::updateConcealment()
160 {
161   if (myLastConcealed != ModelAPI_ResultBody::isConcealed()) {
162     // check the whole tree of results: if one is concealed, everybody are concealed
163     ResultBodyPtr anOwner = std::dynamic_pointer_cast<ModelAPI_ResultBody>(data()->owner());
164     if (!anOwner.get())
165       return; // "this" is invalid
166     ResultBodyPtr aParent = ModelAPI_Tools::bodyOwner(anOwner);
167     while(aParent.get()) {
168       anOwner = aParent;
169       aParent = ModelAPI_Tools::bodyOwner(anOwner);
170     }
171     // iterate all results and collect all results whose state may be updated
172     std::list<ResultBodyPtr> anUpdated;
173     bool aNewFlag = !myLastConcealed;
174     if (checkAllSubs(anOwner, aNewFlag, anUpdated)) { // state of everyone must be updated
175       std::list<ResultBodyPtr>::iterator aRes = anUpdated.begin();
176       for(; aRes != anUpdated.end(); aRes++) {
177         bool aLastConcealed = (*aRes)->isConcealed();
178         if (aNewFlag != aLastConcealed) {
179           std::dynamic_pointer_cast<Model_ResultBody>(*aRes)->myLastConcealed = aNewFlag;
180           if (aNewFlag) { // become concealed, behaves like removed
181             ModelAPI_EventCreator::get()->sendDeleted(document(), groupName());
182           } else { // become not-concealed, behaves like created
183             static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_CREATED);
184             ModelAPI_EventCreator::get()->sendUpdated(*aRes, anEvent);
185           }
186           static Events_ID EVENT_DISP = // must be redisplayed in any case
187             Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
188           ModelAPI_EventCreator::get()->sendUpdated(*aRes, EVENT_DISP);
189         }
190       }
191     }
192   }
193 }
194
195 void Model_ResultBody::updateSubs(const std::shared_ptr<GeomAPI_Shape>& theThisShape)
196 {
197   static Events_Loop* aLoop = Events_Loop::loop();
198   static Events_ID EVENT_DISP = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
199   static Events_ID EVENT_UPD = aLoop->eventByName(EVENT_OBJECT_UPDATED);
200   static const ModelAPI_EventCreator* aECreator = ModelAPI_EventCreator::get();
201   // iterate all sub-solids of compsolid to make sub-results synchronized with them
202   TopoDS_Shape aThisShape;
203   if (theThisShape.get()) aThisShape = theThisShape->impl<TopoDS_Shape>();
204   if (!aThisShape.IsNull() && (aThisShape.ShapeType() == TopAbs_COMPSOLID ||
205        aThisShape.ShapeType() == TopAbs_COMPOUND)) {
206     bool aWasEmpty = mySubs.empty();
207     Model_Objects* anObjects = std::dynamic_pointer_cast<Model_Document>(document())->objects();
208     unsigned int aSubIndex = 0;
209     TopoDS_Iterator aShapesIter(aThisShape);
210     for(; aShapesIter.More(); aShapesIter.Next(), aSubIndex++) {
211       std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape);
212       aShape->setImpl(new TopoDS_Shape(aShapesIter.Value()));
213       ResultBodyPtr aSub;
214       if (mySubs.size() <= aSubIndex) { // it is needed to create a new sub-result
215         aSub = anObjects->createBody(this->data(), aSubIndex);
216         mySubs.push_back(aSub);
217         mySubsMap[aSub] = int(mySubs.size() - 1);
218       } else { // just update shape of this result
219         aSub = mySubs[aSubIndex];
220       }
221       if (!aShape->isEqual(aSub->shape())) {
222         aSub->store(aShape, false);
223         aECreator->sendUpdated(aSub, EVENT_DISP);
224         aECreator->sendUpdated(aSub, EVENT_UPD);
225       }
226       aSub->setDisabled(aSub, isDisabled());
227     }
228     // erase left, unused results
229     while(mySubs.size() > aSubIndex) {
230       ResultBodyPtr anErased = *(mySubs.rbegin());
231       if (anErased->ModelAPI_ResultBody::isConcealed()) {
232         anErased->ModelAPI_ResultBody::setIsConcealed(false);
233         std::dynamic_pointer_cast<Model_ResultBody>(anErased)->updateConcealment();
234       }
235       anErased->setDisabled(anErased, true);
236       mySubsMap.erase(anErased);
237       mySubs.pop_back();
238     }
239     if (aWasEmpty) { // erase all subs
240       // redisplay this because result with and without subs are displayed differently
241       aECreator->sendUpdated(data()->owner(), EVENT_DISP);
242     }
243   } else if (!mySubs.empty()) { // erase all subs
244     while(!mySubs.empty()) {
245       ResultBodyPtr anErased = *(mySubs.rbegin());
246       if (anErased->ModelAPI_ResultBody::isConcealed()) {
247         anErased->ModelAPI_ResultBody::setIsConcealed(false);
248         std::dynamic_pointer_cast<Model_ResultBody>(anErased)->updateConcealment();
249       }
250       anErased->setDisabled(anErased, true); // even if it is invalid (to erase subs on abort/undo)
251       mySubs.pop_back();
252     }
253     mySubsMap.clear();
254     // redisplay this because result with and without subs are displayed differently
255     aECreator->sendUpdated(data()->owner(), EVENT_DISP);
256   }
257 }
258
259 bool Model_ResultBody::isLatestEqual(const std::shared_ptr<GeomAPI_Shape>& theShape)
260 {
261   if (myBuilder->isLatestEqual(theShape))
262     return true;
263   // also check that it is asked for sub-elements
264   std::vector<ResultBodyPtr>::const_iterator aSubIter = mySubs.cbegin();
265   for(; aSubIter != mySubs.cend(); aSubIter++) {
266     if (aSubIter->get() && (*aSubIter)->isLatestEqual(theShape)) {
267       return true;
268     }
269   }
270   return false;
271 }