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