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