]> SALOME platform Git repositories - modules/shaper.git/blob - src/Model/Model_Data.cpp
Salome HOME
Merge branch 'master' of newgeom:newgeom.git
[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 #include <Events_Error.h>
19
20
21 using namespace std;
22
23 Model_Data::Model_Data()
24 {
25 }
26
27 void Model_Data::setLabel(TDF_Label theLab)
28 {
29   myLab = theLab;
30 }
31
32 string Model_Data::name()
33 {
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
38 }
39
40 void Model_Data::setName(const string& theName)
41 {
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());
46     isModified = true;
47   } else {
48     isModified = !aName->Get().IsEqual(theName.c_str());
49     if (isModified)
50       aName->Set(theName.c_str());
51   }
52   // to do not cause the update of the result on name change
53   /*if (isModified) {
54     static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
55     ModelAPI_EventCreator::get()->sendUpdated(myObject, anEvent, false);
56   }*/
57 }
58
59 void Model_Data::addAttribute(const string& theID, const string theAttrType)
60 {
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);
81
82   if (anAttr) {
83     myAttrs[theID] = boost::shared_ptr<ModelAPI_Attribute>(anAttr);
84     anAttr->setObject(myObject);
85   }
86   else {
87     Events_Error::send("Can not create unknown type of attribute " + theAttrType);
88   }
89 }
90
91 boost::shared_ptr<ModelAPI_AttributeDocRef> Model_Data::docRef(const string& theID)
92 {
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>();
97   }
98   boost::shared_ptr<ModelAPI_AttributeDocRef> aRes = 
99     boost::dynamic_pointer_cast<ModelAPI_AttributeDocRef>(aFound->second);
100   if (!aRes) {
101     // TODO: generate error on invalid attribute type request
102   }
103   return aRes;
104 }
105
106 boost::shared_ptr<ModelAPI_AttributeDouble> Model_Data::real(const string& theID)
107 {
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>();
112   }
113   boost::shared_ptr<ModelAPI_AttributeDouble> aRes = 
114     boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(aFound->second);
115   if (!aRes) {
116     // TODO: generate error on invalid attribute type request
117   }
118   return aRes;
119 }
120
121 boost::shared_ptr<ModelAPI_AttributeBoolean> Model_Data::boolean(const std::string& theID)
122 {
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>();
127   }
128   boost::shared_ptr<ModelAPI_AttributeBoolean> aRes = 
129     boost::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(aFound->second);
130   if (!aRes) {
131     // TODO: generate error on invalid attribute type request
132   }
133   return aRes;
134 }
135
136 boost::shared_ptr<ModelAPI_AttributeReference> Model_Data::reference(const string& theID)
137 {
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>();
142   }
143   boost::shared_ptr<ModelAPI_AttributeReference> aRes = 
144     boost::dynamic_pointer_cast<ModelAPI_AttributeReference>(aFound->second);
145   if (!aRes) {
146     // TODO: generate error on invalid attribute type request
147   }
148   return aRes;
149 }
150
151 boost::shared_ptr<ModelAPI_AttributeRefAttr> Model_Data::refattr(const string& theID)
152 {
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>();
157   }
158   boost::shared_ptr<ModelAPI_AttributeRefAttr> aRes = 
159     boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aFound->second);
160   if (!aRes) {
161     // TODO: generate error on invalid attribute type request
162   }
163   return aRes;
164 }
165
166 boost::shared_ptr<ModelAPI_AttributeRefList> Model_Data::reflist(const string& theID)
167 {
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>();
172   }
173   boost::shared_ptr<ModelAPI_AttributeRefList> aRes = 
174     boost::dynamic_pointer_cast<ModelAPI_AttributeRefList>(aFound->second);
175   if (!aRes) {
176     // TODO: generate error on invalid attribute type request
177   }
178   return aRes;
179 }
180
181 boost::shared_ptr<ModelAPI_Attribute> Model_Data::attribute(const std::string& theID)
182 {
183   boost::shared_ptr<ModelAPI_Attribute> aResult;
184   if (myAttrs.find(theID) == myAttrs.end()) // no such attribute
185     return aResult;
186   return myAttrs[theID];
187 }
188
189 const string& Model_Data::id(const boost::shared_ptr<ModelAPI_Attribute>& theAttr)
190 {
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;
194   }
195   // not found
196   static string anEmpty;
197   return anEmpty;
198 }
199
200 bool Model_Data::isEqual(const boost::shared_ptr<ModelAPI_Data>& theData)
201 {
202   boost::shared_ptr<Model_Data> aData = boost::dynamic_pointer_cast<Model_Data>(theData);
203   if (aData)
204     return myLab.IsEqual(aData->myLab) == Standard_True;
205   return false;
206 }
207
208 bool Model_Data::isValid()
209 {
210   return !myLab.IsNull() && myLab.HasAttribute();
211 }
212
213 list<boost::shared_ptr<ModelAPI_Attribute> > Model_Data::attributes(const string& theType)
214 {
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);
220     }
221   }
222   return aResult;
223 }
224
225 void Model_Data::sendAttributeUpdated(ModelAPI_Attribute* theAttr)
226 {
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);
231   }
232 }