1 // Copyright (C) 2014-2017 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
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>
49 #include <GeomDataAPI_Point.h>
50 #include <GeomDataAPI_Point2D.h>
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>
58 #include <TDataStd_Name.hxx>
59 #include <TDataStd_AsciiString.hxx>
60 #include <TDataStd_IntegerArray.hxx>
61 #include <TDF_AttributeIterator.hxx>
62 #include <TDF_ChildIterator.hxx>
63 #include <TDF_RelocationTable.hxx>
64 #include <TColStd_HArray1OfByte.hxx>
69 // TDataStd_Name - name of the object
70 // TDataStd_IntegerArray - state of the object execution, transaction ID of update
71 // TDataStd_BooleanArray - array of flags of this data:
72 // 0 - is in history or not
73 static const int kFlagInHistory = 0;
74 // 1 - is displayed or not
75 static const int kFlagDisplayed = 1;
76 // 2 - is deleted (for results) or not
77 static const int kFlagDeleted = 2;
80 const static std::shared_ptr<ModelAPI_Data> kInvalid(new Model_Data());
82 Model_Data::Model_Data() : mySendAttributeUpdated(true), myWasChangedButBlocked(false)
86 void Model_Data::setLabel(TDF_Label theLab)
89 // set or get the default flags
90 if (!myLab.FindAttribute(TDataStd_BooleanArray::GetID(), myFlags)) {
91 // set default values if not found
92 myFlags = TDataStd_BooleanArray::Set(myLab, 0, 2);
93 myFlags->SetValue(kFlagInHistory, Standard_True); // is in history by default is true
94 myFlags->SetValue(kFlagDisplayed, Standard_True); // is displayed by default is true
95 myFlags->SetValue(kFlagDeleted, Standard_False); // is deleted by default is false
96 } else if (myFlags->Length() != 3) { // for old formats support
97 Standard_Boolean aFlag0 = myFlags->Upper() >= 0 ? myFlags->Value(0) : Standard_True;
98 Standard_Boolean aFlag1 = myFlags->Upper() >= 1 ? myFlags->Value(1) : Standard_True;
99 Standard_Boolean aFlag2 = myFlags->Upper() >= 2 ? myFlags->Value(2) : Standard_True;
100 Handle(TColStd_HArray1OfByte) aNewArray = new TColStd_HArray1OfByte(0, 2);
101 myFlags->SetInternalArray(aNewArray);
102 myFlags->SetValue(0, aFlag0);
103 myFlags->SetValue(1, aFlag1);
104 myFlags->SetValue(2, aFlag2);
108 std::string Model_Data::name()
110 Handle(TDataStd_Name) aName;
111 if (myLab.FindAttribute(TDataStd_Name::GetID(), aName)) {
113 myObject->myName = TCollection_AsciiString(aName->Get()).ToCString();
115 return std::string(TCollection_AsciiString(aName->Get()).ToCString());
117 return ""; // not defined
120 void Model_Data::setName(const std::string& theName)
122 bool isModified = false;
123 std::string anOldName = name();
124 Handle(TDataStd_Name) aName;
125 if (!myLab.FindAttribute(TDataStd_Name::GetID(), aName)) {
126 TDataStd_Name::Set(myLab, theName.c_str());
129 isModified = !aName->Get().IsEqual(theName.c_str());
131 aName->Set(theName.c_str());
133 if (mySendAttributeUpdated && isModified)
134 ModelAPI_ObjectRenamedMessage::send(myObject, anOldName, theName, this);
135 if (isModified && myObject && myObject->document()) {
136 std::dynamic_pointer_cast<Model_Document>(myObject->document())->
137 changeNamingName(anOldName, theName);
140 myObject->myName = theName;
144 AttributePtr Model_Data::addAttribute(const std::string& theID, const std::string theAttrType)
146 AttributePtr aResult;
147 TDF_Label anAttrLab = myLab.FindChild(int(myAttrs.size()) + 1);
148 ModelAPI_Attribute* anAttr = 0;
149 if (theAttrType == ModelAPI_AttributeDocRef::typeId()) {
150 anAttr = new Model_AttributeDocRef(anAttrLab);
151 } else if (theAttrType == Model_AttributeInteger::typeId()) {
152 anAttr = new Model_AttributeInteger(anAttrLab);
153 } else if (theAttrType == ModelAPI_AttributeDouble::typeId()) {
154 anAttr = new Model_AttributeDouble(anAttrLab);
155 } else if (theAttrType == Model_AttributeBoolean::typeId()) {
156 anAttr = new Model_AttributeBoolean(anAttrLab);
157 } else if (theAttrType == Model_AttributeString::typeId()) {
158 anAttr = new Model_AttributeString(anAttrLab);
159 } else if (theAttrType == Model_AttributeStringArray::typeId()) {
160 anAttr = new Model_AttributeStringArray(anAttrLab);
161 } else if (theAttrType == ModelAPI_AttributeReference::typeId()) {
162 anAttr = new Model_AttributeReference(anAttrLab);
163 } else if (theAttrType == ModelAPI_AttributeSelection::typeId()) {
164 anAttr = new Model_AttributeSelection(anAttrLab);
165 } else if (theAttrType == ModelAPI_AttributeSelectionList::typeId()) {
166 anAttr = new Model_AttributeSelectionList(anAttrLab);
167 } else if (theAttrType == ModelAPI_AttributeRefAttr::typeId()) {
168 anAttr = new Model_AttributeRefAttr(anAttrLab);
169 } else if (theAttrType == ModelAPI_AttributeRefList::typeId()) {
170 anAttr = new Model_AttributeRefList(anAttrLab);
171 } else if (theAttrType == ModelAPI_AttributeRefAttrList::typeId()) {
172 anAttr = new Model_AttributeRefAttrList(anAttrLab);
173 } else if (theAttrType == ModelAPI_AttributeIntArray::typeId()) {
174 anAttr = new Model_AttributeIntArray(anAttrLab);
175 } else if (theAttrType == ModelAPI_AttributeDoubleArray::typeId()) {
176 anAttr = new Model_AttributeDoubleArray(anAttrLab);
177 } else if (theAttrType == ModelAPI_AttributeTables::typeId()) {
178 anAttr = new Model_AttributeTables(anAttrLab);
180 // create also GeomData attributes here because only here the OCAF structure is known
181 else if (theAttrType == GeomData_Point::typeId()) {
182 GeomData_Point* anAttribute = new GeomData_Point();
183 bool anAllInitialized = true;
184 for (int aComponent = 0; aComponent < GeomData_Point::NUM_COMPONENTS; ++aComponent) {
185 TDF_Label anExpressionLab = anAttrLab.FindChild(aComponent + 1);
186 anAttribute->myExpression[aComponent].reset(new Model_ExpressionDouble(anExpressionLab));
187 anAllInitialized = anAllInitialized && anAttribute->myExpression[aComponent]->isInitialized();
189 anAttribute->myIsInitialized = anAllInitialized;
190 anAttr = anAttribute;
191 } else if (theAttrType == GeomData_Dir::typeId()) {
192 anAttr = new GeomData_Dir(anAttrLab);
193 } else if (theAttrType == GeomData_Point2D::typeId()) {
194 GeomData_Point2D* anAttribute = new GeomData_Point2D();
195 bool anAllInitialized = true;
196 for (int aComponent = 0; aComponent < GeomData_Point2D::NUM_COMPONENTS; ++aComponent) {
197 TDF_Label anExpressionLab = anAttrLab.FindChild(aComponent + 1);
198 anAttribute->myExpression[aComponent].reset(new Model_ExpressionDouble(anExpressionLab));
199 anAllInitialized = anAllInitialized && anAttribute->myExpression[aComponent]->isInitialized();
201 anAttribute->myIsInitialized = anAllInitialized;
202 anAttr = anAttribute;
205 aResult = std::shared_ptr<ModelAPI_Attribute>(anAttr);
206 myAttrs[theID] = aResult;
207 anAttr->setObject(myObject);
208 anAttr->setID(theID);
210 Events_InfoMessage("Model_Data",
211 "Can not create unknown type of attribute %1").arg(theAttrType).send();
216 // macro for the generic returning of the attribute by the ID
217 #define GET_ATTRIBUTE_BY_ID(ATTR_TYPE, METHOD_NAME) \
218 std::shared_ptr<ATTR_TYPE> Model_Data::METHOD_NAME(const std::string& theID) { \
219 std::shared_ptr<ATTR_TYPE> aRes; \
220 std::map<std::string, AttributePtr >::iterator aFound = \
221 myAttrs.find(theID); \
222 if (aFound != myAttrs.end()) { \
223 aRes = std::dynamic_pointer_cast<ATTR_TYPE>(aFound->second); \
227 // implement nice getting methods for all ModelAPI attributes
228 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeDocRef, document);
229 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeDouble, real);
230 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeInteger, integer);
231 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeBoolean, boolean);
232 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeString, string);
233 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeStringArray, stringArray);
234 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeReference, reference);
235 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeSelection, selection);
236 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeSelectionList, selectionList);
237 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeRefAttr, refattr);
238 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeRefList, reflist);
239 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeRefAttrList, refattrlist);
240 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeIntArray, intArray);
241 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeDoubleArray, realArray);
242 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeTables, tables);
244 std::shared_ptr<ModelAPI_Attribute> Model_Data::attribute(const std::string& theID)
246 std::shared_ptr<ModelAPI_Attribute> aResult;
247 if (myAttrs.find(theID) == myAttrs.end()) // no such attribute
249 return myAttrs[theID];
252 const std::string& Model_Data::id(const std::shared_ptr<ModelAPI_Attribute>& theAttr)
254 std::map<std::string, std::shared_ptr<ModelAPI_Attribute> >::iterator anAttr =
256 for (; anAttr != myAttrs.end(); anAttr++) {
257 if (anAttr->second == theAttr)
258 return anAttr->first;
261 static std::string anEmpty;
265 bool Model_Data::isEqual(const std::shared_ptr<ModelAPI_Data>& theData)
267 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(theData);
269 return myLab.IsEqual(aData->myLab) == Standard_True ;
273 bool Model_Data::isValid()
275 return !myLab.IsNull() && myLab.HasAttribute();
278 std::list<std::shared_ptr<ModelAPI_Attribute> > Model_Data::attributes(const std::string& theType)
280 std::list<std::shared_ptr<ModelAPI_Attribute> > aResult;
281 std::map<std::string, std::shared_ptr<ModelAPI_Attribute> >::iterator anAttrsIter =
283 for (; anAttrsIter != myAttrs.end(); anAttrsIter++) {
284 if (theType.empty() || anAttrsIter->second->attributeType() == theType) {
285 aResult.push_back(anAttrsIter->second);
291 std::list<std::string> Model_Data::attributesIDs(const std::string& theType)
293 std::list<std::string> aResult;
294 std::map<std::string, std::shared_ptr<ModelAPI_Attribute> >::iterator anAttrsIter =
296 for (; anAttrsIter != myAttrs.end(); anAttrsIter++) {
297 if (theType.empty() || anAttrsIter->second->attributeType() == theType) {
298 aResult.push_back(anAttrsIter->first);
304 void Model_Data::sendAttributeUpdated(ModelAPI_Attribute* theAttr)
306 theAttr->setInitialized();
307 if (theAttr->isArgument()) {
308 if (mySendAttributeUpdated) {
311 myObject->attributeChanged(theAttr->id());
313 if (owner().get() && owner()->data().get() && owner()->data()->isValid()) {
314 Events_InfoMessage("Model_Data",
315 "%1 has failed during the update").arg(owner()->data()->name()).send();
318 static const Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
319 ModelAPI_EventCreator::get()->sendUpdated(myObject, anEvent);
322 // to avoid too many duplications do not add the same like the last
323 if (myWasChangedButBlocked.empty() || *(myWasChangedButBlocked.rbegin()) != theAttr)
324 myWasChangedButBlocked.push_back(theAttr);
327 // trim: need to redisplay
329 static const Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY);
330 ModelAPI_EventCreator::get()->sendUpdated(myObject, anEvent);
335 bool Model_Data::blockSendAttributeUpdated(const bool theBlock, const bool theSendMessage)
337 bool aWasBlocked = !mySendAttributeUpdated;
338 if (mySendAttributeUpdated == theBlock) {
339 mySendAttributeUpdated = !theBlock;
340 if (mySendAttributeUpdated && !myWasChangedButBlocked.empty()) {
341 // so, now it is ok to send the update signal
342 if (theSendMessage) {
343 // make a copy to avoid iteration on modified list
344 // (may be cleared by attribute changed call)
345 std::list<ModelAPI_Attribute*> aWasChangedButBlocked = myWasChangedButBlocked;
346 myWasChangedButBlocked.clear();
347 std::list<ModelAPI_Attribute*>::iterator aChangedIter = aWasChangedButBlocked.begin();
348 for(; aChangedIter != aWasChangedButBlocked.end(); aChangedIter++) {
350 myObject->attributeChanged((*aChangedIter)->id());
352 if (owner().get() && owner()->data().get() && owner()->data()->isValid()) {
353 Events_InfoMessage("Model_Data",
354 "%1 has failed during the update").arg(owner()->data()->name()).send();
358 static const Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
359 ModelAPI_EventCreator::get()->sendUpdated(myObject, anEvent);
361 myWasChangedButBlocked.clear();
368 void Model_Data::erase()
370 if (!myLab.IsNull()) {
371 if (myLab.HasAttribute()) {
372 // remove in order to clear back references in other objects
373 std::list<std::pair<std::string, std::list<ObjectPtr> > > aRefs;
374 referencesToObjects(aRefs);
375 std::list<std::pair<std::string, std::list<ObjectPtr> > >::iterator
376 anAttrIter = aRefs.begin();
377 for(; anAttrIter != aRefs.end(); anAttrIter++) {
378 std::list<ObjectPtr>::iterator aReferenced = anAttrIter->second.begin();
379 for(; aReferenced != anAttrIter->second.end(); aReferenced++) {
380 if (aReferenced->get() && (*aReferenced)->data()->isValid()) {
381 std::shared_ptr<Model_Data> aData =
382 std::dynamic_pointer_cast<Model_Data>((*aReferenced)->data());
383 aData->removeBackReference(myAttrs[anAttrIter->first]);
389 myLab.ForgetAllAttributes();
393 // indexes in the state array
395 STATE_INDEX_STATE = 1, // the state type itself
396 STATE_INDEX_TRANSACTION = 2, // transaction ID
399 /// Returns the label array, initialises it by default values if not exists
400 static Handle(TDataStd_IntegerArray) stateArray(TDF_Label& theLab)
402 Handle(TDataStd_IntegerArray) aStateArray;
403 if (!theLab.FindAttribute(TDataStd_IntegerArray::GetID(), aStateArray)) {
404 aStateArray = TDataStd_IntegerArray::Set(theLab, 1, 2);
405 aStateArray->SetValue(STATE_INDEX_STATE, ModelAPI_StateMustBeUpdated); // default state
406 aStateArray->SetValue(STATE_INDEX_TRANSACTION, 0); // default transaction ID (not existing)
411 void Model_Data::execState(const ModelAPI_ExecState theState)
413 if (theState != ModelAPI_StateNothing) {
414 if (stateArray(myLab)->Value(STATE_INDEX_STATE) != (int)theState) {
415 stateArray(myLab)->SetValue(STATE_INDEX_STATE, (int)theState);
420 ModelAPI_ExecState Model_Data::execState()
422 return ModelAPI_ExecState(stateArray(myLab)->Value(STATE_INDEX_STATE));
425 int Model_Data::updateID()
427 return stateArray(myLab)->Value(STATE_INDEX_TRANSACTION);
430 void Model_Data::setUpdateID(const int theID)
432 stateArray(myLab)->SetValue(STATE_INDEX_TRANSACTION, theID);
435 void Model_Data::setError(const std::string& theError, bool theSend)
437 execState(ModelAPI_StateExecFailed);
439 Events_InfoMessage("Model_Data", theError).send();
441 TDataStd_AsciiString::Set(myLab, theError.c_str());
444 void Model_Data::eraseErrorString()
446 myLab.ForgetAttribute(TDataStd_AsciiString::GetID());
449 std::string Model_Data::error() const
451 Handle(TDataStd_AsciiString) anErrorAttr;
452 if (myLab.FindAttribute(TDataStd_AsciiString::GetID(), anErrorAttr)) {
453 return std::string(anErrorAttr->Get().ToCString());
455 return std::string();
458 int Model_Data::featureId() const
460 return myLab.Father().Tag(); // tag of the feature label
463 void Model_Data::eraseBackReferences()
466 std::shared_ptr<ModelAPI_Result> aRes =
467 std::dynamic_pointer_cast<ModelAPI_Result>(myObject);
469 aRes->setIsConcealed(false);
472 void Model_Data::removeBackReference(FeaturePtr theFeature, std::string theAttrID)
474 AttributePtr anAttribute = theFeature->data()->attribute(theAttrID);
475 removeBackReference(anAttribute);
478 void Model_Data::removeBackReference(AttributePtr theAttr)
480 if (myRefsToMe.find(theAttr) == myRefsToMe.end())
483 myRefsToMe.erase(theAttr);
485 // remove concealment immideately: on deselection it must be posible to reselect in GUI the same
486 FeaturePtr aFeatureOwner = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttr->owner());
487 if (aFeatureOwner.get() &&
488 ModelAPI_Session::get()->validators()->isConcealed(aFeatureOwner->getKind(), theAttr->id())) {
489 updateConcealmentFlag();
493 void Model_Data::addBackReference(FeaturePtr theFeature, std::string theAttrID,
494 const bool theApplyConcealment)
496 // it is possible to add the same attribute twice: may be last time the owner was not Stable...
497 AttributePtr anAttribute = theFeature->data()->attribute(theAttrID);
498 if (myRefsToMe.find(anAttribute) == myRefsToMe.end())
499 myRefsToMe.insert(theFeature->data()->attribute(theAttrID));
501 if (theApplyConcealment && theFeature->isStable() &&
502 ModelAPI_Session::get()->validators()->isConcealed(theFeature->getKind(), theAttrID)) {
503 std::shared_ptr<ModelAPI_Result> aRes =
504 std::dynamic_pointer_cast<ModelAPI_Result>(myObject);
505 // the second condition is for history upper than concealment causer, so the feature result may
506 // be displayed and previewed; also for avoiding of quick show/hide on history
508 if (aRes && !theFeature->isDisabled()) {
509 aRes->setIsConcealed(true);
514 void Model_Data::updateConcealmentFlag()
516 std::set<AttributePtr>::iterator aRefsIter = myRefsToMe.begin();
517 for(; aRefsIter != myRefsToMe.end(); aRefsIter++) {
518 if (aRefsIter->get()) {
519 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>((*aRefsIter)->owner());
520 if (aFeature.get() && !aFeature->isDisabled() && aFeature->isStable()) {
521 if (ModelAPI_Session::get()->validators()->isConcealed(
522 aFeature->getKind(), (*aRefsIter)->id())) {
523 std::shared_ptr<ModelAPI_Result> aRes =
524 std::dynamic_pointer_cast<ModelAPI_Result>(myObject);
526 aRes->setIsConcealed(true); // set concealed
533 std::shared_ptr<ModelAPI_Result> aRes =
534 std::dynamic_pointer_cast<ModelAPI_Result>(myObject);
536 aRes->setIsConcealed(false);
540 std::set<std::string> set_union(const std::set<std::string>& theLeft,
541 const std::set<std::string>& theRight)
543 std::set<std::string> aResult;
544 aResult.insert(theLeft.begin(), theLeft.end());
545 aResult.insert(theRight.begin(), theRight.end());
549 std::set<std::string> usedParameters(const AttributePointPtr& theAttribute)
551 std::set<std::string> anUsedParameters;
552 for (int aComponent = 0; aComponent < 3; ++aComponent)
553 anUsedParameters = set_union(anUsedParameters, theAttribute->usedParameters(aComponent));
554 return anUsedParameters;
557 std::set<std::string> usedParameters(const AttributePoint2DPtr& theAttribute)
559 std::set<std::string> anUsedParameters;
560 for (int aComponent = 0; aComponent < 2; ++aComponent)
561 anUsedParameters = set_union(anUsedParameters, theAttribute->usedParameters(aComponent));
562 return anUsedParameters;
565 std::list<ResultParameterPtr> findVariables(const std::set<std::string>& theParameters)
567 std::list<ResultParameterPtr> aResult;
568 std::set<std::string>::const_iterator aParamIt = theParameters.cbegin();
569 for (; aParamIt != theParameters.cend(); ++aParamIt) {
570 const std::string& aName = *aParamIt;
572 ResultParameterPtr aParam;
573 // theSearcher is not needed here: in expressions
574 // of features the parameters history is not needed
575 if (ModelAPI_Tools::findVariable(FeaturePtr(), aName, aValue, aParam))
576 aResult.push_back(aParam);
581 void Model_Data::referencesToObjects(
582 std::list<std::pair<std::string, std::list<ObjectPtr> > >& theRefs)
584 static Model_ValidatorsFactory* aValidators =
585 static_cast<Model_ValidatorsFactory*>(ModelAPI_Session::get()->validators());
586 FeaturePtr aMyFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(myObject);
588 std::map<std::string, std::shared_ptr<ModelAPI_Attribute> >::iterator anAttr = myAttrs.begin();
589 std::list<ObjectPtr> aReferenced; // not inside of cycle to avoid excess memory management
590 for(; anAttr != myAttrs.end(); anAttr++) {
591 // skip not-case attributes, that really may refer to anything not-used (issue 671)
592 if (aMyFeature.get() && !aValidators->isCase(aMyFeature, anAttr->second->id()))
595 std::string aType = anAttr->second->attributeType();
596 if (aType == ModelAPI_AttributeReference::typeId()) { // reference to object
597 std::shared_ptr<ModelAPI_AttributeReference> aRef = std::dynamic_pointer_cast<
598 ModelAPI_AttributeReference>(anAttr->second);
599 aReferenced.push_back(aRef->value());
600 } else if (aType == ModelAPI_AttributeRefAttr::typeId()) { // reference to attribute or object
601 std::shared_ptr<ModelAPI_AttributeRefAttr> aRef = std::dynamic_pointer_cast<
602 ModelAPI_AttributeRefAttr>(anAttr->second);
603 if (aRef->isObject()) {
604 aReferenced.push_back(aRef->object());
606 AttributePtr anAttr = aRef->attr();
608 aReferenced.push_back(anAttr->owner());
610 } else if (aType == ModelAPI_AttributeRefList::typeId()) { // list of references
611 aReferenced = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(anAttr->second)->list();
612 } else if (aType == ModelAPI_AttributeSelection::typeId()) { // selection attribute
613 std::shared_ptr<ModelAPI_AttributeSelection> aRef = std::dynamic_pointer_cast<
614 ModelAPI_AttributeSelection>(anAttr->second);
615 aReferenced.push_back(aRef->context());
616 } else if (aType == ModelAPI_AttributeSelectionList::typeId()) { // list of selection attributes
617 std::shared_ptr<ModelAPI_AttributeSelectionList> aRef = std::dynamic_pointer_cast<
618 ModelAPI_AttributeSelectionList>(anAttr->second);
619 for(int a = aRef->size() - 1; a >= 0; a--) {
620 aReferenced.push_back(aRef->value(a)->context());
622 } else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
623 std::shared_ptr<ModelAPI_AttributeRefAttrList> aRefAttr = std::dynamic_pointer_cast<
624 ModelAPI_AttributeRefAttrList>(anAttr->second);
625 std::list<std::pair<ObjectPtr, AttributePtr> > aRefs = aRefAttr->list();
626 std::list<std::pair<ObjectPtr, AttributePtr> >::const_iterator anIt = aRefs.begin(),
628 for (; anIt != aLast; anIt++) {
629 aReferenced.push_back(anIt->first);
631 } else if (aType == ModelAPI_AttributeInteger::typeId()) { // integer attribute
632 AttributeIntegerPtr anAttribute =
633 std::dynamic_pointer_cast<ModelAPI_AttributeInteger>(anAttr->second);
634 std::set<std::string> anUsedParameters = anAttribute->usedParameters();
635 std::list<ResultParameterPtr> aParameters = findVariables(anUsedParameters);
636 aReferenced.insert(aReferenced.end(), aParameters.begin(), aParameters.end());
637 } else if (aType == ModelAPI_AttributeDouble::typeId()) { // double attribute
638 AttributeDoublePtr anAttribute =
639 std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(anAttr->second);
640 std::set<std::string> anUsedParameters = anAttribute->usedParameters();
641 std::list<ResultParameterPtr> aParameters = findVariables(anUsedParameters);
642 aReferenced.insert(aReferenced.end(), aParameters.begin(), aParameters.end());
643 } else if (aType == GeomDataAPI_Point::typeId()) { // point attribute
644 AttributePointPtr anAttribute =
645 std::dynamic_pointer_cast<GeomDataAPI_Point>(anAttr->second);
646 std::set<std::string> anUsedParameters = usedParameters(anAttribute);
647 std::list<ResultParameterPtr> aParameters = findVariables(anUsedParameters);
648 aReferenced.insert(aReferenced.end(), aParameters.begin(), aParameters.end());
649 } else if (aType == GeomDataAPI_Point2D::typeId()) { // point attribute
650 AttributePoint2DPtr anAttribute =
651 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttr->second);
652 std::set<std::string> anUsedParameters = usedParameters(anAttribute);
653 std::list<ResultParameterPtr> aParameters = findVariables(anUsedParameters);
654 aReferenced.insert(aReferenced.end(), aParameters.begin(), aParameters.end());
656 continue; // nothing to do, not reference
658 if (!aReferenced.empty()) {
659 theRefs.push_back(std::pair<std::string, std::list<ObjectPtr> >(anAttr->first, aReferenced));
665 /// makes copy of all attributes on the given label and all sub-labels
666 static void copyAttrs(TDF_Label theSource, TDF_Label theDestination) {
667 TDF_AttributeIterator anAttrIter(theSource);
668 for(; anAttrIter.More(); anAttrIter.Next()) {
669 Handle(TDF_Attribute) aTargetAttr;
670 if (!theDestination.FindAttribute(anAttrIter.Value()->ID(), aTargetAttr)) {
671 // create a new attribute if not yet exists in the destination
672 aTargetAttr = anAttrIter.Value()->NewEmpty();
673 theDestination.AddAttribute(aTargetAttr);
675 // no special relocation, empty map, but self-relocation is on: copy references w/o changes
676 Handle(TDF_RelocationTable) aRelocTable = new TDF_RelocationTable(Standard_True);
677 anAttrIter.Value()->Paste(aTargetAttr, aRelocTable);
679 // copy the sub-labels content
680 TDF_ChildIterator aSubLabsIter(theSource);
681 for(; aSubLabsIter.More(); aSubLabsIter.Next()) {
682 copyAttrs(aSubLabsIter.Value(), theDestination.FindChild(aSubLabsIter.Value().Tag()));
686 void Model_Data::copyTo(std::shared_ptr<ModelAPI_Data> theTarget)
688 TDF_Label aTargetRoot = std::dynamic_pointer_cast<Model_Data>(theTarget)->label();
689 copyAttrs(myLab, aTargetRoot);
690 // reinitialize Model_Attributes by TDF_Attributes set
691 std::shared_ptr<Model_Data> aTData = std::dynamic_pointer_cast<Model_Data>(theTarget);
692 aTData->myAttrs.clear();
693 theTarget->owner()->initAttributes(); // reinit feature attributes
696 bool Model_Data::isInHistory()
698 return myFlags->Value(kFlagInHistory) == Standard_True;
701 void Model_Data::setIsInHistory(const bool theFlag)
703 return myFlags->SetValue(kFlagInHistory, theFlag);
706 bool Model_Data::isDeleted()
708 return myFlags->Value(kFlagDeleted) == Standard_True;
711 void Model_Data::setIsDeleted(const bool theFlag)
713 return myFlags->SetValue(kFlagDeleted, theFlag);
716 bool Model_Data::isDisplayed()
718 if (!myObject.get() || !myObject->document().get() || // object is in valid
719 myFlags->Value(kFlagDisplayed) != Standard_True) // or it was not displayed before
721 if (myObject->document()->isActive()) // for active documents it must be ok anyway
723 // any object from the root document except part result may be displayed
724 if (myObject->document() == ModelAPI_Session::get()->moduleDocument() &&
725 myObject->groupName() != ModelAPI_ResultPart::group())
730 void Model_Data::setDisplayed(const bool theDisplay)
732 if (theDisplay != isDisplayed()) {
733 myFlags->SetValue(kFlagDisplayed, theDisplay);
734 static Events_Loop* aLoop = Events_Loop::loop();
735 static Events_ID EVENT_DISP = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
736 static const ModelAPI_EventCreator* aECreator = ModelAPI_EventCreator::get();
737 aECreator->sendUpdated(myObject, EVENT_DISP);
741 std::shared_ptr<ModelAPI_Data> Model_Data::invalidPtr()
746 std::shared_ptr<ModelAPI_Data> Model_Data::invalidData()
751 std::shared_ptr<ModelAPI_Object> Model_Data::owner()