1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: ModelAPI_AttributeRefAttr.cxx
5 // Author: Mikhail PONIKAROV
7 #include "Model_AttributeRefAttr.h"
8 #include "Model_Application.h"
9 #include "Model_Data.h"
10 #include <ModelAPI_Feature.h>
14 bool Model_AttributeRefAttr::isObject()
16 return myID->Get().Length() == 0;
19 void Model_AttributeRefAttr::setAttr(std::shared_ptr<ModelAPI_Attribute> theAttr)
21 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(
22 theAttr->owner()->data());
23 string anID = aData->id(theAttr);
24 if (myIsInitialized && object() == theAttr->owner() && myID->Get().IsEqual(anID.c_str()))
25 return; // nothing is changed
26 myRef->Set(aData->label().Father());
27 myID->Set(aData->id(theAttr).c_str());
28 owner()->data()->sendAttributeUpdated(this);
31 std::shared_ptr<ModelAPI_Attribute> Model_AttributeRefAttr::attr()
33 ObjectPtr anObj = object();
34 if (anObj && anObj->data()) {
35 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(anObj->data());
36 return aData->attribute(TCollection_AsciiString(myID->Get()).ToCString());
39 return std::shared_ptr<ModelAPI_Attribute>();
42 void Model_AttributeRefAttr::setObject(ObjectPtr theObject)
44 // the back reference from the previous object to the attribute should be removed
45 ObjectPtr anObject = object();
46 if (anObject.get() && anObject != theObject) {
47 FeaturePtr anOwnerFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(owner());
48 if (anOwnerFeature.get()) {
49 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(
51 aData->removeBackReference(anOwnerFeature, id());
55 if (theObject && (!myIsInitialized || myID->Get().Length() != 0 || object() != theObject)) {
56 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(
58 myRef->Set(aData->label().Father());
59 myID->Set(""); // feature is identified by the empty ID
61 // do it before the transaction finish to make just created/removed objects know dependencies
62 // and reference from composite feature is removed automatically
63 FeaturePtr anOwnerFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(owner());
64 if (anOwnerFeature.get()) {
65 aData->addBackReference(anOwnerFeature, id(), false);
67 owner()->data()->sendAttributeUpdated(this);
68 } else if (theObject.get() == NULL) {
69 myRef->Set(myRef->Label()); // reference to itself means that object is null
70 myID->Set(""); // feature is identified by the empty ID
71 owner()->data()->sendAttributeUpdated(this);
75 ObjectPtr Model_AttributeRefAttr::object()
77 if (myRef->Get() != myRef->Label()) { // initialized
78 std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(
81 TDF_Label aRefLab = myRef->Get();
82 return aDoc->object(aRefLab);
89 Model_AttributeRefAttr::Model_AttributeRefAttr(TDF_Label& theLabel)
91 myIsInitialized = theLabel.FindAttribute(TDataStd_Comment::GetID(), myID) == Standard_True;
92 if (!myIsInitialized) {
93 // create attribute: not initialized by value yet
94 myID = TDataStd_Comment::Set(theLabel, "");
95 myRef = TDF_Reference::Set(theLabel, theLabel); // not initialized: reference to itself
97 theLabel.FindAttribute(TDF_Reference::GetID(), myRef);