Salome HOME
Fixed crash when attribute External in feature Projection changed.
[modules/shaper.git] / src / PartSetAPI / PartSetAPI_Part.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2 // Name   : PartSetAPI_Part.cpp
3 // Purpose:
4 //
5 // History:
6 // 16/06/16 - Sergey POKHODENKO - Creation of the file
7
8 //--------------------------------------------------------------------------------------
9 #include "PartSetAPI_Part.h"
10 //--------------------------------------------------------------------------------------
11 #include <ModelAPI_ResultPart.h>
12 #include <ModelAPI_Session.h>
13 #include <ModelHighAPI_Dumper.h>
14 #include <ModelHighAPI_Selection.h>
15 //--------------------------------------------------------------------------------------
16 #include <PartSetPlugin_Duplicate.h>
17 #include <PartSetPlugin_Remove.h>
18 //--------------------------------------------------------------------------------------
19 PartSetAPI_Part::PartSetAPI_Part(
20     const std::shared_ptr<ModelAPI_Feature> & theFeature)
21 : ModelHighAPI_Interface(theFeature)
22 {
23   initialize();
24 }
25
26 PartSetAPI_Part::~PartSetAPI_Part()
27 {
28 }
29
30 //--------------------------------------------------------------------------------------
31 std::shared_ptr<ModelAPI_Document> PartSetAPI_Part::document() const
32 {
33   return std::dynamic_pointer_cast<ModelAPI_ResultPart>(defaultResult())->partDoc();
34 }
35
36 void PartSetAPI_Part::dump(ModelHighAPI_Dumper& theDumper) const
37 {
38   FeaturePtr aBase = feature();
39   const std::string& aDocName = theDumper.name(aBase->document());
40
41   theDumper << aBase << " = model.addPart(" << aDocName << ")" << std::endl;
42 }
43
44 //--------------------------------------------------------------------------------------
45 PartPtr addPart(const std::shared_ptr<ModelAPI_Document> & thePart)
46 {
47   std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(PartSetAPI_Part::ID());
48   aFeature->execute();
49   return PartPtr(new PartSetAPI_Part(aFeature));
50 }
51
52 PartPtr duplicatePart(const ModelHighAPI_Interface& thePart)
53 {
54   PartPtr aResult;
55   // only the active part is duplicated, so, activate it
56   FeaturePtr aPartFeature = thePart.feature();
57   if (aPartFeature) {
58     // duplication of the current part is performed
59     DocumentPtr aPartSet = ModelAPI_Session::get()->moduleDocument();
60     aPartSet->setCurrentFeature(aPartFeature, false);
61     DocumentPtr aPartDoc =
62       std::dynamic_pointer_cast<ModelAPI_ResultPart>(aPartFeature->firstResult())->partDoc();
63     if (aPartDoc.get()) {
64       ModelAPI_Session::get()->setActiveDocument(aPartDoc);
65       FeaturePtr aDuplicate = aPartDoc->addFeature(PartSetPlugin_Duplicate::ID());
66       //aDuplicate->execute(); // it is executed at creation because it is action
67       FeaturePtr aNewPart = aPartSet->currentFeature(false); // a copy becomes a current
68       if (aNewPart.get()) {
69         aResult = PartPtr(new PartSetAPI_Part(aNewPart));
70         DocumentPtr aNewDoc = std::dynamic_pointer_cast<ModelAPI_ResultPart>(
71           aNewPart->firstResult())->partDoc();
72         ModelAPI_Session::get()->setActiveDocument(aNewDoc);
73       }
74     }
75   }
76   return aResult;
77 }
78
79 void removePart(const ModelHighAPI_Interface& thePart)
80 {
81   FeaturePtr aPartFeature = thePart.feature();
82   if (aPartFeature) {
83     // duplication of the current part is performed
84     DocumentPtr aPartSet = ModelAPI_Session::get()->moduleDocument();
85     aPartSet->setCurrentFeature(aPartFeature, false);
86     DocumentPtr aPartDoc =
87       std::dynamic_pointer_cast<ModelAPI_ResultPart>(aPartFeature->firstResult())->partDoc();
88     if (aPartDoc.get()) {
89       ModelAPI_Session::get()->setActiveDocument(aPartDoc);
90       aPartDoc->addFeature(PartSetPlugin_Remove::ID());
91       ModelAPI_Session::get()->setActiveDocument(aPartSet);
92     }
93   }
94 }