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