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