1 // Copyright (C) 2014-2017 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
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #include <Model_ResultBody.h>
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>
35 #include <TopoDS_Shape.hxx>
36 #include <TopExp_Explorer.hxx>
37 #include <TopTools_MapOfShape.hxx>
38 #include <TDataStd_UAttribute.hxx>
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");
45 Model_ResultBody::Model_ResultBody() : ModelAPI_ResultBody()
47 myBuilder = new Model_BodyBuilder(this);
48 myLastConcealed = false;
49 updateSubs(shape()); // in case of open, etc.
52 Model_ResultBody::~Model_ResultBody()
54 updateSubs(std::shared_ptr<GeomAPI_Shape>()); // erase sub-results
58 bool Model_ResultBody::generated(const GeomShapePtr& theNewShape,
59 const std::string& theName, const bool theCheckIsInResult)
62 if (mySubs.size()) { // consists of subs
63 for (std::vector<ResultBodyPtr>::const_iterator aSubIter = mySubs.cbegin();
64 aSubIter != mySubs.cend();
67 const ResultBodyPtr& aSub = *aSubIter;
68 if (aSub->generated(theNewShape, theName, theCheckIsInResult))
71 } else { // do for this directly
72 if (myBuilder->generated(theNewShape, theName, theCheckIsInResult))
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 const bool theSaveOldIfNotInTree)
84 if (mySubs.size()) { // consists of subs
85 for (std::vector<ResultBodyPtr>::const_iterator aSubIter = mySubs.cbegin();
86 aSubIter != mySubs.cend();
89 const ResultBodyPtr& aSub = *aSubIter;
90 aSub->loadGeneratedShapes(
91 theAlgo, theOldShape, theShapeTypeToExplore, theName, theSaveOldIfNotInTree);
93 } else { // do for this directly
94 myBuilder->loadGeneratedShapes(
95 theAlgo, theOldShape, theShapeTypeToExplore, theName, theSaveOldIfNotInTree);
99 void Model_ResultBody::loadModifiedShapes(const std::shared_ptr<GeomAlgoAPI_MakeShape>& theAlgo,
100 const GeomShapePtr& theOldShape,
101 const GeomAPI_Shape::ShapeType theShapeTypeToExplore,
102 const std::string& theName)
104 if (mySubs.size()) { // consists of subs
105 // optimization of getting of new shapes for specific sub-result
106 if (!theAlgo->isNewShapesCollected(theOldShape, theShapeTypeToExplore))
107 theAlgo->collectNewShapes(theOldShape, theShapeTypeToExplore);
108 std::vector<ResultBodyPtr>::const_iterator aSubIter = mySubs.cbegin();
109 for(; aSubIter != mySubs.cend(); aSubIter++) {
110 (*aSubIter)->loadModifiedShapes(theAlgo, theOldShape, theShapeTypeToExplore, theName);
112 } else { // do for this directly
113 myBuilder->loadModifiedShapes(theAlgo, theOldShape, theShapeTypeToExplore, theName);
117 int Model_ResultBody::numberOfSubs(bool forTree) const
119 return int(mySubs.size());
122 ResultBodyPtr Model_ResultBody::subResult(const int theIndex, bool forTree) const
124 if (theIndex >= int(mySubs.size()))
125 return ResultBodyPtr();
126 return mySubs.at(theIndex);
129 bool Model_ResultBody::isSub(ObjectPtr theObject, int& theIndex) const
131 std::map<ObjectPtr, int>::const_iterator aFound = mySubsMap.find(theObject);
132 if (aFound != mySubsMap.end()) {
133 theIndex = aFound->second;
139 void Model_ResultBody::colorConfigInfo(std::string& theSection, std::string& theName,
140 std::string& theDefault)
142 theSection = "Visualization";
143 theName = "result_body_color";
144 theDefault = DEFAULT_COLOR();
147 bool Model_ResultBody::setDisabled(std::shared_ptr<ModelAPI_Result> theThis, const bool theFlag)
149 bool aChanged = ModelAPI_ResultBody::setDisabled(theThis, theFlag);
150 if (aChanged) { // state is changed, so modifications are needed
151 updateSubs(shape(), false); // to set disabled/enabled
156 bool Model_ResultBody::isConcealed()
158 return myLastConcealed;
161 void Model_ResultBody::setIsConcealed(const bool theValue)
163 if (ModelAPI_ResultBody::isConcealed() != theValue) {
164 ModelAPI_ResultBody::setIsConcealed(theValue);
169 // recursively check all subs for concealment flag, returns true if everybody have "flag" state,
170 // in theAll returns results with "flag" state
171 static bool checkAllSubs(ResultBodyPtr theParent, bool theFlag, std::list<ResultBodyPtr>& theAll)
173 if (theParent->isConcealed() != theFlag)
174 theAll.push_back(theParent);
175 bool aResult = theParent->ModelAPI_ResultBody::isConcealed() == theFlag;
176 for(int a = 0; a < theParent->numberOfSubs(); a++) {
177 bool aSubRes = checkAllSubs(theParent->subResult(a), theFlag, theAll);
179 aResult = aResult || aSubRes; // concealed: one makes concealed everyone
181 aResult = aResult && aSubRes; // not concealed: all must be not concealed
186 void Model_ResultBody::updateConcealment()
188 if (myLastConcealed != ModelAPI_ResultBody::isConcealed()) {
189 // check the whole tree of results: if one is concealed, everybody are concealed
190 ResultBodyPtr anOwner = std::dynamic_pointer_cast<ModelAPI_ResultBody>(data()->owner());
192 return; // "this" is invalid
193 ResultBodyPtr aParent = ModelAPI_Tools::bodyOwner(anOwner);
194 while(aParent.get()) {
196 aParent = ModelAPI_Tools::bodyOwner(anOwner);
198 // iterate all results and collect all results whose state may be updated
199 std::list<ResultBodyPtr> anUpdated;
200 bool aNewFlag = !myLastConcealed;
201 if (checkAllSubs(anOwner, aNewFlag, anUpdated)) { // state of everyone must be updated
202 std::list<ResultBodyPtr>::iterator aRes = anUpdated.begin();
203 for(; aRes != anUpdated.end(); aRes++) {
204 bool aLastConcealed = (*aRes)->isConcealed();
205 if (aNewFlag != aLastConcealed) {
206 std::dynamic_pointer_cast<Model_ResultBody>(*aRes)->myLastConcealed = aNewFlag;
207 if (aNewFlag) { // become concealed, behaves like removed
208 ModelAPI_EventCreator::get()->sendDeleted(document(), groupName());
209 } else { // become not-concealed, behaves like created
210 static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_CREATED);
211 ModelAPI_EventCreator::get()->sendUpdated(*aRes, anEvent);
213 static Events_ID EVENT_DISP = // must be redisplayed in any case
214 Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
215 ModelAPI_EventCreator::get()->sendUpdated(*aRes, EVENT_DISP);
222 void Model_ResultBody::updateSubs(const std::shared_ptr<GeomAPI_Shape>& theThisShape,
223 const bool theShapeChanged)
225 static Events_Loop* aLoop = Events_Loop::loop();
226 static Events_ID EVENT_DISP = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
227 static Events_ID EVENT_UPD = aLoop->eventByName(EVENT_OBJECT_UPDATED);
228 static const ModelAPI_EventCreator* aECreator = ModelAPI_EventCreator::get();
229 // erase flag that topology is connected: the shape is new
230 if (theShapeChanged && data().get()) {
231 TDF_Label aDataLab = std::dynamic_pointer_cast<Model_Data>(data())->label();
232 if (!aDataLab.IsNull()) {
233 TDataStd_UAttribute::Set(aDataLab, kUpdateConnectedTopology);
234 isConnectedTopology(); // to store this flag in transaction, #2630
237 // iterate all sub-solids of compsolid to make sub-results synchronized with them
238 TopoDS_Shape aThisShape;
239 if (theThisShape.get()) aThisShape = theThisShape->impl<TopoDS_Shape>();
240 if (!aThisShape.IsNull() && (aThisShape.ShapeType() == TopAbs_COMPSOLID ||
241 aThisShape.ShapeType() == TopAbs_COMPOUND)) {
242 bool aWasEmpty = mySubs.empty();
243 Model_Objects* anObjects = std::dynamic_pointer_cast<Model_Document>(document())->objects();
244 unsigned int aSubIndex = 0;
245 TopoDS_Iterator aShapesIter(aThisShape);
246 for(; aShapesIter.More(); aShapesIter.Next(), aSubIndex++) {
247 std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape);
248 aShape->setImpl(new TopoDS_Shape(aShapesIter.Value()));
250 if (mySubs.size() <= aSubIndex) { // it is needed to create a new sub-result
251 aSub = anObjects->createBody(this->data(), aSubIndex);
252 mySubs.push_back(aSub);
253 mySubsMap[aSub] = int(mySubs.size() - 1);
254 if (isConcealed()) { // for issue #2579 note7
255 aSub->ModelAPI_ResultBody::setIsConcealed(true);
256 std::dynamic_pointer_cast<Model_ResultBody>(aSub)->updateConcealment();
258 } else { // just update shape of this result
259 aSub = mySubs[aSubIndex];
261 GeomShapePtr anOldSubShape = aSub->shape();
262 if (!aShape->isEqual(anOldSubShape)) {
264 std::list<GeomShapePtr> anOldForSub;
265 computeOldForSub(aShape, myOlds, anOldForSub);
266 myIsGenerated ? aSub->storeGenerated(anOldForSub, aShape, myAlgo) :
267 aSub->storeModified(anOldForSub, aShape, myAlgo);
269 aSub->store(aShape, false);
271 aECreator->sendUpdated(aSub, EVENT_DISP);
272 aECreator->sendUpdated(aSub, EVENT_UPD);
274 aSub->setDisabled(aSub, isDisabled());
276 // erase left, unused results
277 while(mySubs.size() > aSubIndex) {
278 ResultBodyPtr anErased = *(mySubs.rbegin());
279 if (anErased->ModelAPI_ResultBody::isConcealed()) {
280 anErased->ModelAPI_ResultBody::setIsConcealed(false);
281 std::dynamic_pointer_cast<Model_ResultBody>(anErased)->updateConcealment();
283 anErased->setDisabled(anErased, true);
284 mySubsMap.erase(anErased);
287 if (aWasEmpty) { // erase all subs
288 // redisplay this because result with and without subs are displayed differently
289 aECreator->sendUpdated(data()->owner(), EVENT_DISP);
292 } else if (!mySubs.empty()) { // erase all subs
293 while(!mySubs.empty()) {
294 ResultBodyPtr anErased = *(mySubs.rbegin());
295 if (anErased->ModelAPI_ResultBody::isConcealed()) {
296 anErased->ModelAPI_ResultBody::setIsConcealed(false);
297 std::dynamic_pointer_cast<Model_ResultBody>(anErased)->updateConcealment();
299 anErased->setDisabled(anErased, true); // even if it is invalid (to erase subs on abort/undo)
303 // redisplay this because result with and without subs are displayed differently
304 aECreator->sendUpdated(data()->owner(), EVENT_DISP);
308 void Model_ResultBody::updateSubs(
309 const GeomShapePtr& theThisShape, const std::list<GeomShapePtr>& theOlds,
310 const std::shared_ptr<GeomAlgoAPI_MakeShape> theMakeShape, const bool isGenerated)
312 myAlgo = theMakeShape;
314 myIsGenerated = isGenerated;
315 // to avoid changing of "isDisabled" flag in the "updateSubs" cycle
318 updateSubs(theThisShape, true);
321 myHistoryCash.Clear();
325 bool Model_ResultBody::isConnectedTopology()
327 TDF_Label aDataLab = std::dynamic_pointer_cast<Model_Data>(data())->label();
328 if (!aDataLab.IsNull()) {
329 if (aDataLab.IsAttribute(kUpdateConnectedTopology)) { // recompute state
330 aDataLab.ForgetAttribute(kUpdateConnectedTopology);
331 GeomShapePtr aShape = shape();
332 if (aShape.get() && aShape->isConnectedTopology()) {
333 TDataStd_UAttribute::Set(aDataLab, kIsConnectedTopology);
335 aDataLab.ForgetAttribute(kIsConnectedTopology);
338 return aDataLab.IsAttribute(kIsConnectedTopology);
340 return false; // invalid case
343 void Model_ResultBody::cleanCash()
345 myBuilder->cleanCash();
346 for (std::vector<ResultBodyPtr>::const_iterator aSubIter = mySubs.cbegin();
347 aSubIter != mySubs.cend(); ++aSubIter)
349 const ResultBodyPtr& aSub = *aSubIter;
354 // adds to the theSubSubs map all sub-shapes of theSub if it is compound of compsolid
355 static void collectSubs(
356 const GeomShapePtr theSub, TopTools_MapOfShape& theSubSubs, const bool theOneLevelMore)
358 if (theSub->isNull())
360 if (theSubSubs.Add(theSub->impl<TopoDS_Shape>())) {
361 bool aIsComp = theSub->isCompound() || theSub->isCompSolid();
363 for(GeomAPI_ShapeIterator anIter(theSub); anIter.more(); anIter.next())
364 collectSubs(anIter.current(), theSubSubs, theOneLevelMore);
365 } else if (theOneLevelMore) {
366 GeomAPI_Shape::ShapeType aSubType = GeomAPI_Shape::ShapeType(int(theSub->shapeType()) + 1);
367 if (aSubType == GeomAPI_Shape::SHAPE)
369 if (aSubType == GeomAPI_Shape::SHELL)
370 aSubType = GeomAPI_Shape::FACE;
371 if (aSubType == GeomAPI_Shape::WIRE)
372 aSubType = GeomAPI_Shape::EDGE;
374 for(GeomAPI_ShapeExplorer anExp(theSub, aSubType); anExp.more(); anExp.next()) {
375 collectSubs(anExp.current(), theSubSubs, false);
381 void Model_ResultBody::computeOldForSub(const GeomShapePtr& theSub,
382 const std::list<GeomShapePtr>& theAllOlds, std::list<GeomShapePtr>& theOldForSub)
384 // the old can be also used for sub-shape of theSub; collect all subs of compound or compsolid
385 TopTools_MapOfShape aSubSubs;
386 collectSubs(theSub, aSubSubs, false);
388 std::list<GeomShapePtr>::const_iterator aRootOlds = theAllOlds.cbegin();
389 for (; aRootOlds != theAllOlds.cend(); aRootOlds++) {
390 // use sub-shapes of olds too if they are compounds or compsolids
391 TopTools_MapOfShape anOldSubs;
392 // iterate one level more (for intersection of solids this is face)
393 collectSubs(*aRootOlds, anOldSubs, true);
394 for (TopTools_MapOfShape::Iterator anOldIter(anOldSubs); anOldIter.More(); anOldIter.Next()) {
395 TopoDS_Shape anOldShape = anOldIter.Value();
396 if (anOldShape.ShapeType() == TopAbs_COMPOUND || anOldShape.ShapeType() == TopAbs_SHELL ||
397 anOldShape.ShapeType() == TopAbs_WIRE)
398 continue; // container old-shapes are not supported by the history, may cause crash
399 GeomShapePtr anOldSub(new GeomAPI_Shape);
400 anOldSub->setImpl<TopoDS_Shape>(new TopoDS_Shape(anOldShape));
403 if (myHistoryCash.IsBound(anOldShape)) {
404 const TopTools_ListOfShape& aList = myHistoryCash.Find(anOldShape);
405 for(TopTools_ListIteratorOfListOfShape anIter(aList); anIter.More(); anIter.Next()) {
406 GeomShapePtr aShape(new GeomAPI_Shape);
407 aShape->setImpl<TopoDS_Shape>(new TopoDS_Shape(anIter.Value()));
408 aNews.push_back(aShape);
411 myIsGenerated ? myAlgo->generated(anOldSub, aNews) : myAlgo->modified(anOldSub, aNews);
412 // MakeShape may return alone old shape if there is no history information for this input
413 if (aNews.size() == 1 && aNews.front()->isEqual(anOldSub))
415 // store result in the history
416 TopTools_ListOfShape aList;
417 for (ListOfShape::iterator aNewIter = aNews.begin(); aNewIter != aNews.end(); aNewIter++) {
418 aList.Append((*aNewIter)->impl<TopoDS_Shape>());
420 myHistoryCash.Bind(anOldShape, aList);
423 for (ListOfShape::iterator aNewIter = aNews.begin(); aNewIter != aNews.end(); aNewIter++) {
424 if (aSubSubs.Contains((*aNewIter)->impl<TopoDS_Shape>())) {
425 // check list already contains this sub
426 std::list<GeomShapePtr>::iterator aResIter = theOldForSub.begin();
427 for(; aResIter != theOldForSub.end(); aResIter++)
428 if ((*aResIter)->isSame(anOldSub))
430 if (aResIter == theOldForSub.end())
431 theOldForSub.push_back(anOldSub); // found old used for new theSubShape creation