Salome HOME
Merge branch 'Results_Hierarchy'
[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     ResultBodyPtr aParent = ModelAPI_Tools::bodyOwner(anOwner);
163     while(aParent.get()) {
164       anOwner = aParent;
165       aParent = ModelAPI_Tools::bodyOwner(anOwner);
166     }
167     // iterate all results and collect all results whose state may be updated
168     std::list<ResultBodyPtr> anUpdated;
169     bool aNewFlag = !myLastConcealed;
170     if (checkAllSubs(anOwner, aNewFlag, anUpdated)) { // state of everyone must be updated
171       std::list<ResultBodyPtr>::iterator aRes = anUpdated.begin();
172       for(; aRes != anUpdated.end(); aRes++) {
173         bool aLastConcealed = (*aRes)->isConcealed();
174         if (aNewFlag != aLastConcealed) {
175           std::dynamic_pointer_cast<Model_ResultBody>(*aRes)->myLastConcealed = aNewFlag;
176           if (aNewFlag) { // become concealed, behaves like removed
177             ModelAPI_EventCreator::get()->sendDeleted(document(), groupName());
178           } else { // become not-concealed, behaves like created
179             static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_CREATED);
180             ModelAPI_EventCreator::get()->sendUpdated(*aRes, anEvent);
181           }
182           static Events_ID EVENT_DISP = // must be redisplayed in any case
183             Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
184           ModelAPI_EventCreator::get()->sendUpdated(*aRes, EVENT_DISP);
185         }
186       }
187     }
188   }
189 }
190
191 void Model_ResultBody::updateSubs(const std::shared_ptr<GeomAPI_Shape>& theThisShape)
192 {
193   static Events_Loop* aLoop = Events_Loop::loop();
194   static Events_ID EVENT_DISP = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
195   static Events_ID EVENT_UPD = aLoop->eventByName(EVENT_OBJECT_UPDATED);
196   static const ModelAPI_EventCreator* aECreator = ModelAPI_EventCreator::get();
197   // iterate all sub-solids of compsolid to make sub-results synchronized with them
198   TopoDS_Shape aThisShape;
199   if (theThisShape.get()) aThisShape = theThisShape->impl<TopoDS_Shape>();
200   if (!aThisShape.IsNull() && (aThisShape.ShapeType() == TopAbs_COMPSOLID ||
201        aThisShape.ShapeType() == TopAbs_COMPOUND)) {
202     bool aWasEmpty = mySubs.empty();
203     Model_Objects* anObjects = std::dynamic_pointer_cast<Model_Document>(document())->objects();
204     unsigned int aSubIndex = 0;
205     TopoDS_Iterator aShapesIter(aThisShape);
206     for(; aShapesIter.More(); aShapesIter.Next(), aSubIndex++) {
207       std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape);
208       aShape->setImpl(new TopoDS_Shape(aShapesIter.Value()));
209       ResultBodyPtr aSub;
210       if (mySubs.size() <= aSubIndex) { // it is needed to create a new sub-result
211         aSub = anObjects->createBody(this->data(), aSubIndex);
212         mySubs.push_back(aSub);
213         mySubsMap[aSub] = int(mySubs.size() - 1);
214       } else { // just update shape of this result
215         aSub = mySubs[aSubIndex];
216       }
217       if (!aShape->isEqual(aSub->shape())) {
218         aSub->store(aShape, false);
219         aECreator->sendUpdated(aSub, EVENT_DISP);
220         aECreator->sendUpdated(aSub, EVENT_UPD);
221       }
222       aSub->setDisabled(aSub, isDisabled());
223     }
224     // erase left, unused results
225     while(mySubs.size() > aSubIndex) {
226       ResultBodyPtr anErased = *(mySubs.rbegin());
227       if (anErased->ModelAPI_ResultBody::isConcealed()) {
228         anErased->ModelAPI_ResultBody::setIsConcealed(false);
229         std::dynamic_pointer_cast<Model_ResultBody>(anErased)->updateConcealment();
230       }
231       anErased->setDisabled(anErased, true);
232       mySubsMap.erase(anErased);
233       mySubs.pop_back();
234     }
235     if (aWasEmpty) { // erase all subs
236       // redisplay this because result with and without subs are displayed differently
237       aECreator->sendUpdated(data()->owner(), EVENT_DISP);
238     }
239   } else if (!mySubs.empty()) { // erase all subs
240     while(!mySubs.empty()) {
241       ResultBodyPtr anErased = *(mySubs.rbegin());
242       if (anErased->ModelAPI_ResultBody::isConcealed()) {
243         anErased->ModelAPI_ResultBody::setIsConcealed(false);
244         std::dynamic_pointer_cast<Model_ResultBody>(anErased)->updateConcealment();
245       }
246       anErased->setDisabled(anErased, true); // even if it is invalid (to erase subs on abort/undo)
247       mySubs.pop_back();
248     }
249     mySubsMap.clear();
250     // redisplay this because result with and without subs are displayed differently
251     aECreator->sendUpdated(data()->owner(), EVENT_DISP);
252   }
253 }
254
255 bool Model_ResultBody::isLatestEqual(const std::shared_ptr<GeomAPI_Shape>& theShape)
256 {
257   if (myBuilder->isLatestEqual(theShape))
258     return true;
259   // also check that it is asked for sub-elements
260   std::vector<ResultBodyPtr>::const_iterator aSubIter = mySubs.cbegin();
261   for(; aSubIter != mySubs.cend(); aSubIter++) {
262     if (aSubIter->get() && (*aSubIter)->isLatestEqual(theShape)) {
263       return true;
264     }
265   }
266   return false;
267 }