Salome HOME
d97975153fc87bd3f3bfd72cf710da011ff86d5b
[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   // to do not cause the update of the result on name change
56   /*if (isModified) {
57    static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
58    ModelAPI_EventCreator::get()->sendUpdated(myObject, anEvent, false);
59    }*/
60 }
61
62 void Model_Data::addAttribute(const std::string& theID, const std::string theAttrType)
63 {
64   TDF_Label anAttrLab = myLab.FindChild(myAttrs.size() + 1);
65   ModelAPI_Attribute* anAttr = 0;
66   if (theAttrType == ModelAPI_AttributeDocRef::type()) {
67     anAttr = new Model_AttributeDocRef(anAttrLab);
68   } else if (theAttrType == Model_AttributeInteger::type()) {
69     anAttr = new Model_AttributeInteger(anAttrLab);
70   } else if (theAttrType == ModelAPI_AttributeDouble::type()) {
71     anAttr = new Model_AttributeDouble(anAttrLab);
72   } else if (theAttrType == Model_AttributeBoolean::type()) {
73     anAttr = new Model_AttributeBoolean(anAttrLab);
74   } else if (theAttrType == Model_AttributeString::type()) {
75     anAttr = new Model_AttributeString(anAttrLab);
76   } else if (theAttrType == ModelAPI_AttributeReference::type()) {
77     anAttr = new Model_AttributeReference(anAttrLab);
78   } else if (theAttrType == ModelAPI_AttributeRefAttr::type()) {
79     anAttr = new Model_AttributeRefAttr(anAttrLab);
80   } else if (theAttrType == ModelAPI_AttributeRefList::type()) {
81     anAttr = new Model_AttributeRefList(anAttrLab);
82   } else if (theAttrType == GeomData_Point::type()) {
83     anAttr = new GeomData_Point(anAttrLab);
84   } else if (theAttrType == GeomData_Dir::type()) {
85     anAttr = new GeomData_Dir(anAttrLab);
86   } else if (theAttrType == GeomData_Point2D::type()) {
87     anAttr = new GeomData_Point2D(anAttrLab);
88   }
89   if (anAttr) {
90     myAttrs[theID] = boost::shared_ptr<ModelAPI_Attribute>(anAttr);
91     anAttr->setObject(myObject);
92   } else {
93     Events_Error::send("Can not create unknown type of attribute " + theAttrType);
94   }
95 }
96
97 boost::shared_ptr<ModelAPI_AttributeDocRef> Model_Data::docRef(const std::string& theID)
98 {
99   std::map<std::string, boost::shared_ptr<ModelAPI_Attribute> >::iterator aFound = myAttrs.find(theID);
100   if (aFound == myAttrs.end()) {
101     // TODO: generate error on unknown attribute request and/or add mechanism for customization
102     return boost::shared_ptr<ModelAPI_AttributeDocRef>();
103   }
104   boost::shared_ptr<ModelAPI_AttributeDocRef> aRes = boost::dynamic_pointer_cast<
105       ModelAPI_AttributeDocRef>(aFound->second);
106   if (!aRes) {
107     // TODO: generate error on invalid attribute type request
108   }
109   return aRes;
110 }
111
112 boost::shared_ptr<ModelAPI_AttributeDouble> Model_Data::real(const std::string& theID)
113 {
114   std::map<std::string, boost::shared_ptr<ModelAPI_Attribute> >::iterator aFound = myAttrs.find(theID);
115   if (aFound == myAttrs.end()) {
116     // TODO: generate error on unknown attribute request and/or add mechanism for customization
117     return boost::shared_ptr<ModelAPI_AttributeDouble>();
118   }
119   boost::shared_ptr<ModelAPI_AttributeDouble> aRes = boost::dynamic_pointer_cast<
120       ModelAPI_AttributeDouble>(aFound->second);
121   if (!aRes) {
122     // TODO: generate error on invalid attribute type request
123   }
124   return aRes;
125 }
126
127 boost::shared_ptr<ModelAPI_AttributeInteger> Model_Data::integer(const std::string& theID)
128 {
129   std::map<std::string, boost::shared_ptr<ModelAPI_Attribute> >::iterator aFound = myAttrs.find(theID);
130   if (aFound == myAttrs.end()) {
131     // TODO: generate error on unknown attribute request and/or add mechanism for customization
132     return boost::shared_ptr<ModelAPI_AttributeInteger>();
133   }
134   boost::shared_ptr<ModelAPI_AttributeInteger> aRes = boost::dynamic_pointer_cast<
135       ModelAPI_AttributeInteger>(aFound->second);
136   if (!aRes) {
137     // TODO: generate error on invalid attribute type request
138   }
139   return aRes;
140 }
141
142 boost::shared_ptr<ModelAPI_AttributeBoolean> Model_Data::boolean(const std::string& theID)
143 {
144   std::map<std::string, boost::shared_ptr<ModelAPI_Attribute> >::iterator aFound = myAttrs.find(theID);
145   if (aFound == myAttrs.end()) {
146     // TODO: generate error on unknown attribute request and/or add mechanism for customization
147     return boost::shared_ptr<ModelAPI_AttributeBoolean>();
148   }
149   boost::shared_ptr<ModelAPI_AttributeBoolean> aRes = boost::dynamic_pointer_cast<
150       ModelAPI_AttributeBoolean>(aFound->second);
151   if (!aRes) {
152     // TODO: generate error on invalid attribute type request
153   }
154   return aRes;
155 }
156
157 boost::shared_ptr<ModelAPI_AttributeString> Model_Data::string(const std::string& theID)
158 {
159   std::map<std::string, boost::shared_ptr<ModelAPI_Attribute> >::iterator aFound = myAttrs.find(theID);
160   if (aFound == myAttrs.end()) {
161     // TODO: generate error on unknown attribute request and/or add mechanism for customization
162     return boost::shared_ptr<ModelAPI_AttributeString>();
163   }
164   boost::shared_ptr<ModelAPI_AttributeString> aRes =
165       boost::dynamic_pointer_cast<ModelAPI_AttributeString>(aFound->second);
166   if (!aRes) {
167     // TODO: generate error on invalid attribute type request
168   }
169   return aRes;
170
171 }
172
173 boost::shared_ptr<ModelAPI_AttributeReference> Model_Data::reference(const std::string& theID)
174 {
175   std::map<std::string, boost::shared_ptr<ModelAPI_Attribute> >::iterator aFound = myAttrs.find(theID);
176   if (aFound == myAttrs.end()) {
177     // TODO: generate error on unknown attribute request and/or add mechanism for customization
178     return boost::shared_ptr<ModelAPI_AttributeReference>();
179   }
180   boost::shared_ptr<ModelAPI_AttributeReference> aRes = boost::dynamic_pointer_cast<
181       ModelAPI_AttributeReference>(aFound->second);
182   if (!aRes) {
183     // TODO: generate error on invalid attribute type request
184   }
185   return aRes;
186 }
187
188 boost::shared_ptr<ModelAPI_AttributeRefAttr> Model_Data::refattr(const std::string& theID)
189 {
190   std::map<std::string, boost::shared_ptr<ModelAPI_Attribute> >::iterator aFound = myAttrs.find(theID);
191   if (aFound == myAttrs.end()) {
192     // TODO: generate error on unknown attribute request and/or add mechanism for customization
193     return boost::shared_ptr<ModelAPI_AttributeRefAttr>();
194   }
195   boost::shared_ptr<ModelAPI_AttributeRefAttr> aRes = boost::dynamic_pointer_cast<
196       ModelAPI_AttributeRefAttr>(aFound->second);
197   if (!aRes) {
198     // TODO: generate error on invalid attribute type request
199   }
200   return aRes;
201 }
202
203 boost::shared_ptr<ModelAPI_AttributeRefList> Model_Data::reflist(const std::string& theID)
204 {
205   std::map<std::string, boost::shared_ptr<ModelAPI_Attribute> >::iterator aFound = myAttrs.find(theID);
206   if (aFound == myAttrs.end()) {
207     // TODO: generate error on unknown attribute request and/or add mechanism for customization
208     return boost::shared_ptr<ModelAPI_AttributeRefList>();
209   }
210   boost::shared_ptr<ModelAPI_AttributeRefList> aRes = boost::dynamic_pointer_cast<
211       ModelAPI_AttributeRefList>(aFound->second);
212   if (!aRes) {
213     // TODO: generate error on invalid attribute type request
214   }
215   return aRes;
216 }
217
218 boost::shared_ptr<ModelAPI_Attribute> Model_Data::attribute(const std::string& theID)
219 {
220   boost::shared_ptr<ModelAPI_Attribute> aResult;
221   if (myAttrs.find(theID) == myAttrs.end())  // no such attribute
222     return aResult;
223   return myAttrs[theID];
224 }
225
226 const std::string& Model_Data::id(const boost::shared_ptr<ModelAPI_Attribute>& theAttr)
227 {
228   std::map<std::string, boost::shared_ptr<ModelAPI_Attribute> >::iterator anAttr = myAttrs.begin();
229   for (; anAttr != myAttrs.end(); anAttr++) {
230     if (anAttr->second == theAttr)
231       return anAttr->first;
232   }
233   // not found
234   static std::string anEmpty;
235   return anEmpty;
236 }
237
238 bool Model_Data::isEqual(const boost::shared_ptr<ModelAPI_Data>& theData)
239 {
240   boost::shared_ptr<Model_Data> aData = boost::dynamic_pointer_cast<Model_Data>(theData);
241   if (aData)
242     return myLab.IsEqual(aData->myLab) == Standard_True ;
243   return false;
244 }
245
246 bool Model_Data::isValid()
247 {
248   return !myLab.IsNull() && myLab.HasAttribute();
249 }
250
251 std::list<boost::shared_ptr<ModelAPI_Attribute> > Model_Data::attributes(const std::string& theType)
252 {
253   std::list<boost::shared_ptr<ModelAPI_Attribute> > aResult;
254   std::map<std::string, boost::shared_ptr<ModelAPI_Attribute> >::iterator anAttrsIter = myAttrs.begin();
255   for (; anAttrsIter != myAttrs.end(); anAttrsIter++) {
256     if (theType.empty() || anAttrsIter->second->attributeType() == theType) {
257       aResult.push_back(anAttrsIter->second);
258     }
259   }
260   return aResult;
261 }
262
263 void Model_Data::sendAttributeUpdated(ModelAPI_Attribute* theAttr)
264 {
265   theAttr->setInitialized();
266   if (theAttr->isArgument()) {
267     static const Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
268     ModelAPI_EventCreator::get()->sendUpdated(myObject, anEvent);
269   }
270 }