Salome HOME
Sources formated according to the codeing standards
[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 = boost::dynamic_pointer_cast<Model_Data>(
20       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().Father());
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 = boost::dynamic_pointer_cast<Model_Data>(anObj->data());
35     return aData->attribute(TCollection_AsciiString(myID->Get()).ToCString());
36   }
37   // not initialized
38   return boost::shared_ptr<ModelAPI_Attribute>();
39 }
40
41 void Model_AttributeRefAttr::setObject(ObjectPtr theObject)
42 {
43   if (!myIsInitialized || myID->Get().Length() != 0 || object() != theObject) {
44     boost::shared_ptr<Model_Data> aData = boost::dynamic_pointer_cast<Model_Data>(
45         theObject->data());
46     myRef->Set(aData->label().Father());
47     myID->Set("");  // feature is identified by the empty ID
48     owner()->data()->sendAttributeUpdated(this);
49   }
50 }
51
52 ObjectPtr Model_AttributeRefAttr::object()
53 {
54   if (myRef->Get() != myRef->Label()) {  // initialized
55     boost::shared_ptr<Model_Document> aDoc = boost::dynamic_pointer_cast<Model_Document>(
56         owner()->document());
57     if (aDoc) {
58       TDF_Label aRefLab = myRef->Get();
59       return aDoc->object(aRefLab);
60     }
61   }
62   // not initialized
63   return ObjectPtr();
64 }
65
66 Model_AttributeRefAttr::Model_AttributeRefAttr(TDF_Label& theLabel)
67 {
68   myIsInitialized = theLabel.FindAttribute(TDataStd_Comment::GetID(), myID) == Standard_True;
69   if (!myIsInitialized) {
70     // create attribute: not initialized by value yet
71     myID = TDataStd_Comment::Set(theLabel, "");
72     myRef = TDF_Reference::Set(theLabel, theLabel);  // not initialized: reference to itself
73   } else {
74     theLabel.FindAttribute(TDF_Reference::GetID(), myRef);
75   }
76 }