1 // Copyright (C) 2014-2020 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include <Model_ResultBody.h>
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>
34 #include <TopoDS_Shape.hxx>
35 #include <TopExp_Explorer.hxx>
36 #include <TopTools_MapOfShape.hxx>
37 #include <TDataStd_UAttribute.hxx>
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");
44 Model_ResultBody::Model_ResultBody() : ModelAPI_ResultBody()
46 myBuilder = new Model_BodyBuilder(this);
47 myLastConcealed = false;
48 updateSubs(shape()); // in case of open, etc.
51 Model_ResultBody::~Model_ResultBody()
53 updateSubs(std::shared_ptr<GeomAPI_Shape>()); // erase sub-results
57 bool Model_ResultBody::generated(const GeomShapePtr& theNewShape,
58 const std::string& theName, const bool theCheckIsInResult)
61 if (mySubs.size()) { // consists of subs
62 for (std::vector<ResultBodyPtr>::const_iterator aSubIter = mySubs.cbegin();
63 aSubIter != mySubs.cend();
66 const ResultBodyPtr& aSub = *aSubIter;
67 if (aSub->generated(theNewShape, theName, theCheckIsInResult))
70 } else { // do for this directly
71 if (myBuilder->generated(theNewShape, theName, theCheckIsInResult))
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)
83 if (mySubs.size()) { // consists of subs
84 for (std::vector<ResultBodyPtr>::const_iterator aSubIter = mySubs.cbegin();
85 aSubIter != mySubs.cend();
88 const ResultBodyPtr& aSub = *aSubIter;
89 aSub->loadGeneratedShapes(
90 theAlgo, theOldShape, theShapeTypeToExplore, theName, theSaveOldIfNotInTree);
92 } else { // do for this directly
93 myBuilder->loadGeneratedShapes(
94 theAlgo, theOldShape, theShapeTypeToExplore, theName, theSaveOldIfNotInTree);
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)
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);
111 } else { // do for this directly
112 myBuilder->loadModifiedShapes(theAlgo, theOldShape, theShapeTypeToExplore, theName);
116 void Model_ResultBody::loadFirstLevel(GeomShapePtr theShape, const std::string& theName)
118 if (mySubs.size()) { // consists of subs
119 for (std::vector<ResultBodyPtr>::const_iterator aSubIter = mySubs.cbegin();
120 aSubIter != mySubs.cend();
123 const ResultBodyPtr& aSub = *aSubIter;
124 aSub->loadFirstLevel(theShape, theName);
126 } else { // do for this directly
127 myBuilder->loadFirstLevel(theShape, theName);
131 int Model_ResultBody::numberOfSubs(bool /*forTree*/) const
133 return int(mySubs.size());
136 ResultBodyPtr Model_ResultBody::subResult(const int theIndex, bool /*forTree*/) const
138 if (theIndex >= int(mySubs.size()))
139 return ResultBodyPtr();
140 return mySubs.at(theIndex);
143 bool Model_ResultBody::isSub(ObjectPtr theResult, int& theIndex) const
145 std::map<ObjectPtr, int>::const_iterator aFound = mySubsMap.find(theResult);
146 if (aFound != mySubsMap.end()) {
147 theIndex = aFound->second;
153 void Model_ResultBody::colorConfigInfo(std::string& theSection, std::string& theName,
154 std::string& theDefault)
156 theSection = "Visualization";
157 theName = "result_body_color";
158 theDefault = DEFAULT_COLOR();
161 bool Model_ResultBody::setDisabled(std::shared_ptr<ModelAPI_Result> theThis, const bool theFlag)
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
170 bool Model_ResultBody::isConcealed()
172 return myLastConcealed;
175 void Model_ResultBody::setIsConcealed(const bool theValue, const bool theForced)
177 if (ModelAPI_ResultBody::isConcealed() != theValue) {
178 ModelAPI_ResultBody::setIsConcealed(theValue, theForced);
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)
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);
193 aResult = aResult || aSubRes; // concealed: one makes concealed everyone
195 aResult = aResult && aSubRes; // not concealed: all must be not concealed
200 void Model_ResultBody::updateConcealment()
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());
206 return; // "this" is invalid
207 ResultBodyPtr aParent = ModelAPI_Tools::bodyOwner(anOwner);
208 while(aParent.get()) {
210 aParent = ModelAPI_Tools::bodyOwner(anOwner);
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);
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);
236 void Model_ResultBody::updateSubs(const std::shared_ptr<GeomAPI_Shape>& theThisShape,
237 const bool theShapeChanged)
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
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()));
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();
272 } else { // just update shape of this result
273 aSub = mySubs[aSubIndex];
275 GeomShapePtr anOldSubShape = aSub->shape();
276 if (!aShape->isEqual(anOldSubShape)) {
278 std::list<GeomShapePtr> anOldForSub;
279 computeOldForSub(aShape, myOlds, anOldForSub);
280 myIsGenerated ? aSub->storeGenerated(anOldForSub, aShape, myAlgo) :
281 aSub->storeModified(anOldForSub, aShape, myAlgo);
283 aSub->store(aShape, false);
285 aECreator->sendUpdated(aSub, EVENT_DISP);
286 aECreator->sendUpdated(aSub, EVENT_UPD);
288 aSub->setDisabled(aSub, isDisabled());
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();
297 anErased->setDisabled(anErased, true);
298 mySubsMap.erase(anErased);
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);
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();
313 anErased->setDisabled(anErased, true); // even if it is invalid (to erase subs on abort/undo)
317 // redisplay this because result with and without subs are displayed differently
318 aECreator->sendUpdated(data()->owner(), EVENT_DISP);
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)
326 myAlgo = theMakeShape;
328 myIsGenerated = isGenerated;
329 // to avoid changing of "isDisabled" flag in the "updateSubs" cycle
332 updateSubs(theThisShape, true);
335 myHistoryCash.Clear();
339 bool Model_ResultBody::isConnectedTopology()
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);
349 aDataLab.ForgetAttribute(kIsConnectedTopology);
352 return aDataLab.IsAttribute(kIsConnectedTopology);
354 return false; // invalid case
357 void Model_ResultBody::cleanCash()
359 myBuilder->cleanCash();
360 for (std::vector<ResultBodyPtr>::const_iterator aSubIter = mySubs.cbegin();
361 aSubIter != mySubs.cend(); ++aSubIter)
363 const ResultBodyPtr& aSub = *aSubIter;
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)
372 if (theSub->isNull())
374 if (theSubSubs.Add(theSub->impl<TopoDS_Shape>())) {
375 bool aIsComp = theSub->isCompound() || theSub->isCompSolid();
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)
383 if (aSubType == GeomAPI_Shape::SHELL)
384 aSubType = GeomAPI_Shape::FACE;
385 if (aSubType == GeomAPI_Shape::WIRE)
386 aSubType = GeomAPI_Shape::EDGE;
388 for(GeomAPI_ShapeExplorer anExp(theSub, aSubType); anExp.more(); anExp.next()) {
389 collectSubs(anExp.current(), theSubSubs, false);
395 void Model_ResultBody::computeOldForSub(const GeomShapePtr& theSub,
396 const std::list<GeomShapePtr>& theAllOlds, std::list<GeomShapePtr>& theOldForSub)
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);
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));
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);
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))
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>());
434 myHistoryCash.Bind(anOldShape, aList);
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))
444 if (aResIter == theOldForSub.end())
445 theOldForSub.push_back(anOldSub); // found old used for new theSubShape creation