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