]> SALOME platform Git repositories - modules/shaper.git/blob - src/Model/Model_Data.cpp
Salome HOME
Attaching a debug OCAF browser plugin
[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     ModelAPI_EventCreator::get()->sendUpdated(myFeature, anEvent, false);
53   }
54 }
55
56 void Model_Data::addAttribute(string theID, string theAttrType)
57 {
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);
76   else if (theAttrType == Model_AttributeBoolean::type())
77     anAttr = new Model_AttributeBoolean(anAttrLab);
78
79   if (anAttr) {
80     myAttrs[theID] = boost::shared_ptr<ModelAPI_Attribute>(anAttr);
81     anAttr->setFeature(myFeature);
82   }
83   else
84     ; // TODO: generate error on unknown attribute request and/or add mechanism for customization
85 }
86
87 boost::shared_ptr<ModelAPI_AttributeDocRef> Model_Data::docRef(const string theID)
88 {
89   map<string, boost::shared_ptr<ModelAPI_Attribute> >::iterator aFound = myAttrs.find(theID);
90   if (aFound == myAttrs.end()) {
91     // TODO: generate error on unknown attribute request and/or add mechanism for customization
92     return boost::shared_ptr<ModelAPI_AttributeDocRef>();
93   }
94   boost::shared_ptr<ModelAPI_AttributeDocRef> aRes = 
95     boost::dynamic_pointer_cast<ModelAPI_AttributeDocRef>(aFound->second);
96   if (!aRes) {
97     // TODO: generate error on invalid attribute type request
98   }
99   return aRes;
100 }
101
102 boost::shared_ptr<ModelAPI_AttributeDouble> Model_Data::real(const string theID)
103 {
104   map<string, boost::shared_ptr<ModelAPI_Attribute> >::iterator aFound = myAttrs.find(theID);
105   if (aFound == myAttrs.end()) {
106     // TODO: generate error on unknown attribute request and/or add mechanism for customization
107     return boost::shared_ptr<ModelAPI_AttributeDouble>();
108   }
109   boost::shared_ptr<ModelAPI_AttributeDouble> aRes = 
110     boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(aFound->second);
111   if (!aRes) {
112     // TODO: generate error on invalid attribute type request
113   }
114   return aRes;
115 }
116
117 boost::shared_ptr<ModelAPI_AttributeBoolean> Model_Data::boolean(const std::string theID)
118 {
119   map<string, boost::shared_ptr<ModelAPI_Attribute> >::iterator aFound = myAttrs.find(theID);
120   if (aFound == myAttrs.end()) {
121     // TODO: generate error on unknown attribute request and/or add mechanism for customization
122     return boost::shared_ptr<ModelAPI_AttributeBoolean>();
123   }
124   boost::shared_ptr<ModelAPI_AttributeBoolean> aRes = 
125     boost::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(aFound->second);
126   if (!aRes) {
127     // TODO: generate error on invalid attribute type request
128   }
129   return aRes;
130 }
131
132 boost::shared_ptr<ModelAPI_AttributeReference> Model_Data::reference(const string theID)
133 {
134   map<string, boost::shared_ptr<ModelAPI_Attribute> >::iterator aFound = myAttrs.find(theID);
135   if (aFound == myAttrs.end()) {
136     // TODO: generate error on unknown attribute request and/or add mechanism for customization
137     return boost::shared_ptr<ModelAPI_AttributeReference>();
138   }
139   boost::shared_ptr<ModelAPI_AttributeReference> aRes = 
140     boost::dynamic_pointer_cast<ModelAPI_AttributeReference>(aFound->second);
141   if (!aRes) {
142     // TODO: generate error on invalid attribute type request
143   }
144   return aRes;
145 }
146
147 boost::shared_ptr<ModelAPI_AttributeRefAttr> Model_Data::refattr(const string theID)
148 {
149   map<string, boost::shared_ptr<ModelAPI_Attribute> >::iterator aFound = myAttrs.find(theID);
150   if (aFound == myAttrs.end()) {
151     // TODO: generate error on unknown attribute request and/or add mechanism for customization
152     return boost::shared_ptr<ModelAPI_AttributeRefAttr>();
153   }
154   boost::shared_ptr<ModelAPI_AttributeRefAttr> aRes = 
155     boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aFound->second);
156   if (!aRes) {
157     // TODO: generate error on invalid attribute type request
158   }
159   return aRes;
160 }
161
162 boost::shared_ptr<ModelAPI_AttributeRefList> Model_Data::reflist(const string theID)
163 {
164   map<string, boost::shared_ptr<ModelAPI_Attribute> >::iterator aFound = myAttrs.find(theID);
165   if (aFound == myAttrs.end()) {
166     // TODO: generate error on unknown attribute request and/or add mechanism for customization
167     return boost::shared_ptr<ModelAPI_AttributeRefList>();
168   }
169   boost::shared_ptr<ModelAPI_AttributeRefList> aRes = 
170     boost::dynamic_pointer_cast<ModelAPI_AttributeRefList>(aFound->second);
171   if (!aRes) {
172     // TODO: generate error on invalid attribute type request
173   }
174   return aRes;
175 }
176
177 boost::shared_ptr<ModelAPI_Attribute> Model_Data::attribute(const std::string theID)
178 {
179   boost::shared_ptr<ModelAPI_Attribute> aResult;
180   if (myAttrs.find(theID) == myAttrs.end()) // no such attribute
181     return aResult;
182   return myAttrs[theID];
183 }
184
185 const string& Model_Data::id(const boost::shared_ptr<ModelAPI_Attribute> theAttr)
186 {
187   map<string, boost::shared_ptr<ModelAPI_Attribute> >::iterator anAttr = myAttrs.begin();
188   for(; anAttr != myAttrs.end(); anAttr++) {
189     if (anAttr->second == theAttr) return anAttr->first;
190   }
191   // not found
192   static string anEmpty;
193   return anEmpty;
194 }
195
196 bool Model_Data::isEqual(const boost::shared_ptr<ModelAPI_Data> theData)
197 {
198   boost::shared_ptr<Model_Data> aData = boost::dynamic_pointer_cast<Model_Data>(theData);
199   if (aData)
200     return myLab.IsEqual(aData->myLab) == Standard_True;
201   return false;
202 }
203
204 bool Model_Data::isValid()
205 {
206   return !myLab.IsNull() && myLab.HasAttribute();
207 }
208
209 list<boost::shared_ptr<ModelAPI_Attribute> > Model_Data::attributes(const string theType)
210 {
211   list<boost::shared_ptr<ModelAPI_Attribute> > aResult;
212   map<string, boost::shared_ptr<ModelAPI_Attribute> >::iterator anAttrsIter = myAttrs.begin();
213   for(; anAttrsIter != myAttrs.end(); anAttrsIter++) {
214     if (theType.empty() || anAttrsIter->second->attributeType() == theType) {
215       aResult.push_back(anAttrsIter->second);
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   ModelAPI_EventCreator::get()->sendUpdated(myFeature, anEvent);
226 }
227
228 #include <TNaming_Builder.hxx>
229 #include <TNaming_NamedShape.hxx>
230 #include <TopoDS_Shape.hxx>
231 #include <GeomAPI_Shape.h>
232
233 void Model_Data::store(const boost::shared_ptr<GeomAPI_Shape>& theShape)
234 {
235   // the simplest way is to keep this attribute here, on Data
236   // TODO: add naming structure in separated document for shape storage
237   TNaming_Builder aBuilder(myLab);
238   if (!theShape) return; // bad shape
239   TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
240   if (aShape.IsNull()) return; // null shape inside
241
242   aBuilder.Generated(aShape);
243 }
244
245 boost::shared_ptr<GeomAPI_Shape> Model_Data::shape()
246 {
247   Handle(TNaming_NamedShape) aName;
248   if (myLab.FindAttribute(TNaming_NamedShape::GetID(), aName)) {
249     TopoDS_Shape aShape = aName->Get();
250     if (!aShape.IsNull()) {
251       boost::shared_ptr<GeomAPI_Shape> aRes(new GeomAPI_Shape);
252       aRes->setImpl(new TopoDS_Shape(aShape));
253       return aRes;
254     }
255   }
256   return boost::shared_ptr<GeomAPI_Shape>();
257 }