Salome HOME
Update copyrights
[modules/shaper.git] / src / PartSetAPI / PartSetAPI_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 "PartSetAPI_Part.h"
21 //--------------------------------------------------------------------------------------
22 #include <ModelAPI_ResultPart.h>
23 #include <ModelAPI_Session.h>
24 #include <ModelHighAPI_Dumper.h>
25 #include <ModelHighAPI_Selection.h>
26 //--------------------------------------------------------------------------------------
27 #include <PartSetPlugin_Duplicate.h>
28 #include <PartSetPlugin_Remove.h>
29 //--------------------------------------------------------------------------------------
30 PartSetAPI_Part::PartSetAPI_Part(
31     const std::shared_ptr<ModelAPI_Feature> & theFeature)
32 : ModelHighAPI_Interface(theFeature)
33 {
34   initialize();
35 }
36
37 PartSetAPI_Part::~PartSetAPI_Part()
38 {
39 }
40
41 //--------------------------------------------------------------------------------------
42 std::shared_ptr<ModelAPI_Document> PartSetAPI_Part::document() const
43 {
44   return std::dynamic_pointer_cast<ModelAPI_ResultPart>(defaultResult())->partDoc();
45 }
46
47 void PartSetAPI_Part::dump(ModelHighAPI_Dumper& theDumper) const
48 {
49   FeaturePtr aBase = feature();
50   const std::string& aDocName = theDumper.name(aBase->document());
51
52   theDumper << aBase << " = model.addPart(" << aDocName << ")" << std::endl;
53 }
54
55 //--------------------------------------------------------------------------------------
56 PartPtr addPart(const std::shared_ptr<ModelAPI_Document> & thePart)
57 {
58   std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(PartSetAPI_Part::ID());
59   aFeature->execute();
60   return PartPtr(new PartSetAPI_Part(aFeature));
61 }
62
63 PartPtr duplicatePart(const ModelHighAPI_Interface& thePart)
64 {
65   PartPtr aResult;
66   // only the active part is duplicated, so, activate it
67   FeaturePtr aPartFeature = thePart.feature();
68   if (aPartFeature) {
69     // duplication of the current part is performed
70     DocumentPtr aPartSet = ModelAPI_Session::get()->moduleDocument();
71     aPartSet->setCurrentFeature(aPartFeature, false);
72     DocumentPtr aPartDoc =
73       std::dynamic_pointer_cast<ModelAPI_ResultPart>(aPartFeature->firstResult())->partDoc();
74     if (aPartDoc.get()) {
75       ModelAPI_Session::get()->setActiveDocument(aPartDoc);
76       FeaturePtr aDuplicate = aPartDoc->addFeature(PartSetPlugin_Duplicate::ID());
77       //aDuplicate->execute(); // it is executed at creation because it is action
78       FeaturePtr aNewPart = aPartSet->currentFeature(false); // a copy becomes a current
79       if (aNewPart.get()) {
80         aResult = PartPtr(new PartSetAPI_Part(aNewPart));
81         DocumentPtr aNewDoc = std::dynamic_pointer_cast<ModelAPI_ResultPart>(
82           aNewPart->firstResult())->partDoc();
83         ModelAPI_Session::get()->setActiveDocument(aNewDoc);
84       }
85     }
86   }
87   return aResult;
88 }
89
90 void removePart(const ModelHighAPI_Interface& thePart)
91 {
92   FeaturePtr aPartFeature = thePart.feature();
93   if (aPartFeature) {
94     // duplication of the current part is performed
95     DocumentPtr aPartSet = ModelAPI_Session::get()->moduleDocument();
96     aPartSet->setCurrentFeature(aPartFeature, false);
97     DocumentPtr aPartDoc =
98       std::dynamic_pointer_cast<ModelAPI_ResultPart>(aPartFeature->firstResult())->partDoc();
99     if (aPartDoc.get()) {
100       ModelAPI_Session::get()->setActiveDocument(aPartDoc);
101       aPartDoc->addFeature(PartSetPlugin_Remove::ID());
102       ModelAPI_Session::get()->setActiveDocument(aPartSet);
103     }
104   }
105 }