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