Salome HOME
Merge branch 'master' of newgeom:newgeom
[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 <TDataStd_Name.hxx>
9
10 using namespace std;
11
12 Model_Data::Model_Data()
13 {
14 }
15
16 void Model_Data::setLabel(TDF_Label& theLab)
17 {
18   myLab = theLab;
19 }
20
21 string Model_Data::getName()
22 {
23   Handle(TDataStd_Name) aName;
24   if (myLab.FindAttribute(TDataStd_Name::GetID(), aName))
25     return string(TCollection_AsciiString(aName->Get()).ToCString());
26   return ""; // not defined
27 }
28
29 void Model_Data::setName(string theName)
30 {
31   TDataStd_Name::Set(myLab, theName.c_str());
32 }
33
34 void Model_Data::addAttribute(string theID, string theAttrType)
35 {
36   TDF_Label anAttrLab = myLab.FindChild(myAttrs.size() + 1);
37   ModelAPI_Attribute* anAttr = 0;
38   if (theAttrType == ModelAPI_AttributeDocRef::type())
39     anAttr = new Model_AttributeDocRef(anAttrLab);
40   else if (theAttrType == ModelAPI_AttributeDouble::type())
41     anAttr = new Model_AttributeDouble(anAttrLab);
42
43   if (anAttr)
44     myAttrs[theID] = boost::shared_ptr<ModelAPI_Attribute>(anAttr);
45   else
46     ; // TODO: generate error on unknown attribute request and/or add mechanism for customization
47 }
48
49 boost::shared_ptr<ModelAPI_AttributeDocRef> Model_Data::docRef(const string theID)
50 {
51   map<string, boost::shared_ptr<ModelAPI_Attribute> >::iterator aFound = myAttrs.find(theID);
52   if (aFound == myAttrs.end()) {
53     // TODO: generate error on unknown attribute request and/or add mechanism for customization
54     return boost::shared_ptr<ModelAPI_AttributeDocRef>();
55   }
56   boost::shared_ptr<ModelAPI_AttributeDocRef> aRes = 
57     boost::dynamic_pointer_cast<ModelAPI_AttributeDocRef>(aFound->second);
58   if (!aRes) {
59     // TODO: generate error on invalid attribute type request
60   }
61   return aRes;
62 }
63
64 boost::shared_ptr<ModelAPI_AttributeDouble> Model_Data::real(const string theID)
65 {
66   map<string, boost::shared_ptr<ModelAPI_Attribute> >::iterator aFound = myAttrs.find(theID);
67   if (aFound == myAttrs.end()) {
68     // TODO: generate error on unknown attribute request and/or add mechanism for customization
69     return boost::shared_ptr<ModelAPI_AttributeDouble>();
70   }
71   boost::shared_ptr<ModelAPI_AttributeDouble> aRes = 
72     boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(aFound->second);
73   if (!aRes) {
74     // TODO: generate error on invalid attribute type request
75   }
76   return aRes;
77 }