]> SALOME platform Git repositories - modules/shaper.git/blob - src/Model/Model_ResultCompSolid.cpp
Salome HOME
Add copyright header according to request of CEA from 06.06.2017
[modules/shaper.git] / src / Model / Model_ResultCompSolid.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 email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
18 //
19
20 #include <Model_ResultCompSolid.h>
21
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 <Events_Loop.h>
29
30 #include <TopoDS_Shape.hxx>
31 #include <TopExp_Explorer.hxx>
32
33
34 Model_ResultCompSolid::Model_ResultCompSolid()
35 {
36   myBuilder = new Model_BodyBuilder(this);
37   myLastConcealed = false;
38   updateSubs(shape()); // in case of open, etc.
39 }
40
41 Model_ResultCompSolid::~Model_ResultCompSolid()
42 {
43   updateSubs(std::shared_ptr<GeomAPI_Shape>()); // erase sub-results
44 }
45
46 void Model_ResultCompSolid::store(const std::shared_ptr<GeomAPI_Shape>& theShape,
47                                   const bool theIsStoreSameShapes)
48 {
49   ModelAPI_ResultCompSolid::store(theShape, theIsStoreSameShapes);
50   updateSubs(theShape);
51 }
52
53 void Model_ResultCompSolid::storeGenerated(const std::shared_ptr<GeomAPI_Shape>& theFromShape,
54     const std::shared_ptr<GeomAPI_Shape>& theToShape)
55 {
56   ModelAPI_ResultCompSolid::storeGenerated(theFromShape, theToShape);
57   updateSubs(theToShape);
58 }
59
60 void Model_ResultCompSolid::storeModified(const std::shared_ptr<GeomAPI_Shape>& theOldShape,
61     const std::shared_ptr<GeomAPI_Shape>& theNewShape, const int theDecomposeSolidsTag)
62 {
63   ModelAPI_ResultCompSolid::storeModified(theOldShape, theNewShape, theDecomposeSolidsTag);
64   updateSubs(theNewShape);
65 }
66
67 int Model_ResultCompSolid::numberOfSubs(bool forTree) const
68 {
69   return int(mySubs.size());
70 }
71
72 std::shared_ptr<ModelAPI_ResultBody> Model_ResultCompSolid::subResult(const int theIndex,
73                                                                       bool forTree) const
74 {
75   return mySubs.at(theIndex);
76 }
77
78 bool Model_ResultCompSolid::isSub(ObjectPtr theObject) const
79 {
80   std::vector<std::shared_ptr<ModelAPI_ResultBody> >::const_iterator aSubIter = mySubs.cbegin();
81   for(; aSubIter != mySubs.cend(); aSubIter++)
82     if (*aSubIter == theObject)
83       return true;
84   return false;
85 }
86
87 void Model_ResultCompSolid::colorConfigInfo(std::string& theSection, std::string& theName,
88   std::string& theDefault)
89 {
90   theSection = "Visualization";
91   theName = "result_body_color";
92   theDefault = DEFAULT_COLOR();
93 }
94
95 bool Model_ResultCompSolid::setDisabled(std::shared_ptr<ModelAPI_Result> theThis,
96                                         const bool theFlag)
97 {
98   bool aChanged = ModelAPI_ResultBody::setDisabled(theThis, theFlag);
99   if (aChanged) { // state is changed, so modifications are needed
100     myBuilder->evolutionToSelection(theFlag);
101     updateSubs(shape()); // to set disabled/enabled
102   }
103   return aChanged;
104 }
105
106 bool Model_ResultCompSolid::isConcealed()
107 {
108   bool aResult = false;;
109   if (ModelAPI_ResultCompSolid::isConcealed()) {
110     aResult = true;
111   } else {
112     std::vector<std::shared_ptr<ModelAPI_ResultBody> >::const_iterator aSubIter = mySubs.cbegin();
113     for(; aSubIter != mySubs.cend(); aSubIter++) {
114       if ((*aSubIter)->ModelAPI_ResultBody::isConcealed()) {
115         aResult = true;
116         break;
117       }
118     }
119   }
120   if (myLastConcealed != aResult) {
121     myLastConcealed = aResult;
122     //setIsConcealed(aResult); // set for all subs the same result
123     std::vector<std::shared_ptr<ModelAPI_ResultBody> >::const_iterator aSubIter = mySubs.cbegin();
124     for(; aSubIter != mySubs.cend(); aSubIter++) { // update the visualization status of each sub
125       if ((*aSubIter)->ModelAPI_ResultBody::isConcealed() != aResult) {
126         if (aResult) { // hidden unit must be redisplayed (hidden)
127           ModelAPI_EventCreator::get()->sendDeleted(document(), (*aSubIter)->groupName());
128           // redisplay for the viewer (it must be disappeared also)
129           static Events_ID EVENT_DISP =
130             Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
131           ModelAPI_EventCreator::get()->sendUpdated(*aSubIter, EVENT_DISP);
132         } else { // was not concealed become concealed => delete event
133           static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_CREATED);
134           ModelAPI_EventCreator::get()->sendUpdated(*aSubIter, anEvent);
135         }
136       }
137     }
138     // update the display state of the subs: explicitly call Model_ResultBody::isConcealed
139     for(aSubIter = mySubs.cbegin(); aSubIter != mySubs.cend(); aSubIter++) {
140       (*aSubIter)->isConcealed();
141     }
142   }
143   return aResult;
144 }
145
146 void Model_ResultCompSolid::setIsConcealed(const bool theValue)
147 {
148   if (theValue != ModelAPI_ResultCompSolid::isConcealed()) {
149     std::vector<std::shared_ptr<ModelAPI_ResultBody> >::const_iterator aSubIter = mySubs.cbegin();
150     for(; aSubIter != mySubs.cend(); aSubIter++) {
151       if ((*aSubIter)->ModelAPI_ResultBody::isConcealed() != theValue) {
152         if (theValue) { // hidden unit must be redisplayed (hidden)
153           ModelAPI_EventCreator::get()->sendDeleted(document(), (*aSubIter)->groupName());
154           // redisplay for the viewer (it must be disappeared also)
155           static Events_ID EVENT_DISP =
156             Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
157           ModelAPI_EventCreator::get()->sendUpdated(*aSubIter, EVENT_DISP);
158         } else { // was not concealed become concealed => delete event
159           static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_CREATED);
160           ModelAPI_EventCreator::get()->sendUpdated(*aSubIter, anEvent);
161         }
162       }
163     }
164     ModelAPI_ResultCompSolid::setIsConcealed(theValue);
165     // to set correct myLastConcealed
166     isConcealed();
167   }
168   //myLastConcealed = theValue;
169 }
170
171 void Model_ResultCompSolid::updateSubs(const std::shared_ptr<GeomAPI_Shape>& theThisShape)
172 {
173   static Events_Loop* aLoop = Events_Loop::loop();
174   static Events_ID EVENT_DISP = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
175   static Events_ID EVENT_UPD = aLoop->eventByName(EVENT_OBJECT_UPDATED);
176   static const ModelAPI_EventCreator* aECreator = ModelAPI_EventCreator::get();
177   // iterate all sub-solids of compsolid to make sub-results synchronized with them
178   TopoDS_Shape aThisShape;
179   if (theThisShape.get()) aThisShape = theThisShape->impl<TopoDS_Shape>();
180   if (!aThisShape.IsNull() && (aThisShape.ShapeType() == TopAbs_COMPSOLID ||
181        aThisShape.ShapeType() == TopAbs_COMPOUND)) {
182     bool aWasEmpty = mySubs.empty();
183     Model_Objects* anObjects = std::dynamic_pointer_cast<Model_Document>(document())->objects();
184     unsigned int aSubIndex = 0;
185     TopoDS_Iterator aShapesIter(aThisShape);
186     for(; aShapesIter.More(); aShapesIter.Next(), aSubIndex++) {
187       std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape);
188       aShape->setImpl(new TopoDS_Shape(aShapesIter.Value()));
189       ResultBodyPtr aSub;
190       if (mySubs.size() <= aSubIndex) { // it is needed to create a new sub-result
191         aSub = anObjects->createBody(this->data(), aSubIndex);
192         mySubs.push_back(aSub);
193       } else { // just update shape of this result
194         aSub = mySubs[aSubIndex];
195       }
196       if (!aShape->isEqual(aSub->shape())) {
197         aSub->store(aShape, false);
198         aECreator->sendUpdated(aSub, EVENT_DISP);
199         aECreator->sendUpdated(aSub, EVENT_UPD);
200       }
201       aSub->setDisabled(aSub, isDisabled());
202       aSub->setIsConcealed(myLastConcealed);
203     }
204     // erase left, unused results
205     while(mySubs.size() > aSubIndex) {
206       ResultBodyPtr anErased = *(mySubs.rbegin());
207       anErased->setDisabled(anErased, true);
208       mySubs.pop_back();
209     }
210     if (aWasEmpty) { // erase all subs
211       // redisplay this because result with and without subs are displayed differently
212       aECreator->sendUpdated(data()->owner(), EVENT_DISP);
213     }
214   } else if (!mySubs.empty()) { // erase all subs
215     while(!mySubs.empty()) {
216       ResultBodyPtr anErased = *(mySubs.rbegin());
217       anErased->setDisabled(anErased, true);
218       mySubs.pop_back();
219     }
220     // redisplay this because result with and without subs are displayed differently
221     aECreator->sendUpdated(data()->owner(), EVENT_DISP);
222   }
223 }
224
225 bool Model_ResultCompSolid::isLatestEqual(const std::shared_ptr<GeomAPI_Shape>& theShape)
226 {
227   if (myBuilder->isLatestEqual(theShape))
228     return true;
229   // also check that it is asked for sub-elements
230   std::vector<std::shared_ptr<ModelAPI_ResultBody> >::const_iterator aSubIter = mySubs.cbegin();
231   for(; aSubIter != mySubs.cend(); aSubIter++) {
232     if (aSubIter->get() && (*aSubIter)->isLatestEqual(theShape)) {
233       return true;
234     }
235   }
236   return false;
237 }