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::loadAndOrientModifiedShapes(GeomAlgoAPI_MakeShape* theMS,
56 std::shared_ptr<GeomAPI_Shape> theShapeIn, const int theKindOfShape, const int theTag,
57 const std::string& theName, GeomAPI_DataMapOfShapeShape& theSubShapes,
58 const bool theIsStoreSeparate,
59 const bool theIsStoreAsGenerated,
60 const bool theSplitInSubs)
62 if (theSplitInSubs && mySubs.size()) { // consists of subs
63 std::vector<ResultBodyPtr>::const_iterator aSubIter = mySubs.cbegin();
64 for(; aSubIter != mySubs.cend(); aSubIter++) {
65 // check that sub-shape was also created as modification of ShapeIn
66 /* to find when it is needed later to enable: to store modification of sub-bodies not only as primitives
67 GeomShapePtr aSubGeomShape = (*aSubIter)->shape();
68 if (!theIsStoreAsGenerated && aSubGeomShape.get() && !aSubGeomShape->isNull()) {
69 TopoDS_Shape aSubShape = aSubGeomShape->impl<TopoDS_Shape>();
70 TopoDS_Shape aWholeIn = theShapeIn->impl<TopoDS_Shape>();
71 for(TopExp_Explorer anExp(aWholeIn, aSubShape.ShapeType()); anExp.More(); anExp.Next()) {
73 std::shared_ptr<GeomAPI_Shape> aSubIn(new GeomAPI_Shape());
74 aSubIn->setImpl((new TopoDS_Shape(anExp.Current())));
75 theMS->modified(aSubIn, aHistory);
76 std::list<std::shared_ptr<GeomAPI_Shape> >::const_iterator anIt = aHistory.begin();
77 for (; anIt != aHistory.end(); anIt++) {
78 if ((*anIt)->isSame(aSubGeomShape)) {
79 (*aSubIter)->storeModified(aSubIn, aSubGeomShape, -2); // -2 is to avoid clearing
84 (*aSubIter)->loadAndOrientModifiedShapes(
85 theMS, theShapeIn, theKindOfShape, theTag, theName, theSubShapes, theIsStoreSeparate,
86 theIsStoreAsGenerated, theSplitInSubs);
88 } else { // do for this directly
89 myBuilder->loadAndOrientModifiedShapes(
90 theMS, theShapeIn, theKindOfShape, theTag, theName, theSubShapes, theIsStoreSeparate,
91 theIsStoreAsGenerated);
95 int Model_ResultBody::numberOfSubs(bool forTree) const
97 return int(mySubs.size());
100 ResultBodyPtr Model_ResultBody::subResult(const int theIndex, bool forTree) const
102 if (theIndex >= int(mySubs.size()))
103 return ResultBodyPtr();
104 return mySubs.at(theIndex);
107 bool Model_ResultBody::isSub(ObjectPtr theObject, int& theIndex) const
109 std::map<ObjectPtr, int>::const_iterator aFound = mySubsMap.find(theObject);
110 if (aFound != mySubsMap.end()) {
111 theIndex = aFound->second;
117 void Model_ResultBody::colorConfigInfo(std::string& theSection, std::string& theName,
118 std::string& theDefault)
120 theSection = "Visualization";
121 theName = "result_body_color";
122 theDefault = DEFAULT_COLOR();
125 bool Model_ResultBody::setDisabled(std::shared_ptr<ModelAPI_Result> theThis, const bool theFlag)
127 bool aChanged = ModelAPI_ResultBody::setDisabled(theThis, theFlag);
128 if (aChanged) { // state is changed, so modifications are needed
129 myBuilder->evolutionToSelection(theFlag);
130 updateSubs(shape(), false); // to set disabled/enabled
135 bool Model_ResultBody::isConcealed()
137 return myLastConcealed;
140 void Model_ResultBody::setIsConcealed(const bool theValue)
142 if (ModelAPI_ResultBody::isConcealed() != theValue) {
143 ModelAPI_ResultBody::setIsConcealed(theValue);
148 // recursively check all subs for concealment flag, returns true if everybody have "flag" state,
149 // in theAll returns results with "flag" state
150 static bool checkAllSubs(ResultBodyPtr theParent, bool theFlag, std::list<ResultBodyPtr>& theAll)
152 if (theParent->isConcealed() != theFlag)
153 theAll.push_back(theParent);
154 bool aResult = theParent->ModelAPI_ResultBody::isConcealed() == theFlag;
155 for(int a = 0; a < theParent->numberOfSubs(); a++) {
156 bool aSubRes = checkAllSubs(theParent->subResult(a), theFlag, theAll);
158 aResult = aResult || aSubRes; // concealed: one makes concealed everyone
160 aResult = aResult && aSubRes; // not concealed: all must be not concealed
165 void Model_ResultBody::updateConcealment()
167 if (myLastConcealed != ModelAPI_ResultBody::isConcealed()) {
168 // check the whole tree of results: if one is concealed, everybody are concealed
169 ResultBodyPtr anOwner = std::dynamic_pointer_cast<ModelAPI_ResultBody>(data()->owner());
171 return; // "this" is invalid
172 ResultBodyPtr aParent = ModelAPI_Tools::bodyOwner(anOwner);
173 while(aParent.get()) {
175 aParent = ModelAPI_Tools::bodyOwner(anOwner);
177 // iterate all results and collect all results whose state may be updated
178 std::list<ResultBodyPtr> anUpdated;
179 bool aNewFlag = !myLastConcealed;
180 if (checkAllSubs(anOwner, aNewFlag, anUpdated)) { // state of everyone must be updated
181 std::list<ResultBodyPtr>::iterator aRes = anUpdated.begin();
182 for(; aRes != anUpdated.end(); aRes++) {
183 bool aLastConcealed = (*aRes)->isConcealed();
184 if (aNewFlag != aLastConcealed) {
185 std::dynamic_pointer_cast<Model_ResultBody>(*aRes)->myLastConcealed = aNewFlag;
186 if (aNewFlag) { // become concealed, behaves like removed
187 ModelAPI_EventCreator::get()->sendDeleted(document(), groupName());
188 } else { // become not-concealed, behaves like created
189 static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_CREATED);
190 ModelAPI_EventCreator::get()->sendUpdated(*aRes, anEvent);
192 static Events_ID EVENT_DISP = // must be redisplayed in any case
193 Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
194 ModelAPI_EventCreator::get()->sendUpdated(*aRes, EVENT_DISP);
201 void Model_ResultBody::updateSubs(const std::shared_ptr<GeomAPI_Shape>& theThisShape,
202 const bool theShapeChanged)
204 static Events_Loop* aLoop = Events_Loop::loop();
205 static Events_ID EVENT_DISP = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
206 static Events_ID EVENT_UPD = aLoop->eventByName(EVENT_OBJECT_UPDATED);
207 static const ModelAPI_EventCreator* aECreator = ModelAPI_EventCreator::get();
208 // erase flag that topology is connected: the shape is new
209 if (theShapeChanged && data().get()) {
210 TDF_Label aDataLab = std::dynamic_pointer_cast<Model_Data>(data())->label();
211 if (!aDataLab.IsNull()) {
212 TDataStd_UAttribute::Set(aDataLab, kUpdateConnectedTopology);
215 // iterate all sub-solids of compsolid to make sub-results synchronized with them
216 TopoDS_Shape aThisShape;
217 if (theThisShape.get()) aThisShape = theThisShape->impl<TopoDS_Shape>();
218 if (!aThisShape.IsNull() && (aThisShape.ShapeType() == TopAbs_COMPSOLID ||
219 aThisShape.ShapeType() == TopAbs_COMPOUND)) {
220 bool aWasEmpty = mySubs.empty();
221 Model_Objects* anObjects = std::dynamic_pointer_cast<Model_Document>(document())->objects();
222 unsigned int aSubIndex = 0;
223 TopoDS_Iterator aShapesIter(aThisShape);
224 for(; aShapesIter.More(); aShapesIter.Next(), aSubIndex++) {
225 std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape);
226 aShape->setImpl(new TopoDS_Shape(aShapesIter.Value()));
228 if (mySubs.size() <= aSubIndex) { // it is needed to create a new sub-result
229 aSub = anObjects->createBody(this->data(), aSubIndex);
230 mySubs.push_back(aSub);
231 mySubsMap[aSub] = int(mySubs.size() - 1);
232 if (isConcealed()) { // for issue #2579 note7
233 aSub->ModelAPI_ResultBody::setIsConcealed(true);
234 std::dynamic_pointer_cast<Model_ResultBody>(aSub)->updateConcealment();
236 } else { // just update shape of this result
237 aSub = mySubs[aSubIndex];
239 if (!aShape->isEqual(aSub->shape())) {
240 aSub->store(aShape, false);
241 aECreator->sendUpdated(aSub, EVENT_DISP);
242 aECreator->sendUpdated(aSub, EVENT_UPD);
244 aSub->setDisabled(aSub, isDisabled());
246 // erase left, unused results
247 while(mySubs.size() > aSubIndex) {
248 ResultBodyPtr anErased = *(mySubs.rbegin());
249 if (anErased->ModelAPI_ResultBody::isConcealed()) {
250 anErased->ModelAPI_ResultBody::setIsConcealed(false);
251 std::dynamic_pointer_cast<Model_ResultBody>(anErased)->updateConcealment();
253 anErased->setDisabled(anErased, true);
254 mySubsMap.erase(anErased);
257 if (aWasEmpty) { // erase all subs
258 // redisplay this because result with and without subs are displayed differently
259 aECreator->sendUpdated(data()->owner(), EVENT_DISP);
261 } else if (!mySubs.empty()) { // erase all subs
262 while(!mySubs.empty()) {
263 ResultBodyPtr anErased = *(mySubs.rbegin());
264 if (anErased->ModelAPI_ResultBody::isConcealed()) {
265 anErased->ModelAPI_ResultBody::setIsConcealed(false);
266 std::dynamic_pointer_cast<Model_ResultBody>(anErased)->updateConcealment();
268 anErased->setDisabled(anErased, true); // even if it is invalid (to erase subs on abort/undo)
272 // redisplay this because result with and without subs are displayed differently
273 aECreator->sendUpdated(data()->owner(), EVENT_DISP);
277 bool Model_ResultBody::isLatestEqual(const std::shared_ptr<GeomAPI_Shape>& theShape)
279 if (myBuilder->isLatestEqual(theShape))
281 // also check that it is asked for sub-elements
282 std::vector<ResultBodyPtr>::const_iterator aSubIter = mySubs.cbegin();
283 for(; aSubIter != mySubs.cend(); aSubIter++) {
284 if (aSubIter->get() && (*aSubIter)->isLatestEqual(theShape)) {
291 bool Model_ResultBody::isConnectedTopology()
293 TDF_Label aDataLab = std::dynamic_pointer_cast<Model_Data>(data())->label();
294 if (!aDataLab.IsNull()) {
295 if (aDataLab.IsAttribute(kUpdateConnectedTopology)) { // recompute state
296 aDataLab.ForgetAttribute(kUpdateConnectedTopology);
297 GeomShapePtr aShape = shape();
298 if (aShape.get() && aShape->isConnectedTopology()) {
299 TDataStd_UAttribute::Set(aDataLab, kIsConnectedTopology);
301 aDataLab.ForgetAttribute(kIsConnectedTopology);
304 return aDataLab.IsAttribute(kIsConnectedTopology);
306 return false; // invalid case