Salome HOME
Implementation of proposal 3 in the issue #2882 : Groups are not exported to GEOM...
[modules/shaper.git] / src / Model / Model_ResultBody.cpp
1 // Copyright (C) 2014-2019  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 email : webmaster.salome@opencascade.com
18 //
19
20 #include <Model_ResultBody.h>
21
22 #include <Model_Document.h>
23 #include <Model_Objects.h>
24 #include <Model_BodyBuilder.h>
25 #include <Model_Document.h>
26 #include <ModelAPI_Object.h>
27 #include <ModelAPI_Events.h>
28 #include <ModelAPI_Tools.h>
29 #include <Model_Data.h>
30 #include <Events_Loop.h>
31 #include <GeomAPI_ShapeIterator.h>
32 #include <GeomAPI_ShapeExplorer.h>
33
34 #include <TopoDS_Shape.hxx>
35 #include <TopExp_Explorer.hxx>
36 #include <TopTools_MapOfShape.hxx>
37 #include <TDataStd_UAttribute.hxx>
38
39 // if this attribute exists, the shape is connected topology
40 Standard_GUID kIsConnectedTopology("e51392e0-3a4d-405d-8e36-bbfe19858ef5");
41 // if this attribute exists, the connected topology flag must be recomputed
42 Standard_GUID kUpdateConnectedTopology("01ef7a45-0bec-4266-b0b4-4aa570921818");
43
44 Model_ResultBody::Model_ResultBody() : ModelAPI_ResultBody()
45 {
46   myBuilder = new Model_BodyBuilder(this);
47   myLastConcealed = false;
48   updateSubs(shape()); // in case of open, etc.
49 }
50
51 Model_ResultBody::~Model_ResultBody()
52 {
53   updateSubs(std::shared_ptr<GeomAPI_Shape>()); // erase sub-results
54   delete myBuilder;
55 }
56
57 bool Model_ResultBody::generated(const GeomShapePtr& theNewShape,
58   const std::string& theName, const bool theCheckIsInResult)
59 {
60   bool aResult = false;
61   if (mySubs.size()) { // consists of subs
62     for (std::vector<ResultBodyPtr>::const_iterator aSubIter = mySubs.cbegin();
63          aSubIter != mySubs.cend();
64          ++aSubIter)
65     {
66       const ResultBodyPtr& aSub = *aSubIter;
67       if (aSub->generated(theNewShape, theName, theCheckIsInResult))
68         aResult = true;
69     }
70   } else { // do for this directly
71     if (myBuilder->generated(theNewShape, theName, theCheckIsInResult))
72       aResult = true;
73   }
74   return aResult;
75 }
76
77 void Model_ResultBody::loadGeneratedShapes(const std::shared_ptr<GeomAlgoAPI_MakeShape>& theAlgo,
78                                            const GeomShapePtr& theOldShape,
79                                            const GeomAPI_Shape::ShapeType theShapeTypeToExplore,
80                                            const std::string& theName,
81                                            const bool theSaveOldIfNotInTree)
82 {
83   if (mySubs.size()) { // consists of subs
84     for (std::vector<ResultBodyPtr>::const_iterator aSubIter = mySubs.cbegin();
85          aSubIter != mySubs.cend();
86          ++aSubIter)
87     {
88       const ResultBodyPtr& aSub = *aSubIter;
89       aSub->loadGeneratedShapes(
90         theAlgo, theOldShape, theShapeTypeToExplore, theName, theSaveOldIfNotInTree);
91     }
92   } else { // do for this directly
93     myBuilder->loadGeneratedShapes(
94       theAlgo, theOldShape, theShapeTypeToExplore, theName, theSaveOldIfNotInTree);
95   }
96 }
97
98 void Model_ResultBody::loadModifiedShapes(const std::shared_ptr<GeomAlgoAPI_MakeShape>& theAlgo,
99                                           const GeomShapePtr& theOldShape,
100                                           const GeomAPI_Shape::ShapeType theShapeTypeToExplore,
101                                           const std::string& theName)
102 {
103   if (mySubs.size()) { // consists of subs
104     // optimization of getting of new shapes for specific sub-result
105     if (!theAlgo->isNewShapesCollected(theOldShape, theShapeTypeToExplore))
106       theAlgo->collectNewShapes(theOldShape, theShapeTypeToExplore);
107     std::vector<ResultBodyPtr>::const_iterator aSubIter = mySubs.cbegin();
108     for(; aSubIter != mySubs.cend(); aSubIter++) {
109       (*aSubIter)->loadModifiedShapes(theAlgo, theOldShape, theShapeTypeToExplore, theName);
110     }
111   } else { // do for this directly
112     myBuilder->loadModifiedShapes(theAlgo, theOldShape, theShapeTypeToExplore, theName);
113   }
114 }
115
116 int Model_ResultBody::numberOfSubs(bool forTree) const
117 {
118   return int(mySubs.size());
119 }
120
121 ResultBodyPtr Model_ResultBody::subResult(const int theIndex, bool forTree) const
122 {
123   if (theIndex >= int(mySubs.size()))
124     return ResultBodyPtr();
125   return mySubs.at(theIndex);
126 }
127
128 bool Model_ResultBody::isSub(ObjectPtr theResult, int& theIndex) const
129 {
130   std::map<ObjectPtr, int>::const_iterator aFound = mySubsMap.find(theResult);
131   if (aFound != mySubsMap.end()) {
132     theIndex = aFound->second;
133     return true;
134   }
135   return false;
136 }
137
138 void Model_ResultBody::colorConfigInfo(std::string& theSection, std::string& theName,
139   std::string& theDefault)
140 {
141   theSection = "Visualization";
142   theName = "result_body_color";
143   theDefault = DEFAULT_COLOR();
144 }
145
146 bool Model_ResultBody::setDisabled(std::shared_ptr<ModelAPI_Result> theThis, const bool theFlag)
147 {
148   bool aChanged = ModelAPI_ResultBody::setDisabled(theThis, theFlag);
149   if (aChanged) { // state is changed, so modifications are needed
150     updateSubs(shape(), false); // to set disabled/enabled
151   }
152   return aChanged;
153 }
154
155 bool Model_ResultBody::isConcealed()
156 {
157   return myLastConcealed;
158 }
159
160 void Model_ResultBody::setIsConcealed(const bool theValue)
161 {
162   if (ModelAPI_ResultBody::isConcealed() != theValue) {
163     ModelAPI_ResultBody::setIsConcealed(theValue);
164     updateConcealment();
165   }
166 }
167
168 // recursively check all subs for concealment flag, returns true if everybody have "flag" state,
169 // in theAll returns results with "flag" state
170 static bool checkAllSubs(ResultBodyPtr theParent, bool theFlag, std::list<ResultBodyPtr>& theAll)
171 {
172   if (theParent->isConcealed() != theFlag)
173     theAll.push_back(theParent);
174   bool aResult = theParent->ModelAPI_ResultBody::isConcealed() == theFlag;
175   for(int a = 0; a < theParent->numberOfSubs(); a++) {
176     bool aSubRes = checkAllSubs(theParent->subResult(a), theFlag, theAll);
177     if (theFlag)
178       aResult = aResult || aSubRes; // concealed: one makes concealed everyone
179     else
180       aResult = aResult && aSubRes; // not concealed: all must be not concealed
181   }
182   return aResult;
183 }
184
185 void Model_ResultBody::updateConcealment()
186 {
187   if (myLastConcealed != ModelAPI_ResultBody::isConcealed()) {
188     // check the whole tree of results: if one is concealed, everybody are concealed
189     ResultBodyPtr anOwner = std::dynamic_pointer_cast<ModelAPI_ResultBody>(data()->owner());
190     if (!anOwner.get())
191       return; // "this" is invalid
192     ResultBodyPtr aParent = ModelAPI_Tools::bodyOwner(anOwner);
193     while(aParent.get()) {
194       anOwner = aParent;
195       aParent = ModelAPI_Tools::bodyOwner(anOwner);
196     }
197     // iterate all results and collect all results whose state may be updated
198     std::list<ResultBodyPtr> anUpdated;
199     bool aNewFlag = !myLastConcealed;
200     if (checkAllSubs(anOwner, aNewFlag, anUpdated)) { // state of everyone must be updated
201       std::list<ResultBodyPtr>::iterator aRes = anUpdated.begin();
202       for(; aRes != anUpdated.end(); aRes++) {
203         bool aLastConcealed = (*aRes)->isConcealed();
204         if (aNewFlag != aLastConcealed) {
205           std::dynamic_pointer_cast<Model_ResultBody>(*aRes)->myLastConcealed = aNewFlag;
206           if (aNewFlag) { // become concealed, behaves like removed
207             ModelAPI_EventCreator::get()->sendDeleted(document(), groupName());
208           } else { // become not-concealed, behaves like created
209             static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_CREATED);
210             ModelAPI_EventCreator::get()->sendUpdated(*aRes, anEvent);
211           }
212           static Events_ID EVENT_DISP = // must be redisplayed in any case
213             Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
214           ModelAPI_EventCreator::get()->sendUpdated(*aRes, EVENT_DISP);
215         }
216       }
217     }
218   }
219 }
220
221 void Model_ResultBody::updateSubs(const std::shared_ptr<GeomAPI_Shape>& theThisShape,
222                                   const bool theShapeChanged)
223 {
224   static Events_Loop* aLoop = Events_Loop::loop();
225   static Events_ID EVENT_DISP = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
226   static Events_ID EVENT_UPD = aLoop->eventByName(EVENT_OBJECT_UPDATED);
227   static const ModelAPI_EventCreator* aECreator = ModelAPI_EventCreator::get();
228   // erase flag that topology is connected: the shape is new
229   if (theShapeChanged && data().get()) {
230     TDF_Label aDataLab = std::dynamic_pointer_cast<Model_Data>(data())->label();
231     if (!aDataLab.IsNull()) {
232       TDataStd_UAttribute::Set(aDataLab, kUpdateConnectedTopology);
233       isConnectedTopology(); // to store this flag in transaction, #2630
234     }
235   }
236   // iterate all sub-solids of compsolid to make sub-results synchronized with them
237   TopoDS_Shape aThisShape;
238   if (theThisShape.get()) aThisShape = theThisShape->impl<TopoDS_Shape>();
239   if (!aThisShape.IsNull() && (aThisShape.ShapeType() == TopAbs_COMPSOLID ||
240        aThisShape.ShapeType() == TopAbs_COMPOUND)) {
241     bool aWasEmpty = mySubs.empty();
242     Model_Objects* anObjects = std::dynamic_pointer_cast<Model_Document>(document())->objects();
243     unsigned int aSubIndex = 0;
244     TopoDS_Iterator aShapesIter(aThisShape);
245     for(; aShapesIter.More(); aShapesIter.Next(), aSubIndex++) {
246       std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape);
247       aShape->setImpl(new TopoDS_Shape(aShapesIter.Value()));
248       ResultBodyPtr aSub;
249       if (mySubs.size() <= aSubIndex) { // it is needed to create a new sub-result
250         aSub = anObjects->createBody(this->data(), aSubIndex);
251         mySubs.push_back(aSub);
252         mySubsMap[aSub] = int(mySubs.size() - 1);
253         if (isConcealed()) { // for issue #2579 note7
254           aSub->ModelAPI_ResultBody::setIsConcealed(true);
255           std::dynamic_pointer_cast<Model_ResultBody>(aSub)->updateConcealment();
256         }
257       } else { // just update shape of this result
258         aSub = mySubs[aSubIndex];
259       }
260       GeomShapePtr anOldSubShape = aSub->shape();
261       if (!aShape->isEqual(anOldSubShape)) {
262         if (myAlgo.get()) {
263           std::list<GeomShapePtr> anOldForSub;
264           computeOldForSub(aShape, myOlds, anOldForSub);
265           myIsGenerated ? aSub->storeGenerated(anOldForSub, aShape, myAlgo) :
266             aSub->storeModified(anOldForSub, aShape, myAlgo);
267         } else {
268           aSub->store(aShape, false);
269         }
270         aECreator->sendUpdated(aSub, EVENT_DISP);
271         aECreator->sendUpdated(aSub, EVENT_UPD);
272       }
273       aSub->setDisabled(aSub, isDisabled());
274     }
275     // erase left, unused results
276     while(mySubs.size() > aSubIndex) {
277       ResultBodyPtr anErased = *(mySubs.rbegin());
278       if (anErased->ModelAPI_ResultBody::isConcealed()) {
279         anErased->ModelAPI_ResultBody::setIsConcealed(false);
280         std::dynamic_pointer_cast<Model_ResultBody>(anErased)->updateConcealment();
281       }
282       anErased->setDisabled(anErased, true);
283       mySubsMap.erase(anErased);
284       mySubs.pop_back();
285     }
286     if (aWasEmpty) { // erase all subs
287       // redisplay this because result with and without subs are displayed differently
288       aECreator->sendUpdated(data()->owner(), EVENT_DISP);
289     }
290     cleanCash();
291   } else if (!mySubs.empty()) { // erase all subs
292     while(!mySubs.empty()) {
293       ResultBodyPtr anErased = *(mySubs.rbegin());
294       if (anErased->ModelAPI_ResultBody::isConcealed()) {
295         anErased->ModelAPI_ResultBody::setIsConcealed(false);
296         std::dynamic_pointer_cast<Model_ResultBody>(anErased)->updateConcealment();
297       }
298       anErased->setDisabled(anErased, true); // even if it is invalid (to erase subs on abort/undo)
299       mySubs.pop_back();
300     }
301     mySubsMap.clear();
302     // redisplay this because result with and without subs are displayed differently
303     aECreator->sendUpdated(data()->owner(), EVENT_DISP);
304   }
305 }
306
307 void Model_ResultBody::updateSubs(
308   const GeomShapePtr& theThisShape, const std::list<GeomShapePtr>& theOlds,
309   const std::shared_ptr<GeomAlgoAPI_MakeShape> theMakeShape, const bool isGenerated)
310 {
311   myAlgo = theMakeShape;
312   myOlds = theOlds;
313   myIsGenerated = isGenerated;
314   // to avoid changing of "isDisabled" flag in the "updateSubs" cycle
315   isDisabled();
316
317   updateSubs(theThisShape, true);
318   myAlgo.reset();
319   myOlds.clear();
320   myHistoryCash.Clear();
321 }
322
323
324 bool Model_ResultBody::isConnectedTopology()
325 {
326   TDF_Label aDataLab = std::dynamic_pointer_cast<Model_Data>(data())->label();
327   if (!aDataLab.IsNull()) {
328     if (aDataLab.IsAttribute(kUpdateConnectedTopology)) { // recompute state
329       aDataLab.ForgetAttribute(kUpdateConnectedTopology);
330       GeomShapePtr aShape = shape();
331       if (aShape.get() && aShape->isConnectedTopology()) {
332         TDataStd_UAttribute::Set(aDataLab, kIsConnectedTopology);
333       } else {
334         aDataLab.ForgetAttribute(kIsConnectedTopology);
335       }
336     }
337     return aDataLab.IsAttribute(kIsConnectedTopology);
338   }
339   return false; // invalid case
340 }
341
342 void Model_ResultBody::cleanCash()
343 {
344   myBuilder->cleanCash();
345   for (std::vector<ResultBodyPtr>::const_iterator aSubIter = mySubs.cbegin();
346     aSubIter != mySubs.cend(); ++aSubIter)
347   {
348     const ResultBodyPtr& aSub = *aSubIter;
349     aSub->cleanCash();
350   }
351 }
352
353 // adds to the theSubSubs map all sub-shapes of theSub if it is compound of compsolid
354 static void collectSubs(
355   const GeomShapePtr theSub, TopTools_MapOfShape& theSubSubs, const bool theOneLevelMore)
356 {
357   if (theSub->isNull())
358     return;
359   if (theSubSubs.Add(theSub->impl<TopoDS_Shape>()))  {
360     bool aIsComp = theSub->isCompound() || theSub->isCompSolid();
361     if (aIsComp) {
362       for(GeomAPI_ShapeIterator anIter(theSub); anIter.more(); anIter.next())
363         collectSubs(anIter.current(), theSubSubs, theOneLevelMore);
364     } else if (theOneLevelMore) {
365       GeomAPI_Shape::ShapeType aSubType = GeomAPI_Shape::ShapeType(int(theSub->shapeType()) + 1);
366       if (aSubType == GeomAPI_Shape::SHAPE)
367         return;
368       if (aSubType == GeomAPI_Shape::SHELL)
369         aSubType = GeomAPI_Shape::FACE;
370       if (aSubType == GeomAPI_Shape::WIRE)
371         aSubType = GeomAPI_Shape::EDGE;
372
373       for(GeomAPI_ShapeExplorer anExp(theSub, aSubType); anExp.more(); anExp.next()) {
374         collectSubs(anExp.current(), theSubSubs, false);
375       }
376     }
377   }
378 }
379
380 void Model_ResultBody::computeOldForSub(const GeomShapePtr& theSub,
381   const std::list<GeomShapePtr>& theAllOlds, std::list<GeomShapePtr>& theOldForSub)
382 {
383   // the old can be also used for sub-shape of theSub; collect all subs of compound or compsolid
384   TopTools_MapOfShape aSubSubs;
385   collectSubs(theSub, aSubSubs, false);
386
387   std::list<GeomShapePtr>::const_iterator aRootOlds = theAllOlds.cbegin();
388   for (; aRootOlds != theAllOlds.cend(); aRootOlds++) {
389     // use sub-shapes of olds too if they are compounds or compsolids
390     TopTools_MapOfShape anOldSubs;
391     // iterate one level more (for intersection of solids this is face)
392     collectSubs(*aRootOlds, anOldSubs, true);
393     for (TopTools_MapOfShape::Iterator anOldIter(anOldSubs); anOldIter.More(); anOldIter.Next()) {
394       TopoDS_Shape anOldShape = anOldIter.Value();
395       if (anOldShape.ShapeType() == TopAbs_COMPOUND || anOldShape.ShapeType() == TopAbs_SHELL ||
396           anOldShape.ShapeType() == TopAbs_WIRE)
397         continue; // container old-shapes are not supported by the history, may cause crash
398       GeomShapePtr anOldSub(new GeomAPI_Shape);
399       anOldSub->setImpl<TopoDS_Shape>(new TopoDS_Shape(anOldShape));
400
401       ListOfShape aNews;
402       if (myHistoryCash.IsBound(anOldShape)) {
403         const TopTools_ListOfShape& aList = myHistoryCash.Find(anOldShape);
404         for(TopTools_ListIteratorOfListOfShape anIter(aList); anIter.More(); anIter.Next()) {
405           GeomShapePtr aShape(new GeomAPI_Shape);
406           aShape->setImpl<TopoDS_Shape>(new TopoDS_Shape(anIter.Value()));
407           aNews.push_back(aShape);
408         }
409       } else {
410         myIsGenerated ? myAlgo->generated(anOldSub, aNews) : myAlgo->modified(anOldSub, aNews);
411         // MakeShape may return alone old shape if there is no history information for this input
412         if (aNews.size() == 1 && aNews.front()->isEqual(anOldSub))
413           aNews.clear();
414         // store result in the history
415         TopTools_ListOfShape aList;
416         for (ListOfShape::iterator aNewIter = aNews.begin(); aNewIter != aNews.end(); aNewIter++) {
417           aList.Append((*aNewIter)->impl<TopoDS_Shape>());
418         }
419         myHistoryCash.Bind(anOldShape, aList);
420       }
421
422       for (ListOfShape::iterator aNewIter = aNews.begin(); aNewIter != aNews.end(); aNewIter++) {
423         if (aSubSubs.Contains((*aNewIter)->impl<TopoDS_Shape>())) {
424           // check list already contains this sub
425           std::list<GeomShapePtr>::iterator aResIter = theOldForSub.begin();
426           for(; aResIter != theOldForSub.end(); aResIter++)
427             if ((*aResIter)->isSame(anOldSub))
428               break;
429           if (aResIter == theOldForSub.end())
430             theOldForSub.push_back(anOldSub); // found old used for new theSubShape creation
431           break;
432         }
433       }
434     }
435   }
436 }