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