Salome HOME
Make Boolean operation tools hidden on apply of Boolean
[modules/shaper.git] / src / Model / Model_AttributeReference.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        ModelAPI_AttributeReference.cxx
4 // Created:     2 Apr 2014
5 // Author:      Mikhail PONIKAROV
6
7 #include "Model_AttributeReference.h"
8 #include "Model_Application.h"
9 #include "Model_Events.h"
10 #include "Model_Data.h"
11 #include <ModelAPI_Feature.h>
12 #include <ModelAPI_Session.h>
13
14 #include <TDataStd_Comment.hxx>
15 #include <TDataStd_AsciiString.hxx>
16 #include <TDF_Tool.hxx>
17
18 using namespace std;
19
20 void Model_AttributeReference::setValue(ObjectPtr theObject)
21 {
22   if(!theObject)
23     return;
24   if (!myIsInitialized || value() != theObject) {
25     std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(
26         theObject->data());
27     TDF_Label anObjLab = aData->label().Father(); // object label
28
29     if (owner()->document() == theObject->document()) { // same document, use reference attribute
30
31       std::shared_ptr<Model_Document> aDoc =
32         std::dynamic_pointer_cast<Model_Document>(owner()->document());
33       myRef->Set(anObjLab);  // references to the object label
34        // remove external link attributes (if any)
35       myRef->Label().ForgetAttribute(TDataStd_Comment::GetID());
36       myRef->Label().ForgetAttribute(TDataStd_AsciiString::GetID());
37     } else { // different document: store the document name (comment) and entry (string): external
38       // if these attributes exist, the link is external: keep reference to access the label
39       TDataStd_Comment::Set(myRef->Label(), theObject->document()->id().c_str());
40       TCollection_AsciiString anEntry;
41       TDF_Tool::Entry(anObjLab, anEntry);
42       TDataStd_AsciiString::Set(myRef->Label(), anEntry);
43     }
44     // do it before the transaction finish to make just created/removed objects know dependencies
45     // and reference from composite feature is removed automatically
46     FeaturePtr anOwnerFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(owner());
47     if (anOwnerFeature.get()) {
48       aData->addBackReference(anOwnerFeature, id(), false);
49     }
50
51     owner()->data()->sendAttributeUpdated(this);
52   }
53 }
54
55 ObjectPtr Model_AttributeReference::value()
56 {
57   if (myIsInitialized) {
58     Handle(TDataStd_Comment) aDocID;
59     if (myRef->Label().FindAttribute(TDataStd_Comment::GetID(), aDocID)) { // external ref
60       DocumentPtr aRefDoc =
61         ModelAPI_Session::get()->document(TCollection_AsciiString(aDocID->Get()).ToCString());
62       if (aRefDoc) {
63         Handle(TDataStd_AsciiString) anEntry;
64         if (myRef->Label().FindAttribute(TDataStd_AsciiString::GetID(), anEntry)) {
65           std::shared_ptr<Model_Document> aDR = std::dynamic_pointer_cast<Model_Document>(aRefDoc);
66           TDF_Label aRefLab;
67           TDF_Tool::Label(aDR->featuresLabel().Data(), anEntry->Get().ToCString(), aRefLab);
68           if (!aRefLab.IsNull()) {
69             return aDR->object(aRefLab);
70           }
71         }
72       }
73     } else { // internal ref
74       std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(
75           owner()->document());
76       if (aDoc) {
77         TDF_Label aRefLab = myRef->Get();
78         if (!aRefLab.IsNull()) {  // it may happen with old document, issue #285
79           return aDoc->object(aRefLab);
80         }
81       }
82     }
83   }
84   // not initialized
85   return FeaturePtr();
86 }
87
88 Model_AttributeReference::Model_AttributeReference(TDF_Label& theLabel)
89 {
90   myIsInitialized = theLabel.FindAttribute(TDF_Reference::GetID(), myRef) == Standard_True;
91   if (!myIsInitialized) {
92     myRef = TDF_Reference::Set(theLabel, theLabel);  // not initialized references to itself
93   } else {
94     if (owner()) {
95       std::shared_ptr<Model_Document> aDoc =
96         std::dynamic_pointer_cast<Model_Document>(owner()->document());
97     }
98   }
99 }
100
101 void Model_AttributeReference::setObject(const std::shared_ptr<ModelAPI_Object>& theObject)
102 {
103   if (owner() != theObject) {
104     ModelAPI_AttributeReference::setObject(theObject);
105     std::shared_ptr<Model_Document> aDoc =
106       std::dynamic_pointer_cast<Model_Document>(owner()->document());
107   }
108 }
109
110 Model_AttributeReference::~Model_AttributeReference()
111 {
112   std::shared_ptr<Model_Document> aDoc =
113     std::dynamic_pointer_cast<Model_Document>(owner()->document());
114   TDF_Label aLab = myRef->Get();
115 }