]> SALOME platform Git repositories - modules/shaper.git/blob - src/Model/Model_Data.cpp
Salome HOME
Merge branch 'master' of newgeom:newgeom.git
[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 <Model_AttributeString.h>
13 #include <Model_Events.h>
14
15 #include <GeomData_Point.h>
16 #include <GeomData_Point2D.h>
17 #include <GeomData_Dir.h>
18 #include <Events_Loop.h>
19 #include <Events_Error.h>
20
21 #include <TDataStd_Name.hxx>
22
23 Model_Data::Model_Data()
24 {
25 }
26
27 void Model_Data::setLabel(TDF_Label theLab)
28 {
29   myLab = theLab;
30 }
31
32 std::string Model_Data::name()
33 {
34   Handle(TDataStd_Name) aName;
35   if (myLab.FindAttribute(TDataStd_Name::GetID(), aName))
36     return std::string(TCollection_AsciiString(aName->Get()).ToCString());
37   return "";  // not defined
38 }
39
40 void Model_Data::setName(const std::string& theName)
41 {
42   bool isModified = false;
43   Handle(TDataStd_Name) aName;
44   if (!myLab.FindAttribute(TDataStd_Name::GetID(), aName)) {
45     TDataStd_Name::Set(myLab, theName.c_str());
46     isModified = true;
47   } else {
48     isModified = !aName->Get().IsEqual(theName.c_str());
49     if (isModified)
50       aName->Set(theName.c_str());
51   }
52   // to do not cause the update of the result on name change
53   /*if (isModified) {
54    static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
55    ModelAPI_EventCreator::get()->sendUpdated(myObject, anEvent, false);
56    }*/
57 }
58
59 void Model_Data::addAttribute(const std::string& theID, const std::string theAttrType)
60 {
61   TDF_Label anAttrLab = myLab.FindChild(myAttrs.size() + 1);
62   ModelAPI_Attribute* anAttr = 0;
63   if (theAttrType == ModelAPI_AttributeDocRef::type()) {
64     anAttr = new Model_AttributeDocRef(anAttrLab);
65   } else if (theAttrType == ModelAPI_AttributeDouble::type()) {
66     anAttr = new Model_AttributeDouble(anAttrLab);
67   } else if (theAttrType == ModelAPI_AttributeReference::type()) {
68     anAttr = new Model_AttributeReference(anAttrLab);
69   } else if (theAttrType == ModelAPI_AttributeRefAttr::type()) {
70     anAttr = new Model_AttributeRefAttr(anAttrLab);
71   } else if (theAttrType == ModelAPI_AttributeRefList::type()) {
72     anAttr = new Model_AttributeRefList(anAttrLab);
73   } else if (theAttrType == GeomData_Point::type()) {
74     anAttr = new GeomData_Point(anAttrLab);
75   } else if (theAttrType == GeomData_Dir::type()) {
76     anAttr = new GeomData_Dir(anAttrLab);
77   } else if (theAttrType == GeomData_Point2D::type()) {
78     anAttr = new GeomData_Point2D(anAttrLab);
79   } else if (theAttrType == Model_AttributeBoolean::type()) {
80     anAttr = new Model_AttributeBoolean(anAttrLab);
81   } else if (theAttrType == Model_AttributeString::type()) {
82     anAttr = new Model_AttributeString(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_AttributeBoolean> Model_Data::boolean(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_AttributeBoolean>();
128   }
129   boost::shared_ptr<ModelAPI_AttributeBoolean> aRes = boost::dynamic_pointer_cast<
130       ModelAPI_AttributeBoolean>(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_AttributeString> Model_Data::string(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_AttributeString>();
143   }
144   boost::shared_ptr<ModelAPI_AttributeString> aRes =
145       boost::dynamic_pointer_cast<ModelAPI_AttributeString>(aFound->second);
146   if (!aRes) {
147     // TODO: generate error on invalid attribute type request
148   }
149   return aRes;
150
151 }
152
153 boost::shared_ptr<ModelAPI_AttributeReference> Model_Data::reference(const std::string& theID)
154 {
155   std::map<std::string, boost::shared_ptr<ModelAPI_Attribute> >::iterator aFound = myAttrs.find(theID);
156   if (aFound == myAttrs.end()) {
157     // TODO: generate error on unknown attribute request and/or add mechanism for customization
158     return boost::shared_ptr<ModelAPI_AttributeReference>();
159   }
160   boost::shared_ptr<ModelAPI_AttributeReference> aRes = boost::dynamic_pointer_cast<
161       ModelAPI_AttributeReference>(aFound->second);
162   if (!aRes) {
163     // TODO: generate error on invalid attribute type request
164   }
165   return aRes;
166 }
167
168 boost::shared_ptr<ModelAPI_AttributeRefAttr> Model_Data::refattr(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_AttributeRefAttr>();
174   }
175   boost::shared_ptr<ModelAPI_AttributeRefAttr> aRes = boost::dynamic_pointer_cast<
176       ModelAPI_AttributeRefAttr>(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_AttributeRefList> Model_Data::reflist(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_AttributeRefList>();
189   }
190   boost::shared_ptr<ModelAPI_AttributeRefList> aRes = boost::dynamic_pointer_cast<
191       ModelAPI_AttributeRefList>(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_Attribute> Model_Data::attribute(const std::string& theID)
199 {
200   boost::shared_ptr<ModelAPI_Attribute> aResult;
201   if (myAttrs.find(theID) == myAttrs.end())  // no such attribute
202     return aResult;
203   return myAttrs[theID];
204 }
205
206 const std::string& Model_Data::id(const boost::shared_ptr<ModelAPI_Attribute>& theAttr)
207 {
208   std::map<std::string, boost::shared_ptr<ModelAPI_Attribute> >::iterator anAttr = myAttrs.begin();
209   for (; anAttr != myAttrs.end(); anAttr++) {
210     if (anAttr->second == theAttr)
211       return anAttr->first;
212   }
213   // not found
214   static std::string anEmpty;
215   return anEmpty;
216 }
217
218 bool Model_Data::isEqual(const boost::shared_ptr<ModelAPI_Data>& theData)
219 {
220   boost::shared_ptr<Model_Data> aData = boost::dynamic_pointer_cast<Model_Data>(theData);
221   if (aData)
222     return myLab.IsEqual(aData->myLab) == Standard_True ;
223   return false;
224 }
225
226 bool Model_Data::isValid()
227 {
228   return !myLab.IsNull() && myLab.HasAttribute();
229 }
230
231 std::list<boost::shared_ptr<ModelAPI_Attribute> > Model_Data::attributes(const std::string& theType)
232 {
233   std::list<boost::shared_ptr<ModelAPI_Attribute> > aResult;
234   std::map<std::string, boost::shared_ptr<ModelAPI_Attribute> >::iterator anAttrsIter = myAttrs.begin();
235   for (; anAttrsIter != myAttrs.end(); anAttrsIter++) {
236     if (theType.empty() || anAttrsIter->second->attributeType() == theType) {
237       aResult.push_back(anAttrsIter->second);
238     }
239   }
240   return aResult;
241 }
242
243 void Model_Data::sendAttributeUpdated(ModelAPI_Attribute* theAttr)
244 {
245   theAttr->setInitialized();
246   if (theAttr->isArgument()) {
247     static const Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
248     ModelAPI_EventCreator::get()->sendUpdated(myObject, anEvent);
249   }
250 }