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 <GeomData_Point.h>
13 #include <GeomData_Point2D.h>
14 #include <GeomData_Dir.h>
15 #include <TDataStd_Name.hxx>
16 #include "Model_Events.h"
17 #include <Events_Loop.h>
18 #include <Events_Error.h>
23 Model_Data::Model_Data()
27 void Model_Data::setLabel(TDF_Label theLab)
32 string Model_Data::name()
34 Handle(TDataStd_Name) aName;
35 if (myLab.FindAttribute(TDataStd_Name::GetID(), aName))
36 return string(TCollection_AsciiString(aName->Get()).ToCString());
37 return ""; // not defined
40 void Model_Data::setName(const 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 string& theID, const 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);
83 myAttrs[theID] = boost::shared_ptr<ModelAPI_Attribute>(anAttr);
84 anAttr->setObject(myObject);
87 Events_Error::send("Can not create unknown type of attribute " + theAttrType);
91 boost::shared_ptr<ModelAPI_AttributeDocRef> Model_Data::docRef(const string& theID)
93 map<string, boost::shared_ptr<ModelAPI_Attribute> >::iterator aFound = myAttrs.find(theID);
94 if (aFound == myAttrs.end()) {
95 // TODO: generate error on unknown attribute request and/or add mechanism for customization
96 return boost::shared_ptr<ModelAPI_AttributeDocRef>();
98 boost::shared_ptr<ModelAPI_AttributeDocRef> aRes =
99 boost::dynamic_pointer_cast<ModelAPI_AttributeDocRef>(aFound->second);
101 // TODO: generate error on invalid attribute type request
106 boost::shared_ptr<ModelAPI_AttributeDouble> Model_Data::real(const string& theID)
108 map<string, boost::shared_ptr<ModelAPI_Attribute> >::iterator aFound = myAttrs.find(theID);
109 if (aFound == myAttrs.end()) {
110 // TODO: generate error on unknown attribute request and/or add mechanism for customization
111 return boost::shared_ptr<ModelAPI_AttributeDouble>();
113 boost::shared_ptr<ModelAPI_AttributeDouble> aRes =
114 boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(aFound->second);
116 // TODO: generate error on invalid attribute type request
121 boost::shared_ptr<ModelAPI_AttributeBoolean> Model_Data::boolean(const std::string& theID)
123 map<string, boost::shared_ptr<ModelAPI_Attribute> >::iterator aFound = myAttrs.find(theID);
124 if (aFound == myAttrs.end()) {
125 // TODO: generate error on unknown attribute request and/or add mechanism for customization
126 return boost::shared_ptr<ModelAPI_AttributeBoolean>();
128 boost::shared_ptr<ModelAPI_AttributeBoolean> aRes =
129 boost::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(aFound->second);
131 // TODO: generate error on invalid attribute type request
136 boost::shared_ptr<ModelAPI_AttributeReference> Model_Data::reference(const string& theID)
138 map<string, boost::shared_ptr<ModelAPI_Attribute> >::iterator aFound = myAttrs.find(theID);
139 if (aFound == myAttrs.end()) {
140 // TODO: generate error on unknown attribute request and/or add mechanism for customization
141 return boost::shared_ptr<ModelAPI_AttributeReference>();
143 boost::shared_ptr<ModelAPI_AttributeReference> aRes =
144 boost::dynamic_pointer_cast<ModelAPI_AttributeReference>(aFound->second);
146 // TODO: generate error on invalid attribute type request
151 boost::shared_ptr<ModelAPI_AttributeRefAttr> Model_Data::refattr(const string& theID)
153 map<string, boost::shared_ptr<ModelAPI_Attribute> >::iterator aFound = myAttrs.find(theID);
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_AttributeRefAttr>();
158 boost::shared_ptr<ModelAPI_AttributeRefAttr> aRes =
159 boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aFound->second);
161 // TODO: generate error on invalid attribute type request
166 boost::shared_ptr<ModelAPI_AttributeRefList> Model_Data::reflist(const string& theID)
168 map<string, boost::shared_ptr<ModelAPI_Attribute> >::iterator aFound = myAttrs.find(theID);
169 if (aFound == myAttrs.end()) {
170 // TODO: generate error on unknown attribute request and/or add mechanism for customization
171 return boost::shared_ptr<ModelAPI_AttributeRefList>();
173 boost::shared_ptr<ModelAPI_AttributeRefList> aRes =
174 boost::dynamic_pointer_cast<ModelAPI_AttributeRefList>(aFound->second);
176 // TODO: generate error on invalid attribute type request
181 boost::shared_ptr<ModelAPI_Attribute> Model_Data::attribute(const std::string& theID)
183 boost::shared_ptr<ModelAPI_Attribute> aResult;
184 if (myAttrs.find(theID) == myAttrs.end()) // no such attribute
186 return myAttrs[theID];
189 const string& Model_Data::id(const boost::shared_ptr<ModelAPI_Attribute>& theAttr)
191 map<string, boost::shared_ptr<ModelAPI_Attribute> >::iterator anAttr = myAttrs.begin();
192 for(; anAttr != myAttrs.end(); anAttr++) {
193 if (anAttr->second == theAttr) return anAttr->first;
196 static string anEmpty;
200 bool Model_Data::isEqual(const boost::shared_ptr<ModelAPI_Data>& theData)
202 boost::shared_ptr<Model_Data> aData = boost::dynamic_pointer_cast<Model_Data>(theData);
204 return myLab.IsEqual(aData->myLab) == Standard_True;
208 bool Model_Data::isValid()
210 return !myLab.IsNull() && myLab.HasAttribute();
213 list<boost::shared_ptr<ModelAPI_Attribute> > Model_Data::attributes(const string& theType)
215 list<boost::shared_ptr<ModelAPI_Attribute> > aResult;
216 map<string, boost::shared_ptr<ModelAPI_Attribute> >::iterator anAttrsIter = myAttrs.begin();
217 for(; anAttrsIter != myAttrs.end(); anAttrsIter++) {
218 if (theType.empty() || anAttrsIter->second->attributeType() == theType) {
219 aResult.push_back(anAttrsIter->second);
225 void Model_Data::sendAttributeUpdated(ModelAPI_Attribute* theAttr)
227 theAttr->setInitialized();
228 if (theAttr->isArgument()) {
229 static const Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
230 ModelAPI_EventCreator::get()->sendUpdated(myObject, anEvent);