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