Salome HOME
updated copyright message
[modules/shaper.git] / src / PartSetPlugin / PartSetPlugin_Remove.cpp
1 // Copyright (C) 2014-2023  CEA, EDF
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "PartSetPlugin_Remove.h"
21 #include "PartSetPlugin_Part.h"
22
23 #include <ModelAPI_Document.h>
24 #include <ModelAPI_Data.h>
25 #include <ModelAPI_AttributeDocRef.h>
26 #include <ModelAPI_ResultPart.h>
27 #include <ModelAPI_Session.h>
28 #include <ModelAPI_Feature.h>
29 #include <ModelAPI_Tools.h>
30 #include <ModelAPI_Events.h>
31
32 #include <Events_Loop.h>
33
34 void PartSetPlugin_Remove::execute()
35 {
36   std::shared_ptr<ModelAPI_Session> aPManager = ModelAPI_Session::get();
37   std::shared_ptr<ModelAPI_Document> aRoot = aPManager->moduleDocument();
38   DocumentPtr aThisDoc = document();
39   ResultPtr aPart = ModelAPI_Tools::findPartResult(aRoot, aThisDoc);
40   if (aPart.get()) {
41     FeaturePtr aFeature = aRoot->feature(aPart);
42     if (aFeature) {
43       // do remove, but don't do real close (features are erased without persistence changes
44       // document remove may be undoed)
45       // aPart->data()->document(ModelAPI_ResultPart::DOC_REF())->value()->close();
46       std::set<std::shared_ptr<ModelAPI_Feature> > aRefFeatures;
47       aRoot->refsToFeature(aFeature, aRefFeatures);
48       if (aRefFeatures.empty()) {
49         // before removing the part make the partSet active document
50         if (aThisDoc == aPManager->activeDocument())
51           aPManager->setActiveDocument(aRoot);
52         aRoot->removeFeature(aFeature);
53         // the redisplay signal should be flushed in order to erase the feature presentation
54         // in the viewer after removeFeature from the document
55         Events_Loop::loop()->flush(Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY));
56       }
57     }
58   }
59 }