1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: ModelAPI_AttributeReference.cxx
5 // Author: Mikhail PONIKAROV
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>
15 #include <TDataStd_Comment.hxx>
16 #include <TDataStd_AsciiString.hxx>
17 #include <TDF_Tool.hxx>
21 void Model_AttributeReference::setValue(ObjectPtr theObject)
23 // now allow to deselect in this attribute: extrusion from/to
26 ObjectPtr aValue = value();
27 if (!myIsInitialized || aValue != theObject) {
28 REMOVE_BACK_REF(aValue);
31 if (theObject.get() && theObject->data()->isValid()) {
32 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(
34 anObjLab = aData->label().Father(); // object label
36 // same document, use reference attribute
37 if (anObjLab.IsNull() || owner()->document() == theObject->document()) {
39 if (anObjLab.IsNull()) {
40 myRef->Set(myRef->Label());
42 myRef->Set(anObjLab); // references to the object label
44 // remove external link attributes (if any)
45 myRef->Label().ForgetAttribute(TDataStd_Comment::GetID());
46 myRef->Label().ForgetAttribute(TDataStd_AsciiString::GetID());
47 } else { // different document: store the document name (comment) and entry (string): external
48 // if these attributes exist, the link is external: keep reference to access the label
49 TDataStd_Comment::Set(myRef->Label(), theObject->document()->id().c_str());
50 TCollection_AsciiString anEntry;
51 TDF_Tool::Entry(anObjLab, anEntry);
52 TDataStd_AsciiString::Set(myRef->Label(), anEntry);
54 // do it before the transaction finish to make just created/removed objects know dependencies
55 // and reference from composite feature is removed automatically
56 ADD_BACK_REF(theObject);
58 owner()->data()->sendAttributeUpdated(this);
62 ObjectPtr Model_AttributeReference::value()
64 if (isInitialized()) {
65 Handle(TDataStd_Comment) aDocID;
66 if (myRef->Label().FindAttribute(TDataStd_Comment::GetID(), aDocID)) { // external ref
68 ModelAPI_Session::get()->document(TCollection_AsciiString(aDocID->Get()).ToCString());
70 Handle(TDataStd_AsciiString) anEntry;
71 if (myRef->Label().FindAttribute(TDataStd_AsciiString::GetID(), anEntry)) {
72 std::shared_ptr<Model_Document> aDR = std::dynamic_pointer_cast<Model_Document>(aRefDoc);
74 TDF_Tool::Label(aDR->objects()->featuresLabel().Data(), anEntry->Get().ToCString(), aRefLab);
75 if (!aRefLab.IsNull()) {
76 return aDR->objects()->object(aRefLab);
80 } else { // internal ref
81 std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(
84 TDF_Label aRefLab = myRef->Get();
85 if (!aRefLab.IsNull()) { // it may happen with old document, issue #285
86 return aDoc->objects()->object(aRefLab);
95 bool Model_AttributeReference::isInitialized()
97 if (myRef->Label() == myRef->Get() && !myRef->Label().IsAttribute(TDataStd_Comment::GetID())) {
98 // empty reference is not initialized
101 return ModelAPI_AttributeReference::isInitialized();
104 Model_AttributeReference::Model_AttributeReference(TDF_Label& theLabel)
106 myIsInitialized = theLabel.FindAttribute(TDF_Reference::GetID(), myRef) == Standard_True;
107 if (!myIsInitialized) {
108 myRef = TDF_Reference::Set(theLabel, theLabel); // not initialized references to itself
111 std::shared_ptr<Model_Document> aDoc =
112 std::dynamic_pointer_cast<Model_Document>(owner()->document());
117 void Model_AttributeReference::setObject(const std::shared_ptr<ModelAPI_Object>& theObject)
119 if (owner() != theObject) {
120 ModelAPI_AttributeReference::setObject(theObject);
121 std::shared_ptr<Model_Document> aDoc =
122 std::dynamic_pointer_cast<Model_Document>(owner()->document());
126 Model_AttributeReference::~Model_AttributeReference()
128 std::shared_ptr<Model_Document> aDoc =
129 std::dynamic_pointer_cast<Model_Document>(owner()->document());
130 TDF_Label aLab = myRef->Get();