Salome HOME
592fc75db9d6479b27c06f869c95ec26246ed269
[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 <Model_AttributeBoolean.h>
12 #include <GeomData_Point.h>
13 #include <GeomData_Point2D.h>
14 #include <GeomData_Dir.h>
15 #include <TDataStd_Name.hxx>
16 #include "Model_Events.h"
17 #include <Events_Loop.h>
18 #include <Events_Error.h>
19
20 using namespace std;
21
22 Model_Data::Model_Data()
23 {
24 }
25
26 void Model_Data::setLabel(TDF_Label theLab)
27 {
28   myLab = theLab;
29 }
30
31 string Model_Data::name()
32 {
33   Handle(TDataStd_Name) aName;
34   if (myLab.FindAttribute(TDataStd_Name::GetID(), aName))
35     return string(TCollection_AsciiString(aName->Get()).ToCString());
36   return "";  // not defined
37 }
38
39 void Model_Data::setName(const string& theName)
40 {
41   bool isModified = false;
42   Handle(TDataStd_Name) aName;
43   if (!myLab.FindAttribute(TDataStd_Name::GetID(), aName)) {
44     TDataStd_Name::Set(myLab, theName.c_str());
45     isModified = true;
46   } else {
47     isModified = !aName->Get().IsEqual(theName.c_str());
48     if (isModified)
49       aName->Set(theName.c_str());
50   }
51   // to do not cause the update of the result on name change
52   /*if (isModified) {
53    static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
54    ModelAPI_EventCreator::get()->sendUpdated(myObject, anEvent, false);
55    }*/
56 }
57
58 void Model_Data::addAttribute(const string& theID, const string theAttrType)
59 {
60   TDF_Label anAttrLab = myLab.FindChild(myAttrs.size() + 1);
61   ModelAPI_Attribute* anAttr = 0;
62   if (theAttrType == ModelAPI_AttributeDocRef::type())
63     anAttr = new Model_AttributeDocRef(anAttrLab);
64   else if (theAttrType == ModelAPI_AttributeDouble::type())
65     anAttr = new Model_AttributeDouble(anAttrLab);
66   else if (theAttrType == ModelAPI_AttributeReference::type())
67     anAttr = new Model_AttributeReference(anAttrLab);
68   else if (theAttrType == ModelAPI_AttributeRefAttr::type())
69     anAttr = new Model_AttributeRefAttr(anAttrLab);
70   else if (theAttrType == ModelAPI_AttributeRefList::type())
71     anAttr = new Model_AttributeRefList(anAttrLab);
72   else if (theAttrType == GeomData_Point::type())
73     anAttr = new GeomData_Point(anAttrLab);
74   else if (theAttrType == GeomData_Dir::type())
75     anAttr = new GeomData_Dir(anAttrLab);
76   else if (theAttrType == GeomData_Point2D::type())
77     anAttr = new GeomData_Point2D(anAttrLab);
78   else if (theAttrType == Model_AttributeBoolean::type())
79     anAttr = new Model_AttributeBoolean(anAttrLab);
80
81   if (anAttr) {
82     myAttrs[theID] = boost::shared_ptr<ModelAPI_Attribute>(anAttr);
83     anAttr->setObject(myObject);
84   } else {
85     Events_Error::send("Can not create unknown type of attribute " + theAttrType);
86   }
87 }
88
89 boost::shared_ptr<ModelAPI_AttributeDocRef> Model_Data::docRef(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_AttributeDocRef>();
95   }
96   boost::shared_ptr<ModelAPI_AttributeDocRef> aRes = boost::dynamic_pointer_cast<
97       ModelAPI_AttributeDocRef>(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_AttributeDouble> Model_Data::real(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_AttributeDouble>();
110   }
111   boost::shared_ptr<ModelAPI_AttributeDouble> aRes = boost::dynamic_pointer_cast<
112       ModelAPI_AttributeDouble>(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_AttributeBoolean> Model_Data::boolean(const std::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_AttributeBoolean>();
125   }
126   boost::shared_ptr<ModelAPI_AttributeBoolean> aRes = boost::dynamic_pointer_cast<
127       ModelAPI_AttributeBoolean>(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_AttributeReference> Model_Data::reference(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_AttributeReference>();
140   }
141   boost::shared_ptr<ModelAPI_AttributeReference> aRes = boost::dynamic_pointer_cast<
142       ModelAPI_AttributeReference>(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_AttributeRefAttr> Model_Data::refattr(const string& theID)
150 {
151   map<string, boost::shared_ptr<ModelAPI_Attribute> >::iterator aFound = myAttrs.find(theID);
152   if (aFound == myAttrs.end()) {
153     // TODO: generate error on unknown attribute request and/or add mechanism for customization
154     return boost::shared_ptr<ModelAPI_AttributeRefAttr>();
155   }
156   boost::shared_ptr<ModelAPI_AttributeRefAttr> aRes = boost::dynamic_pointer_cast<
157       ModelAPI_AttributeRefAttr>(aFound->second);
158   if (!aRes) {
159     // TODO: generate error on invalid attribute type request
160   }
161   return aRes;
162 }
163
164 boost::shared_ptr<ModelAPI_AttributeRefList> Model_Data::reflist(const string& theID)
165 {
166   map<string, boost::shared_ptr<ModelAPI_Attribute> >::iterator aFound = myAttrs.find(theID);
167   if (aFound == myAttrs.end()) {
168     // TODO: generate error on unknown attribute request and/or add mechanism for customization
169     return boost::shared_ptr<ModelAPI_AttributeRefList>();
170   }
171   boost::shared_ptr<ModelAPI_AttributeRefList> aRes = boost::dynamic_pointer_cast<
172       ModelAPI_AttributeRefList>(aFound->second);
173   if (!aRes) {
174     // TODO: generate error on invalid attribute type request
175   }
176   return aRes;
177 }
178
179 boost::shared_ptr<ModelAPI_Attribute> Model_Data::attribute(const std::string& theID)
180 {
181   boost::shared_ptr<ModelAPI_Attribute> aResult;
182   if (myAttrs.find(theID) == myAttrs.end())  // no such attribute
183     return aResult;
184   return myAttrs[theID];
185 }
186
187 const string& Model_Data::id(const boost::shared_ptr<ModelAPI_Attribute>& theAttr)
188 {
189   map<string, boost::shared_ptr<ModelAPI_Attribute> >::iterator anAttr = myAttrs.begin();
190   for (; anAttr != myAttrs.end(); anAttr++) {
191     if (anAttr->second == theAttr)
192       return anAttr->first;
193   }
194   // not found
195   static string anEmpty;
196   return anEmpty;
197 }
198
199 bool Model_Data::isEqual(const boost::shared_ptr<ModelAPI_Data>& theData)
200 {
201   boost::shared_ptr<Model_Data> aData = boost::dynamic_pointer_cast<Model_Data>(theData);
202   if (aData)
203     return myLab.IsEqual(aData->myLab) == Standard_True ;
204   return false;
205 }
206
207 bool Model_Data::isValid()
208 {
209   return !myLab.IsNull() && myLab.HasAttribute();
210 }
211
212 list<boost::shared_ptr<ModelAPI_Attribute> > Model_Data::attributes(const string& theType)
213 {
214   list<boost::shared_ptr<ModelAPI_Attribute> > aResult;
215   map<string, boost::shared_ptr<ModelAPI_Attribute> >::iterator anAttrsIter = myAttrs.begin();
216   for (; anAttrsIter != myAttrs.end(); anAttrsIter++) {
217     if (theType.empty() || anAttrsIter->second->attributeType() == theType) {
218       aResult.push_back(anAttrsIter->second);
219     }
220   }
221   return aResult;
222 }
223
224 void Model_Data::sendAttributeUpdated(ModelAPI_Attribute* theAttr)
225 {
226   theAttr->setInitialized();
227   if (theAttr->isArgument()) {
228     static const Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
229     ModelAPI_EventCreator::get()->sendUpdated(myObject, anEvent);
230   }
231 }