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