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