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 <GeomData_Point.h>
12 #include <GeomData_Point2D.h>
13 #include <GeomData_Dir.h>
14 #include <TDataStd_Name.hxx>
15 #include "Model_Events.h"
16 #include <Events_Loop.h>
20 Model_Data::Model_Data()
24 void Model_Data::setLabel(TDF_Label& theLab)
29 string Model_Data::getName()
31 Handle(TDataStd_Name) aName;
32 if (myLab.FindAttribute(TDataStd_Name::GetID(), aName))
33 return string(TCollection_AsciiString(aName->Get()).ToCString());
34 return ""; // not defined
37 void Model_Data::setName(string theName)
39 TDataStd_Name::Set(myLab, theName.c_str());
40 static Events_ID anEvent = Events_Loop::eventByName(EVENT_FEATURE_UPDATED);
41 Model_FeatureUpdatedMessage aMsg(myFeature, anEvent);
42 Events_Loop::loop()->send(aMsg, false);
45 void Model_Data::addAttribute(string theID, string theAttrType)
47 TDF_Label anAttrLab = myLab.FindChild(myAttrs.size() + 1);
48 ModelAPI_Attribute* anAttr = 0;
49 if (theAttrType == ModelAPI_AttributeDocRef::type())
50 anAttr = new Model_AttributeDocRef(anAttrLab);
51 else if (theAttrType == ModelAPI_AttributeDouble::type())
52 anAttr = new Model_AttributeDouble(anAttrLab);
53 else if (theAttrType == ModelAPI_AttributeReference::type())
54 anAttr = new Model_AttributeReference(anAttrLab);
55 else if (theAttrType == ModelAPI_AttributeRefAttr::type())
56 anAttr = new Model_AttributeRefAttr(anAttrLab);
57 else if (theAttrType == ModelAPI_AttributeRefList::type())
58 anAttr = new Model_AttributeRefList(anAttrLab);
59 else if (theAttrType == GeomData_Point::type())
60 anAttr = new GeomData_Point(anAttrLab);
61 else if (theAttrType == GeomData_Dir::type())
62 anAttr = new GeomData_Dir(anAttrLab);
63 else if (theAttrType == GeomData_Point2D::type())
64 anAttr = new GeomData_Point2D(anAttrLab);
67 myAttrs[theID] = boost::shared_ptr<ModelAPI_Attribute>(anAttr);
68 anAttr->setFeature(myFeature);
71 ; // TODO: generate error on unknown attribute request and/or add mechanism for customization
74 boost::shared_ptr<ModelAPI_AttributeDocRef> Model_Data::docRef(const string theID)
76 map<string, boost::shared_ptr<ModelAPI_Attribute> >::iterator aFound = myAttrs.find(theID);
77 if (aFound == myAttrs.end()) {
78 // TODO: generate error on unknown attribute request and/or add mechanism for customization
79 return boost::shared_ptr<ModelAPI_AttributeDocRef>();
81 boost::shared_ptr<ModelAPI_AttributeDocRef> aRes =
82 boost::dynamic_pointer_cast<ModelAPI_AttributeDocRef>(aFound->second);
84 // TODO: generate error on invalid attribute type request
89 boost::shared_ptr<ModelAPI_AttributeDouble> Model_Data::real(const string theID)
91 map<string, boost::shared_ptr<ModelAPI_Attribute> >::iterator aFound = myAttrs.find(theID);
92 if (aFound == myAttrs.end()) {
93 // TODO: generate error on unknown attribute request and/or add mechanism for customization
94 return boost::shared_ptr<ModelAPI_AttributeDouble>();
96 boost::shared_ptr<ModelAPI_AttributeDouble> aRes =
97 boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(aFound->second);
99 // TODO: generate error on invalid attribute type request
104 boost::shared_ptr<ModelAPI_AttributeReference> Model_Data::reference(const string theID)
106 map<string, boost::shared_ptr<ModelAPI_Attribute> >::iterator aFound = myAttrs.find(theID);
107 if (aFound == myAttrs.end()) {
108 // TODO: generate error on unknown attribute request and/or add mechanism for customization
109 return boost::shared_ptr<ModelAPI_AttributeReference>();
111 boost::shared_ptr<ModelAPI_AttributeReference> aRes =
112 boost::dynamic_pointer_cast<ModelAPI_AttributeReference>(aFound->second);
114 // TODO: generate error on invalid attribute type request
119 boost::shared_ptr<ModelAPI_AttributeRefAttr> Model_Data::refattr(const string theID)
121 map<string, boost::shared_ptr<ModelAPI_Attribute> >::iterator aFound = myAttrs.find(theID);
122 if (aFound == myAttrs.end()) {
123 // TODO: generate error on unknown attribute request and/or add mechanism for customization
124 return boost::shared_ptr<ModelAPI_AttributeRefAttr>();
126 boost::shared_ptr<ModelAPI_AttributeRefAttr> aRes =
127 boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aFound->second);
129 // TODO: generate error on invalid attribute type request
134 boost::shared_ptr<ModelAPI_AttributeRefList> Model_Data::reflist(const string theID)
136 map<string, boost::shared_ptr<ModelAPI_Attribute> >::iterator aFound = myAttrs.find(theID);
137 if (aFound == myAttrs.end()) {
138 // TODO: generate error on unknown attribute request and/or add mechanism for customization
139 return boost::shared_ptr<ModelAPI_AttributeRefList>();
141 boost::shared_ptr<ModelAPI_AttributeRefList> aRes =
142 boost::dynamic_pointer_cast<ModelAPI_AttributeRefList>(aFound->second);
144 // TODO: generate error on invalid attribute type request
149 boost::shared_ptr<ModelAPI_Attribute> Model_Data::attribute(const std::string theID)
151 boost::shared_ptr<ModelAPI_Attribute> aResult;
152 if (myAttrs.find(theID) == myAttrs.end()) // no such attribute
154 return myAttrs[theID];
157 const string& Model_Data::id(const boost::shared_ptr<ModelAPI_Attribute> theAttr)
159 map<string, boost::shared_ptr<ModelAPI_Attribute> >::iterator anAttr = myAttrs.begin();
160 for(; anAttr != myAttrs.end(); anAttr++) {
161 if (anAttr->second == theAttr) return anAttr->first;
164 static string anEmpty;
168 bool Model_Data::isEqual(const boost::shared_ptr<ModelAPI_Data> theData)
170 boost::shared_ptr<Model_Data> aData = boost::dynamic_pointer_cast<Model_Data>(theData);
172 return myLab.IsEqual(aData->myLab) == Standard_True;
176 bool Model_Data::isValid()
178 return !myLab.IsNull() && myLab.HasAttribute();