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