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 void Model_ResultBody::generated(const GeomShapePtr& theNewShape,
56 const std::string& theName)
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 aSub->generated(theNewShape, theName);
66 } else { // do for this directly
67 myBuilder->generated(theNewShape, theName);
71 void Model_ResultBody::loadGeneratedShapes(const std::shared_ptr<GeomAlgoAPI_MakeShape>& theAlgo,
72 const GeomShapePtr& theOldShape,
73 const GeomAPI_Shape::ShapeType theShapeTypeToExplore,
74 const std::string& theName)
76 if (mySubs.size()) { // consists of subs
77 for (std::vector<ResultBodyPtr>::const_iterator aSubIter = mySubs.cbegin();
78 aSubIter != mySubs.cend();
81 const ResultBodyPtr& aSub = *aSubIter;
82 aSub->loadGeneratedShapes(theAlgo, theOldShape, theShapeTypeToExplore, theName);
84 } else { // do for this directly
85 myBuilder->loadGeneratedShapes(theAlgo, theOldShape, theShapeTypeToExplore, theName);
89 void Model_ResultBody::loadModifiedShapes(const std::shared_ptr<GeomAlgoAPI_MakeShape>& theAlgo,
90 const GeomShapePtr& theOldShape,
91 const GeomAPI_Shape::ShapeType theShapeTypeToExplore,
92 const std::string& theName)
94 if (/*theSplitInSubs &&*/ mySubs.size()) { // consists of subs
95 // optimization of getting of new shapes for specific sub-result
96 if (!theAlgo->isNewShapesCollected(theOldShape, theShapeTypeToExplore))
97 theAlgo->collectNewShapes(theOldShape, theShapeTypeToExplore);
98 std::vector<ResultBodyPtr>::const_iterator aSubIter = mySubs.cbegin();
99 for(; aSubIter != mySubs.cend(); aSubIter++) {
100 // check that sub-shape was also created as modification of ShapeIn
101 /* to find when it is needed later to enable: to store modification of sub-bodies not only as primitives
102 GeomShapePtr aSubGeomShape = (*aSubIter)->shape();
103 if (!theIsStoreAsGenerated && aSubGeomShape.get() && !aSubGeomShape->isNull()) {
104 TopoDS_Shape aSubShape = aSubGeomShape->impl<TopoDS_Shape>();
105 TopoDS_Shape aWholeIn = theShapeIn->impl<TopoDS_Shape>();
106 for(TopExp_Explorer anExp(aWholeIn, aSubShape.ShapeType()); anExp.More(); anExp.Next()) {
107 ListOfShape aHistory;
108 std::shared_ptr<GeomAPI_Shape> aSubIn(new GeomAPI_Shape());
109 aSubIn->setImpl((new TopoDS_Shape(anExp.Current())));
110 theMS->modified(aSubIn, aHistory);
111 std::list<std::shared_ptr<GeomAPI_Shape> >::const_iterator anIt = aHistory.begin();
112 for (; anIt != aHistory.end(); anIt++) {
113 if ((*anIt)->isSame(aSubGeomShape)) {
114 (*aSubIter)->storeModified(aSubIn, aSubGeomShape, -2); // -2 is to avoid clearing
119 (*aSubIter)->loadModifiedShapes(theAlgo, theOldShape, theShapeTypeToExplore, theName);
121 } else { // do for this directly
122 myBuilder->loadModifiedShapes(theAlgo, theOldShape, theShapeTypeToExplore, theName);
126 int Model_ResultBody::numberOfSubs(bool forTree) const
128 return int(mySubs.size());
131 ResultBodyPtr Model_ResultBody::subResult(const int theIndex, bool forTree) const
133 if (theIndex >= int(mySubs.size()))
134 return ResultBodyPtr();
135 return mySubs.at(theIndex);
138 bool Model_ResultBody::isSub(ObjectPtr theObject, int& theIndex) const
140 std::map<ObjectPtr, int>::const_iterator aFound = mySubsMap.find(theObject);
141 if (aFound != mySubsMap.end()) {
142 theIndex = aFound->second;
148 void Model_ResultBody::colorConfigInfo(std::string& theSection, std::string& theName,
149 std::string& theDefault)
151 theSection = "Visualization";
152 theName = "result_body_color";
153 theDefault = DEFAULT_COLOR();
156 bool Model_ResultBody::setDisabled(std::shared_ptr<ModelAPI_Result> theThis, const bool theFlag)
158 bool aChanged = ModelAPI_ResultBody::setDisabled(theThis, theFlag);
159 if (aChanged) { // state is changed, so modifications are needed
160 updateSubs(shape(), false); // to set disabled/enabled
165 bool Model_ResultBody::isConcealed()
167 return myLastConcealed;
170 void Model_ResultBody::setIsConcealed(const bool theValue)
172 if (ModelAPI_ResultBody::isConcealed() != theValue) {
173 ModelAPI_ResultBody::setIsConcealed(theValue);
178 // recursively check all subs for concealment flag, returns true if everybody have "flag" state,
179 // in theAll returns results with "flag" state
180 static bool checkAllSubs(ResultBodyPtr theParent, bool theFlag, std::list<ResultBodyPtr>& theAll)
182 if (theParent->isConcealed() != theFlag)
183 theAll.push_back(theParent);
184 bool aResult = theParent->ModelAPI_ResultBody::isConcealed() == theFlag;
185 for(int a = 0; a < theParent->numberOfSubs(); a++) {
186 bool aSubRes = checkAllSubs(theParent->subResult(a), theFlag, theAll);
188 aResult = aResult || aSubRes; // concealed: one makes concealed everyone
190 aResult = aResult && aSubRes; // not concealed: all must be not concealed
195 void Model_ResultBody::updateConcealment()
197 if (myLastConcealed != ModelAPI_ResultBody::isConcealed()) {
198 // check the whole tree of results: if one is concealed, everybody are concealed
199 ResultBodyPtr anOwner = std::dynamic_pointer_cast<ModelAPI_ResultBody>(data()->owner());
201 return; // "this" is invalid
202 ResultBodyPtr aParent = ModelAPI_Tools::bodyOwner(anOwner);
203 while(aParent.get()) {
205 aParent = ModelAPI_Tools::bodyOwner(anOwner);
207 // iterate all results and collect all results whose state may be updated
208 std::list<ResultBodyPtr> anUpdated;
209 bool aNewFlag = !myLastConcealed;
210 if (checkAllSubs(anOwner, aNewFlag, anUpdated)) { // state of everyone must be updated
211 std::list<ResultBodyPtr>::iterator aRes = anUpdated.begin();
212 for(; aRes != anUpdated.end(); aRes++) {
213 bool aLastConcealed = (*aRes)->isConcealed();
214 if (aNewFlag != aLastConcealed) {
215 std::dynamic_pointer_cast<Model_ResultBody>(*aRes)->myLastConcealed = aNewFlag;
216 if (aNewFlag) { // become concealed, behaves like removed
217 ModelAPI_EventCreator::get()->sendDeleted(document(), groupName());
218 } else { // become not-concealed, behaves like created
219 static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_CREATED);
220 ModelAPI_EventCreator::get()->sendUpdated(*aRes, anEvent);
222 static Events_ID EVENT_DISP = // must be redisplayed in any case
223 Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
224 ModelAPI_EventCreator::get()->sendUpdated(*aRes, EVENT_DISP);
231 void Model_ResultBody::updateSubs(const std::shared_ptr<GeomAPI_Shape>& theThisShape,
232 const bool theShapeChanged)
234 static Events_Loop* aLoop = Events_Loop::loop();
235 static Events_ID EVENT_DISP = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
236 static Events_ID EVENT_UPD = aLoop->eventByName(EVENT_OBJECT_UPDATED);
237 static const ModelAPI_EventCreator* aECreator = ModelAPI_EventCreator::get();
238 // erase flag that topology is connected: the shape is new
239 if (theShapeChanged && data().get()) {
240 TDF_Label aDataLab = std::dynamic_pointer_cast<Model_Data>(data())->label();
241 if (!aDataLab.IsNull()) {
242 TDataStd_UAttribute::Set(aDataLab, kUpdateConnectedTopology);
243 isConnectedTopology(); // to store this flag in transaction, #2630
246 // iterate all sub-solids of compsolid to make sub-results synchronized with them
247 TopoDS_Shape aThisShape;
248 if (theThisShape.get()) aThisShape = theThisShape->impl<TopoDS_Shape>();
249 if (!aThisShape.IsNull() && (aThisShape.ShapeType() == TopAbs_COMPSOLID ||
250 aThisShape.ShapeType() == TopAbs_COMPOUND)) {
251 bool aWasEmpty = mySubs.empty();
252 Model_Objects* anObjects = std::dynamic_pointer_cast<Model_Document>(document())->objects();
253 unsigned int aSubIndex = 0;
254 TopoDS_Iterator aShapesIter(aThisShape);
255 for(; aShapesIter.More(); aShapesIter.Next(), aSubIndex++) {
256 std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape);
257 aShape->setImpl(new TopoDS_Shape(aShapesIter.Value()));
259 if (mySubs.size() <= aSubIndex) { // it is needed to create a new sub-result
260 aSub = anObjects->createBody(this->data(), aSubIndex);
261 mySubs.push_back(aSub);
262 mySubsMap[aSub] = int(mySubs.size() - 1);
263 if (isConcealed()) { // for issue #2579 note7
264 aSub->ModelAPI_ResultBody::setIsConcealed(true);
265 std::dynamic_pointer_cast<Model_ResultBody>(aSub)->updateConcealment();
267 } else { // just update shape of this result
268 aSub = mySubs[aSubIndex];
270 if (!aShape->isEqual(aSub->shape())) {
271 aSub->store(aShape, false);
272 aECreator->sendUpdated(aSub, EVENT_DISP);
273 aECreator->sendUpdated(aSub, EVENT_UPD);
275 aSub->setDisabled(aSub, isDisabled());
277 // erase left, unused results
278 while(mySubs.size() > aSubIndex) {
279 ResultBodyPtr anErased = *(mySubs.rbegin());
280 if (anErased->ModelAPI_ResultBody::isConcealed()) {
281 anErased->ModelAPI_ResultBody::setIsConcealed(false);
282 std::dynamic_pointer_cast<Model_ResultBody>(anErased)->updateConcealment();
284 anErased->setDisabled(anErased, true);
285 mySubsMap.erase(anErased);
288 if (aWasEmpty) { // erase all subs
289 // redisplay this because result with and without subs are displayed differently
290 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 bool Model_ResultBody::isLatestEqual(const std::shared_ptr<GeomAPI_Shape>& theShape)
310 if (myBuilder->isLatestEqual(theShape))
312 // also check that it is asked for sub-elements
313 std::vector<ResultBodyPtr>::const_iterator aSubIter = mySubs.cbegin();
314 for(; aSubIter != mySubs.cend(); aSubIter++) {
315 if (aSubIter->get() && (*aSubIter)->isLatestEqual(theShape)) {
322 bool Model_ResultBody::isConnectedTopology()
324 TDF_Label aDataLab = std::dynamic_pointer_cast<Model_Data>(data())->label();
325 if (!aDataLab.IsNull()) {
326 if (aDataLab.IsAttribute(kUpdateConnectedTopology)) { // recompute state
327 aDataLab.ForgetAttribute(kUpdateConnectedTopology);
328 GeomShapePtr aShape = shape();
329 if (aShape.get() && aShape->isConnectedTopology()) {
330 TDataStd_UAttribute::Set(aDataLab, kIsConnectedTopology);
332 aDataLab.ForgetAttribute(kIsConnectedTopology);
335 return aDataLab.IsAttribute(kIsConnectedTopology);
337 return false; // invalid case