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