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