1 // Copyright (C) 2014-2021 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 <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 void Model_ResultBody::setShapeName(
285 std::map< std::wstring, std::shared_ptr<GeomAPI_Shape>>& theShapeName,
286 std::map< std::wstring, std::vector<int>>& theColorsShape)
288 myNamesShape = theShapeName;
289 myColorsShape = theColorsShape;
292 void Model_ResultBody::clearShapeNameAndColor(){
293 myNamesShape.clear();
294 myColorsShape.clear();
297 void Model_ResultBody::updateSubs(const std::shared_ptr<GeomAPI_Shape>& theThisShape,
298 const bool theShapeChanged)
300 static Events_Loop* aLoop = Events_Loop::loop();
301 static Events_ID EVENT_DISP = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
302 static Events_ID EVENT_UPD = aLoop->eventByName(EVENT_OBJECT_UPDATED);
303 static const ModelAPI_EventCreator* aECreator = ModelAPI_EventCreator::get();
304 // erase flag that topology is connected: the shape is new
305 if (theShapeChanged && data().get()) {
306 TDF_Label aDataLab = std::dynamic_pointer_cast<Model_Data>(data())->label();
307 if (!aDataLab.IsNull()) {
308 TDataStd_UAttribute::Set(aDataLab, kUpdateConnectedTopology);
309 isConnectedTopology(); // to store this flag in transaction, #2630
312 // iterate all sub-solids of compsolid to make sub-results synchronized with them
313 TopoDS_Shape aThisShape;
314 if (theThisShape.get()) aThisShape = theThisShape->impl<TopoDS_Shape>();
315 if (!aThisShape.IsNull() && (aThisShape.ShapeType() == TopAbs_COMPSOLID ||
316 aThisShape.ShapeType() == TopAbs_COMPOUND)) {
317 bool aWasEmpty = mySubs.empty();
318 Model_Objects* anObjects = std::dynamic_pointer_cast<Model_Document>(document())->objects();
319 unsigned int aSubIndex = 0;
320 TopoDS_Iterator aShapesIter(aThisShape);
321 for(; aShapesIter.More(); aShapesIter.Next(), aSubIndex++) {
322 std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape);
323 aShape->setImpl(new TopoDS_Shape(aShapesIter.Value()));
325 if (mySubs.size() <= aSubIndex) { // it is needed to create a new sub-result
326 std::wstring thenameshape = L"";
327 // find shape name read
328 for (std::map< std::wstring, std::shared_ptr<GeomAPI_Shape> >::iterator it =
329 myNamesShape.begin();
330 it != myNamesShape.end();
333 TopoDS_Shape curSelectedShape = (*it).second->impl<TopoDS_Shape>();
334 if (!(aShapesIter.Value().IsSame(curSelectedShape))) continue;
335 thenameshape = (*it).first;
338 aSub = anObjects->createBody(this->data(), aSubIndex,thenameshape);
340 std::map< std::wstring, std::vector<int>>::iterator itColor =
341 myColorsShape.find(thenameshape);
342 if (itColor != myColorsShape.end()){
343 ModelAPI_Tools::setColor(aSub,(*itColor).second);
345 aSub->setShapeName(myNamesShape,myColorsShape);
346 mySubs.push_back(aSub);
347 mySubsMap[aSub] = int(mySubs.size() - 1);
348 if (isConcealed()) { // for issue #2579 note7
349 aSub->ModelAPI_ResultBody::setIsConcealed(true);
350 std::dynamic_pointer_cast<Model_ResultBody>(aSub)->updateConcealment();
353 } else { // just update shape of this result
354 aSub = mySubs[aSubIndex];
356 GeomShapePtr anOldSubShape = aSub->shape();
357 if (!aShape->isEqual(anOldSubShape)) {
359 std::list<GeomShapePtr> anOldForSub;
360 computeOldForSub(aShape, myOlds, anOldForSub);
361 myIsGenerated ? aSub->storeGenerated(anOldForSub, aShape, myAlgo) :
362 aSub->storeModified(anOldForSub, aShape, myAlgo);
364 aSub->store(aShape, false);
366 aECreator->sendUpdated(aSub, EVENT_DISP);
367 aECreator->sendUpdated(aSub, EVENT_UPD);
369 aSub->setDisabled(aSub, isDisabled());
371 // erase left, unused results
372 while(mySubs.size() > aSubIndex) {
373 ResultBodyPtr anErased = *(mySubs.rbegin());
374 if (anErased->ModelAPI_ResultBody::isConcealed()) {
375 anErased->ModelAPI_ResultBody::setIsConcealed(false);
376 std::dynamic_pointer_cast<Model_ResultBody>(anErased)->updateConcealment();
378 anErased->setDisabled(anErased, true);
379 mySubsMap.erase(anErased);
382 if (aWasEmpty) { // erase all subs
383 // redisplay this because result with and without subs are displayed differently
384 aECreator->sendUpdated(data()->owner(), EVENT_DISP);
387 } else if (!mySubs.empty()) { // erase all subs
388 while(!mySubs.empty()) {
389 ResultBodyPtr anErased = *(mySubs.rbegin());
390 if (anErased->ModelAPI_ResultBody::isConcealed()) {
391 anErased->ModelAPI_ResultBody::setIsConcealed(false);
392 std::dynamic_pointer_cast<Model_ResultBody>(anErased)->updateConcealment();
394 anErased->setDisabled(anErased, true); // even if it is invalid (to erase subs on abort/undo)
398 // redisplay this because result with and without subs are displayed differently
399 aECreator->sendUpdated(data()->owner(), EVENT_DISP);
403 void Model_ResultBody::updateSubs(
404 const GeomShapePtr& theThisShape, const std::list<GeomShapePtr>& theOlds,
405 const std::shared_ptr<GeomAlgoAPI_MakeShape> theMakeShape, const bool isGenerated)
407 myAlgo = theMakeShape;
409 myIsGenerated = isGenerated;
410 // to avoid changing of "isDisabled" flag in the "updateSubs" cycle
413 updateSubs(theThisShape, true);
416 myHistoryCash.Clear();
419 bool Model_ResultBody::isConnectedTopology()
421 TDF_Label aDataLab = std::dynamic_pointer_cast<Model_Data>(data())->label();
422 if (!aDataLab.IsNull()) {
423 if (aDataLab.IsAttribute(kUpdateConnectedTopology)) { // recompute state
424 aDataLab.ForgetAttribute(kUpdateConnectedTopology);
425 GeomShapePtr aShape = shape();
426 if (aShape.get() && aShape->isConnectedTopology()) {
427 TDataStd_UAttribute::Set(aDataLab, kIsConnectedTopology);
429 aDataLab.ForgetAttribute(kIsConnectedTopology);
432 return aDataLab.IsAttribute(kIsConnectedTopology);
434 return false; // invalid case
437 void Model_ResultBody::cleanCash()
439 myBuilder->cleanCash();
440 for (std::vector<ResultBodyPtr>::const_iterator aSubIter = mySubs.cbegin();
441 aSubIter != mySubs.cend(); ++aSubIter)
443 const ResultBodyPtr& aSub = *aSubIter;
448 bool Model_ResultBody::hasTexture()
450 AttributeImagePtr anImageAttr =
451 data()->image(ModelAPI_ResultBody::IMAGE_ID());
452 if (anImageAttr.get()) {
453 return anImageAttr->hasTexture();
458 // adds to the theSubSubs map all sub-shapes of theSub if it is compound of compsolid
459 static void collectSubs(
460 const GeomShapePtr theSub, TopTools_MapOfShape& theSubSubs, const bool theOneLevelMore)
462 if (theSub->isNull())
464 if (theSubSubs.Add(theSub->impl<TopoDS_Shape>())) {
465 bool aIsComp = theSub->isCompound() || theSub->isCompSolid();
467 for(GeomAPI_ShapeIterator anIter(theSub); anIter.more(); anIter.next())
468 collectSubs(anIter.current(), theSubSubs, theOneLevelMore);
469 } else if (theOneLevelMore) {
470 GeomAPI_Shape::ShapeType aSubType = GeomAPI_Shape::ShapeType(int(theSub->shapeType()) + 1);
471 if (aSubType == GeomAPI_Shape::SHAPE)
473 if (aSubType == GeomAPI_Shape::SHELL)
474 aSubType = GeomAPI_Shape::FACE;
475 if (aSubType == GeomAPI_Shape::WIRE)
476 aSubType = GeomAPI_Shape::EDGE;
478 for(GeomAPI_ShapeExplorer anExp(theSub, aSubType); anExp.more(); anExp.next()) {
479 collectSubs(anExp.current(), theSubSubs, false);
485 void Model_ResultBody::computeOldForSub(const GeomShapePtr& theSub,
486 const std::list<GeomShapePtr>& theAllOlds, std::list<GeomShapePtr>& theOldForSub)
488 // the old can be also used for sub-shape of theSub; collect all subs of compound or compsolid
489 TopTools_MapOfShape aSubSubs;
490 collectSubs(theSub, aSubSubs, false);
492 std::list<GeomShapePtr>::const_iterator aRootOlds = theAllOlds.cbegin();
493 for (; aRootOlds != theAllOlds.cend(); aRootOlds++) {
494 // use sub-shapes of olds too if they are compounds or compsolids
495 TopTools_MapOfShape anOldSubs;
496 // iterate one level more (for intersection of solids this is face)
497 collectSubs(*aRootOlds, anOldSubs, true);
498 for (TopTools_MapOfShape::Iterator anOldIter(anOldSubs); anOldIter.More(); anOldIter.Next()) {
499 TopoDS_Shape anOldShape = anOldIter.Value();
500 if (anOldShape.ShapeType() == TopAbs_COMPOUND || anOldShape.ShapeType() == TopAbs_SHELL ||
501 anOldShape.ShapeType() == TopAbs_WIRE || anOldShape.ShapeType() == TopAbs_COMPSOLID)
502 continue; // container old-shapes are not supported by the history, may cause crash
503 GeomShapePtr anOldSub(new GeomAPI_Shape);
504 anOldSub->setImpl<TopoDS_Shape>(new TopoDS_Shape(anOldShape));
507 if (myHistoryCash.IsBound(anOldShape)) {
508 const TopTools_ListOfShape& aList = myHistoryCash.Find(anOldShape);
509 for(TopTools_ListIteratorOfListOfShape anIter(aList); anIter.More(); anIter.Next()) {
510 GeomShapePtr aShape(new GeomAPI_Shape);
511 aShape->setImpl<TopoDS_Shape>(new TopoDS_Shape(anIter.Value()));
512 aNews.push_back(aShape);
515 myIsGenerated ? myAlgo->generated(anOldSub, aNews) : myAlgo->modified(anOldSub, aNews);
516 // MakeShape may return alone old shape if there is no history information for this input
517 if (aNews.size() == 1 && aNews.front()->isEqual(anOldSub))
519 // store result in the history
520 TopTools_ListOfShape aList;
521 for (ListOfShape::iterator aNewIter = aNews.begin(); aNewIter != aNews.end(); aNewIter++) {
522 aList.Append((*aNewIter)->impl<TopoDS_Shape>());
524 myHistoryCash.Bind(anOldShape, aList);
527 for (ListOfShape::iterator aNewIter = aNews.begin(); aNewIter != aNews.end(); aNewIter++) {
528 if (aSubSubs.Contains((*aNewIter)->impl<TopoDS_Shape>())) {
529 // check list already contains this sub
530 std::list<GeomShapePtr>::iterator aResIter = theOldForSub.begin();
531 for(; aResIter != theOldForSub.end(); aResIter++)
532 if ((*aResIter)->isSame(anOldSub))
534 if (aResIter == theOldForSub.end())
535 theOldForSub.push_back(anOldSub); // found old used for new theSubShape creation