Salome HOME
Merge branch 'master' of newgeom:newgeom.git
[modules/shaper.git] / src / Model / Model_AttributeReference.cpp
1 // File:        ModelAPI_AttributeReference.cxx
2 // Created:     2 Apr 2014
3 // Author:      Mikhail PONIKAROV
4
5 #include "Model_AttributeReference.h"
6 #include "Model_Application.h"
7 #include "Model_Events.h"
8 #include "Model_Data.h"
9 #include <ModelAPI_Feature.h>
10
11 using namespace std;
12
13 void Model_AttributeReference::setValue(ObjectPtr theObject)
14 {
15   if(!theObject)
16     return;
17   if (!myIsInitialized || value() != theObject) {
18     boost::shared_ptr<Model_Data> aData = boost::dynamic_pointer_cast<Model_Data>(
19         theObject->data());
20
21     boost::shared_ptr<Model_Document> aDoc =
22       boost::dynamic_pointer_cast<Model_Document>(owner()->document());
23     if (aDoc) aDoc->objectIsNotReferenced(aDoc->object(myRef->Label()));
24     myRef->Set(aData->label().Father());  // references to the feature label
25     boost::shared_dynamic_cast<Model_Document>(owner()->document())->objectIsReferenced(theObject);
26
27     owner()->data()->sendAttributeUpdated(this);
28   }
29 }
30
31 ObjectPtr Model_AttributeReference::value()
32 {
33   if (myIsInitialized) {
34     boost::shared_ptr<Model_Document> aDoc = boost::dynamic_pointer_cast<Model_Document>(
35         owner()->document());
36     if (aDoc) {
37       TDF_Label aRefLab = myRef->Get();
38       return aDoc->object(aRefLab);
39     }
40   }
41   // not initialized
42   return FeaturePtr();
43 }
44
45 Model_AttributeReference::Model_AttributeReference(TDF_Label& theLabel)
46 {
47   myIsInitialized = theLabel.FindAttribute(TDF_Reference::GetID(), myRef) == Standard_True;
48   if (!myIsInitialized) {
49     myRef = TDF_Reference::Set(theLabel, theLabel);  // not initialized references to itself
50   } else {
51     boost::shared_ptr<Model_Document> aDoc =
52       boost::dynamic_pointer_cast<Model_Document>(owner()->document());
53     if (aDoc) aDoc->objectIsReferenced(aDoc->object(myRef->Label()));
54   }
55 }
56
57 Model_AttributeReference::~Model_AttributeReference()
58 {
59   boost::shared_ptr<Model_Document> aDoc =
60     boost::dynamic_pointer_cast<Model_Document>(owner()->document());
61   if (aDoc) aDoc->objectIsNotReferenced(aDoc->object(myRef->Label()));
62 }