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