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>
26 Model_Data::Model_Data()
30 void Model_Data::setLabel(TDF_Label theLab)
35 std::string Model_Data::name()
37 Handle(TDataStd_Name) aName;
38 if (myLab.FindAttribute(TDataStd_Name::GetID(), aName))
39 return std::string(TCollection_AsciiString(aName->Get()).ToCString());
40 return ""; // not defined
43 void Model_Data::setName(const std::string& theName)
45 bool isModified = false;
46 Handle(TDataStd_Name) aName;
47 if (!myLab.FindAttribute(TDataStd_Name::GetID(), aName)) {
48 TDataStd_Name::Set(myLab, theName.c_str());
51 isModified = !aName->Get().IsEqual(theName.c_str());
53 aName->Set(theName.c_str());
55 // to do not cause the update of the result on name change
57 static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
58 ModelAPI_EventCreator::get()->sendUpdated(myObject, anEvent, false);
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_AttributeRefAttr::type()) {
79 anAttr = new Model_AttributeRefAttr(anAttrLab);
80 } else if (theAttrType == ModelAPI_AttributeRefList::type()) {
81 anAttr = new Model_AttributeRefList(anAttrLab);
82 } else if (theAttrType == GeomData_Point::type()) {
83 anAttr = new GeomData_Point(anAttrLab);
84 } else if (theAttrType == GeomData_Dir::type()) {
85 anAttr = new GeomData_Dir(anAttrLab);
86 } else if (theAttrType == GeomData_Point2D::type()) {
87 anAttr = new GeomData_Point2D(anAttrLab);
90 myAttrs[theID] = boost::shared_ptr<ModelAPI_Attribute>(anAttr);
91 anAttr->setObject(myObject);
93 Events_Error::send("Can not create unknown type of attribute " + theAttrType);
97 boost::shared_ptr<ModelAPI_AttributeDocRef> Model_Data::docRef(const std::string& theID)
99 std::map<std::string, boost::shared_ptr<ModelAPI_Attribute> >::iterator aFound = myAttrs.find(theID);
100 if (aFound == myAttrs.end()) {
101 // TODO: generate error on unknown attribute request and/or add mechanism for customization
102 return boost::shared_ptr<ModelAPI_AttributeDocRef>();
104 boost::shared_ptr<ModelAPI_AttributeDocRef> aRes = boost::dynamic_pointer_cast<
105 ModelAPI_AttributeDocRef>(aFound->second);
107 // TODO: generate error on invalid attribute type request
112 boost::shared_ptr<ModelAPI_AttributeDouble> Model_Data::real(const std::string& theID)
114 std::map<std::string, boost::shared_ptr<ModelAPI_Attribute> >::iterator aFound = myAttrs.find(theID);
115 if (aFound == myAttrs.end()) {
116 // TODO: generate error on unknown attribute request and/or add mechanism for customization
117 return boost::shared_ptr<ModelAPI_AttributeDouble>();
119 boost::shared_ptr<ModelAPI_AttributeDouble> aRes = boost::dynamic_pointer_cast<
120 ModelAPI_AttributeDouble>(aFound->second);
122 // TODO: generate error on invalid attribute type request
127 boost::shared_ptr<ModelAPI_AttributeInteger> Model_Data::integer(const std::string& theID)
129 std::map<std::string, boost::shared_ptr<ModelAPI_Attribute> >::iterator aFound = myAttrs.find(theID);
130 if (aFound == myAttrs.end()) {
131 // TODO: generate error on unknown attribute request and/or add mechanism for customization
132 return boost::shared_ptr<ModelAPI_AttributeInteger>();
134 boost::shared_ptr<ModelAPI_AttributeInteger> aRes = boost::dynamic_pointer_cast<
135 ModelAPI_AttributeInteger>(aFound->second);
137 // TODO: generate error on invalid attribute type request
142 boost::shared_ptr<ModelAPI_AttributeBoolean> Model_Data::boolean(const std::string& theID)
144 std::map<std::string, boost::shared_ptr<ModelAPI_Attribute> >::iterator aFound = myAttrs.find(theID);
145 if (aFound == myAttrs.end()) {
146 // TODO: generate error on unknown attribute request and/or add mechanism for customization
147 return boost::shared_ptr<ModelAPI_AttributeBoolean>();
149 boost::shared_ptr<ModelAPI_AttributeBoolean> aRes = boost::dynamic_pointer_cast<
150 ModelAPI_AttributeBoolean>(aFound->second);
152 // TODO: generate error on invalid attribute type request
157 boost::shared_ptr<ModelAPI_AttributeString> Model_Data::string(const std::string& theID)
159 std::map<std::string, boost::shared_ptr<ModelAPI_Attribute> >::iterator aFound = myAttrs.find(theID);
160 if (aFound == myAttrs.end()) {
161 // TODO: generate error on unknown attribute request and/or add mechanism for customization
162 return boost::shared_ptr<ModelAPI_AttributeString>();
164 boost::shared_ptr<ModelAPI_AttributeString> aRes =
165 boost::dynamic_pointer_cast<ModelAPI_AttributeString>(aFound->second);
167 // TODO: generate error on invalid attribute type request
173 boost::shared_ptr<ModelAPI_AttributeReference> Model_Data::reference(const std::string& theID)
175 std::map<std::string, boost::shared_ptr<ModelAPI_Attribute> >::iterator aFound = myAttrs.find(theID);
176 if (aFound == myAttrs.end()) {
177 // TODO: generate error on unknown attribute request and/or add mechanism for customization
178 return boost::shared_ptr<ModelAPI_AttributeReference>();
180 boost::shared_ptr<ModelAPI_AttributeReference> aRes = boost::dynamic_pointer_cast<
181 ModelAPI_AttributeReference>(aFound->second);
183 // TODO: generate error on invalid attribute type request
188 boost::shared_ptr<ModelAPI_AttributeRefAttr> Model_Data::refattr(const std::string& theID)
190 std::map<std::string, boost::shared_ptr<ModelAPI_Attribute> >::iterator aFound = myAttrs.find(theID);
191 if (aFound == myAttrs.end()) {
192 // TODO: generate error on unknown attribute request and/or add mechanism for customization
193 return boost::shared_ptr<ModelAPI_AttributeRefAttr>();
195 boost::shared_ptr<ModelAPI_AttributeRefAttr> aRes = boost::dynamic_pointer_cast<
196 ModelAPI_AttributeRefAttr>(aFound->second);
198 // TODO: generate error on invalid attribute type request
203 boost::shared_ptr<ModelAPI_AttributeRefList> Model_Data::reflist(const std::string& theID)
205 std::map<std::string, boost::shared_ptr<ModelAPI_Attribute> >::iterator aFound = myAttrs.find(theID);
206 if (aFound == myAttrs.end()) {
207 // TODO: generate error on unknown attribute request and/or add mechanism for customization
208 return boost::shared_ptr<ModelAPI_AttributeRefList>();
210 boost::shared_ptr<ModelAPI_AttributeRefList> aRes = boost::dynamic_pointer_cast<
211 ModelAPI_AttributeRefList>(aFound->second);
213 // TODO: generate error on invalid attribute type request
218 boost::shared_ptr<ModelAPI_Attribute> Model_Data::attribute(const std::string& theID)
220 boost::shared_ptr<ModelAPI_Attribute> aResult;
221 if (myAttrs.find(theID) == myAttrs.end()) // no such attribute
223 return myAttrs[theID];
226 const std::string& Model_Data::id(const boost::shared_ptr<ModelAPI_Attribute>& theAttr)
228 std::map<std::string, boost::shared_ptr<ModelAPI_Attribute> >::iterator anAttr = myAttrs.begin();
229 for (; anAttr != myAttrs.end(); anAttr++) {
230 if (anAttr->second == theAttr)
231 return anAttr->first;
234 static std::string anEmpty;
238 bool Model_Data::isEqual(const boost::shared_ptr<ModelAPI_Data>& theData)
240 boost::shared_ptr<Model_Data> aData = boost::dynamic_pointer_cast<Model_Data>(theData);
242 return myLab.IsEqual(aData->myLab) == Standard_True ;
246 bool Model_Data::isValid()
248 return !myLab.IsNull() && myLab.HasAttribute();
251 std::list<boost::shared_ptr<ModelAPI_Attribute> > Model_Data::attributes(const std::string& theType)
253 std::list<boost::shared_ptr<ModelAPI_Attribute> > aResult;
254 std::map<std::string, boost::shared_ptr<ModelAPI_Attribute> >::iterator anAttrsIter =
256 for (; anAttrsIter != myAttrs.end(); anAttrsIter++) {
257 if (theType.empty() || anAttrsIter->second->attributeType() == theType) {
258 aResult.push_back(anAttrsIter->second);
264 std::list<std::string> Model_Data::attributesIDs(const std::string& theType)
266 std::list<std::string> aResult;
267 std::map<std::string, boost::shared_ptr<ModelAPI_Attribute> >::iterator anAttrsIter =
269 for (; anAttrsIter != myAttrs.end(); anAttrsIter++) {
270 if (theType.empty() || anAttrsIter->second->attributeType() == theType) {
271 aResult.push_back(anAttrsIter->first);
277 void Model_Data::sendAttributeUpdated(ModelAPI_Attribute* theAttr)
279 theAttr->setInitialized();
280 if (theAttr->isArgument()) {
281 static const Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
282 ModelAPI_EventCreator::get()->sendUpdated(myObject, anEvent);
286 void Model_Data::erase()
289 myLab.ForgetAllAttributes();