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 <TDataStd_UAttribute.hxx>
62 #include <TDF_AttributeIterator.hxx>
63 #include <TDF_ChildIterator.hxx>
64 #include <TDF_RelocationTable.hxx>
65 #include <TColStd_HArray1OfByte.hxx>
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");
84 const static std::shared_ptr<ModelAPI_Data> kInvalid(new Model_Data());
86 Model_Data::Model_Data() : mySendAttributeUpdated(true), myWasChangedButBlocked(false)
90 void Model_Data::setLabel(TDF_Label 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);
112 std::string Model_Data::name()
114 Handle(TDataStd_Name) aName;
115 if (myLab.FindAttribute(TDataStd_Name::GetID(), aName)) {
117 myObject->myName = TCollection_AsciiString(aName->Get()).ToCString();
119 return std::string(TCollection_AsciiString(aName->Get()).ToCString());
121 return ""; // not defined
124 void Model_Data::setName(const std::string& theName)
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());
133 isModified = !aName->Get().IsEqual(theName.c_str());
135 aName->Set(theName.c_str());
136 // name is changed, thus special attribute is set
137 TDataStd_UAttribute::Set(myLab, kUSER_DEFINED_NAME);
140 if (mySendAttributeUpdated && isModified)
141 ModelAPI_ObjectRenamedMessage::send(myObject, anOldName, theName, this);
142 if (isModified && myObject && myObject->document()) {
143 std::dynamic_pointer_cast<Model_Document>(myObject->document())->
144 changeNamingName(anOldName, theName, myLab);
147 myObject->myName = theName;
151 bool Model_Data::hasUserDefinedName() const
153 return myLab.IsAttribute(kUSER_DEFINED_NAME);
156 AttributePtr Model_Data::addAttribute(const std::string& theID, const std::string theAttrType)
158 AttributePtr aResult;
159 int anAttrIndex = int(myAttrs.size()) + 1;
160 TDF_Label anAttrLab = myLab.FindChild(anAttrIndex);
161 ModelAPI_Attribute* anAttr = 0;
162 if (theAttrType == ModelAPI_AttributeDocRef::typeId()) {
163 anAttr = new Model_AttributeDocRef(anAttrLab);
164 } else if (theAttrType == Model_AttributeInteger::typeId()) {
165 anAttr = new Model_AttributeInteger(anAttrLab);
166 } else if (theAttrType == ModelAPI_AttributeDouble::typeId()) {
167 anAttr = new Model_AttributeDouble(anAttrLab);
168 } else if (theAttrType == Model_AttributeBoolean::typeId()) {
169 anAttr = new Model_AttributeBoolean(anAttrLab);
170 } else if (theAttrType == Model_AttributeString::typeId()) {
171 anAttr = new Model_AttributeString(anAttrLab);
172 } else if (theAttrType == Model_AttributeStringArray::typeId()) {
173 anAttr = new Model_AttributeStringArray(anAttrLab);
174 } else if (theAttrType == ModelAPI_AttributeReference::typeId()) {
175 anAttr = new Model_AttributeReference(anAttrLab);
176 } else if (theAttrType == ModelAPI_AttributeSelection::typeId()) {
177 anAttr = new Model_AttributeSelection(anAttrLab);
178 } else if (theAttrType == ModelAPI_AttributeSelectionList::typeId()) {
179 anAttr = new Model_AttributeSelectionList(anAttrLab);
180 } else if (theAttrType == ModelAPI_AttributeRefAttr::typeId()) {
181 anAttr = new Model_AttributeRefAttr(anAttrLab);
182 } else if (theAttrType == ModelAPI_AttributeRefList::typeId()) {
183 anAttr = new Model_AttributeRefList(anAttrLab);
184 } else if (theAttrType == ModelAPI_AttributeRefAttrList::typeId()) {
185 anAttr = new Model_AttributeRefAttrList(anAttrLab);
186 } else if (theAttrType == ModelAPI_AttributeIntArray::typeId()) {
187 anAttr = new Model_AttributeIntArray(anAttrLab);
188 } else if (theAttrType == ModelAPI_AttributeDoubleArray::typeId()) {
189 anAttr = new Model_AttributeDoubleArray(anAttrLab);
190 } else if (theAttrType == ModelAPI_AttributeTables::typeId()) {
191 anAttr = new Model_AttributeTables(anAttrLab);
193 // create also GeomData attributes here because only here the OCAF structure is known
194 else if (theAttrType == GeomData_Point::typeId()) {
195 GeomData_Point* anAttribute = new GeomData_Point();
196 bool anAllInitialized = true;
197 for (int aComponent = 0; aComponent < GeomData_Point::NUM_COMPONENTS; ++aComponent) {
198 TDF_Label anExpressionLab = anAttrLab.FindChild(aComponent + 1);
199 anAttribute->myExpression[aComponent].reset(new Model_ExpressionDouble(anExpressionLab));
200 anAllInitialized = anAllInitialized && anAttribute->myExpression[aComponent]->isInitialized();
202 anAttribute->myIsInitialized = anAllInitialized;
203 anAttr = anAttribute;
204 } else if (theAttrType == GeomData_Dir::typeId()) {
205 anAttr = new GeomData_Dir(anAttrLab);
206 } else if (theAttrType == GeomData_Point2D::typeId()) {
207 GeomData_Point2D* anAttribute = new GeomData_Point2D();
208 bool anAllInitialized = true;
209 for (int aComponent = 0; aComponent < GeomData_Point2D::NUM_COMPONENTS; ++aComponent) {
210 TDF_Label anExpressionLab = anAttrLab.FindChild(aComponent + 1);
211 anAttribute->myExpression[aComponent].reset(new Model_ExpressionDouble(anExpressionLab));
212 anAllInitialized = anAllInitialized && anAttribute->myExpression[aComponent]->isInitialized();
214 anAttribute->myIsInitialized = anAllInitialized;
215 anAttr = anAttribute;
218 aResult = std::shared_ptr<ModelAPI_Attribute>(anAttr);
219 myAttrs[theID] = std::pair<AttributePtr, int>(aResult, anAttrIndex);
220 anAttr->setObject(myObject);
221 anAttr->setID(theID);
223 Events_InfoMessage("Model_Data",
224 "Can not create unknown type of attribute %1").arg(theAttrType).send();
229 // macro for the generic returning of the attribute by the ID
230 #define GET_ATTRIBUTE_BY_ID(ATTR_TYPE, METHOD_NAME) \
231 std::shared_ptr<ATTR_TYPE> Model_Data::METHOD_NAME(const std::string& theID) { \
232 std::shared_ptr<ATTR_TYPE> aRes; \
233 AttributeMap::iterator aFound = myAttrs.find(theID); \
234 if (aFound != myAttrs.end()) { \
235 aRes = std::dynamic_pointer_cast<ATTR_TYPE>(aFound->second.first); \
239 // implement nice getting methods for all ModelAPI attributes
240 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeDocRef, document);
241 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeDouble, real);
242 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeInteger, integer);
243 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeBoolean, boolean);
244 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeString, string);
245 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeStringArray, stringArray);
246 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeReference, reference);
247 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeSelection, selection);
248 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeSelectionList, selectionList);
249 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeRefAttr, refattr);
250 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeRefList, reflist);
251 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeRefAttrList, refattrlist);
252 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeIntArray, intArray);
253 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeDoubleArray, realArray);
254 GET_ATTRIBUTE_BY_ID(ModelAPI_AttributeTables, tables);
256 std::shared_ptr<ModelAPI_Attribute> Model_Data::attribute(const std::string& theID)
258 std::shared_ptr<ModelAPI_Attribute> aResult;
259 if (myAttrs.find(theID) == myAttrs.end()) // no such attribute
261 return myAttrs[theID].first;
264 const std::string& Model_Data::id(const std::shared_ptr<ModelAPI_Attribute>& theAttr)
266 AttributeMap::iterator anAttr = myAttrs.begin();
267 for (; anAttr != myAttrs.end(); anAttr++) {
268 if (anAttr->second.first == theAttr)
269 return anAttr->first;
272 static std::string anEmpty;
276 bool Model_Data::isEqual(const std::shared_ptr<ModelAPI_Data>& theData)
278 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(theData);
280 return myLab.IsEqual(aData->myLab) == Standard_True ;
284 bool Model_Data::isValid()
286 return !myLab.IsNull() && myLab.HasAttribute();
289 std::list<std::shared_ptr<ModelAPI_Attribute> > Model_Data::attributes(const std::string& theType)
291 std::list<std::shared_ptr<ModelAPI_Attribute> > aResult;
292 AttributeMap::iterator anAttrsIter = myAttrs.begin();
293 for (; anAttrsIter != myAttrs.end(); anAttrsIter++) {
294 AttributePtr anAttr = anAttrsIter->second.first;
295 if (theType.empty() || anAttr->attributeType() == theType) {
296 aResult.push_back(anAttr);
302 std::list<std::string> Model_Data::attributesIDs(const std::string& theType)
304 std::list<std::string> aResult;
305 AttributeMap::iterator anAttrsIter = myAttrs.begin();
306 for (; anAttrsIter != myAttrs.end(); anAttrsIter++) {
307 AttributePtr anAttr = anAttrsIter->second.first;
308 if (theType.empty() || anAttr->attributeType() == theType) {
309 aResult.push_back(anAttrsIter->first);
315 void Model_Data::sendAttributeUpdated(ModelAPI_Attribute* theAttr)
317 theAttr->setInitialized();
318 if (theAttr->isArgument()) {
319 if (mySendAttributeUpdated) {
322 myObject->attributeChanged(theAttr->id());
324 if (owner().get() && owner()->data().get() && owner()->data()->isValid()) {
325 Events_InfoMessage("Model_Data",
326 "%1 has failed during the update").arg(owner()->data()->name()).send();
329 static const Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
330 ModelAPI_EventCreator::get()->sendUpdated(myObject, anEvent);
333 // to avoid too many duplications do not add the same like the last
334 if (myWasChangedButBlocked.empty() || *(myWasChangedButBlocked.rbegin()) != theAttr)
335 myWasChangedButBlocked.push_back(theAttr);
338 // trim: need to redisplay
340 static const Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY);
341 ModelAPI_EventCreator::get()->sendUpdated(myObject, anEvent);
346 bool Model_Data::blockSendAttributeUpdated(const bool theBlock, const bool theSendMessage)
348 bool aWasBlocked = !mySendAttributeUpdated;
349 if (mySendAttributeUpdated == theBlock) {
350 mySendAttributeUpdated = !theBlock;
351 if (mySendAttributeUpdated && !myWasChangedButBlocked.empty()) {
352 // so, now it is ok to send the update signal
353 if (theSendMessage) {
354 // make a copy to avoid iteration on modified list
355 // (may be cleared by attribute changed call)
356 std::list<ModelAPI_Attribute*> aWasChangedButBlocked = myWasChangedButBlocked;
357 myWasChangedButBlocked.clear();
358 std::list<ModelAPI_Attribute*>::iterator aChangedIter = aWasChangedButBlocked.begin();
359 for(; aChangedIter != aWasChangedButBlocked.end(); aChangedIter++) {
361 myObject->attributeChanged((*aChangedIter)->id());
363 if (owner().get() && owner()->data().get() && owner()->data()->isValid()) {
364 Events_InfoMessage("Model_Data",
365 "%1 has failed during the update").arg(owner()->data()->name()).send();
369 static const Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
370 ModelAPI_EventCreator::get()->sendUpdated(myObject, anEvent);
372 myWasChangedButBlocked.clear();
379 void Model_Data::erase()
381 if (!myLab.IsNull()) {
382 if (myLab.HasAttribute()) {
383 // remove in order to clear back references in other objects
384 std::list<std::pair<std::string, std::list<ObjectPtr> > > aRefs;
385 referencesToObjects(aRefs);
386 std::list<std::pair<std::string, std::list<ObjectPtr> > >::iterator
387 anAttrIter = aRefs.begin();
388 for(; anAttrIter != aRefs.end(); anAttrIter++) {
389 std::list<ObjectPtr>::iterator aReferenced = anAttrIter->second.begin();
390 for(; aReferenced != anAttrIter->second.end(); aReferenced++) {
391 if (aReferenced->get() && (*aReferenced)->data()->isValid()) {
392 std::shared_ptr<Model_Data> aData =
393 std::dynamic_pointer_cast<Model_Data>((*aReferenced)->data());
394 aData->removeBackReference(myAttrs[anAttrIter->first].first);
400 myLab.ForgetAllAttributes();
404 // indexes in the state array
406 STATE_INDEX_STATE = 1, // the state type itself
407 STATE_INDEX_TRANSACTION = 2, // transaction ID
410 /// Returns the label array, initialises it by default values if not exists
411 static Handle(TDataStd_IntegerArray) stateArray(TDF_Label& theLab)
413 Handle(TDataStd_IntegerArray) aStateArray;
414 if (!theLab.FindAttribute(TDataStd_IntegerArray::GetID(), aStateArray)) {
415 aStateArray = TDataStd_IntegerArray::Set(theLab, 1, 2);
416 aStateArray->SetValue(STATE_INDEX_STATE, ModelAPI_StateMustBeUpdated); // default state
417 aStateArray->SetValue(STATE_INDEX_TRANSACTION, 0); // default transaction ID (not existing)
422 void Model_Data::execState(const ModelAPI_ExecState theState)
424 if (theState != ModelAPI_StateNothing) {
425 if (stateArray(myLab)->Value(STATE_INDEX_STATE) != (int)theState) {
426 stateArray(myLab)->SetValue(STATE_INDEX_STATE, (int)theState);
431 ModelAPI_ExecState Model_Data::execState()
433 return ModelAPI_ExecState(stateArray(myLab)->Value(STATE_INDEX_STATE));
436 int Model_Data::updateID()
438 return stateArray(myLab)->Value(STATE_INDEX_TRANSACTION);
441 void Model_Data::setUpdateID(const int theID)
443 stateArray(myLab)->SetValue(STATE_INDEX_TRANSACTION, theID);
446 void Model_Data::setError(const std::string& theError, bool theSend)
448 execState(ModelAPI_StateExecFailed);
450 Events_InfoMessage("Model_Data", theError).send();
452 TDataStd_AsciiString::Set(myLab, theError.c_str());
455 void Model_Data::eraseErrorString()
457 myLab.ForgetAttribute(TDataStd_AsciiString::GetID());
460 std::string Model_Data::error() const
462 Handle(TDataStd_AsciiString) anErrorAttr;
463 if (myLab.FindAttribute(TDataStd_AsciiString::GetID(), anErrorAttr)) {
464 return std::string(anErrorAttr->Get().ToCString());
466 return std::string();
469 int Model_Data::featureId() const
471 return myLab.Father().Tag(); // tag of the feature label
474 void Model_Data::eraseBackReferences()
477 std::shared_ptr<ModelAPI_Result> aRes =
478 std::dynamic_pointer_cast<ModelAPI_Result>(myObject);
480 aRes->setIsConcealed(false);
483 void Model_Data::removeBackReference(ObjectPtr theObject, std::string theAttrID)
485 AttributePtr anAttribute = theObject->data()->attribute(theAttrID);
486 removeBackReference(anAttribute);
489 void Model_Data::removeBackReference(AttributePtr theAttr)
491 if (myRefsToMe.find(theAttr) == myRefsToMe.end())
494 myRefsToMe.erase(theAttr);
496 // remove concealment immideately: on deselection it must be posible to reselect in GUI the same
497 FeaturePtr aFeatureOwner = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttr->owner());
498 if (aFeatureOwner.get() &&
499 ModelAPI_Session::get()->validators()->isConcealed(aFeatureOwner->getKind(), theAttr->id())) {
500 updateConcealmentFlag();
504 void Model_Data::addBackReference(FeaturePtr theFeature, std::string theAttrID,
505 const bool theApplyConcealment)
507 addBackReference(ObjectPtr(theFeature), theAttrID);
509 if (theApplyConcealment && theFeature->isStable() &&
510 ModelAPI_Session::get()->validators()->isConcealed(theFeature->getKind(), theAttrID)) {
511 std::shared_ptr<ModelAPI_Result> aRes =
512 std::dynamic_pointer_cast<ModelAPI_Result>(myObject);
513 // the second condition is for history upper than concealment causer, so the feature result may
514 // be displayed and previewed; also for avoiding of quick show/hide on history
516 if (aRes && !theFeature->isDisabled()) {
517 aRes->setIsConcealed(true);
522 void Model_Data::addBackReference(ObjectPtr theObject, std::string theAttrID)
524 // it is possible to add the same attribute twice: may be last time the owner was not Stable...
525 AttributePtr anAttribute = theObject->data()->attribute(theAttrID);
526 if (myRefsToMe.find(anAttribute) == myRefsToMe.end())
527 myRefsToMe.insert(anAttribute);
530 void Model_Data::updateConcealmentFlag()
532 std::set<AttributePtr>::iterator aRefsIter = myRefsToMe.begin();
533 for(; aRefsIter != myRefsToMe.end(); aRefsIter++) {
534 if (aRefsIter->get()) {
535 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>((*aRefsIter)->owner());
536 if (aFeature.get() && !aFeature->isDisabled() && aFeature->isStable()) {
537 if (ModelAPI_Session::get()->validators()->isConcealed(
538 aFeature->getKind(), (*aRefsIter)->id())) {
539 std::shared_ptr<ModelAPI_Result> aRes =
540 std::dynamic_pointer_cast<ModelAPI_Result>(myObject);
542 aRes->setIsConcealed(true); // set concealed
549 std::shared_ptr<ModelAPI_Result> aRes =
550 std::dynamic_pointer_cast<ModelAPI_Result>(myObject);
552 aRes->setIsConcealed(false);
556 std::set<std::string> set_union(const std::set<std::string>& theLeft,
557 const std::set<std::string>& theRight)
559 std::set<std::string> aResult;
560 aResult.insert(theLeft.begin(), theLeft.end());
561 aResult.insert(theRight.begin(), theRight.end());
565 std::set<std::string> usedParameters(const AttributePointPtr& theAttribute)
567 std::set<std::string> anUsedParameters;
568 for (int aComponent = 0; aComponent < 3; ++aComponent)
569 anUsedParameters = set_union(anUsedParameters, theAttribute->usedParameters(aComponent));
570 return anUsedParameters;
573 std::set<std::string> usedParameters(const AttributePoint2DPtr& theAttribute)
575 std::set<std::string> anUsedParameters;
576 for (int aComponent = 0; aComponent < 2; ++aComponent)
577 anUsedParameters = set_union(anUsedParameters, theAttribute->usedParameters(aComponent));
578 return anUsedParameters;
581 std::list<ResultParameterPtr> findVariables(const std::set<std::string>& theParameters)
583 std::list<ResultParameterPtr> aResult;
584 std::set<std::string>::const_iterator aParamIt = theParameters.cbegin();
585 for (; aParamIt != theParameters.cend(); ++aParamIt) {
586 const std::string& aName = *aParamIt;
588 ResultParameterPtr aParam;
589 // theSearcher is not needed here: in expressions
590 // of features the parameters history is not needed
591 if (ModelAPI_Tools::findVariable(FeaturePtr(), aName, aValue, aParam))
592 aResult.push_back(aParam);
597 void Model_Data::referencesToObjects(
598 std::list<std::pair<std::string, std::list<ObjectPtr> > >& theRefs)
600 static Model_ValidatorsFactory* aValidators =
601 static_cast<Model_ValidatorsFactory*>(ModelAPI_Session::get()->validators());
602 FeaturePtr aMyFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(myObject);
604 AttributeMap::iterator anAttrIt = myAttrs.begin();
605 std::list<ObjectPtr> aReferenced; // not inside of cycle to avoid excess memory management
606 for(; anAttrIt != myAttrs.end(); anAttrIt++) {
607 AttributePtr anAttr = anAttrIt->second.first;
608 // skip not-case attributes, that really may refer to anything not-used (issue 671)
609 if (aMyFeature.get() && !aValidators->isCase(aMyFeature, anAttr->id()))
612 std::string aType = anAttr->attributeType();
613 if (aType == ModelAPI_AttributeReference::typeId()) { // reference to object
614 std::shared_ptr<ModelAPI_AttributeReference> aRef = std::dynamic_pointer_cast<
615 ModelAPI_AttributeReference>(anAttr);
616 aReferenced.push_back(aRef->value());
617 } else if (aType == ModelAPI_AttributeRefAttr::typeId()) { // reference to attribute or object
618 std::shared_ptr<ModelAPI_AttributeRefAttr> aRef = std::dynamic_pointer_cast<
619 ModelAPI_AttributeRefAttr>(anAttr);
620 if (aRef->isObject()) {
621 aReferenced.push_back(aRef->object());
623 AttributePtr anAttr = aRef->attr();
625 aReferenced.push_back(anAttr->owner());
627 } else if (aType == ModelAPI_AttributeRefList::typeId()) { // list of references
628 aReferenced = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(anAttr)->list();
629 } else if (aType == ModelAPI_AttributeSelection::typeId()) { // selection attribute
630 std::shared_ptr<ModelAPI_AttributeSelection> aRef = std::dynamic_pointer_cast<
631 ModelAPI_AttributeSelection>(anAttr);
632 aReferenced.push_back(aRef->context());
633 } else if (aType == ModelAPI_AttributeSelectionList::typeId()) { // list of selection attributes
634 std::shared_ptr<ModelAPI_AttributeSelectionList> aRef = std::dynamic_pointer_cast<
635 ModelAPI_AttributeSelectionList>(anAttr);
636 for(int a = 0, aSize = aRef->size(); a < aSize; ++a) {
637 aReferenced.push_back(aRef->value(a)->context());
639 } else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
640 std::shared_ptr<ModelAPI_AttributeRefAttrList> aRefAttr = std::dynamic_pointer_cast<
641 ModelAPI_AttributeRefAttrList>(anAttr);
642 std::list<std::pair<ObjectPtr, AttributePtr> > aRefs = aRefAttr->list();
643 std::list<std::pair<ObjectPtr, AttributePtr> >::const_iterator anIt = aRefs.begin(),
645 for (; anIt != aLast; anIt++) {
646 aReferenced.push_back(anIt->first);
648 } else if (aType == ModelAPI_AttributeInteger::typeId()) { // integer attribute
649 AttributeIntegerPtr anAttribute =
650 std::dynamic_pointer_cast<ModelAPI_AttributeInteger>(anAttr);
651 std::set<std::string> anUsedParameters = anAttribute->usedParameters();
652 std::list<ResultParameterPtr> aParameters = findVariables(anUsedParameters);
653 aReferenced.insert(aReferenced.end(), aParameters.begin(), aParameters.end());
654 } else if (aType == ModelAPI_AttributeDouble::typeId()) { // double attribute
655 AttributeDoublePtr anAttribute =
656 std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(anAttr);
657 std::set<std::string> anUsedParameters = anAttribute->usedParameters();
658 std::list<ResultParameterPtr> aParameters = findVariables(anUsedParameters);
659 aReferenced.insert(aReferenced.end(), aParameters.begin(), aParameters.end());
660 } else if (aType == GeomDataAPI_Point::typeId()) { // point attribute
661 AttributePointPtr anAttribute =
662 std::dynamic_pointer_cast<GeomDataAPI_Point>(anAttr);
663 std::set<std::string> anUsedParameters = usedParameters(anAttribute);
664 std::list<ResultParameterPtr> aParameters = findVariables(anUsedParameters);
665 aReferenced.insert(aReferenced.end(), aParameters.begin(), aParameters.end());
666 } else if (aType == GeomDataAPI_Point2D::typeId()) { // point attribute
667 AttributePoint2DPtr anAttribute =
668 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttr);
669 std::set<std::string> anUsedParameters = usedParameters(anAttribute);
670 std::list<ResultParameterPtr> aParameters = findVariables(anUsedParameters);
671 aReferenced.insert(aReferenced.end(), aParameters.begin(), aParameters.end());
673 continue; // nothing to do, not reference
675 if (!aReferenced.empty()) {
677 std::pair<std::string, std::list<ObjectPtr> >(anAttrIt->first, aReferenced));
683 /// makes copy of all attributes on the given label and all sub-labels
684 static void copyAttrs(TDF_Label theSource, TDF_Label theDestination) {
685 TDF_AttributeIterator anAttrIter(theSource);
686 for(; anAttrIter.More(); anAttrIter.Next()) {
687 Handle(TDF_Attribute) aTargetAttr;
688 if (!theDestination.FindAttribute(anAttrIter.Value()->ID(), aTargetAttr)) {
689 // create a new attribute if not yet exists in the destination
690 aTargetAttr = anAttrIter.Value()->NewEmpty();
691 theDestination.AddAttribute(aTargetAttr);
693 // no special relocation, empty map, but self-relocation is on: copy references w/o changes
694 Handle(TDF_RelocationTable) aRelocTable = new TDF_RelocationTable(Standard_True);
695 anAttrIter.Value()->Paste(aTargetAttr, aRelocTable);
697 // copy the sub-labels content
698 TDF_ChildIterator aSubLabsIter(theSource);
699 for(; aSubLabsIter.More(); aSubLabsIter.Next()) {
700 copyAttrs(aSubLabsIter.Value(), theDestination.FindChild(aSubLabsIter.Value().Tag()));
704 void Model_Data::copyTo(std::shared_ptr<ModelAPI_Data> theTarget)
706 TDF_Label aTargetRoot = std::dynamic_pointer_cast<Model_Data>(theTarget)->label();
707 copyAttrs(myLab, aTargetRoot);
708 // reinitialize Model_Attributes by TDF_Attributes set
709 std::shared_ptr<Model_Data> aTData = std::dynamic_pointer_cast<Model_Data>(theTarget);
710 aTData->myAttrs.clear();
711 theTarget->owner()->initAttributes(); // reinit feature attributes
714 bool Model_Data::isInHistory()
716 return myFlags->Value(kFlagInHistory) == Standard_True;
719 void Model_Data::setIsInHistory(const bool theFlag)
721 return myFlags->SetValue(kFlagInHistory, theFlag);
724 bool Model_Data::isDeleted()
726 return myFlags->Value(kFlagDeleted) == Standard_True;
729 void Model_Data::setIsDeleted(const bool theFlag)
731 return myFlags->SetValue(kFlagDeleted, theFlag);
734 bool Model_Data::isDisplayed()
736 if (!myObject.get() || !myObject->document().get() || // object is in valid
737 myFlags->Value(kFlagDisplayed) != Standard_True) // or it was not displayed before
739 if (myObject->document()->isActive()) // for active documents it must be ok anyway
741 // any object from the root document except part result may be displayed
742 if (myObject->document() == ModelAPI_Session::get()->moduleDocument() &&
743 myObject->groupName() != ModelAPI_ResultPart::group())
748 void Model_Data::setDisplayed(const bool theDisplay)
750 if (theDisplay != isDisplayed()) {
751 myFlags->SetValue(kFlagDisplayed, theDisplay);
752 static Events_Loop* aLoop = Events_Loop::loop();
753 static Events_ID EVENT_DISP = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
754 static const ModelAPI_EventCreator* aECreator = ModelAPI_EventCreator::get();
755 aECreator->sendUpdated(myObject, EVENT_DISP);
759 std::shared_ptr<ModelAPI_Data> Model_Data::invalidPtr()
764 std::shared_ptr<ModelAPI_Data> Model_Data::invalidData()
769 std::shared_ptr<ModelAPI_Object> Model_Data::owner()
774 bool Model_Data::isPrecedingAttribute(const std::string& theAttribute1,
775 const std::string& theAttribute2) const
777 AttributeMap::const_iterator aFound1 = myAttrs.find(theAttribute1);
778 AttributeMap::const_iterator aFound2 = myAttrs.find(theAttribute2);
779 if (aFound2 == myAttrs.end())
781 else if (aFound1 == myAttrs.end())
783 return aFound1->second.second < aFound2->second.second;