Salome HOME
List of references attribute and usage of it in the sketch
[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 <Model_AttributeReference.h>
9 #include <Model_AttributeRefAttr.h>
10 #include <Model_AttributeRefList.h>
11 #include <GeomData_Point.h>
12 #include <GeomData_Point2D.h>
13 #include <GeomData_Dir.h>
14 #include <TDataStd_Name.hxx>
15
16 using namespace std;
17
18 Model_Data::Model_Data()
19 {
20 }
21
22 void Model_Data::setLabel(TDF_Label& theLab)
23 {
24   myLab = theLab;
25 }
26
27 string Model_Data::getName()
28 {
29   Handle(TDataStd_Name) aName;
30   if (myLab.FindAttribute(TDataStd_Name::GetID(), aName))
31     return string(TCollection_AsciiString(aName->Get()).ToCString());
32   return ""; // not defined
33 }
34
35 void Model_Data::setName(string theName)
36 {
37   TDataStd_Name::Set(myLab, theName.c_str());
38 }
39
40 void Model_Data::addAttribute(string theID, string theAttrType)
41 {
42   TDF_Label anAttrLab = myLab.FindChild(myAttrs.size() + 1);
43   ModelAPI_Attribute* anAttr = 0;
44   if (theAttrType == ModelAPI_AttributeDocRef::type())
45     anAttr = new Model_AttributeDocRef(anAttrLab);
46   else if (theAttrType == ModelAPI_AttributeDouble::type())
47     anAttr = new Model_AttributeDouble(anAttrLab);
48   else if (theAttrType == ModelAPI_AttributeReference::type())
49     anAttr = new Model_AttributeReference(anAttrLab);
50   else if (theAttrType == ModelAPI_AttributeRefAttr::type())
51     anAttr = new Model_AttributeRefAttr(anAttrLab);
52   else if (theAttrType == ModelAPI_AttributeRefList::type())
53     anAttr = new Model_AttributeRefList(anAttrLab);
54   else if (theAttrType == GeomData_Point::type())
55     anAttr = new GeomData_Point(anAttrLab);
56   else if (theAttrType == GeomData_Dir::type())
57     anAttr = new GeomData_Dir(anAttrLab);
58   else if (theAttrType == GeomData_Point2D::type())
59     anAttr = new GeomData_Point2D(anAttrLab);
60
61   if (anAttr) {
62     myAttrs[theID] = boost::shared_ptr<ModelAPI_Attribute>(anAttr);
63     anAttr->setFeature(myFeature);
64   }
65   else
66     ; // TODO: generate error on unknown attribute request and/or add mechanism for customization
67 }
68
69 boost::shared_ptr<ModelAPI_AttributeDocRef> Model_Data::docRef(const string theID)
70 {
71   map<string, boost::shared_ptr<ModelAPI_Attribute> >::iterator aFound = myAttrs.find(theID);
72   if (aFound == myAttrs.end()) {
73     // TODO: generate error on unknown attribute request and/or add mechanism for customization
74     return boost::shared_ptr<ModelAPI_AttributeDocRef>();
75   }
76   boost::shared_ptr<ModelAPI_AttributeDocRef> aRes = 
77     boost::dynamic_pointer_cast<ModelAPI_AttributeDocRef>(aFound->second);
78   if (!aRes) {
79     // TODO: generate error on invalid attribute type request
80   }
81   return aRes;
82 }
83
84 boost::shared_ptr<ModelAPI_AttributeDouble> Model_Data::real(const string theID)
85 {
86   map<string, boost::shared_ptr<ModelAPI_Attribute> >::iterator aFound = myAttrs.find(theID);
87   if (aFound == myAttrs.end()) {
88     // TODO: generate error on unknown attribute request and/or add mechanism for customization
89     return boost::shared_ptr<ModelAPI_AttributeDouble>();
90   }
91   boost::shared_ptr<ModelAPI_AttributeDouble> aRes = 
92     boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(aFound->second);
93   if (!aRes) {
94     // TODO: generate error on invalid attribute type request
95   }
96   return aRes;
97 }
98
99 boost::shared_ptr<ModelAPI_AttributeReference> Model_Data::reference(const string theID)
100 {
101   map<string, boost::shared_ptr<ModelAPI_Attribute> >::iterator aFound = myAttrs.find(theID);
102   if (aFound == myAttrs.end()) {
103     // TODO: generate error on unknown attribute request and/or add mechanism for customization
104     return boost::shared_ptr<ModelAPI_AttributeReference>();
105   }
106   boost::shared_ptr<ModelAPI_AttributeReference> aRes = 
107     boost::dynamic_pointer_cast<ModelAPI_AttributeReference>(aFound->second);
108   if (!aRes) {
109     // TODO: generate error on invalid attribute type request
110   }
111   return aRes;
112 }
113
114 boost::shared_ptr<ModelAPI_AttributeRefAttr> Model_Data::refattr(const string theID)
115 {
116   map<string, boost::shared_ptr<ModelAPI_Attribute> >::iterator aFound = myAttrs.find(theID);
117   if (aFound == myAttrs.end()) {
118     // TODO: generate error on unknown attribute request and/or add mechanism for customization
119     return boost::shared_ptr<ModelAPI_AttributeRefAttr>();
120   }
121   boost::shared_ptr<ModelAPI_AttributeRefAttr> aRes = 
122     boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aFound->second);
123   if (!aRes) {
124     // TODO: generate error on invalid attribute type request
125   }
126   return aRes;
127 }
128
129 boost::shared_ptr<ModelAPI_AttributeRefList> Model_Data::reflist(const string theID)
130 {
131   map<string, boost::shared_ptr<ModelAPI_Attribute> >::iterator aFound = myAttrs.find(theID);
132   if (aFound == myAttrs.end()) {
133     // TODO: generate error on unknown attribute request and/or add mechanism for customization
134     return boost::shared_ptr<ModelAPI_AttributeRefList>();
135   }
136   boost::shared_ptr<ModelAPI_AttributeRefList> aRes = 
137     boost::dynamic_pointer_cast<ModelAPI_AttributeRefList>(aFound->second);
138   if (!aRes) {
139     // TODO: generate error on invalid attribute type request
140   }
141   return aRes;
142 }
143
144 boost::shared_ptr<ModelAPI_Attribute> Model_Data::attribute(const std::string theID)
145 {
146   boost::shared_ptr<ModelAPI_Attribute> aResult;
147   if (myAttrs.find(theID) == myAttrs.end()) // no such attribute
148     return aResult;
149   return myAttrs[theID];
150 }
151
152 const string& Model_Data::id(const boost::shared_ptr<ModelAPI_Attribute> theAttr)
153 {
154   map<string, boost::shared_ptr<ModelAPI_Attribute> >::iterator anAttr = myAttrs.begin();
155   for(; anAttr != myAttrs.end(); anAttr++) {
156     if (anAttr->second == theAttr) return anAttr->first;
157   }
158   // not found
159   static string anEmpty;
160   return anEmpty;
161 }