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>
33 #include <TopoDS_Shape.hxx>
34 #include <TopExp_Explorer.hxx>
35 #include <TDataStd_UAttribute.hxx>
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");
42 Model_ResultBody::Model_ResultBody() : ModelAPI_ResultBody()
44 myBuilder = new Model_BodyBuilder(this);
45 myLastConcealed = false;
46 updateSubs(shape()); // in case of open, etc.
49 Model_ResultBody::~Model_ResultBody()
51 updateSubs(std::shared_ptr<GeomAPI_Shape>()); // erase sub-results
55 bool Model_ResultBody::generated(const GeomShapePtr& theNewShape,
56 const std::string& theName, const bool theCheckIsInResult)
59 if (mySubs.size()) { // consists of subs
60 for (std::vector<ResultBodyPtr>::const_iterator aSubIter = mySubs.cbegin();
61 aSubIter != mySubs.cend();
64 const ResultBodyPtr& aSub = *aSubIter;
65 if (aSub->generated(theNewShape, theName, theCheckIsInResult))
68 } else { // do for this directly
69 if (myBuilder->generated(theNewShape, theName, theCheckIsInResult))
75 void Model_ResultBody::loadGeneratedShapes(const std::shared_ptr<GeomAlgoAPI_MakeShape>& theAlgo,
76 const GeomShapePtr& theOldShape,
77 const GeomAPI_Shape::ShapeType theShapeTypeToExplore,
78 const std::string& theName)
80 if (mySubs.size()) { // consists of subs
81 for (std::vector<ResultBodyPtr>::const_iterator aSubIter = mySubs.cbegin();
82 aSubIter != mySubs.cend();
85 const ResultBodyPtr& aSub = *aSubIter;
86 aSub->loadGeneratedShapes(theAlgo, theOldShape, theShapeTypeToExplore, theName);
88 } else { // do for this directly
89 myBuilder->loadGeneratedShapes(theAlgo, theOldShape, theShapeTypeToExplore, theName);
93 void Model_ResultBody::loadModifiedShapes(const std::shared_ptr<GeomAlgoAPI_MakeShape>& theAlgo,
94 const GeomShapePtr& theOldShape,
95 const GeomAPI_Shape::ShapeType theShapeTypeToExplore,
96 const std::string& theName)
98 if (mySubs.size()) { // consists of subs
99 // optimization of getting of new shapes for specific sub-result
100 if (!theAlgo->isNewShapesCollected(theOldShape, theShapeTypeToExplore))
101 theAlgo->collectNewShapes(theOldShape, theShapeTypeToExplore);
102 std::vector<ResultBodyPtr>::const_iterator aSubIter = mySubs.cbegin();
103 for(; aSubIter != mySubs.cend(); aSubIter++) {
104 (*aSubIter)->loadModifiedShapes(theAlgo, theOldShape, theShapeTypeToExplore, theName);
106 } else { // do for this directly
107 myBuilder->loadModifiedShapes(theAlgo, theOldShape, theShapeTypeToExplore, theName);
111 int Model_ResultBody::numberOfSubs(bool forTree) const
113 return int(mySubs.size());
116 ResultBodyPtr Model_ResultBody::subResult(const int theIndex, bool forTree) const
118 if (theIndex >= int(mySubs.size()))
119 return ResultBodyPtr();
120 return mySubs.at(theIndex);
123 bool Model_ResultBody::isSub(ObjectPtr theObject, int& theIndex) const
125 std::map<ObjectPtr, int>::const_iterator aFound = mySubsMap.find(theObject);
126 if (aFound != mySubsMap.end()) {
127 theIndex = aFound->second;
133 void Model_ResultBody::colorConfigInfo(std::string& theSection, std::string& theName,
134 std::string& theDefault)
136 theSection = "Visualization";
137 theName = "result_body_color";
138 theDefault = DEFAULT_COLOR();
141 bool Model_ResultBody::setDisabled(std::shared_ptr<ModelAPI_Result> theThis, const bool theFlag)
143 bool aChanged = ModelAPI_ResultBody::setDisabled(theThis, theFlag);
144 if (aChanged) { // state is changed, so modifications are needed
145 updateSubs(shape(), false); // to set disabled/enabled
150 bool Model_ResultBody::isConcealed()
152 return myLastConcealed;
155 void Model_ResultBody::setIsConcealed(const bool theValue)
157 if (ModelAPI_ResultBody::isConcealed() != theValue) {
158 ModelAPI_ResultBody::setIsConcealed(theValue);
163 // recursively check all subs for concealment flag, returns true if everybody have "flag" state,
164 // in theAll returns results with "flag" state
165 static bool checkAllSubs(ResultBodyPtr theParent, bool theFlag, std::list<ResultBodyPtr>& theAll)
167 if (theParent->isConcealed() != theFlag)
168 theAll.push_back(theParent);
169 bool aResult = theParent->ModelAPI_ResultBody::isConcealed() == theFlag;
170 for(int a = 0; a < theParent->numberOfSubs(); a++) {
171 bool aSubRes = checkAllSubs(theParent->subResult(a), theFlag, theAll);
173 aResult = aResult || aSubRes; // concealed: one makes concealed everyone
175 aResult = aResult && aSubRes; // not concealed: all must be not concealed
180 void Model_ResultBody::updateConcealment()
182 if (myLastConcealed != ModelAPI_ResultBody::isConcealed()) {
183 // check the whole tree of results: if one is concealed, everybody are concealed
184 ResultBodyPtr anOwner = std::dynamic_pointer_cast<ModelAPI_ResultBody>(data()->owner());
186 return; // "this" is invalid
187 ResultBodyPtr aParent = ModelAPI_Tools::bodyOwner(anOwner);
188 while(aParent.get()) {
190 aParent = ModelAPI_Tools::bodyOwner(anOwner);
192 // iterate all results and collect all results whose state may be updated
193 std::list<ResultBodyPtr> anUpdated;
194 bool aNewFlag = !myLastConcealed;
195 if (checkAllSubs(anOwner, aNewFlag, anUpdated)) { // state of everyone must be updated
196 std::list<ResultBodyPtr>::iterator aRes = anUpdated.begin();
197 for(; aRes != anUpdated.end(); aRes++) {
198 bool aLastConcealed = (*aRes)->isConcealed();
199 if (aNewFlag != aLastConcealed) {
200 std::dynamic_pointer_cast<Model_ResultBody>(*aRes)->myLastConcealed = aNewFlag;
201 if (aNewFlag) { // become concealed, behaves like removed
202 ModelAPI_EventCreator::get()->sendDeleted(document(), groupName());
203 } else { // become not-concealed, behaves like created
204 static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_CREATED);
205 ModelAPI_EventCreator::get()->sendUpdated(*aRes, anEvent);
207 static Events_ID EVENT_DISP = // must be redisplayed in any case
208 Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
209 ModelAPI_EventCreator::get()->sendUpdated(*aRes, EVENT_DISP);
216 void Model_ResultBody::updateSubs(const std::shared_ptr<GeomAPI_Shape>& theThisShape,
217 const bool theShapeChanged)
219 static Events_Loop* aLoop = Events_Loop::loop();
220 static Events_ID EVENT_DISP = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
221 static Events_ID EVENT_UPD = aLoop->eventByName(EVENT_OBJECT_UPDATED);
222 static const ModelAPI_EventCreator* aECreator = ModelAPI_EventCreator::get();
223 // erase flag that topology is connected: the shape is new
224 if (theShapeChanged && data().get()) {
225 TDF_Label aDataLab = std::dynamic_pointer_cast<Model_Data>(data())->label();
226 if (!aDataLab.IsNull()) {
227 TDataStd_UAttribute::Set(aDataLab, kUpdateConnectedTopology);
228 isConnectedTopology(); // to store this flag in transaction, #2630
231 // iterate all sub-solids of compsolid to make sub-results synchronized with them
232 TopoDS_Shape aThisShape;
233 if (theThisShape.get()) aThisShape = theThisShape->impl<TopoDS_Shape>();
234 if (!aThisShape.IsNull() && (aThisShape.ShapeType() == TopAbs_COMPSOLID ||
235 aThisShape.ShapeType() == TopAbs_COMPOUND)) {
236 bool aWasEmpty = mySubs.empty();
237 Model_Objects* anObjects = std::dynamic_pointer_cast<Model_Document>(document())->objects();
238 unsigned int aSubIndex = 0;
239 TopoDS_Iterator aShapesIter(aThisShape);
240 for(; aShapesIter.More(); aShapesIter.Next(), aSubIndex++) {
241 std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape);
242 aShape->setImpl(new TopoDS_Shape(aShapesIter.Value()));
244 if (mySubs.size() <= aSubIndex) { // it is needed to create a new sub-result
245 aSub = anObjects->createBody(this->data(), aSubIndex);
246 mySubs.push_back(aSub);
247 mySubsMap[aSub] = int(mySubs.size() - 1);
248 if (isConcealed()) { // for issue #2579 note7
249 aSub->ModelAPI_ResultBody::setIsConcealed(true);
250 std::dynamic_pointer_cast<Model_ResultBody>(aSub)->updateConcealment();
252 } else { // just update shape of this result
253 aSub = mySubs[aSubIndex];
255 GeomShapePtr anOldSubShape = aSub->shape();
256 if (!aShape->isEqual(anOldSubShape)) {
258 std::list<GeomShapePtr> anOldForSub;
259 computeOldForSub(aShape, anOldForSub);
260 myIsGenerated ? aSub->storeGenerated(anOldForSub, aShape, myAlgo) :
261 aSub->storeModified(anOldForSub, aShape, myAlgo);
263 aSub->store(aShape, false);
265 aECreator->sendUpdated(aSub, EVENT_DISP);
266 aECreator->sendUpdated(aSub, EVENT_UPD);
268 aSub->setDisabled(aSub, isDisabled());
270 // erase left, unused results
271 while(mySubs.size() > aSubIndex) {
272 ResultBodyPtr anErased = *(mySubs.rbegin());
273 if (anErased->ModelAPI_ResultBody::isConcealed()) {
274 anErased->ModelAPI_ResultBody::setIsConcealed(false);
275 std::dynamic_pointer_cast<Model_ResultBody>(anErased)->updateConcealment();
277 anErased->setDisabled(anErased, true);
278 mySubsMap.erase(anErased);
281 if (aWasEmpty) { // erase all subs
282 // redisplay this because result with and without subs are displayed differently
283 aECreator->sendUpdated(data()->owner(), EVENT_DISP);
286 } else if (!mySubs.empty()) { // erase all subs
287 while(!mySubs.empty()) {
288 ResultBodyPtr anErased = *(mySubs.rbegin());
289 if (anErased->ModelAPI_ResultBody::isConcealed()) {
290 anErased->ModelAPI_ResultBody::setIsConcealed(false);
291 std::dynamic_pointer_cast<Model_ResultBody>(anErased)->updateConcealment();
293 anErased->setDisabled(anErased, true); // even if it is invalid (to erase subs on abort/undo)
297 // redisplay this because result with and without subs are displayed differently
298 aECreator->sendUpdated(data()->owner(), EVENT_DISP);
302 void Model_ResultBody::updateSubs(
303 const GeomShapePtr& theThisShape, const std::list<GeomShapePtr>& theOlds,
304 const std::shared_ptr<GeomAlgoAPI_MakeShape> theMakeShape, const bool isGenerated)
306 myAlgo = theMakeShape;
308 myIsGenerated = isGenerated;
309 updateSubs(theThisShape, true);
315 bool Model_ResultBody::isConnectedTopology()
317 TDF_Label aDataLab = std::dynamic_pointer_cast<Model_Data>(data())->label();
318 if (!aDataLab.IsNull()) {
319 if (aDataLab.IsAttribute(kUpdateConnectedTopology)) { // recompute state
320 aDataLab.ForgetAttribute(kUpdateConnectedTopology);
321 GeomShapePtr aShape = shape();
322 if (aShape.get() && aShape->isConnectedTopology()) {
323 TDataStd_UAttribute::Set(aDataLab, kIsConnectedTopology);
325 aDataLab.ForgetAttribute(kIsConnectedTopology);
328 return aDataLab.IsAttribute(kIsConnectedTopology);
330 return false; // invalid case
333 void Model_ResultBody::cleanCash()
335 myBuilder->cleanCash();
336 for (std::vector<ResultBodyPtr>::const_iterator aSubIter = mySubs.cbegin();
337 aSubIter != mySubs.cend(); ++aSubIter)
339 const ResultBodyPtr& aSub = *aSubIter;
344 void Model_ResultBody::computeOldForSub(
345 const GeomShapePtr& theSubShape, std::list<GeomShapePtr>& theOldForSub)
347 std::list<GeomShapePtr>::iterator aRootOlds = myOlds.begin();
348 for(; aRootOlds != myOlds.end(); aRootOlds++) {
350 myIsGenerated ? myAlgo->generated(*aRootOlds, aNews) : myAlgo->modified(*aRootOlds, aNews);
351 for(ListOfShape::iterator aNewIter = aNews.begin(); aNewIter != aNews.end(); aNewIter++) {
352 if (theSubShape->isSame(*aNewIter)) { // found old that was used for new theSubShape creation
353 theOldForSub.push_back(*aRootOlds);