Salome HOME
Merge branch 'master' into cgt/devCEA
[modules/shaper.git] / src / Model / Model_AttributeReference.cpp
1 // Copyright (C) 2014-2017  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #include "Model_AttributeReference.h"
22 #include "Model_Application.h"
23 #include "Model_Events.h"
24 #include "Model_Data.h"
25 #include "Model_Objects.h"
26 #include <ModelAPI_Feature.h>
27 #include <ModelAPI_Session.h>
28
29 #include <TDataStd_Comment.hxx>
30 #include <TDataStd_AsciiString.hxx>
31 #include <TDF_Tool.hxx>
32
33 void Model_AttributeReference::setValue(ObjectPtr theObject)
34 {
35   // now allow to deselect in this attribute: extrusion from/to
36   //if(!theObject)
37   //  return;
38   ObjectPtr aValue = value();
39   if (!myIsInitialized || aValue != theObject) {
40     REMOVE_BACK_REF(aValue);
41
42     TDF_Label anObjLab;
43     if (theObject.get() && theObject->data()->isValid()) {
44       std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(
45         theObject->data());
46       anObjLab = aData->label().Father(); // object label
47     }
48     // same document, use reference attribute
49     if (anObjLab.IsNull() || owner()->document() == theObject->document()) {
50
51       if (anObjLab.IsNull()) {
52         myRef->Set(myRef->Label());
53       } else {
54         myRef->Set(anObjLab);  // references to the object label
55       }
56        // remove external link attributes (if any)
57       myRef->Label().ForgetAttribute(TDataStd_Comment::GetID());
58       myRef->Label().ForgetAttribute(TDataStd_AsciiString::GetID());
59     } else { // different document: store the document name (comment) and entry (string): external
60       // if these attributes exist, the link is external: keep reference to access the label
61       std::ostringstream anIdString; // string with document Id
62       anIdString<<theObject->document()->id();
63       TDataStd_Comment::Set(myRef->Label(), anIdString.str().c_str());
64       TCollection_AsciiString anEntry;
65       TDF_Tool::Entry(anObjLab, anEntry);
66       TDataStd_AsciiString::Set(myRef->Label(), anEntry);
67     }
68     // do it before the transaction finish to make just created/removed objects know dependencies
69     // and reference from composite feature is removed automatically
70     ADD_BACK_REF(theObject);
71
72     owner()->data()->sendAttributeUpdated(this);
73   }
74 }
75
76 ObjectPtr Model_AttributeReference::value()
77 {
78   Handle(TDataStd_Comment) aDocID;
79   if (myRef->Label().FindAttribute(TDataStd_Comment::GetID(), aDocID)) { // external ref
80     int anID = atoi(TCollection_AsciiString(aDocID->Get()).ToCString());
81     DocumentPtr aRefDoc = Model_Application::getApplication()->document(anID);
82     if (aRefDoc.get()) {
83       Handle(TDataStd_AsciiString) anEntry;
84       if (myRef->Label().FindAttribute(TDataStd_AsciiString::GetID(), anEntry)) {
85         std::shared_ptr<Model_Document> aDR = std::dynamic_pointer_cast<Model_Document>(aRefDoc);
86         TDF_Label aRefLab;
87         TDF_Tool::Label(aDR->objects()->featuresLabel().Data(),
88           anEntry->Get().ToCString(), aRefLab);
89         if (!aRefLab.IsNull()) {
90           return aDR->objects()->object(aRefLab);
91         }
92       }
93     }
94   } else { // internal ref
95     if (owner().get()) {
96       std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(
97           owner()->document());
98       if (aDoc) {
99         TDF_Label aRefLab = myRef->Get();
100         if (!aRefLab.IsNull()) {  // it may happen with old document, issue #285
101           return aDoc->objects()->object(aRefLab);
102         }
103       }
104     }
105   }
106   return FeaturePtr();
107 }
108
109 bool Model_AttributeReference::isInitialized()
110 {
111   if (myRef->Label() == myRef->Get() && !myRef->Label().IsAttribute(TDataStd_Comment::GetID())) {
112     // empty reference is not initialized
113     return false;
114   }
115   return ModelAPI_AttributeReference::isInitialized();
116 }
117
118 Model_AttributeReference::Model_AttributeReference(TDF_Label& theLabel)
119 {
120   myLab = theLabel;
121   reinit();
122 }
123
124 void Model_AttributeReference::reinit()
125 {
126   myIsInitialized = myLab.FindAttribute(TDF_Reference::GetID(), myRef) == Standard_True;
127   if (!myIsInitialized) {
128     myRef = TDF_Reference::Set(myLab, myLab);  // not initialized references to itself
129   } else {
130     if (owner()) {
131       std::shared_ptr<Model_Document> aDoc =
132         std::dynamic_pointer_cast<Model_Document>(owner()->document());
133     }
134   }
135 }
136
137 void Model_AttributeReference::setObject(const std::shared_ptr<ModelAPI_Object>& theObject)
138 {
139   if (owner() != theObject) {
140     ModelAPI_AttributeReference::setObject(theObject);
141     //std::shared_ptr<Model_Document> aDoc =
142     //  std::dynamic_pointer_cast<Model_Document>(owner()->document());
143   }
144 }
145
146 Model_AttributeReference::~Model_AttributeReference()
147 {}