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