Salome HOME
Merge branch 'Dev_1.2.0' of newgeom:newgeom into Dev_1.2.0
[modules/shaper.git] / src / Model / Model_AttributeRefAttr.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        ModelAPI_AttributeRefAttr.cxx
4 // Created:     2 Apr 2014
5 // Author:      Mikhail PONIKAROV
6
7 #include "Model_AttributeRefAttr.h"
8 #include "Model_Application.h"
9 #include "Model_Data.h"
10 #include "Model_Objects.h"
11 #include <ModelAPI_Feature.h>
12
13 using namespace std;
14
15 bool Model_AttributeRefAttr::isObject()
16 {
17   return myID->Get().Length() == 0;
18 }
19
20 void Model_AttributeRefAttr::setAttr(std::shared_ptr<ModelAPI_Attribute> theAttr)
21 {
22   std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(
23       theAttr->owner()->data());
24   string anID = aData->id(theAttr);
25   if (myIsInitialized && object() == theAttr->owner() && myID->Get().IsEqual(anID.c_str()))
26     return;  // nothing is changed
27   REMOVE_BACK_REF(theAttr->owner());
28   myRef->Set(aData->label().Father());
29   myID->Set(aData->id(theAttr).c_str());
30   ADD_BACK_REF(theAttr->owner());
31   owner()->data()->sendAttributeUpdated(this);
32 }
33
34 std::shared_ptr<ModelAPI_Attribute> Model_AttributeRefAttr::attr()
35 {
36   ObjectPtr anObj = object();
37   if (anObj && anObj->data()) {
38     std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(anObj->data());
39     return aData->attribute(TCollection_AsciiString(myID->Get()).ToCString());
40   }
41   // not initialized
42   return std::shared_ptr<ModelAPI_Attribute>();
43 }
44
45 void Model_AttributeRefAttr::setObject(ObjectPtr theObject)
46 {
47   // the back reference from the previous object to the attribute should be removed
48   ObjectPtr anObject = object();
49   if (theObject.get() && (!myIsInitialized || myID->Get().Length() != 0 || object() != theObject)) {
50     REMOVE_BACK_REF(anObject);
51
52     std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(
53         theObject->data());
54     myRef->Set(aData->label().Father());
55     myID->Set("");  // feature is identified by the empty ID
56
57     // do it before the transaction finish to make just created/removed objects know dependencies
58     // and reference from composite feature is removed automatically
59     FeaturePtr anOwnerFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(owner());
60     if (anOwnerFeature.get()) {
61       aData->addBackReference(anOwnerFeature, id(), false);
62     }
63     ADD_BACK_REF(theObject);
64     owner()->data()->sendAttributeUpdated(this);
65   } else if (theObject.get() == NULL) {
66     REMOVE_BACK_REF(anObject);
67     myRef->Set(myRef->Label()); // reference to itself means that object is null
68     myID->Set("");  // feature is identified by the empty ID
69     owner()->data()->sendAttributeUpdated(this);
70   }
71 }
72
73 ObjectPtr Model_AttributeRefAttr::object()
74 {
75   if (myRef->Get() != myRef->Label()) {  // initialized
76     std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(
77         owner()->document());
78     if (aDoc) {
79       TDF_Label aRefLab = myRef->Get();
80       return aDoc->objects()->object(aRefLab);
81     }
82   }
83   // not initialized
84   return ObjectPtr();
85 }
86
87 bool Model_AttributeRefAttr::isInitialized()
88 {
89   if (myRef->Get() == myRef->Label()) { // empty is not initialized: sketch parallelity
90     return false;
91   }
92   return ModelAPI_AttributeRefAttr::isInitialized();
93 }
94
95 Model_AttributeRefAttr::Model_AttributeRefAttr(TDF_Label& theLabel)
96 {
97   myIsInitialized = theLabel.FindAttribute(TDataStd_Comment::GetID(), myID) == Standard_True;
98   if (!myIsInitialized) {
99     // create attribute: not initialized by value yet
100     myID = TDataStd_Comment::Set(theLabel, "");
101     myRef = TDF_Reference::Set(theLabel, theLabel);  // not initialized: reference to itself
102   } else {
103     theLabel.FindAttribute(TDF_Reference::GetID(), myRef);
104   }
105 }