Salome HOME
updated copyright message
[modules/shaper.git] / src / Model / Model_AttributeRefAttr.cpp
1 // Copyright (C) 2014-2023  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_AttributeRefAttr.h"
21 #include "Model_Application.h"
22 #include "Model_Data.h"
23 #include "Model_Objects.h"
24 #include <ModelAPI_Feature.h>
25
26 bool Model_AttributeRefAttr::isObject()
27 {
28   return myID->Get().Length() == 0;
29 }
30
31 void Model_AttributeRefAttr::setAttr(std::shared_ptr<ModelAPI_Attribute> theAttr)
32 {
33   std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(
34       theAttr->owner()->data());
35   std::string anID = aData->id(theAttr);
36   ObjectPtr anObj = object();
37   if (myIsInitialized && anObj == theAttr->owner() && myID->Get().IsEqual(anID.c_str()))
38     return;  // nothing is changed
39   REMOVE_BACK_REF(anObj);
40   myRef->Set(aData->label().Father());
41   myID->Set(aData->id(theAttr).c_str());
42   ADD_BACK_REF(theAttr->owner());
43   owner()->data()->sendAttributeUpdated(this);
44 }
45
46 std::shared_ptr<ModelAPI_Attribute> Model_AttributeRefAttr::attr()
47 {
48   ObjectPtr anObj = object();
49   if (anObj && anObj->data()) {
50     std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(anObj->data());
51     return aData->attribute(TCollection_AsciiString(myID->Get()).ToCString());
52   }
53   // not initialized
54   return std::shared_ptr<ModelAPI_Attribute>();
55 }
56
57 void Model_AttributeRefAttr::setObject(ObjectPtr theObject)
58 {
59   // the back reference from the previous object to the attribute should be removed
60   ObjectPtr anObject = object();
61   if (theObject.get() && (!myIsInitialized || myID->Get().Length() != 0 || object() != theObject)) {
62     REMOVE_BACK_REF(anObject);
63
64     std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(
65         theObject->data());
66     myRef->Set(aData->label().Father());
67     myID->Set("");  // feature is identified by the empty ID
68
69     // do it before the transaction finish to make just created/removed objects know dependencies
70     // and reference from composite feature is removed automatically
71     FeaturePtr anOwnerFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(owner());
72     if (anOwnerFeature.get()) {
73       aData->addBackReference(anOwnerFeature, id(), false);
74     }
75     ADD_BACK_REF(theObject);
76     owner()->data()->sendAttributeUpdated(this);
77   } else if (theObject.get() == NULL) {
78     REMOVE_BACK_REF(anObject);
79     myRef->Set(myRef->Label()); // reference to itself means that object is null
80     myID->Set("");  // feature is identified by the empty ID
81     owner()->data()->sendAttributeUpdated(this);
82   }
83 }
84
85 ObjectPtr Model_AttributeRefAttr::object()
86 {
87   if (myRef->Get() != myRef->Label()) {  // initialized
88     std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(
89         owner()->document());
90     if (aDoc) {
91       TDF_Label aRefLab = myRef->Get();
92       return aDoc->objects()->object(aRefLab);
93     }
94   }
95   // not initialized
96   return ObjectPtr();
97 }
98
99 bool Model_AttributeRefAttr::isInitialized()
100 {
101   if (myRef->Get() == myRef->Label()) { // empty is not initialized: sketch parallelity
102     return false;
103   }
104   return ModelAPI_AttributeRefAttr::isInitialized();
105 }
106
107 Model_AttributeRefAttr::Model_AttributeRefAttr(TDF_Label& theLabel)
108 {
109   myLab = theLabel;
110   reinit();
111 }
112
113 void Model_AttributeRefAttr::reinit()
114 {
115   myIsInitialized = myLab.FindAttribute(TDataStd_Comment::GetID(), myID) == Standard_True;
116   if (!myIsInitialized) {
117     // create attribute: not initialized by value yet
118     myID = TDataStd_Comment::Set(myLab, "");
119     myRef = TDF_Reference::Set(myLab, myLab);  // not initialized: reference to itself
120   } else {
121     myLab.FindAttribute(TDF_Reference::GetID(), myRef);
122   }
123 }