1 // File: Model_Data.hxx
2 // Created: 21 Mar 2014
3 // Author: Mikhail PONIKAROV
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_AttributeSelection.h>
15 #include <Model_AttributeSelectionList.h>
16 #include <Model_Events.h>
17 #include <ModelAPI_Feature.h>
18 #include <ModelAPI_Result.h>
20 #include <GeomData_Point.h>
21 #include <GeomData_Point2D.h>
22 #include <GeomData_Dir.h>
23 #include <Events_Loop.h>
24 #include <Events_Error.h>
26 #include <TDataStd_Name.hxx>
27 #include <TDataStd_UAttribute.hxx>
31 Model_Data::Model_Data()
35 void Model_Data::setLabel(TDF_Label theLab)
40 std::string Model_Data::name()
42 Handle(TDataStd_Name) aName;
43 if (myLab.FindAttribute(TDataStd_Name::GetID(), aName))
44 return std::string(TCollection_AsciiString(aName->Get()).ToCString());
45 return ""; // not defined
48 void Model_Data::setName(const std::string& theName)
50 bool isModified = false;
51 Handle(TDataStd_Name) aName;
52 if (!myLab.FindAttribute(TDataStd_Name::GetID(), aName)) {
53 TDataStd_Name::Set(myLab, theName.c_str());
56 isModified = !aName->Get().IsEqual(theName.c_str());
58 aName->Set(theName.c_str());
62 void Model_Data::addAttribute(const std::string& theID, const std::string theAttrType)
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_AttributeSelection::type()) {
79 anAttr = new Model_AttributeSelection(anAttrLab);
80 } else if (theAttrType == ModelAPI_AttributeSelectionList::type()) {
81 anAttr = new Model_AttributeSelectionList(anAttrLab);
82 } else if (theAttrType == ModelAPI_AttributeRefAttr::type()) {
83 anAttr = new Model_AttributeRefAttr(anAttrLab);
84 } else if (theAttrType == ModelAPI_AttributeRefList::type()) {
85 anAttr = new Model_AttributeRefList(anAttrLab);
86 } else if (theAttrType == GeomData_Point::type()) {
87 anAttr = new GeomData_Point(anAttrLab);
88 } else if (theAttrType == GeomData_Dir::type()) {
89 anAttr = new GeomData_Dir(anAttrLab);
90 } else if (theAttrType == GeomData_Point2D::type()) {
91 anAttr = new GeomData_Point2D(anAttrLab);
94 myAttrs[theID] = boost::shared_ptr<ModelAPI_Attribute>(anAttr);
95 anAttr->setObject(myObject);
97 Events_Error::send("Can not create unknown type of attribute " + theAttrType);
101 boost::shared_ptr<ModelAPI_AttributeDocRef> Model_Data::document(const std::string& theID)
103 std::map<std::string, boost::shared_ptr<ModelAPI_Attribute> >::iterator aFound =
105 if (aFound == myAttrs.end()) {
106 // TODO: generate error on unknown attribute request and/or add mechanism for customization
107 return boost::shared_ptr<ModelAPI_AttributeDocRef>();
109 boost::shared_ptr<ModelAPI_AttributeDocRef> aRes = boost::dynamic_pointer_cast<
110 ModelAPI_AttributeDocRef>(aFound->second);
112 // TODO: generate error on invalid attribute type request
117 boost::shared_ptr<ModelAPI_AttributeDouble> Model_Data::real(const std::string& theID)
119 std::map<std::string, boost::shared_ptr<ModelAPI_Attribute> >::iterator aFound =
121 if (aFound == myAttrs.end()) {
122 // TODO: generate error on unknown attribute request and/or add mechanism for customization
123 return boost::shared_ptr<ModelAPI_AttributeDouble>();
125 boost::shared_ptr<ModelAPI_AttributeDouble> aRes = boost::dynamic_pointer_cast<
126 ModelAPI_AttributeDouble>(aFound->second);
128 // TODO: generate error on invalid attribute type request
133 boost::shared_ptr<ModelAPI_AttributeInteger> Model_Data::integer(const std::string& theID)
135 std::map<std::string, boost::shared_ptr<ModelAPI_Attribute> >::iterator aFound =
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_AttributeInteger>();
141 boost::shared_ptr<ModelAPI_AttributeInteger> aRes = boost::dynamic_pointer_cast<
142 ModelAPI_AttributeInteger>(aFound->second);
144 // TODO: generate error on invalid attribute type request
149 boost::shared_ptr<ModelAPI_AttributeBoolean> Model_Data::boolean(const std::string& theID)
151 std::map<std::string, boost::shared_ptr<ModelAPI_Attribute> >::iterator aFound =
153 if (aFound == myAttrs.end()) {
154 // TODO: generate error on unknown attribute request and/or add mechanism for customization
155 return boost::shared_ptr<ModelAPI_AttributeBoolean>();
157 boost::shared_ptr<ModelAPI_AttributeBoolean> aRes = boost::dynamic_pointer_cast<
158 ModelAPI_AttributeBoolean>(aFound->second);
160 // TODO: generate error on invalid attribute type request
165 boost::shared_ptr<ModelAPI_AttributeString> Model_Data::string(const std::string& theID)
167 std::map<std::string, boost::shared_ptr<ModelAPI_Attribute> >::iterator aFound =
169 if (aFound == myAttrs.end()) {
170 // TODO: generate error on unknown attribute request and/or add mechanism for customization
171 return boost::shared_ptr<ModelAPI_AttributeString>();
173 boost::shared_ptr<ModelAPI_AttributeString> aRes =
174 boost::dynamic_pointer_cast<ModelAPI_AttributeString>(aFound->second);
176 // TODO: generate error on invalid attribute type request
182 boost::shared_ptr<ModelAPI_AttributeReference> Model_Data::reference(const std::string& theID)
184 std::map<std::string, boost::shared_ptr<ModelAPI_Attribute> >::iterator aFound =
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_AttributeReference>();
190 boost::shared_ptr<ModelAPI_AttributeReference> aRes = boost::dynamic_pointer_cast<
191 ModelAPI_AttributeReference>(aFound->second);
193 // TODO: generate error on invalid attribute type request
198 boost::shared_ptr<ModelAPI_AttributeSelection> Model_Data::selection(const std::string& theID)
200 std::map<std::string, boost::shared_ptr<ModelAPI_Attribute> >::iterator aFound =
202 if (aFound == myAttrs.end()) {
203 // TODO: generate error on unknown attribute request and/or add mechanism for customization
204 return boost::shared_ptr<ModelAPI_AttributeSelection>();
206 boost::shared_ptr<ModelAPI_AttributeSelection> aRes =
207 boost::dynamic_pointer_cast<ModelAPI_AttributeSelection>(aFound->second);
209 // TODO: generate error on invalid attribute type request
214 boost::shared_ptr<ModelAPI_AttributeSelectionList>
215 Model_Data::selectionList(const std::string& theID)
217 std::map<std::string, boost::shared_ptr<ModelAPI_Attribute> >::iterator aFound =
219 if (aFound == myAttrs.end()) {
220 // TODO: generate error on unknown attribute request and/or add mechanism for customization
221 return boost::shared_ptr<ModelAPI_AttributeSelectionList>();
223 boost::shared_ptr<ModelAPI_AttributeSelectionList> aRes =
224 boost::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(aFound->second);
226 // TODO: generate error on invalid attribute type request
231 boost::shared_ptr<ModelAPI_AttributeRefAttr> Model_Data::refattr(const std::string& theID)
233 std::map<std::string, boost::shared_ptr<ModelAPI_Attribute> >::iterator aFound =
235 if (aFound == myAttrs.end()) {
236 // TODO: generate error on unknown attribute request and/or add mechanism for customization
237 return boost::shared_ptr<ModelAPI_AttributeRefAttr>();
239 boost::shared_ptr<ModelAPI_AttributeRefAttr> aRes = boost::dynamic_pointer_cast<
240 ModelAPI_AttributeRefAttr>(aFound->second);
242 // TODO: generate error on invalid attribute type request
247 boost::shared_ptr<ModelAPI_AttributeRefList> Model_Data::reflist(const std::string& theID)
249 std::map<std::string, boost::shared_ptr<ModelAPI_Attribute> >::iterator aFound =
251 if (aFound == myAttrs.end()) {
252 // TODO: generate error on unknown attribute request and/or add mechanism for customization
253 return boost::shared_ptr<ModelAPI_AttributeRefList>();
255 boost::shared_ptr<ModelAPI_AttributeRefList> aRes = boost::dynamic_pointer_cast<
256 ModelAPI_AttributeRefList>(aFound->second);
258 // TODO: generate error on invalid attribute type request
263 boost::shared_ptr<ModelAPI_Attribute> Model_Data::attribute(const std::string& theID)
265 boost::shared_ptr<ModelAPI_Attribute> aResult;
266 if (myAttrs.find(theID) == myAttrs.end()) // no such attribute
268 return myAttrs[theID];
271 const std::string& Model_Data::id(const boost::shared_ptr<ModelAPI_Attribute>& theAttr)
273 std::map<std::string, boost::shared_ptr<ModelAPI_Attribute> >::iterator anAttr =
275 for (; anAttr != myAttrs.end(); anAttr++) {
276 if (anAttr->second == theAttr)
277 return anAttr->first;
280 static std::string anEmpty;
284 bool Model_Data::isEqual(const boost::shared_ptr<ModelAPI_Data>& theData)
286 boost::shared_ptr<Model_Data> aData = boost::dynamic_pointer_cast<Model_Data>(theData);
288 return myLab.IsEqual(aData->myLab) == Standard_True ;
292 bool Model_Data::isValid()
294 return !myLab.IsNull() && myLab.HasAttribute();
297 std::list<boost::shared_ptr<ModelAPI_Attribute> > Model_Data::attributes(const std::string& theType)
299 std::list<boost::shared_ptr<ModelAPI_Attribute> > aResult;
300 std::map<std::string, boost::shared_ptr<ModelAPI_Attribute> >::iterator anAttrsIter =
302 for (; anAttrsIter != myAttrs.end(); anAttrsIter++) {
303 if (theType.empty() || anAttrsIter->second->attributeType() == theType) {
304 aResult.push_back(anAttrsIter->second);
310 std::list<std::string> Model_Data::attributesIDs(const std::string& theType)
312 std::list<std::string> aResult;
313 std::map<std::string, boost::shared_ptr<ModelAPI_Attribute> >::iterator anAttrsIter =
315 for (; anAttrsIter != myAttrs.end(); anAttrsIter++) {
316 if (theType.empty() || anAttrsIter->second->attributeType() == theType) {
317 aResult.push_back(anAttrsIter->first);
323 void Model_Data::sendAttributeUpdated(ModelAPI_Attribute* theAttr)
325 theAttr->setInitialized();
326 if (theAttr->isArgument()) {
327 static const Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
328 ModelAPI_EventCreator::get()->sendUpdated(myObject, anEvent);
332 void Model_Data::erase()
335 myLab.ForgetAllAttributes();
338 /// identifeir of the "must be updated" flag in the data tree
339 Standard_GUID kMustBeUpdatedGUID("baede74c-31a6-4416-9c4d-e48ce65f2005");
341 void Model_Data::mustBeUpdated(const bool theFlag)
344 TDataStd_UAttribute::Set(myLab, kMustBeUpdatedGUID);
346 myLab.ForgetAttribute(kMustBeUpdatedGUID);
349 bool Model_Data::mustBeUpdated()
351 return myLab.IsAttribute(kMustBeUpdatedGUID) == Standard_True;
354 bool Model_Data::referencesTo(const boost::shared_ptr<ModelAPI_Feature>& theFeature)
356 // collect results of this feature first to check references quickly in the cycle
357 std::set<ObjectPtr> aFeatureObjs;
358 aFeatureObjs.insert(theFeature);
359 std::list<boost::shared_ptr<ModelAPI_Result> >::const_iterator aRIter =
360 theFeature->results().cbegin();
361 for(; aRIter != theFeature->results().cend(); aRIter++) {
363 aFeatureObjs.insert(*aRIter);
366 std::map<std::string, boost::shared_ptr<ModelAPI_Attribute> >::iterator anAttrsIter =
368 for (; anAttrsIter != myAttrs.end(); anAttrsIter++) {
369 if (anAttrsIter->second->attributeType() == ModelAPI_AttributeRefAttr::type()) {
370 boost::shared_ptr<ModelAPI_AttributeRefAttr> aRefAttr =
371 boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(anAttrsIter->second);
372 if (aRefAttr && aRefAttr->isObject()) { // check referenced object
373 if (aFeatureObjs.find(aRefAttr->object()) != aFeatureObjs.end())
375 } else { // check object of referenced attribute
376 boost::shared_ptr<ModelAPI_Attribute> anAttr = aRefAttr->attr();
377 if (anAttr && aFeatureObjs.find(anAttr->owner()) != aFeatureObjs.end())
380 } else if (anAttrsIter->second->attributeType() == ModelAPI_AttributeReference::type()) {
381 boost::shared_ptr<ModelAPI_AttributeReference> aRef =
382 boost::dynamic_pointer_cast<ModelAPI_AttributeReference>(anAttrsIter->second);
383 if (aFeatureObjs.find(aRef->value()) != aFeatureObjs.end()) {