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