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