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