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