Salome HOME
Fix unstability of number of Bodies on OB on Linux
[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   setIsConcealed(false);
27   myIsDisabled = true; // by default it is not initialized and false to be after created
28   updateSubs(shape()); // in case of open, etc.
29 }
30
31 Model_ResultCompSolid::~Model_ResultCompSolid()
32 {
33   updateSubs(std::shared_ptr<GeomAPI_Shape>()); // erase sub-results
34 }
35
36 void Model_ResultCompSolid::initAttributes()
37 {
38   DataPtr aData = data();
39   aData->addAttribute(COLOR_ID(), ModelAPI_AttributeIntArray::typeId());
40 }
41
42 void Model_ResultCompSolid::store(const std::shared_ptr<GeomAPI_Shape>& theShape)
43 {
44   ModelAPI_ResultCompSolid::store(theShape);
45   updateSubs(theShape);
46 }
47
48 void Model_ResultCompSolid::storeGenerated(const std::shared_ptr<GeomAPI_Shape>& theFromShape,
49     const std::shared_ptr<GeomAPI_Shape>& theToShape)
50 {
51   ModelAPI_ResultCompSolid::storeGenerated(theFromShape, theToShape);
52   updateSubs(theToShape);
53 }
54
55 void Model_ResultCompSolid::storeModified(const std::shared_ptr<GeomAPI_Shape>& theOldShape,
56     const std::shared_ptr<GeomAPI_Shape>& theNewShape, const int theDecomposeSolidsTag)
57 {
58   ModelAPI_ResultCompSolid::storeModified(theOldShape, theNewShape, theDecomposeSolidsTag);
59   updateSubs(theNewShape);
60 }
61
62 int Model_ResultCompSolid::numberOfSubs(bool forTree) const
63 {
64   return mySubs.size();
65 }
66
67 std::shared_ptr<ModelAPI_ResultBody> Model_ResultCompSolid::subResult(const int theIndex,
68                                                                       bool forTree) const
69 {
70   return mySubs.at(theIndex);
71 }
72
73 bool Model_ResultCompSolid::isSub(ObjectPtr theObject) const
74 {
75   std::vector<std::shared_ptr<ModelAPI_ResultBody> >::const_iterator aSubIter = mySubs.cbegin();
76   for(; aSubIter != mySubs.cend(); aSubIter++)
77     if (*aSubIter == theObject)
78       return true;
79   return false;
80 }
81
82 void Model_ResultCompSolid::colorConfigInfo(std::string& theSection, std::string& theName,
83   std::string& theDefault)
84 {
85   theSection = "Visualization";
86   theName = "result_body_color";
87   theDefault = DEFAULT_COLOR();
88 }
89
90 bool Model_ResultCompSolid::setDisabled(std::shared_ptr<ModelAPI_Result> theThis, const bool theFlag)
91 {
92   bool aChanged = ModelAPI_ResultBody::setDisabled(theThis, theFlag);
93   if (aChanged) { // state is changed, so modifications are needed
94     myBuilder->evolutionToSelection(theFlag);
95     updateSubs(shape()); // to set disabled/enabled 
96   }
97   return aChanged;
98 }
99
100 bool Model_ResultCompSolid::isConcealed()
101 {
102   if (ModelAPI_ResultCompSolid::isConcealed())
103     return true;
104   std::vector<std::shared_ptr<ModelAPI_ResultBody> >::const_iterator aSubIter = mySubs.cbegin();
105   for(; aSubIter != mySubs.cend(); aSubIter++)
106     if ((*aSubIter)->isConcealed())
107       return true;
108   return false;
109 }
110
111 void Model_ResultCompSolid::updateSubs(const std::shared_ptr<GeomAPI_Shape>& theThisShape)
112 {
113   static Events_Loop* aLoop = Events_Loop::loop();
114   static Events_ID EVENT_DISP = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
115   static Events_ID EVENT_UPD = aLoop->eventByName(EVENT_OBJECT_UPDATED);
116   static const ModelAPI_EventCreator* aECreator = ModelAPI_EventCreator::get();
117   // iterate all sub-solids of compsolid to make sub-results synchronized with them
118   TopoDS_Shape aThisShape;
119   if (theThisShape.get()) aThisShape = theThisShape->impl<TopoDS_Shape>();
120   if (!aThisShape.IsNull() && (aThisShape.ShapeType() == TopAbs_COMPSOLID ||
121        aThisShape.ShapeType() == TopAbs_COMPOUND)) {
122     bool aWasEmpty = mySubs.empty();
123     Model_Objects* anObjects = std::dynamic_pointer_cast<Model_Document>(document())->objects();
124     unsigned int aSubIndex = 0;
125     TopExp_Explorer aSolids(aThisShape, TopAbs_SOLID);
126     for(; aSolids.More(); aSolids.Next(), aSubIndex++) {
127       std::shared_ptr<GeomAPI_Shape> aSolidShape(new GeomAPI_Shape);
128       aSolidShape->setImpl(new TopoDS_Shape(aSolids.Current()));
129       ResultBodyPtr aSub;
130       if (mySubs.size() <= aSubIndex) { // it is needed to create a new sub-result
131         aSub = anObjects->createBody(this->data(), aSubIndex);
132         mySubs.push_back(aSub);
133       } else { // just update shape of this result
134         aSub = mySubs[aSubIndex];
135       }
136       if (!aSolidShape->isEqual(aSub->shape())) {
137         aSub->store(aSolidShape);
138         aECreator->sendUpdated(aSub, EVENT_DISP);
139         aECreator->sendUpdated(aSub, EVENT_UPD);
140       }
141       aSub->setDisabled(aSub, isDisabled());
142     }
143     // erase left, unused results
144     while(mySubs.size() > aSubIndex) {
145       ResultBodyPtr anErased = *(mySubs.rbegin());
146       anErased->setDisabled(anErased, true);
147       mySubs.pop_back();
148     }
149     if (aWasEmpty) { // erase all subs
150       // redisplay this because result with and without subs are displayed differently
151       aECreator->sendUpdated(data()->owner(), EVENT_DISP);
152     }
153   } else if (!mySubs.empty()) { // erase all subs
154     while(!mySubs.empty()) {
155       ResultBodyPtr anErased = *(mySubs.rbegin());
156       anErased->setDisabled(anErased, true);
157       mySubs.pop_back();
158     }
159     // redisplay this because result with and without subs are displayed differently
160     aECreator->sendUpdated(data()->owner(), EVENT_DISP);
161   }
162 }