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