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