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>
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)
82 if (mySubs.size()) { // consists of subs
83 for (std::vector<ResultBodyPtr>::const_iterator aSubIter = mySubs.cbegin();
84 aSubIter != mySubs.cend();
87 const ResultBodyPtr& aSub = *aSubIter;
88 aSub->loadGeneratedShapes(theAlgo, theOldShape, theShapeTypeToExplore, theName);
90 } else { // do for this directly
91 myBuilder->loadGeneratedShapes(theAlgo, theOldShape, theShapeTypeToExplore, theName);
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)
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);
108 } else { // do for this directly
109 myBuilder->loadModifiedShapes(theAlgo, theOldShape, theShapeTypeToExplore, theName);
113 int Model_ResultBody::numberOfSubs(bool forTree) const
115 return int(mySubs.size());
118 ResultBodyPtr Model_ResultBody::subResult(const int theIndex, bool forTree) const
120 if (theIndex >= int(mySubs.size()))
121 return ResultBodyPtr();
122 return mySubs.at(theIndex);
125 bool Model_ResultBody::isSub(ObjectPtr theObject, int& theIndex) const
127 std::map<ObjectPtr, int>::const_iterator aFound = mySubsMap.find(theObject);
128 if (aFound != mySubsMap.end()) {
129 theIndex = aFound->second;
135 void Model_ResultBody::colorConfigInfo(std::string& theSection, std::string& theName,
136 std::string& theDefault)
138 theSection = "Visualization";
139 theName = "result_body_color";
140 theDefault = DEFAULT_COLOR();
143 bool Model_ResultBody::setDisabled(std::shared_ptr<ModelAPI_Result> theThis, const bool theFlag)
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
152 bool Model_ResultBody::isConcealed()
154 return myLastConcealed;
157 void Model_ResultBody::setIsConcealed(const bool theValue)
159 if (ModelAPI_ResultBody::isConcealed() != theValue) {
160 ModelAPI_ResultBody::setIsConcealed(theValue);
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)
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);
175 aResult = aResult || aSubRes; // concealed: one makes concealed everyone
177 aResult = aResult && aSubRes; // not concealed: all must be not concealed
182 void Model_ResultBody::updateConcealment()
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());
188 return; // "this" is invalid
189 ResultBodyPtr aParent = ModelAPI_Tools::bodyOwner(anOwner);
190 while(aParent.get()) {
192 aParent = ModelAPI_Tools::bodyOwner(anOwner);
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);
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);
218 void Model_ResultBody::updateSubs(const std::shared_ptr<GeomAPI_Shape>& theThisShape,
219 const bool theShapeChanged)
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
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()));
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();
254 } else { // just update shape of this result
255 aSub = mySubs[aSubIndex];
257 GeomShapePtr anOldSubShape = aSub->shape();
258 if (!aShape->isEqual(anOldSubShape)) {
260 std::list<GeomShapePtr> anOldForSub;
261 computeOldForSub(aShape, myOlds, anOldForSub);
262 myIsGenerated ? aSub->storeGenerated(anOldForSub, aShape, myAlgo) :
263 aSub->storeModified(anOldForSub, aShape, myAlgo);
265 aSub->store(aShape, false);
267 aECreator->sendUpdated(aSub, EVENT_DISP);
268 aECreator->sendUpdated(aSub, EVENT_UPD);
270 aSub->setDisabled(aSub, isDisabled());
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();
279 anErased->setDisabled(anErased, true);
280 mySubsMap.erase(anErased);
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);
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();
295 anErased->setDisabled(anErased, true); // even if it is invalid (to erase subs on abort/undo)
299 // redisplay this because result with and without subs are displayed differently
300 aECreator->sendUpdated(data()->owner(), EVENT_DISP);
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)
308 myAlgo = theMakeShape;
310 myIsGenerated = isGenerated;
311 updateSubs(theThisShape, true);
317 bool Model_ResultBody::isConnectedTopology()
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);
327 aDataLab.ForgetAttribute(kIsConnectedTopology);
330 return aDataLab.IsAttribute(kIsConnectedTopology);
332 return false; // invalid case
335 void Model_ResultBody::cleanCash()
337 myBuilder->cleanCash();
338 for (std::vector<ResultBodyPtr>::const_iterator aSubIter = mySubs.cbegin();
339 aSubIter != mySubs.cend(); ++aSubIter)
341 const ResultBodyPtr& aSub = *aSubIter;
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)
350 if (theSub->isNull())
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);
362 void Model_ResultBody::computeOldForSub(const GeomShapePtr& theSub,
363 const std::list<GeomShapePtr>& theAllOlds, std::list<GeomShapePtr>& theOldForSub)
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);
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()));
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))
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))
391 if (aResIter == theOldForSub.end())
392 theOldForSub.push_back(anOldSub); // found old used for new theSubShape creation