Salome HOME
Parts results and activation/deactivation management (show/hide also)
[modules/shaper.git] / src / Model / Model_Data.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        Model_Data.hxx
4 // Created:     21 Mar 2014
5 // Author:      Mikhail PONIKAROV
6
7 #include <Model_Data.h>
8 #include <Model_AttributeDocRef.h>
9 #include <Model_AttributeInteger.h>
10 #include <Model_AttributeDouble.h>
11 #include <Model_AttributeReference.h>
12 #include <Model_AttributeRefAttr.h>
13 #include <Model_AttributeRefList.h>
14 #include <Model_AttributeBoolean.h>
15 #include <Model_AttributeString.h>
16 #include <Model_AttributeSelection.h>
17 #include <Model_AttributeSelectionList.h>
18 #include <Model_AttributeIntArray.h>
19 #include <Model_Events.h>
20 #include <ModelAPI_Feature.h>
21 #include <ModelAPI_Result.h>
22 #include <ModelAPI_Validator.h>
23 #include <ModelAPI_Session.h>
24
25 #include <GeomData_Point.h>
26 #include <GeomData_Point2D.h>
27 #include <GeomData_Dir.h>
28 #include <Events_Loop.h>
29 #include <Events_Error.h>
30
31 #include <TDataStd_Name.hxx>
32 #include <TDataStd_AsciiString.hxx>
33 #include <TDF_AttributeIterator.hxx>
34 #include <TDF_ChildIterator.hxx>
35 #include <TDF_RelocationTable.hxx>
36
37 #include <string>
38
39 // myLab contains:
40 // TDataStd_Name - name of the object
41 // TDataStd_Integer - state of the object execution
42 // TDataStd_BooleanArray - array of flags of this data:
43 //                             0 - is in history or not
44 static const int kFlagInHistory = 0;
45 //                             1 - is displayed or not
46 static const int kFlagDisplayed = 1;
47
48 // invalid data
49 const static std::shared_ptr<ModelAPI_Data> kInvalid(new Model_Data());
50
51 Model_Data::Model_Data() : mySendAttributeUpdated(true)
52 {
53 }
54
55 void Model_Data::setLabel(TDF_Label theLab)
56 {
57   myLab = theLab;
58   // set or get the default flags
59   if (!myLab.FindAttribute(TDataStd_BooleanArray::GetID(), myFlags)) {
60     // set default values if not found
61     myFlags = TDataStd_BooleanArray::Set(myLab, 0, 1);
62     myFlags->SetValue(kFlagInHistory, Standard_True); // is in history by default is true
63     myFlags->SetValue(kFlagDisplayed, Standard_True); // is displayed by default is true
64   }
65 }
66
67 std::string Model_Data::name()
68 {
69   Handle(TDataStd_Name) aName;
70   if (myLab.FindAttribute(TDataStd_Name::GetID(), aName))
71     return std::string(TCollection_AsciiString(aName->Get()).ToCString());
72   return "";  // not defined
73 }
74
75 void Model_Data::setName(const std::string& theName)
76 {
77   bool isModified = false;
78   Handle(TDataStd_Name) aName;
79   if (!myLab.FindAttribute(TDataStd_Name::GetID(), aName)) {
80     TDataStd_Name::Set(myLab, theName.c_str());
81     isModified = true;
82   } else {
83     isModified = !aName->Get().IsEqual(theName.c_str());
84     if (isModified)
85       aName->Set(theName.c_str());
86   }
87 }
88
89 AttributePtr Model_Data::addAttribute(const std::string& theID, const std::string theAttrType)
90 {
91   AttributePtr aResult;
92   TDF_Label anAttrLab = myLab.FindChild(myAttrs.size() + 1);
93   ModelAPI_Attribute* anAttr = 0;
94   if (theAttrType == ModelAPI_AttributeDocRef::typeId()) {
95     anAttr = new Model_AttributeDocRef(anAttrLab);
96   } else if (theAttrType == Model_AttributeInteger::typeId()) {
97     anAttr = new Model_AttributeInteger(anAttrLab);
98   } else if (theAttrType == ModelAPI_AttributeDouble::typeId()) {
99     anAttr = new Model_AttributeDouble(anAttrLab);
100   } else if (theAttrType == Model_AttributeBoolean::typeId()) {
101     anAttr = new Model_AttributeBoolean(anAttrLab);
102   } else if (theAttrType == Model_AttributeString::typeId()) {
103     anAttr = new Model_AttributeString(anAttrLab);
104   } else if (theAttrType == ModelAPI_AttributeReference::typeId()) {
105     anAttr = new Model_AttributeReference(anAttrLab);
106   } else if (theAttrType == ModelAPI_AttributeSelection::typeId()) {
107     anAttr = new Model_AttributeSelection(anAttrLab);
108   } else if (theAttrType == ModelAPI_AttributeSelectionList::typeId()) {
109     anAttr = new Model_AttributeSelectionList(anAttrLab);
110   } else if (theAttrType == ModelAPI_AttributeRefAttr::typeId()) {
111     anAttr = new Model_AttributeRefAttr(anAttrLab);
112   } else if (theAttrType == ModelAPI_AttributeRefList::typeId()) {
113     anAttr = new Model_AttributeRefList(anAttrLab);
114   } else if (theAttrType == ModelAPI_AttributeIntArray::typeId()) {
115     anAttr = new Model_AttributeIntArray(anAttrLab);
116   } 
117   // create also GeomData attributes here because only here the OCAF strucure is known
118   else if (theAttrType == GeomData_Point::typeId()) {
119     anAttr = new GeomData_Point(anAttrLab);
120   } else if (theAttrType == GeomData_Dir::typeId()) {
121     anAttr = new GeomData_Dir(anAttrLab);
122   } else if (theAttrType == GeomData_Point2D::typeId()) {
123     anAttr = new GeomData_Point2D(anAttrLab);
124   }
125   if (anAttr) {
126     aResult = std::shared_ptr<ModelAPI_Attribute>(anAttr);
127     myAttrs[theID] = aResult;
128     anAttr->setObject(myObject);
129     anAttr->setID(theID);
130   } else {
131     Events_Error::send("Can not create unknown type of attribute " + theAttrType);
132   }
133   return aResult;
134 }
135
136 // macro for gthe generic returning of the attribute by the ID
137 #define GET_ATTRIBUTE_BY_ID(ATTR_TYPE, METHOD_NAME) \
138   std::shared_ptr<ATTR_TYPE> Model_Data::METHOD_NAME(const std::string& theID) { \
139     std::shared_ptr<ATTR_TYPE> aRes; \
140     std::map<std::string, AttributePtr >::iterator aFound = \
141       myAttrs.find(theID); \
142     if (aFound != myAttrs.end()) { \
143       aRes = std::dynamic_pointer_cast<ATTR_TYPE>(aFound->second); \
144     } \
145     return aRes; \
146   }
147 // implement nice getting methods for all ModelAPI attributes
148 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeDocRef, document);
149 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeDouble, real);
150 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeInteger, integer);
151 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeBoolean, boolean);
152 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeString, string);
153 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeReference, reference);
154 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeSelection, selection);
155 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeSelectionList, selectionList);
156 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeRefAttr, refattr);
157 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeRefList, reflist);
158 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeIntArray, intArray);
159
160 std::shared_ptr<ModelAPI_Attribute> Model_Data::attribute(const std::string& theID)
161 {
162   std::shared_ptr<ModelAPI_Attribute> aResult;
163   if (myAttrs.find(theID) == myAttrs.end())  // no such attribute
164     return aResult;
165   return myAttrs[theID];
166 }
167
168 const std::string& Model_Data::id(const std::shared_ptr<ModelAPI_Attribute>& theAttr)
169 {
170   std::map<std::string, std::shared_ptr<ModelAPI_Attribute> >::iterator anAttr = 
171     myAttrs.begin();
172   for (; anAttr != myAttrs.end(); anAttr++) {
173     if (anAttr->second == theAttr)
174       return anAttr->first;
175   }
176   // not found
177   static std::string anEmpty;
178   return anEmpty;
179 }
180
181 bool Model_Data::isEqual(const std::shared_ptr<ModelAPI_Data>& theData)
182 {
183   std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(theData);
184   if (aData)
185     return myLab.IsEqual(aData->myLab) == Standard_True ;
186   return false;
187 }
188
189 bool Model_Data::isValid()
190 {
191   return !myLab.IsNull() && myLab.HasAttribute();
192 }
193
194 std::list<std::shared_ptr<ModelAPI_Attribute> > Model_Data::attributes(const std::string& theType)
195 {
196   std::list<std::shared_ptr<ModelAPI_Attribute> > aResult;
197   std::map<std::string, std::shared_ptr<ModelAPI_Attribute> >::iterator anAttrsIter = 
198     myAttrs.begin();
199   for (; anAttrsIter != myAttrs.end(); anAttrsIter++) {
200     if (theType.empty() || anAttrsIter->second->attributeType() == theType) {
201       aResult.push_back(anAttrsIter->second);
202     }
203   }
204   return aResult;
205 }
206
207 std::list<std::string> Model_Data::attributesIDs(const std::string& theType) 
208 {
209   std::list<std::string> aResult;
210   std::map<std::string, std::shared_ptr<ModelAPI_Attribute> >::iterator anAttrsIter = 
211     myAttrs.begin();
212   for (; anAttrsIter != myAttrs.end(); anAttrsIter++) {
213     if (theType.empty() || anAttrsIter->second->attributeType() == theType) {
214       aResult.push_back(anAttrsIter->first);
215     }
216   }
217   return aResult;
218 }
219
220 void Model_Data::sendAttributeUpdated(ModelAPI_Attribute* theAttr)
221 {
222   theAttr->setInitialized();
223   if (theAttr->isArgument()) {
224     static const Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
225     ModelAPI_EventCreator::get()->sendUpdated(myObject, anEvent);
226     if (mySendAttributeUpdated && myObject) {
227       myObject->attributeChanged(theAttr->id());
228     }
229   }
230 }
231
232 void Model_Data::blockSendAttributeUpdated(const bool theBlock)
233 {
234   mySendAttributeUpdated = !theBlock;
235 }
236
237 void Model_Data::erase()
238 {
239   if (!myLab.IsNull())
240     myLab.ForgetAllAttributes();
241 }
242
243 void Model_Data::execState(const ModelAPI_ExecState theState)
244 {
245   if (theState != ModelAPI_StateNothing)
246     TDataStd_Integer::Set(myLab, (int)theState);
247 }
248
249 ModelAPI_ExecState Model_Data::execState()
250 {
251   Handle(TDataStd_Integer) aStateAttr;
252   if (myLab.FindAttribute(TDataStd_Integer::GetID(), aStateAttr)) {
253     return ModelAPI_ExecState(aStateAttr->Get());
254   }
255   return ModelAPI_StateMustBeUpdated; // default value
256 }
257
258 void Model_Data::setError(const std::string& theError, bool theSend)
259 {
260   execState(ModelAPI_StateExecFailed);
261   if (theSend) {
262     Events_Error::send(theError);
263   }
264   TDataStd_AsciiString::Set(myLab, theError.c_str());
265 }
266
267 std::string Model_Data::error() const
268 {
269   Handle(TDataStd_AsciiString) anErrorAttr;
270   if (myLab.FindAttribute(TDataStd_AsciiString::GetID(), anErrorAttr)) {
271     return std::string(anErrorAttr->Get().ToCString());
272   }
273   return std::string();
274 }
275
276 int Model_Data::featureId() const
277 {
278   return myLab.Father().Tag(); // tag of the feature label
279 }
280
281 void Model_Data::eraseBackReferences()
282 {
283   myRefsToMe.clear();
284   std::shared_ptr<ModelAPI_Result> aRes = 
285     std::dynamic_pointer_cast<ModelAPI_Result>(myObject);
286   if (aRes)
287     aRes->setIsConcealed(false);
288 }
289
290 void Model_Data::removeBackReference(FeaturePtr theFeature, std::string theAttrID)
291 {
292   AttributePtr anAttribute = theFeature->data()->attribute(theAttrID);
293   if (myRefsToMe.find(anAttribute) == myRefsToMe.end())
294     return;
295
296   myRefsToMe.erase(anAttribute);
297 }
298
299 void Model_Data::addBackReference(FeaturePtr theFeature, std::string theAttrID, 
300    const bool theApplyConcealment)
301 {
302   // do not add the same attribute twice
303   AttributePtr anAttribute = theFeature->data()->attribute(theAttrID);
304   if (myRefsToMe.find(anAttribute) != myRefsToMe.end())
305     return;
306
307   myRefsToMe.insert(theFeature->data()->attribute(theAttrID));
308   if (theApplyConcealment && 
309       ModelAPI_Session::get()->validators()->isConcealed(theFeature->getKind(), theAttrID)) {
310     std::shared_ptr<ModelAPI_Result> aRes = 
311       std::dynamic_pointer_cast<ModelAPI_Result>(myObject);
312     // the second condition is for history upper than concealment causer, so the feature result may
313     // be displayed and previewed; also for avoiding of quick show/hide on history
314     // moving deep down
315     if (aRes && !theFeature->isDisabled()) {
316       aRes->setIsConcealed(true);
317     }
318   }
319 }
320
321 void Model_Data::referencesToObjects(
322   std::list<std::pair<std::string, std::list<ObjectPtr> > >& theRefs)
323 {
324   std::map<std::string, std::shared_ptr<ModelAPI_Attribute> >::iterator anAttr = myAttrs.begin();
325   std::list<ObjectPtr> aReferenced; // not inside of cycle to avoid excess memory menagement
326   for(; anAttr != myAttrs.end(); anAttr++) {
327     std::string aType = anAttr->second->attributeType();
328     if (aType == ModelAPI_AttributeReference::typeId()) { // reference to object
329       std::shared_ptr<ModelAPI_AttributeReference> aRef = std::dynamic_pointer_cast<
330           ModelAPI_AttributeReference>(anAttr->second);
331       aReferenced.push_back(aRef->value());
332     } else if (aType == ModelAPI_AttributeRefAttr::typeId()) { // reference to attribute or object
333       std::shared_ptr<ModelAPI_AttributeRefAttr> aRef = std::dynamic_pointer_cast<
334           ModelAPI_AttributeRefAttr>(anAttr->second);
335       aReferenced.push_back(aRef->isObject() ? aRef->object() : aRef->attr()->owner());
336     } else if (aType == ModelAPI_AttributeRefList::typeId()) { // list of references
337       aReferenced = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(anAttr->second)->list();
338     } else if (aType == ModelAPI_AttributeSelection::typeId()) { // selection attribute
339       std::shared_ptr<ModelAPI_AttributeSelection> aRef = std::dynamic_pointer_cast<
340           ModelAPI_AttributeSelection>(anAttr->second);
341       aReferenced.push_back(aRef->context());
342     } else if (aType == ModelAPI_AttributeSelectionList::typeId()) { // list of selection attributes
343       std::shared_ptr<ModelAPI_AttributeSelectionList> aRef = std::dynamic_pointer_cast<
344           ModelAPI_AttributeSelectionList>(anAttr->second);
345       for(int a = aRef->size() - 1; a >= 0; a--) {
346         aReferenced.push_back(aRef->value(a)->context());
347       }
348     } else
349       continue; // nothing to do, not reference
350
351     if (!aReferenced.empty()) {
352       theRefs.push_back(std::pair<std::string, std::list<ObjectPtr> >(anAttr->first, aReferenced));
353       aReferenced.clear();
354     }
355   }
356 }
357
358 /// makes copy of all attributes on the given label and all sub-labels
359 static void copyAttrs(TDF_Label theSource, TDF_Label theDestination) {
360   TDF_AttributeIterator anAttrIter(theSource);
361   for(; anAttrIter.More(); anAttrIter.Next()) {
362     Handle(TDF_Attribute) aTargetAttr;
363     if (!theDestination.FindAttribute(anAttrIter.Value()->ID(), aTargetAttr)) {
364       // create a new attribute if not yet exists in the destination
365             aTargetAttr = anAttrIter.Value()->NewEmpty();
366       theDestination.AddAttribute(aTargetAttr);
367     }
368     Handle(TDF_RelocationTable) aRelocTable = new TDF_RelocationTable(); // no relocation, empty map
369     anAttrIter.Value()->Paste(aTargetAttr, aRelocTable);
370   }
371   // copy the sub-labels content
372   TDF_ChildIterator aSubLabsIter(theSource);
373   for(; aSubLabsIter.More(); aSubLabsIter.Next()) {
374     copyAttrs(aSubLabsIter.Value(), theDestination.FindChild(aSubLabsIter.Value().Tag()));
375   }
376 }
377
378 void Model_Data::copyTo(std::shared_ptr<ModelAPI_Data> theTarget)
379 {
380   TDF_Label aTargetRoot = std::dynamic_pointer_cast<Model_Data>(theTarget)->label();
381   copyAttrs(myLab, aTargetRoot);
382   // make initialized the initialized attributes
383   std::map<std::string, std::shared_ptr<ModelAPI_Attribute> >::iterator aMyIter = myAttrs.begin();
384   for(; aMyIter != myAttrs.end(); aMyIter++) {
385     if (aMyIter->second->isInitialized()) {
386       theTarget->attribute(aMyIter->first)->setInitialized();
387     }
388   }
389 }
390
391 bool Model_Data::isInHistory()
392 {
393   return myFlags->Value(kFlagInHistory) == Standard_True;
394 }
395
396 void Model_Data::setIsInHistory(const bool theFlag)
397 {
398   return myFlags->SetValue(kFlagInHistory, theFlag);
399 }
400
401 bool Model_Data::isDisplayed()
402 {
403   return myObject.get() && myObject->document().get() && 
404     (myObject->document()->isActive() || 
405      myObject->document() == ModelAPI_Session::get()->moduleDocument()) && // root is accessible allways
406     myFlags->Value(kFlagDisplayed) == Standard_True;
407 }
408
409 void Model_Data::setDisplayed(const bool theDisplay)
410 {
411   if (theDisplay != isDisplayed()) {
412     myFlags->SetValue(kFlagDisplayed, theDisplay);
413     static Events_Loop* aLoop = Events_Loop::loop();
414     static Events_ID EVENT_DISP = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
415     static const ModelAPI_EventCreator* aECreator = ModelAPI_EventCreator::get();
416     aECreator->sendUpdated(myObject, EVENT_DISP);
417   }
418 }
419
420 std::shared_ptr<ModelAPI_Data> Model_Data::invalidPtr()
421 {
422   return kInvalid;
423 }
424
425 std::shared_ptr<ModelAPI_Data> Model_Data::invalidData()
426 {
427   return kInvalid;
428 }