Salome HOME
Fixes for issue #1956 and issue #2104 : correctly remove features on part remove.
[modules/shaper.git] / src / PartSetPlugin / PartSetPlugin_Part.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        PartSetPlugin_Part.cxx
4 // Created:     27 Mar 2014
5 // Author:      Mikhail PONIKAROV
6
7 #include "PartSetPlugin_Part.h"
8 #include "ModelAPI_Session.h"
9 #include "ModelAPI_Document.h"
10 #include "ModelAPI_Data.h"
11 #include "ModelAPI_AttributeDocRef.h"
12 #include <ModelAPI_ResultPart.h>
13 #include <ModelAPI_Events.h>
14
15 PartSetPlugin_Part::PartSetPlugin_Part()
16 {
17 }
18
19 void PartSetPlugin_Part::initAttributes()
20 {  // all is in part result
21 }
22
23 void PartSetPlugin_Part::execute()
24 {
25   ResultPartPtr aResult = std::dynamic_pointer_cast<ModelAPI_ResultPart>(firstResult());
26   if (!aResult) {
27     aResult = document()->createPart(data());
28     setResult(aResult);
29     // do not activate part by simple execution if it is not loaded yet: it must be explicitly
30     // activated for this
31     if (!ModelAPI_Session::get()->isLoadByDemand(aResult->data()->name())) {
32       // On undo/redo creation of the part result the Object Browser must get creation event
33       // earlier that activation of this part event (otherwise the crash is produced)
34       // So, send a creation event earlier, without any grouping
35       static Events_ID aCreateID = Events_Loop::eventByName(EVENT_OBJECT_CREATED);
36       ModelAPI_EventCreator::get()->sendUpdated(aResult, aCreateID, false);
37
38       aResult->activate();
39     }
40   } else { // execute is called for result update anyway
41     aResult->updateShape();
42   }
43 }
44
45 const std::string& PartSetPlugin_Part::documentToAdd()
46 {
47   // part must be added only to the module document
48   return ModelAPI_Session::get()->moduleDocument()->kind();
49 }
50
51 std::shared_ptr<ModelAPI_Feature> PartSetPlugin_Part::addFeature(std::string theID)
52 {
53   ResultPartPtr aResult = std::dynamic_pointer_cast<ModelAPI_ResultPart>(firstResult());
54   if (aResult.get()) {
55     DocumentPtr aDoc = aResult->partDoc();
56     if (aDoc.get() && aDoc->isOpened())
57       return aDoc->addFeature(theID);
58   }
59   return FeaturePtr();
60 }
61
62 int PartSetPlugin_Part::numberOfSubs(bool forTree) const
63 {
64   ResultPartPtr aResult = std::dynamic_pointer_cast<ModelAPI_ResultPart>(firstResult());
65   if (aResult.get()) {
66     DocumentPtr aDoc = aResult->partDoc();
67     if (aDoc.get() && aDoc->isOpened())
68       return aDoc->numInternalFeatures();
69   }
70   return 0;
71 }
72
73 std::shared_ptr<ModelAPI_Feature> PartSetPlugin_Part::subFeature(const int theIndex, bool forTree)
74 {
75   ResultPartPtr aResult = std::dynamic_pointer_cast<ModelAPI_ResultPart>(firstResult());
76   if (aResult.get()) {
77     DocumentPtr aDoc = aResult->partDoc();
78     if (aDoc.get() && aDoc->isOpened()) {
79       return aDoc->internalFeature(theIndex);
80     }
81   }
82   return FeaturePtr();
83 }
84
85 int PartSetPlugin_Part::subFeatureId(const int theIndex) const
86 {
87   ResultPartPtr aResult = std::dynamic_pointer_cast<ModelAPI_ResultPart>(firstResult());
88   if (aResult.get()) {
89     DocumentPtr aDoc = aResult->partDoc();
90     if (aDoc.get() && aDoc->isOpened()) {
91       return aDoc->object(ModelAPI_Feature::group(), theIndex)->data()->featureId();
92     }
93   }
94   return 0; // none
95 }
96
97 bool PartSetPlugin_Part::isSub(ObjectPtr theObject) const
98 {
99   ResultPartPtr aResult = std::dynamic_pointer_cast<ModelAPI_ResultPart>(firstResult());
100   if (aResult.get()) {
101     DocumentPtr aDoc = aResult->partDoc();
102     return theObject->document() == aDoc;
103   }
104   return false;
105 }
106
107 void PartSetPlugin_Part::removeFeature(std::shared_ptr<ModelAPI_Feature> theFeature)
108 {
109 }
110
111 void PartSetPlugin_Part::erase() {
112   ResultPartPtr aResult = std::dynamic_pointer_cast<ModelAPI_ResultPart>(firstResult());
113   if (aResult.get()) {
114     DocumentPtr aDoc = aResult->partDoc();
115     if (aDoc.get())
116       aDoc->eraseAllFeatures();
117   }
118   ModelAPI_Feature::erase();
119 }