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_Events.h>
15 #include <ModelAPI_Feature.h>
16 #include <ModelAPI_Result.h>
18 #include <GeomData_Point.h>
19 #include <GeomData_Point2D.h>
20 #include <GeomData_Dir.h>
21 #include <Events_Loop.h>
22 #include <Events_Error.h>
24 #include <TDataStd_Name.hxx>
25 #include <TDataStd_UAttribute.hxx>
29 Model_Data::Model_Data()
33 void Model_Data::setLabel(TDF_Label theLab)
38 std::string Model_Data::name()
40 Handle(TDataStd_Name) aName;
41 if (myLab.FindAttribute(TDataStd_Name::GetID(), aName))
42 return std::string(TCollection_AsciiString(aName->Get()).ToCString());
43 return ""; // not defined
46 void Model_Data::setName(const std::string& theName)
48 bool isModified = false;
49 Handle(TDataStd_Name) aName;
50 if (!myLab.FindAttribute(TDataStd_Name::GetID(), aName)) {
51 TDataStd_Name::Set(myLab, theName.c_str());
54 isModified = !aName->Get().IsEqual(theName.c_str());
56 aName->Set(theName.c_str());
60 void Model_Data::addAttribute(const std::string& theID, const std::string theAttrType)
62 TDF_Label anAttrLab = myLab.FindChild(myAttrs.size() + 1);
63 ModelAPI_Attribute* anAttr = 0;
64 if (theAttrType == ModelAPI_AttributeDocRef::type()) {
65 anAttr = new Model_AttributeDocRef(anAttrLab);
66 } else if (theAttrType == Model_AttributeInteger::type()) {
67 anAttr = new Model_AttributeInteger(anAttrLab);
68 } else if (theAttrType == ModelAPI_AttributeDouble::type()) {
69 anAttr = new Model_AttributeDouble(anAttrLab);
70 } else if (theAttrType == Model_AttributeBoolean::type()) {
71 anAttr = new Model_AttributeBoolean(anAttrLab);
72 } else if (theAttrType == Model_AttributeString::type()) {
73 anAttr = new Model_AttributeString(anAttrLab);
74 } else if (theAttrType == ModelAPI_AttributeReference::type()) {
75 anAttr = new Model_AttributeReference(anAttrLab);
76 } else if (theAttrType == ModelAPI_AttributeRefAttr::type()) {
77 anAttr = new Model_AttributeRefAttr(anAttrLab);
78 } else if (theAttrType == ModelAPI_AttributeRefList::type()) {
79 anAttr = new Model_AttributeRefList(anAttrLab);
80 } else if (theAttrType == GeomData_Point::type()) {
81 anAttr = new GeomData_Point(anAttrLab);
82 } else if (theAttrType == GeomData_Dir::type()) {
83 anAttr = new GeomData_Dir(anAttrLab);
84 } else if (theAttrType == GeomData_Point2D::type()) {
85 anAttr = new GeomData_Point2D(anAttrLab);
88 myAttrs[theID] = boost::shared_ptr<ModelAPI_Attribute>(anAttr);
89 anAttr->setObject(myObject);
91 Events_Error::send("Can not create unknown type of attribute " + theAttrType);
95 boost::shared_ptr<ModelAPI_AttributeDocRef> Model_Data::document(const std::string& theID)
97 std::map<std::string, boost::shared_ptr<ModelAPI_Attribute> >::iterator aFound = myAttrs.find(theID);
98 if (aFound == myAttrs.end()) {
99 // TODO: generate error on unknown attribute request and/or add mechanism for customization
100 return boost::shared_ptr<ModelAPI_AttributeDocRef>();
102 boost::shared_ptr<ModelAPI_AttributeDocRef> aRes = boost::dynamic_pointer_cast<
103 ModelAPI_AttributeDocRef>(aFound->second);
105 // TODO: generate error on invalid attribute type request
110 boost::shared_ptr<ModelAPI_AttributeDouble> Model_Data::real(const std::string& theID)
112 std::map<std::string, boost::shared_ptr<ModelAPI_Attribute> >::iterator aFound = myAttrs.find(theID);
113 if (aFound == myAttrs.end()) {
114 // TODO: generate error on unknown attribute request and/or add mechanism for customization
115 return boost::shared_ptr<ModelAPI_AttributeDouble>();
117 boost::shared_ptr<ModelAPI_AttributeDouble> aRes = boost::dynamic_pointer_cast<
118 ModelAPI_AttributeDouble>(aFound->second);
120 // TODO: generate error on invalid attribute type request
125 boost::shared_ptr<ModelAPI_AttributeInteger> Model_Data::integer(const std::string& theID)
127 std::map<std::string, boost::shared_ptr<ModelAPI_Attribute> >::iterator aFound = myAttrs.find(theID);
128 if (aFound == myAttrs.end()) {
129 // TODO: generate error on unknown attribute request and/or add mechanism for customization
130 return boost::shared_ptr<ModelAPI_AttributeInteger>();
132 boost::shared_ptr<ModelAPI_AttributeInteger> aRes = boost::dynamic_pointer_cast<
133 ModelAPI_AttributeInteger>(aFound->second);
135 // TODO: generate error on invalid attribute type request
140 boost::shared_ptr<ModelAPI_AttributeBoolean> Model_Data::boolean(const std::string& theID)
142 std::map<std::string, boost::shared_ptr<ModelAPI_Attribute> >::iterator aFound = myAttrs.find(theID);
143 if (aFound == myAttrs.end()) {
144 // TODO: generate error on unknown attribute request and/or add mechanism for customization
145 return boost::shared_ptr<ModelAPI_AttributeBoolean>();
147 boost::shared_ptr<ModelAPI_AttributeBoolean> aRes = boost::dynamic_pointer_cast<
148 ModelAPI_AttributeBoolean>(aFound->second);
150 // TODO: generate error on invalid attribute type request
155 boost::shared_ptr<ModelAPI_AttributeString> Model_Data::string(const std::string& theID)
157 std::map<std::string, boost::shared_ptr<ModelAPI_Attribute> >::iterator aFound = myAttrs.find(theID);
158 if (aFound == myAttrs.end()) {
159 // TODO: generate error on unknown attribute request and/or add mechanism for customization
160 return boost::shared_ptr<ModelAPI_AttributeString>();
162 boost::shared_ptr<ModelAPI_AttributeString> aRes =
163 boost::dynamic_pointer_cast<ModelAPI_AttributeString>(aFound->second);
165 // TODO: generate error on invalid attribute type request
171 boost::shared_ptr<ModelAPI_AttributeReference> Model_Data::reference(const std::string& theID)
173 std::map<std::string, boost::shared_ptr<ModelAPI_Attribute> >::iterator aFound = myAttrs.find(theID);
174 if (aFound == myAttrs.end()) {
175 // TODO: generate error on unknown attribute request and/or add mechanism for customization
176 return boost::shared_ptr<ModelAPI_AttributeReference>();
178 boost::shared_ptr<ModelAPI_AttributeReference> aRes = boost::dynamic_pointer_cast<
179 ModelAPI_AttributeReference>(aFound->second);
181 // TODO: generate error on invalid attribute type request
186 boost::shared_ptr<ModelAPI_AttributeRefAttr> Model_Data::refattr(const std::string& theID)
188 std::map<std::string, boost::shared_ptr<ModelAPI_Attribute> >::iterator aFound = myAttrs.find(theID);
189 if (aFound == myAttrs.end()) {
190 // TODO: generate error on unknown attribute request and/or add mechanism for customization
191 return boost::shared_ptr<ModelAPI_AttributeRefAttr>();
193 boost::shared_ptr<ModelAPI_AttributeRefAttr> aRes = boost::dynamic_pointer_cast<
194 ModelAPI_AttributeRefAttr>(aFound->second);
196 // TODO: generate error on invalid attribute type request
201 boost::shared_ptr<ModelAPI_AttributeRefList> Model_Data::reflist(const std::string& theID)
203 std::map<std::string, boost::shared_ptr<ModelAPI_Attribute> >::iterator aFound = myAttrs.find(theID);
204 if (aFound == myAttrs.end()) {
205 // TODO: generate error on unknown attribute request and/or add mechanism for customization
206 return boost::shared_ptr<ModelAPI_AttributeRefList>();
208 boost::shared_ptr<ModelAPI_AttributeRefList> aRes = boost::dynamic_pointer_cast<
209 ModelAPI_AttributeRefList>(aFound->second);
211 // TODO: generate error on invalid attribute type request
216 boost::shared_ptr<ModelAPI_Attribute> Model_Data::attribute(const std::string& theID)
218 boost::shared_ptr<ModelAPI_Attribute> aResult;
219 if (myAttrs.find(theID) == myAttrs.end()) // no such attribute
221 return myAttrs[theID];
224 const std::string& Model_Data::id(const boost::shared_ptr<ModelAPI_Attribute>& theAttr)
226 std::map<std::string, boost::shared_ptr<ModelAPI_Attribute> >::iterator anAttr = myAttrs.begin();
227 for (; anAttr != myAttrs.end(); anAttr++) {
228 if (anAttr->second == theAttr)
229 return anAttr->first;
232 static std::string anEmpty;
236 bool Model_Data::isEqual(const boost::shared_ptr<ModelAPI_Data>& theData)
238 boost::shared_ptr<Model_Data> aData = boost::dynamic_pointer_cast<Model_Data>(theData);
240 return myLab.IsEqual(aData->myLab) == Standard_True ;
244 bool Model_Data::isValid()
246 return !myLab.IsNull() && myLab.HasAttribute();
249 std::list<boost::shared_ptr<ModelAPI_Attribute> > Model_Data::attributes(const std::string& theType)
251 std::list<boost::shared_ptr<ModelAPI_Attribute> > aResult;
252 std::map<std::string, boost::shared_ptr<ModelAPI_Attribute> >::iterator anAttrsIter =
254 for (; anAttrsIter != myAttrs.end(); anAttrsIter++) {
255 if (theType.empty() || anAttrsIter->second->attributeType() == theType) {
256 aResult.push_back(anAttrsIter->second);
262 std::list<std::string> Model_Data::attributesIDs(const std::string& theType)
264 std::list<std::string> aResult;
265 std::map<std::string, boost::shared_ptr<ModelAPI_Attribute> >::iterator anAttrsIter =
267 for (; anAttrsIter != myAttrs.end(); anAttrsIter++) {
268 if (theType.empty() || anAttrsIter->second->attributeType() == theType) {
269 aResult.push_back(anAttrsIter->first);
275 void Model_Data::sendAttributeUpdated(ModelAPI_Attribute* theAttr)
277 theAttr->setInitialized();
278 if (theAttr->isArgument()) {
279 static const Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
280 ModelAPI_EventCreator::get()->sendUpdated(myObject, anEvent);
284 void Model_Data::erase()
287 myLab.ForgetAllAttributes();
290 /// identifeir of the "must be updated" flag in the data tree
291 Standard_GUID kMustBeUpdatedGUID("baede74c-31a6-4416-9c4d-e48ce65f2005");
293 void Model_Data::mustBeUpdated(const bool theFlag)
296 TDataStd_UAttribute::Set(myLab, kMustBeUpdatedGUID);
298 myLab.ForgetAttribute(kMustBeUpdatedGUID);
301 bool Model_Data::mustBeUpdated()
303 return myLab.IsAttribute(kMustBeUpdatedGUID) == Standard_True;
306 bool Model_Data::referencesTo(const boost::shared_ptr<ModelAPI_Feature>& theFeature)
308 // collect results of this feature first to check references quickly in the cycle
309 std::set<ObjectPtr> aFeatureObjs;
310 aFeatureObjs.insert(theFeature);
311 std::list<boost::shared_ptr<ModelAPI_Result> >::const_iterator aRIter =
312 theFeature->results().cbegin();
313 for(; aRIter != theFeature->results().cend(); aRIter++) {
315 aFeatureObjs.insert(*aRIter);
318 std::map<std::string, boost::shared_ptr<ModelAPI_Attribute> >::iterator anAttrsIter =
320 for (; anAttrsIter != myAttrs.end(); anAttrsIter++) {
321 if (anAttrsIter->second->attributeType() == ModelAPI_AttributeRefAttr::type()) {
322 boost::shared_ptr<ModelAPI_AttributeRefAttr> aRefAttr =
323 boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(anAttrsIter->second);
324 if (aRefAttr && aRefAttr->isObject()) { // check referenced object
325 if (aFeatureObjs.find(aRefAttr->object()) != aFeatureObjs.end())
327 } else { // check object of referenced attribute
328 boost::shared_ptr<ModelAPI_Attribute> anAttr = aRefAttr->attr();
329 if (anAttr && aFeatureObjs.find(anAttr->owner()) != aFeatureObjs.end())
332 } else if (anAttrsIter->second->attributeType() == ModelAPI_AttributeReference::type()) {
333 boost::shared_ptr<ModelAPI_AttributeReference> aRef =
334 boost::dynamic_pointer_cast<ModelAPI_AttributeReference>(anAttrsIter->second);
335 if (aFeatureObjs.find(aRef->value()) != aFeatureObjs.end()) {