Salome HOME
Merge branch 'master' of newgeom:newgeom
[modules/shaper.git] / src / Model / Model_AttributeRefAttr.cpp
1 // File:        ModelAPI_AttributeRefAttr.cxx
2 // Created:     2 Apr 2014
3 // Author:      Mikhail PONIKAROV
4
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>
11
12 using namespace std;
13
14 bool Model_AttributeRefAttr::isFeature()
15 {
16   return myID->Get().Length() == 0;
17 }
18
19 void Model_AttributeRefAttr::setAttr(boost::shared_ptr<ModelAPI_Attribute> theAttr)
20 {
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
26
27   myRef->Set(aData->label());
28   myID->Set(aData->id(theAttr).c_str());
29
30   static Events_ID anEvent = Events_Loop::eventByName(EVENT_FEATURE_UPDATED);
31   Model_FeatureUpdatedMessage aMsg(feature(), anEvent);
32   Events_Loop::loop()->send(aMsg);
33 }
34
35 boost::shared_ptr<ModelAPI_Attribute> Model_AttributeRefAttr::attr()
36 {
37   boost::shared_ptr<ModelAPI_Feature> aFeature = feature();
38   if (aFeature) {
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());
42   }
43   // not initialized
44   return boost::shared_ptr<ModelAPI_Attribute>();
45 }
46
47 void Model_AttributeRefAttr::setFeature(boost::shared_ptr<ModelAPI_Feature> theFeature)
48 {
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
54
55     static Events_ID anEvent = Events_Loop::eventByName(EVENT_FEATURE_UPDATED);
56     Model_FeatureUpdatedMessage aMsg(owner(), anEvent);
57     Events_Loop::loop()->send(aMsg);
58   }
59 }
60
61 boost::shared_ptr<ModelAPI_Feature> Model_AttributeRefAttr::feature()
62 {
63   if (myRef->Get() != myRef->Label()) { // initialized
64     boost::shared_ptr<Model_Document> aDoc = 
65       boost::dynamic_pointer_cast<Model_Document>(owner()->document());
66     if (aDoc) {
67       TDF_Label aRefLab = myRef->Get();
68       TDF_Label aFeatureLab = aRefLab.Father();
69       return aDoc->feature(aRefLab);
70     }
71   }
72   // not initialized
73   return boost::shared_ptr<ModelAPI_Feature>();
74 }
75
76 Model_AttributeRefAttr::Model_AttributeRefAttr(TDF_Label& theLabel)
77 {
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
83   }
84 }