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>
16 #include <GeomData_Point.h>
17 #include <GeomData_Point2D.h>
18 #include <GeomData_Dir.h>
19 #include <Events_Loop.h>
20 #include <Events_Error.h>
22 #include <TDataStd_Name.hxx>
23 #include <TDataStd_UAttribute.hxx>
27 Model_Data::Model_Data()
31 void Model_Data::setLabel(TDF_Label theLab)
36 std::string Model_Data::name()
38 Handle(TDataStd_Name) aName;
39 if (myLab.FindAttribute(TDataStd_Name::GetID(), aName))
40 return std::string(TCollection_AsciiString(aName->Get()).ToCString());
41 return ""; // not defined
44 void Model_Data::setName(const std::string& theName)
46 bool isModified = false;
47 Handle(TDataStd_Name) aName;
48 if (!myLab.FindAttribute(TDataStd_Name::GetID(), aName)) {
49 TDataStd_Name::Set(myLab, theName.c_str());
52 isModified = !aName->Get().IsEqual(theName.c_str());
54 aName->Set(theName.c_str());
58 void Model_Data::addAttribute(const std::string& theID, const std::string theAttrType)
60 TDF_Label anAttrLab = myLab.FindChild(myAttrs.size() + 1);
61 ModelAPI_Attribute* anAttr = 0;
62 if (theAttrType == ModelAPI_AttributeDocRef::type()) {
63 anAttr = new Model_AttributeDocRef(anAttrLab);
64 } else if (theAttrType == Model_AttributeInteger::type()) {
65 anAttr = new Model_AttributeInteger(anAttrLab);
66 } else if (theAttrType == ModelAPI_AttributeDouble::type()) {
67 anAttr = new Model_AttributeDouble(anAttrLab);
68 } else if (theAttrType == Model_AttributeBoolean::type()) {
69 anAttr = new Model_AttributeBoolean(anAttrLab);
70 } else if (theAttrType == Model_AttributeString::type()) {
71 anAttr = new Model_AttributeString(anAttrLab);
72 } else if (theAttrType == ModelAPI_AttributeReference::type()) {
73 anAttr = new Model_AttributeReference(anAttrLab);
74 } else if (theAttrType == ModelAPI_AttributeRefAttr::type()) {
75 anAttr = new Model_AttributeRefAttr(anAttrLab);
76 } else if (theAttrType == ModelAPI_AttributeRefList::type()) {
77 anAttr = new Model_AttributeRefList(anAttrLab);
78 } else if (theAttrType == GeomData_Point::type()) {
79 anAttr = new GeomData_Point(anAttrLab);
80 } else if (theAttrType == GeomData_Dir::type()) {
81 anAttr = new GeomData_Dir(anAttrLab);
82 } else if (theAttrType == GeomData_Point2D::type()) {
83 anAttr = new GeomData_Point2D(anAttrLab);
86 myAttrs[theID] = boost::shared_ptr<ModelAPI_Attribute>(anAttr);
87 anAttr->setObject(myObject);
89 Events_Error::send("Can not create unknown type of attribute " + theAttrType);
93 boost::shared_ptr<ModelAPI_AttributeDocRef> Model_Data::document(const std::string& theID)
95 std::map<std::string, boost::shared_ptr<ModelAPI_Attribute> >::iterator aFound = myAttrs.find(theID);
96 if (aFound == myAttrs.end()) {
97 // TODO: generate error on unknown attribute request and/or add mechanism for customization
98 return boost::shared_ptr<ModelAPI_AttributeDocRef>();
100 boost::shared_ptr<ModelAPI_AttributeDocRef> aRes = boost::dynamic_pointer_cast<
101 ModelAPI_AttributeDocRef>(aFound->second);
103 // TODO: generate error on invalid attribute type request
108 boost::shared_ptr<ModelAPI_AttributeDouble> Model_Data::real(const std::string& theID)
110 std::map<std::string, boost::shared_ptr<ModelAPI_Attribute> >::iterator aFound = myAttrs.find(theID);
111 if (aFound == myAttrs.end()) {
112 // TODO: generate error on unknown attribute request and/or add mechanism for customization
113 return boost::shared_ptr<ModelAPI_AttributeDouble>();
115 boost::shared_ptr<ModelAPI_AttributeDouble> aRes = boost::dynamic_pointer_cast<
116 ModelAPI_AttributeDouble>(aFound->second);
118 // TODO: generate error on invalid attribute type request
123 boost::shared_ptr<ModelAPI_AttributeInteger> Model_Data::integer(const std::string& theID)
125 std::map<std::string, boost::shared_ptr<ModelAPI_Attribute> >::iterator aFound = myAttrs.find(theID);
126 if (aFound == myAttrs.end()) {
127 // TODO: generate error on unknown attribute request and/or add mechanism for customization
128 return boost::shared_ptr<ModelAPI_AttributeInteger>();
130 boost::shared_ptr<ModelAPI_AttributeInteger> aRes = boost::dynamic_pointer_cast<
131 ModelAPI_AttributeInteger>(aFound->second);
133 // TODO: generate error on invalid attribute type request
138 boost::shared_ptr<ModelAPI_AttributeBoolean> Model_Data::boolean(const std::string& theID)
140 std::map<std::string, boost::shared_ptr<ModelAPI_Attribute> >::iterator aFound = myAttrs.find(theID);
141 if (aFound == myAttrs.end()) {
142 // TODO: generate error on unknown attribute request and/or add mechanism for customization
143 return boost::shared_ptr<ModelAPI_AttributeBoolean>();
145 boost::shared_ptr<ModelAPI_AttributeBoolean> aRes = boost::dynamic_pointer_cast<
146 ModelAPI_AttributeBoolean>(aFound->second);
148 // TODO: generate error on invalid attribute type request
153 boost::shared_ptr<ModelAPI_AttributeString> Model_Data::string(const std::string& theID)
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_AttributeString>();
160 boost::shared_ptr<ModelAPI_AttributeString> aRes =
161 boost::dynamic_pointer_cast<ModelAPI_AttributeString>(aFound->second);
163 // TODO: generate error on invalid attribute type request
169 boost::shared_ptr<ModelAPI_AttributeReference> Model_Data::reference(const std::string& theID)
171 std::map<std::string, boost::shared_ptr<ModelAPI_Attribute> >::iterator aFound = myAttrs.find(theID);
172 if (aFound == myAttrs.end()) {
173 // TODO: generate error on unknown attribute request and/or add mechanism for customization
174 return boost::shared_ptr<ModelAPI_AttributeReference>();
176 boost::shared_ptr<ModelAPI_AttributeReference> aRes = boost::dynamic_pointer_cast<
177 ModelAPI_AttributeReference>(aFound->second);
179 // TODO: generate error on invalid attribute type request
184 boost::shared_ptr<ModelAPI_AttributeRefAttr> Model_Data::refattr(const std::string& theID)
186 std::map<std::string, boost::shared_ptr<ModelAPI_Attribute> >::iterator aFound = myAttrs.find(theID);
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_AttributeRefAttr>();
191 boost::shared_ptr<ModelAPI_AttributeRefAttr> aRes = boost::dynamic_pointer_cast<
192 ModelAPI_AttributeRefAttr>(aFound->second);
194 // TODO: generate error on invalid attribute type request
199 boost::shared_ptr<ModelAPI_AttributeRefList> Model_Data::reflist(const std::string& theID)
201 std::map<std::string, boost::shared_ptr<ModelAPI_Attribute> >::iterator aFound = myAttrs.find(theID);
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_AttributeRefList>();
206 boost::shared_ptr<ModelAPI_AttributeRefList> aRes = boost::dynamic_pointer_cast<
207 ModelAPI_AttributeRefList>(aFound->second);
209 // TODO: generate error on invalid attribute type request
214 boost::shared_ptr<ModelAPI_Attribute> Model_Data::attribute(const std::string& theID)
216 boost::shared_ptr<ModelAPI_Attribute> aResult;
217 if (myAttrs.find(theID) == myAttrs.end()) // no such attribute
219 return myAttrs[theID];
222 const std::string& Model_Data::id(const boost::shared_ptr<ModelAPI_Attribute>& theAttr)
224 std::map<std::string, boost::shared_ptr<ModelAPI_Attribute> >::iterator anAttr = myAttrs.begin();
225 for (; anAttr != myAttrs.end(); anAttr++) {
226 if (anAttr->second == theAttr)
227 return anAttr->first;
230 static std::string anEmpty;
234 bool Model_Data::isEqual(const boost::shared_ptr<ModelAPI_Data>& theData)
236 boost::shared_ptr<Model_Data> aData = boost::dynamic_pointer_cast<Model_Data>(theData);
238 return myLab.IsEqual(aData->myLab) == Standard_True ;
242 bool Model_Data::isValid()
244 return !myLab.IsNull() && myLab.HasAttribute();
247 std::list<boost::shared_ptr<ModelAPI_Attribute> > Model_Data::attributes(const std::string& theType)
249 std::list<boost::shared_ptr<ModelAPI_Attribute> > aResult;
250 std::map<std::string, boost::shared_ptr<ModelAPI_Attribute> >::iterator anAttrsIter =
252 for (; anAttrsIter != myAttrs.end(); anAttrsIter++) {
253 if (theType.empty() || anAttrsIter->second->attributeType() == theType) {
254 aResult.push_back(anAttrsIter->second);
260 std::list<std::string> Model_Data::attributesIDs(const std::string& theType)
262 std::list<std::string> aResult;
263 std::map<std::string, boost::shared_ptr<ModelAPI_Attribute> >::iterator anAttrsIter =
265 for (; anAttrsIter != myAttrs.end(); anAttrsIter++) {
266 if (theType.empty() || anAttrsIter->second->attributeType() == theType) {
267 aResult.push_back(anAttrsIter->first);
273 void Model_Data::sendAttributeUpdated(ModelAPI_Attribute* theAttr)
275 theAttr->setInitialized();
276 if (theAttr->isArgument()) {
277 static const Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
278 ModelAPI_EventCreator::get()->sendUpdated(myObject, anEvent);
282 void Model_Data::erase()
285 myLab.ForgetAllAttributes();
288 Standard_GUID kMustBeUpdatedGUID("baede74c-31a6-4416-9c4d-e48ce65f2005");
290 void Model_Data::mustBeUpdated(const bool theFlag)
293 TDataStd_UAttribute::Set(myLab, kMustBeUpdatedGUID);
295 myLab.ForgetAttribute(kMustBeUpdatedGUID);
298 bool Model_Data::mustBeUpdated()
300 return myLab.IsAttribute(kMustBeUpdatedGUID) == Standard_True;