Salome HOME
Spell-checking
[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 using namespace std;
16
17 PartSetPlugin_Part::PartSetPlugin_Part()
18 {
19 }
20
21 void PartSetPlugin_Part::initAttributes()
22 {  // all is in part result
23 }
24
25 void PartSetPlugin_Part::execute()
26 {
27   ResultPartPtr aResult = std::dynamic_pointer_cast<ModelAPI_ResultPart>(firstResult());
28   if (!aResult) {
29     aResult = document()->createPart(data());
30     setResult(aResult);
31     // do not activate part by simple execution if it is not loaded yet: it must be explicitly
32     // activated for this
33     if (!ModelAPI_Session::get()->isLoadByDemand(aResult->data()->name())) {
34       // On undo/redo creation of the part result the Object Browser must get creation event
35       // earlier that activation of this part event (otherwise the crash is produced)
36       // So, send a creation event earlier, without any grouping
37       static Events_ID aCreateID = Events_Loop::eventByName(EVENT_OBJECT_CREATED);
38       ModelAPI_EventCreator::get()->sendUpdated(aResult, aCreateID, false);
39
40       aResult->activate();
41     }
42   } else { // execute is called for result update anyway
43     aResult->updateShape();
44   }
45 }
46
47 const std::string& PartSetPlugin_Part::documentToAdd()
48 {
49   // part must be added only to the module document
50   return ModelAPI_Session::get()->moduleDocument()->kind();
51 }
52
53 std::shared_ptr<ModelAPI_Feature> PartSetPlugin_Part::addFeature(std::string theID)
54 {
55   ResultPartPtr aResult = std::dynamic_pointer_cast<ModelAPI_ResultPart>(firstResult());
56   if (aResult.get()) {
57     DocumentPtr aDoc = aResult->partDoc();
58     if (aDoc.get() && aDoc->isOpened())
59       return aDoc->addFeature(theID);
60   }
61   return FeaturePtr();
62 }
63
64 int PartSetPlugin_Part::numberOfSubs(bool forTree) const
65 {
66   ResultPartPtr aResult = std::dynamic_pointer_cast<ModelAPI_ResultPart>(firstResult());
67   if (aResult.get()) {
68     DocumentPtr aDoc = aResult->partDoc();
69     if (aDoc.get() && aDoc->isOpened())
70       return aDoc->numInternalFeatures();
71   }
72   return 0;
73 }
74
75 std::shared_ptr<ModelAPI_Feature> PartSetPlugin_Part::subFeature(const int theIndex, bool forTree)
76 {
77   ResultPartPtr aResult = std::dynamic_pointer_cast<ModelAPI_ResultPart>(firstResult());
78   if (aResult.get()) {
79     DocumentPtr aDoc = aResult->partDoc();
80     if (aDoc.get() && aDoc->isOpened()) {
81       return aDoc->internalFeature(theIndex);
82     }
83   }
84   return FeaturePtr();
85 }
86
87 int PartSetPlugin_Part::subFeatureId(const int theIndex) const
88 {
89   ResultPartPtr aResult = std::dynamic_pointer_cast<ModelAPI_ResultPart>(firstResult());
90   if (aResult.get()) {
91     DocumentPtr aDoc = aResult->partDoc();
92     if (aDoc.get() && aDoc->isOpened()) {
93       return aDoc->object(ModelAPI_Feature::group(), theIndex)->data()->featureId();
94     }
95   }
96   return 0; // none
97 }
98
99 bool PartSetPlugin_Part::isSub(ObjectPtr theObject) const
100 {
101   ResultPartPtr aResult = std::dynamic_pointer_cast<ModelAPI_ResultPart>(firstResult());
102   if (aResult.get()) {
103     DocumentPtr aDoc = aResult->partDoc();
104     return theObject->document() == aDoc;
105   }
106   return false;
107 }
108
109 void PartSetPlugin_Part::removeFeature(std::shared_ptr<ModelAPI_Feature> theFeature)
110 {
111   ResultPartPtr aResult = std::dynamic_pointer_cast<ModelAPI_ResultPart>(firstResult());
112   if (aResult.get()) {
113     DocumentPtr aDoc = aResult->partDoc();
114     if (aDoc.get() && aDoc->isOpened())
115       aDoc->removeFeature(theFeature);
116   }
117 }