1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: Model_ResultCompSolid.cpp
4 // Created: 20 Jul 2015
5 // Author: Natalia ERMOLAEVA
7 #include <Model_ResultCompSolid.h>
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>
19 #include <TopoDS_Shape.hxx>
20 #include <TopExp_Explorer.hxx>
23 Model_ResultCompSolid::Model_ResultCompSolid()
25 myBuilder = new Model_BodyBuilder(this);
26 myLastConcealed = false;
27 updateSubs(shape()); // in case of open, etc.
30 Model_ResultCompSolid::~Model_ResultCompSolid()
32 updateSubs(std::shared_ptr<GeomAPI_Shape>()); // erase sub-results
35 void Model_ResultCompSolid::initAttributes()
37 DataPtr aData = data();
38 aData->addAttribute(COLOR_ID(), ModelAPI_AttributeIntArray::typeId());
41 void Model_ResultCompSolid::store(const std::shared_ptr<GeomAPI_Shape>& theShape)
43 ModelAPI_ResultCompSolid::store(theShape);
47 void Model_ResultCompSolid::storeGenerated(const std::shared_ptr<GeomAPI_Shape>& theFromShape,
48 const std::shared_ptr<GeomAPI_Shape>& theToShape)
50 ModelAPI_ResultCompSolid::storeGenerated(theFromShape, theToShape);
51 updateSubs(theToShape);
54 void Model_ResultCompSolid::storeModified(const std::shared_ptr<GeomAPI_Shape>& theOldShape,
55 const std::shared_ptr<GeomAPI_Shape>& theNewShape, const int theDecomposeSolidsTag)
57 ModelAPI_ResultCompSolid::storeModified(theOldShape, theNewShape, theDecomposeSolidsTag);
58 updateSubs(theNewShape);
61 int Model_ResultCompSolid::numberOfSubs(bool forTree) const
66 std::shared_ptr<ModelAPI_ResultBody> Model_ResultCompSolid::subResult(const int theIndex,
69 return mySubs.at(theIndex);
72 bool Model_ResultCompSolid::isSub(ObjectPtr theObject) const
74 std::vector<std::shared_ptr<ModelAPI_ResultBody> >::const_iterator aSubIter = mySubs.cbegin();
75 for(; aSubIter != mySubs.cend(); aSubIter++)
76 if (*aSubIter == theObject)
81 void Model_ResultCompSolid::colorConfigInfo(std::string& theSection, std::string& theName,
82 std::string& theDefault)
84 theSection = "Visualization";
85 theName = "result_body_color";
86 theDefault = DEFAULT_COLOR();
89 bool Model_ResultCompSolid::setDisabled(std::shared_ptr<ModelAPI_Result> theThis, const bool theFlag)
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
99 bool Model_ResultCompSolid::isConcealed()
101 bool aResult = false;;
102 if (ModelAPI_ResultCompSolid::isConcealed()) {
105 std::vector<std::shared_ptr<ModelAPI_ResultBody> >::const_iterator aSubIter = mySubs.cbegin();
106 for(; aSubIter != mySubs.cend(); aSubIter++)
107 if ((*aSubIter)->isConcealed())
110 if (myLastConcealed != aResult) {
111 myLastConcealed = aResult;
112 setIsConcealed(aResult); // set for all subs the same result
117 void Model_ResultCompSolid::setIsConcealed(const bool theValue)
119 ModelAPI_ResultCompSolid::setIsConcealed(theValue);
120 std::vector<std::shared_ptr<ModelAPI_ResultBody> >::const_iterator aSubIter = mySubs.cbegin();
121 for(; aSubIter != mySubs.cend(); aSubIter++) {
122 if ((*aSubIter)->isConcealed() != theValue) {
123 (*aSubIter)->setIsConcealed(theValue);
124 if (theValue) { // hidden unit must be redisplayed (hidden)
125 ModelAPI_EventCreator::get()->sendDeleted(document(), (*aSubIter)->groupName());
126 // redisplay for the viewer (it must be disappeared also)
127 static Events_ID EVENT_DISP =
128 Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
129 ModelAPI_EventCreator::get()->sendUpdated(*aSubIter, EVENT_DISP);
130 } else { // was not concealed become concealed => delete event
131 static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_CREATED);
132 ModelAPI_EventCreator::get()->sendUpdated(*aSubIter, anEvent);
138 void Model_ResultCompSolid::updateSubs(const std::shared_ptr<GeomAPI_Shape>& theThisShape)
140 static Events_Loop* aLoop = Events_Loop::loop();
141 static Events_ID EVENT_DISP = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
142 static Events_ID EVENT_UPD = aLoop->eventByName(EVENT_OBJECT_UPDATED);
143 static const ModelAPI_EventCreator* aECreator = ModelAPI_EventCreator::get();
144 // iterate all sub-solids of compsolid to make sub-results synchronized with them
145 TopoDS_Shape aThisShape;
146 if (theThisShape.get()) aThisShape = theThisShape->impl<TopoDS_Shape>();
147 if (!aThisShape.IsNull() && (aThisShape.ShapeType() == TopAbs_COMPSOLID ||
148 aThisShape.ShapeType() == TopAbs_COMPOUND)) {
149 bool aWasEmpty = mySubs.empty();
150 Model_Objects* anObjects = std::dynamic_pointer_cast<Model_Document>(document())->objects();
151 unsigned int aSubIndex = 0;
152 TopExp_Explorer aSolids(aThisShape, TopAbs_SOLID);
153 for(; aSolids.More(); aSolids.Next(), aSubIndex++) {
154 std::shared_ptr<GeomAPI_Shape> aSolidShape(new GeomAPI_Shape);
155 aSolidShape->setImpl(new TopoDS_Shape(aSolids.Current()));
157 if (mySubs.size() <= aSubIndex) { // it is needed to create a new sub-result
158 aSub = anObjects->createBody(this->data(), aSubIndex);
159 mySubs.push_back(aSub);
160 } else { // just update shape of this result
161 aSub = mySubs[aSubIndex];
163 if (!aSolidShape->isEqual(aSub->shape())) {
164 aSub->store(aSolidShape);
165 aECreator->sendUpdated(aSub, EVENT_DISP);
166 aECreator->sendUpdated(aSub, EVENT_UPD);
168 aSub->setDisabled(aSub, isDisabled());
169 aSub->setIsConcealed(myLastConcealed);
171 // erase left, unused results
172 while(mySubs.size() > aSubIndex) {
173 ResultBodyPtr anErased = *(mySubs.rbegin());
174 anErased->setDisabled(anErased, true);
177 if (aWasEmpty) { // erase all subs
178 // redisplay this because result with and without subs are displayed differently
179 aECreator->sendUpdated(data()->owner(), EVENT_DISP);
181 } else if (!mySubs.empty()) { // erase all subs
182 while(!mySubs.empty()) {
183 ResultBodyPtr anErased = *(mySubs.rbegin());
184 anErased->setDisabled(anErased, true);
187 // redisplay this because result with and without subs are displayed differently
188 aECreator->sendUpdated(data()->owner(), EVENT_DISP);
192 bool Model_ResultCompSolid::isLatestEqual(const std::shared_ptr<GeomAPI_Shape>& theShape)
194 return myBuilder->isLatestEqual(theShape);