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_Tools.h>
46 #include <Model_Validator.h>
48 #include <GeomDataAPI_Point.h>
49 #include <GeomDataAPI_Point2D.h>
51 #include <GeomData_Point.h>
52 #include <GeomData_Point2D.h>
53 #include <GeomData_Dir.h>
54 #include <Events_Loop.h>
55 #include <Events_InfoMessage.h>
57 #include <TDataStd_Name.hxx>
58 #include <TDataStd_AsciiString.hxx>
59 #include <TDataStd_IntegerArray.hxx>
60 #include <TDataStd_UAttribute.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;
78 // TDataStd_Integer - 0 if the name of the object is generated automatically,
79 // otherwise the name is defined by user
80 Standard_GUID kUSER_DEFINED_NAME("9c694d18-a83c-4a56-bc9b-8020628a8244");
83 const static std::shared_ptr<ModelAPI_Data> kInvalid(new Model_Data());
85 Model_Data::Model_Data() : mySendAttributeUpdated(true), myWasChangedButBlocked(false)
89 void Model_Data::setLabel(TDF_Label theLab)
92 // set or get the default flags
93 if (!myLab.FindAttribute(TDataStd_BooleanArray::GetID(), myFlags)) {
94 // set default values if not found
95 myFlags = TDataStd_BooleanArray::Set(myLab, 0, 2);
96 myFlags->SetValue(kFlagInHistory, Standard_True); // is in history by default is true
97 myFlags->SetValue(kFlagDisplayed, Standard_True); // is displayed by default is true
98 myFlags->SetValue(kFlagDeleted, Standard_False); // is deleted by default is false
102 std::string Model_Data::name()
104 Handle(TDataStd_Name) aName;
105 if (shapeLab().FindAttribute(TDataStd_Name::GetID(), aName)) {
107 myObject->myName = TCollection_AsciiString(aName->Get()).ToCString();
109 return std::string(TCollection_AsciiString(aName->Get()).ToCString());
111 return ""; // not defined
114 void Model_Data::setName(const std::string& theName)
116 bool isModified = false;
117 std::string anOldName = name();
118 Handle(TDataStd_Name) aName;
119 if (!shapeLab().FindAttribute(TDataStd_Name::GetID(), aName)) {
120 TDataStd_Name::Set(shapeLab(), theName.c_str());
123 isModified = !aName->Get().IsEqual(theName.c_str());
125 aName->Set(theName.c_str());
127 // check the name of result is defined by user
128 // (name of result does not composed of the name of feature and the result index)
129 bool isUserDefined = true;
130 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(myObject);
132 std::string aDefaultName = ModelAPI_Tools::getDefaultName(aResult, false).first;
133 isUserDefined = aDefaultName != theName;
136 // name is user-defined, thus special attribute is set
137 TDataStd_UAttribute::Set(shapeLab(), kUSER_DEFINED_NAME);
141 if (mySendAttributeUpdated && isModified)
142 ModelAPI_ObjectRenamedMessage::send(myObject, anOldName, theName, this);
143 if (isModified && myObject && myObject->document()) {
144 std::dynamic_pointer_cast<Model_Document>(myObject->document())->
145 changeNamingName(anOldName, theName, shapeLab());
148 myObject->myName = theName;
152 bool Model_Data::hasUserDefinedName() const
154 return shapeLab().IsAttribute(kUSER_DEFINED_NAME);
157 AttributePtr Model_Data::addAttribute(const std::string& theID, const std::string theAttrType)
159 AttributePtr aResult;
160 int anAttrIndex = int(myAttrs.size()) + 1;
161 TDF_Label anAttrLab = myLab.FindChild(anAttrIndex);
162 ModelAPI_Attribute* anAttr = 0;
163 if (theAttrType == ModelAPI_AttributeDocRef::typeId()) {
164 anAttr = new Model_AttributeDocRef(anAttrLab);
165 } else if (theAttrType == Model_AttributeInteger::typeId()) {
166 anAttr = new Model_AttributeInteger(anAttrLab);
167 } else if (theAttrType == ModelAPI_AttributeDouble::typeId()) {
168 anAttr = new Model_AttributeDouble(anAttrLab);
169 } else if (theAttrType == Model_AttributeBoolean::typeId()) {
170 anAttr = new Model_AttributeBoolean(anAttrLab);
171 } else if (theAttrType == Model_AttributeString::typeId()) {
172 anAttr = new Model_AttributeString(anAttrLab);
173 } else if (theAttrType == Model_AttributeStringArray::typeId()) {
174 anAttr = new Model_AttributeStringArray(anAttrLab);
175 } else if (theAttrType == ModelAPI_AttributeReference::typeId()) {
176 anAttr = new Model_AttributeReference(anAttrLab);
177 } else if (theAttrType == ModelAPI_AttributeSelection::typeId()) {
178 anAttr = new Model_AttributeSelection(anAttrLab);
179 } else if (theAttrType == ModelAPI_AttributeSelectionList::typeId()) {
180 anAttr = new Model_AttributeSelectionList(anAttrLab);
181 } else if (theAttrType == ModelAPI_AttributeRefAttr::typeId()) {
182 anAttr = new Model_AttributeRefAttr(anAttrLab);
183 } else if (theAttrType == ModelAPI_AttributeRefList::typeId()) {
184 anAttr = new Model_AttributeRefList(anAttrLab);
185 } else if (theAttrType == ModelAPI_AttributeRefAttrList::typeId()) {
186 anAttr = new Model_AttributeRefAttrList(anAttrLab);
187 } else if (theAttrType == ModelAPI_AttributeIntArray::typeId()) {
188 anAttr = new Model_AttributeIntArray(anAttrLab);
189 } else if (theAttrType == ModelAPI_AttributeDoubleArray::typeId()) {
190 anAttr = new Model_AttributeDoubleArray(anAttrLab);
191 } else if (theAttrType == ModelAPI_AttributeTables::typeId()) {
192 anAttr = new Model_AttributeTables(anAttrLab);
194 // create also GeomData attributes here because only here the OCAF structure is known
195 else if (theAttrType == GeomData_Point::typeId()) {
196 GeomData_Point* anAttribute = new GeomData_Point();
197 bool anAllInitialized = true;
198 for (int aComponent = 0; aComponent < GeomData_Point::NUM_COMPONENTS; ++aComponent) {
199 TDF_Label anExpressionLab = anAttrLab.FindChild(aComponent + 1);
200 anAttribute->myExpression[aComponent].reset(new Model_ExpressionDouble(anExpressionLab));
201 anAllInitialized = anAllInitialized && anAttribute->myExpression[aComponent]->isInitialized();
203 anAttribute->myIsInitialized = anAllInitialized;
204 anAttr = anAttribute;
205 } else if (theAttrType == GeomData_Dir::typeId()) {
206 anAttr = new GeomData_Dir(anAttrLab);
207 } else if (theAttrType == GeomData_Point2D::typeId()) {
208 GeomData_Point2D* anAttribute = new GeomData_Point2D();
209 bool anAllInitialized = true;
210 for (int aComponent = 0; aComponent < GeomData_Point2D::NUM_COMPONENTS; ++aComponent) {
211 TDF_Label anExpressionLab = anAttrLab.FindChild(aComponent + 1);
212 anAttribute->myExpression[aComponent].reset(new Model_ExpressionDouble(anExpressionLab));
213 anAllInitialized = anAllInitialized && anAttribute->myExpression[aComponent]->isInitialized();
215 anAttribute->myIsInitialized = anAllInitialized;
216 anAttr = anAttribute;
219 aResult = std::shared_ptr<ModelAPI_Attribute>(anAttr);
220 myAttrs[theID] = std::pair<AttributePtr, int>(aResult, anAttrIndex);
221 anAttr->setObject(myObject);
222 anAttr->setID(theID);
224 Events_InfoMessage("Model_Data",
225 "Can not create unknown type of attribute %1").arg(theAttrType).send();
230 // macro for the generic returning of the attribute by the ID
231 #define GET_ATTRIBUTE_BY_ID(ATTR_TYPE, METHOD_NAME) \
232 std::shared_ptr<ATTR_TYPE> Model_Data::METHOD_NAME(const std::string& theID) { \
233 std::shared_ptr<ATTR_TYPE> aRes; \
234 AttributeMap::iterator aFound = myAttrs.find(theID); \
235 if (aFound != myAttrs.end()) { \
236 aRes = std::dynamic_pointer_cast<ATTR_TYPE>(aFound->second.first); \
240 // implement nice getting methods for all ModelAPI attributes
241 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeDocRef, document);
242 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeDouble, real);
243 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeInteger, integer);
244 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeBoolean, boolean);
245 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeString, string);
246 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeStringArray, stringArray);
247 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeReference, reference);
248 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeSelection, selection);
249 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeSelectionList, selectionList);
250 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeRefAttr, refattr);
251 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeRefList, reflist);
252 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeRefAttrList, refattrlist);
253 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeIntArray, intArray);
254 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeDoubleArray, realArray);
255 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeTables, tables);
257 std::shared_ptr<ModelAPI_Attribute> Model_Data::attribute(const std::string& theID)
259 std::shared_ptr<ModelAPI_Attribute> aResult;
260 if (myAttrs.find(theID) == myAttrs.end()) // no such attribute
262 return myAttrs[theID].first;
265 const std::string& Model_Data::id(const std::shared_ptr<ModelAPI_Attribute>& theAttr)
267 AttributeMap::iterator anAttr = myAttrs.begin();
268 for (; anAttr != myAttrs.end(); anAttr++) {
269 if (anAttr->second.first == theAttr)
270 return anAttr->first;
273 static std::string anEmpty;
277 bool Model_Data::isEqual(const std::shared_ptr<ModelAPI_Data>& theData)
279 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(theData);
281 return myLab.IsEqual(aData->myLab) == Standard_True ;
285 bool Model_Data::isValid()
287 return !myLab.IsNull() && myLab.HasAttribute();
290 std::list<std::shared_ptr<ModelAPI_Attribute> > Model_Data::attributes(const std::string& theType)
292 std::list<std::shared_ptr<ModelAPI_Attribute> > aResult;
293 AttributeMap::iterator anAttrsIter = myAttrs.begin();
294 for (; anAttrsIter != myAttrs.end(); anAttrsIter++) {
295 AttributePtr anAttr = anAttrsIter->second.first;
296 if (theType.empty() || anAttr->attributeType() == theType) {
297 aResult.push_back(anAttr);
303 std::list<std::string> Model_Data::attributesIDs(const std::string& theType)
305 std::list<std::string> aResult;
306 AttributeMap::iterator anAttrsIter = myAttrs.begin();
307 for (; anAttrsIter != myAttrs.end(); anAttrsIter++) {
308 AttributePtr anAttr = anAttrsIter->second.first;
309 if (theType.empty() || anAttr->attributeType() == theType) {
310 aResult.push_back(anAttrsIter->first);
316 void Model_Data::sendAttributeUpdated(ModelAPI_Attribute* theAttr)
318 theAttr->setInitialized();
319 if (theAttr->isArgument()) {
320 if (mySendAttributeUpdated) {
323 myObject->attributeChanged(theAttr->id());
325 if (owner().get() && owner()->data().get() && owner()->data()->isValid()) {
326 Events_InfoMessage("Model_Data",
327 "%1 has failed during the update").arg(owner()->data()->name()).send();
330 static const Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
331 ModelAPI_EventCreator::get()->sendUpdated(myObject, anEvent);
334 // to avoid too many duplications do not add the same like the last
335 if (myWasChangedButBlocked.empty() || *(myWasChangedButBlocked.rbegin()) != theAttr)
336 myWasChangedButBlocked.push_back(theAttr);
339 // trim: need to redisplay
340 if (myObject && theAttr->attributeType() == "Point2D") {
341 static const Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY);
342 ModelAPI_EventCreator::get()->sendUpdated(myObject, anEvent);
347 bool Model_Data::blockSendAttributeUpdated(const bool theBlock, const bool theSendMessage)
349 bool aWasBlocked = !mySendAttributeUpdated;
350 if (mySendAttributeUpdated == theBlock) {
351 mySendAttributeUpdated = !theBlock;
352 if (mySendAttributeUpdated && !myWasChangedButBlocked.empty()) {
353 // so, now it is ok to send the update signal
354 if (theSendMessage) {
355 // make a copy to avoid iteration on modified list
356 // (may be cleared by attribute changed call)
357 std::list<ModelAPI_Attribute*> aWasChangedButBlocked = myWasChangedButBlocked;
358 myWasChangedButBlocked.clear();
359 std::list<ModelAPI_Attribute*>::iterator aChangedIter = aWasChangedButBlocked.begin();
360 for(; aChangedIter != aWasChangedButBlocked.end(); aChangedIter++) {
362 myObject->attributeChanged((*aChangedIter)->id());
364 if (owner().get() && owner()->data().get() && owner()->data()->isValid()) {
365 Events_InfoMessage("Model_Data",
366 "%1 has failed during the update").arg(owner()->data()->name()).send();
370 static const Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
371 ModelAPI_EventCreator::get()->sendUpdated(myObject, anEvent);
373 myWasChangedButBlocked.clear();
380 void Model_Data::erase()
382 if (!myLab.IsNull()) {
383 if (myLab.HasAttribute()) {
384 // remove in order to clear back references in other objects
385 std::list<std::pair<std::string, std::list<ObjectPtr> > > aRefs;
386 referencesToObjects(aRefs);
387 std::list<std::pair<std::string, std::list<ObjectPtr> > >::iterator
388 anAttrIter = aRefs.begin();
389 for(; anAttrIter != aRefs.end(); anAttrIter++) {
390 std::list<ObjectPtr>::iterator aReferenced = anAttrIter->second.begin();
391 for(; aReferenced != anAttrIter->second.end(); aReferenced++) {
392 if (aReferenced->get() && (*aReferenced)->data()->isValid()) {
393 std::shared_ptr<Model_Data> aData =
394 std::dynamic_pointer_cast<Model_Data>((*aReferenced)->data());
395 aData->removeBackReference(myAttrs[anAttrIter->first].first);
401 myLab.ForgetAllAttributes();
405 // indexes in the state array
407 STATE_INDEX_STATE = 1, // the state type itself
408 STATE_INDEX_TRANSACTION = 2, // transaction ID
411 /// Returns the label array, initializes it by default values if not exists
412 static Handle(TDataStd_IntegerArray) stateArray(TDF_Label& theLab)
414 Handle(TDataStd_IntegerArray) aStateArray;
415 if (!theLab.FindAttribute(TDataStd_IntegerArray::GetID(), aStateArray)) {
416 aStateArray = TDataStd_IntegerArray::Set(theLab, 1, 2);
417 aStateArray->SetValue(STATE_INDEX_STATE, ModelAPI_StateMustBeUpdated); // default state
418 aStateArray->SetValue(STATE_INDEX_TRANSACTION, 0); // default transaction ID (not existing)
423 void Model_Data::execState(const ModelAPI_ExecState theState)
425 if (theState != ModelAPI_StateNothing) {
426 if (stateArray(myLab)->Value(STATE_INDEX_STATE) != (int)theState) {
427 stateArray(myLab)->SetValue(STATE_INDEX_STATE, (int)theState);
432 ModelAPI_ExecState Model_Data::execState()
434 return ModelAPI_ExecState(stateArray(myLab)->Value(STATE_INDEX_STATE));
437 int Model_Data::updateID()
439 return stateArray(myLab)->Value(STATE_INDEX_TRANSACTION);
442 void Model_Data::setUpdateID(const int theID)
444 stateArray(myLab)->SetValue(STATE_INDEX_TRANSACTION, theID);
447 void Model_Data::setError(const std::string& theError, bool theSend)
449 execState(ModelAPI_StateExecFailed);
451 Events_InfoMessage("Model_Data", theError).send();
453 TDataStd_AsciiString::Set(myLab, theError.c_str());
456 void Model_Data::eraseErrorString()
458 myLab.ForgetAttribute(TDataStd_AsciiString::GetID());
461 std::string Model_Data::error() const
463 Handle(TDataStd_AsciiString) anErrorAttr;
464 if (myLab.FindAttribute(TDataStd_AsciiString::GetID(), anErrorAttr)) {
465 return std::string(anErrorAttr->Get().ToCString());
467 return std::string();
470 int Model_Data::featureId() const
472 return myLab.Father().Tag(); // tag of the feature label
475 void Model_Data::removeBackReference(ObjectPtr theObject, std::string theAttrID)
477 AttributePtr anAttribute = theObject->data()->attribute(theAttrID);
478 removeBackReference(anAttribute);
481 void Model_Data::removeBackReference(AttributePtr theAttr)
483 if (myRefsToMe.find(theAttr) == myRefsToMe.end())
486 myRefsToMe.erase(theAttr);
488 // remove concealment immediately: on deselection it must be possible to reselect in GUI the same
489 FeaturePtr aFeatureOwner = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttr->owner());
490 if (aFeatureOwner.get() &&
491 ModelAPI_Session::get()->validators()->isConcealed(aFeatureOwner->getKind(), theAttr->id())) {
492 updateConcealmentFlag();
496 void Model_Data::addBackReference(FeaturePtr theFeature, std::string theAttrID,
497 const bool theApplyConcealment)
499 addBackReference(ObjectPtr(theFeature), theAttrID);
501 if (theApplyConcealment && theFeature->isStable() &&
502 ModelAPI_Session::get()->validators()->isConcealed(theFeature->getKind(), theAttrID)) {
503 std::shared_ptr<ModelAPI_Result> aRes = std::dynamic_pointer_cast<ModelAPI_Result>(myObject);
504 // the second condition is for history upper than concealment causer, so the feature result may
505 // be displayed and previewed; also for avoiding of quick show/hide on history
507 if (aRes && !theFeature->isDisabled()) {
508 aRes->setIsConcealed(true);
513 void Model_Data::addBackReference(ObjectPtr theObject, std::string theAttrID)
515 // it is possible to add the same attribute twice: may be last time the owner was not Stable...
516 AttributePtr anAttribute = theObject->data()->attribute(theAttrID);
517 if (myRefsToMe.find(anAttribute) == myRefsToMe.end())
518 myRefsToMe.insert(anAttribute);
521 void Model_Data::updateConcealmentFlag()
523 std::set<AttributePtr>::iterator aRefsIter = myRefsToMe.begin();
524 for(; aRefsIter != myRefsToMe.end(); aRefsIter++) {
525 if (aRefsIter->get()) {
526 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>((*aRefsIter)->owner());
527 if (aFeature.get() && !aFeature->isDisabled() && aFeature->isStable()) {
528 if (ModelAPI_Session::get()->validators()->isConcealed(
529 aFeature->getKind(), (*aRefsIter)->id())) {
530 std::shared_ptr<ModelAPI_Result> aRes =
531 std::dynamic_pointer_cast<ModelAPI_Result>(myObject);
533 aRes->setIsConcealed(true); // set concealed
540 std::shared_ptr<ModelAPI_Result> aRes =
541 std::dynamic_pointer_cast<ModelAPI_Result>(myObject);
543 aRes->setIsConcealed(false);
547 std::set<std::string> set_union(const std::set<std::string>& theLeft,
548 const std::set<std::string>& theRight)
550 std::set<std::string> aResult;
551 aResult.insert(theLeft.begin(), theLeft.end());
552 aResult.insert(theRight.begin(), theRight.end());
556 std::set<std::string> usedParameters(const AttributePointPtr& theAttribute)
558 std::set<std::string> anUsedParameters;
559 for (int aComponent = 0; aComponent < 3; ++aComponent)
560 anUsedParameters = set_union(anUsedParameters, theAttribute->usedParameters(aComponent));
561 return anUsedParameters;
564 std::set<std::string> usedParameters(const AttributePoint2DPtr& theAttribute)
566 std::set<std::string> anUsedParameters;
567 for (int aComponent = 0; aComponent < 2; ++aComponent)
568 anUsedParameters = set_union(anUsedParameters, theAttribute->usedParameters(aComponent));
569 return anUsedParameters;
572 std::list<ResultParameterPtr> findVariables(const std::set<std::string>& theParameters,
573 const DocumentPtr& theDocument)
575 std::list<ResultParameterPtr> aResult;
576 std::set<std::string>::const_iterator aParamIt = theParameters.cbegin();
577 for (; aParamIt != theParameters.cend(); ++aParamIt) {
578 const std::string& aName = *aParamIt;
580 ResultParameterPtr aParam;
581 // theSearcher is not needed here: in expressions
582 // of features the parameters history is not needed
583 if (ModelAPI_Tools::findVariable(FeaturePtr(), aName, aValue, aParam, theDocument))
584 aResult.push_back(aParam);
589 void Model_Data::referencesToObjects(
590 std::list<std::pair<std::string, std::list<ObjectPtr> > >& theRefs)
592 static Model_ValidatorsFactory* aValidators =
593 static_cast<Model_ValidatorsFactory*>(ModelAPI_Session::get()->validators());
594 FeaturePtr aMyFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(myObject);
596 AttributeMap::iterator anAttrIt = myAttrs.begin();
597 std::list<ObjectPtr> aReferenced; // not inside of cycle to avoid excess memory management
598 for(; anAttrIt != myAttrs.end(); anAttrIt++) {
599 AttributePtr anAttr = anAttrIt->second.first;
600 // skip not-case attributes, that really may refer to anything not-used (issue 671)
601 if (aMyFeature.get() && !aValidators->isCase(aMyFeature, anAttr->id()))
604 std::string aType = anAttr->attributeType();
605 if (aType == ModelAPI_AttributeReference::typeId()) { // reference to object
606 std::shared_ptr<ModelAPI_AttributeReference> aRef = std::dynamic_pointer_cast<
607 ModelAPI_AttributeReference>(anAttr);
608 aReferenced.push_back(aRef->value());
609 } else if (aType == ModelAPI_AttributeRefAttr::typeId()) { // reference to attribute or object
610 std::shared_ptr<ModelAPI_AttributeRefAttr> aRef = std::dynamic_pointer_cast<
611 ModelAPI_AttributeRefAttr>(anAttr);
612 if (aRef->isObject()) {
613 aReferenced.push_back(aRef->object());
615 AttributePtr anAttr = aRef->attr();
617 aReferenced.push_back(anAttr->owner());
619 } else if (aType == ModelAPI_AttributeRefList::typeId()) { // list of references
620 aReferenced = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(anAttr)->list();
622 else if (aType == ModelAPI_AttributeSelection::typeId()) { // selection attribute
623 std::shared_ptr<ModelAPI_AttributeSelection> aRef = std::dynamic_pointer_cast<
624 ModelAPI_AttributeSelection>(anAttr);
625 FeaturePtr aRefFeat = aRef->contextFeature();
626 if (aRefFeat.get()) { // reference to all results of the referenced feature
627 const std::list<ResultPtr>& allRes = aRefFeat->results();
628 std::list<ResultPtr>::const_iterator aRefRes = allRes.cbegin();
629 for(; aRefRes != allRes.cend(); aRefRes++) {
630 aReferenced.push_back(*aRefRes);
633 aReferenced.push_back(aRef->context());
635 } else if (aType == ModelAPI_AttributeSelectionList::typeId()) { // list of selection attributes
636 std::shared_ptr<ModelAPI_AttributeSelectionList> aRef = std::dynamic_pointer_cast<
637 ModelAPI_AttributeSelectionList>(anAttr);
638 for(int a = 0, aSize = aRef->size(); a < aSize; ++a) {
639 FeaturePtr aRefFeat = aRef->value(a)->contextFeature();
640 if (aRefFeat.get()) { // reference to all results of the referenced feature
641 const std::list<ResultPtr>& allRes = aRefFeat->results();
642 std::list<ResultPtr>::const_iterator aRefRes = allRes.cbegin();
643 for (; aRefRes != allRes.cend(); aRefRes++) {
644 aReferenced.push_back(*aRefRes);
647 aReferenced.push_back(aRef->value(a)->context());
650 } else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
651 std::shared_ptr<ModelAPI_AttributeRefAttrList> aRefAttr = std::dynamic_pointer_cast<
652 ModelAPI_AttributeRefAttrList>(anAttr);
653 std::list<std::pair<ObjectPtr, AttributePtr> > aRefs = aRefAttr->list();
654 std::list<std::pair<ObjectPtr, AttributePtr> >::const_iterator anIt = aRefs.begin(),
656 for (; anIt != aLast; anIt++) {
657 aReferenced.push_back(anIt->first);
659 } else if (aType == ModelAPI_AttributeInteger::typeId()) { // integer attribute
660 AttributeIntegerPtr anAttribute =
661 std::dynamic_pointer_cast<ModelAPI_AttributeInteger>(anAttr);
662 std::set<std::string> anUsedParameters = anAttribute->usedParameters();
663 std::list<ResultParameterPtr> aParameters =
664 findVariables(anUsedParameters, owner()->document());
665 aReferenced.insert(aReferenced.end(), aParameters.begin(), aParameters.end());
666 } else if (aType == ModelAPI_AttributeDouble::typeId()) { // double attribute
667 AttributeDoublePtr anAttribute =
668 std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(anAttr);
669 std::set<std::string> anUsedParameters = anAttribute->usedParameters();
670 std::list<ResultParameterPtr> aParameters =
671 findVariables(anUsedParameters, owner()->document());
672 aReferenced.insert(aReferenced.end(), aParameters.begin(), aParameters.end());
673 } else if (aType == GeomDataAPI_Point::typeId()) { // point attribute
674 AttributePointPtr anAttribute =
675 std::dynamic_pointer_cast<GeomDataAPI_Point>(anAttr);
676 std::set<std::string> anUsedParameters = usedParameters(anAttribute);
677 std::list<ResultParameterPtr> aParameters =
678 findVariables(anUsedParameters, owner()->document());
679 aReferenced.insert(aReferenced.end(), aParameters.begin(), aParameters.end());
680 } else if (aType == GeomDataAPI_Point2D::typeId()) { // point attribute
681 AttributePoint2DPtr anAttribute =
682 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttr);
683 std::set<std::string> anUsedParameters = usedParameters(anAttribute);
684 std::list<ResultParameterPtr> aParameters =
685 findVariables(anUsedParameters, owner()->document());
686 aReferenced.insert(aReferenced.end(), aParameters.begin(), aParameters.end());
688 continue; // nothing to do, not reference
690 if (!aReferenced.empty()) {
692 std::pair<std::string, std::list<ObjectPtr> >(anAttrIt->first, aReferenced));
698 /// makes copy of all attributes on the given label and all sub-labels
699 static void copyAttrs(TDF_Label theSource, TDF_Label theDestination) {
700 TDF_AttributeIterator anAttrIter(theSource);
701 for(; anAttrIter.More(); anAttrIter.Next()) {
702 Handle(TDF_Attribute) aTargetAttr;
703 if (!theDestination.FindAttribute(anAttrIter.Value()->ID(), aTargetAttr)) {
704 // create a new attribute if not yet exists in the destination
705 aTargetAttr = anAttrIter.Value()->NewEmpty();
706 theDestination.AddAttribute(aTargetAttr);
708 // no special relocation, empty map, but self-relocation is on: copy references w/o changes
709 Handle(TDF_RelocationTable) aRelocTable = new TDF_RelocationTable(Standard_True);
710 anAttrIter.Value()->Paste(aTargetAttr, aRelocTable);
712 // copy the sub-labels content
713 TDF_ChildIterator aSubLabsIter(theSource);
714 for(; aSubLabsIter.More(); aSubLabsIter.Next()) {
715 copyAttrs(aSubLabsIter.Value(), theDestination.FindChild(aSubLabsIter.Value().Tag()));
719 void Model_Data::copyTo(std::shared_ptr<ModelAPI_Data> theTarget)
721 TDF_Label aTargetRoot = std::dynamic_pointer_cast<Model_Data>(theTarget)->label();
722 copyAttrs(myLab, aTargetRoot);
723 // reinitialize Model_Attributes by TDF_Attributes set
724 std::shared_ptr<Model_Data> aTData = std::dynamic_pointer_cast<Model_Data>(theTarget);
725 aTData->myAttrs.clear();
726 theTarget->owner()->initAttributes(); // reinitialize feature attributes
729 bool Model_Data::isInHistory()
731 return myFlags->Value(kFlagInHistory) == Standard_True;
734 void Model_Data::setIsInHistory(const bool theFlag)
736 return myFlags->SetValue(kFlagInHistory, theFlag);
739 bool Model_Data::isDeleted()
741 return myFlags->Value(kFlagDeleted) == Standard_True;
744 void Model_Data::setIsDeleted(const bool theFlag)
746 return myFlags->SetValue(kFlagDeleted, theFlag);
749 bool Model_Data::isDisplayed()
751 if (!myObject.get() || !myObject->document().get() || // object is in valid
752 myFlags->Value(kFlagDisplayed) != Standard_True) // or it was not displayed before
754 if (myObject->document()->isActive()) // for active documents it must be ok anyway
756 // any object from the root document except part result may be displayed
757 if (myObject->document() == ModelAPI_Session::get()->moduleDocument() &&
758 myObject->groupName() != ModelAPI_ResultPart::group())
763 void Model_Data::setDisplayed(const bool theDisplay)
765 if (theDisplay != isDisplayed()) {
766 myFlags->SetValue(kFlagDisplayed, theDisplay);
767 static Events_Loop* aLoop = Events_Loop::loop();
768 static Events_ID EVENT_DISP = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
769 static const ModelAPI_EventCreator* aECreator = ModelAPI_EventCreator::get();
770 aECreator->sendUpdated(myObject, EVENT_DISP);
774 std::shared_ptr<ModelAPI_Data> Model_Data::invalidPtr()
779 std::shared_ptr<ModelAPI_Data> Model_Data::invalidData()
784 std::shared_ptr<ModelAPI_Object> Model_Data::owner()
789 bool Model_Data::isPrecedingAttribute(const std::string& theAttribute1,
790 const std::string& theAttribute2) const
792 AttributeMap::const_iterator aFound1 = myAttrs.find(theAttribute1);
793 AttributeMap::const_iterator aFound2 = myAttrs.find(theAttribute2);
794 if (aFound2 == myAttrs.end())
796 else if (aFound1 == myAttrs.end())
798 return aFound1->second.second < aFound2->second.second;