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