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_AttributeRefAttr.h"
22 #include "Model_Application.h"
23 #include "Model_Data.h"
24 #include "Model_Objects.h"
25 #include <ModelAPI_Feature.h>
27 bool Model_AttributeRefAttr::isObject()
29 return myID->Get().Length() == 0;
32 void Model_AttributeRefAttr::setAttr(std::shared_ptr<ModelAPI_Attribute> theAttr)
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);
47 std::shared_ptr<ModelAPI_Attribute> Model_AttributeRefAttr::attr()
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());
55 return std::shared_ptr<ModelAPI_Attribute>();
58 void Model_AttributeRefAttr::setObject(ObjectPtr theObject)
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);
65 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(
67 myRef->Set(aData->label().Father());
68 myID->Set(""); // feature is identified by the empty ID
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);
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);
86 ObjectPtr Model_AttributeRefAttr::object()
88 if (myRef->Get() != myRef->Label()) { // initialized
89 std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(
92 TDF_Label aRefLab = myRef->Get();
93 return aDoc->objects()->object(aRefLab);
100 bool Model_AttributeRefAttr::isInitialized()
102 if (myRef->Get() == myRef->Label()) { // empty is not initialized: sketch parallelity
105 return ModelAPI_AttributeRefAttr::isInitialized();
108 Model_AttributeRefAttr::Model_AttributeRefAttr(TDF_Label& theLabel)
114 void Model_AttributeRefAttr::reinit()
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
122 myLab.FindAttribute(TDF_Reference::GetID(), myRef);