Salome HOME
Add copyright header according to request of CEA from 06.06.2017
[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         myObject->attributeChanged(theAttr->id());
311         static const Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
312         ModelAPI_EventCreator::get()->sendUpdated(myObject, anEvent);
313       }
314     } else {
315       // to avoid too many duplications do not add the same like the last
316       if (myWasChangedButBlocked.empty() || *(myWasChangedButBlocked.rbegin()) != theAttr)
317         myWasChangedButBlocked.push_back(theAttr);
318     }
319   } else {
320     // trim: need to redisplay
321     if (myObject) {
322       static const Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY);
323       ModelAPI_EventCreator::get()->sendUpdated(myObject, anEvent);
324     }
325   }
326 }
327
328 bool Model_Data::blockSendAttributeUpdated(const bool theBlock, const bool theSendMessage)
329 {
330   bool aWasBlocked = !mySendAttributeUpdated;
331   if (mySendAttributeUpdated == theBlock) {
332     mySendAttributeUpdated = !theBlock;
333     if (mySendAttributeUpdated && !myWasChangedButBlocked.empty()) {
334       // so, now it is ok to send the update signal
335       if (theSendMessage) {
336         // make a copy to avoid iteration on modified list
337         // (may be cleared by attribute changed call)
338         std::list<ModelAPI_Attribute*> aWasChangedButBlocked = myWasChangedButBlocked;
339         myWasChangedButBlocked.clear();
340         std::list<ModelAPI_Attribute*>::iterator aChangedIter = aWasChangedButBlocked.begin();
341         for(; aChangedIter != aWasChangedButBlocked.end(); aChangedIter++) {
342           myObject->attributeChanged((*aChangedIter)->id());
343         }
344         static const Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
345         ModelAPI_EventCreator::get()->sendUpdated(myObject, anEvent);
346       } else {
347         myWasChangedButBlocked.clear();
348       }
349     }
350   }
351   return aWasBlocked;
352 }
353
354 void Model_Data::erase()
355 {
356   if (!myLab.IsNull()) {
357     if (myLab.HasAttribute()) {
358       // remove in order to clear back references in other objects
359       std::list<std::pair<std::string, std::list<ObjectPtr> > > aRefs;
360       referencesToObjects(aRefs);
361       std::list<std::pair<std::string, std::list<ObjectPtr> > >::iterator
362         anAttrIter = aRefs.begin();
363       for(; anAttrIter != aRefs.end(); anAttrIter++) {
364         std::list<ObjectPtr>::iterator aReferenced = anAttrIter->second.begin();
365         for(; aReferenced != anAttrIter->second.end(); aReferenced++) {
366           if (aReferenced->get() && (*aReferenced)->data()->isValid()) {
367             std::shared_ptr<Model_Data> aData =
368               std::dynamic_pointer_cast<Model_Data>((*aReferenced)->data());
369             aData->removeBackReference(myAttrs[anAttrIter->first]);
370           }
371         }
372       }
373     }
374     myAttrs.clear();
375     myLab.ForgetAllAttributes();
376   }
377 }
378
379 // indexes in the state array
380 enum StatesIndexes {
381   STATE_INDEX_STATE = 1, // the state type itself
382   STATE_INDEX_TRANSACTION = 2, // transaction ID
383 };
384
385 /// Returns the label array, initialises it by default values if not exists
386 static Handle(TDataStd_IntegerArray) stateArray(TDF_Label& theLab)
387 {
388   Handle(TDataStd_IntegerArray) aStateArray;
389   if (!theLab.FindAttribute(TDataStd_IntegerArray::GetID(), aStateArray)) {
390     aStateArray = TDataStd_IntegerArray::Set(theLab, 1, 2);
391     aStateArray->SetValue(STATE_INDEX_STATE, ModelAPI_StateMustBeUpdated); // default state
392     aStateArray->SetValue(STATE_INDEX_TRANSACTION, 0); // default transaction ID (not existing)
393   }
394   return aStateArray;
395 }
396
397 void Model_Data::execState(const ModelAPI_ExecState theState)
398 {
399   if (theState != ModelAPI_StateNothing) {
400     if (stateArray(myLab)->Value(STATE_INDEX_STATE) != (int)theState) {
401       stateArray(myLab)->SetValue(STATE_INDEX_STATE, (int)theState);
402     }
403   }
404 }
405
406 ModelAPI_ExecState Model_Data::execState()
407 {
408   return ModelAPI_ExecState(stateArray(myLab)->Value(STATE_INDEX_STATE));
409 }
410
411 int Model_Data::updateID()
412 {
413   return stateArray(myLab)->Value(STATE_INDEX_TRANSACTION);
414 }
415
416 void Model_Data::setUpdateID(const int theID)
417 {
418   stateArray(myLab)->SetValue(STATE_INDEX_TRANSACTION, theID);
419 }
420
421 void Model_Data::setError(const std::string& theError, bool theSend)
422 {
423   execState(ModelAPI_StateExecFailed);
424   if (theSend) {
425     Events_InfoMessage("Model_Data", theError).send();
426   }
427   TDataStd_AsciiString::Set(myLab, theError.c_str());
428 }
429
430 void Model_Data::eraseErrorString()
431 {
432   myLab.ForgetAttribute(TDataStd_AsciiString::GetID());
433 }
434
435 std::string Model_Data::error() const
436 {
437   Handle(TDataStd_AsciiString) anErrorAttr;
438   if (myLab.FindAttribute(TDataStd_AsciiString::GetID(), anErrorAttr)) {
439     return std::string(anErrorAttr->Get().ToCString());
440   }
441   return std::string();
442 }
443
444 int Model_Data::featureId() const
445 {
446   return myLab.Father().Tag(); // tag of the feature label
447 }
448
449 void Model_Data::eraseBackReferences()
450 {
451   myRefsToMe.clear();
452   std::shared_ptr<ModelAPI_Result> aRes =
453     std::dynamic_pointer_cast<ModelAPI_Result>(myObject);
454   if (aRes)
455     aRes->setIsConcealed(false);
456 }
457
458 void Model_Data::removeBackReference(FeaturePtr theFeature, std::string theAttrID)
459 {
460   AttributePtr anAttribute = theFeature->data()->attribute(theAttrID);
461   removeBackReference(anAttribute);
462 }
463
464 void Model_Data::removeBackReference(AttributePtr theAttr)
465 {
466   if (myRefsToMe.find(theAttr) == myRefsToMe.end())
467     return;
468
469   myRefsToMe.erase(theAttr);
470
471   // remove concealment immideately: on deselection it must be posible to reselect in GUI the same
472   FeaturePtr aFeatureOwner = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttr->owner());
473   if (aFeatureOwner.get() &&
474     ModelAPI_Session::get()->validators()->isConcealed(aFeatureOwner->getKind(), theAttr->id())) {
475     updateConcealmentFlag();
476   }
477 }
478
479 void Model_Data::addBackReference(FeaturePtr theFeature, std::string theAttrID,
480    const bool theApplyConcealment)
481 {
482   // it is possible to add the same attribute twice: may be last time the owner was not Stable...
483   AttributePtr anAttribute = theFeature->data()->attribute(theAttrID);
484   if (myRefsToMe.find(anAttribute) == myRefsToMe.end())
485     myRefsToMe.insert(theFeature->data()->attribute(theAttrID));
486
487   if (theApplyConcealment &&  theFeature->isStable() &&
488       ModelAPI_Session::get()->validators()->isConcealed(theFeature->getKind(), theAttrID)) {
489     std::shared_ptr<ModelAPI_Result> aRes =
490       std::dynamic_pointer_cast<ModelAPI_Result>(myObject);
491     // the second condition is for history upper than concealment causer, so the feature result may
492     // be displayed and previewed; also for avoiding of quick show/hide on history
493     // moving deep down
494     if (aRes && !theFeature->isDisabled()) {
495       aRes->setIsConcealed(true);
496     }
497   }
498 }
499
500 void Model_Data::updateConcealmentFlag()
501 {
502   std::set<AttributePtr>::iterator aRefsIter = myRefsToMe.begin();
503   for(; aRefsIter != myRefsToMe.end(); aRefsIter++) {
504     if (aRefsIter->get()) {
505       FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>((*aRefsIter)->owner());
506       if (aFeature.get() && !aFeature->isDisabled() && aFeature->isStable()) {
507         if (ModelAPI_Session::get()->validators()->isConcealed(
508               aFeature->getKind(), (*aRefsIter)->id())) {
509           std::shared_ptr<ModelAPI_Result> aRes =
510             std::dynamic_pointer_cast<ModelAPI_Result>(myObject);
511           if (aRes.get()) {
512             aRes->setIsConcealed(true); // set concealed
513             return;
514           }
515         }
516       }
517     }
518   }
519   std::shared_ptr<ModelAPI_Result> aRes =
520     std::dynamic_pointer_cast<ModelAPI_Result>(myObject);
521   if (aRes.get()) {
522     aRes->setIsConcealed(false);
523   }
524 }
525
526 std::set<std::string> set_union(const std::set<std::string>& theLeft,
527                                 const std::set<std::string>& theRight)
528 {
529   std::set<std::string> aResult;
530   aResult.insert(theLeft.begin(), theLeft.end());
531   aResult.insert(theRight.begin(), theRight.end());
532   return aResult;
533 }
534
535 std::set<std::string> usedParameters(const AttributePointPtr& theAttribute)
536 {
537   std::set<std::string> anUsedParameters;
538   for (int aComponent = 0; aComponent < 3; ++aComponent)
539     anUsedParameters = set_union(anUsedParameters, theAttribute->usedParameters(aComponent));
540   return anUsedParameters;
541 }
542
543 std::set<std::string> usedParameters(const AttributePoint2DPtr& theAttribute)
544 {
545   std::set<std::string> anUsedParameters;
546   for (int aComponent = 0; aComponent < 2; ++aComponent)
547     anUsedParameters = set_union(anUsedParameters, theAttribute->usedParameters(aComponent));
548   return anUsedParameters;
549 }
550
551 std::list<ResultParameterPtr> findVariables(const std::set<std::string>& theParameters)
552 {
553   std::list<ResultParameterPtr> aResult;
554   std::set<std::string>::const_iterator aParamIt = theParameters.cbegin();
555   for (; aParamIt != theParameters.cend(); ++aParamIt) {
556     const std::string& aName = *aParamIt;
557     double aValue;
558     ResultParameterPtr aParam;
559     // theSearcher is not needed here: in expressions
560     // of features the parameters history is not needed
561     if (ModelAPI_Tools::findVariable(FeaturePtr(), aName, aValue, aParam))
562       aResult.push_back(aParam);
563   }
564   return aResult;
565 }
566
567 void Model_Data::referencesToObjects(
568   std::list<std::pair<std::string, std::list<ObjectPtr> > >& theRefs)
569 {
570   static Model_ValidatorsFactory* aValidators =
571     static_cast<Model_ValidatorsFactory*>(ModelAPI_Session::get()->validators());
572   FeaturePtr aMyFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(myObject);
573
574   std::map<std::string, std::shared_ptr<ModelAPI_Attribute> >::iterator anAttr = myAttrs.begin();
575   std::list<ObjectPtr> aReferenced; // not inside of cycle to avoid excess memory management
576   for(; anAttr != myAttrs.end(); anAttr++) {
577     // skip not-case attributes, that really may refer to anything not-used (issue 671)
578     if (aMyFeature.get() && !aValidators->isCase(aMyFeature, anAttr->second->id()))
579       continue;
580
581     std::string aType = anAttr->second->attributeType();
582     if (aType == ModelAPI_AttributeReference::typeId()) { // reference to object
583       std::shared_ptr<ModelAPI_AttributeReference> aRef = std::dynamic_pointer_cast<
584           ModelAPI_AttributeReference>(anAttr->second);
585       aReferenced.push_back(aRef->value());
586     } else if (aType == ModelAPI_AttributeRefAttr::typeId()) { // reference to attribute or object
587       std::shared_ptr<ModelAPI_AttributeRefAttr> aRef = std::dynamic_pointer_cast<
588           ModelAPI_AttributeRefAttr>(anAttr->second);
589       if (aRef->isObject()) {
590         aReferenced.push_back(aRef->object());
591       } else {
592         AttributePtr anAttr = aRef->attr();
593         if (anAttr.get())
594           aReferenced.push_back(anAttr->owner());
595       }
596     } else if (aType == ModelAPI_AttributeRefList::typeId()) { // list of references
597       aReferenced = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(anAttr->second)->list();
598     } else if (aType == ModelAPI_AttributeSelection::typeId()) { // selection attribute
599       std::shared_ptr<ModelAPI_AttributeSelection> aRef = std::dynamic_pointer_cast<
600           ModelAPI_AttributeSelection>(anAttr->second);
601       aReferenced.push_back(aRef->context());
602     } else if (aType == ModelAPI_AttributeSelectionList::typeId()) { // list of selection attributes
603       std::shared_ptr<ModelAPI_AttributeSelectionList> aRef = std::dynamic_pointer_cast<
604           ModelAPI_AttributeSelectionList>(anAttr->second);
605       for(int a = aRef->size() - 1; a >= 0; a--) {
606         aReferenced.push_back(aRef->value(a)->context());
607       }
608     } else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
609       std::shared_ptr<ModelAPI_AttributeRefAttrList> aRefAttr = std::dynamic_pointer_cast<
610           ModelAPI_AttributeRefAttrList>(anAttr->second);
611       std::list<std::pair<ObjectPtr, AttributePtr> > aRefs = aRefAttr->list();
612       std::list<std::pair<ObjectPtr, AttributePtr> >::const_iterator anIt = aRefs.begin(),
613                                                                      aLast = aRefs.end();
614       for (; anIt != aLast; anIt++) {
615         aReferenced.push_back(anIt->first);
616       }
617     } else if (aType == ModelAPI_AttributeInteger::typeId()) { // integer attribute
618       AttributeIntegerPtr anAttribute =
619           std::dynamic_pointer_cast<ModelAPI_AttributeInteger>(anAttr->second);
620       std::set<std::string> anUsedParameters = anAttribute->usedParameters();
621       std::list<ResultParameterPtr> aParameters = findVariables(anUsedParameters);
622       aReferenced.insert(aReferenced.end(), aParameters.begin(), aParameters.end());
623     } else if (aType == ModelAPI_AttributeDouble::typeId()) { // double attribute
624       AttributeDoublePtr anAttribute =
625           std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(anAttr->second);
626       std::set<std::string> anUsedParameters = anAttribute->usedParameters();
627       std::list<ResultParameterPtr> aParameters = findVariables(anUsedParameters);
628       aReferenced.insert(aReferenced.end(), aParameters.begin(), aParameters.end());
629     } else if (aType == GeomDataAPI_Point::typeId()) { // point attribute
630       AttributePointPtr anAttribute =
631         std::dynamic_pointer_cast<GeomDataAPI_Point>(anAttr->second);
632       std::set<std::string> anUsedParameters = usedParameters(anAttribute);
633       std::list<ResultParameterPtr> aParameters = findVariables(anUsedParameters);
634       aReferenced.insert(aReferenced.end(), aParameters.begin(), aParameters.end());
635     } else if (aType == GeomDataAPI_Point2D::typeId()) { // point attribute
636       AttributePoint2DPtr anAttribute =
637         std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttr->second);
638       std::set<std::string> anUsedParameters = usedParameters(anAttribute);
639       std::list<ResultParameterPtr> aParameters = findVariables(anUsedParameters);
640       aReferenced.insert(aReferenced.end(), aParameters.begin(), aParameters.end());
641     } else
642       continue; // nothing to do, not reference
643
644     if (!aReferenced.empty()) {
645       theRefs.push_back(std::pair<std::string, std::list<ObjectPtr> >(anAttr->first, aReferenced));
646       aReferenced.clear();
647     }
648   }
649 }
650
651 /// makes copy of all attributes on the given label and all sub-labels
652 static void copyAttrs(TDF_Label theSource, TDF_Label theDestination) {
653   TDF_AttributeIterator anAttrIter(theSource);
654   for(; anAttrIter.More(); anAttrIter.Next()) {
655     Handle(TDF_Attribute) aTargetAttr;
656     if (!theDestination.FindAttribute(anAttrIter.Value()->ID(), aTargetAttr)) {
657       // create a new attribute if not yet exists in the destination
658             aTargetAttr = anAttrIter.Value()->NewEmpty();
659       theDestination.AddAttribute(aTargetAttr);
660     }
661     // no special relocation, empty map, but self-relocation is on: copy references w/o changes
662     Handle(TDF_RelocationTable) aRelocTable = new TDF_RelocationTable(Standard_True);
663     anAttrIter.Value()->Paste(aTargetAttr, aRelocTable);
664   }
665   // copy the sub-labels content
666   TDF_ChildIterator aSubLabsIter(theSource);
667   for(; aSubLabsIter.More(); aSubLabsIter.Next()) {
668     copyAttrs(aSubLabsIter.Value(), theDestination.FindChild(aSubLabsIter.Value().Tag()));
669   }
670 }
671
672 void Model_Data::copyTo(std::shared_ptr<ModelAPI_Data> theTarget)
673 {
674   TDF_Label aTargetRoot = std::dynamic_pointer_cast<Model_Data>(theTarget)->label();
675   copyAttrs(myLab, aTargetRoot);
676   // reinitialize Model_Attributes by TDF_Attributes set
677   std::shared_ptr<Model_Data> aTData = std::dynamic_pointer_cast<Model_Data>(theTarget);
678   aTData->myAttrs.clear();
679   theTarget->owner()->initAttributes(); // reinit feature attributes
680 }
681
682 bool Model_Data::isInHistory()
683 {
684   return myFlags->Value(kFlagInHistory) == Standard_True;
685 }
686
687 void Model_Data::setIsInHistory(const bool theFlag)
688 {
689   return myFlags->SetValue(kFlagInHistory, theFlag);
690 }
691
692 bool Model_Data::isDeleted()
693 {
694   return myFlags->Value(kFlagDeleted) == Standard_True;
695 }
696
697 void Model_Data::setIsDeleted(const bool theFlag)
698 {
699   return myFlags->SetValue(kFlagDeleted, theFlag);
700 }
701
702 bool Model_Data::isDisplayed()
703 {
704   if (!myObject.get() || !myObject->document().get() || // object is in valid
705       myFlags->Value(kFlagDisplayed) != Standard_True) // or it was not displayed before
706     return false;
707   if (myObject->document()->isActive()) // for active documents it must be ok anyway
708     return true;
709   // any object from the root document except part result may be displayed
710   if (myObject->document() == ModelAPI_Session::get()->moduleDocument() &&
711       myObject->groupName() != ModelAPI_ResultPart::group())
712     return true;
713   return false;
714 }
715
716 void Model_Data::setDisplayed(const bool theDisplay)
717 {
718   if (theDisplay != isDisplayed()) {
719     myFlags->SetValue(kFlagDisplayed, theDisplay);
720     static Events_Loop* aLoop = Events_Loop::loop();
721     static Events_ID EVENT_DISP = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
722     static const ModelAPI_EventCreator* aECreator = ModelAPI_EventCreator::get();
723     aECreator->sendUpdated(myObject, EVENT_DISP);
724   }
725 }
726
727 std::shared_ptr<ModelAPI_Data> Model_Data::invalidPtr()
728 {
729   return kInvalid;
730 }
731
732 std::shared_ptr<ModelAPI_Data> Model_Data::invalidData()
733 {
734   return kInvalid;
735 }
736
737 std::shared_ptr<ModelAPI_Object> Model_Data::owner()
738 {
739   return myObject;
740 }