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