]> SALOME platform Git repositories - modules/shaper.git/blob - src/Model/Model_ResultCompSolid.cpp
Salome HOME
Issue #1774: Can't select edge for tangent constraint creation
[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, const bool theFlag)
83 {
84   bool aChanged = ModelAPI_ResultBody::setDisabled(theThis, theFlag);
85   if (aChanged) { // state is changed, so modifications are needed
86     myBuilder->evolutionToSelection(theFlag);
87     updateSubs(shape()); // to set disabled/enabled 
88   }
89   return aChanged;
90 }
91
92 bool Model_ResultCompSolid::isConcealed()
93 {
94   bool aResult = false;;
95   if (ModelAPI_ResultCompSolid::isConcealed()) {
96     aResult = true;
97   } else {
98     std::vector<std::shared_ptr<ModelAPI_ResultBody> >::const_iterator aSubIter = mySubs.cbegin();
99     for(; aSubIter != mySubs.cend(); aSubIter++) {
100       if ((*aSubIter)->ModelAPI_ResultBody::isConcealed()) {
101         aResult = true;
102         break;
103       }
104     }
105   }
106   if (myLastConcealed != aResult) {
107     myLastConcealed = aResult;
108     //setIsConcealed(aResult); // set for all subs the same result
109     std::vector<std::shared_ptr<ModelAPI_ResultBody> >::const_iterator aSubIter = mySubs.cbegin();
110     for(; aSubIter != mySubs.cend(); aSubIter++) { // update the visualization status of each sub
111       if ((*aSubIter)->ModelAPI_ResultBody::isConcealed() != aResult) {
112         if (aResult) { // hidden unit must be redisplayed (hidden)
113           ModelAPI_EventCreator::get()->sendDeleted(document(), (*aSubIter)->groupName());
114           // redisplay for the viewer (it must be disappeared also)
115           static Events_ID EVENT_DISP = 
116             Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
117           ModelAPI_EventCreator::get()->sendUpdated(*aSubIter, EVENT_DISP);
118         } else { // was not concealed become concealed => delete event
119           static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_CREATED);
120           ModelAPI_EventCreator::get()->sendUpdated(*aSubIter, anEvent);
121         }
122       }
123     }
124   }
125   return aResult;
126 }
127
128 void Model_ResultCompSolid::setIsConcealed(const bool theValue)
129 {
130   if (theValue != ModelAPI_ResultCompSolid::isConcealed()) {
131     std::vector<std::shared_ptr<ModelAPI_ResultBody> >::const_iterator aSubIter = mySubs.cbegin();
132     for(; aSubIter != mySubs.cend(); aSubIter++) {
133       if ((*aSubIter)->ModelAPI_ResultBody::isConcealed() != theValue) {
134         if (theValue) { // hidden unit must be redisplayed (hidden)
135           ModelAPI_EventCreator::get()->sendDeleted(document(), (*aSubIter)->groupName());
136           // redisplay for the viewer (it must be disappeared also)
137           static Events_ID EVENT_DISP = 
138             Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
139           ModelAPI_EventCreator::get()->sendUpdated(*aSubIter, EVENT_DISP);
140         } else { // was not concealed become concealed => delete event
141           static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_CREATED);
142           ModelAPI_EventCreator::get()->sendUpdated(*aSubIter, anEvent);
143         }
144       }
145     }
146     ModelAPI_ResultCompSolid::setIsConcealed(theValue);
147     // to set correct myLastConcealed
148     isConcealed();
149   }
150   //myLastConcealed = theValue;
151 }
152
153 void Model_ResultCompSolid::updateSubs(const std::shared_ptr<GeomAPI_Shape>& theThisShape)
154 {
155   static Events_Loop* aLoop = Events_Loop::loop();
156   static Events_ID EVENT_DISP = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
157   static Events_ID EVENT_UPD = aLoop->eventByName(EVENT_OBJECT_UPDATED);
158   static const ModelAPI_EventCreator* aECreator = ModelAPI_EventCreator::get();
159   // iterate all sub-solids of compsolid to make sub-results synchronized with them
160   TopoDS_Shape aThisShape;
161   if (theThisShape.get()) aThisShape = theThisShape->impl<TopoDS_Shape>();
162   if (!aThisShape.IsNull() && (aThisShape.ShapeType() == TopAbs_COMPSOLID ||
163        aThisShape.ShapeType() == TopAbs_COMPOUND)) {
164     bool aWasEmpty = mySubs.empty();
165     Model_Objects* anObjects = std::dynamic_pointer_cast<Model_Document>(document())->objects();
166     unsigned int aSubIndex = 0;
167     TopoDS_Iterator aShapesIter(aThisShape);
168     for(; aShapesIter.More(); aShapesIter.Next(), aSubIndex++) {
169       std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape);
170       aShape->setImpl(new TopoDS_Shape(aShapesIter.Value()));
171       ResultBodyPtr aSub;
172       if (mySubs.size() <= aSubIndex) { // it is needed to create a new sub-result
173         aSub = anObjects->createBody(this->data(), aSubIndex);
174         mySubs.push_back(aSub);
175       } else { // just update shape of this result
176         aSub = mySubs[aSubIndex];
177       }
178       if (!aShape->isEqual(aSub->shape())) {
179         aSub->store(aShape, false);
180         aECreator->sendUpdated(aSub, EVENT_DISP);
181         aECreator->sendUpdated(aSub, EVENT_UPD);
182       }
183       aSub->setDisabled(aSub, isDisabled());
184       aSub->setIsConcealed(myLastConcealed);
185     }
186     // erase left, unused results
187     while(mySubs.size() > aSubIndex) {
188       ResultBodyPtr anErased = *(mySubs.rbegin());
189       anErased->setDisabled(anErased, true);
190       mySubs.pop_back();
191     }
192     if (aWasEmpty) { // erase all subs
193       // redisplay this because result with and without subs are displayed differently
194       aECreator->sendUpdated(data()->owner(), EVENT_DISP);
195     }
196   } else if (!mySubs.empty()) { // erase all subs
197     while(!mySubs.empty()) {
198       ResultBodyPtr anErased = *(mySubs.rbegin());
199       anErased->setDisabled(anErased, true);
200       mySubs.pop_back();
201     }
202     // redisplay this because result with and without subs are displayed differently
203     aECreator->sendUpdated(data()->owner(), EVENT_DISP);
204   }
205 }
206
207 bool Model_ResultCompSolid::isLatestEqual(const std::shared_ptr<GeomAPI_Shape>& theShape)
208 {
209   if (myBuilder->isLatestEqual(theShape))
210     return true;
211   // also check that it is asked for sub-elements
212   std::vector<std::shared_ptr<ModelAPI_ResultBody> >::const_iterator aSubIter = mySubs.cbegin();
213   for(; aSubIter != mySubs.cend(); aSubIter++) {
214     if (aSubIter->get() && (*aSubIter)->isLatestEqual(theShape)) {
215       return true;
216     }
217   }
218   return false;
219 }