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