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