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