Salome HOME
Make Data not null, but invalid after the feature remove
[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   // now allow to deselect in this attribute: extrusion from/to
24   //if(!theObject)
25   //  return;
26   ObjectPtr aValue = value();
27   if (!myIsInitialized || aValue != theObject) {
28     REMOVE_BACK_REF(aValue);
29
30     TDF_Label anObjLab;
31     if (theObject.get() && theObject->data()->isValid()) {
32       std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(
33         theObject->data());
34       anObjLab = aData->label().Father(); // object label
35     }
36     // same document, use reference attribute
37     if (anObjLab.IsNull() || owner()->document() == theObject->document()) {
38
39       std::shared_ptr<Model_Document> aDoc =
40         std::dynamic_pointer_cast<Model_Document>(owner()->document());
41       if (anObjLab.IsNull()) {
42         myRef->Set(myRef->Label());
43       } else {
44         myRef->Set(anObjLab);  // references to the object label
45       }
46        // remove external link attributes (if any)
47       myRef->Label().ForgetAttribute(TDataStd_Comment::GetID());
48       myRef->Label().ForgetAttribute(TDataStd_AsciiString::GetID());
49     } else { // different document: store the document name (comment) and entry (string): external
50       // if these attributes exist, the link is external: keep reference to access the label
51       TDataStd_Comment::Set(myRef->Label(), theObject->document()->id().c_str());
52       TCollection_AsciiString anEntry;
53       TDF_Tool::Entry(anObjLab, anEntry);
54       TDataStd_AsciiString::Set(myRef->Label(), anEntry);
55     }
56     // do it before the transaction finish to make just created/removed objects know dependencies
57     // and reference from composite feature is removed automatically
58     ADD_BACK_REF(theObject);
59
60     owner()->data()->sendAttributeUpdated(this);
61   }
62 }
63
64 ObjectPtr Model_AttributeReference::value()
65 {
66   if (isInitialized()) {
67     Handle(TDataStd_Comment) aDocID;
68     if (myRef->Label().FindAttribute(TDataStd_Comment::GetID(), aDocID)) { // external ref
69       DocumentPtr aRefDoc =
70         ModelAPI_Session::get()->document(TCollection_AsciiString(aDocID->Get()).ToCString());
71       if (aRefDoc) {
72         Handle(TDataStd_AsciiString) anEntry;
73         if (myRef->Label().FindAttribute(TDataStd_AsciiString::GetID(), anEntry)) {
74           std::shared_ptr<Model_Document> aDR = std::dynamic_pointer_cast<Model_Document>(aRefDoc);
75           TDF_Label aRefLab;
76           TDF_Tool::Label(aDR->objects()->featuresLabel().Data(), anEntry->Get().ToCString(), aRefLab);
77           if (!aRefLab.IsNull()) {
78             return aDR->objects()->object(aRefLab);
79           }
80         }
81       }
82     } else { // internal ref
83       std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(
84           owner()->document());
85       if (aDoc) {
86         TDF_Label aRefLab = myRef->Get();
87         if (!aRefLab.IsNull()) {  // it may happen with old document, issue #285
88           return aDoc->objects()->object(aRefLab);
89         }
90       }
91     }
92   }
93   // not initialized
94   return FeaturePtr();
95 }
96
97 bool Model_AttributeReference::isInitialized()
98 {
99   if (myRef->Label() == myRef->Get() && !myRef->Label().IsAttribute(TDataStd_Comment::GetID())) {
100     // empty reference is not initialized
101     return false;
102   }
103   return ModelAPI_AttributeReference::isInitialized();
104 }
105
106 Model_AttributeReference::Model_AttributeReference(TDF_Label& theLabel)
107 {
108   myIsInitialized = theLabel.FindAttribute(TDF_Reference::GetID(), myRef) == Standard_True;
109   if (!myIsInitialized) {
110     myRef = TDF_Reference::Set(theLabel, theLabel);  // not initialized references to itself
111   } else {
112     if (owner()) {
113       std::shared_ptr<Model_Document> aDoc =
114         std::dynamic_pointer_cast<Model_Document>(owner()->document());
115     }
116   }
117 }
118
119 void Model_AttributeReference::setObject(const std::shared_ptr<ModelAPI_Object>& theObject)
120 {
121   if (owner() != theObject) {
122     ModelAPI_AttributeReference::setObject(theObject);
123     std::shared_ptr<Model_Document> aDoc =
124       std::dynamic_pointer_cast<Model_Document>(owner()->document());
125   }
126 }
127
128 Model_AttributeReference::~Model_AttributeReference()
129 {
130   std::shared_ptr<Model_Document> aDoc =
131     std::dynamic_pointer_cast<Model_Document>(owner()->document());
132   TDF_Label aLab = myRef->Get();
133 }