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