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