]> SALOME platform Git repositories - modules/shaper.git/blob - src/Model/Model_ResultCompSolid.cpp
Salome HOME
Speed-up of the access from OB to complicated compsolids in the frames of issue ...
[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     const bool theSplitInSubs)
74 {
75   if (theSplitInSubs && mySubs.size()) { // consists of subs
76     std::vector<std::shared_ptr<ModelAPI_ResultBody> >::const_iterator aSubIter = mySubs.cbegin();
77     for(; aSubIter != mySubs.cend(); aSubIter++) {
78       (*aSubIter)->loadAndOrientModifiedShapes(
79         theMS, theShapeIn, theKindOfShape, theTag, theName, theSubShapes, theIsStoreSeparate,
80         theIsStoreAsGenerated);
81     }
82   } else { // do for this directly
83     ModelAPI_ResultCompSolid::loadAndOrientModifiedShapes(
84     theMS, theShapeIn, theKindOfShape, theTag, theName, theSubShapes, theIsStoreSeparate,
85     theIsStoreAsGenerated);
86   }
87 }
88
89
90 int Model_ResultCompSolid::numberOfSubs(bool forTree) const
91 {
92   return int(mySubs.size());
93 }
94
95 std::shared_ptr<ModelAPI_ResultBody> Model_ResultCompSolid::subResult(const int theIndex,
96                                                                       bool forTree) const
97 {
98   return mySubs.at(theIndex);
99 }
100
101 bool Model_ResultCompSolid::isSub(ObjectPtr theObject, int& theIndex) const
102 {
103   std::map<ObjectPtr, int>::const_iterator aFound = mySubsMap.find(theObject);
104   if (aFound != mySubsMap.end()) {
105     theIndex = aFound->second;
106     return true;
107   }
108   return false;
109 }
110
111 void Model_ResultCompSolid::colorConfigInfo(std::string& theSection, std::string& theName,
112   std::string& theDefault)
113 {
114   theSection = "Visualization";
115   theName = "result_body_color";
116   theDefault = DEFAULT_COLOR();
117 }
118
119 bool Model_ResultCompSolid::setDisabled(std::shared_ptr<ModelAPI_Result> theThis,
120                                         const bool theFlag)
121 {
122   bool aChanged = ModelAPI_ResultBody::setDisabled(theThis, theFlag);
123   if (aChanged) { // state is changed, so modifications are needed
124     myBuilder->evolutionToSelection(theFlag);
125     updateSubs(shape()); // to set disabled/enabled
126   }
127   return aChanged;
128 }
129
130 bool Model_ResultCompSolid::isConcealed()
131 {
132   bool aResult = false;;
133   if (ModelAPI_ResultCompSolid::isConcealed()) {
134     aResult = true;
135   } else {
136     std::vector<std::shared_ptr<ModelAPI_ResultBody> >::const_iterator aSubIter = mySubs.cbegin();
137     for(; aSubIter != mySubs.cend(); aSubIter++) {
138       if ((*aSubIter)->ModelAPI_ResultBody::isConcealed()) {
139         aResult = true;
140         break;
141       }
142     }
143   }
144   if (myLastConcealed != aResult) {
145     myLastConcealed = aResult;
146     //setIsConcealed(aResult); // set for all subs the same result
147     std::vector<std::shared_ptr<ModelAPI_ResultBody> >::const_iterator aSubIter = mySubs.cbegin();
148     for(; aSubIter != mySubs.cend(); aSubIter++) { // update the visualization status of each sub
149       if ((*aSubIter)->ModelAPI_ResultBody::isConcealed() != aResult) {
150         if (aResult) { // hidden unit must be redisplayed (hidden)
151           ModelAPI_EventCreator::get()->sendDeleted(document(), (*aSubIter)->groupName());
152           // redisplay for the viewer (it must be disappeared also)
153           static Events_ID EVENT_DISP =
154             Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
155           ModelAPI_EventCreator::get()->sendUpdated(*aSubIter, EVENT_DISP);
156         } else { // was not concealed become concealed => delete event
157           static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_CREATED);
158           ModelAPI_EventCreator::get()->sendUpdated(*aSubIter, anEvent);
159         }
160       }
161     }
162     // update the display state of the subs: explicitly call Model_ResultBody::isConcealed
163     for(aSubIter = mySubs.cbegin(); aSubIter != mySubs.cend(); aSubIter++) {
164       (*aSubIter)->isConcealed();
165     }
166   }
167   return aResult;
168 }
169
170 void Model_ResultCompSolid::setIsConcealed(const bool theValue)
171 {
172   if (theValue != ModelAPI_ResultCompSolid::isConcealed()) {
173     std::vector<std::shared_ptr<ModelAPI_ResultBody> >::const_iterator aSubIter = mySubs.cbegin();
174     for(; aSubIter != mySubs.cend(); aSubIter++) {
175       if ((*aSubIter)->ModelAPI_ResultBody::isConcealed() != theValue) {
176         if (theValue) { // hidden unit must be redisplayed (hidden)
177           ModelAPI_EventCreator::get()->sendDeleted(document(), (*aSubIter)->groupName());
178           // redisplay for the viewer (it must be disappeared also)
179           static Events_ID EVENT_DISP =
180             Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
181           ModelAPI_EventCreator::get()->sendUpdated(*aSubIter, EVENT_DISP);
182         } else { // was not concealed become concealed => delete event
183           static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_CREATED);
184           ModelAPI_EventCreator::get()->sendUpdated(*aSubIter, anEvent);
185         }
186       }
187     }
188     ModelAPI_ResultCompSolid::setIsConcealed(theValue);
189     // to set correct myLastConcealed
190     isConcealed();
191   }
192   //myLastConcealed = theValue;
193 }
194
195 void Model_ResultCompSolid::updateSubs(const std::shared_ptr<GeomAPI_Shape>& theThisShape)
196 {
197   static Events_Loop* aLoop = Events_Loop::loop();
198   static Events_ID EVENT_DISP = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
199   static Events_ID EVENT_UPD = aLoop->eventByName(EVENT_OBJECT_UPDATED);
200   static const ModelAPI_EventCreator* aECreator = ModelAPI_EventCreator::get();
201   // iterate all sub-solids of compsolid to make sub-results synchronized with them
202   TopoDS_Shape aThisShape;
203   if (theThisShape.get()) aThisShape = theThisShape->impl<TopoDS_Shape>();
204   if (!aThisShape.IsNull() && (aThisShape.ShapeType() == TopAbs_COMPSOLID ||
205        aThisShape.ShapeType() == TopAbs_COMPOUND)) {
206     bool aWasEmpty = mySubs.empty();
207     Model_Objects* anObjects = std::dynamic_pointer_cast<Model_Document>(document())->objects();
208     unsigned int aSubIndex = 0;
209     TopoDS_Iterator aShapesIter(aThisShape);
210     for(; aShapesIter.More(); aShapesIter.Next(), aSubIndex++) {
211       std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape);
212       aShape->setImpl(new TopoDS_Shape(aShapesIter.Value()));
213       ResultBodyPtr aSub;
214       if (mySubs.size() <= aSubIndex) { // it is needed to create a new sub-result
215         aSub = anObjects->createBody(this->data(), aSubIndex);
216         mySubs.push_back(aSub);
217         mySubsMap[aSub] = int(mySubs.size() - 1);
218       } else { // just update shape of this result
219         aSub = mySubs[aSubIndex];
220       }
221       if (!aShape->isEqual(aSub->shape())) {
222         aSub->store(aShape, false);
223         aECreator->sendUpdated(aSub, EVENT_DISP);
224         aECreator->sendUpdated(aSub, EVENT_UPD);
225       }
226       aSub->setDisabled(aSub, isDisabled());
227       aSub->setIsConcealed(myLastConcealed);
228     }
229     // erase left, unused results
230     while(mySubs.size() > aSubIndex) {
231       ResultBodyPtr anErased = *(mySubs.rbegin());
232       anErased->setDisabled(anErased, true);
233       mySubsMap.erase(anErased);
234       mySubs.pop_back();
235     }
236     if (aWasEmpty) { // erase all subs
237       // redisplay this because result with and without subs are displayed differently
238       aECreator->sendUpdated(data()->owner(), EVENT_DISP);
239     }
240   } else if (!mySubs.empty()) { // erase all subs
241     while(!mySubs.empty()) {
242       ResultBodyPtr anErased = *(mySubs.rbegin());
243       if (anErased->data()->isValid())
244         anErased->setDisabled(anErased, true);
245       mySubs.pop_back();
246     }
247     mySubsMap.clear();
248     // redisplay this because result with and without subs are displayed differently
249     aECreator->sendUpdated(data()->owner(), EVENT_DISP);
250   }
251 }
252
253 bool Model_ResultCompSolid::isLatestEqual(const std::shared_ptr<GeomAPI_Shape>& theShape)
254 {
255   if (myBuilder->isLatestEqual(theShape))
256     return true;
257   // also check that it is asked for sub-elements
258   std::vector<std::shared_ptr<ModelAPI_ResultBody> >::const_iterator aSubIter = mySubs.cbegin();
259   for(; aSubIter != mySubs.cend(); aSubIter++) {
260     if (aSubIter->get() && (*aSubIter)->isLatestEqual(theShape)) {
261       return true;
262     }
263   }
264   return false;
265 }