Salome HOME
Fix for the issue #2814 : error when dump/load script from valid document
[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 <Model_Data.h>
31 #include <Events_Loop.h>
32
33 #include <TopoDS_Shape.hxx>
34 #include <TopExp_Explorer.hxx>
35 #include <TDataStd_UAttribute.hxx>
36
37 // if this attribute exists, the shape is connected topology
38 Standard_GUID kIsConnectedTopology("e51392e0-3a4d-405d-8e36-bbfe19858ef5");
39 // if this attribute exists, the connected topology flag must be recomputed
40 Standard_GUID kUpdateConnectedTopology("01ef7a45-0bec-4266-b0b4-4aa570921818");
41
42 Model_ResultBody::Model_ResultBody() : ModelAPI_ResultBody()
43 {
44   myBuilder = new Model_BodyBuilder(this);
45   myLastConcealed = false;
46   updateSubs(shape()); // in case of open, etc.
47 }
48
49 Model_ResultBody::~Model_ResultBody()
50 {
51   updateSubs(std::shared_ptr<GeomAPI_Shape>()); // erase sub-results
52   delete myBuilder;
53 }
54
55 bool Model_ResultBody::generated(const GeomShapePtr& theNewShape,
56   const std::string& theName, const bool theCheckIsInResult)
57 {
58   bool aResult = false;
59   if (mySubs.size()) { // consists of subs
60     for (std::vector<ResultBodyPtr>::const_iterator aSubIter = mySubs.cbegin();
61          aSubIter != mySubs.cend();
62          ++aSubIter)
63     {
64       const ResultBodyPtr& aSub = *aSubIter;
65       if (aSub->generated(theNewShape, theName, theCheckIsInResult))
66         aResult = true;
67     }
68   } else { // do for this directly
69     if (myBuilder->generated(theNewShape, theName, theCheckIsInResult))
70       aResult = true;
71   }
72   return aResult;
73 }
74
75 void Model_ResultBody::loadGeneratedShapes(const std::shared_ptr<GeomAlgoAPI_MakeShape>& theAlgo,
76                                            const GeomShapePtr& theOldShape,
77                                            const GeomAPI_Shape::ShapeType theShapeTypeToExplore,
78                                            const std::string& theName)
79 {
80   if (mySubs.size()) { // consists of subs
81     for (std::vector<ResultBodyPtr>::const_iterator aSubIter = mySubs.cbegin();
82          aSubIter != mySubs.cend();
83          ++aSubIter)
84     {
85       const ResultBodyPtr& aSub = *aSubIter;
86       aSub->loadGeneratedShapes(theAlgo, theOldShape, theShapeTypeToExplore, theName);
87     }
88   } else { // do for this directly
89     myBuilder->loadGeneratedShapes(theAlgo, theOldShape, theShapeTypeToExplore, theName);
90   }
91 }
92
93 void Model_ResultBody::loadModifiedShapes(const std::shared_ptr<GeomAlgoAPI_MakeShape>& theAlgo,
94                                           const GeomShapePtr& theOldShape,
95                                           const GeomAPI_Shape::ShapeType theShapeTypeToExplore,
96                                           const std::string& theName)
97 {
98   if (/*theSplitInSubs &&*/ mySubs.size()) { // consists of subs
99     // optimization of getting of new shapes for specific sub-result
100     if (!theAlgo->isNewShapesCollected(theOldShape, theShapeTypeToExplore))
101       theAlgo->collectNewShapes(theOldShape, theShapeTypeToExplore);
102     std::vector<ResultBodyPtr>::const_iterator aSubIter = mySubs.cbegin();
103     for(; aSubIter != mySubs.cend(); aSubIter++) {
104       // check that sub-shape was also created as modification of ShapeIn
105       /* to find when it is needed later to enable: to store modification of sub-bodies not only as primitives
106       GeomShapePtr aSubGeomShape = (*aSubIter)->shape();
107       if (!theIsStoreAsGenerated && aSubGeomShape.get() && !aSubGeomShape->isNull()) {
108         TopoDS_Shape aSubShape = aSubGeomShape->impl<TopoDS_Shape>();
109         TopoDS_Shape aWholeIn = theShapeIn->impl<TopoDS_Shape>();
110         for(TopExp_Explorer anExp(aWholeIn, aSubShape.ShapeType()); anExp.More(); anExp.Next()) {
111           ListOfShape aHistory;
112           std::shared_ptr<GeomAPI_Shape> aSubIn(new GeomAPI_Shape());
113           aSubIn->setImpl((new TopoDS_Shape(anExp.Current())));
114           theMS->modified(aSubIn, aHistory);
115           std::list<std::shared_ptr<GeomAPI_Shape> >::const_iterator anIt = aHistory.begin();
116           for (; anIt != aHistory.end(); anIt++) {
117             if ((*anIt)->isSame(aSubGeomShape)) {
118               (*aSubIter)->storeModified(aSubIn, aSubGeomShape, -2); // -2 is to avoid clearing
119             }
120           }
121         }
122       }*/
123       (*aSubIter)->loadModifiedShapes(theAlgo, theOldShape, theShapeTypeToExplore, theName);
124     }
125   } else { // do for this directly
126     myBuilder->loadModifiedShapes(theAlgo, theOldShape, theShapeTypeToExplore, theName);
127   }
128 }
129
130 int Model_ResultBody::numberOfSubs(bool forTree) const
131 {
132   return int(mySubs.size());
133 }
134
135 ResultBodyPtr Model_ResultBody::subResult(const int theIndex, bool forTree) const
136 {
137   if (theIndex >= int(mySubs.size()))
138     return ResultBodyPtr();
139   return mySubs.at(theIndex);
140 }
141
142 bool Model_ResultBody::isSub(ObjectPtr theObject, int& theIndex) const
143 {
144   std::map<ObjectPtr, int>::const_iterator aFound = mySubsMap.find(theObject);
145   if (aFound != mySubsMap.end()) {
146     theIndex = aFound->second;
147     return true;
148   }
149   return false;
150 }
151
152 void Model_ResultBody::colorConfigInfo(std::string& theSection, std::string& theName,
153   std::string& theDefault)
154 {
155   theSection = "Visualization";
156   theName = "result_body_color";
157   theDefault = DEFAULT_COLOR();
158 }
159
160 bool Model_ResultBody::setDisabled(std::shared_ptr<ModelAPI_Result> theThis, const bool theFlag)
161 {
162   bool aChanged = ModelAPI_ResultBody::setDisabled(theThis, theFlag);
163   if (aChanged) { // state is changed, so modifications are needed
164     updateSubs(shape(), false); // to set disabled/enabled
165   }
166   return aChanged;
167 }
168
169 bool Model_ResultBody::isConcealed()
170 {
171   return myLastConcealed;
172 }
173
174 void Model_ResultBody::setIsConcealed(const bool theValue)
175 {
176   if (ModelAPI_ResultBody::isConcealed() != theValue) {
177     ModelAPI_ResultBody::setIsConcealed(theValue);
178     updateConcealment();
179   }
180 }
181
182 // recursively check all subs for concealment flag, returns true if everybody have "flag" state,
183 // in theAll returns results with "flag" state
184 static bool checkAllSubs(ResultBodyPtr theParent, bool theFlag, std::list<ResultBodyPtr>& theAll)
185 {
186   if (theParent->isConcealed() != theFlag)
187     theAll.push_back(theParent);
188   bool aResult = theParent->ModelAPI_ResultBody::isConcealed() == theFlag;
189   for(int a = 0; a < theParent->numberOfSubs(); a++) {
190     bool aSubRes = checkAllSubs(theParent->subResult(a), theFlag, theAll);
191     if (theFlag)
192       aResult = aResult || aSubRes; // concealed: one makes concealed everyone
193     else
194       aResult = aResult && aSubRes; // not concealed: all must be not concealed
195   }
196   return aResult;
197 }
198
199 void Model_ResultBody::updateConcealment()
200 {
201   if (myLastConcealed != ModelAPI_ResultBody::isConcealed()) {
202     // check the whole tree of results: if one is concealed, everybody are concealed
203     ResultBodyPtr anOwner = std::dynamic_pointer_cast<ModelAPI_ResultBody>(data()->owner());
204     if (!anOwner.get())
205       return; // "this" is invalid
206     ResultBodyPtr aParent = ModelAPI_Tools::bodyOwner(anOwner);
207     while(aParent.get()) {
208       anOwner = aParent;
209       aParent = ModelAPI_Tools::bodyOwner(anOwner);
210     }
211     // iterate all results and collect all results whose state may be updated
212     std::list<ResultBodyPtr> anUpdated;
213     bool aNewFlag = !myLastConcealed;
214     if (checkAllSubs(anOwner, aNewFlag, anUpdated)) { // state of everyone must be updated
215       std::list<ResultBodyPtr>::iterator aRes = anUpdated.begin();
216       for(; aRes != anUpdated.end(); aRes++) {
217         bool aLastConcealed = (*aRes)->isConcealed();
218         if (aNewFlag != aLastConcealed) {
219           std::dynamic_pointer_cast<Model_ResultBody>(*aRes)->myLastConcealed = aNewFlag;
220           if (aNewFlag) { // become concealed, behaves like removed
221             ModelAPI_EventCreator::get()->sendDeleted(document(), groupName());
222           } else { // become not-concealed, behaves like created
223             static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_CREATED);
224             ModelAPI_EventCreator::get()->sendUpdated(*aRes, anEvent);
225           }
226           static Events_ID EVENT_DISP = // must be redisplayed in any case
227             Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
228           ModelAPI_EventCreator::get()->sendUpdated(*aRes, EVENT_DISP);
229         }
230       }
231     }
232   }
233 }
234
235 void Model_ResultBody::updateSubs(const std::shared_ptr<GeomAPI_Shape>& theThisShape,
236                                   const bool theShapeChanged)
237 {
238   static Events_Loop* aLoop = Events_Loop::loop();
239   static Events_ID EVENT_DISP = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
240   static Events_ID EVENT_UPD = aLoop->eventByName(EVENT_OBJECT_UPDATED);
241   static const ModelAPI_EventCreator* aECreator = ModelAPI_EventCreator::get();
242   // erase flag that topology is connected: the shape is new
243   if (theShapeChanged && data().get()) {
244     TDF_Label aDataLab = std::dynamic_pointer_cast<Model_Data>(data())->label();
245     if (!aDataLab.IsNull()) {
246       TDataStd_UAttribute::Set(aDataLab, kUpdateConnectedTopology);
247       isConnectedTopology(); // to store this flag in transaction, #2630
248     }
249   }
250   // iterate all sub-solids of compsolid to make sub-results synchronized with them
251   TopoDS_Shape aThisShape;
252   if (theThisShape.get()) aThisShape = theThisShape->impl<TopoDS_Shape>();
253   if (!aThisShape.IsNull() && (aThisShape.ShapeType() == TopAbs_COMPSOLID ||
254        aThisShape.ShapeType() == TopAbs_COMPOUND)) {
255     bool aWasEmpty = mySubs.empty();
256     Model_Objects* anObjects = std::dynamic_pointer_cast<Model_Document>(document())->objects();
257     unsigned int aSubIndex = 0;
258     TopoDS_Iterator aShapesIter(aThisShape);
259     for(; aShapesIter.More(); aShapesIter.Next(), aSubIndex++) {
260       std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape);
261       aShape->setImpl(new TopoDS_Shape(aShapesIter.Value()));
262       ResultBodyPtr aSub;
263       if (mySubs.size() <= aSubIndex) { // it is needed to create a new sub-result
264         aSub = anObjects->createBody(this->data(), aSubIndex);
265         mySubs.push_back(aSub);
266         mySubsMap[aSub] = int(mySubs.size() - 1);
267         if (isConcealed()) { // for issue #2579 note7
268           aSub->ModelAPI_ResultBody::setIsConcealed(true);
269           std::dynamic_pointer_cast<Model_ResultBody>(aSub)->updateConcealment();
270         }
271       } else { // just update shape of this result
272         aSub = mySubs[aSubIndex];
273       }
274       GeomShapePtr anOldSubShape = aSub->shape();
275       aSub->store(aShape, false); // store even equal to call "clear": #2814
276       if (!aShape->isEqual(anOldSubShape)) {
277         aECreator->sendUpdated(aSub, EVENT_DISP);
278         aECreator->sendUpdated(aSub, EVENT_UPD);
279       }
280       aSub->setDisabled(aSub, isDisabled());
281     }
282     // erase left, unused results
283     while(mySubs.size() > aSubIndex) {
284       ResultBodyPtr anErased = *(mySubs.rbegin());
285       if (anErased->ModelAPI_ResultBody::isConcealed()) {
286         anErased->ModelAPI_ResultBody::setIsConcealed(false);
287         std::dynamic_pointer_cast<Model_ResultBody>(anErased)->updateConcealment();
288       }
289       anErased->setDisabled(anErased, true);
290       mySubsMap.erase(anErased);
291       mySubs.pop_back();
292     }
293     if (aWasEmpty) { // erase all subs
294       // redisplay this because result with and without subs are displayed differently
295       aECreator->sendUpdated(data()->owner(), EVENT_DISP);
296     }
297   } else if (!mySubs.empty()) { // erase all subs
298     while(!mySubs.empty()) {
299       ResultBodyPtr anErased = *(mySubs.rbegin());
300       if (anErased->ModelAPI_ResultBody::isConcealed()) {
301         anErased->ModelAPI_ResultBody::setIsConcealed(false);
302         std::dynamic_pointer_cast<Model_ResultBody>(anErased)->updateConcealment();
303       }
304       anErased->setDisabled(anErased, true); // even if it is invalid (to erase subs on abort/undo)
305       mySubs.pop_back();
306     }
307     mySubsMap.clear();
308     // redisplay this because result with and without subs are displayed differently
309     aECreator->sendUpdated(data()->owner(), EVENT_DISP);
310   }
311 }
312
313 bool Model_ResultBody::isConnectedTopology()
314 {
315   TDF_Label aDataLab = std::dynamic_pointer_cast<Model_Data>(data())->label();
316   if (!aDataLab.IsNull()) {
317     if (aDataLab.IsAttribute(kUpdateConnectedTopology)) { // recompute state
318       aDataLab.ForgetAttribute(kUpdateConnectedTopology);
319       GeomShapePtr aShape = shape();
320       if (aShape.get() && aShape->isConnectedTopology()) {
321         TDataStd_UAttribute::Set(aDataLab, kIsConnectedTopology);
322       } else {
323         aDataLab.ForgetAttribute(kIsConnectedTopology);
324       }
325     }
326     return aDataLab.IsAttribute(kIsConnectedTopology);
327   }
328   return false; // invalid case
329 }