1 // Copyright (C) 2014-2019 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
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>
28 #include <TDataStd_Comment.hxx>
29 #include <TDataStd_AsciiString.hxx>
30 #include <TDF_Tool.hxx>
32 void Model_AttributeReference::setValue(ObjectPtr theObject)
34 // now allow to deselect in this attribute: extrusion from/to
37 ObjectPtr aValue = value();
38 if (!myIsInitialized || aValue != theObject) {
39 REMOVE_BACK_REF(aValue);
42 if (theObject.get() && theObject->data()->isValid()) {
43 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(
45 anObjLab = aData->label().Father(); // object label
47 // same document, use reference attribute
48 if (anObjLab.IsNull() || owner()->document() == theObject->document()) {
50 if (anObjLab.IsNull()) {
51 myRef->Set(myRef->Label());
53 myRef->Set(anObjLab); // references to the object label
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);
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);
71 owner()->data()->sendAttributeUpdated(this);
75 ObjectPtr Model_AttributeReference::value()
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);
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);
86 TDF_Tool::Label(aDR->objects()->featuresLabel().Data(),
87 anEntry->Get().ToCString(), aRefLab);
88 if (!aRefLab.IsNull()) {
89 return aDR->objects()->object(aRefLab);
93 } else { // internal ref
95 std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(
98 TDF_Label aRefLab = myRef->Get();
99 if (!aRefLab.IsNull()) { // it may happen with old document, issue #285
100 return aDoc->objects()->object(aRefLab);
108 bool Model_AttributeReference::isInitialized()
110 if (myRef->Label() == myRef->Get() && !myRef->Label().IsAttribute(TDataStd_Comment::GetID())) {
111 // empty reference is not initialized
114 return ModelAPI_AttributeReference::isInitialized();
117 Model_AttributeReference::Model_AttributeReference(TDF_Label& theLabel)
123 void Model_AttributeReference::reinit()
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
130 std::shared_ptr<Model_Document> aDoc =
131 std::dynamic_pointer_cast<Model_Document>(owner()->document());
136 void Model_AttributeReference::setObject(const std::shared_ptr<ModelAPI_Object>& theObject)
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());
145 Model_AttributeReference::~Model_AttributeReference()