]> SALOME platform Git repositories - modules/shaper.git/blob - src/Model/Model_ResultBody.cpp
Salome HOME
Initial implementation of support of any level of hierarchy in Result Bodies.
[modules/shaper.git] / src / Model / Model_ResultBody.cpp
1 // Copyright (C) 2014-2017  CEA/DEN, EDF R&D
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #include <Model_ResultBody.h>
22
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 <Events_Loop.h>
31
32 #include <TopoDS_Shape.hxx>
33 #include <TopExp_Explorer.hxx>
34
35
36 Model_ResultBody::Model_ResultBody() : ModelAPI_ResultBody()
37 {
38   myBuilder = new Model_BodyBuilder(this);
39   myLastConcealed = false;
40   updateSubs(shape()); // in case of open, etc.
41 }
42
43 Model_ResultBody::~Model_ResultBody()
44 {
45   updateSubs(std::shared_ptr<GeomAPI_Shape>()); // erase sub-results
46   delete myBuilder;
47 }
48
49 void Model_ResultBody::loadAndOrientModifiedShapes(GeomAlgoAPI_MakeShape* theMS,
50     std::shared_ptr<GeomAPI_Shape>  theShapeIn, const int  theKindOfShape, const int  theTag,
51     const std::string& theName, GeomAPI_DataMapOfShapeShape& theSubShapes,
52     const bool theIsStoreSeparate,
53     const bool theIsStoreAsGenerated,
54     const bool theSplitInSubs)
55 {
56   if (theSplitInSubs && mySubs.size()) { // consists of subs
57     std::vector<ResultBodyPtr>::const_iterator aSubIter = mySubs.cbegin();
58     for(; aSubIter != mySubs.cend(); aSubIter++) {
59       // check that sub-shape was also created as modification of ShapeIn
60       /* to find when it is needed later to enable: to store modification of sub-bodies not only as primitives
61       GeomShapePtr aSubGeomShape = (*aSubIter)->shape();
62       if (!theIsStoreAsGenerated && aSubGeomShape.get() && !aSubGeomShape->isNull()) {
63         TopoDS_Shape aSubShape = aSubGeomShape->impl<TopoDS_Shape>();
64         TopoDS_Shape aWholeIn = theShapeIn->impl<TopoDS_Shape>();
65         for(TopExp_Explorer anExp(aWholeIn, aSubShape.ShapeType()); anExp.More(); anExp.Next()) {
66           ListOfShape aHistory;
67           std::shared_ptr<GeomAPI_Shape> aSubIn(new GeomAPI_Shape());
68           aSubIn->setImpl((new TopoDS_Shape(anExp.Current())));
69           theMS->modified(aSubIn, aHistory);
70           std::list<std::shared_ptr<GeomAPI_Shape> >::const_iterator anIt = aHistory.begin();
71           for (; anIt != aHistory.end(); anIt++) {
72             if ((*anIt)->isSame(aSubGeomShape)) {
73               (*aSubIter)->storeModified(aSubIn, aSubGeomShape, -2); // -2 is to avoid clearing
74             }
75           }
76         }
77       }*/
78       (*aSubIter)->loadAndOrientModifiedShapes(
79         theMS, theShapeIn, theKindOfShape, theTag, theName, theSubShapes, theIsStoreSeparate,
80         theIsStoreAsGenerated);
81     }
82   } else { // do for this directly
83     myBuilder->loadAndOrientGeneratedShapes(
84       theMS, theShapeIn, theKindOfShape, theTag, theName, theSubShapes);
85   }
86 }
87
88 int Model_ResultBody::numberOfSubs(bool forTree) const
89 {
90   return int(mySubs.size());
91 }
92
93 ResultBodyPtr Model_ResultBody::subResult(const int theIndex, bool forTree) const
94 {
95   return mySubs.at(theIndex);
96 }
97
98 bool Model_ResultBody::isSub(ObjectPtr theObject, int& theIndex) const
99 {
100   std::map<ObjectPtr, int>::const_iterator aFound = mySubsMap.find(theObject);
101   if (aFound != mySubsMap.end()) {
102     theIndex = aFound->second;
103     return true;
104   }
105   return false;
106 }
107
108 void Model_ResultBody::colorConfigInfo(std::string& theSection, std::string& theName,
109   std::string& theDefault)
110 {
111   theSection = "Visualization";
112   theName = "result_body_color";
113   theDefault = DEFAULT_COLOR();
114 }
115
116 bool Model_ResultBody::setDisabled(std::shared_ptr<ModelAPI_Result> theThis, const bool theFlag)
117 {
118   bool aChanged = ModelAPI_ResultBody::setDisabled(theThis, theFlag);
119   if (aChanged) { // state is changed, so modifications are needed
120     myBuilder->evolutionToSelection(theFlag);
121     updateSubs(shape()); // to set disabled/enabled
122   }
123   return aChanged;
124 }
125
126 bool Model_ResultBody::isConcealed()
127 {
128   return myLastConcealed;
129 }
130
131 void Model_ResultBody::setIsConcealed(const bool theValue)
132 {
133   if (ModelAPI_ResultBody::isConcealed() != theValue) {
134     ModelAPI_ResultBody::setIsConcealed(theValue);
135     updateConcealment();
136   }
137 }
138
139 void Model_ResultBody::updateConcealment()
140 {
141   if (myLastConcealed != ModelAPI_ResultBody::isConcealed()) {
142     ResultPtr anOwner = std::dynamic_pointer_cast<ModelAPI_Result>(data()->owner());
143     std::shared_ptr<Model_ResultBody> aParent = std::dynamic_pointer_cast<Model_ResultBody>(
144       ModelAPI_Tools::bodyOwner(anOwner));
145
146     myLastConcealed = ModelAPI_ResultBody::isConcealed(); // set new value and check parent
147     if (myLastConcealed) { // this becomes concealed, so, update all: parent and children
148       if (aParent.get())
149         aParent->updateConcealment();
150       static Events_ID EVENT_DISP =
151         Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
152       ModelAPI_EventCreator::get()->sendDeleted(document(), groupName());
153       ModelAPI_EventCreator::get()->sendUpdated(data()->owner(), EVENT_DISP);
154       std::vector<ResultBodyPtr>::const_iterator aSubIter = mySubs.cbegin();
155       for (; aSubIter != mySubs.cend(); aSubIter++) {
156         std::dynamic_pointer_cast<Model_ResultBody>(*aSubIter)->updateConcealment();
157       }
158     } else {
159       // ask parent: if it is still concealed, nothing is changed
160       if (aParent.get()) {
161         aParent->updateConcealment();
162         if (aParent->isConcealed()) {
163           myLastConcealed = true;
164           return;
165         }
166       }
167       // iterate children: if they are concealed, nothing is changed
168       bool aChildConcealed = false;
169       std::vector<ResultBodyPtr>::const_iterator aSubIter = mySubs.cbegin();
170       for (; aSubIter != mySubs.cend(); aSubIter++) {
171         std::dynamic_pointer_cast<Model_ResultBody>(*aSubIter)->updateConcealment();
172         if ((*aSubIter)->isConcealed()) {
173           aChildConcealed = true;
174           break;
175         }
176       }
177       if (aChildConcealed) { // some child is concealed, so, update back
178         myLastConcealed = true;
179         if (aParent.get())
180           aParent->updateConcealment();
181         std::vector<ResultBodyPtr>::const_iterator aSubIter = mySubs.cbegin();
182         for (; aSubIter != mySubs.cend(); aSubIter++) {
183           std::dynamic_pointer_cast<Model_ResultBody>(*aSubIter)->updateConcealment();
184         }
185       } else { // so, it becomes unconcealed
186         static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_CREATED);
187         ModelAPI_EventCreator::get()->sendUpdated(data()->owner(), anEvent);
188         static Events_ID EVENT_DISP =
189           Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
190         ModelAPI_EventCreator::get()->sendUpdated(data()->owner(), EVENT_DISP);
191       }
192     }
193   }
194 }
195
196 void Model_ResultBody::updateSubs(const std::shared_ptr<GeomAPI_Shape>& theThisShape)
197 {
198   static Events_Loop* aLoop = Events_Loop::loop();
199   static Events_ID EVENT_DISP = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
200   static Events_ID EVENT_UPD = aLoop->eventByName(EVENT_OBJECT_UPDATED);
201   static const ModelAPI_EventCreator* aECreator = ModelAPI_EventCreator::get();
202   // iterate all sub-solids of compsolid to make sub-results synchronized with them
203   TopoDS_Shape aThisShape;
204   if (theThisShape.get()) aThisShape = theThisShape->impl<TopoDS_Shape>();
205   if (!aThisShape.IsNull() && (aThisShape.ShapeType() == TopAbs_COMPSOLID ||
206        aThisShape.ShapeType() == TopAbs_COMPOUND)) {
207     bool aWasEmpty = mySubs.empty();
208     Model_Objects* anObjects = std::dynamic_pointer_cast<Model_Document>(document())->objects();
209     unsigned int aSubIndex = 0;
210     TopoDS_Iterator aShapesIter(aThisShape);
211     for(; aShapesIter.More(); aShapesIter.Next(), aSubIndex++) {
212       std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape);
213       aShape->setImpl(new TopoDS_Shape(aShapesIter.Value()));
214       ResultBodyPtr aSub;
215       if (mySubs.size() <= aSubIndex) { // it is needed to create a new sub-result
216         aSub = anObjects->createBody(this->data(), aSubIndex);
217         mySubs.push_back(aSub);
218         mySubsMap[aSub] = int(mySubs.size() - 1);
219       } else { // just update shape of this result
220         aSub = mySubs[aSubIndex];
221       }
222       if (!aShape->isEqual(aSub->shape())) {
223         aSub->store(aShape, false);
224         aECreator->sendUpdated(aSub, EVENT_DISP);
225         aECreator->sendUpdated(aSub, EVENT_UPD);
226       }
227       aSub->setDisabled(aSub, isDisabled());
228     }
229     // erase left, unused results
230     while(mySubs.size() > aSubIndex) {
231       ResultBodyPtr anErased = *(mySubs.rbegin());
232       if (anErased->ModelAPI_ResultBody::isConcealed())
233         std::dynamic_pointer_cast<Model_ResultBody>(anErased)->updateConcealment();
234       anErased->setDisabled(anErased, true);
235       mySubsMap.erase(anErased);
236       mySubs.pop_back();
237     }
238     if (aWasEmpty) { // erase all subs
239       // redisplay this because result with and without subs are displayed differently
240       aECreator->sendUpdated(data()->owner(), EVENT_DISP);
241     }
242   } else if (!mySubs.empty()) { // erase all subs
243     while(!mySubs.empty()) {
244       ResultBodyPtr anErased = *(mySubs.rbegin());
245       if (anErased->ModelAPI_ResultBody::isConcealed())
246         std::dynamic_pointer_cast<Model_ResultBody>(anErased)->updateConcealment();
247       anErased->setDisabled(anErased, true); // even if it is invalid (to erase subs on abort/undo)
248       mySubs.pop_back();
249     }
250     mySubsMap.clear();
251     // redisplay this because result with and without subs are displayed differently
252     aECreator->sendUpdated(data()->owner(), EVENT_DISP);
253   }
254 }
255
256 bool Model_ResultBody::isLatestEqual(const std::shared_ptr<GeomAPI_Shape>& theShape)
257 {
258   if (myBuilder->isLatestEqual(theShape))
259     return true;
260   // also check that it is asked for sub-elements
261   std::vector<ResultBodyPtr>::const_iterator aSubIter = mySubs.cbegin();
262   for(; aSubIter != mySubs.cend(); aSubIter++) {
263     if (aSubIter->get() && (*aSubIter)->isLatestEqual(theShape)) {
264       return true;
265     }
266   }
267   return false;
268 }