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