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