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