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