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 <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   else
55     ; // TODO: generate error on unknown attribute request and/or add mechanism for customization
56 }
57
58 boost::shared_ptr<ModelAPI_AttributeDocRef> Model_Data::docRef(const string theID)
59 {
60   map<string, boost::shared_ptr<ModelAPI_Attribute> >::iterator aFound = myAttrs.find(theID);
61   if (aFound == myAttrs.end()) {
62     // TODO: generate error on unknown attribute request and/or add mechanism for customization
63     return boost::shared_ptr<ModelAPI_AttributeDocRef>();
64   }
65   boost::shared_ptr<ModelAPI_AttributeDocRef> aRes = 
66     boost::dynamic_pointer_cast<ModelAPI_AttributeDocRef>(aFound->second);
67   if (!aRes) {
68     // TODO: generate error on invalid attribute type request
69   }
70   return aRes;
71 }
72
73 boost::shared_ptr<ModelAPI_AttributeDouble> Model_Data::real(const string theID)
74 {
75   map<string, boost::shared_ptr<ModelAPI_Attribute> >::iterator aFound = myAttrs.find(theID);
76   if (aFound == myAttrs.end()) {
77     // TODO: generate error on unknown attribute request and/or add mechanism for customization
78     return boost::shared_ptr<ModelAPI_AttributeDouble>();
79   }
80   boost::shared_ptr<ModelAPI_AttributeDouble> aRes = 
81     boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(aFound->second);
82   if (!aRes) {
83     // TODO: generate error on invalid attribute type request
84   }
85   return aRes;
86 }
87
88 boost::shared_ptr<ModelAPI_Attribute> Model_Data::attribute(const std::string theID)
89 {
90   boost::shared_ptr<ModelAPI_Attribute> aResult;
91   if (myAttrs.find(theID) == myAttrs.end()) // no such attribute
92     return aResult;
93   return myAttrs[theID];
94 }