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