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 <TDataStd_UAttribute.hxx>
38 // if this attribute exists, the shape is connected topology
39 Standard_GUID kIsConnectedTopology("e51392e0-3a4d-405d-8e36-bbfe19858ef5");
40 // if this attribute exists, the connected topology flag must be recomputed
41 Standard_GUID kUpdateConnectedTopology("01ef7a45-0bec-4266-b0b4-4aa570921818");
43 Model_ResultBody::Model_ResultBody() : ModelAPI_ResultBody()
45 myBuilder = new Model_BodyBuilder(this);
46 myLastConcealed = false;
47 updateSubs(shape()); // in case of open, etc.
50 Model_ResultBody::~Model_ResultBody()
52 updateSubs(std::shared_ptr<GeomAPI_Shape>()); // erase sub-results
56 bool Model_ResultBody::generated(const GeomShapePtr& theNewShape,
57 const std::string& theName, const bool theCheckIsInResult)
60 if (mySubs.size()) { // consists of subs
61 for (std::vector<ResultBodyPtr>::const_iterator aSubIter = mySubs.cbegin();
62 aSubIter != mySubs.cend();
65 const ResultBodyPtr& aSub = *aSubIter;
66 if (aSub->generated(theNewShape, theName, theCheckIsInResult))
69 } else { // do for this directly
70 if (myBuilder->generated(theNewShape, theName, theCheckIsInResult))
76 void Model_ResultBody::loadGeneratedShapes(const std::shared_ptr<GeomAlgoAPI_MakeShape>& theAlgo,
77 const GeomShapePtr& theOldShape,
78 const GeomAPI_Shape::ShapeType theShapeTypeToExplore,
79 const std::string& theName)
81 if (mySubs.size()) { // consists of subs
82 for (std::vector<ResultBodyPtr>::const_iterator aSubIter = mySubs.cbegin();
83 aSubIter != mySubs.cend();
86 const ResultBodyPtr& aSub = *aSubIter;
87 aSub->loadGeneratedShapes(theAlgo, theOldShape, theShapeTypeToExplore, theName);
89 } else { // do for this directly
90 myBuilder->loadGeneratedShapes(theAlgo, theOldShape, theShapeTypeToExplore, theName);
94 void Model_ResultBody::loadModifiedShapes(const std::shared_ptr<GeomAlgoAPI_MakeShape>& theAlgo,
95 const GeomShapePtr& theOldShape,
96 const GeomAPI_Shape::ShapeType theShapeTypeToExplore,
97 const std::string& theName)
99 if (mySubs.size()) { // consists of subs
100 // optimization of getting of new shapes for specific sub-result
101 if (!theAlgo->isNewShapesCollected(theOldShape, theShapeTypeToExplore))
102 theAlgo->collectNewShapes(theOldShape, theShapeTypeToExplore);
103 std::vector<ResultBodyPtr>::const_iterator aSubIter = mySubs.cbegin();
104 for(; aSubIter != mySubs.cend(); aSubIter++) {
105 (*aSubIter)->loadModifiedShapes(theAlgo, theOldShape, theShapeTypeToExplore, theName);
107 } else { // do for this directly
108 myBuilder->loadModifiedShapes(theAlgo, theOldShape, theShapeTypeToExplore, theName);
112 int Model_ResultBody::numberOfSubs(bool forTree) const
114 return int(mySubs.size());
117 ResultBodyPtr Model_ResultBody::subResult(const int theIndex, bool forTree) const
119 if (theIndex >= int(mySubs.size()))
120 return ResultBodyPtr();
121 return mySubs.at(theIndex);
124 bool Model_ResultBody::isSub(ObjectPtr theObject, int& theIndex) const
126 std::map<ObjectPtr, int>::const_iterator aFound = mySubsMap.find(theObject);
127 if (aFound != mySubsMap.end()) {
128 theIndex = aFound->second;
134 void Model_ResultBody::colorConfigInfo(std::string& theSection, std::string& theName,
135 std::string& theDefault)
137 theSection = "Visualization";
138 theName = "result_body_color";
139 theDefault = DEFAULT_COLOR();
142 bool Model_ResultBody::setDisabled(std::shared_ptr<ModelAPI_Result> theThis, const bool theFlag)
144 bool aChanged = ModelAPI_ResultBody::setDisabled(theThis, theFlag);
145 if (aChanged) { // state is changed, so modifications are needed
146 updateSubs(shape(), false); // to set disabled/enabled
151 bool Model_ResultBody::isConcealed()
153 return myLastConcealed;
156 void Model_ResultBody::setIsConcealed(const bool theValue)
158 if (ModelAPI_ResultBody::isConcealed() != theValue) {
159 ModelAPI_ResultBody::setIsConcealed(theValue);
164 // recursively check all subs for concealment flag, returns true if everybody have "flag" state,
165 // in theAll returns results with "flag" state
166 static bool checkAllSubs(ResultBodyPtr theParent, bool theFlag, std::list<ResultBodyPtr>& theAll)
168 if (theParent->isConcealed() != theFlag)
169 theAll.push_back(theParent);
170 bool aResult = theParent->ModelAPI_ResultBody::isConcealed() == theFlag;
171 for(int a = 0; a < theParent->numberOfSubs(); a++) {
172 bool aSubRes = checkAllSubs(theParent->subResult(a), theFlag, theAll);
174 aResult = aResult || aSubRes; // concealed: one makes concealed everyone
176 aResult = aResult && aSubRes; // not concealed: all must be not concealed
181 void Model_ResultBody::updateConcealment()
183 if (myLastConcealed != ModelAPI_ResultBody::isConcealed()) {
184 // check the whole tree of results: if one is concealed, everybody are concealed
185 ResultBodyPtr anOwner = std::dynamic_pointer_cast<ModelAPI_ResultBody>(data()->owner());
187 return; // "this" is invalid
188 ResultBodyPtr aParent = ModelAPI_Tools::bodyOwner(anOwner);
189 while(aParent.get()) {
191 aParent = ModelAPI_Tools::bodyOwner(anOwner);
193 // iterate all results and collect all results whose state may be updated
194 std::list<ResultBodyPtr> anUpdated;
195 bool aNewFlag = !myLastConcealed;
196 if (checkAllSubs(anOwner, aNewFlag, anUpdated)) { // state of everyone must be updated
197 std::list<ResultBodyPtr>::iterator aRes = anUpdated.begin();
198 for(; aRes != anUpdated.end(); aRes++) {
199 bool aLastConcealed = (*aRes)->isConcealed();
200 if (aNewFlag != aLastConcealed) {
201 std::dynamic_pointer_cast<Model_ResultBody>(*aRes)->myLastConcealed = aNewFlag;
202 if (aNewFlag) { // become concealed, behaves like removed
203 ModelAPI_EventCreator::get()->sendDeleted(document(), groupName());
204 } else { // become not-concealed, behaves like created
205 static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_CREATED);
206 ModelAPI_EventCreator::get()->sendUpdated(*aRes, anEvent);
208 static Events_ID EVENT_DISP = // must be redisplayed in any case
209 Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
210 ModelAPI_EventCreator::get()->sendUpdated(*aRes, EVENT_DISP);
217 void Model_ResultBody::updateSubs(const std::shared_ptr<GeomAPI_Shape>& theThisShape,
218 const bool theShapeChanged)
220 static Events_Loop* aLoop = Events_Loop::loop();
221 static Events_ID EVENT_DISP = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
222 static Events_ID EVENT_UPD = aLoop->eventByName(EVENT_OBJECT_UPDATED);
223 static const ModelAPI_EventCreator* aECreator = ModelAPI_EventCreator::get();
224 // erase flag that topology is connected: the shape is new
225 if (theShapeChanged && data().get()) {
226 TDF_Label aDataLab = std::dynamic_pointer_cast<Model_Data>(data())->label();
227 if (!aDataLab.IsNull()) {
228 TDataStd_UAttribute::Set(aDataLab, kUpdateConnectedTopology);
229 isConnectedTopology(); // to store this flag in transaction, #2630
232 // iterate all sub-solids of compsolid to make sub-results synchronized with them
233 TopoDS_Shape aThisShape;
234 if (theThisShape.get()) aThisShape = theThisShape->impl<TopoDS_Shape>();
235 if (!aThisShape.IsNull() && (aThisShape.ShapeType() == TopAbs_COMPSOLID ||
236 aThisShape.ShapeType() == TopAbs_COMPOUND)) {
237 bool aWasEmpty = mySubs.empty();
238 Model_Objects* anObjects = std::dynamic_pointer_cast<Model_Document>(document())->objects();
239 unsigned int aSubIndex = 0;
240 TopoDS_Iterator aShapesIter(aThisShape);
241 for(; aShapesIter.More(); aShapesIter.Next(), aSubIndex++) {
242 std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape);
243 aShape->setImpl(new TopoDS_Shape(aShapesIter.Value()));
245 if (mySubs.size() <= aSubIndex) { // it is needed to create a new sub-result
246 aSub = anObjects->createBody(this->data(), aSubIndex);
247 mySubs.push_back(aSub);
248 mySubsMap[aSub] = int(mySubs.size() - 1);
249 if (isConcealed()) { // for issue #2579 note7
250 aSub->ModelAPI_ResultBody::setIsConcealed(true);
251 std::dynamic_pointer_cast<Model_ResultBody>(aSub)->updateConcealment();
253 } else { // just update shape of this result
254 aSub = mySubs[aSubIndex];
256 GeomShapePtr anOldSubShape = aSub->shape();
257 if (!aShape->isEqual(anOldSubShape)) {
259 std::list<GeomShapePtr> anOldForSub;
260 computeOldForSub(aShape, myOlds, anOldForSub);
261 myIsGenerated ? aSub->storeGenerated(anOldForSub, aShape, myAlgo) :
262 aSub->storeModified(anOldForSub, aShape, myAlgo);
264 aSub->store(aShape, false);
266 aECreator->sendUpdated(aSub, EVENT_DISP);
267 aECreator->sendUpdated(aSub, EVENT_UPD);
269 aSub->setDisabled(aSub, isDisabled());
271 // erase left, unused results
272 while(mySubs.size() > aSubIndex) {
273 ResultBodyPtr anErased = *(mySubs.rbegin());
274 if (anErased->ModelAPI_ResultBody::isConcealed()) {
275 anErased->ModelAPI_ResultBody::setIsConcealed(false);
276 std::dynamic_pointer_cast<Model_ResultBody>(anErased)->updateConcealment();
278 anErased->setDisabled(anErased, true);
279 mySubsMap.erase(anErased);
282 if (aWasEmpty) { // erase all subs
283 // redisplay this because result with and without subs are displayed differently
284 aECreator->sendUpdated(data()->owner(), EVENT_DISP);
287 } else if (!mySubs.empty()) { // erase all subs
288 while(!mySubs.empty()) {
289 ResultBodyPtr anErased = *(mySubs.rbegin());
290 if (anErased->ModelAPI_ResultBody::isConcealed()) {
291 anErased->ModelAPI_ResultBody::setIsConcealed(false);
292 std::dynamic_pointer_cast<Model_ResultBody>(anErased)->updateConcealment();
294 anErased->setDisabled(anErased, true); // even if it is invalid (to erase subs on abort/undo)
298 // redisplay this because result with and without subs are displayed differently
299 aECreator->sendUpdated(data()->owner(), EVENT_DISP);
303 void Model_ResultBody::updateSubs(
304 const GeomShapePtr& theThisShape, const std::list<GeomShapePtr>& theOlds,
305 const std::shared_ptr<GeomAlgoAPI_MakeShape> theMakeShape, const bool isGenerated)
307 myAlgo = theMakeShape;
309 myIsGenerated = isGenerated;
310 updateSubs(theThisShape, true);
316 bool Model_ResultBody::isConnectedTopology()
318 TDF_Label aDataLab = std::dynamic_pointer_cast<Model_Data>(data())->label();
319 if (!aDataLab.IsNull()) {
320 if (aDataLab.IsAttribute(kUpdateConnectedTopology)) { // recompute state
321 aDataLab.ForgetAttribute(kUpdateConnectedTopology);
322 GeomShapePtr aShape = shape();
323 if (aShape.get() && aShape->isConnectedTopology()) {
324 TDataStd_UAttribute::Set(aDataLab, kIsConnectedTopology);
326 aDataLab.ForgetAttribute(kIsConnectedTopology);
329 return aDataLab.IsAttribute(kIsConnectedTopology);
331 return false; // invalid case
334 void Model_ResultBody::cleanCash()
336 myBuilder->cleanCash();
337 for (std::vector<ResultBodyPtr>::const_iterator aSubIter = mySubs.cbegin();
338 aSubIter != mySubs.cend(); ++aSubIter)
340 const ResultBodyPtr& aSub = *aSubIter;
345 void Model_ResultBody::computeOldForSub(const GeomShapePtr& theSub,
346 const std::list<GeomShapePtr>& theAllOlds, std::list<GeomShapePtr>& theOldForSub)
348 std::list<GeomShapePtr>::const_iterator aRootOlds = theAllOlds.cbegin();
349 for(; aRootOlds != theAllOlds.cend(); aRootOlds++) {
351 myIsGenerated ? myAlgo->generated(*aRootOlds, aNews) : myAlgo->modified(*aRootOlds, aNews);
352 // MakeShape may return alone old shape if there is no history information for this input
353 if (aNews.size() == 1 && aNews.front()->isEqual(*aRootOlds))
355 if (aNews.empty()) { // try to iterate to sub-elements (for intersection of solids this is face)
356 std::list<GeomShapePtr> theAllSubOlds;
357 for(GeomAPI_ShapeIterator aSubOld(*aRootOlds); aSubOld.more(); aSubOld.next()) {
358 GeomShapePtr aSub = aSubOld.current();
359 if (aSub.get() && !aSub->isNull())
360 theAllSubOlds.push_back(aSub);
362 computeOldForSub(theSub, theAllSubOlds, theOldForSub);
364 for(ListOfShape::iterator aNewIter = aNews.begin(); aNewIter != aNews.end(); aNewIter++) {
365 if (theSub->isSame(*aNewIter)) { // found old that was used for new theSubShape creation
366 theOldForSub.push_back(*aRootOlds);