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