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