Salome HOME
Issue #1662: implementation of Recover feature.
[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_AttributeDoubleArray.h>
12 #include <Model_AttributeReference.h>
13 #include <Model_AttributeRefAttr.h>
14 #include <Model_AttributeRefList.h>
15 #include <Model_AttributeRefAttrList.h>
16 #include <Model_AttributeBoolean.h>
17 #include <Model_AttributeString.h>
18 #include <Model_AttributeSelection.h>
19 #include <Model_AttributeSelectionList.h>
20 #include <Model_AttributeIntArray.h>
21 #include <Model_Events.h>
22 #include <Model_Expression.h>
23 #include <ModelAPI_Feature.h>
24 #include <ModelAPI_Result.h>
25 #include <ModelAPI_ResultParameter.h>
26 #include <ModelAPI_Validator.h>
27 #include <ModelAPI_Session.h>
28 #include <ModelAPI_ResultPart.h>
29 #include <ModelAPI_ResultCompSolid.h>
30 #include <ModelAPI_Tools.h>
31 #include <Model_Validator.h>
32
33 #include <GeomDataAPI_Point.h>
34 #include <GeomDataAPI_Point2D.h>
35
36 #include <GeomData_Point.h>
37 #include <GeomData_Point2D.h>
38 #include <GeomData_Dir.h>
39 #include <Events_Loop.h>
40 #include <Events_InfoMessage.h>
41
42 #include <TDataStd_Name.hxx>
43 #include <TDataStd_AsciiString.hxx>
44 #include <TDataStd_IntegerArray.hxx>
45 #include <TDF_AttributeIterator.hxx>
46 #include <TDF_ChildIterator.hxx>
47 #include <TDF_RelocationTable.hxx>
48 #include <TColStd_HArray1OfByte.hxx>
49
50 #include <string>
51
52 // myLab contains:
53 // TDataStd_Name - name of the object
54 // TDataStd_IntegerArray - state of the object execution, transaction ID of update
55 // TDataStd_BooleanArray - array of flags of this data:
56 //                             0 - is in history or not
57 static const int kFlagInHistory = 0;
58 //                             1 - is displayed or not
59 static const int kFlagDisplayed = 1;
60 //                             2 - is deleted (for results) or not
61 static const int kFlagDeleted = 2;
62
63 // invalid data
64 const static std::shared_ptr<ModelAPI_Data> kInvalid(new Model_Data());
65
66 Model_Data::Model_Data() : mySendAttributeUpdated(true), myWasChangedButBlocked(false)
67 {
68 }
69
70 void Model_Data::setLabel(TDF_Label theLab)
71 {
72   myLab = theLab;
73   // set or get the default flags
74   if (!myLab.FindAttribute(TDataStd_BooleanArray::GetID(), myFlags)) {
75     // set default values if not found
76     myFlags = TDataStd_BooleanArray::Set(myLab, 0, 2);
77     myFlags->SetValue(kFlagInHistory, Standard_True); // is in history by default is true
78     myFlags->SetValue(kFlagDisplayed, Standard_True); // is displayed by default is true
79     myFlags->SetValue(kFlagDeleted, Standard_False); // is deleted by default is false
80   } else if (myFlags->Length() != 3) { // for old formats support
81     Standard_Boolean aFlag0 = myFlags->Upper() >= 0 ? myFlags->Value(0) : Standard_True;
82     Standard_Boolean aFlag1 = myFlags->Upper() >= 1 ? myFlags->Value(1) : Standard_True;
83     Standard_Boolean aFlag2 = myFlags->Upper() >= 2 ? myFlags->Value(2) : Standard_True;
84     Handle(TColStd_HArray1OfByte) aNewArray = new TColStd_HArray1OfByte(0, 2);
85     myFlags->SetInternalArray(aNewArray);
86     myFlags->SetValue(0, aFlag0); 
87     myFlags->SetValue(1, aFlag1); 
88     myFlags->SetValue(2, aFlag2); 
89   }
90 }
91
92 std::string Model_Data::name()
93 {
94   Handle(TDataStd_Name) aName;
95   if (myLab.FindAttribute(TDataStd_Name::GetID(), aName))
96     return std::string(TCollection_AsciiString(aName->Get()).ToCString());
97   return "";  // not defined
98 }
99
100 void Model_Data::setName(const std::string& theName)
101 {
102   bool isModified = false;
103   std::string anOldName = name();
104   Handle(TDataStd_Name) aName;
105   if (!myLab.FindAttribute(TDataStd_Name::GetID(), aName)) {
106     TDataStd_Name::Set(myLab, theName.c_str());
107     isModified = true;
108   } else {
109     isModified = !aName->Get().IsEqual(theName.c_str());
110     if (isModified)
111       aName->Set(theName.c_str());
112   }
113   if (mySendAttributeUpdated && isModified)
114     ModelAPI_ObjectRenamedMessage::send(myObject, anOldName, theName, this);
115 }
116
117 AttributePtr Model_Data::addAttribute(const std::string& theID, const std::string theAttrType)
118 {
119   AttributePtr aResult;
120   TDF_Label anAttrLab = myLab.FindChild(int(myAttrs.size()) + 1);
121   ModelAPI_Attribute* anAttr = 0;
122   if (theAttrType == ModelAPI_AttributeDocRef::typeId()) {
123     anAttr = new Model_AttributeDocRef(anAttrLab);
124   } else if (theAttrType == Model_AttributeInteger::typeId()) {
125     Model_AttributeInteger* anAttribute = new Model_AttributeInteger();
126     // Expression should use the same label to support backward compatibility
127     TDF_Label anExpressionLab = anAttrLab;
128     anAttribute->myExpression.reset(new Model_ExpressionInteger(anExpressionLab));
129     anAttribute->myIsInitialized = anAttribute->myExpression->isInitialized();
130     anAttr = anAttribute;
131   } else if (theAttrType == ModelAPI_AttributeDouble::typeId()) {
132     Model_AttributeDouble* anAttribute = new Model_AttributeDouble();
133     TDF_Label anExpressionLab = anAttrLab.FindChild(1);
134     anAttribute->myExpression.reset(new Model_ExpressionDouble(anExpressionLab));
135     anAttribute->myIsInitialized = anAttribute->myExpression->isInitialized();
136     anAttr = anAttribute;
137   } else if (theAttrType == Model_AttributeBoolean::typeId()) {
138     anAttr = new Model_AttributeBoolean(anAttrLab);
139   } else if (theAttrType == Model_AttributeString::typeId()) {
140     anAttr = new Model_AttributeString(anAttrLab);
141   } else if (theAttrType == ModelAPI_AttributeReference::typeId()) {
142     anAttr = new Model_AttributeReference(anAttrLab);
143   } else if (theAttrType == ModelAPI_AttributeSelection::typeId()) {
144     anAttr = new Model_AttributeSelection(anAttrLab);
145   } else if (theAttrType == ModelAPI_AttributeSelectionList::typeId()) {
146     anAttr = new Model_AttributeSelectionList(anAttrLab);
147   } else if (theAttrType == ModelAPI_AttributeRefAttr::typeId()) {
148     anAttr = new Model_AttributeRefAttr(anAttrLab);
149   } else if (theAttrType == ModelAPI_AttributeRefList::typeId()) {
150     anAttr = new Model_AttributeRefList(anAttrLab);
151   } else if (theAttrType == ModelAPI_AttributeRefAttrList::typeId()) {
152     anAttr = new Model_AttributeRefAttrList(anAttrLab);
153   } else if (theAttrType == ModelAPI_AttributeIntArray::typeId()) {
154     anAttr = new Model_AttributeIntArray(anAttrLab);
155   } else if (theAttrType == ModelAPI_AttributeDoubleArray::typeId()) {
156     anAttr = new Model_AttributeDoubleArray(anAttrLab);
157   }
158   // create also GeomData attributes here because only here the OCAF structure is known
159   else if (theAttrType == GeomData_Point::typeId()) {
160     GeomData_Point* anAttribute = new GeomData_Point();
161     bool anAllInitialized = true;
162     for (int aComponent = 0; aComponent < GeomData_Point::NUM_COMPONENTS; ++aComponent) {
163       TDF_Label anExpressionLab = anAttrLab.FindChild(aComponent + 1);
164       anAttribute->myExpression[aComponent].reset(new Model_ExpressionDouble(anExpressionLab));
165       anAllInitialized = anAllInitialized && anAttribute->myExpression[aComponent]->isInitialized();
166     }
167     anAttribute->myIsInitialized = anAllInitialized;
168     anAttr = anAttribute;
169   } else if (theAttrType == GeomData_Dir::typeId()) {
170     anAttr = new GeomData_Dir(anAttrLab);
171   } else if (theAttrType == GeomData_Point2D::typeId()) {
172     GeomData_Point2D* anAttribute = new GeomData_Point2D();
173     bool anAllInitialized = true;
174     for (int aComponent = 0; aComponent < GeomData_Point2D::NUM_COMPONENTS; ++aComponent) {
175       TDF_Label anExpressionLab = anAttrLab.FindChild(aComponent + 1);
176       anAttribute->myExpression[aComponent].reset(new Model_ExpressionDouble(anExpressionLab));
177       anAllInitialized = anAllInitialized && anAttribute->myExpression[aComponent]->isInitialized();
178     }
179     anAttribute->myIsInitialized = anAllInitialized;
180     anAttr = anAttribute;
181   }
182   if (anAttr) {
183     aResult = std::shared_ptr<ModelAPI_Attribute>(anAttr);
184     myAttrs[theID] = aResult;
185     anAttr->setObject(myObject);
186     anAttr->setID(theID);
187   } else {
188     Events_InfoMessage("Model_Data", "Can not create unknown type of attribute %1").arg(theAttrType).send();
189   }
190   return aResult;
191 }
192
193 // macro for the generic returning of the attribute by the ID
194 #define GET_ATTRIBUTE_BY_ID(ATTR_TYPE, METHOD_NAME) \
195   std::shared_ptr<ATTR_TYPE> Model_Data::METHOD_NAME(const std::string& theID) { \
196     std::shared_ptr<ATTR_TYPE> aRes; \
197     std::map<std::string, AttributePtr >::iterator aFound = \
198       myAttrs.find(theID); \
199     if (aFound != myAttrs.end()) { \
200       aRes = std::dynamic_pointer_cast<ATTR_TYPE>(aFound->second); \
201     } \
202     return aRes; \
203   }
204 // implement nice getting methods for all ModelAPI attributes
205 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeDocRef, document);
206 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeDouble, real);
207 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeInteger, integer);
208 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeBoolean, boolean);
209 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeString, string);
210 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeReference, reference);
211 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeSelection, selection);
212 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeSelectionList, selectionList);
213 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeRefAttr, refattr);
214 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeRefList, reflist);
215 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeRefAttrList, refattrlist);
216 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeIntArray, intArray);
217 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeDoubleArray, realArray);
218
219 std::shared_ptr<ModelAPI_Attribute> Model_Data::attribute(const std::string& theID)
220 {
221   std::shared_ptr<ModelAPI_Attribute> aResult;
222   if (myAttrs.find(theID) == myAttrs.end())  // no such attribute
223     return aResult;
224   return myAttrs[theID];
225 }
226
227 const std::string& Model_Data::id(const std::shared_ptr<ModelAPI_Attribute>& theAttr)
228 {
229   std::map<std::string, std::shared_ptr<ModelAPI_Attribute> >::iterator anAttr = 
230     myAttrs.begin();
231   for (; anAttr != myAttrs.end(); anAttr++) {
232     if (anAttr->second == theAttr)
233       return anAttr->first;
234   }
235   // not found
236   static std::string anEmpty;
237   return anEmpty;
238 }
239
240 bool Model_Data::isEqual(const std::shared_ptr<ModelAPI_Data>& theData)
241 {
242   std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(theData);
243   if (aData)
244     return myLab.IsEqual(aData->myLab) == Standard_True ;
245   return false;
246 }
247
248 bool Model_Data::isValid()
249 {
250   return !myLab.IsNull() && myLab.HasAttribute();
251 }
252
253 std::list<std::shared_ptr<ModelAPI_Attribute> > Model_Data::attributes(const std::string& theType)
254 {
255   std::list<std::shared_ptr<ModelAPI_Attribute> > aResult;
256   std::map<std::string, std::shared_ptr<ModelAPI_Attribute> >::iterator anAttrsIter = 
257     myAttrs.begin();
258   for (; anAttrsIter != myAttrs.end(); anAttrsIter++) {
259     if (theType.empty() || anAttrsIter->second->attributeType() == theType) {
260       aResult.push_back(anAttrsIter->second);
261     }
262   }
263   return aResult;
264 }
265
266 std::list<std::string> Model_Data::attributesIDs(const std::string& theType) 
267 {
268   std::list<std::string> aResult;
269   std::map<std::string, std::shared_ptr<ModelAPI_Attribute> >::iterator anAttrsIter = 
270     myAttrs.begin();
271   for (; anAttrsIter != myAttrs.end(); anAttrsIter++) {
272     if (theType.empty() || anAttrsIter->second->attributeType() == theType) {
273       aResult.push_back(anAttrsIter->first);
274     }
275   }
276   return aResult;
277 }
278
279 void Model_Data::sendAttributeUpdated(ModelAPI_Attribute* theAttr)
280 {
281   theAttr->setInitialized();
282   if (theAttr->isArgument()) {
283     if (mySendAttributeUpdated) {
284       if (myObject) {
285         myObject->attributeChanged(theAttr->id());
286         static const Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
287         ModelAPI_EventCreator::get()->sendUpdated(myObject, anEvent);
288       }
289     } else {
290       // to avoid too many duplications do not add the same like the last
291       if (myWasChangedButBlocked.empty() || *(myWasChangedButBlocked.rbegin()) != theAttr)
292         myWasChangedButBlocked.push_back(theAttr);
293     }
294   }
295 }
296
297 void Model_Data::blockSendAttributeUpdated(const bool theBlock, const bool theSendMessage)
298 {
299   if (mySendAttributeUpdated == theBlock) {
300     mySendAttributeUpdated = !theBlock;
301     if (mySendAttributeUpdated && !myWasChangedButBlocked.empty()) { // so, now it is ok to send the update signal
302       if (theSendMessage) {
303         // make a copy to avoid iteration on modified list (may be cleared by attribute changed call)
304         std::list<ModelAPI_Attribute*> aWasChangedButBlocked = myWasChangedButBlocked;
305         myWasChangedButBlocked.clear();
306         std::list<ModelAPI_Attribute*>::iterator aChangedIter = aWasChangedButBlocked.begin();
307         for(; aChangedIter != aWasChangedButBlocked.end(); aChangedIter++) {
308           myObject->attributeChanged((*aChangedIter)->id());
309         }
310         static const Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
311         ModelAPI_EventCreator::get()->sendUpdated(myObject, anEvent);
312       } else {
313         myWasChangedButBlocked.clear();
314       }
315     }
316   }
317 }
318
319 void Model_Data::erase()
320 {
321   if (!myLab.IsNull()) {
322     if (myLab.HasAttribute()) {
323       // remove in order to clear back references in other objects
324       std::list<std::pair<std::string, std::list<ObjectPtr> > > aRefs;
325       referencesToObjects(aRefs);
326       std::list<std::pair<std::string, std::list<ObjectPtr> > >::iterator anAttrIter = aRefs.begin();
327       for(; anAttrIter != aRefs.end(); anAttrIter++) {
328         std::list<ObjectPtr>::iterator aReferenced = anAttrIter->second.begin();
329         for(; aReferenced != anAttrIter->second.end(); aReferenced++) {
330           if (aReferenced->get() && (*aReferenced)->data()->isValid()) {
331             std::shared_ptr<Model_Data> aData = 
332               std::dynamic_pointer_cast<Model_Data>((*aReferenced)->data());
333             aData->removeBackReference(myAttrs[anAttrIter->first]);
334           }
335         }
336       }
337     }
338     myAttrs.clear();
339     myLab.ForgetAllAttributes();
340   }
341 }
342
343 // indexes in the state array
344 enum StatesIndexes {
345   STATE_INDEX_STATE = 1, // the state type itself
346   STATE_INDEX_TRANSACTION = 2, // transaction ID
347 };
348
349 /// Returns the label array, initialises it by default values if not exists
350 static Handle(TDataStd_IntegerArray) stateArray(TDF_Label& theLab)
351 {
352   Handle(TDataStd_IntegerArray) aStateArray;
353   if (!theLab.FindAttribute(TDataStd_IntegerArray::GetID(), aStateArray)) {
354     aStateArray = TDataStd_IntegerArray::Set(theLab, 1, 2);
355     aStateArray->SetValue(STATE_INDEX_STATE, ModelAPI_StateMustBeUpdated); // default state
356     aStateArray->SetValue(STATE_INDEX_TRANSACTION, 0); // default transaction ID (not existing)
357   }
358   return aStateArray;
359 }
360
361 void Model_Data::execState(const ModelAPI_ExecState theState)
362 {
363   if (theState != ModelAPI_StateNothing) {
364     if (stateArray(myLab)->Value(STATE_INDEX_STATE) != (int)theState) {
365       stateArray(myLab)->SetValue(STATE_INDEX_STATE, (int)theState);
366     }
367   }
368 }
369
370 ModelAPI_ExecState Model_Data::execState()
371 {
372   return ModelAPI_ExecState(stateArray(myLab)->Value(STATE_INDEX_STATE));
373 }
374
375 int Model_Data::updateID()
376 {
377   return stateArray(myLab)->Value(STATE_INDEX_TRANSACTION);
378 }
379
380 void Model_Data::setUpdateID(const int theID)
381 {
382   stateArray(myLab)->SetValue(STATE_INDEX_TRANSACTION, theID);
383 }
384
385 void Model_Data::setError(const std::string& theError, bool theSend)
386 {
387   execState(ModelAPI_StateExecFailed);
388   if (theSend) {
389     Events_InfoMessage("Model_Data", theError).send();
390   }
391   TDataStd_AsciiString::Set(myLab, theError.c_str());
392 }
393
394 void Model_Data::eraseErrorString()
395 {
396   myLab.ForgetAttribute(TDataStd_AsciiString::GetID());
397 }
398
399 std::string Model_Data::error() const
400 {
401   Handle(TDataStd_AsciiString) anErrorAttr;
402   if (myLab.FindAttribute(TDataStd_AsciiString::GetID(), anErrorAttr)) {
403     return std::string(anErrorAttr->Get().ToCString());
404   }
405   return std::string();
406 }
407
408 int Model_Data::featureId() const
409 {
410   return myLab.Father().Tag(); // tag of the feature label
411 }
412
413 void Model_Data::eraseBackReferences()
414 {
415   myRefsToMe.clear();
416   std::shared_ptr<ModelAPI_Result> aRes = 
417     std::dynamic_pointer_cast<ModelAPI_Result>(myObject);
418   if (aRes)
419     aRes->setIsConcealed(false);
420 }
421
422 void Model_Data::removeBackReference(FeaturePtr theFeature, std::string theAttrID)
423 {
424   AttributePtr anAttribute = theFeature->data()->attribute(theAttrID);
425   removeBackReference(anAttribute);
426 }
427
428 void Model_Data::removeBackReference(AttributePtr theAttr)
429 {
430   if (myRefsToMe.find(theAttr) == myRefsToMe.end())
431     return;
432
433   myRefsToMe.erase(theAttr);
434
435   // remove concealment immideately: on deselection it must be posible to reselect in GUI the same
436   FeaturePtr aFeatureOwner = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttr->owner());
437   if (aFeatureOwner.get() &&
438     ModelAPI_Session::get()->validators()->isConcealed(aFeatureOwner->getKind(), theAttr->id())) {
439     updateConcealmentFlag();
440   }
441 }
442
443 void Model_Data::addBackReference(FeaturePtr theFeature, std::string theAttrID, 
444    const bool theApplyConcealment)
445 {
446   // it is possible to add the same attribute twice: may be last time the owner was not Stable...
447   AttributePtr anAttribute = theFeature->data()->attribute(theAttrID);
448   if (myRefsToMe.find(anAttribute) == myRefsToMe.end())
449     myRefsToMe.insert(theFeature->data()->attribute(theAttrID));
450
451   if (theApplyConcealment &&  theFeature->isStable() && 
452       ModelAPI_Session::get()->validators()->isConcealed(theFeature->getKind(), theAttrID)) {
453     std::shared_ptr<ModelAPI_Result> aRes = 
454       std::dynamic_pointer_cast<ModelAPI_Result>(myObject);
455     // the second condition is for history upper than concealment causer, so the feature result may
456     // be displayed and previewed; also for avoiding of quick show/hide on history
457     // moving deep down
458     if (aRes && !theFeature->isDisabled() && 
459         !ModelAPI_Session::get()->validators()->isUnconcealed(aRes, theFeature)) {
460       aRes->setIsConcealed(true);
461     }
462   }
463 }
464
465 void Model_Data::updateConcealmentFlag()
466 {
467   std::set<AttributePtr>::iterator aRefsIter = myRefsToMe.begin();
468   for(; aRefsIter != myRefsToMe.end(); aRefsIter++) {
469     if (aRefsIter->get()) {
470       FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>((*aRefsIter)->owner());
471       if (aFeature.get() && !aFeature->isDisabled() && aFeature->isStable()) {
472         if (ModelAPI_Session::get()->validators()->isConcealed(
473               aFeature->getKind(), (*aRefsIter)->id())) {
474           std::shared_ptr<ModelAPI_Result> aRes = 
475             std::dynamic_pointer_cast<ModelAPI_Result>(myObject);
476           if (aRes.get()) {
477             if (!ModelAPI_Session::get()->validators()->isUnconcealed(aRes, aFeature)) {
478               aRes->setIsConcealed(true); // set concealed
479               return;
480             }
481           }
482         }
483       }
484     }
485   }
486   std::shared_ptr<ModelAPI_Result> aRes = 
487     std::dynamic_pointer_cast<ModelAPI_Result>(myObject);
488   if (aRes.get()) {
489     aRes->setIsConcealed(false);
490   }
491 }
492
493 std::set<std::string> set_union(const std::set<std::string>& theLeft, 
494                                 const std::set<std::string>& theRight)
495 {
496   std::set<std::string> aResult;
497   aResult.insert(theLeft.begin(), theLeft.end());
498   aResult.insert(theRight.begin(), theRight.end());
499   return aResult;
500 }
501
502 std::set<std::string> usedParameters(const AttributePointPtr& theAttribute)
503 {
504   std::set<std::string> anUsedParameters;
505   for (int aComponent = 0; aComponent < 3; ++aComponent)
506     anUsedParameters = set_union(anUsedParameters, theAttribute->usedParameters(aComponent));
507   return anUsedParameters;
508 }
509
510 std::set<std::string> usedParameters(const AttributePoint2DPtr& theAttribute)
511 {
512   std::set<std::string> anUsedParameters;
513   for (int aComponent = 0; aComponent < 2; ++aComponent)
514     anUsedParameters = set_union(anUsedParameters, theAttribute->usedParameters(aComponent));
515   return anUsedParameters;
516 }
517
518 std::list<ResultParameterPtr> findVariables(const std::set<std::string>& theParameters)
519 {
520   std::list<ResultParameterPtr> aResult;
521   std::set<std::string>::const_iterator aParamIt = theParameters.cbegin();
522   for (; aParamIt != theParameters.cend(); ++aParamIt) {
523     const std::string& aName = *aParamIt;
524     double aValue;
525     ResultParameterPtr aParam;
526     // theSearcher is not needed here: in expressions of features the parameters history is not needed
527     if (ModelAPI_Tools::findVariable(FeaturePtr(), aName, aValue, aParam))
528       aResult.push_back(aParam);
529   }
530   return aResult;
531 }
532
533 void Model_Data::referencesToObjects(
534   std::list<std::pair<std::string, std::list<ObjectPtr> > >& theRefs)
535 {
536   static Model_ValidatorsFactory* aValidators = 
537     static_cast<Model_ValidatorsFactory*>(ModelAPI_Session::get()->validators());
538   FeaturePtr aMyFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(myObject);
539
540   std::map<std::string, std::shared_ptr<ModelAPI_Attribute> >::iterator anAttr = myAttrs.begin();
541   std::list<ObjectPtr> aReferenced; // not inside of cycle to avoid excess memory management
542   for(; anAttr != myAttrs.end(); anAttr++) {
543     // skip not-case attributes, that really may refer to anything not-used (issue 671)
544     if (aMyFeature.get() && !aValidators->isCase(aMyFeature, anAttr->second->id()))
545       continue;
546
547     std::string aType = anAttr->second->attributeType();
548     if (aType == ModelAPI_AttributeReference::typeId()) { // reference to object
549       std::shared_ptr<ModelAPI_AttributeReference> aRef = std::dynamic_pointer_cast<
550           ModelAPI_AttributeReference>(anAttr->second);
551       aReferenced.push_back(aRef->value());
552     } else if (aType == ModelAPI_AttributeRefAttr::typeId()) { // reference to attribute or object
553       std::shared_ptr<ModelAPI_AttributeRefAttr> aRef = std::dynamic_pointer_cast<
554           ModelAPI_AttributeRefAttr>(anAttr->second);
555       if (aRef->isObject()) {
556         aReferenced.push_back(aRef->object());
557       } else {
558         AttributePtr anAttr = aRef->attr();
559         if (anAttr.get())
560           aReferenced.push_back(anAttr->owner());
561       }
562     } else if (aType == ModelAPI_AttributeRefList::typeId()) { // list of references
563       aReferenced = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(anAttr->second)->list();
564     } else if (aType == ModelAPI_AttributeSelection::typeId()) { // selection attribute
565       std::shared_ptr<ModelAPI_AttributeSelection> aRef = std::dynamic_pointer_cast<
566           ModelAPI_AttributeSelection>(anAttr->second);
567       aReferenced.push_back(aRef->context());
568     } else if (aType == ModelAPI_AttributeSelectionList::typeId()) { // list of selection attributes
569       std::shared_ptr<ModelAPI_AttributeSelectionList> aRef = std::dynamic_pointer_cast<
570           ModelAPI_AttributeSelectionList>(anAttr->second);
571       for(int a = aRef->size() - 1; a >= 0; a--) {
572         aReferenced.push_back(aRef->value(a)->context());
573       }
574     } else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
575       std::shared_ptr<ModelAPI_AttributeRefAttrList> aRefAttr = std::dynamic_pointer_cast<
576           ModelAPI_AttributeRefAttrList>(anAttr->second);
577       std::list<std::pair<ObjectPtr, AttributePtr> > aRefs = aRefAttr->list();
578       std::list<std::pair<ObjectPtr, AttributePtr> >::const_iterator anIt = aRefs.begin(),
579                                                                      aLast = aRefs.end();
580       for (; anIt != aLast; anIt++) {
581         aReferenced.push_back(anIt->first);
582       }
583     } else if (aType == ModelAPI_AttributeInteger::typeId()) { // integer attribute
584       AttributeIntegerPtr anAttribute =
585           std::dynamic_pointer_cast<ModelAPI_AttributeInteger>(anAttr->second);
586       std::set<std::string> anUsedParameters = anAttribute->usedParameters();
587       std::list<ResultParameterPtr> aParameters = findVariables(anUsedParameters);
588       aReferenced.insert(aReferenced.end(), aParameters.begin(), aParameters.end());
589     } else if (aType == ModelAPI_AttributeDouble::typeId()) { // double attribute
590       AttributeDoublePtr anAttribute =
591           std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(anAttr->second);
592       std::set<std::string> anUsedParameters = anAttribute->usedParameters();
593       std::list<ResultParameterPtr> aParameters = findVariables(anUsedParameters);
594       aReferenced.insert(aReferenced.end(), aParameters.begin(), aParameters.end());
595     } else if (aType == GeomDataAPI_Point::typeId()) { // point attribute
596       AttributePointPtr anAttribute =
597         std::dynamic_pointer_cast<GeomDataAPI_Point>(anAttr->second);
598       std::set<std::string> anUsedParameters = usedParameters(anAttribute);
599       std::list<ResultParameterPtr> aParameters = findVariables(anUsedParameters);
600       aReferenced.insert(aReferenced.end(), aParameters.begin(), aParameters.end());
601     } else if (aType == GeomDataAPI_Point2D::typeId()) { // point attribute
602       AttributePoint2DPtr anAttribute =
603         std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttr->second);
604       std::set<std::string> anUsedParameters = usedParameters(anAttribute);
605       std::list<ResultParameterPtr> aParameters = findVariables(anUsedParameters);
606       aReferenced.insert(aReferenced.end(), aParameters.begin(), aParameters.end());
607     } else
608       continue; // nothing to do, not reference
609
610     if (!aReferenced.empty()) {
611       theRefs.push_back(std::pair<std::string, std::list<ObjectPtr> >(anAttr->first, aReferenced));
612       aReferenced.clear();
613     }
614   }
615 }
616
617 /// makes copy of all attributes on the given label and all sub-labels
618 static void copyAttrs(TDF_Label theSource, TDF_Label theDestination) {
619   TDF_AttributeIterator anAttrIter(theSource);
620   for(; anAttrIter.More(); anAttrIter.Next()) {
621     Handle(TDF_Attribute) aTargetAttr;
622     if (!theDestination.FindAttribute(anAttrIter.Value()->ID(), aTargetAttr)) {
623       // create a new attribute if not yet exists in the destination
624             aTargetAttr = anAttrIter.Value()->NewEmpty();
625       theDestination.AddAttribute(aTargetAttr);
626     }
627     Handle(TDF_RelocationTable) aRelocTable = new TDF_RelocationTable(); // no relocation, empty map
628     anAttrIter.Value()->Paste(aTargetAttr, aRelocTable);
629   }
630   // copy the sub-labels content
631   TDF_ChildIterator aSubLabsIter(theSource);
632   for(; aSubLabsIter.More(); aSubLabsIter.Next()) {
633     copyAttrs(aSubLabsIter.Value(), theDestination.FindChild(aSubLabsIter.Value().Tag()));
634   }
635 }
636
637 void Model_Data::copyTo(std::shared_ptr<ModelAPI_Data> theTarget)
638 {
639   TDF_Label aTargetRoot = std::dynamic_pointer_cast<Model_Data>(theTarget)->label();
640   copyAttrs(myLab, aTargetRoot);
641   // make initialized the initialized attributes
642   std::map<std::string, std::shared_ptr<ModelAPI_Attribute> >::iterator aMyIter = myAttrs.begin();
643   for(; aMyIter != myAttrs.end(); aMyIter++) {
644     if (aMyIter->second->isInitialized()) {
645       AttributePtr aTargetAttr = theTarget->attribute(aMyIter->first);
646       if (aTargetAttr)
647         aTargetAttr->setInitialized();
648     }
649   }
650 }
651
652 bool Model_Data::isInHistory()
653 {
654   return myFlags->Value(kFlagInHistory) == Standard_True;
655 }
656
657 void Model_Data::setIsInHistory(const bool theFlag)
658 {
659   return myFlags->SetValue(kFlagInHistory, theFlag);
660 }
661
662 bool Model_Data::isDeleted()
663 {
664   return myFlags->Value(kFlagDeleted) == Standard_True;
665 }
666
667 void Model_Data::setIsDeleted(const bool theFlag)
668 {
669   return myFlags->SetValue(kFlagDeleted, theFlag);
670 }
671
672 bool Model_Data::isDisplayed()
673 {
674   if (!myObject.get() || !myObject->document().get() || // object is in valid
675       myFlags->Value(kFlagDisplayed) != Standard_True) // or it was not displayed before
676     return false;
677   if (myObject->document()->isActive()) // for active documents it must be ok anyway
678     return true;
679   // any object from the root document except part result may be displayed
680   if (myObject->document() == ModelAPI_Session::get()->moduleDocument() &&
681       myObject->groupName() != ModelAPI_ResultPart::group())
682     return true;
683   return false;
684 }
685
686 void Model_Data::setDisplayed(const bool theDisplay)
687 {
688   if (theDisplay != isDisplayed()) {
689     myFlags->SetValue(kFlagDisplayed, theDisplay);
690     static Events_Loop* aLoop = Events_Loop::loop();
691     static Events_ID EVENT_DISP = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
692     static const ModelAPI_EventCreator* aECreator = ModelAPI_EventCreator::get();
693     aECreator->sendUpdated(myObject, EVENT_DISP);
694   }
695 }
696
697 std::shared_ptr<ModelAPI_Data> Model_Data::invalidPtr()
698 {
699   return kInvalid;
700 }
701
702 std::shared_ptr<ModelAPI_Data> Model_Data::invalidData()
703 {
704   return kInvalid;
705 }
706
707 std::shared_ptr<ModelAPI_Object> Model_Data::owner()
708 {
709   return myObject;
710 }