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