]> SALOME platform Git repositories - modules/shaper.git/blob - src/Model/Model_Data.cpp
Salome HOME
Sending events on attributes data changed
[modules/shaper.git] / src / Model / Model_Data.cpp
1 // File:        Model_Data.hxx
2 // Created:     21 Mar 2014
3 // Author:      Mikhail PONIKAROV
4
5 #include <Model_Data.h>
6 #include <Model_AttributeDocRef.h>
7 #include <Model_AttributeDouble.h>
8 #include <GeomData_Point.h>
9 #include <GeomData_Point2D.h>
10 #include <GeomData_Dir.h>
11 #include <TDataStd_Name.hxx>
12
13 using namespace std;
14
15 Model_Data::Model_Data()
16 {
17 }
18
19 void Model_Data::setLabel(TDF_Label& theLab)
20 {
21   myLab = theLab;
22 }
23
24 string Model_Data::getName()
25 {
26   Handle(TDataStd_Name) aName;
27   if (myLab.FindAttribute(TDataStd_Name::GetID(), aName))
28     return string(TCollection_AsciiString(aName->Get()).ToCString());
29   return ""; // not defined
30 }
31
32 void Model_Data::setName(string theName)
33 {
34   TDataStd_Name::Set(myLab, theName.c_str());
35 }
36
37 void Model_Data::addAttribute(string theID, string theAttrType)
38 {
39   TDF_Label anAttrLab = myLab.FindChild(myAttrs.size() + 1);
40   ModelAPI_Attribute* anAttr = 0;
41   if (theAttrType == ModelAPI_AttributeDocRef::type())
42     anAttr = new Model_AttributeDocRef(anAttrLab);
43   else if (theAttrType == ModelAPI_AttributeDouble::type())
44     anAttr = new Model_AttributeDouble(anAttrLab);
45   else if (theAttrType == GeomData_Point::type())
46     anAttr = new GeomData_Point(anAttrLab);
47   else if (theAttrType == GeomData_Dir::type())
48     anAttr = new GeomData_Dir(anAttrLab);
49   else if (theAttrType == GeomData_Point2D::type())
50     anAttr = new GeomData_Point2D(anAttrLab);
51
52   if (anAttr) {
53     myAttrs[theID] = boost::shared_ptr<ModelAPI_Attribute>(anAttr);
54     anAttr->setFeature(myFeature);
55   }
56   else
57     ; // TODO: generate error on unknown attribute request and/or add mechanism for customization
58 }
59
60 boost::shared_ptr<ModelAPI_AttributeDocRef> Model_Data::docRef(const string theID)
61 {
62   map<string, boost::shared_ptr<ModelAPI_Attribute> >::iterator aFound = myAttrs.find(theID);
63   if (aFound == myAttrs.end()) {
64     // TODO: generate error on unknown attribute request and/or add mechanism for customization
65     return boost::shared_ptr<ModelAPI_AttributeDocRef>();
66   }
67   boost::shared_ptr<ModelAPI_AttributeDocRef> aRes = 
68     boost::dynamic_pointer_cast<ModelAPI_AttributeDocRef>(aFound->second);
69   if (!aRes) {
70     // TODO: generate error on invalid attribute type request
71   }
72   return aRes;
73 }
74
75 boost::shared_ptr<ModelAPI_AttributeDouble> Model_Data::real(const string theID)
76 {
77   map<string, boost::shared_ptr<ModelAPI_Attribute> >::iterator aFound = myAttrs.find(theID);
78   if (aFound == myAttrs.end()) {
79     // TODO: generate error on unknown attribute request and/or add mechanism for customization
80     return boost::shared_ptr<ModelAPI_AttributeDouble>();
81   }
82   boost::shared_ptr<ModelAPI_AttributeDouble> aRes = 
83     boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(aFound->second);
84   if (!aRes) {
85     // TODO: generate error on invalid attribute type request
86   }
87   return aRes;
88 }
89
90 boost::shared_ptr<ModelAPI_Attribute> Model_Data::attribute(const std::string theID)
91 {
92   boost::shared_ptr<ModelAPI_Attribute> aResult;
93   if (myAttrs.find(theID) == myAttrs.end()) // no such attribute
94     return aResult;
95   return myAttrs[theID];
96 }