Salome HOME
Merge branch 'Dev_0.6.1' of newgeom:newgeom into Dev_0.6.1
[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       anObjLab.ForgetAttribute(TDataStd_Comment::GetID());
36       anObjLab.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
45     owner()->data()->sendAttributeUpdated(this);
46   }
47 }
48
49 ObjectPtr Model_AttributeReference::value()
50 {
51   if (myIsInitialized) {
52     Handle(TDataStd_Comment) aDocID;
53     if (myRef->Label().FindAttribute(TDataStd_Comment::GetID(), aDocID)) { // external ref
54       DocumentPtr aRefDoc =
55         ModelAPI_Session::get()->document(TCollection_AsciiString(aDocID->Get()).ToCString());
56       if (aRefDoc) {
57         Handle(TDataStd_AsciiString) anEntry;
58         if (myRef->Label().FindAttribute(TDataStd_AsciiString::GetID(), anEntry)) {
59           std::shared_ptr<Model_Document> aDR = std::dynamic_pointer_cast<Model_Document>(aRefDoc);
60           TDF_Label aRefLab;
61           TDF_Tool::Label(aDR->featuresLabel().Data(), anEntry->Get().ToCString(), aRefLab);
62           if (!aRefLab.IsNull()) {
63             return aDR->object(aRefLab);
64           }
65         }
66       }
67     } else { // internal ref
68       std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(
69           owner()->document());
70       if (aDoc) {
71         TDF_Label aRefLab = myRef->Get();
72         return aDoc->object(aRefLab);
73       }
74     }
75   }
76   // not initialized
77   return FeaturePtr();
78 }
79
80 Model_AttributeReference::Model_AttributeReference(TDF_Label& theLabel)
81 {
82   myIsInitialized = theLabel.FindAttribute(TDF_Reference::GetID(), myRef) == Standard_True;
83   if (!myIsInitialized) {
84     myRef = TDF_Reference::Set(theLabel, theLabel);  // not initialized references to itself
85   } else {
86     if (owner()) {
87       std::shared_ptr<Model_Document> aDoc =
88         std::dynamic_pointer_cast<Model_Document>(owner()->document());
89     }
90   }
91 }
92
93 void Model_AttributeReference::setObject(const std::shared_ptr<ModelAPI_Object>& theObject)
94 {
95   if (owner() != theObject) {
96     ModelAPI_AttributeReference::setObject(theObject);
97     std::shared_ptr<Model_Document> aDoc =
98       std::dynamic_pointer_cast<Model_Document>(owner()->document());
99   }
100 }
101
102 Model_AttributeReference::~Model_AttributeReference()
103 {
104   std::shared_ptr<Model_Document> aDoc =
105     std::dynamic_pointer_cast<Model_Document>(owner()->document());
106   TDF_Label aLab = myRef->Get();
107 }