1 // File: ModelAPI_AttributeReference.cxx
3 // Author: Mikhail PONIKAROV
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>
12 #include <TDataStd_Comment.hxx>
13 #include <TDataStd_AsciiString.hxx>
14 #include <TDF_Tool.hxx>
18 void Model_AttributeReference::setValue(ObjectPtr theObject)
22 if (!myIsInitialized || value() != theObject) {
23 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(
25 TDF_Label anObjLab = aData->label().Father(); // object label
27 if (owner()->document() == theObject->document()) { // same document, use reference attribute
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);
43 owner()->data()->sendAttributeUpdated(this);
47 ObjectPtr Model_AttributeReference::value()
49 if (myIsInitialized) {
50 Handle(TDataStd_Comment) aDocID;
51 if (myRef->Label().FindAttribute(TDataStd_Comment::GetID(), aDocID)) { // external ref
53 ModelAPI_Session::get()->document(TCollection_AsciiString(aDocID->Get()).ToCString());
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);
59 TDF_Tool::Label(aDR->featuresLabel().Data(), anEntry->Get().ToCString(), aRefLab);
60 if (!aRefLab.IsNull()) {
61 return aDR->object(aRefLab);
65 } else { // internal ref
66 std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(
69 TDF_Label aRefLab = myRef->Get();
70 return aDoc->object(aRefLab);
78 Model_AttributeReference::Model_AttributeReference(TDF_Label& theLabel)
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
85 std::shared_ptr<Model_Document> aDoc =
86 std::dynamic_pointer_cast<Model_Document>(owner()->document());
91 void Model_AttributeReference::setObject(const std::shared_ptr<ModelAPI_Object>& theObject)
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());
100 Model_AttributeReference::~Model_AttributeReference()
102 std::shared_ptr<Model_Document> aDoc =
103 std::dynamic_pointer_cast<Model_Document>(owner()->document());
104 TDF_Label aLab = myRef->Get();