1 // Copyright (C) 2014-2017 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
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
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>
29 #include <TDataStd_Comment.hxx>
30 #include <TDataStd_AsciiString.hxx>
31 #include <TDF_Tool.hxx>
33 void Model_AttributeReference::setValue(ObjectPtr theObject)
35 // now allow to deselect in this attribute: extrusion from/to
38 ObjectPtr aValue = value();
39 if (!myIsInitialized || aValue != theObject) {
40 REMOVE_BACK_REF(aValue);
43 if (theObject.get() && theObject->data()->isValid()) {
44 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(
46 anObjLab = aData->label().Father(); // object label
48 // same document, use reference attribute
49 if (anObjLab.IsNull() || owner()->document() == theObject->document()) {
51 if (anObjLab.IsNull()) {
52 myRef->Set(myRef->Label());
54 myRef->Set(anObjLab); // references to the object label
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);
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);
72 owner()->data()->sendAttributeUpdated(this);
76 ObjectPtr Model_AttributeReference::value()
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);
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);
87 TDF_Tool::Label(aDR->objects()->featuresLabel().Data(),
88 anEntry->Get().ToCString(), aRefLab);
89 if (!aRefLab.IsNull()) {
90 return aDR->objects()->object(aRefLab);
94 } else { // internal ref
96 std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(
99 TDF_Label aRefLab = myRef->Get();
100 if (!aRefLab.IsNull()) { // it may happen with old document, issue #285
101 return aDoc->objects()->object(aRefLab);
109 bool Model_AttributeReference::isInitialized()
111 if (myRef->Label() == myRef->Get() && !myRef->Label().IsAttribute(TDataStd_Comment::GetID())) {
112 // empty reference is not initialized
115 return ModelAPI_AttributeReference::isInitialized();
118 Model_AttributeReference::Model_AttributeReference(TDF_Label& theLabel)
124 void Model_AttributeReference::reinit()
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
131 std::shared_ptr<Model_Document> aDoc =
132 std::dynamic_pointer_cast<Model_Document>(owner()->document());
137 void Model_AttributeReference::setObject(const std::shared_ptr<ModelAPI_Object>& theObject)
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());
146 Model_AttributeReference::~Model_AttributeReference()