Salome HOME
Merge branch 'master' of newgeom:newgeom.git
[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_Data.h"
8 #include <ModelAPI_Feature.h>
9
10 using namespace std;
11
12 bool Model_AttributeRefAttr::isObject()
13 {
14   return myID->Get().Length() == 0;
15 }
16
17 void Model_AttributeRefAttr::setAttr(boost::shared_ptr<ModelAPI_Attribute> theAttr)
18 {
19   boost::shared_ptr<Model_Data> aData = 
20     boost::dynamic_pointer_cast<Model_Data>(theAttr->owner()->data());
21   string anID = aData->id(theAttr);
22   if (myIsInitialized && object() == theAttr->owner() && myID->Get().IsEqual(anID.c_str()))
23     return; // nothing is changed
24
25   myRef->Set(aData->label());
26   myID->Set(aData->id(theAttr).c_str());
27   owner()->data()->sendAttributeUpdated(this);
28 }
29
30 boost::shared_ptr<ModelAPI_Attribute> Model_AttributeRefAttr::attr()
31 {
32   ObjectPtr anObj = object();
33   if (anObj) {
34     boost::shared_ptr<Model_Data> aData = 
35       boost::dynamic_pointer_cast<Model_Data>(anObj->data());
36     return aData->attribute(TCollection_AsciiString(myID->Get()).ToCString());
37   }
38   // not initialized
39   return boost::shared_ptr<ModelAPI_Attribute>();
40 }
41
42 void Model_AttributeRefAttr::setObject(ObjectPtr theObject)
43 {
44   if (!myIsInitialized || myID->Get().Length() != 0 || object() != theObject) {
45     boost::shared_ptr<Model_Data> aData = 
46       boost::dynamic_pointer_cast<Model_Data>(theObject->data());
47     myRef->Set(aData->label());
48     myID->Set(""); // feature is identified by the empty ID
49     owner()->data()->sendAttributeUpdated(this);
50   }
51 }
52
53 ObjectPtr Model_AttributeRefAttr::object()
54 {
55   if (myRef->Get() != myRef->Label()) { // initialized
56     boost::shared_ptr<Model_Document> aDoc = 
57       boost::dynamic_pointer_cast<Model_Document>(owner()->document());
58     if (aDoc) {
59       TDF_Label aRefLab = myRef->Get();
60       TDF_Label anObjLab = aRefLab.Father();
61       return aDoc->object(aRefLab);
62     }
63   }
64   // not initialized
65   return ObjectPtr();
66 }
67
68 Model_AttributeRefAttr::Model_AttributeRefAttr(TDF_Label& theLabel)
69 {
70   myIsInitialized = theLabel.FindAttribute(TDataStd_Comment::GetID(), myID) == Standard_True;
71   if (!myIsInitialized) {
72     // create attribute: not initialized by value yet
73     myID = TDataStd_Comment::Set(theLabel, "");
74     myRef = TDF_Reference::Set(theLabel, theLabel); // not initialized: reference to itself
75   } else {
76     theLabel.FindAttribute(TDF_Reference::GetID(), myRef);
77   }
78 }