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