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