1 // Copyright (C) 2014-2020 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 email : webmaster.salome@opencascade.com
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>
26 bool Model_AttributeRefAttr::isObject()
28 return myID->Get().Length() == 0;
31 void Model_AttributeRefAttr::setAttr(std::shared_ptr<ModelAPI_Attribute> theAttr)
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);
46 std::shared_ptr<ModelAPI_Attribute> Model_AttributeRefAttr::attr()
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());
54 return std::shared_ptr<ModelAPI_Attribute>();
57 void Model_AttributeRefAttr::setObject(ObjectPtr theObject)
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);
64 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(
66 myRef->Set(aData->label().Father());
67 myID->Set(""); // feature is identified by the empty ID
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);
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);
85 ObjectPtr Model_AttributeRefAttr::object()
87 if (myRef->Get() != myRef->Label()) { // initialized
88 std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(
91 TDF_Label aRefLab = myRef->Get();
92 return aDoc->objects()->object(aRefLab);
99 bool Model_AttributeRefAttr::isInitialized()
101 if (myRef->Get() == myRef->Label()) { // empty is not initialized: sketch parallelity
104 return ModelAPI_AttributeRefAttr::isInitialized();
107 Model_AttributeRefAttr::Model_AttributeRefAttr(TDF_Label& theLabel)
113 void Model_AttributeRefAttr::reinit()
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
121 myLab.FindAttribute(TDF_Reference::GetID(), myRef);