Salome HOME
Task #267: initial implementation of errors management
[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_AttributeInteger.h>
8 #include <Model_AttributeDouble.h>
9 #include <Model_AttributeReference.h>
10 #include <Model_AttributeRefAttr.h>
11 #include <Model_AttributeRefList.h>
12 #include <Model_AttributeBoolean.h>
13 #include <Model_AttributeString.h>
14 #include <Model_AttributeSelection.h>
15 #include <Model_AttributeSelectionList.h>
16 #include <Model_Events.h>
17 #include <ModelAPI_Feature.h>
18 #include <ModelAPI_Result.h>
19 #include <ModelAPI_Validator.h>
20 #include <ModelAPI_Session.h>
21
22 #include <GeomData_Point.h>
23 #include <GeomData_Point2D.h>
24 #include <GeomData_Dir.h>
25 #include <Events_Loop.h>
26 #include <Events_Error.h>
27
28 #include <TDataStd_Name.hxx>
29
30 #include <string>
31
32 // myLab contains:
33 // TDataStd_Name - name of the object
34 // TDataStd_Integer - state of the object execution
35
36 Model_Data::Model_Data()
37 {
38 }
39
40 void Model_Data::setLabel(TDF_Label theLab)
41 {
42   myLab = theLab;
43 }
44
45 std::string Model_Data::name()
46 {
47   Handle(TDataStd_Name) aName;
48   if (myLab.FindAttribute(TDataStd_Name::GetID(), aName))
49     return std::string(TCollection_AsciiString(aName->Get()).ToCString());
50   return "";  // not defined
51 }
52
53 void Model_Data::setName(const std::string& theName)
54 {
55   bool isModified = false;
56   Handle(TDataStd_Name) aName;
57   if (!myLab.FindAttribute(TDataStd_Name::GetID(), aName)) {
58     TDataStd_Name::Set(myLab, theName.c_str());
59     isModified = true;
60   } else {
61     isModified = !aName->Get().IsEqual(theName.c_str());
62     if (isModified)
63       aName->Set(theName.c_str());
64   }
65 }
66
67 void Model_Data::addAttribute(const std::string& theID, const std::string theAttrType)
68 {
69   TDF_Label anAttrLab = myLab.FindChild(myAttrs.size() + 1);
70   ModelAPI_Attribute* anAttr = 0;
71   if (theAttrType == ModelAPI_AttributeDocRef::type()) {
72     anAttr = new Model_AttributeDocRef(anAttrLab);
73   } else if (theAttrType == Model_AttributeInteger::type()) {
74     anAttr = new Model_AttributeInteger(anAttrLab);
75   } else if (theAttrType == ModelAPI_AttributeDouble::type()) {
76     anAttr = new Model_AttributeDouble(anAttrLab);
77   } else if (theAttrType == Model_AttributeBoolean::type()) {
78     anAttr = new Model_AttributeBoolean(anAttrLab);
79   } else if (theAttrType == Model_AttributeString::type()) {
80     anAttr = new Model_AttributeString(anAttrLab);
81   } else if (theAttrType == ModelAPI_AttributeReference::type()) {
82     anAttr = new Model_AttributeReference(anAttrLab);
83   } else if (theAttrType == ModelAPI_AttributeSelection::type()) {
84     anAttr = new Model_AttributeSelection(anAttrLab);
85   } else if (theAttrType == ModelAPI_AttributeSelectionList::type()) {
86     anAttr = new Model_AttributeSelectionList(anAttrLab);
87   } else if (theAttrType == ModelAPI_AttributeRefAttr::type()) {
88     anAttr = new Model_AttributeRefAttr(anAttrLab);
89   } else if (theAttrType == ModelAPI_AttributeRefList::type()) {
90     anAttr = new Model_AttributeRefList(anAttrLab);
91   } 
92   // create also GeomData attributes here because only here the OCAF strucure is known
93   else if (theAttrType == GeomData_Point::type()) {
94     anAttr = new GeomData_Point(anAttrLab);
95   } else if (theAttrType == GeomData_Dir::type()) {
96     anAttr = new GeomData_Dir(anAttrLab);
97   } else if (theAttrType == GeomData_Point2D::type()) {
98     anAttr = new GeomData_Point2D(anAttrLab);
99   }
100   if (anAttr) {
101     myAttrs[theID] = std::shared_ptr<ModelAPI_Attribute>(anAttr);
102     anAttr->setObject(myObject);
103   } else {
104     Events_Error::send("Can not create unknown type of attribute " + theAttrType);
105   }
106 }
107
108 // macro for gthe generic returning of the attribute by the ID
109 #define GET_ATTRIBUTE_BY_ID(ATTR_TYPE, METHOD_NAME) \
110   std::shared_ptr<ATTR_TYPE> Model_Data::METHOD_NAME(const std::string& theID) { \
111     std::shared_ptr<ATTR_TYPE> aRes; \
112     std::map<std::string, AttributePtr >::iterator aFound = \
113       myAttrs.find(theID); \
114     if (aFound != myAttrs.end()) { \
115       aRes = std::dynamic_pointer_cast<ATTR_TYPE>(aFound->second); \
116     } \
117     return aRes; \
118   }
119 // implement nice getting methods for all ModelAPI attributes
120 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeDocRef, document);
121 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeDouble, real);
122 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeInteger, integer);
123 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeBoolean, boolean);
124 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeString, string);
125 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeReference, reference);
126 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeSelection, selection);
127 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeSelectionList, selectionList);
128 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeRefAttr, refattr);
129 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeRefList, reflist);
130
131 std::shared_ptr<ModelAPI_Attribute> Model_Data::attribute(const std::string& theID)
132 {
133   std::shared_ptr<ModelAPI_Attribute> aResult;
134   if (myAttrs.find(theID) == myAttrs.end())  // no such attribute
135     return aResult;
136   return myAttrs[theID];
137 }
138
139 const std::string& Model_Data::id(const std::shared_ptr<ModelAPI_Attribute>& theAttr)
140 {
141   std::map<std::string, std::shared_ptr<ModelAPI_Attribute> >::iterator anAttr = 
142     myAttrs.begin();
143   for (; anAttr != myAttrs.end(); anAttr++) {
144     if (anAttr->second == theAttr)
145       return anAttr->first;
146   }
147   // not found
148   static std::string anEmpty;
149   return anEmpty;
150 }
151
152 bool Model_Data::isEqual(const std::shared_ptr<ModelAPI_Data>& theData)
153 {
154   std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(theData);
155   if (aData)
156     return myLab.IsEqual(aData->myLab) == Standard_True ;
157   return false;
158 }
159
160 bool Model_Data::isValid()
161 {
162   return !myLab.IsNull() && myLab.HasAttribute();
163 }
164
165 std::list<std::shared_ptr<ModelAPI_Attribute> > Model_Data::attributes(const std::string& theType)
166 {
167   std::list<std::shared_ptr<ModelAPI_Attribute> > aResult;
168   std::map<std::string, std::shared_ptr<ModelAPI_Attribute> >::iterator anAttrsIter = 
169     myAttrs.begin();
170   for (; anAttrsIter != myAttrs.end(); anAttrsIter++) {
171     if (theType.empty() || anAttrsIter->second->attributeType() == theType) {
172       aResult.push_back(anAttrsIter->second);
173     }
174   }
175   return aResult;
176 }
177
178 std::list<std::string> Model_Data::attributesIDs(const std::string& theType) 
179 {
180   std::list<std::string> aResult;
181   std::map<std::string, std::shared_ptr<ModelAPI_Attribute> >::iterator anAttrsIter = 
182     myAttrs.begin();
183   for (; anAttrsIter != myAttrs.end(); anAttrsIter++) {
184     if (theType.empty() || anAttrsIter->second->attributeType() == theType) {
185       aResult.push_back(anAttrsIter->first);
186     }
187   }
188   return aResult;
189 }
190
191 void Model_Data::sendAttributeUpdated(ModelAPI_Attribute* theAttr)
192 {
193   theAttr->setInitialized();
194   if (theAttr->isArgument()) {
195     static const Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
196     ModelAPI_EventCreator::get()->sendUpdated(myObject, anEvent);
197     if (myObject) {
198       myObject->attributeChanged();
199     }
200   }
201 }
202
203 void Model_Data::erase()
204 {
205   if (!myLab.IsNull())
206     myLab.ForgetAllAttributes();
207 }
208
209 void Model_Data::execState(const ModelAPI_ExecState theState)
210 {
211   if (theState != ModelAPI_StateNothing)
212     TDataStd_Integer::Set(myLab, (int)theState);
213 }
214
215 ModelAPI_ExecState Model_Data::execState()
216 {
217   Handle(TDataStd_Integer) aStateAttr;
218   if (myLab.FindAttribute(TDataStd_Integer::GetID(), aStateAttr)) {
219     return ModelAPI_ExecState(aStateAttr->Get());
220   }
221   return ModelAPI_StateMustBeUpdated; // default value
222 }
223
224 void Model_Data::setError(const std::string& theError)
225 {
226   execState(ModelAPI_StateExecFailed);
227   Events_Error::send(theError);
228 }
229
230 int Model_Data::featureId() const
231 {
232   return myLab.Father().Tag(); // tag of the feature label
233 }
234
235 void Model_Data::eraseBackReferences()
236 {
237   myRefsToMe.clear();
238   std::shared_ptr<ModelAPI_Result> aRes = 
239     std::dynamic_pointer_cast<ModelAPI_Result>(myObject);
240   if (aRes)
241     aRes->setIsConcealed(false);
242 }
243
244 void Model_Data::addBackReference(FeaturePtr theFeature, std::string theAttrID)
245 {
246   myRefsToMe.insert(theFeature->data()->attribute(theAttrID));
247   if (ModelAPI_Session::get()->validators()->isConcealed(theFeature->getKind(), theAttrID)) {
248     std::shared_ptr<ModelAPI_Result> aRes = 
249       std::dynamic_pointer_cast<ModelAPI_Result>(myObject);
250     if (aRes) {
251       aRes->setIsConcealed(true);
252     }
253   }
254 }
255
256 void Model_Data::referencesToObjects(
257   std::list<std::pair<std::string, std::list<ObjectPtr> > >& theRefs)
258 {
259   std::map<std::string, std::shared_ptr<ModelAPI_Attribute> >::iterator anAttr = myAttrs.begin();
260   std::list<ObjectPtr> aReferenced; // not inside of cycle to avoid excess memory menagement
261   for(; anAttr != myAttrs.end(); anAttr++) {
262     std::string aType = anAttr->second->attributeType();
263     if (aType == ModelAPI_AttributeReference::type()) { // reference to object
264       std::shared_ptr<ModelAPI_AttributeReference> aRef = std::dynamic_pointer_cast<
265           ModelAPI_AttributeReference>(anAttr->second);
266       aReferenced.push_back(aRef->value());
267       theRefs.push_back(std::pair<std::string, std::list<ObjectPtr> >(anAttr->first, aReferenced));
268     } else if (aType == ModelAPI_AttributeRefAttr::type()) { // reference to attribute or object
269       std::shared_ptr<ModelAPI_AttributeRefAttr> aRef = std::dynamic_pointer_cast<
270           ModelAPI_AttributeRefAttr>(anAttr->second);
271       aReferenced.push_back(aRef->isObject() ? aRef->object() : aRef->attr()->owner());
272     } else if (aType == ModelAPI_AttributeRefList::type()) { // list of references
273       aReferenced = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(anAttr->second)->list();
274     } else if (aType == ModelAPI_AttributeSelection::type()) { // selection attribute
275       std::shared_ptr<ModelAPI_AttributeSelection> aRef = std::dynamic_pointer_cast<
276           ModelAPI_AttributeSelection>(anAttr->second);
277       aReferenced.push_back(aRef->context());
278     } else if (aType == ModelAPI_AttributeSelectionList::type()) { // list of selection attributes
279       std::shared_ptr<ModelAPI_AttributeSelectionList> aRef = std::dynamic_pointer_cast<
280           ModelAPI_AttributeSelectionList>(anAttr->second);
281       for(int a = aRef->size() - 1; a >= 0; a--) {
282         aReferenced.push_back(aRef->value(a)->context());
283       }
284     } else
285       continue; // nothing to do, not reference
286
287     if (!aReferenced.empty()) {
288       theRefs.push_back(std::pair<std::string, std::list<ObjectPtr> >(anAttr->first, aReferenced));
289       aReferenced.clear();
290     }
291   }
292 }