]> SALOME platform Git repositories - modules/shaper.git/blob - src/Model/Model_Data.cpp
Salome HOME
Added the initialization of attributes flag and "attributes": method that returns...
[modules/shaper.git] / src / Model / Model_Data.cpp
1 // File:        Model_Data.hxx
2 // Created:     21 Mar 2014
3 // Author:      Mikhail PONIKAROV
4
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
19 using namespace std;
20
21 Model_Data::Model_Data()
22 {
23 }
24
25 void Model_Data::setLabel(TDF_Label& theLab)
26 {
27   myLab = theLab;
28 }
29
30 string Model_Data::getName()
31 {
32   Handle(TDataStd_Name) aName;
33   if (myLab.FindAttribute(TDataStd_Name::GetID(), aName))
34     return string(TCollection_AsciiString(aName->Get()).ToCString());
35   return ""; // not defined
36 }
37
38 void Model_Data::setName(string theName)
39 {
40   bool isModified = false;
41   Handle(TDataStd_Name) aName;
42   if (!myLab.FindAttribute(TDataStd_Name::GetID(), aName)) {
43     TDataStd_Name::Set(myLab, theName.c_str());
44     isModified = true;
45   } else {
46     isModified = !aName->Get().IsEqual(theName.c_str());
47     if (isModified)
48       aName->Set(theName.c_str());
49   }
50   if (isModified) {
51     static Events_ID anEvent = Events_Loop::eventByName(EVENT_FEATURE_UPDATED);
52     Model_FeatureUpdatedMessage aMsg(myFeature, anEvent);
53     Events_Loop::loop()->send(aMsg, false);
54   }
55 }
56
57 void Model_Data::addAttribute(string theID, string theAttrType)
58 {
59   TDF_Label anAttrLab = myLab.FindChild(myAttrs.size() + 1);
60   ModelAPI_Attribute* anAttr = 0;
61   if (theAttrType == ModelAPI_AttributeDocRef::type())
62     anAttr = new Model_AttributeDocRef(anAttrLab);
63   else if (theAttrType == ModelAPI_AttributeDouble::type())
64     anAttr = new Model_AttributeDouble(anAttrLab);
65   else if (theAttrType == ModelAPI_AttributeReference::type())
66     anAttr = new Model_AttributeReference(anAttrLab);
67   else if (theAttrType == ModelAPI_AttributeRefAttr::type())
68     anAttr = new Model_AttributeRefAttr(anAttrLab);
69   else if (theAttrType == ModelAPI_AttributeRefList::type())
70     anAttr = new Model_AttributeRefList(anAttrLab);
71   else if (theAttrType == GeomData_Point::type())
72     anAttr = new GeomData_Point(anAttrLab);
73   else if (theAttrType == GeomData_Dir::type())
74     anAttr = new GeomData_Dir(anAttrLab);
75   else if (theAttrType == GeomData_Point2D::type())
76     anAttr = new GeomData_Point2D(anAttrLab);
77   else if (theAttrType == Model_AttributeBoolean::type())
78     anAttr = new Model_AttributeBoolean(anAttrLab);
79
80   if (anAttr) {
81     myAttrs[theID] = boost::shared_ptr<ModelAPI_Attribute>(anAttr);
82     anAttr->setFeature(myFeature);
83   }
84   else
85     ; // TODO: generate error on unknown attribute request and/or add mechanism for customization
86 }
87
88 boost::shared_ptr<ModelAPI_AttributeDocRef> Model_Data::docRef(const string theID)
89 {
90   map<string, boost::shared_ptr<ModelAPI_Attribute> >::iterator aFound = myAttrs.find(theID);
91   if (aFound == myAttrs.end()) {
92     // TODO: generate error on unknown attribute request and/or add mechanism for customization
93     return boost::shared_ptr<ModelAPI_AttributeDocRef>();
94   }
95   boost::shared_ptr<ModelAPI_AttributeDocRef> aRes = 
96     boost::dynamic_pointer_cast<ModelAPI_AttributeDocRef>(aFound->second);
97   if (!aRes) {
98     // TODO: generate error on invalid attribute type request
99   }
100   return aRes;
101 }
102
103 boost::shared_ptr<ModelAPI_AttributeDouble> Model_Data::real(const string theID)
104 {
105   map<string, boost::shared_ptr<ModelAPI_Attribute> >::iterator aFound = myAttrs.find(theID);
106   if (aFound == myAttrs.end()) {
107     // TODO: generate error on unknown attribute request and/or add mechanism for customization
108     return boost::shared_ptr<ModelAPI_AttributeDouble>();
109   }
110   boost::shared_ptr<ModelAPI_AttributeDouble> aRes = 
111     boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(aFound->second);
112   if (!aRes) {
113     // TODO: generate error on invalid attribute type request
114   }
115   return aRes;
116 }
117
118 boost::shared_ptr<ModelAPI_AttributeBoolean> Model_Data::boolean(const std::string theID)
119 {
120   map<string, boost::shared_ptr<ModelAPI_Attribute> >::iterator aFound = myAttrs.find(theID);
121   if (aFound == myAttrs.end()) {
122     // TODO: generate error on unknown attribute request and/or add mechanism for customization
123     return boost::shared_ptr<ModelAPI_AttributeBoolean>();
124   }
125   boost::shared_ptr<ModelAPI_AttributeBoolean> aRes = 
126     boost::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(aFound->second);
127   if (!aRes) {
128     // TODO: generate error on invalid attribute type request
129   }
130   return aRes;
131 }
132
133 boost::shared_ptr<ModelAPI_AttributeReference> Model_Data::reference(const string theID)
134 {
135   map<string, boost::shared_ptr<ModelAPI_Attribute> >::iterator aFound = myAttrs.find(theID);
136   if (aFound == myAttrs.end()) {
137     // TODO: generate error on unknown attribute request and/or add mechanism for customization
138     return boost::shared_ptr<ModelAPI_AttributeReference>();
139   }
140   boost::shared_ptr<ModelAPI_AttributeReference> aRes = 
141     boost::dynamic_pointer_cast<ModelAPI_AttributeReference>(aFound->second);
142   if (!aRes) {
143     // TODO: generate error on invalid attribute type request
144   }
145   return aRes;
146 }
147
148 boost::shared_ptr<ModelAPI_AttributeRefAttr> Model_Data::refattr(const string theID)
149 {
150   map<string, boost::shared_ptr<ModelAPI_Attribute> >::iterator aFound = myAttrs.find(theID);
151   if (aFound == myAttrs.end()) {
152     // TODO: generate error on unknown attribute request and/or add mechanism for customization
153     return boost::shared_ptr<ModelAPI_AttributeRefAttr>();
154   }
155   boost::shared_ptr<ModelAPI_AttributeRefAttr> aRes = 
156     boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aFound->second);
157   if (!aRes) {
158     // TODO: generate error on invalid attribute type request
159   }
160   return aRes;
161 }
162
163 boost::shared_ptr<ModelAPI_AttributeRefList> Model_Data::reflist(const string theID)
164 {
165   map<string, boost::shared_ptr<ModelAPI_Attribute> >::iterator aFound = myAttrs.find(theID);
166   if (aFound == myAttrs.end()) {
167     // TODO: generate error on unknown attribute request and/or add mechanism for customization
168     return boost::shared_ptr<ModelAPI_AttributeRefList>();
169   }
170   boost::shared_ptr<ModelAPI_AttributeRefList> aRes = 
171     boost::dynamic_pointer_cast<ModelAPI_AttributeRefList>(aFound->second);
172   if (!aRes) {
173     // TODO: generate error on invalid attribute type request
174   }
175   return aRes;
176 }
177
178 boost::shared_ptr<ModelAPI_Attribute> Model_Data::attribute(const std::string theID)
179 {
180   boost::shared_ptr<ModelAPI_Attribute> aResult;
181   if (myAttrs.find(theID) == myAttrs.end()) // no such attribute
182     return aResult;
183   return myAttrs[theID];
184 }
185
186 const string& Model_Data::id(const boost::shared_ptr<ModelAPI_Attribute> theAttr)
187 {
188   map<string, boost::shared_ptr<ModelAPI_Attribute> >::iterator anAttr = myAttrs.begin();
189   for(; anAttr != myAttrs.end(); anAttr++) {
190     if (anAttr->second == theAttr) return anAttr->first;
191   }
192   // not found
193   static string anEmpty;
194   return anEmpty;
195 }
196
197 bool Model_Data::isEqual(const boost::shared_ptr<ModelAPI_Data> theData)
198 {
199   boost::shared_ptr<Model_Data> aData = boost::dynamic_pointer_cast<Model_Data>(theData);
200   if (aData)
201     return myLab.IsEqual(aData->myLab) == Standard_True;
202   return false;
203 }
204
205 bool Model_Data::isValid()
206 {
207   return !myLab.IsNull() && myLab.HasAttribute();
208 }
209
210 list<boost::shared_ptr<ModelAPI_Attribute> > Model_Data::attributes(const string theType)
211 {
212   list<boost::shared_ptr<ModelAPI_Attribute> > aResult;
213   map<string, boost::shared_ptr<ModelAPI_Attribute> >::iterator anAttrsIter = myAttrs.begin();
214   for(; anAttrsIter != myAttrs.end(); anAttrsIter++) {
215     if (theType.empty() || anAttrsIter->second->attributeType() == theType) {
216     }
217   }
218   return aResult;
219 }
220
221 void Model_Data::sendAttributeUpdated(ModelAPI_Attribute* theAttr)
222 {
223   theAttr->setInitialized();
224   static const Events_ID anEvent = Events_Loop::eventByName(EVENT_FEATURE_UPDATED);
225   Model_FeatureUpdatedMessage aMsg(myFeature, anEvent);
226   Events_Loop::loop()->send(aMsg);
227 }
228
229 #include <TNaming_Builder.hxx>
230 #include <TNaming_NamedShape.hxx>
231 #include <TopoDS_Shape.hxx>
232 #include <GeomAPI_Shape.h>
233
234 void Model_Data::store(const boost::shared_ptr<GeomAPI_Shape>& theShape)
235 {
236   // the simplest way is to keep this attribute here, on Data
237   // TODO: add naming structure in separated document for shape storage
238   TNaming_Builder aBuilder(myLab);
239   if (!theShape) return; // bad shape
240   TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
241   if (aShape.IsNull()) return; // null shape inside
242
243   aBuilder.Generated(aShape);
244 }
245
246 boost::shared_ptr<GeomAPI_Shape> Model_Data::shape()
247 {
248   Handle(TNaming_NamedShape) aName;
249   if (myLab.FindAttribute(TNaming_NamedShape::GetID(), aName)) {
250     TopoDS_Shape aShape = aName->Get();
251     if (!aShape.IsNull()) {
252       boost::shared_ptr<GeomAPI_Shape> aRes(new GeomAPI_Shape);
253       aRes->setImpl(new TopoDS_Shape(aShape));
254       return aRes;
255     }
256   }
257   return boost::shared_ptr<GeomAPI_Shape>();
258 }