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