1 // File: ModelAPI_AttributeRefAttr.cxx
3 // Author: Mikhail PONIKAROV
5 #include "Model_AttributeRefAttr.h"
6 #include "Model_Application.h"
7 #include "Model_Events.h"
8 #include "Model_Data.h"
9 #include <ModelAPI_Feature.h>
10 #include <Events_Loop.h>
14 bool Model_AttributeRefAttr::isFeature()
16 return myID->Get().Length() == 0;
19 void Model_AttributeRefAttr::setAttr(boost::shared_ptr<ModelAPI_Attribute> theAttr)
21 boost::shared_ptr<Model_Data> aData =
22 boost::dynamic_pointer_cast<Model_Data>(theAttr->owner()->data());
23 string anID = aData->id(theAttr);
24 if (feature() == theAttr->owner() && myID->Get().IsEqual(anID.c_str()))
25 return; // nothing is changed
27 myRef->Set(aData->label());
28 myID->Set(aData->id(theAttr).c_str());
30 static Events_ID anEvent = Events_Loop::eventByName(EVENT_FEATURE_UPDATED);
31 Model_FeatureUpdatedMessage aMsg(owner(), anEvent);
32 Events_Loop::loop()->send(aMsg);
35 boost::shared_ptr<ModelAPI_Attribute> Model_AttributeRefAttr::attr()
37 boost::shared_ptr<ModelAPI_Feature> aFeature = feature();
39 boost::shared_ptr<Model_Data> aData =
40 boost::dynamic_pointer_cast<Model_Data>(aFeature->data());
41 return aData->attribute(TCollection_AsciiString(myID->Get()).ToCString());
44 return boost::shared_ptr<ModelAPI_Attribute>();
47 void Model_AttributeRefAttr::setFeature(boost::shared_ptr<ModelAPI_Feature> theFeature)
49 if (myID->Get().Length() != 0 || feature() != theFeature) {
50 boost::shared_ptr<Model_Data> aData =
51 boost::dynamic_pointer_cast<Model_Data>(theFeature->data());
52 myRef->Set(aData->label());
53 myID->Set(""); // feature is identified by the empty ID
55 static Events_ID anEvent = Events_Loop::eventByName(EVENT_FEATURE_UPDATED);
56 Model_FeatureUpdatedMessage aMsg(owner(), anEvent);
57 Events_Loop::loop()->send(aMsg);
61 boost::shared_ptr<ModelAPI_Feature> Model_AttributeRefAttr::feature()
63 if (myRef->Get() != myRef->Label()) { // initialized
64 boost::shared_ptr<Model_Document> aDoc =
65 boost::dynamic_pointer_cast<Model_Document>(owner()->document());
67 TDF_Label aRefLab = myRef->Get();
68 TDF_Label aFeatureLab = aRefLab.Father();
69 return aDoc->feature(aRefLab);
73 return boost::shared_ptr<ModelAPI_Feature>();
76 Model_AttributeRefAttr::Model_AttributeRefAttr(TDF_Label& theLabel)
78 // check the attribute could be already presented in this doc (after load document)
79 if (!theLabel.FindAttribute(TDataStd_Comment::GetID(), myID)) {
80 // create attribute: not initialized by value yet
81 myID = TDataStd_Comment::Set(theLabel, "");
82 myRef = TDF_Reference::Set(theLabel, theLabel); // not initialized: reference to itself
84 theLabel.FindAttribute(TDF_Reference::GetID(), myRef);