Salome HOME
Updated copyright comment
[modules/shaper.git] / src / Model / Model_AttributeReference.cpp
1 // Copyright (C) 2014-2024  CEA, EDF
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
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         if (aDR.get() && aDR->objects()) {
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     }
95   } else { // internal ref
96     if (owner().get()) {
97       std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(
98           owner()->document());
99       if (aDoc) {
100         TDF_Label aRefLab = myRef->Get();
101         if (!aRefLab.IsNull()) {  // it may happen with old document, issue #285
102           return aDoc->objects()->object(aRefLab);
103         }
104       }
105     }
106   }
107   return FeaturePtr();
108 }
109
110 bool Model_AttributeReference::isInitialized()
111 {
112   if (myRef->Label() == myRef->Get() && !myRef->Label().IsAttribute(TDataStd_Comment::GetID())) {
113     // empty reference is not initialized
114     return false;
115   }
116   return ModelAPI_AttributeReference::isInitialized();
117 }
118
119 Model_AttributeReference::Model_AttributeReference(TDF_Label& theLabel)
120 {
121   myLab = theLabel;
122   reinit();
123 }
124
125 void Model_AttributeReference::reinit()
126 {
127   myIsInitialized = myLab.FindAttribute(TDF_Reference::GetID(), myRef) == Standard_True;
128   if (!myIsInitialized) {
129     myRef = TDF_Reference::Set(myLab, myLab);  // not initialized references to itself
130     myIsInitialized = true;
131   } else {
132     if (owner()) {
133       std::shared_ptr<Model_Document> aDoc =
134         std::dynamic_pointer_cast<Model_Document>(owner()->document());
135     }
136   }
137 }
138
139 void Model_AttributeReference::setObject(const std::shared_ptr<ModelAPI_Object>& theObject)
140 {
141   if (owner() != theObject) {
142     ModelAPI_AttributeReference::setObject(theObject);
143     //std::shared_ptr<Model_Document> aDoc =
144     //  std::dynamic_pointer_cast<Model_Document>(owner()->document());
145   }
146 }
147
148 Model_AttributeReference::~Model_AttributeReference()
149 {}