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 <ModelAPI_AttributeSelection.h>
15 #include <Model_Events.h>
16 #include <ModelAPI_Feature.h>
17 #include <ModelAPI_Result.h>
19 #include <GeomData_Point.h>
20 #include <GeomData_Point2D.h>
21 #include <GeomData_Dir.h>
22 #include <Events_Loop.h>
23 #include <Events_Error.h>
25 #include <TDataStd_Name.hxx>
26 #include <TDataStd_UAttribute.hxx>
30 Model_Data::Model_Data()
34 void Model_Data::setLabel(TDF_Label theLab)
39 std::string Model_Data::name()
41 Handle(TDataStd_Name) aName;
42 if (myLab.FindAttribute(TDataStd_Name::GetID(), aName))
43 return std::string(TCollection_AsciiString(aName->Get()).ToCString());
44 return ""; // not defined
47 void Model_Data::setName(const std::string& theName)
49 bool isModified = false;
50 Handle(TDataStd_Name) aName;
51 if (!myLab.FindAttribute(TDataStd_Name::GetID(), aName)) {
52 TDataStd_Name::Set(myLab, theName.c_str());
55 isModified = !aName->Get().IsEqual(theName.c_str());
57 aName->Set(theName.c_str());
61 void Model_Data::addAttribute(const std::string& theID, const std::string theAttrType)
63 TDF_Label anAttrLab = myLab.FindChild(myAttrs.size() + 1);
64 ModelAPI_Attribute* anAttr = 0;
65 if (theAttrType == ModelAPI_AttributeDocRef::type()) {
66 anAttr = new Model_AttributeDocRef(anAttrLab);
67 } else if (theAttrType == Model_AttributeInteger::type()) {
68 anAttr = new Model_AttributeInteger(anAttrLab);
69 } else if (theAttrType == ModelAPI_AttributeDouble::type()) {
70 anAttr = new Model_AttributeDouble(anAttrLab);
71 } else if (theAttrType == Model_AttributeBoolean::type()) {
72 anAttr = new Model_AttributeBoolean(anAttrLab);
73 } else if (theAttrType == Model_AttributeString::type()) {
74 anAttr = new Model_AttributeString(anAttrLab);
75 } else if (theAttrType == ModelAPI_AttributeReference::type()) {
76 anAttr = new Model_AttributeReference(anAttrLab);
77 } else if (theAttrType == ModelAPI_AttributeRefAttr::type()) {
78 anAttr = new Model_AttributeRefAttr(anAttrLab);
79 } else if (theAttrType == ModelAPI_AttributeRefList::type()) {
80 anAttr = new Model_AttributeRefList(anAttrLab);
81 } else if (theAttrType == GeomData_Point::type()) {
82 anAttr = new GeomData_Point(anAttrLab);
83 } else if (theAttrType == GeomData_Dir::type()) {
84 anAttr = new GeomData_Dir(anAttrLab);
85 } else if (theAttrType == GeomData_Point2D::type()) {
86 anAttr = new GeomData_Point2D(anAttrLab);
89 myAttrs[theID] = boost::shared_ptr<ModelAPI_Attribute>(anAttr);
90 anAttr->setObject(myObject);
92 Events_Error::send("Can not create unknown type of attribute " + theAttrType);
96 boost::shared_ptr<ModelAPI_AttributeDocRef> Model_Data::document(const std::string& theID)
98 std::map<std::string, boost::shared_ptr<ModelAPI_Attribute> >::iterator aFound = myAttrs.find(theID);
99 if (aFound == myAttrs.end()) {
100 // TODO: generate error on unknown attribute request and/or add mechanism for customization
101 return boost::shared_ptr<ModelAPI_AttributeDocRef>();
103 boost::shared_ptr<ModelAPI_AttributeDocRef> aRes = boost::dynamic_pointer_cast<
104 ModelAPI_AttributeDocRef>(aFound->second);
106 // TODO: generate error on invalid attribute type request
111 boost::shared_ptr<ModelAPI_AttributeDouble> Model_Data::real(const std::string& theID)
113 std::map<std::string, boost::shared_ptr<ModelAPI_Attribute> >::iterator aFound = myAttrs.find(theID);
114 if (aFound == myAttrs.end()) {
115 // TODO: generate error on unknown attribute request and/or add mechanism for customization
116 return boost::shared_ptr<ModelAPI_AttributeDouble>();
118 boost::shared_ptr<ModelAPI_AttributeDouble> aRes = boost::dynamic_pointer_cast<
119 ModelAPI_AttributeDouble>(aFound->second);
121 // TODO: generate error on invalid attribute type request
126 boost::shared_ptr<ModelAPI_AttributeInteger> Model_Data::integer(const std::string& theID)
128 std::map<std::string, boost::shared_ptr<ModelAPI_Attribute> >::iterator aFound = myAttrs.find(theID);
129 if (aFound == myAttrs.end()) {
130 // TODO: generate error on unknown attribute request and/or add mechanism for customization
131 return boost::shared_ptr<ModelAPI_AttributeInteger>();
133 boost::shared_ptr<ModelAPI_AttributeInteger> aRes = boost::dynamic_pointer_cast<
134 ModelAPI_AttributeInteger>(aFound->second);
136 // TODO: generate error on invalid attribute type request
141 boost::shared_ptr<ModelAPI_AttributeBoolean> Model_Data::boolean(const std::string& theID)
143 std::map<std::string, boost::shared_ptr<ModelAPI_Attribute> >::iterator aFound = myAttrs.find(theID);
144 if (aFound == myAttrs.end()) {
145 // TODO: generate error on unknown attribute request and/or add mechanism for customization
146 return boost::shared_ptr<ModelAPI_AttributeBoolean>();
148 boost::shared_ptr<ModelAPI_AttributeBoolean> aRes = boost::dynamic_pointer_cast<
149 ModelAPI_AttributeBoolean>(aFound->second);
151 // TODO: generate error on invalid attribute type request
156 boost::shared_ptr<ModelAPI_AttributeString> Model_Data::string(const std::string& theID)
158 std::map<std::string, boost::shared_ptr<ModelAPI_Attribute> >::iterator aFound = myAttrs.find(theID);
159 if (aFound == myAttrs.end()) {
160 // TODO: generate error on unknown attribute request and/or add mechanism for customization
161 return boost::shared_ptr<ModelAPI_AttributeString>();
163 boost::shared_ptr<ModelAPI_AttributeString> aRes =
164 boost::dynamic_pointer_cast<ModelAPI_AttributeString>(aFound->second);
166 // TODO: generate error on invalid attribute type request
172 boost::shared_ptr<ModelAPI_AttributeReference> Model_Data::reference(const std::string& theID)
174 std::map<std::string, boost::shared_ptr<ModelAPI_Attribute> >::iterator aFound = myAttrs.find(theID);
175 if (aFound == myAttrs.end()) {
176 // TODO: generate error on unknown attribute request and/or add mechanism for customization
177 return boost::shared_ptr<ModelAPI_AttributeReference>();
179 boost::shared_ptr<ModelAPI_AttributeReference> aRes = boost::dynamic_pointer_cast<
180 ModelAPI_AttributeReference>(aFound->second);
182 // TODO: generate error on invalid attribute type request
187 boost::shared_ptr<ModelAPI_AttributeSelection> Model_Data::selection(const std::string& theID)
189 std::map<std::string, boost::shared_ptr<ModelAPI_Attribute> >::iterator aFound = myAttrs.find(theID);
190 if (aFound == myAttrs.end()) {
191 // TODO: generate error on unknown attribute request and/or add mechanism for customization
192 return boost::shared_ptr<ModelAPI_AttributeSelection>();
194 boost::shared_ptr<ModelAPI_AttributeSelection> aRes =
195 boost::dynamic_pointer_cast<ModelAPI_AttributeSelection>(aFound->second);
197 // TODO: generate error on invalid attribute type request
202 boost::shared_ptr<ModelAPI_AttributeRefAttr> Model_Data::refattr(const std::string& theID)
204 std::map<std::string, boost::shared_ptr<ModelAPI_Attribute> >::iterator aFound = myAttrs.find(theID);
205 if (aFound == myAttrs.end()) {
206 // TODO: generate error on unknown attribute request and/or add mechanism for customization
207 return boost::shared_ptr<ModelAPI_AttributeRefAttr>();
209 boost::shared_ptr<ModelAPI_AttributeRefAttr> aRes = boost::dynamic_pointer_cast<
210 ModelAPI_AttributeRefAttr>(aFound->second);
212 // TODO: generate error on invalid attribute type request
217 boost::shared_ptr<ModelAPI_AttributeRefList> Model_Data::reflist(const std::string& theID)
219 std::map<std::string, boost::shared_ptr<ModelAPI_Attribute> >::iterator aFound = myAttrs.find(theID);
220 if (aFound == myAttrs.end()) {
221 // TODO: generate error on unknown attribute request and/or add mechanism for customization
222 return boost::shared_ptr<ModelAPI_AttributeRefList>();
224 boost::shared_ptr<ModelAPI_AttributeRefList> aRes = boost::dynamic_pointer_cast<
225 ModelAPI_AttributeRefList>(aFound->second);
227 // TODO: generate error on invalid attribute type request
232 boost::shared_ptr<ModelAPI_Attribute> Model_Data::attribute(const std::string& theID)
234 boost::shared_ptr<ModelAPI_Attribute> aResult;
235 if (myAttrs.find(theID) == myAttrs.end()) // no such attribute
237 return myAttrs[theID];
240 const std::string& Model_Data::id(const boost::shared_ptr<ModelAPI_Attribute>& theAttr)
242 std::map<std::string, boost::shared_ptr<ModelAPI_Attribute> >::iterator anAttr = myAttrs.begin();
243 for (; anAttr != myAttrs.end(); anAttr++) {
244 if (anAttr->second == theAttr)
245 return anAttr->first;
248 static std::string anEmpty;
252 bool Model_Data::isEqual(const boost::shared_ptr<ModelAPI_Data>& theData)
254 boost::shared_ptr<Model_Data> aData = boost::dynamic_pointer_cast<Model_Data>(theData);
256 return myLab.IsEqual(aData->myLab) == Standard_True ;
260 bool Model_Data::isValid()
262 return !myLab.IsNull() && myLab.HasAttribute();
265 std::list<boost::shared_ptr<ModelAPI_Attribute> > Model_Data::attributes(const std::string& theType)
267 std::list<boost::shared_ptr<ModelAPI_Attribute> > aResult;
268 std::map<std::string, boost::shared_ptr<ModelAPI_Attribute> >::iterator anAttrsIter =
270 for (; anAttrsIter != myAttrs.end(); anAttrsIter++) {
271 if (theType.empty() || anAttrsIter->second->attributeType() == theType) {
272 aResult.push_back(anAttrsIter->second);
278 std::list<std::string> Model_Data::attributesIDs(const std::string& theType)
280 std::list<std::string> aResult;
281 std::map<std::string, boost::shared_ptr<ModelAPI_Attribute> >::iterator anAttrsIter =
283 for (; anAttrsIter != myAttrs.end(); anAttrsIter++) {
284 if (theType.empty() || anAttrsIter->second->attributeType() == theType) {
285 aResult.push_back(anAttrsIter->first);
291 void Model_Data::sendAttributeUpdated(ModelAPI_Attribute* theAttr)
293 theAttr->setInitialized();
294 if (theAttr->isArgument()) {
295 static const Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
296 ModelAPI_EventCreator::get()->sendUpdated(myObject, anEvent);
300 void Model_Data::erase()
303 myLab.ForgetAllAttributes();
306 /// identifeir of the "must be updated" flag in the data tree
307 Standard_GUID kMustBeUpdatedGUID("baede74c-31a6-4416-9c4d-e48ce65f2005");
309 void Model_Data::mustBeUpdated(const bool theFlag)
312 TDataStd_UAttribute::Set(myLab, kMustBeUpdatedGUID);
314 myLab.ForgetAttribute(kMustBeUpdatedGUID);
317 bool Model_Data::mustBeUpdated()
319 return myLab.IsAttribute(kMustBeUpdatedGUID) == Standard_True;
322 bool Model_Data::referencesTo(const boost::shared_ptr<ModelAPI_Feature>& theFeature)
324 // collect results of this feature first to check references quickly in the cycle
325 std::set<ObjectPtr> aFeatureObjs;
326 aFeatureObjs.insert(theFeature);
327 std::list<boost::shared_ptr<ModelAPI_Result> >::const_iterator aRIter =
328 theFeature->results().cbegin();
329 for(; aRIter != theFeature->results().cend(); aRIter++) {
331 aFeatureObjs.insert(*aRIter);
334 std::map<std::string, boost::shared_ptr<ModelAPI_Attribute> >::iterator anAttrsIter =
336 for (; anAttrsIter != myAttrs.end(); anAttrsIter++) {
337 if (anAttrsIter->second->attributeType() == ModelAPI_AttributeRefAttr::type()) {
338 boost::shared_ptr<ModelAPI_AttributeRefAttr> aRefAttr =
339 boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(anAttrsIter->second);
340 if (aRefAttr && aRefAttr->isObject()) { // check referenced object
341 if (aFeatureObjs.find(aRefAttr->object()) != aFeatureObjs.end())
343 } else { // check object of referenced attribute
344 boost::shared_ptr<ModelAPI_Attribute> anAttr = aRefAttr->attr();
345 if (anAttr && aFeatureObjs.find(anAttr->owner()) != aFeatureObjs.end())
348 } else if (anAttrsIter->second->attributeType() == ModelAPI_AttributeReference::type()) {
349 boost::shared_ptr<ModelAPI_AttributeReference> aRef =
350 boost::dynamic_pointer_cast<ModelAPI_AttributeReference>(anAttrsIter->second);
351 if (aFeatureObjs.find(aRef->value()) != aFeatureObjs.end()) {