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