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