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