1 // Copyright (C) 2014-2023 CEA, EDF
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 <ModelAPI_AttributeImage.h>
30 #include <Model_Data.h>
31 #include <Events_Loop.h>
32 #include <GeomAPI_ShapeIterator.h>
33 #include <GeomAPI_ShapeExplorer.h>
34 #include <GeomAPI_Face.h>
35 #include <GeomAPI_Pnt.h>
37 #include <TopoDS_Shape.hxx>
38 #include <TopExp_Explorer.hxx>
39 #include <TopTools_MapOfShape.hxx>
40 #include <TDataStd_UAttribute.hxx>
42 // if this attribute exists, the shape is connected topology
43 Standard_GUID kIsConnectedTopology("e51392e0-3a4d-405d-8e36-bbfe19858ef5");
44 // if this attribute exists, the connected topology flag must be recomputed
45 Standard_GUID kUpdateConnectedTopology("01ef7a45-0bec-4266-b0b4-4aa570921818");
47 Model_ResultBody::Model_ResultBody() : ModelAPI_ResultBody()
49 myBuilder = new Model_BodyBuilder(this);
50 myLastConcealed = false;
51 updateSubs(shape()); // in case of open, etc.
54 Model_ResultBody::~Model_ResultBody()
56 updateSubs(std::shared_ptr<GeomAPI_Shape>()); // erase sub-results
60 void Model_ResultBody::initAttributes()
62 ModelAPI_Result::initAttributes();
63 // append the image attribute. It is empty, the attribute will be filled by a request
64 data()->addAttribute(IMAGE_ID(), ModelAPI_AttributeImage::typeId())->setIsArgument(false);
67 bool Model_ResultBody::generated(const GeomShapePtr& theNewShape,
68 const std::string& theName, const bool theCheckIsInResult)
71 if (mySubs.size()) { // consists of subs
72 for (std::vector<ResultBodyPtr>::const_iterator aSubIter = mySubs.cbegin();
73 aSubIter != mySubs.cend();
76 const ResultBodyPtr& aSub = *aSubIter;
77 if (aSub->generated(theNewShape, theName, theCheckIsInResult))
80 } else { // do for this directly
81 if (myBuilder->generated(theNewShape, theName, theCheckIsInResult))
87 void Model_ResultBody::loadGeneratedShapes(const std::shared_ptr<GeomAlgoAPI_MakeShape>& theAlgo,
88 const GeomShapePtr& theOldShape,
89 const GeomAPI_Shape::ShapeType theShapeTypeToExplore,
90 const std::string& theName,
91 const bool theSaveOldIfNotInTree)
93 if (mySubs.size()) { // consists of subs
94 for (std::vector<ResultBodyPtr>::const_iterator aSubIter = mySubs.cbegin();
95 aSubIter != mySubs.cend();
98 const ResultBodyPtr& aSub = *aSubIter;
99 aSub->loadGeneratedShapes(
100 theAlgo, theOldShape, theShapeTypeToExplore, theName, theSaveOldIfNotInTree);
102 } else { // do for this directly
103 myBuilder->loadGeneratedShapes(
104 theAlgo, theOldShape, theShapeTypeToExplore, theName, theSaveOldIfNotInTree);
108 void Model_ResultBody::loadModifiedShapes(const std::shared_ptr<GeomAlgoAPI_MakeShape>& theAlgo,
109 const GeomShapePtr& theOldShape,
110 const GeomAPI_Shape::ShapeType theShapeTypeToExplore,
111 const std::string& theName)
113 if (mySubs.size()) { // consists of subs
114 // optimization of getting of new shapes for specific sub-result
115 if (!theAlgo->isNewShapesCollected(theOldShape, theShapeTypeToExplore))
116 theAlgo->collectNewShapes(theOldShape, theShapeTypeToExplore);
117 std::vector<ResultBodyPtr>::const_iterator aSubIter = mySubs.cbegin();
118 for(; aSubIter != mySubs.cend(); aSubIter++) {
119 (*aSubIter)->loadModifiedShapes(theAlgo, theOldShape, theShapeTypeToExplore, theName);
121 } else { // do for this directly
122 myBuilder->loadModifiedShapes(theAlgo, theOldShape, theShapeTypeToExplore, theName);
126 void Model_ResultBody::loadFirstLevel(GeomShapePtr theShape, const std::string& theName)
128 if (mySubs.size()) { // consists of subs
129 for (std::vector<ResultBodyPtr>::const_iterator aSubIter = mySubs.cbegin();
130 aSubIter != mySubs.cend();
133 const ResultBodyPtr& aSub = *aSubIter;
134 aSub->loadFirstLevel(theShape, theName);
136 } else { // do for this directly
137 myBuilder->loadFirstLevel(theShape, theName);
141 int Model_ResultBody::numberOfSubs(bool /*forTree*/) const
143 return int(mySubs.size());
146 ResultBodyPtr Model_ResultBody::subResult(const int theIndex, bool /*forTree*/) const
148 if (theIndex >= int(mySubs.size()))
149 return ResultBodyPtr();
150 return mySubs.at(theIndex);
153 bool Model_ResultBody::isSub(ObjectPtr theResult, int& theIndex) const
155 std::map<ObjectPtr, int>::const_iterator aFound = mySubsMap.find(theResult);
156 if (aFound != mySubsMap.end()) {
157 theIndex = aFound->second;
163 void Model_ResultBody::colorConfigInfo(std::string& theSection, std::string& theName,
164 std::string& theDefault)
166 theSection = "Visualization";
167 theName = "result_body_color";
168 theDefault = DEFAULT_COLOR();
171 bool Model_ResultBody::setDisabled(std::shared_ptr<ModelAPI_Result> theThis, const bool theFlag)
173 bool aChanged = ModelAPI_ResultBody::setDisabled(theThis, theFlag);
174 if (aChanged) { // state is changed, so modifications are needed
175 updateSubs(shape(), false); // to set disabled/enabled
180 bool Model_ResultBody::isConcealed()
182 return myLastConcealed;
185 void Model_ResultBody::setIsConcealed(const bool theValue, const bool theForced)
187 if (ModelAPI_ResultBody::isConcealed() != theValue) {
188 ModelAPI_ResultBody::setIsConcealed(theValue, theForced);
193 // recursively check all subs for concealment flag, returns true if everybody have "flag" state,
194 // in theAll returns results with "flag" state
195 static bool checkAllSubs(ResultBodyPtr theParent, bool theFlag, std::list<ResultBodyPtr>& theAll)
197 if (theParent->isConcealed() != theFlag)
198 theAll.push_back(theParent);
199 bool aResult = theParent->ModelAPI_ResultBody::isConcealed() == theFlag;
200 for(int a = 0; a < theParent->numberOfSubs(); a++) {
201 bool aSubRes = checkAllSubs(theParent->subResult(a), theFlag, theAll);
203 aResult = aResult || aSubRes; // concealed: one makes concealed everyone
205 aResult = aResult && aSubRes; // not concealed: all must be not concealed
210 void Model_ResultBody::updateConcealment()
212 if (myLastConcealed != ModelAPI_ResultBody::isConcealed()) {
213 // check the whole tree of results: if one is concealed, everybody are concealed
214 ResultBodyPtr anOwner = std::dynamic_pointer_cast<ModelAPI_ResultBody>(data()->owner());
216 return; // "this" is invalid
217 ResultBodyPtr aParent = ModelAPI_Tools::bodyOwner(anOwner);
218 while(aParent.get()) {
220 aParent = ModelAPI_Tools::bodyOwner(anOwner);
222 // iterate all results and collect all results whose state may be updated
223 std::list<ResultBodyPtr> anUpdated;
224 bool aNewFlag = !myLastConcealed;
225 if (checkAllSubs(anOwner, aNewFlag, anUpdated)) { // state of everyone must be updated
226 std::list<ResultBodyPtr>::iterator aRes = anUpdated.begin();
227 for(; aRes != anUpdated.end(); aRes++) {
228 bool aLastConcealed = (*aRes)->isConcealed();
229 if (aNewFlag != aLastConcealed) {
230 std::dynamic_pointer_cast<Model_ResultBody>(*aRes)->myLastConcealed = aNewFlag;
231 if (aNewFlag) { // become concealed, behaves like removed
232 ModelAPI_EventCreator::get()->sendDeleted(document(), groupName());
233 } else { // become not-concealed, behaves like created
234 static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_CREATED);
235 ModelAPI_EventCreator::get()->sendUpdated(*aRes, anEvent);
237 static Events_ID EVENT_DISP = // must be redisplayed in any case
238 Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
239 ModelAPI_EventCreator::get()->sendUpdated(*aRes, EVENT_DISP);
246 void Model_ResultBody::addShapeColor( const std::wstring& theName,std::vector<int>& color) {
248 if (myColorsShape.find(theName) == myColorsShape.end())
249 myColorsShape[ theName ] = color;
252 std::wstring Model_ResultBody::addShapeName(std::shared_ptr<GeomAPI_Shape> theshape,
253 const std::wstring& theName ){
256 std::wstringstream aName;
258 while(myNamesShape.find(aName.str()) != myNamesShape.end() ){
260 aName << theName << L"__" << indice;
263 myNamesShape[ aName.str() ] = theshape;
268 std::wstring Model_ResultBody::findShapeName(std::shared_ptr<GeomAPI_Shape> theShape){
270 TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
271 for (std::map< std::wstring, std::shared_ptr<GeomAPI_Shape> >::iterator it =
272 myNamesShape.begin();
273 it != myNamesShape.end();
276 TopoDS_Shape curSelectedShape = (*it).second->impl<TopoDS_Shape>();
277 if ((aShape.IsSame(curSelectedShape))) {
281 return L"material not found" ;
284 const std::vector<int>& Model_ResultBody::findShapeColor(const std::wstring& theShapeName)
286 std::map<std::wstring, std::vector<int> >::iterator aColor = myColorsShape.find(theShapeName);
287 if (aColor != myColorsShape.end())
288 return aColor->second;
289 static std::vector<int> anEmptyVector;
290 return anEmptyVector;
293 void Model_ResultBody::setShapeName(
294 std::map< std::wstring, std::shared_ptr<GeomAPI_Shape>>& theShapeName,
295 std::map< std::wstring, std::vector<int>>& theColorsShape)
297 myNamesShape = theShapeName;
298 myColorsShape = theColorsShape;
301 void Model_ResultBody::clearShapeNameAndColor(){
302 myNamesShape.clear();
303 myColorsShape.clear();
306 void Model_ResultBody::updateSubs(const std::shared_ptr<GeomAPI_Shape>& theThisShape,
307 const bool theShapeChanged)
309 static Events_Loop* aLoop = Events_Loop::loop();
310 static Events_ID EVENT_DISP = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
311 static Events_ID EVENT_UPD = aLoop->eventByName(EVENT_OBJECT_UPDATED);
312 static const ModelAPI_EventCreator* aECreator = ModelAPI_EventCreator::get();
313 // erase flag that topology is connected: the shape is new
314 if (theShapeChanged && data().get()) {
315 TDF_Label aDataLab = std::dynamic_pointer_cast<Model_Data>(data())->label();
316 if (!aDataLab.IsNull()) {
317 TDataStd_UAttribute::Set(aDataLab, kUpdateConnectedTopology);
318 isConnectedTopology(); // to store this flag in transaction, #2630
321 // iterate all sub-solids of compsolid to make sub-results synchronized with them
322 TopoDS_Shape aThisShape;
323 if (theThisShape.get()) aThisShape = theThisShape->impl<TopoDS_Shape>();
324 if (!aThisShape.IsNull() && (aThisShape.ShapeType() == TopAbs_COMPSOLID ||
325 aThisShape.ShapeType() == TopAbs_COMPOUND)) {
326 bool aWasEmpty = mySubs.empty();
327 Model_Objects* anObjects = std::dynamic_pointer_cast<Model_Document>(document())->objects();
328 unsigned int aSubIndex = 0;
329 TopoDS_Iterator aShapesIter(aThisShape);
330 for(; aShapesIter.More(); aShapesIter.Next(), aSubIndex++) {
331 std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape);
332 aShape->setImpl(new TopoDS_Shape(aShapesIter.Value()));
334 if (mySubs.size() <= aSubIndex) { // it is needed to create a new sub-result
335 std::wstring thenameshape = L"";
336 // find shape name read
337 for (std::map< std::wstring, std::shared_ptr<GeomAPI_Shape> >::iterator it =
338 myNamesShape.begin();
339 it != myNamesShape.end();
342 TopoDS_Shape curSelectedShape = (*it).second->impl<TopoDS_Shape>();
343 if (!(aShapesIter.Value().IsSame(curSelectedShape))) continue;
344 thenameshape = (*it).first;
347 aSub = anObjects->createBody(this->data(), aSubIndex,thenameshape);
349 std::map< std::wstring, std::vector<int>>::iterator itColor =
350 myColorsShape.find(thenameshape);
351 if (itColor != myColorsShape.end()){
352 ModelAPI_Tools::setColor(aSub,(*itColor).second);
354 aSub->setShapeName(myNamesShape,myColorsShape);
355 mySubs.push_back(aSub);
356 mySubsMap[aSub] = int(mySubs.size() - 1);
357 if (isConcealed()) { // for issue #2579 note7
358 aSub->ModelAPI_ResultBody::setIsConcealed(true);
359 std::dynamic_pointer_cast<Model_ResultBody>(aSub)->updateConcealment();
362 } else { // just update shape of this result
363 aSub = mySubs[aSubIndex];
365 GeomShapePtr anOldSubShape = aSub->shape();
366 if (!aShape->isEqual(anOldSubShape)) {
368 std::list<GeomShapePtr> anOldForSub;
369 computeOldForSub(aShape, myOlds, anOldForSub);
370 myIsGenerated ? aSub->storeGenerated(anOldForSub, aShape, myAlgo) :
371 aSub->storeModified(anOldForSub, aShape, myAlgo);
373 aSub->store(aShape, false);
375 aECreator->sendUpdated(aSub, EVENT_DISP);
376 aECreator->sendUpdated(aSub, EVENT_UPD);
378 aSub->setDisabled(aSub, isDisabled());
380 // erase left, unused results
381 while(mySubs.size() > aSubIndex) {
382 ResultBodyPtr anErased = *(mySubs.rbegin());
383 if (anErased->ModelAPI_ResultBody::isConcealed()) {
384 anErased->ModelAPI_ResultBody::setIsConcealed(false);
385 std::dynamic_pointer_cast<Model_ResultBody>(anErased)->updateConcealment();
387 anErased->setDisabled(anErased, true);
388 mySubsMap.erase(anErased);
391 if (aWasEmpty) { // erase all subs
392 // redisplay this because result with and without subs are displayed differently
393 aECreator->sendUpdated(data()->owner(), EVENT_DISP);
396 } else if (!mySubs.empty()) { // erase all subs
397 while(!mySubs.empty()) {
398 ResultBodyPtr anErased = *(mySubs.rbegin());
399 if (anErased->ModelAPI_ResultBody::isConcealed()) {
400 anErased->ModelAPI_ResultBody::setIsConcealed(false);
401 std::dynamic_pointer_cast<Model_ResultBody>(anErased)->updateConcealment();
403 anErased->setDisabled(anErased, true); // even if it is invalid (to erase subs on abort/undo)
407 // redisplay this because result with and without subs are displayed differently
408 aECreator->sendUpdated(data()->owner(), EVENT_DISP);
412 void Model_ResultBody::updateSubs(
413 const GeomShapePtr& theThisShape, const std::list<GeomShapePtr>& theOlds,
414 const std::shared_ptr<GeomAlgoAPI_MakeShape> theMakeShape, const bool isGenerated)
416 myAlgo = theMakeShape;
418 myIsGenerated = isGenerated;
419 // to avoid changing of "isDisabled" flag in the "updateSubs" cycle
422 updateSubs(theThisShape, true);
425 myHistoryCash.Clear();
428 bool Model_ResultBody::isConnectedTopology()
430 TDF_Label aDataLab = std::dynamic_pointer_cast<Model_Data>(data())->label();
431 if (!aDataLab.IsNull()) {
432 if (aDataLab.IsAttribute(kUpdateConnectedTopology)) { // recompute state
433 aDataLab.ForgetAttribute(kUpdateConnectedTopology);
434 GeomShapePtr aShape = shape();
435 if (aShape.get() && aShape->isConnectedTopology()) {
436 TDataStd_UAttribute::Set(aDataLab, kIsConnectedTopology);
438 aDataLab.ForgetAttribute(kIsConnectedTopology);
441 return aDataLab.IsAttribute(kIsConnectedTopology);
443 return false; // invalid case
446 void Model_ResultBody::cleanCash()
448 myBuilder->cleanCash();
449 for (std::vector<ResultBodyPtr>::const_iterator aSubIter = mySubs.cbegin();
450 aSubIter != mySubs.cend(); ++aSubIter)
452 const ResultBodyPtr& aSub = *aSubIter;
457 bool Model_ResultBody::hasTexture()
459 AttributeImagePtr anImageAttr =
460 data()->image(ModelAPI_ResultBody::IMAGE_ID());
461 if (anImageAttr.get()) {
462 return anImageAttr->hasTexture();
467 // adds to the theSubSubs map all sub-shapes of theSub if it is compound of compsolid
468 static void collectSubs(
469 const GeomShapePtr theSub, TopTools_MapOfShape& theSubSubs, const bool theOneLevelMore)
471 if (theSub->isNull())
473 if (theSubSubs.Add(theSub->impl<TopoDS_Shape>())) {
474 bool aIsComp = theSub->isCompound() || theSub->isCompSolid();
476 for(GeomAPI_ShapeIterator anIter(theSub); anIter.more(); anIter.next())
477 collectSubs(anIter.current(), theSubSubs, theOneLevelMore);
478 } else if (theOneLevelMore) {
479 GeomAPI_Shape::ShapeType aSubType = GeomAPI_Shape::ShapeType(int(theSub->shapeType()) + 1);
480 if (aSubType == GeomAPI_Shape::SHAPE)
482 if (aSubType == GeomAPI_Shape::SHELL)
483 aSubType = GeomAPI_Shape::FACE;
484 if (aSubType == GeomAPI_Shape::WIRE)
485 aSubType = GeomAPI_Shape::EDGE;
487 for(GeomAPI_ShapeExplorer anExp(theSub, aSubType); anExp.more(); anExp.next()) {
488 collectSubs(anExp.current(), theSubSubs, false);
494 void Model_ResultBody::computeOldForSub(const GeomShapePtr& theSub,
495 const std::list<GeomShapePtr>& theAllOlds, std::list<GeomShapePtr>& theOldForSub)
497 // the old can be also used for sub-shape of theSub; collect all subs of compound or compsolid
498 TopTools_MapOfShape aSubSubs;
499 collectSubs(theSub, aSubSubs, false);
501 std::list<GeomShapePtr>::const_iterator aRootOlds = theAllOlds.cbegin();
502 for (; aRootOlds != theAllOlds.cend(); aRootOlds++) {
503 // use sub-shapes of olds too if they are compounds or compsolids
504 TopTools_MapOfShape anOldSubs;
505 // iterate one level more (for intersection of solids this is face)
506 collectSubs(*aRootOlds, anOldSubs, true);
507 for (TopTools_MapOfShape::Iterator anOldIter(anOldSubs); anOldIter.More(); anOldIter.Next()) {
508 TopoDS_Shape anOldShape = anOldIter.Value();
509 if (anOldShape.ShapeType() == TopAbs_COMPOUND || anOldShape.ShapeType() == TopAbs_SHELL ||
510 anOldShape.ShapeType() == TopAbs_WIRE || anOldShape.ShapeType() == TopAbs_COMPSOLID)
511 continue; // container old-shapes are not supported by the history, may cause crash
512 GeomShapePtr anOldSub(new GeomAPI_Shape);
513 anOldSub->setImpl<TopoDS_Shape>(new TopoDS_Shape(anOldShape));
516 if (myHistoryCash.IsBound(anOldShape)) {
517 const TopTools_ListOfShape& aList = myHistoryCash.Find(anOldShape);
518 for(TopTools_ListIteratorOfListOfShape anIter(aList); anIter.More(); anIter.Next()) {
519 GeomShapePtr aShape(new GeomAPI_Shape);
520 aShape->setImpl<TopoDS_Shape>(new TopoDS_Shape(anIter.Value()));
521 aNews.push_back(aShape);
524 myIsGenerated ? myAlgo->generated(anOldSub, aNews) : myAlgo->modified(anOldSub, aNews);
525 // MakeShape may return alone old shape if there is no history information for this input
526 if (aNews.size() == 1 && aNews.front()->isEqual(anOldSub))
528 // store result in the history
529 TopTools_ListOfShape aList;
530 for (ListOfShape::iterator aNewIter = aNews.begin(); aNewIter != aNews.end(); aNewIter++) {
531 aList.Append((*aNewIter)->impl<TopoDS_Shape>());
533 myHistoryCash.Bind(anOldShape, aList);
536 for (ListOfShape::iterator aNewIter = aNews.begin(); aNewIter != aNews.end(); aNewIter++) {
537 if (aSubSubs.Contains((*aNewIter)->impl<TopoDS_Shape>())) {
538 // check list already contains this sub
539 std::list<GeomShapePtr>::iterator aResIter = theOldForSub.begin();
540 for(; aResIter != theOldForSub.end(); aResIter++)
541 if ((*aResIter)->isSame(anOldSub))
543 if (aResIter == theOldForSub.end())
544 theOldForSub.push_back(anOldSub); // found old used for new theSubShape creation