Salome HOME
Fix for the issue #686: now compounds produced by the Boolean operations can be corre...
[modules/shaper.git] / src / Model / Model_ResultPart.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        ModelAPI_ResultPart.cpp
4 // Created:     07 Jul 2014
5 // Author:      Mikhail PONIKAROV
6
7 #include <Model_ResultPart.h>
8 #include <ModelAPI_Data.h>
9 #include <ModelAPI_AttributeDocRef.h>
10 #include <ModelAPI_Session.h>
11 #include <ModelAPI_Feature.h>
12
13 std::shared_ptr<ModelAPI_Document> Model_ResultPart::partDoc()
14 {
15   return data()->document("PartDocument")->value();
16 }
17
18 std::shared_ptr<ModelAPI_Feature> Model_ResultPart::owner()
19 {
20   return std::shared_ptr<ModelAPI_Feature>();  // return empty pointer
21 }
22
23 Model_ResultPart::Model_ResultPart()
24 {
25   myIsDisabled = true; // by default it is not initialized and false to be after created
26   setIsConcealed(false);
27 }
28
29 void Model_ResultPart::setData(std::shared_ptr<ModelAPI_Data> theData)
30 {
31   ModelAPI_Result::setData(theData);
32   if (theData) {
33     data()->addAttribute(DOC_REF(), ModelAPI_AttributeDocRef::typeId());
34   }
35 }
36
37 void Model_ResultPart::activate()
38 {
39   std::shared_ptr<ModelAPI_AttributeDocRef> aDocRef = data()->document(DOC_REF());
40   
41   if (!aDocRef->value().get()) {  // create (or open) a document if it is not yet created
42     std::shared_ptr<ModelAPI_Document> aDoc = document()->subDocument(data()->name());
43     if (aDoc) {
44       aDocRef->setValue(aDoc);
45     }
46   }
47   if (aDocRef->value().get()) {
48     SessionPtr aMgr = ModelAPI_Session::get();
49     bool isNewTransaction = !aMgr->isOperation();
50     // activation may cause changes in current features in document, so it must be in transaction
51     if (isNewTransaction) {
52       aMgr->startOperation("Activation");
53     }
54     ModelAPI_Session::get()->setActiveDocument(aDocRef->value());
55     if (isNewTransaction) {
56       aMgr->finishOperation();
57     }
58   }
59 }
60
61 bool Model_ResultPart::isActivated() 
62 {
63   std::shared_ptr<ModelAPI_AttributeDocRef> aDocRef = data()->document(DOC_REF());
64   return aDocRef->value().get();
65 }
66
67 bool Model_ResultPart::setDisabled(std::shared_ptr<ModelAPI_Result> theThis,
68     const bool theFlag)
69 {
70   if (ModelAPI_ResultPart::setDisabled(theThis, theFlag)) {
71     DocumentPtr aDoc = Model_ResultPart::partDoc();
72     if (aDoc.get()) {
73       if (theFlag) { // disable, so make all features disabled too
74         aDoc->setCurrentFeature(FeaturePtr(), false);
75       } else { // enabled, so make the current feature the last inside of this document
76         FeaturePtr aLastFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aDoc->object(
77           ModelAPI_Feature::group(), aDoc->size(ModelAPI_Feature::group()) - 1));
78         aDoc->setCurrentFeature(aLastFeature, false);
79       }
80     }
81     return true;
82   }
83   return false;
84 }