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