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