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 bool isModified = false;
40 Handle(TDataStd_Name) aName;
41 if (!myLab.FindAttribute(TDataStd_Name::GetID(), aName)) {
42 TDataStd_Name::Set(myLab, theName.c_str());
45 isModified = !aName->Get().IsEqual(theName.c_str());
47 aName->Set(theName.c_str());
50 static Events_ID anEvent = Events_Loop::eventByName(EVENT_FEATURE_UPDATED);
51 Model_FeatureUpdatedMessage aMsg(myFeature, anEvent);
52 Events_Loop::loop()->send(aMsg, false);
56 void Model_Data::addAttribute(string theID, string theAttrType)
58 TDF_Label anAttrLab = myLab.FindChild(myAttrs.size() + 1);
59 ModelAPI_Attribute* anAttr = 0;
60 if (theAttrType == ModelAPI_AttributeDocRef::type())
61 anAttr = new Model_AttributeDocRef(anAttrLab);
62 else if (theAttrType == ModelAPI_AttributeDouble::type())
63 anAttr = new Model_AttributeDouble(anAttrLab);
64 else if (theAttrType == ModelAPI_AttributeReference::type())
65 anAttr = new Model_AttributeReference(anAttrLab);
66 else if (theAttrType == ModelAPI_AttributeRefAttr::type())
67 anAttr = new Model_AttributeRefAttr(anAttrLab);
68 else if (theAttrType == ModelAPI_AttributeRefList::type())
69 anAttr = new Model_AttributeRefList(anAttrLab);
70 else if (theAttrType == GeomData_Point::type())
71 anAttr = new GeomData_Point(anAttrLab);
72 else if (theAttrType == GeomData_Dir::type())
73 anAttr = new GeomData_Dir(anAttrLab);
74 else if (theAttrType == GeomData_Point2D::type())
75 anAttr = new GeomData_Point2D(anAttrLab);
78 myAttrs[theID] = boost::shared_ptr<ModelAPI_Attribute>(anAttr);
79 anAttr->setFeature(myFeature);
82 ; // TODO: generate error on unknown attribute request and/or add mechanism for customization
85 boost::shared_ptr<ModelAPI_AttributeDocRef> Model_Data::docRef(const string theID)
87 map<string, boost::shared_ptr<ModelAPI_Attribute> >::iterator aFound = myAttrs.find(theID);
88 if (aFound == myAttrs.end()) {
89 // TODO: generate error on unknown attribute request and/or add mechanism for customization
90 return boost::shared_ptr<ModelAPI_AttributeDocRef>();
92 boost::shared_ptr<ModelAPI_AttributeDocRef> aRes =
93 boost::dynamic_pointer_cast<ModelAPI_AttributeDocRef>(aFound->second);
95 // TODO: generate error on invalid attribute type request
100 boost::shared_ptr<ModelAPI_AttributeDouble> Model_Data::real(const string theID)
102 map<string, boost::shared_ptr<ModelAPI_Attribute> >::iterator aFound = myAttrs.find(theID);
103 if (aFound == myAttrs.end()) {
104 // TODO: generate error on unknown attribute request and/or add mechanism for customization
105 return boost::shared_ptr<ModelAPI_AttributeDouble>();
107 boost::shared_ptr<ModelAPI_AttributeDouble> aRes =
108 boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(aFound->second);
110 // TODO: generate error on invalid attribute type request
115 boost::shared_ptr<ModelAPI_AttributeReference> Model_Data::reference(const string theID)
117 map<string, boost::shared_ptr<ModelAPI_Attribute> >::iterator aFound = myAttrs.find(theID);
118 if (aFound == myAttrs.end()) {
119 // TODO: generate error on unknown attribute request and/or add mechanism for customization
120 return boost::shared_ptr<ModelAPI_AttributeReference>();
122 boost::shared_ptr<ModelAPI_AttributeReference> aRes =
123 boost::dynamic_pointer_cast<ModelAPI_AttributeReference>(aFound->second);
125 // TODO: generate error on invalid attribute type request
130 boost::shared_ptr<ModelAPI_AttributeRefAttr> Model_Data::refattr(const string theID)
132 map<string, boost::shared_ptr<ModelAPI_Attribute> >::iterator aFound = myAttrs.find(theID);
133 if (aFound == myAttrs.end()) {
134 // TODO: generate error on unknown attribute request and/or add mechanism for customization
135 return boost::shared_ptr<ModelAPI_AttributeRefAttr>();
137 boost::shared_ptr<ModelAPI_AttributeRefAttr> aRes =
138 boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aFound->second);
140 // TODO: generate error on invalid attribute type request
145 boost::shared_ptr<ModelAPI_AttributeRefList> Model_Data::reflist(const string theID)
147 map<string, boost::shared_ptr<ModelAPI_Attribute> >::iterator aFound = myAttrs.find(theID);
148 if (aFound == myAttrs.end()) {
149 // TODO: generate error on unknown attribute request and/or add mechanism for customization
150 return boost::shared_ptr<ModelAPI_AttributeRefList>();
152 boost::shared_ptr<ModelAPI_AttributeRefList> aRes =
153 boost::dynamic_pointer_cast<ModelAPI_AttributeRefList>(aFound->second);
155 // TODO: generate error on invalid attribute type request
160 boost::shared_ptr<ModelAPI_Attribute> Model_Data::attribute(const std::string theID)
162 boost::shared_ptr<ModelAPI_Attribute> aResult;
163 if (myAttrs.find(theID) == myAttrs.end()) // no such attribute
165 return myAttrs[theID];
168 const string& Model_Data::id(const boost::shared_ptr<ModelAPI_Attribute> theAttr)
170 map<string, boost::shared_ptr<ModelAPI_Attribute> >::iterator anAttr = myAttrs.begin();
171 for(; anAttr != myAttrs.end(); anAttr++) {
172 if (anAttr->second == theAttr) return anAttr->first;
175 static string anEmpty;
179 bool Model_Data::isEqual(const boost::shared_ptr<ModelAPI_Data> theData)
181 boost::shared_ptr<Model_Data> aData = boost::dynamic_pointer_cast<Model_Data>(theData);
183 return myLab.IsEqual(aData->myLab) == Standard_True;
187 bool Model_Data::isValid()
189 return !myLab.IsNull() && myLab.HasAttribute();
192 #include <TNaming_Builder.hxx>
193 #include <TNaming_NamedShape.hxx>
194 #include <TopoDS_Shape.hxx>
195 #include <GeomAPI_Shape.h>
197 void Model_Data::store(const boost::shared_ptr<GeomAPI_Shape>& theShape)
199 // the simplest way is to keep this attribute here, on Data
200 // TODO: add naming structure in separated document for shape storage
201 TNaming_Builder aBuilder(myLab);
202 if (!theShape) return; // bad shape
203 TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
204 if (aShape.IsNull()) return; // null shape inside
206 aBuilder.Generated(aShape);
209 boost::shared_ptr<GeomAPI_Shape> Model_Data::shape()
211 Handle(TNaming_NamedShape) aName;
212 if (myLab.FindAttribute(TNaming_NamedShape::GetID(), aName)) {
213 TopoDS_Shape aShape = aName->Get();
214 if (!aShape.IsNull()) {
215 boost::shared_ptr<GeomAPI_Shape> aRes(new GeomAPI_Shape);
216 aRes->setImpl(new TopoDS_Shape(aShape));
220 return boost::shared_ptr<GeomAPI_Shape>();