Salome HOME
Put groups to the separated plugin: Collection
[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 bool Model_AttributeRefAttr::isObject()
14 {
15   return myID->Get().Length() == 0;
16 }
17
18 void Model_AttributeRefAttr::setAttr(std::shared_ptr<ModelAPI_Attribute> theAttr)
19 {
20   std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(
21       theAttr->owner()->data());
22   std::string anID = aData->id(theAttr);
23   if (myIsInitialized && object() == theAttr->owner() && myID->Get().IsEqual(anID.c_str()))
24     return;  // nothing is changed
25   REMOVE_BACK_REF(theAttr->owner());
26   myRef->Set(aData->label().Father());
27   myID->Set(aData->id(theAttr).c_str());
28   ADD_BACK_REF(theAttr->owner());
29   owner()->data()->sendAttributeUpdated(this);
30 }
31
32 std::shared_ptr<ModelAPI_Attribute> Model_AttributeRefAttr::attr()
33 {
34   ObjectPtr anObj = object();
35   if (anObj && anObj->data()) {
36     std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(anObj->data());
37     return aData->attribute(TCollection_AsciiString(myID->Get()).ToCString());
38   }
39   // not initialized
40   return std::shared_ptr<ModelAPI_Attribute>();
41 }
42
43 void Model_AttributeRefAttr::setObject(ObjectPtr theObject)
44 {
45   // the back reference from the previous object to the attribute should be removed
46   ObjectPtr anObject = object();
47   if (theObject.get() && (!myIsInitialized || myID->Get().Length() != 0 || object() != theObject)) {
48     REMOVE_BACK_REF(anObject);
49
50     std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(
51         theObject->data());
52     myRef->Set(aData->label().Father());
53     myID->Set("");  // feature is identified by the empty ID
54
55     // do it before the transaction finish to make just created/removed objects know dependencies
56     // and reference from composite feature is removed automatically
57     FeaturePtr anOwnerFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(owner());
58     if (anOwnerFeature.get()) {
59       aData->addBackReference(anOwnerFeature, id(), false);
60     }
61     ADD_BACK_REF(theObject);
62     owner()->data()->sendAttributeUpdated(this);
63   } else if (theObject.get() == NULL) {
64     REMOVE_BACK_REF(anObject);
65     myRef->Set(myRef->Label()); // reference to itself means that object is null
66     myID->Set("");  // feature is identified by the empty ID
67     owner()->data()->sendAttributeUpdated(this);
68   }
69 }
70
71 ObjectPtr Model_AttributeRefAttr::object()
72 {
73   if (myRef->Get() != myRef->Label()) {  // initialized
74     std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(
75         owner()->document());
76     if (aDoc) {
77       TDF_Label aRefLab = myRef->Get();
78       return aDoc->objects()->object(aRefLab);
79     }
80   }
81   // not initialized
82   return ObjectPtr();
83 }
84
85 bool Model_AttributeRefAttr::isInitialized()
86 {
87   if (myRef->Get() == myRef->Label()) { // empty is not initialized: sketch parallelity
88     return false;
89   }
90   return ModelAPI_AttributeRefAttr::isInitialized();
91 }
92
93 Model_AttributeRefAttr::Model_AttributeRefAttr(TDF_Label& theLabel)
94 {
95   myLab = theLabel;
96   reinit();
97 }
98
99 void Model_AttributeRefAttr::reinit()
100 {
101   myIsInitialized = myLab.FindAttribute(TDataStd_Comment::GetID(), myID) == Standard_True;
102   if (!myIsInitialized) {
103     // create attribute: not initialized by value yet
104     myID = TDataStd_Comment::Set(myLab, "");
105     myRef = TDF_Reference::Set(myLab, myLab);  // not initialized: reference to itself
106   } else {
107     myLab.FindAttribute(TDF_Reference::GetID(), myRef);
108   }
109 }