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_AttributeDouble.h>
8 #include <Model_AttributeReference.h>
9 #include <Model_AttributeRefAttr.h>
10 #include <Model_AttributeRefList.h>
11 #include <Model_AttributeBoolean.h>
12 #include <Model_AttributeString.h>
13 #include <Model_Events.h>
15 #include <GeomData_Point.h>
16 #include <GeomData_Point2D.h>
17 #include <GeomData_Dir.h>
18 #include <Events_Loop.h>
19 #include <Events_Error.h>
21 #include <TDataStd_Name.hxx>
23 Model_Data::Model_Data()
27 void Model_Data::setLabel(TDF_Label theLab)
32 std::string Model_Data::name()
34 Handle(TDataStd_Name) aName;
35 if (myLab.FindAttribute(TDataStd_Name::GetID(), aName))
36 return std::string(TCollection_AsciiString(aName->Get()).ToCString());
37 return ""; // not defined
40 void Model_Data::setName(const std::string& theName)
42 bool isModified = false;
43 Handle(TDataStd_Name) aName;
44 if (!myLab.FindAttribute(TDataStd_Name::GetID(), aName)) {
45 TDataStd_Name::Set(myLab, theName.c_str());
48 isModified = !aName->Get().IsEqual(theName.c_str());
50 aName->Set(theName.c_str());
52 // to do not cause the update of the result on name change
54 static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
55 ModelAPI_EventCreator::get()->sendUpdated(myObject, anEvent, false);
59 void Model_Data::addAttribute(const std::string& theID, const std::string theAttrType)
61 TDF_Label anAttrLab = myLab.FindChild(myAttrs.size() + 1);
62 ModelAPI_Attribute* anAttr = 0;
63 if (theAttrType == ModelAPI_AttributeDocRef::type()) {
64 anAttr = new Model_AttributeDocRef(anAttrLab);
65 } else if (theAttrType == ModelAPI_AttributeDouble::type()) {
66 anAttr = new Model_AttributeDouble(anAttrLab);
67 } else if (theAttrType == ModelAPI_AttributeReference::type()) {
68 anAttr = new Model_AttributeReference(anAttrLab);
69 } else if (theAttrType == ModelAPI_AttributeRefAttr::type()) {
70 anAttr = new Model_AttributeRefAttr(anAttrLab);
71 } else if (theAttrType == ModelAPI_AttributeRefList::type()) {
72 anAttr = new Model_AttributeRefList(anAttrLab);
73 } else if (theAttrType == GeomData_Point::type()) {
74 anAttr = new GeomData_Point(anAttrLab);
75 } else if (theAttrType == GeomData_Dir::type()) {
76 anAttr = new GeomData_Dir(anAttrLab);
77 } else if (theAttrType == GeomData_Point2D::type()) {
78 anAttr = new GeomData_Point2D(anAttrLab);
79 } else if (theAttrType == Model_AttributeBoolean::type()) {
80 anAttr = new Model_AttributeBoolean(anAttrLab);
81 } else if (theAttrType == Model_AttributeString::type()) {
82 anAttr = new Model_AttributeString(anAttrLab);
85 myAttrs[theID] = boost::shared_ptr<ModelAPI_Attribute>(anAttr);
86 anAttr->setObject(myObject);
88 Events_Error::send("Can not create unknown type of attribute " + theAttrType);
92 boost::shared_ptr<ModelAPI_AttributeDocRef> Model_Data::docRef(const std::string& theID)
94 std::map<std::string, boost::shared_ptr<ModelAPI_Attribute> >::iterator aFound = myAttrs.find(theID);
95 if (aFound == myAttrs.end()) {
96 // TODO: generate error on unknown attribute request and/or add mechanism for customization
97 return boost::shared_ptr<ModelAPI_AttributeDocRef>();
99 boost::shared_ptr<ModelAPI_AttributeDocRef> aRes = boost::dynamic_pointer_cast<
100 ModelAPI_AttributeDocRef>(aFound->second);
102 // TODO: generate error on invalid attribute type request
107 boost::shared_ptr<ModelAPI_AttributeDouble> Model_Data::real(const std::string& theID)
109 std::map<std::string, boost::shared_ptr<ModelAPI_Attribute> >::iterator aFound = myAttrs.find(theID);
110 if (aFound == myAttrs.end()) {
111 // TODO: generate error on unknown attribute request and/or add mechanism for customization
112 return boost::shared_ptr<ModelAPI_AttributeDouble>();
114 boost::shared_ptr<ModelAPI_AttributeDouble> aRes = boost::dynamic_pointer_cast<
115 ModelAPI_AttributeDouble>(aFound->second);
117 // TODO: generate error on invalid attribute type request
122 boost::shared_ptr<ModelAPI_AttributeBoolean> Model_Data::boolean(const std::string& theID)
124 std::map<std::string, boost::shared_ptr<ModelAPI_Attribute> >::iterator aFound = myAttrs.find(theID);
125 if (aFound == myAttrs.end()) {
126 // TODO: generate error on unknown attribute request and/or add mechanism for customization
127 return boost::shared_ptr<ModelAPI_AttributeBoolean>();
129 boost::shared_ptr<ModelAPI_AttributeBoolean> aRes = boost::dynamic_pointer_cast<
130 ModelAPI_AttributeBoolean>(aFound->second);
132 // TODO: generate error on invalid attribute type request
137 boost::shared_ptr<ModelAPI_AttributeString> Model_Data::string(const std::string& theID)
139 std::map<std::string, boost::shared_ptr<ModelAPI_Attribute> >::iterator aFound = myAttrs.find(theID);
140 if (aFound == myAttrs.end()) {
141 // TODO: generate error on unknown attribute request and/or add mechanism for customization
142 return boost::shared_ptr<ModelAPI_AttributeString>();
144 boost::shared_ptr<ModelAPI_AttributeString> aRes =
145 boost::dynamic_pointer_cast<ModelAPI_AttributeString>(aFound->second);
147 // TODO: generate error on invalid attribute type request
153 boost::shared_ptr<ModelAPI_AttributeReference> Model_Data::reference(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_AttributeReference>();
160 boost::shared_ptr<ModelAPI_AttributeReference> aRes = boost::dynamic_pointer_cast<
161 ModelAPI_AttributeReference>(aFound->second);
163 // TODO: generate error on invalid attribute type request
168 boost::shared_ptr<ModelAPI_AttributeRefAttr> Model_Data::refattr(const std::string& theID)
170 std::map<std::string, boost::shared_ptr<ModelAPI_Attribute> >::iterator aFound = myAttrs.find(theID);
171 if (aFound == myAttrs.end()) {
172 // TODO: generate error on unknown attribute request and/or add mechanism for customization
173 return boost::shared_ptr<ModelAPI_AttributeRefAttr>();
175 boost::shared_ptr<ModelAPI_AttributeRefAttr> aRes = boost::dynamic_pointer_cast<
176 ModelAPI_AttributeRefAttr>(aFound->second);
178 // TODO: generate error on invalid attribute type request
183 boost::shared_ptr<ModelAPI_AttributeRefList> Model_Data::reflist(const std::string& theID)
185 std::map<std::string, boost::shared_ptr<ModelAPI_Attribute> >::iterator aFound = myAttrs.find(theID);
186 if (aFound == myAttrs.end()) {
187 // TODO: generate error on unknown attribute request and/or add mechanism for customization
188 return boost::shared_ptr<ModelAPI_AttributeRefList>();
190 boost::shared_ptr<ModelAPI_AttributeRefList> aRes = boost::dynamic_pointer_cast<
191 ModelAPI_AttributeRefList>(aFound->second);
193 // TODO: generate error on invalid attribute type request
198 boost::shared_ptr<ModelAPI_Attribute> Model_Data::attribute(const std::string& theID)
200 boost::shared_ptr<ModelAPI_Attribute> aResult;
201 if (myAttrs.find(theID) == myAttrs.end()) // no such attribute
203 return myAttrs[theID];
206 const std::string& Model_Data::id(const boost::shared_ptr<ModelAPI_Attribute>& theAttr)
208 std::map<std::string, boost::shared_ptr<ModelAPI_Attribute> >::iterator anAttr = myAttrs.begin();
209 for (; anAttr != myAttrs.end(); anAttr++) {
210 if (anAttr->second == theAttr)
211 return anAttr->first;
214 static std::string anEmpty;
218 bool Model_Data::isEqual(const boost::shared_ptr<ModelAPI_Data>& theData)
220 boost::shared_ptr<Model_Data> aData = boost::dynamic_pointer_cast<Model_Data>(theData);
222 return myLab.IsEqual(aData->myLab) == Standard_True ;
226 bool Model_Data::isValid()
228 return !myLab.IsNull() && myLab.HasAttribute();
231 std::list<boost::shared_ptr<ModelAPI_Attribute> > Model_Data::attributes(const std::string& theType)
233 std::list<boost::shared_ptr<ModelAPI_Attribute> > aResult;
234 std::map<std::string, boost::shared_ptr<ModelAPI_Attribute> >::iterator anAttrsIter = myAttrs.begin();
235 for (; anAttrsIter != myAttrs.end(); anAttrsIter++) {
236 if (theType.empty() || anAttrsIter->second->attributeType() == theType) {
237 aResult.push_back(anAttrsIter->second);
243 void Model_Data::sendAttributeUpdated(ModelAPI_Attribute* theAttr)
245 theAttr->setInitialized();
246 if (theAttr->isArgument()) {
247 static const Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
248 ModelAPI_EventCreator::get()->sendUpdated(myObject, anEvent);