Salome HOME
Merge branch 'master' of newgeom:newgeom
[modules/shaper.git] / src / Model / Model_AttributeDocRef.cpp
1 // File:        ModelAPI_AttributeDocRef.cxx
2 // Created:     2 Apr 2014
3 // Author:      Mikhail PONIKAROV
4
5 #include "Model_AttributeDocRef.h"
6 #include "Model_Application.h"
7 #include <ModelAPI_Feature.h>
8 #include <ModelAPI_Data.h>
9
10 using namespace std;
11
12 void Model_AttributeDocRef::setValue(boost::shared_ptr<ModelAPI_Document> theDoc)
13 {
14   TCollection_ExtendedString aNewID(theDoc->id().c_str());
15   if (!myIsInitialized || myComment->Get() != aNewID) {
16     myComment->Set(TCollection_ExtendedString(theDoc->id().c_str()));
17     owner()->data()->sendAttributeUpdated(this);
18   }
19 }
20
21 boost::shared_ptr<ModelAPI_Document> Model_AttributeDocRef::value()
22 {
23   if (myComment->Get().Length())
24     return Model_Application::getApplication()->getDocument(
25       TCollection_AsciiString(myComment->Get()).ToCString());
26   // not initialized
27   return boost::shared_ptr<ModelAPI_Document>();
28 }
29
30 Model_AttributeDocRef::Model_AttributeDocRef(TDF_Label& theLabel)
31 {
32   myIsInitialized = theLabel.FindAttribute(TDataStd_Comment::GetID(), myComment) == Standard_True;
33   if (!myIsInitialized) {
34     // create attribute: not initialized by value yet, just empty string
35     myComment = TDataStd_Comment::Set(theLabel, "");
36   } else { // document was already referenced: try to set it as loaded by demand
37     Handle(Model_Application) anApp = Model_Application::getApplication();
38     string anID(TCollection_AsciiString(myComment->Get()).ToCString());
39     if (!anApp->hasDocument(anID)) {
40       anApp->setLoadByDemand(anID);
41     }
42   }
43 }