1 // Copyright (C) 2014-2019 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>
32 #include <TopoDS_Shape.hxx>
33 #include <TopExp_Explorer.hxx>
34 #include <TDataStd_UAttribute.hxx>
36 // if this attribute exists, the shape is connected topology
37 Standard_GUID kIsConnectedTopology("e51392e0-3a4d-405d-8e36-bbfe19858ef5");
38 // if this attribute exists, the connected topology flag must be recomputed
39 Standard_GUID kUpdateConnectedTopology("01ef7a45-0bec-4266-b0b4-4aa570921818");
41 Model_ResultBody::Model_ResultBody() : ModelAPI_ResultBody()
43 myBuilder = new Model_BodyBuilder(this);
44 myLastConcealed = false;
45 updateSubs(shape()); // in case of open, etc.
48 Model_ResultBody::~Model_ResultBody()
50 updateSubs(std::shared_ptr<GeomAPI_Shape>()); // erase sub-results
54 bool Model_ResultBody::generated(const GeomShapePtr& theNewShape,
55 const std::string& theName, const bool theCheckIsInResult)
58 if (mySubs.size()) { // consists of subs
59 for (std::vector<ResultBodyPtr>::const_iterator aSubIter = mySubs.cbegin();
60 aSubIter != mySubs.cend();
63 const ResultBodyPtr& aSub = *aSubIter;
64 if (aSub->generated(theNewShape, theName, theCheckIsInResult))
67 } else { // do for this directly
68 if (myBuilder->generated(theNewShape, theName, theCheckIsInResult))
74 void Model_ResultBody::loadGeneratedShapes(const std::shared_ptr<GeomAlgoAPI_MakeShape>& theAlgo,
75 const GeomShapePtr& theOldShape,
76 const GeomAPI_Shape::ShapeType theShapeTypeToExplore,
77 const std::string& theName)
79 if (mySubs.size()) { // consists of subs
80 for (std::vector<ResultBodyPtr>::const_iterator aSubIter = mySubs.cbegin();
81 aSubIter != mySubs.cend();
84 const ResultBodyPtr& aSub = *aSubIter;
85 aSub->loadGeneratedShapes(theAlgo, theOldShape, theShapeTypeToExplore, theName);
87 } else { // do for this directly
88 myBuilder->loadGeneratedShapes(theAlgo, theOldShape, theShapeTypeToExplore, theName);
92 void Model_ResultBody::loadModifiedShapes(const std::shared_ptr<GeomAlgoAPI_MakeShape>& theAlgo,
93 const GeomShapePtr& theOldShape,
94 const GeomAPI_Shape::ShapeType theShapeTypeToExplore,
95 const std::string& theName)
97 if (/*theSplitInSubs &&*/ mySubs.size()) { // consists of subs
98 // optimization of getting of new shapes for specific sub-result
99 if (!theAlgo->isNewShapesCollected(theOldShape, theShapeTypeToExplore))
100 theAlgo->collectNewShapes(theOldShape, theShapeTypeToExplore);
101 std::vector<ResultBodyPtr>::const_iterator aSubIter = mySubs.cbegin();
102 for(; aSubIter != mySubs.cend(); aSubIter++) {
103 // check that sub-shape was also created as modification of ShapeIn
104 /* to find when it is needed later to enable: to store modification of sub-bodies not only as primitives
105 GeomShapePtr aSubGeomShape = (*aSubIter)->shape();
106 if (!theIsStoreAsGenerated && aSubGeomShape.get() && !aSubGeomShape->isNull()) {
107 TopoDS_Shape aSubShape = aSubGeomShape->impl<TopoDS_Shape>();
108 TopoDS_Shape aWholeIn = theShapeIn->impl<TopoDS_Shape>();
109 for(TopExp_Explorer anExp(aWholeIn, aSubShape.ShapeType()); anExp.More(); anExp.Next()) {
110 ListOfShape aHistory;
111 std::shared_ptr<GeomAPI_Shape> aSubIn(new GeomAPI_Shape());
112 aSubIn->setImpl((new TopoDS_Shape(anExp.Current())));
113 theMS->modified(aSubIn, aHistory);
114 std::list<std::shared_ptr<GeomAPI_Shape> >::const_iterator anIt = aHistory.begin();
115 for (; anIt != aHistory.end(); anIt++) {
116 if ((*anIt)->isSame(aSubGeomShape)) {
117 (*aSubIter)->storeModified(aSubIn, aSubGeomShape, -2); // -2 is to avoid clearing
122 (*aSubIter)->loadModifiedShapes(theAlgo, theOldShape, theShapeTypeToExplore, theName);
124 } else { // do for this directly
125 myBuilder->loadModifiedShapes(theAlgo, theOldShape, theShapeTypeToExplore, theName);
129 int Model_ResultBody::numberOfSubs(bool forTree) const
131 return int(mySubs.size());
134 ResultBodyPtr Model_ResultBody::subResult(const int theIndex, bool forTree) const
136 if (theIndex >= int(mySubs.size()))
137 return ResultBodyPtr();
138 return mySubs.at(theIndex);
141 bool Model_ResultBody::isSub(ObjectPtr theObject, int& theIndex) const
143 std::map<ObjectPtr, int>::const_iterator aFound = mySubsMap.find(theObject);
144 if (aFound != mySubsMap.end()) {
145 theIndex = aFound->second;
151 void Model_ResultBody::colorConfigInfo(std::string& theSection, std::string& theName,
152 std::string& theDefault)
154 theSection = "Visualization";
155 theName = "result_body_color";
156 theDefault = DEFAULT_COLOR();
159 bool Model_ResultBody::setDisabled(std::shared_ptr<ModelAPI_Result> theThis, const bool theFlag)
161 bool aChanged = ModelAPI_ResultBody::setDisabled(theThis, theFlag);
162 if (aChanged) { // state is changed, so modifications are needed
163 updateSubs(shape(), false); // to set disabled/enabled
168 bool Model_ResultBody::isConcealed()
170 return myLastConcealed;
173 void Model_ResultBody::setIsConcealed(const bool theValue)
175 if (ModelAPI_ResultBody::isConcealed() != theValue) {
176 ModelAPI_ResultBody::setIsConcealed(theValue);
181 // recursively check all subs for concealment flag, returns true if everybody have "flag" state,
182 // in theAll returns results with "flag" state
183 static bool checkAllSubs(ResultBodyPtr theParent, bool theFlag, std::list<ResultBodyPtr>& theAll)
185 if (theParent->isConcealed() != theFlag)
186 theAll.push_back(theParent);
187 bool aResult = theParent->ModelAPI_ResultBody::isConcealed() == theFlag;
188 for(int a = 0; a < theParent->numberOfSubs(); a++) {
189 bool aSubRes = checkAllSubs(theParent->subResult(a), theFlag, theAll);
191 aResult = aResult || aSubRes; // concealed: one makes concealed everyone
193 aResult = aResult && aSubRes; // not concealed: all must be not concealed
198 void Model_ResultBody::updateConcealment()
200 if (myLastConcealed != ModelAPI_ResultBody::isConcealed()) {
201 // check the whole tree of results: if one is concealed, everybody are concealed
202 ResultBodyPtr anOwner = std::dynamic_pointer_cast<ModelAPI_ResultBody>(data()->owner());
204 return; // "this" is invalid
205 ResultBodyPtr aParent = ModelAPI_Tools::bodyOwner(anOwner);
206 while(aParent.get()) {
208 aParent = ModelAPI_Tools::bodyOwner(anOwner);
210 // iterate all results and collect all results whose state may be updated
211 std::list<ResultBodyPtr> anUpdated;
212 bool aNewFlag = !myLastConcealed;
213 if (checkAllSubs(anOwner, aNewFlag, anUpdated)) { // state of everyone must be updated
214 std::list<ResultBodyPtr>::iterator aRes = anUpdated.begin();
215 for(; aRes != anUpdated.end(); aRes++) {
216 bool aLastConcealed = (*aRes)->isConcealed();
217 if (aNewFlag != aLastConcealed) {
218 std::dynamic_pointer_cast<Model_ResultBody>(*aRes)->myLastConcealed = aNewFlag;
219 if (aNewFlag) { // become concealed, behaves like removed
220 ModelAPI_EventCreator::get()->sendDeleted(document(), groupName());
221 } else { // become not-concealed, behaves like created
222 static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_CREATED);
223 ModelAPI_EventCreator::get()->sendUpdated(*aRes, anEvent);
225 static Events_ID EVENT_DISP = // must be redisplayed in any case
226 Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
227 ModelAPI_EventCreator::get()->sendUpdated(*aRes, EVENT_DISP);
234 void Model_ResultBody::updateSubs(const std::shared_ptr<GeomAPI_Shape>& theThisShape,
235 const bool theShapeChanged)
237 static Events_Loop* aLoop = Events_Loop::loop();
238 static Events_ID EVENT_DISP = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
239 static Events_ID EVENT_UPD = aLoop->eventByName(EVENT_OBJECT_UPDATED);
240 static const ModelAPI_EventCreator* aECreator = ModelAPI_EventCreator::get();
241 // erase flag that topology is connected: the shape is new
242 if (theShapeChanged && data().get()) {
243 TDF_Label aDataLab = std::dynamic_pointer_cast<Model_Data>(data())->label();
244 if (!aDataLab.IsNull()) {
245 TDataStd_UAttribute::Set(aDataLab, kUpdateConnectedTopology);
246 isConnectedTopology(); // to store this flag in transaction, #2630
249 // iterate all sub-solids of compsolid to make sub-results synchronized with them
250 TopoDS_Shape aThisShape;
251 if (theThisShape.get()) aThisShape = theThisShape->impl<TopoDS_Shape>();
252 if (!aThisShape.IsNull() && (aThisShape.ShapeType() == TopAbs_COMPSOLID ||
253 aThisShape.ShapeType() == TopAbs_COMPOUND)) {
254 bool aWasEmpty = mySubs.empty();
255 Model_Objects* anObjects = std::dynamic_pointer_cast<Model_Document>(document())->objects();
256 unsigned int aSubIndex = 0;
257 TopoDS_Iterator aShapesIter(aThisShape);
258 for(; aShapesIter.More(); aShapesIter.Next(), aSubIndex++) {
259 std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape);
260 aShape->setImpl(new TopoDS_Shape(aShapesIter.Value()));
262 if (mySubs.size() <= aSubIndex) { // it is needed to create a new sub-result
263 aSub = anObjects->createBody(this->data(), aSubIndex);
264 mySubs.push_back(aSub);
265 mySubsMap[aSub] = int(mySubs.size() - 1);
266 if (isConcealed()) { // for issue #2579 note7
267 aSub->ModelAPI_ResultBody::setIsConcealed(true);
268 std::dynamic_pointer_cast<Model_ResultBody>(aSub)->updateConcealment();
270 } else { // just update shape of this result
271 aSub = mySubs[aSubIndex];
273 GeomShapePtr anOldSubShape = aSub->shape();
274 if (!aShape->isEqual(anOldSubShape)) {
275 aSub->store(aShape, false);
276 aECreator->sendUpdated(aSub, EVENT_DISP);
277 aECreator->sendUpdated(aSub, EVENT_UPD);
279 aSub->setDisabled(aSub, isDisabled());
281 // erase left, unused results
282 while(mySubs.size() > aSubIndex) {
283 ResultBodyPtr anErased = *(mySubs.rbegin());
284 if (anErased->ModelAPI_ResultBody::isConcealed()) {
285 anErased->ModelAPI_ResultBody::setIsConcealed(false);
286 std::dynamic_pointer_cast<Model_ResultBody>(anErased)->updateConcealment();
288 anErased->setDisabled(anErased, true);
289 mySubsMap.erase(anErased);
292 if (aWasEmpty) { // erase all subs
293 // redisplay this because result with and without subs are displayed differently
294 aECreator->sendUpdated(data()->owner(), EVENT_DISP);
297 } else if (!mySubs.empty()) { // erase all subs
298 while(!mySubs.empty()) {
299 ResultBodyPtr anErased = *(mySubs.rbegin());
300 if (anErased->ModelAPI_ResultBody::isConcealed()) {
301 anErased->ModelAPI_ResultBody::setIsConcealed(false);
302 std::dynamic_pointer_cast<Model_ResultBody>(anErased)->updateConcealment();
304 anErased->setDisabled(anErased, true); // even if it is invalid (to erase subs on abort/undo)
308 // redisplay this because result with and without subs are displayed differently
309 aECreator->sendUpdated(data()->owner(), EVENT_DISP);
313 bool Model_ResultBody::isConnectedTopology()
315 TDF_Label aDataLab = std::dynamic_pointer_cast<Model_Data>(data())->label();
316 if (!aDataLab.IsNull()) {
317 if (aDataLab.IsAttribute(kUpdateConnectedTopology)) { // recompute state
318 aDataLab.ForgetAttribute(kUpdateConnectedTopology);
319 GeomShapePtr aShape = shape();
320 if (aShape.get() && aShape->isConnectedTopology()) {
321 TDataStd_UAttribute::Set(aDataLab, kIsConnectedTopology);
323 aDataLab.ForgetAttribute(kIsConnectedTopology);
326 return aDataLab.IsAttribute(kIsConnectedTopology);
328 return false; // invalid case
331 void Model_ResultBody::cleanCash()
333 myBuilder->cleanCash();
334 for (std::vector<ResultBodyPtr>::const_iterator aSubIter = mySubs.cbegin();
335 aSubIter != mySubs.cend(); ++aSubIter)
337 const ResultBodyPtr& aSub = *aSubIter;