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