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>
19 #include <ModelAPI_Validator.h>
21 #include <GeomData_Point.h>
22 #include <GeomData_Point2D.h>
23 #include <GeomData_Dir.h>
24 #include <Events_Loop.h>
25 #include <Events_Error.h>
27 #include <TDataStd_Name.hxx>
28 #include <TDataStd_UAttribute.hxx>
32 Model_Data::Model_Data()
36 void Model_Data::setLabel(TDF_Label theLab)
41 std::string Model_Data::name()
43 Handle(TDataStd_Name) aName;
44 if (myLab.FindAttribute(TDataStd_Name::GetID(), aName))
45 return std::string(TCollection_AsciiString(aName->Get()).ToCString());
46 return ""; // not defined
49 void Model_Data::setName(const std::string& theName)
51 bool isModified = false;
52 Handle(TDataStd_Name) aName;
53 if (!myLab.FindAttribute(TDataStd_Name::GetID(), aName)) {
54 TDataStd_Name::Set(myLab, theName.c_str());
57 isModified = !aName->Get().IsEqual(theName.c_str());
59 aName->Set(theName.c_str());
63 void Model_Data::addAttribute(const std::string& theID, const std::string theAttrType)
65 TDF_Label anAttrLab = myLab.FindChild(myAttrs.size() + 1);
66 ModelAPI_Attribute* anAttr = 0;
67 if (theAttrType == ModelAPI_AttributeDocRef::type()) {
68 anAttr = new Model_AttributeDocRef(anAttrLab);
69 } else if (theAttrType == Model_AttributeInteger::type()) {
70 anAttr = new Model_AttributeInteger(anAttrLab);
71 } else if (theAttrType == ModelAPI_AttributeDouble::type()) {
72 anAttr = new Model_AttributeDouble(anAttrLab);
73 } else if (theAttrType == Model_AttributeBoolean::type()) {
74 anAttr = new Model_AttributeBoolean(anAttrLab);
75 } else if (theAttrType == Model_AttributeString::type()) {
76 anAttr = new Model_AttributeString(anAttrLab);
77 } else if (theAttrType == ModelAPI_AttributeReference::type()) {
78 anAttr = new Model_AttributeReference(anAttrLab);
79 } else if (theAttrType == ModelAPI_AttributeSelection::type()) {
80 anAttr = new Model_AttributeSelection(anAttrLab);
81 } else if (theAttrType == ModelAPI_AttributeSelectionList::type()) {
82 anAttr = new Model_AttributeSelectionList(anAttrLab);
83 } else if (theAttrType == ModelAPI_AttributeRefAttr::type()) {
84 anAttr = new Model_AttributeRefAttr(anAttrLab);
85 } else if (theAttrType == ModelAPI_AttributeRefList::type()) {
86 anAttr = new Model_AttributeRefList(anAttrLab);
87 } else if (theAttrType == GeomData_Point::type()) {
88 anAttr = new GeomData_Point(anAttrLab);
89 } else if (theAttrType == GeomData_Dir::type()) {
90 anAttr = new GeomData_Dir(anAttrLab);
91 } else if (theAttrType == GeomData_Point2D::type()) {
92 anAttr = new GeomData_Point2D(anAttrLab);
95 myAttrs[theID] = boost::shared_ptr<ModelAPI_Attribute>(anAttr);
96 anAttr->setObject(myObject);
98 Events_Error::send("Can not create unknown type of attribute " + theAttrType);
102 boost::shared_ptr<ModelAPI_AttributeDocRef> Model_Data::document(const std::string& theID)
104 std::map<std::string, boost::shared_ptr<ModelAPI_Attribute> >::iterator aFound =
106 if (aFound == myAttrs.end()) {
107 // TODO: generate error on unknown attribute request and/or add mechanism for customization
108 return boost::shared_ptr<ModelAPI_AttributeDocRef>();
110 boost::shared_ptr<ModelAPI_AttributeDocRef> aRes = boost::dynamic_pointer_cast<
111 ModelAPI_AttributeDocRef>(aFound->second);
113 // TODO: generate error on invalid attribute type request
118 boost::shared_ptr<ModelAPI_AttributeDouble> Model_Data::real(const std::string& theID)
120 std::map<std::string, boost::shared_ptr<ModelAPI_Attribute> >::iterator aFound =
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_AttributeDouble>();
126 boost::shared_ptr<ModelAPI_AttributeDouble> aRes = boost::dynamic_pointer_cast<
127 ModelAPI_AttributeDouble>(aFound->second);
129 // TODO: generate error on invalid attribute type request
134 boost::shared_ptr<ModelAPI_AttributeInteger> Model_Data::integer(const std::string& theID)
136 std::map<std::string, boost::shared_ptr<ModelAPI_Attribute> >::iterator aFound =
138 if (aFound == myAttrs.end()) {
139 // TODO: generate error on unknown attribute request and/or add mechanism for customization
140 return boost::shared_ptr<ModelAPI_AttributeInteger>();
142 boost::shared_ptr<ModelAPI_AttributeInteger> aRes = boost::dynamic_pointer_cast<
143 ModelAPI_AttributeInteger>(aFound->second);
145 // TODO: generate error on invalid attribute type request
150 boost::shared_ptr<ModelAPI_AttributeBoolean> Model_Data::boolean(const std::string& theID)
152 std::map<std::string, boost::shared_ptr<ModelAPI_Attribute> >::iterator aFound =
154 if (aFound == myAttrs.end()) {
155 // TODO: generate error on unknown attribute request and/or add mechanism for customization
156 return boost::shared_ptr<ModelAPI_AttributeBoolean>();
158 boost::shared_ptr<ModelAPI_AttributeBoolean> aRes = boost::dynamic_pointer_cast<
159 ModelAPI_AttributeBoolean>(aFound->second);
161 // TODO: generate error on invalid attribute type request
166 boost::shared_ptr<ModelAPI_AttributeString> Model_Data::string(const std::string& theID)
168 std::map<std::string, boost::shared_ptr<ModelAPI_Attribute> >::iterator aFound =
170 if (aFound == myAttrs.end()) {
171 // TODO: generate error on unknown attribute request and/or add mechanism for customization
172 return boost::shared_ptr<ModelAPI_AttributeString>();
174 boost::shared_ptr<ModelAPI_AttributeString> aRes =
175 boost::dynamic_pointer_cast<ModelAPI_AttributeString>(aFound->second);
177 // TODO: generate error on invalid attribute type request
183 boost::shared_ptr<ModelAPI_AttributeReference> Model_Data::reference(const std::string& theID)
185 std::map<std::string, boost::shared_ptr<ModelAPI_Attribute> >::iterator aFound =
187 if (aFound == myAttrs.end()) {
188 // TODO: generate error on unknown attribute request and/or add mechanism for customization
189 return boost::shared_ptr<ModelAPI_AttributeReference>();
191 boost::shared_ptr<ModelAPI_AttributeReference> aRes = boost::dynamic_pointer_cast<
192 ModelAPI_AttributeReference>(aFound->second);
194 // TODO: generate error on invalid attribute type request
199 boost::shared_ptr<ModelAPI_AttributeSelection> Model_Data::selection(const std::string& theID)
201 std::map<std::string, boost::shared_ptr<ModelAPI_Attribute> >::iterator aFound =
203 if (aFound == myAttrs.end()) {
204 // TODO: generate error on unknown attribute request and/or add mechanism for customization
205 return boost::shared_ptr<ModelAPI_AttributeSelection>();
207 boost::shared_ptr<ModelAPI_AttributeSelection> aRes =
208 boost::dynamic_pointer_cast<ModelAPI_AttributeSelection>(aFound->second);
210 // TODO: generate error on invalid attribute type request
215 boost::shared_ptr<ModelAPI_AttributeSelectionList>
216 Model_Data::selectionList(const std::string& theID)
218 std::map<std::string, boost::shared_ptr<ModelAPI_Attribute> >::iterator aFound =
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_AttributeSelectionList>();
224 boost::shared_ptr<ModelAPI_AttributeSelectionList> aRes =
225 boost::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(aFound->second);
227 // TODO: generate error on invalid attribute type request
232 boost::shared_ptr<ModelAPI_AttributeRefAttr> Model_Data::refattr(const std::string& theID)
234 std::map<std::string, boost::shared_ptr<ModelAPI_Attribute> >::iterator aFound =
236 if (aFound == myAttrs.end()) {
237 // TODO: generate error on unknown attribute request and/or add mechanism for customization
238 return boost::shared_ptr<ModelAPI_AttributeRefAttr>();
240 boost::shared_ptr<ModelAPI_AttributeRefAttr> aRes = boost::dynamic_pointer_cast<
241 ModelAPI_AttributeRefAttr>(aFound->second);
243 // TODO: generate error on invalid attribute type request
248 boost::shared_ptr<ModelAPI_AttributeRefList> Model_Data::reflist(const std::string& theID)
250 std::map<std::string, boost::shared_ptr<ModelAPI_Attribute> >::iterator aFound =
252 if (aFound == myAttrs.end()) {
253 // TODO: generate error on unknown attribute request and/or add mechanism for customization
254 return boost::shared_ptr<ModelAPI_AttributeRefList>();
256 boost::shared_ptr<ModelAPI_AttributeRefList> aRes = boost::dynamic_pointer_cast<
257 ModelAPI_AttributeRefList>(aFound->second);
259 // TODO: generate error on invalid attribute type request
264 boost::shared_ptr<ModelAPI_Attribute> Model_Data::attribute(const std::string& theID)
266 boost::shared_ptr<ModelAPI_Attribute> aResult;
267 if (myAttrs.find(theID) == myAttrs.end()) // no such attribute
269 return myAttrs[theID];
272 const std::string& Model_Data::id(const boost::shared_ptr<ModelAPI_Attribute>& theAttr)
274 std::map<std::string, boost::shared_ptr<ModelAPI_Attribute> >::iterator anAttr =
276 for (; anAttr != myAttrs.end(); anAttr++) {
277 if (anAttr->second == theAttr)
278 return anAttr->first;
281 static std::string anEmpty;
285 bool Model_Data::isEqual(const boost::shared_ptr<ModelAPI_Data>& theData)
287 boost::shared_ptr<Model_Data> aData = boost::dynamic_pointer_cast<Model_Data>(theData);
289 return myLab.IsEqual(aData->myLab) == Standard_True ;
293 bool Model_Data::isValid()
295 return !myLab.IsNull() && myLab.HasAttribute();
298 std::list<boost::shared_ptr<ModelAPI_Attribute> > Model_Data::attributes(const std::string& theType)
300 std::list<boost::shared_ptr<ModelAPI_Attribute> > aResult;
301 std::map<std::string, boost::shared_ptr<ModelAPI_Attribute> >::iterator anAttrsIter =
303 for (; anAttrsIter != myAttrs.end(); anAttrsIter++) {
304 if (theType.empty() || anAttrsIter->second->attributeType() == theType) {
305 aResult.push_back(anAttrsIter->second);
311 std::list<std::string> Model_Data::attributesIDs(const std::string& theType)
313 std::list<std::string> aResult;
314 std::map<std::string, boost::shared_ptr<ModelAPI_Attribute> >::iterator anAttrsIter =
316 for (; anAttrsIter != myAttrs.end(); anAttrsIter++) {
317 if (theType.empty() || anAttrsIter->second->attributeType() == theType) {
318 aResult.push_back(anAttrsIter->first);
324 void Model_Data::sendAttributeUpdated(ModelAPI_Attribute* theAttr)
326 theAttr->setInitialized();
327 if (theAttr->isArgument()) {
328 static const Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
329 ModelAPI_EventCreator::get()->sendUpdated(myObject, anEvent);
333 void Model_Data::erase()
336 myLab.ForgetAllAttributes();
339 /// identifeir of the "must be updated" flag in the data tree
340 Standard_GUID kMustBeUpdatedGUID("baede74c-31a6-4416-9c4d-e48ce65f2005");
342 void Model_Data::mustBeUpdated(const bool theFlag)
345 TDataStd_UAttribute::Set(myLab, kMustBeUpdatedGUID);
347 myLab.ForgetAttribute(kMustBeUpdatedGUID);
350 bool Model_Data::mustBeUpdated()
352 return myLab.IsAttribute(kMustBeUpdatedGUID) == Standard_True;
355 int Model_Data::featureId() const
357 return myLab.Father().Tag(); // tag of the feature label
360 void Model_Data::eraseBackReferences()
363 boost::shared_ptr<ModelAPI_Result> aRes =
364 boost::dynamic_pointer_cast<ModelAPI_Result>(myObject);
366 aRes->setIsConcealed(false);
369 void Model_Data::addBackReference(FeaturePtr theFeature, std::string theAttrID)
371 myRefsToMe.insert(theFeature->data()->attribute(theAttrID));
372 if (ModelAPI_Session::get()->validators()->isConcealed(theFeature->getKind(), theAttrID)) {
373 boost::shared_ptr<ModelAPI_Result> aRes =
374 boost::dynamic_pointer_cast<ModelAPI_Result>(myObject);
376 aRes->setIsConcealed(true);
381 void Model_Data::referencesToObjects(
382 std::list<std::pair<std::string, std::list<ObjectPtr> > >& theRefs)
384 std::map<std::string, boost::shared_ptr<ModelAPI_Attribute> >::iterator anAttr = myAttrs.begin();
385 std::list<ObjectPtr> aReferenced; // not inside of cycle to avoid excess memory menagement
386 for(; anAttr != myAttrs.end(); anAttr++) {
387 std::string aType = anAttr->second->attributeType();
388 if (aType == ModelAPI_AttributeReference::type()) { // reference to object
389 boost::shared_ptr<ModelAPI_AttributeReference> aRef = boost::dynamic_pointer_cast<
390 ModelAPI_AttributeReference>(anAttr->second);
391 aReferenced.push_back(aRef->value());
392 theRefs.push_back(std::pair<std::string, std::list<ObjectPtr> >(anAttr->first, aReferenced));
393 } else if (aType == ModelAPI_AttributeRefAttr::type()) { // reference to attribute or object
394 boost::shared_ptr<ModelAPI_AttributeRefAttr> aRef = boost::dynamic_pointer_cast<
395 ModelAPI_AttributeRefAttr>(anAttr->second);
396 aReferenced.push_back(aRef->isObject() ? aRef->object() : aRef->attr()->owner());
397 } else if (aType == ModelAPI_AttributeRefList::type()) { // list of references
398 aReferenced = boost::dynamic_pointer_cast<ModelAPI_AttributeRefList>(anAttr->second)->list();
399 } else if (aType == ModelAPI_AttributeSelection::type()) { // selection attribute
400 boost::shared_ptr<ModelAPI_AttributeSelection> aRef = boost::dynamic_pointer_cast<
401 ModelAPI_AttributeSelection>(anAttr->second);
402 aReferenced.push_back(aRef->context());
403 theRefs.push_back(std::pair<std::string, std::list<ObjectPtr> >(anAttr->first, aReferenced));
404 } else if (aType == ModelAPI_AttributeSelectionList::type()) { // list of selection attributes
405 boost::shared_ptr<ModelAPI_AttributeSelectionList> aRef = boost::dynamic_pointer_cast<
406 ModelAPI_AttributeSelectionList>(anAttr->second);
407 for(int a = aRef->size() - 1; a >= 0; a--) {
408 aReferenced.push_back(aRef->value(a)->context());
411 continue; // nothing to do, not reference
413 if (!aReferenced.empty()) {
414 theRefs.push_back(std::pair<std::string, std::list<ObjectPtr> >(anAttr->first, aReferenced));