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