1 // Copyright (C) 2014-2020 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include <Model_ResultBody.h>
22 #include <Model_Document.h>
23 #include <Model_Objects.h>
24 #include <Model_BodyBuilder.h>
25 #include <Model_Document.h>
26 #include <ModelAPI_Object.h>
27 #include <ModelAPI_Events.h>
28 #include <ModelAPI_Tools.h>
29 #include <Model_Data.h>
30 #include <Events_Loop.h>
31 #include <GeomAPI_ShapeIterator.h>
32 #include <GeomAPI_ShapeExplorer.h>
33 #include <GeomAPI_Face.h>
34 #include <GeomAPI_Pnt.h>
36 #include <TopoDS_Shape.hxx>
37 #include <TopExp_Explorer.hxx>
38 #include <TopTools_MapOfShape.hxx>
39 #include <TDataStd_UAttribute.hxx>
41 // if this attribute exists, the shape is connected topology
42 Standard_GUID kIsConnectedTopology("e51392e0-3a4d-405d-8e36-bbfe19858ef5");
43 // if this attribute exists, the connected topology flag must be recomputed
44 Standard_GUID kUpdateConnectedTopology("01ef7a45-0bec-4266-b0b4-4aa570921818");
46 Model_ResultBody::Model_ResultBody() : ModelAPI_ResultBody()
48 myBuilder = new Model_BodyBuilder(this);
49 myLastConcealed = false;
50 updateSubs(shape()); // in case of open, etc.
53 Model_ResultBody::~Model_ResultBody()
55 updateSubs(std::shared_ptr<GeomAPI_Shape>()); // erase sub-results
59 bool Model_ResultBody::generated(const GeomShapePtr& theNewShape,
60 const std::string& theName, const bool theCheckIsInResult)
63 if (mySubs.size()) { // consists of subs
64 for (std::vector<ResultBodyPtr>::const_iterator aSubIter = mySubs.cbegin();
65 aSubIter != mySubs.cend();
68 const ResultBodyPtr& aSub = *aSubIter;
69 if (aSub->generated(theNewShape, theName, theCheckIsInResult))
72 } else { // do for this directly
73 if (myBuilder->generated(theNewShape, theName, theCheckIsInResult))
79 void Model_ResultBody::loadGeneratedShapes(const std::shared_ptr<GeomAlgoAPI_MakeShape>& theAlgo,
80 const GeomShapePtr& theOldShape,
81 const GeomAPI_Shape::ShapeType theShapeTypeToExplore,
82 const std::string& theName,
83 const bool theSaveOldIfNotInTree)
85 if (mySubs.size()) { // consists of subs
86 for (std::vector<ResultBodyPtr>::const_iterator aSubIter = mySubs.cbegin();
87 aSubIter != mySubs.cend();
90 const ResultBodyPtr& aSub = *aSubIter;
91 aSub->loadGeneratedShapes(
92 theAlgo, theOldShape, theShapeTypeToExplore, theName, theSaveOldIfNotInTree);
94 } else { // do for this directly
95 myBuilder->loadGeneratedShapes(
96 theAlgo, theOldShape, theShapeTypeToExplore, theName, theSaveOldIfNotInTree);
100 void Model_ResultBody::loadModifiedShapes(const std::shared_ptr<GeomAlgoAPI_MakeShape>& theAlgo,
101 const GeomShapePtr& theOldShape,
102 const GeomAPI_Shape::ShapeType theShapeTypeToExplore,
103 const std::string& theName)
105 if (mySubs.size()) { // consists of subs
106 // optimization of getting of new shapes for specific sub-result
107 if (!theAlgo->isNewShapesCollected(theOldShape, theShapeTypeToExplore))
108 theAlgo->collectNewShapes(theOldShape, theShapeTypeToExplore);
109 std::vector<ResultBodyPtr>::const_iterator aSubIter = mySubs.cbegin();
110 for(; aSubIter != mySubs.cend(); aSubIter++) {
111 (*aSubIter)->loadModifiedShapes(theAlgo, theOldShape, theShapeTypeToExplore, theName);
113 } else { // do for this directly
114 myBuilder->loadModifiedShapes(theAlgo, theOldShape, theShapeTypeToExplore, theName);
118 void Model_ResultBody::loadFirstLevel(GeomShapePtr theShape, const std::string& theName)
120 if (mySubs.size()) { // consists of subs
121 for (std::vector<ResultBodyPtr>::const_iterator aSubIter = mySubs.cbegin();
122 aSubIter != mySubs.cend();
125 const ResultBodyPtr& aSub = *aSubIter;
126 aSub->loadFirstLevel(theShape, theName);
128 } else { // do for this directly
129 myBuilder->loadFirstLevel(theShape, theName);
133 int Model_ResultBody::numberOfSubs(bool /*forTree*/) const
135 return int(mySubs.size());
138 ResultBodyPtr Model_ResultBody::subResult(const int theIndex, bool /*forTree*/) const
140 if (theIndex >= int(mySubs.size()))
141 return ResultBodyPtr();
142 return mySubs.at(theIndex);
145 bool Model_ResultBody::isSub(ObjectPtr theResult, int& theIndex) const
147 std::map<ObjectPtr, int>::const_iterator aFound = mySubsMap.find(theResult);
148 if (aFound != mySubsMap.end()) {
149 theIndex = aFound->second;
155 void Model_ResultBody::colorConfigInfo(std::string& theSection, std::string& theName,
156 std::string& theDefault)
158 theSection = "Visualization";
159 theName = "result_body_color";
160 theDefault = DEFAULT_COLOR();
163 bool Model_ResultBody::setDisabled(std::shared_ptr<ModelAPI_Result> theThis, const bool theFlag)
165 bool aChanged = ModelAPI_ResultBody::setDisabled(theThis, theFlag);
166 if (aChanged) { // state is changed, so modifications are needed
167 updateSubs(shape(), false); // to set disabled/enabled
172 bool Model_ResultBody::isConcealed()
174 return myLastConcealed;
177 void Model_ResultBody::setIsConcealed(const bool theValue, const bool theForced)
179 if (ModelAPI_ResultBody::isConcealed() != theValue) {
180 ModelAPI_ResultBody::setIsConcealed(theValue, theForced);
185 // recursively check all subs for concealment flag, returns true if everybody have "flag" state,
186 // in theAll returns results with "flag" state
187 static bool checkAllSubs(ResultBodyPtr theParent, bool theFlag, std::list<ResultBodyPtr>& theAll)
189 if (theParent->isConcealed() != theFlag)
190 theAll.push_back(theParent);
191 bool aResult = theParent->ModelAPI_ResultBody::isConcealed() == theFlag;
192 for(int a = 0; a < theParent->numberOfSubs(); a++) {
193 bool aSubRes = checkAllSubs(theParent->subResult(a), theFlag, theAll);
195 aResult = aResult || aSubRes; // concealed: one makes concealed everyone
197 aResult = aResult && aSubRes; // not concealed: all must be not concealed
202 void Model_ResultBody::updateConcealment()
204 if (myLastConcealed != ModelAPI_ResultBody::isConcealed()) {
205 // check the whole tree of results: if one is concealed, everybody are concealed
206 ResultBodyPtr anOwner = std::dynamic_pointer_cast<ModelAPI_ResultBody>(data()->owner());
208 return; // "this" is invalid
209 ResultBodyPtr aParent = ModelAPI_Tools::bodyOwner(anOwner);
210 while(aParent.get()) {
212 aParent = ModelAPI_Tools::bodyOwner(anOwner);
214 // iterate all results and collect all results whose state may be updated
215 std::list<ResultBodyPtr> anUpdated;
216 bool aNewFlag = !myLastConcealed;
217 if (checkAllSubs(anOwner, aNewFlag, anUpdated)) { // state of everyone must be updated
218 std::list<ResultBodyPtr>::iterator aRes = anUpdated.begin();
219 for(; aRes != anUpdated.end(); aRes++) {
220 bool aLastConcealed = (*aRes)->isConcealed();
221 if (aNewFlag != aLastConcealed) {
222 std::dynamic_pointer_cast<Model_ResultBody>(*aRes)->myLastConcealed = aNewFlag;
223 if (aNewFlag) { // become concealed, behaves like removed
224 ModelAPI_EventCreator::get()->sendDeleted(document(), groupName());
225 } else { // become not-concealed, behaves like created
226 static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_CREATED);
227 ModelAPI_EventCreator::get()->sendUpdated(*aRes, anEvent);
229 static Events_ID EVENT_DISP = // must be redisplayed in any case
230 Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
231 ModelAPI_EventCreator::get()->sendUpdated(*aRes, EVENT_DISP);
238 void Model_ResultBody::addShapeColor( const std::wstring& theName,std::vector<int>& color) {
240 if (myColorsShape.find(theName) == myColorsShape.end())
241 myColorsShape[ theName ] = color;
244 std::wstring Model_ResultBody::addShapeName(std::shared_ptr<GeomAPI_Shape> theshape,
245 const std::wstring& theName ){
248 std::wstringstream aName;
250 while(myNamesShape.find(aName.str()) != myNamesShape.end() ){
252 aName << theName << L"__" << indice;
255 myNamesShape[ aName.str() ] = theshape;
260 std::wstring Model_ResultBody::findShapeName(std::shared_ptr<GeomAPI_Shape> theShape){
262 TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
263 for (std::map< std::wstring, std::shared_ptr<GeomAPI_Shape> >::iterator it =
264 myNamesShape.begin();
265 it != myNamesShape.end();
268 TopoDS_Shape curSelectedShape = (*it).second->impl<TopoDS_Shape>();
269 if ((aShape.IsSame(curSelectedShape))) {
273 return L"material not found" ;
276 void Model_ResultBody::setShapeName(
277 std::map< std::wstring, std::shared_ptr<GeomAPI_Shape>>& theShapeName,
278 std::map< std::wstring, std::vector<int>>& theColorsShape)
280 myNamesShape = theShapeName;
281 myColorsShape = theColorsShape;
284 void Model_ResultBody::clearShapeNameAndColor(){
285 myNamesShape.clear();
286 myColorsShape.clear();
289 void Model_ResultBody::updateSubs(const std::shared_ptr<GeomAPI_Shape>& theThisShape,
290 const bool theShapeChanged)
292 static Events_Loop* aLoop = Events_Loop::loop();
293 static Events_ID EVENT_DISP = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
294 static Events_ID EVENT_UPD = aLoop->eventByName(EVENT_OBJECT_UPDATED);
295 static const ModelAPI_EventCreator* aECreator = ModelAPI_EventCreator::get();
296 // erase flag that topology is connected: the shape is new
297 if (theShapeChanged && data().get()) {
298 TDF_Label aDataLab = std::dynamic_pointer_cast<Model_Data>(data())->label();
299 if (!aDataLab.IsNull()) {
300 TDataStd_UAttribute::Set(aDataLab, kUpdateConnectedTopology);
301 isConnectedTopology(); // to store this flag in transaction, #2630
304 // iterate all sub-solids of compsolid to make sub-results synchronized with them
305 TopoDS_Shape aThisShape;
306 if (theThisShape.get()) aThisShape = theThisShape->impl<TopoDS_Shape>();
307 if (!aThisShape.IsNull() && (aThisShape.ShapeType() == TopAbs_COMPSOLID ||
308 aThisShape.ShapeType() == TopAbs_COMPOUND)) {
309 bool aWasEmpty = mySubs.empty();
310 Model_Objects* anObjects = std::dynamic_pointer_cast<Model_Document>(document())->objects();
311 unsigned int aSubIndex = 0;
312 TopoDS_Iterator aShapesIter(aThisShape);
313 for(; aShapesIter.More(); aShapesIter.Next(), aSubIndex++) {
314 std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape);
315 aShape->setImpl(new TopoDS_Shape(aShapesIter.Value()));
317 if (mySubs.size() <= aSubIndex) { // it is needed to create a new sub-result
318 std::wstring thenameshape = L"";
319 // find shape name read
320 for (std::map< std::wstring, std::shared_ptr<GeomAPI_Shape> >::iterator it =
321 myNamesShape.begin();
322 it != myNamesShape.end();
325 TopoDS_Shape curSelectedShape = (*it).second->impl<TopoDS_Shape>();
326 if (!(aShapesIter.Value().IsSame(curSelectedShape))) continue;
327 thenameshape = (*it).first;
330 aSub = anObjects->createBody(this->data(), aSubIndex,thenameshape);
332 std::map< std::wstring, std::vector<int>>::iterator itColor =
333 myColorsShape.find(thenameshape);
334 if (itColor != myColorsShape.end()){
335 ModelAPI_Tools::setColor(aSub,(*itColor).second);
337 aSub->setShapeName(myNamesShape,myColorsShape);
338 mySubs.push_back(aSub);
339 mySubsMap[aSub] = int(mySubs.size() - 1);
340 if (isConcealed()) { // for issue #2579 note7
341 aSub->ModelAPI_ResultBody::setIsConcealed(true);
342 std::dynamic_pointer_cast<Model_ResultBody>(aSub)->updateConcealment();
345 } else { // just update shape of this result
346 aSub = mySubs[aSubIndex];
348 GeomShapePtr anOldSubShape = aSub->shape();
349 if (!aShape->isEqual(anOldSubShape)) {
351 std::list<GeomShapePtr> anOldForSub;
352 computeOldForSub(aShape, myOlds, anOldForSub);
353 myIsGenerated ? aSub->storeGenerated(anOldForSub, aShape, myAlgo) :
354 aSub->storeModified(anOldForSub, aShape, myAlgo);
356 aSub->store(aShape, false);
358 aECreator->sendUpdated(aSub, EVENT_DISP);
359 aECreator->sendUpdated(aSub, EVENT_UPD);
361 aSub->setDisabled(aSub, isDisabled());
363 // erase left, unused results
364 while(mySubs.size() > aSubIndex) {
365 ResultBodyPtr anErased = *(mySubs.rbegin());
366 if (anErased->ModelAPI_ResultBody::isConcealed()) {
367 anErased->ModelAPI_ResultBody::setIsConcealed(false);
368 std::dynamic_pointer_cast<Model_ResultBody>(anErased)->updateConcealment();
370 anErased->setDisabled(anErased, true);
371 mySubsMap.erase(anErased);
374 if (aWasEmpty) { // erase all subs
375 // redisplay this because result with and without subs are displayed differently
376 aECreator->sendUpdated(data()->owner(), EVENT_DISP);
379 } else if (!mySubs.empty()) { // erase all subs
380 while(!mySubs.empty()) {
381 ResultBodyPtr anErased = *(mySubs.rbegin());
382 if (anErased->ModelAPI_ResultBody::isConcealed()) {
383 anErased->ModelAPI_ResultBody::setIsConcealed(false);
384 std::dynamic_pointer_cast<Model_ResultBody>(anErased)->updateConcealment();
386 anErased->setDisabled(anErased, true); // even if it is invalid (to erase subs on abort/undo)
390 // redisplay this because result with and without subs are displayed differently
391 aECreator->sendUpdated(data()->owner(), EVENT_DISP);
395 void Model_ResultBody::updateSubs(
396 const GeomShapePtr& theThisShape, const std::list<GeomShapePtr>& theOlds,
397 const std::shared_ptr<GeomAlgoAPI_MakeShape> theMakeShape, const bool isGenerated)
399 myAlgo = theMakeShape;
401 myIsGenerated = isGenerated;
402 // to avoid changing of "isDisabled" flag in the "updateSubs" cycle
405 updateSubs(theThisShape, true);
408 myHistoryCash.Clear();
411 void Model_ResultBody::setTextureFile(const std::string & theTextureFile)
413 ModelAPI_Result::setTextureFile(theTextureFile);
414 for( auto sub : mySubs){
415 sub->setTextureFile(theTextureFile);
417 for(auto map : mySubsMap){
418 map.first->setTextureFile(theTextureFile);
422 bool Model_ResultBody::isConnectedTopology()
424 TDF_Label aDataLab = std::dynamic_pointer_cast<Model_Data>(data())->label();
425 if (!aDataLab.IsNull()) {
426 if (aDataLab.IsAttribute(kUpdateConnectedTopology)) { // recompute state
427 aDataLab.ForgetAttribute(kUpdateConnectedTopology);
428 GeomShapePtr aShape = shape();
429 if (aShape.get() && aShape->isConnectedTopology()) {
430 TDataStd_UAttribute::Set(aDataLab, kIsConnectedTopology);
432 aDataLab.ForgetAttribute(kIsConnectedTopology);
435 return aDataLab.IsAttribute(kIsConnectedTopology);
437 return false; // invalid case
440 void Model_ResultBody::cleanCash()
442 myBuilder->cleanCash();
443 for (std::vector<ResultBodyPtr>::const_iterator aSubIter = mySubs.cbegin();
444 aSubIter != mySubs.cend(); ++aSubIter)
446 const ResultBodyPtr& aSub = *aSubIter;
451 // adds to the theSubSubs map all sub-shapes of theSub if it is compound of compsolid
452 static void collectSubs(
453 const GeomShapePtr theSub, TopTools_MapOfShape& theSubSubs, const bool theOneLevelMore)
455 if (theSub->isNull())
457 if (theSubSubs.Add(theSub->impl<TopoDS_Shape>())) {
458 bool aIsComp = theSub->isCompound() || theSub->isCompSolid();
460 for(GeomAPI_ShapeIterator anIter(theSub); anIter.more(); anIter.next())
461 collectSubs(anIter.current(), theSubSubs, theOneLevelMore);
462 } else if (theOneLevelMore) {
463 GeomAPI_Shape::ShapeType aSubType = GeomAPI_Shape::ShapeType(int(theSub->shapeType()) + 1);
464 if (aSubType == GeomAPI_Shape::SHAPE)
466 if (aSubType == GeomAPI_Shape::SHELL)
467 aSubType = GeomAPI_Shape::FACE;
468 if (aSubType == GeomAPI_Shape::WIRE)
469 aSubType = GeomAPI_Shape::EDGE;
471 for(GeomAPI_ShapeExplorer anExp(theSub, aSubType); anExp.more(); anExp.next()) {
472 collectSubs(anExp.current(), theSubSubs, false);
478 void Model_ResultBody::computeOldForSub(const GeomShapePtr& theSub,
479 const std::list<GeomShapePtr>& theAllOlds, std::list<GeomShapePtr>& theOldForSub)
481 // the old can be also used for sub-shape of theSub; collect all subs of compound or compsolid
482 TopTools_MapOfShape aSubSubs;
483 collectSubs(theSub, aSubSubs, false);
485 std::list<GeomShapePtr>::const_iterator aRootOlds = theAllOlds.cbegin();
486 for (; aRootOlds != theAllOlds.cend(); aRootOlds++) {
487 // use sub-shapes of olds too if they are compounds or compsolids
488 TopTools_MapOfShape anOldSubs;
489 // iterate one level more (for intersection of solids this is face)
490 collectSubs(*aRootOlds, anOldSubs, true);
491 for (TopTools_MapOfShape::Iterator anOldIter(anOldSubs); anOldIter.More(); anOldIter.Next()) {
492 TopoDS_Shape anOldShape = anOldIter.Value();
493 if (anOldShape.ShapeType() == TopAbs_COMPOUND || anOldShape.ShapeType() == TopAbs_SHELL ||
494 anOldShape.ShapeType() == TopAbs_WIRE || anOldShape.ShapeType() == TopAbs_COMPSOLID)
495 continue; // container old-shapes are not supported by the history, may cause crash
496 GeomShapePtr anOldSub(new GeomAPI_Shape);
497 anOldSub->setImpl<TopoDS_Shape>(new TopoDS_Shape(anOldShape));
500 if (myHistoryCash.IsBound(anOldShape)) {
501 const TopTools_ListOfShape& aList = myHistoryCash.Find(anOldShape);
502 for(TopTools_ListIteratorOfListOfShape anIter(aList); anIter.More(); anIter.Next()) {
503 GeomShapePtr aShape(new GeomAPI_Shape);
504 aShape->setImpl<TopoDS_Shape>(new TopoDS_Shape(anIter.Value()));
505 aNews.push_back(aShape);
508 myIsGenerated ? myAlgo->generated(anOldSub, aNews) : myAlgo->modified(anOldSub, aNews);
509 // MakeShape may return alone old shape if there is no history information for this input
510 if (aNews.size() == 1 && aNews.front()->isEqual(anOldSub))
512 // store result in the history
513 TopTools_ListOfShape aList;
514 for (ListOfShape::iterator aNewIter = aNews.begin(); aNewIter != aNews.end(); aNewIter++) {
515 aList.Append((*aNewIter)->impl<TopoDS_Shape>());
517 myHistoryCash.Bind(anOldShape, aList);
520 for (ListOfShape::iterator aNewIter = aNews.begin(); aNewIter != aNews.end(); aNewIter++) {
521 if (aSubSubs.Contains((*aNewIter)->impl<TopoDS_Shape>())) {
522 // check list already contains this sub
523 std::list<GeomShapePtr>::iterator aResIter = theOldForSub.begin();
524 for(; aResIter != theOldForSub.end(); aResIter++)
525 if ((*aResIter)->isSame(anOldSub))
527 if (aResIter == theOldForSub.end())
528 theOldForSub.push_back(anOldSub); // found old used for new theSubShape creation