Salome HOME
b384b49d77846c29b47a856eeca3280fe9cc5bac
[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_AttributeInteger.h>
8 #include <Model_AttributeDouble.h>
9 #include <Model_AttributeReference.h>
10 #include <Model_AttributeRefAttr.h>
11 #include <Model_AttributeRefList.h>
12 #include <Model_AttributeBoolean.h>
13 #include <Model_AttributeString.h>
14 #include <Model_Events.h>
15
16 #include <GeomData_Point.h>
17 #include <GeomData_Point2D.h>
18 #include <GeomData_Dir.h>
19 #include <Events_Loop.h>
20 #include <Events_Error.h>
21
22 #include <TDataStd_Name.hxx>
23
24 #include <string>
25
26 Model_Data::Model_Data()
27 {
28 }
29
30 void Model_Data::setLabel(TDF_Label theLab)
31 {
32   myLab = theLab;
33 }
34
35 std::string Model_Data::name()
36 {
37   Handle(TDataStd_Name) aName;
38   if (myLab.FindAttribute(TDataStd_Name::GetID(), aName))
39     return std::string(TCollection_AsciiString(aName->Get()).ToCString());
40   return "";  // not defined
41 }
42
43 void Model_Data::setName(const std::string& theName)
44 {
45   bool isModified = false;
46   Handle(TDataStd_Name) aName;
47   if (!myLab.FindAttribute(TDataStd_Name::GetID(), aName)) {
48     TDataStd_Name::Set(myLab, theName.c_str());
49     isModified = true;
50   } else {
51     isModified = !aName->Get().IsEqual(theName.c_str());
52     if (isModified)
53       aName->Set(theName.c_str());
54   }
55 }
56
57 void Model_Data::addAttribute(const std::string& theID, const std::string theAttrType)
58 {
59   TDF_Label anAttrLab = myLab.FindChild(myAttrs.size() + 1);
60   ModelAPI_Attribute* anAttr = 0;
61   if (theAttrType == ModelAPI_AttributeDocRef::type()) {
62     anAttr = new Model_AttributeDocRef(anAttrLab);
63   } else if (theAttrType == Model_AttributeInteger::type()) {
64     anAttr = new Model_AttributeInteger(anAttrLab);
65   } else if (theAttrType == ModelAPI_AttributeDouble::type()) {
66     anAttr = new Model_AttributeDouble(anAttrLab);
67   } else if (theAttrType == Model_AttributeBoolean::type()) {
68     anAttr = new Model_AttributeBoolean(anAttrLab);
69   } else if (theAttrType == Model_AttributeString::type()) {
70     anAttr = new Model_AttributeString(anAttrLab);
71   } else if (theAttrType == ModelAPI_AttributeReference::type()) {
72     anAttr = new Model_AttributeReference(anAttrLab);
73   } else if (theAttrType == ModelAPI_AttributeRefAttr::type()) {
74     anAttr = new Model_AttributeRefAttr(anAttrLab);
75   } else if (theAttrType == ModelAPI_AttributeRefList::type()) {
76     anAttr = new Model_AttributeRefList(anAttrLab);
77   } else if (theAttrType == GeomData_Point::type()) {
78     anAttr = new GeomData_Point(anAttrLab);
79   } else if (theAttrType == GeomData_Dir::type()) {
80     anAttr = new GeomData_Dir(anAttrLab);
81   } else if (theAttrType == GeomData_Point2D::type()) {
82     anAttr = new GeomData_Point2D(anAttrLab);
83   }
84   if (anAttr) {
85     myAttrs[theID] = boost::shared_ptr<ModelAPI_Attribute>(anAttr);
86     anAttr->setObject(myObject);
87   } else {
88     Events_Error::send("Can not create unknown type of attribute " + theAttrType);
89   }
90 }
91
92 boost::shared_ptr<ModelAPI_AttributeDocRef> Model_Data::docRef(const std::string& theID)
93 {
94   std::map<std::string, boost::shared_ptr<ModelAPI_Attribute> >::iterator aFound = myAttrs.find(theID);
95   if (aFound == myAttrs.end()) {
96     // TODO: generate error on unknown attribute request and/or add mechanism for customization
97     return boost::shared_ptr<ModelAPI_AttributeDocRef>();
98   }
99   boost::shared_ptr<ModelAPI_AttributeDocRef> aRes = boost::dynamic_pointer_cast<
100       ModelAPI_AttributeDocRef>(aFound->second);
101   if (!aRes) {
102     // TODO: generate error on invalid attribute type request
103   }
104   return aRes;
105 }
106
107 boost::shared_ptr<ModelAPI_AttributeDouble> Model_Data::real(const std::string& theID)
108 {
109   std::map<std::string, boost::shared_ptr<ModelAPI_Attribute> >::iterator aFound = myAttrs.find(theID);
110   if (aFound == myAttrs.end()) {
111     // TODO: generate error on unknown attribute request and/or add mechanism for customization
112     return boost::shared_ptr<ModelAPI_AttributeDouble>();
113   }
114   boost::shared_ptr<ModelAPI_AttributeDouble> aRes = boost::dynamic_pointer_cast<
115       ModelAPI_AttributeDouble>(aFound->second);
116   if (!aRes) {
117     // TODO: generate error on invalid attribute type request
118   }
119   return aRes;
120 }
121
122 boost::shared_ptr<ModelAPI_AttributeInteger> Model_Data::integer(const std::string& theID)
123 {
124   std::map<std::string, boost::shared_ptr<ModelAPI_Attribute> >::iterator aFound = myAttrs.find(theID);
125   if (aFound == myAttrs.end()) {
126     // TODO: generate error on unknown attribute request and/or add mechanism for customization
127     return boost::shared_ptr<ModelAPI_AttributeInteger>();
128   }
129   boost::shared_ptr<ModelAPI_AttributeInteger> aRes = boost::dynamic_pointer_cast<
130       ModelAPI_AttributeInteger>(aFound->second);
131   if (!aRes) {
132     // TODO: generate error on invalid attribute type request
133   }
134   return aRes;
135 }
136
137 boost::shared_ptr<ModelAPI_AttributeBoolean> Model_Data::boolean(const std::string& theID)
138 {
139   std::map<std::string, boost::shared_ptr<ModelAPI_Attribute> >::iterator aFound = myAttrs.find(theID);
140   if (aFound == myAttrs.end()) {
141     // TODO: generate error on unknown attribute request and/or add mechanism for customization
142     return boost::shared_ptr<ModelAPI_AttributeBoolean>();
143   }
144   boost::shared_ptr<ModelAPI_AttributeBoolean> aRes = boost::dynamic_pointer_cast<
145       ModelAPI_AttributeBoolean>(aFound->second);
146   if (!aRes) {
147     // TODO: generate error on invalid attribute type request
148   }
149   return aRes;
150 }
151
152 boost::shared_ptr<ModelAPI_AttributeString> Model_Data::string(const std::string& theID)
153 {
154   std::map<std::string, boost::shared_ptr<ModelAPI_Attribute> >::iterator aFound = myAttrs.find(theID);
155   if (aFound == myAttrs.end()) {
156     // TODO: generate error on unknown attribute request and/or add mechanism for customization
157     return boost::shared_ptr<ModelAPI_AttributeString>();
158   }
159   boost::shared_ptr<ModelAPI_AttributeString> aRes =
160       boost::dynamic_pointer_cast<ModelAPI_AttributeString>(aFound->second);
161   if (!aRes) {
162     // TODO: generate error on invalid attribute type request
163   }
164   return aRes;
165
166 }
167
168 boost::shared_ptr<ModelAPI_AttributeReference> Model_Data::reference(const std::string& theID)
169 {
170   std::map<std::string, boost::shared_ptr<ModelAPI_Attribute> >::iterator aFound = myAttrs.find(theID);
171   if (aFound == myAttrs.end()) {
172     // TODO: generate error on unknown attribute request and/or add mechanism for customization
173     return boost::shared_ptr<ModelAPI_AttributeReference>();
174   }
175   boost::shared_ptr<ModelAPI_AttributeReference> aRes = boost::dynamic_pointer_cast<
176       ModelAPI_AttributeReference>(aFound->second);
177   if (!aRes) {
178     // TODO: generate error on invalid attribute type request
179   }
180   return aRes;
181 }
182
183 boost::shared_ptr<ModelAPI_AttributeRefAttr> Model_Data::refattr(const std::string& theID)
184 {
185   std::map<std::string, boost::shared_ptr<ModelAPI_Attribute> >::iterator aFound = myAttrs.find(theID);
186   if (aFound == myAttrs.end()) {
187     // TODO: generate error on unknown attribute request and/or add mechanism for customization
188     return boost::shared_ptr<ModelAPI_AttributeRefAttr>();
189   }
190   boost::shared_ptr<ModelAPI_AttributeRefAttr> aRes = boost::dynamic_pointer_cast<
191       ModelAPI_AttributeRefAttr>(aFound->second);
192   if (!aRes) {
193     // TODO: generate error on invalid attribute type request
194   }
195   return aRes;
196 }
197
198 boost::shared_ptr<ModelAPI_AttributeRefList> Model_Data::reflist(const std::string& theID)
199 {
200   std::map<std::string, boost::shared_ptr<ModelAPI_Attribute> >::iterator aFound = myAttrs.find(theID);
201   if (aFound == myAttrs.end()) {
202     // TODO: generate error on unknown attribute request and/or add mechanism for customization
203     return boost::shared_ptr<ModelAPI_AttributeRefList>();
204   }
205   boost::shared_ptr<ModelAPI_AttributeRefList> aRes = boost::dynamic_pointer_cast<
206       ModelAPI_AttributeRefList>(aFound->second);
207   if (!aRes) {
208     // TODO: generate error on invalid attribute type request
209   }
210   return aRes;
211 }
212
213 boost::shared_ptr<ModelAPI_Attribute> Model_Data::attribute(const std::string& theID)
214 {
215   boost::shared_ptr<ModelAPI_Attribute> aResult;
216   if (myAttrs.find(theID) == myAttrs.end())  // no such attribute
217     return aResult;
218   return myAttrs[theID];
219 }
220
221 const std::string& Model_Data::id(const boost::shared_ptr<ModelAPI_Attribute>& theAttr)
222 {
223   std::map<std::string, boost::shared_ptr<ModelAPI_Attribute> >::iterator anAttr = myAttrs.begin();
224   for (; anAttr != myAttrs.end(); anAttr++) {
225     if (anAttr->second == theAttr)
226       return anAttr->first;
227   }
228   // not found
229   static std::string anEmpty;
230   return anEmpty;
231 }
232
233 bool Model_Data::isEqual(const boost::shared_ptr<ModelAPI_Data>& theData)
234 {
235   boost::shared_ptr<Model_Data> aData = boost::dynamic_pointer_cast<Model_Data>(theData);
236   if (aData)
237     return myLab.IsEqual(aData->myLab) == Standard_True ;
238   return false;
239 }
240
241 bool Model_Data::isValid()
242 {
243   return !myLab.IsNull() && myLab.HasAttribute();
244 }
245
246 std::list<boost::shared_ptr<ModelAPI_Attribute> > Model_Data::attributes(const std::string& theType)
247 {
248   std::list<boost::shared_ptr<ModelAPI_Attribute> > aResult;
249   std::map<std::string, boost::shared_ptr<ModelAPI_Attribute> >::iterator anAttrsIter = 
250     myAttrs.begin();
251   for (; anAttrsIter != myAttrs.end(); anAttrsIter++) {
252     if (theType.empty() || anAttrsIter->second->attributeType() == theType) {
253       aResult.push_back(anAttrsIter->second);
254     }
255   }
256   return aResult;
257 }
258
259 std::list<std::string> Model_Data::attributesIDs(const std::string& theType) 
260 {
261   std::list<std::string> aResult;
262   std::map<std::string, boost::shared_ptr<ModelAPI_Attribute> >::iterator anAttrsIter = 
263     myAttrs.begin();
264   for (; anAttrsIter != myAttrs.end(); anAttrsIter++) {
265     if (theType.empty() || anAttrsIter->second->attributeType() == theType) {
266       aResult.push_back(anAttrsIter->first);
267     }
268   }
269   return aResult;
270 }
271
272 void Model_Data::sendAttributeUpdated(ModelAPI_Attribute* theAttr)
273 {
274   theAttr->setInitialized();
275   if (theAttr->isArgument()) {
276     static const Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
277     ModelAPI_EventCreator::get()->sendUpdated(myObject, anEvent);
278   }
279 }
280
281 void Model_Data::erase()
282 {
283   if (!myLab.IsNull())
284     myLab.ForgetAllAttributes();
285 }