Salome HOME
Issue #273: Add copyright string
[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 <ModelAPI_Feature.h>
11
12 using namespace std;
13
14 bool Model_AttributeRefAttr::isObject()
15 {
16   return myID->Get().Length() == 0;
17 }
18
19 void Model_AttributeRefAttr::setAttr(std::shared_ptr<ModelAPI_Attribute> theAttr)
20 {
21   std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(
22       theAttr->owner()->data());
23   string anID = aData->id(theAttr);
24   if (myIsInitialized && object() == theAttr->owner() && myID->Get().IsEqual(anID.c_str()))
25     return;  // nothing is changed
26   myRef->Set(aData->label().Father());
27   myID->Set(aData->id(theAttr).c_str());
28   owner()->data()->sendAttributeUpdated(this);
29 }
30
31 std::shared_ptr<ModelAPI_Attribute> Model_AttributeRefAttr::attr()
32 {
33   ObjectPtr anObj = object();
34   if (anObj) {
35     std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(anObj->data());
36     return aData->attribute(TCollection_AsciiString(myID->Get()).ToCString());
37   }
38   // not initialized
39   return std::shared_ptr<ModelAPI_Attribute>();
40 }
41
42 void Model_AttributeRefAttr::setObject(ObjectPtr theObject)
43 {
44   if (theObject && (!myIsInitialized || myID->Get().Length() != 0 || object() != theObject)) {
45     std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(
46         theObject->data());
47     myRef->Set(aData->label().Father());
48     myID->Set("");  // feature is identified by the empty ID
49     owner()->data()->sendAttributeUpdated(this);
50   }
51 }
52
53 ObjectPtr Model_AttributeRefAttr::object()
54 {
55   if (myRef->Get() != myRef->Label()) {  // initialized
56     std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(
57         owner()->document());
58     if (aDoc) {
59       TDF_Label aRefLab = myRef->Get();
60       return aDoc->object(aRefLab);
61     }
62   }
63   // not initialized
64   return ObjectPtr();
65 }
66
67 Model_AttributeRefAttr::Model_AttributeRefAttr(TDF_Label& theLabel)
68 {
69   myIsInitialized = theLabel.FindAttribute(TDataStd_Comment::GetID(), myID) == Standard_True;
70   if (!myIsInitialized) {
71     // create attribute: not initialized by value yet
72     myID = TDataStd_Comment::Set(theLabel, "");
73     myRef = TDF_Reference::Set(theLabel, theLabel);  // not initialized: reference to itself
74   } else {
75     theLabel.FindAttribute(TDF_Reference::GetID(), myRef);
76   }
77 }