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_Objects.h>
22 #include <Model_Data.h>
23 #include <Model_Document.h>
24 #include <Model_Events.h>
25 #include <Model_Session.h>
26 #include <Model_ResultPart.h>
27 #include <Model_ResultConstruction.h>
28 #include <Model_ResultBody.h>
29 #include <Model_ResultCompSolid.h>
30 #include <Model_ResultGroup.h>
31 #include <Model_ResultField.h>
32 #include <Model_ResultParameter.h>
33 #include <ModelAPI_Validator.h>
34 #include <ModelAPI_CompositeFeature.h>
35 #include <ModelAPI_Tools.h>
37 #include <Events_Loop.h>
38 #include <Events_InfoMessage.h>
40 #include <TDataStd_Integer.hxx>
41 #include <TDataStd_Comment.hxx>
42 #include <TDF_ChildIDIterator.hxx>
43 #include <TDataStd_ReferenceArray.hxx>
44 #include <TDataStd_HLabelArray1.hxx>
45 #include <TDataStd_Name.hxx>
46 #include <TDF_Reference.hxx>
47 #include <TDF_ChildIDIterator.hxx>
48 #include <TDF_LabelMapHasher.hxx>
49 #include <TDF_LabelMap.hxx>
50 #include <TDF_ListIteratorOfLabelList.hxx>
52 static const std::string& groupNameFoldering(const std::string& theGroupID,
53 const bool theAllowFolder)
56 static std::map<std::string, std::string> aNames;
57 std::map<std::string, std::string>::const_iterator aFound = aNames.find(theGroupID);
58 if (aFound == aNames.end()) {
59 aNames[theGroupID] = std::string("__") + theGroupID;
60 aFound = aNames.find(theGroupID);
62 return aFound->second;
68 static const int TAG_OBJECTS = 2; // tag of the objects sub-tree (features, results)
71 static const int TAG_FEATURE_ARGUMENTS = 1; ///< where the arguments are located
72 static const int TAG_FEATURE_RESULTS = 2; ///< where the results are located
75 /// 0:1:2 - where features are located
76 /// 0:1:2:N:1 - data of the feature N
77 /// 0:1:2:N:2:K:1 - data of the K result of the feature N
79 Model_Objects::Model_Objects(TDF_Label theMainLab) : myMain(theMainLab)
83 void Model_Objects::setOwner(DocumentPtr theDoc)
86 // update all fields and recreate features and result objects if needed
87 TDF_LabelList aNoUpdated;
88 synchronizeFeatures(aNoUpdated, true, true, true, true);
92 Model_Objects::~Model_Objects()
94 // delete all features of this document
95 Events_Loop* aLoop = Events_Loop::loop();
96 // erase one by one to avoid access from the feature destructor itself from he map
97 // blocks the flush signals to avoid the temporary objects visualization in the viewer
98 // they should not be shown in order to do not lose highlight by erasing them
99 bool isActive = aLoop->activateFlushes(false);
101 while(!myFeatures.IsEmpty()) {
102 NCollection_DataMap<TDF_Label, FeaturePtr>::Iterator aFeaturesIter(myFeatures);
103 FeaturePtr aFeature = aFeaturesIter.Value();
104 static Events_ID EVENT_DISP = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
105 ModelAPI_EventCreator::get()->sendDeleted(myDoc, ModelAPI_Feature::group());
106 ModelAPI_EventCreator::get()->sendUpdated(aFeature, EVENT_DISP);
107 aFeature->removeResults(0, false);
109 myFeatures.UnBind(aFeaturesIter.Key());
111 while (!myFolders.IsEmpty()) {
112 NCollection_DataMap<TDF_Label, ObjectPtr>::Iterator aFoldersIter(myFolders);
113 ObjectPtr aFolder = aFoldersIter.Value();
114 static Events_ID EVENT_DISP = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
115 ModelAPI_EventCreator::get()->sendDeleted(myDoc, ModelAPI_Folder::group());
116 ModelAPI_EventCreator::get()->sendUpdated(aFolder, EVENT_DISP);
118 myFolders.UnBind(aFoldersIter.Key());
121 aLoop->activateFlushes(isActive);
122 // erase update, because features are destroyed and update should not performed for them anywhere
123 aLoop->eraseMessages(Events_Loop::eventByName(EVENT_OBJECT_CREATED));
124 aLoop->eraseMessages(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
125 // deleted and redisplayed is correctly performed: they know that features are destroyed
126 aLoop->flush(Events_Loop::eventByName(EVENT_OBJECT_DELETED));
127 aLoop->flush(Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
131 /// Appends to the array of references a new referenced label
132 static void AddToRefArray(TDF_Label& theArrayLab, TDF_Label& theReferenced, TDF_Label& thePrevLab)
134 Handle(TDataStd_ReferenceArray) aRefs;
135 if (!theArrayLab.FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
136 aRefs = TDataStd_ReferenceArray::Set(theArrayLab, 0, 0);
137 aRefs->SetValue(0, theReferenced);
138 } else { // extend array by one more element
139 Handle(TDataStd_HLabelArray1) aNewArray = new TDataStd_HLabelArray1(aRefs->Lower(),
141 int aPassedPrev = 0; // prev feature is found and passed
142 if (thePrevLab.IsNull()) { // null means that inserted feature must be the first
143 aNewArray->SetValue(aRefs->Lower(), theReferenced);
146 for (int a = aRefs->Lower(); a <= aRefs->Upper(); a++) {
147 aNewArray->SetValue(a + aPassedPrev, aRefs->Value(a));
148 if (!aPassedPrev && aRefs->Value(a).IsEqual(thePrevLab)) {
150 aNewArray->SetValue(a + 1, theReferenced);
153 if (!aPassedPrev) // not found: unknown situation
154 aNewArray->SetValue(aRefs->Upper() + 1, theReferenced);
155 aRefs->SetInternalArray(aNewArray);
159 void Model_Objects::addFeature(FeaturePtr theFeature, const FeaturePtr theAfterThis)
161 if (!theFeature->isAction()) { // do not add action to the data model
162 TDF_Label aFeaturesLab = featuresLabel();
163 TDF_Label aFeatureLab = aFeaturesLab.NewChild();
164 // store feature in the features array: before "initData" because in macro features
165 // in initData it creates new features, appeared later than this
166 TDF_Label aPrevFeateureLab;
167 if (theAfterThis.get()) { // searching for the previous feature label
168 std::shared_ptr<Model_Data> aPrevData =
169 std::dynamic_pointer_cast<Model_Data>(theAfterThis->data());
170 if (aPrevData.get()) {
171 aPrevFeateureLab = aPrevData->label().Father();
174 AddToRefArray(aFeaturesLab, aFeatureLab, aPrevFeateureLab);
176 // keep the feature ID to restore document later correctly
177 TDataStd_Comment::Set(aFeatureLab, theFeature->getKind().c_str());
178 myFeatures.Bind(aFeatureLab, theFeature);
179 // must be before the event sending: for OB the feature is already added
180 updateHistory(ModelAPI_Feature::group());
181 // do not change the order:
184 // during python script with fillet constraint feature data should be
185 // initialized before using it in GUI
187 // must be after binding to the map because of "Box" macro feature that
188 // creates other features in "initData"
189 initData(theFeature, aFeatureLab, TAG_FEATURE_ARGUMENTS);
190 // event: feature is added, mist be before "initData" to update OB correctly on Duplicate:
191 // first new part, then the content
192 static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_CREATED);
193 ModelAPI_EventCreator::get()->sendUpdated(theFeature, anEvent);
194 } else { // make feature has not-null data anyway
195 theFeature->setData(Model_Data::invalidData());
196 theFeature->setDoc(myDoc);
200 /// Appends to the array of references a new referenced label.
201 /// If theIndex is not -1, removes element at this index, not theReferenced.
202 /// \returns the index of removed element
203 static int RemoveFromRefArray(TDF_Label theArrayLab, TDF_Label theReferenced,
204 const int theIndex = -1)
206 int aResult = -1; // no returned
207 Handle(TDataStd_ReferenceArray) aRefs;
208 if (theArrayLab.FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
209 if (aRefs->Length() == 1) { // just erase an array
210 if ((theIndex == -1 && aRefs->Value(0) == theReferenced) || theIndex == 0) {
211 theArrayLab.ForgetAttribute(TDataStd_ReferenceArray::GetID());
214 } else { // reduce the array
215 Handle(TDataStd_HLabelArray1) aNewArray = new TDataStd_HLabelArray1(aRefs->Lower(),
217 int aCount = aRefs->Lower();
218 for (int a = aCount; a <= aRefs->Upper(); a++, aCount++) {
219 if ((theIndex == -1 && aRefs->Value(a) == theReferenced) || theIndex == a) {
223 aNewArray->SetValue(aCount, aRefs->Value(a));
226 aRefs->SetInternalArray(aNewArray);
232 void Model_Objects::refsToFeature(FeaturePtr theFeature,
233 std::set<std::shared_ptr<ModelAPI_Feature> >& theRefs, const bool isSendError)
235 // check the feature: it must have no depended objects on it
236 // the dependencies can be in the feature results
237 std::list<ResultPtr> aResults;
238 ModelAPI_Tools::allResults(theFeature, aResults);
239 std::list<ResultPtr>::const_iterator aResIter = aResults.cbegin();
240 for (; aResIter != aResults.cend(); aResIter++) {
241 ResultPtr aResult = (*aResIter);
242 std::shared_ptr<Model_Data> aData =
243 std::dynamic_pointer_cast<Model_Data>(aResult->data());
244 if (aData.get() != NULL) {
245 const std::set<AttributePtr>& aRefs = aData->refsToMe();
246 std::set<AttributePtr>::const_iterator aRefIt = aRefs.begin(), aRefLast = aRefs.end();
247 for (; aRefIt != aRefLast; aRefIt++) {
248 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>((*aRefIt)->owner());
249 if (aFeature.get() != NULL)
250 theRefs.insert(aFeature);
254 // the dependencies can be in the feature itself
255 std::shared_ptr<Model_Data> aData =
256 std::dynamic_pointer_cast<Model_Data>(theFeature->data());
257 if (aData.get() && !aData->refsToMe().empty()) {
258 const std::set<AttributePtr>& aRefs = aData->refsToMe();
259 std::set<AttributePtr>::const_iterator aRefIt = aRefs.begin(), aRefLast = aRefs.end();
260 for (; aRefIt != aRefLast; aRefIt++) {
261 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>((*aRefIt)->owner());
262 if (aFeature.get() != NULL)
263 theRefs.insert(aFeature);
267 if (!theRefs.empty() && isSendError) {
268 Events_InfoMessage("Model_Objects",
269 "Feature '%1' is used and can not be deleted").arg(theFeature->data()->name()).send();
273 void Model_Objects::removeFeature(FeaturePtr theFeature)
275 std::shared_ptr<Model_Data> aData = std::static_pointer_cast<Model_Data>(theFeature->data());
276 if (aData.get() && aData->isValid()) {
277 // checking that the sub-element of composite feature is removed: if yes, inform the owner
278 std::set<std::shared_ptr<ModelAPI_Feature> > aRefs;
279 refsToFeature(theFeature, aRefs, false);
280 std::set<std::shared_ptr<ModelAPI_Feature> >::iterator aRefIter = aRefs.begin();
281 for(; aRefIter != aRefs.end(); aRefIter++) {
282 std::shared_ptr<ModelAPI_CompositeFeature> aComposite =
283 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(*aRefIter);
284 if (aComposite.get() && aComposite->isSub(theFeature)) {
285 aComposite->removeFeature(theFeature);
288 // this must be before erase since theFeature erasing removes all information about
289 // the feature results and groups of results
290 // To reproduce: create sketch, extrusion, remove sketch => constructions tree is not updated
291 clearHistory(theFeature);
295 TDF_Label aFeatureLabel = aData->label().Father();
296 if (myFeatures.IsBound(aFeatureLabel))
297 myFeatures.UnBind(aFeatureLabel);
299 static Events_ID EVENT_DISP = Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
300 ModelAPI_EventCreator::get()->sendUpdated(theFeature, EVENT_DISP);
301 // erase all attributes under the label of feature
302 aFeatureLabel.ForgetAllAttributes();
303 // remove it from the references array
304 RemoveFromRefArray(featuresLabel(), aFeatureLabel);
305 // event: feature is deleted
306 ModelAPI_EventCreator::get()->sendDeleted(theFeature->document(), ModelAPI_Feature::group());
307 updateHistory(ModelAPI_Feature::group());
311 void Model_Objects::eraseAllFeatures()
313 static Events_ID kDispEvent = Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
314 static const ModelAPI_EventCreator* kCreator = ModelAPI_EventCreator::get();
315 // make all features invalid (like deleted)
316 NCollection_DataMap<TDF_Label, FeaturePtr>::Iterator aFIter(myFeatures);
317 for(; aFIter.More(); aFIter.Next()) {
318 FeaturePtr aFeature = aFIter.Value();
319 std::list<ResultPtr> aResList;
320 ModelAPI_Tools::allResults(aFeature, aResList);
321 std::list<ResultPtr>::iterator aRIter = aResList.begin();
322 for(; aRIter != aResList.end(); aRIter++) {
323 ResultPtr aRes = *aRIter;
324 if (aRes && aRes->data()->isValid()) {
325 kCreator->sendDeleted(myDoc, aRes->groupName());
326 kCreator->sendUpdated(aRes, kDispEvent);
327 aRes->setData(aRes->data()->invalidPtr());
331 kCreator->sendUpdated(aFeature, kDispEvent);
332 aFeature->setData(aFeature->data()->invalidPtr());
334 kCreator->sendDeleted(myDoc, ModelAPI_Feature::group());
335 myFeatures.Clear(); // just remove features without modification of DS
336 updateHistory(ModelAPI_Feature::group());
339 void Model_Objects::moveFeature(FeaturePtr theMoved, FeaturePtr theAfterThis)
341 TDF_Label aFeaturesLab = featuresLabel();
342 Handle(TDataStd_ReferenceArray) aRefs;
343 if (!aFeaturesLab.FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs))
345 TDF_Label anAfterLab, aMovedLab =
346 std::dynamic_pointer_cast<Model_Data>(theMoved->data())->label().Father();
347 if (theAfterThis.get())
348 anAfterLab = std::dynamic_pointer_cast<Model_Data>(theAfterThis->data())->label().Father();
350 Handle(TDataStd_HLabelArray1) aNewArray =
351 new TDataStd_HLabelArray1(aRefs->Lower(), aRefs->Upper());
352 int aPassedMovedFrom = 0; // the prev feature location is found and passed
353 int aPassedMovedTo = 0; // the feature is added and this location is passed
354 if (!theAfterThis.get()) { // null means that inserted feature must be the first
355 aNewArray->SetValue(aRefs->Lower(), aMovedLab);
358 for (int a = aRefs->Lower(); a <= aRefs->Upper(); a++) {
359 if (aPassedMovedTo == 0 && aRefs->Value(a) == anAfterLab) { // add two
361 aNewArray->SetValue(a - aPassedMovedFrom, anAfterLab);
362 if (a + 1 - aPassedMovedFrom <= aRefs->Upper())
363 aNewArray->SetValue(a + 1 - aPassedMovedFrom, aMovedLab);
364 } else if (aPassedMovedFrom == 0 && aRefs->Value(a) == aMovedLab) { // skip
366 } else { // just copy one
367 if (a - aPassedMovedFrom + aPassedMovedTo <= aRefs->Upper())
368 aNewArray->SetValue(a - aPassedMovedFrom + aPassedMovedTo, aRefs->Value(a));
371 if (!aPassedMovedFrom || !aPassedMovedTo) {// not found: unknown situation
372 if (!aPassedMovedFrom) {
373 static std::string aMovedFromError("The moved feature is not found");
374 Events_InfoMessage("Model_Objects", aMovedFromError).send();
376 static std::string aMovedToError("The 'after' feature for movement is not found");
377 Events_InfoMessage("Model_Objects", aMovedToError).send();
381 // store the new array
382 aRefs->SetInternalArray(aNewArray);
383 // update the feature and the history
384 clearHistory(theMoved);
385 // make sure all (selection) attributes of moved feature will be updated
386 static Events_ID kUpdateSelection = Events_Loop::loop()->eventByName(EVENT_UPDATE_SELECTION);
387 ModelAPI_EventCreator::get()->sendUpdated(theMoved, kUpdateSelection, false);
388 ModelAPI_EventCreator::get()->sendReordered(theMoved);
391 void Model_Objects::clearHistory(ObjectPtr theObj)
394 const std::string aGroup = theObj->groupName();
395 updateHistory(aGroup);
397 if (theObj->groupName() == ModelAPI_Feature::group()) { // clear results group of the feature
398 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObj);
399 std::string aResultGroup = featureResultGroup(aFeature);
400 if (!aResultGroup.empty()) {
401 std::map<std::string, std::vector<ObjectPtr> >::iterator aHIter =
402 myHistory.find(aResultGroup);
403 if (aHIter != myHistory.end())
404 myHistory.erase(aHIter); // erase from map => this means that it is not synchronized
410 void Model_Objects::createHistory(const std::string& theGroupID)
412 std::map<std::string, std::vector<ObjectPtr> >::iterator aHIter = myHistory.find(theGroupID);
413 if (aHIter == myHistory.end()) {
414 std::vector<ObjectPtr> aResult;
415 std::vector<ObjectPtr> aResultOutOfFolder;
416 FeaturePtr aLastFeatureInFolder;
417 // iterate the array of references and get feature by feature from the array
418 bool isFeature = theGroupID == ModelAPI_Feature::group();
419 Handle(TDataStd_ReferenceArray) aRefs;
420 if (featuresLabel().FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
421 for(int a = aRefs->Lower(); a <= aRefs->Upper(); a++) {
422 FeaturePtr aFeature = feature(aRefs->Value(a));
423 if (aFeature.get()) {
424 // if feature is in sub-component, remove it from history:
425 // it is in sub-tree of sub-component
426 bool isSub = ModelAPI_Tools::compositeOwner(aFeature).get() != NULL;
427 if (isFeature) { // here may be also disabled features
428 if (!isSub && aFeature->isInHistory()) {
429 aResult.push_back(aFeature);
430 // the feature is out of the folders
431 if (aLastFeatureInFolder.get() == NULL)
432 aResultOutOfFolder.push_back(aFeature);
434 } else if (!aFeature->isDisabled()) { // iterate all results of not-disabled feature
435 // construction results of sub-features should not be in the tree
436 if (!isSub || theGroupID != ModelAPI_ResultConstruction::group()) {
437 // do not use reference to the list here since results can be changed by "isConcealed"
438 const std::list<std::shared_ptr<ModelAPI_Result> > aResults = aFeature->results();
439 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator
440 aRIter = aResults.begin();
441 for (; aRIter != aResults.cend(); aRIter++) {
442 ResultPtr aRes = *aRIter;
443 if (aRes->groupName() != theGroupID) break; // feature have only same group results
444 if (!aRes->isDisabled() && aRes->isInHistory() && !aRes->isConcealed()) {
445 aResult.push_back(*aRIter);
451 // the feature closes the folder, so the next features will be treated as out-of-folder
452 if (aLastFeatureInFolder.get() && aLastFeatureInFolder == aFeature)
453 aLastFeatureInFolder = FeaturePtr();
456 // it may be a folder
457 ObjectPtr aFolder = folder(aRefs->Value(a));
459 aResult.push_back(aFolder);
460 if (theGroupID != ModelAPI_Folder::group())
461 aResultOutOfFolder.push_back(aFolder);
463 // get the last feature in the folder
464 AttributeReferencePtr aLastFeatAttr =
465 aFolder->data()->reference(ModelAPI_Folder::LAST_FEATURE_ID());
467 aLastFeatureInFolder = ModelAPI_Feature::feature(aLastFeatAttr->value());
472 // to be sure that isConcealed did not update the history (issue 1089) during the iteration
473 if (myHistory.find(theGroupID) == myHistory.end()) {
474 myHistory[theGroupID] = aResult;
476 // store the features placed out of any folder
477 const std::string& anOutOfFolderGroupID = groupNameFoldering(theGroupID, true);
478 myHistory[anOutOfFolderGroupID] = aResultOutOfFolder;
483 void Model_Objects::updateHistory(const std::shared_ptr<ModelAPI_Object> theObject)
485 clearHistory(theObject);
488 void Model_Objects::updateHistory(const std::string theGroup)
490 std::map<std::string, std::vector<ObjectPtr> >::iterator aHIter = myHistory.find(theGroup);
491 if (aHIter != myHistory.end()) {
492 myHistory.erase(aHIter); // erase from map => this means that it is not synchronized
494 // erase history for the group of objects placed out of any folder
495 const std::string& anOutOfFolderGroupID = groupNameFoldering(theGroup, true);
496 myHistory.erase(anOutOfFolderGroupID);
500 ObjectPtr Model_Objects::folder(TDF_Label theLabel) const
502 if (myFolders.IsBound(theLabel))
503 return myFolders.Find(theLabel);
507 FeaturePtr Model_Objects::feature(TDF_Label theLabel) const
509 if (myFeatures.IsBound(theLabel))
510 return myFeatures.Find(theLabel);
511 return FeaturePtr(); // not found
514 ObjectPtr Model_Objects::object(TDF_Label theLabel)
516 // try feature by label
517 FeaturePtr aFeature = feature(theLabel);
519 return feature(theLabel);
520 TDF_Label aFeatureLabel = theLabel.Father().Father(); // let's suppose it is result
521 aFeature = feature(aFeatureLabel);
522 bool isSubResult = false;
523 if (!aFeature.get() && aFeatureLabel.Depth() > 1) { // let's suppose this is sub-result of result
524 aFeatureLabel = aFeatureLabel.Father().Father();
525 aFeature = feature(aFeatureLabel);
528 if (aFeature.get()) {
529 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aFeature->results();
530 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.cbegin();
531 for (; aRIter != aResults.cend(); aRIter++) {
533 ResultCompSolidPtr aCompRes = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(*aRIter);
534 if (aCompRes.get()) {
535 int aNumSubs = aCompRes->numberOfSubs();
536 for(int a = 0; a < aNumSubs; a++) {
537 ResultPtr aSub = aCompRes->subResult(a);
539 std::shared_ptr<Model_Data> aSubData = std::dynamic_pointer_cast<Model_Data>(
541 if (aSubData->label().Father().IsEqual(theLabel))
547 std::shared_ptr<Model_Data> aResData = std::dynamic_pointer_cast<Model_Data>(
549 if (aResData->label().Father().IsEqual(theLabel))
554 return FeaturePtr(); // not found
557 ObjectPtr Model_Objects::object(const std::string& theGroupID,
559 const bool theAllowFolder)
563 createHistory(theGroupID);
564 const std::string& aGroupID = groupNameFoldering(theGroupID, theAllowFolder);
565 return myHistory[aGroupID][theIndex];
568 std::shared_ptr<ModelAPI_Object> Model_Objects::objectByName(
569 const std::string& theGroupID, const std::string& theName)
571 createHistory(theGroupID);
572 if (theGroupID == ModelAPI_Feature::group()) { // searching among features (in history or not)
573 std::list<std::shared_ptr<ModelAPI_Feature> > allObjs = allFeatures();
574 std::list<std::shared_ptr<ModelAPI_Feature> >::iterator anObjIter = allObjs.begin();
575 for(; anObjIter != allObjs.end(); anObjIter++) {
576 if ((*anObjIter)->data()->name() == theName)
579 } else { // searching among results (concealed or not)
580 std::list<std::shared_ptr<ModelAPI_Feature> > allObjs = allFeatures();
581 std::list<std::shared_ptr<ModelAPI_Feature> >::iterator anObjIter = allObjs.begin();
582 for(; anObjIter != allObjs.end(); anObjIter++) {
583 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = (*anObjIter)->results();
584 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.cbegin();
585 for (; aRIter != aResults.cend(); aRIter++) {
586 if (aRIter->get() && (*aRIter)->groupName() == theGroupID) {
587 if ((*aRIter)->data()->name() == theName)
589 ResultCompSolidPtr aCompRes =
590 std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(*aRIter);
591 if (aCompRes.get()) {
592 int aNumSubs = aCompRes->numberOfSubs();
593 for(int a = 0; a < aNumSubs; a++) {
594 ResultPtr aSub = aCompRes->subResult(a);
595 if (aSub.get() && aSub->groupName() == theGroupID) {
596 if (aSub->data()->name() == theName)
609 const int Model_Objects::index(std::shared_ptr<ModelAPI_Object> theObject)
611 std::string aGroup = theObject->groupName();
612 createHistory(aGroup);
613 std::vector<ObjectPtr>& allObjs = myHistory[aGroup];
614 std::vector<ObjectPtr>::iterator anObjIter = allObjs.begin(); // iterate to search object
615 for(int anIndex = 0; anObjIter != allObjs.end(); anObjIter++, anIndex++) {
616 if ((*anObjIter) == theObject)
623 int Model_Objects::size(const std::string& theGroupID, const bool theAllowFolder)
625 createHistory(theGroupID);
626 const std::string& aGroupID = groupNameFoldering(theGroupID, theAllowFolder);
627 return int(myHistory[aGroupID].size());
630 void Model_Objects::allResults(const std::string& theGroupID, std::list<ResultPtr>& theResults)
632 // iterate the array of references and get feature by feature from the array
633 Handle(TDataStd_ReferenceArray) aRefs;
634 if (featuresLabel().FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
635 for(int a = aRefs->Lower(); a <= aRefs->Upper(); a++) {
636 FeaturePtr aFeature = feature(aRefs->Value(a));
637 if (aFeature.get()) {
638 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aFeature->results();
639 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
640 for (; aRIter != aResults.cend(); aRIter++) {
641 ResultPtr aRes = *aRIter;
642 if (aRes->groupName() != theGroupID) break; // feature have only same group results
643 // iterate also concealed: ALL RESULTS (for translation parts undo/redo management)
644 //if (aRes->isInHistory() && !aRes->isConcealed()) {
645 theResults.push_back(*aRIter);
654 TDF_Label Model_Objects::featuresLabel() const
656 return myMain.FindChild(TAG_OBJECTS);
659 static std::string composeName(const std::string& theFeatureKind, const int theIndex)
661 std::stringstream aNameStream;
662 aNameStream << theFeatureKind << "_" << theIndex;
663 return aNameStream.str();
666 void Model_Objects::setUniqueName(FeaturePtr theFeature)
668 if (!theFeature->data()->name().empty())
669 return; // not needed, name is already defined
670 std::string aName; // result
671 // first count all features of such kind to start with index = count + 1
672 int aNumObjects = -1; // this feature is already in this map
673 NCollection_DataMap<TDF_Label, FeaturePtr>::Iterator aFIter(myFeatures);
674 for (; aFIter.More(); aFIter.Next()) {
675 if (aFIter.Value()->getKind() == theFeature->getKind())
678 // generate candidate name
679 aName = composeName(theFeature->getKind(), aNumObjects + 1);
680 // check this is unique, if not, increase index by 1
681 for (aFIter.Initialize(myFeatures); aFIter.More();) {
682 FeaturePtr aFeature = aFIter.Value();
683 bool isSameName = aFeature->data()->name() == aName;
684 if (!isSameName) { // check also results to avoid same results names (actual for Parts)
685 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aFeature->results();
686 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
687 for (; aRIter != aResults.cend(); aRIter++) {
688 isSameName = (*aRIter)->data()->name() == aName;
694 aName = composeName(theFeature->getKind(), aNumObjects + 1);
695 // reinitialize iterator to make sure a new name is unique
696 aFIter.Initialize(myFeatures);
700 theFeature->data()->setName(aName);
703 void Model_Objects::setUniqueName(FolderPtr theFolder)
705 if (!theFolder->name().empty())
706 return; // name is already defined
708 int aNbFolders = myFolders.Size();
709 std::string aName = composeName(ModelAPI_Folder::ID(), aNbFolders);
711 // check the uniqueness of the name
712 NCollection_DataMap<TDF_Label, ObjectPtr>::Iterator anIt(myFolders);
713 while (anIt.More()) {
714 if (anIt.Value()->data()->name() == aName) {
715 aName = composeName(ModelAPI_Folder::ID(), aNbFolders);
716 // reinitialize iterator to make sure a new name is unique
717 anIt.Initialize(myFolders);
722 theFolder->data()->setName(aName);
725 void Model_Objects::initData(ObjectPtr theObj, TDF_Label theLab, const int theTag)
727 std::shared_ptr<Model_Data> aData(new Model_Data);
728 aData->setLabel(theLab.FindChild(theTag));
729 aData->setObject(theObj);
730 theObj->setDoc(myDoc);
731 theObj->setData(aData);
732 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObj);
733 if (aFeature.get()) {
734 setUniqueName(aFeature); // must be before "initAttributes" because duplicate part uses name
735 } else { // is it a folder?
736 FolderPtr aFolder = std::dynamic_pointer_cast<ModelAPI_Folder>(theObj);
738 setUniqueName(aFolder);
740 theObj->initAttributes();
743 std::shared_ptr<ModelAPI_Feature> Model_Objects::featureById(const int theId)
746 TDF_Label aLab = featuresLabel().FindChild(theId, Standard_False);
747 return feature(aLab);
749 return std::shared_ptr<ModelAPI_Feature>(); // not found
752 void Model_Objects::synchronizeFeatures(
753 const TDF_LabelList& theUpdated, const bool theUpdateReferences,
754 const bool theExecuteFeatures, const bool theOpen, const bool theFlush)
756 Model_Document* anOwner = std::dynamic_pointer_cast<Model_Document>(myDoc).get();
757 if (!anOwner) // this may happen on creation of document: nothing there, so nothing to synchronize
759 // after all updates, sends a message that groups of features were created or updated
760 Events_Loop* aLoop = Events_Loop::loop();
761 static Events_ID aDispEvent = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
762 static Events_ID aCreateEvent = Events_Loop::eventByName(EVENT_OBJECT_CREATED);
763 static Events_ID anUpdateEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
764 static Events_ID aRedispEvent = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
765 static Events_ID aDeleteEvent = Events_Loop::eventByName(EVENT_OBJECT_DELETED);
766 static Events_ID aToHideEvent = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
767 bool isActive = aLoop->activateFlushes(false);
769 // collect all updated labels map
770 TDF_LabelMap anUpdatedMap;
771 TDF_ListIteratorOfLabelList anUpdatedIter(theUpdated);
772 for(; anUpdatedIter.More(); anUpdatedIter.Next()) {
773 TDF_Label& aFeatureLab = anUpdatedIter.Value();
774 while(aFeatureLab.Depth() > 3)
775 aFeatureLab = aFeatureLab.Father();
776 if (myFeatures.IsBound(aFeatureLab))
777 anUpdatedMap.Add(aFeatureLab);
780 // update all objects by checking are they on labels or not
781 std::set<FeaturePtr> aNewFeatures, aKeptFeatures;
782 TDF_ChildIDIterator aLabIter(featuresLabel(), TDataStd_Comment::GetID());
783 for (; aLabIter.More(); aLabIter.Next()) {
784 TDF_Label aFeatureLabel = aLabIter.Value()->Label();
786 if (!myFeatures.IsBound(aFeatureLabel)) { // a new feature is inserted
788 aFeature = std::dynamic_pointer_cast<Model_Session>(ModelAPI_Session::get())->createFeature(
789 TCollection_AsciiString(Handle(TDataStd_Comment)::DownCast(aLabIter.Value())->Get())
790 .ToCString(), anOwner);
791 if (!aFeature.get()) {
792 // somethig is wrong, most probably, the opened document has invalid structure
793 Events_InfoMessage("Model_Objects", "Invalid type of object in the document").send();
794 aLabIter.Value()->Label().ForgetAllAttributes();
798 // this must be before "setData" to redo the sketch line correctly
799 myFeatures.Bind(aFeatureLabel, aFeature);
800 aNewFeatures.insert(aFeature);
801 initData(aFeature, aFeatureLabel, TAG_FEATURE_ARGUMENTS);
802 updateHistory(aFeature);
804 // event: model is updated
805 ModelAPI_EventCreator::get()->sendUpdated(aFeature, aCreateEvent);
806 } else { // nothing is changed, both iterators are incremented
807 aFeature = myFeatures.Find(aFeatureLabel);
808 aKeptFeatures.insert(aFeature);
809 if (anUpdatedMap.Contains(aFeatureLabel)) {
810 if (!theOpen) { // on abort/undo/redo reinitialize attributes if something is changed
811 std::list<std::shared_ptr<ModelAPI_Attribute> > anAttrs =
812 aFeature->data()->attributes("");
813 std::list<std::shared_ptr<ModelAPI_Attribute> >::iterator anAttr = anAttrs.begin();
814 for(; anAttr != anAttrs.end(); anAttr++)
817 ModelAPI_EventCreator::get()->sendUpdated(aFeature, anUpdateEvent);
818 if (aFeature->getKind() == "Parameter") {
819 // if parameters are changed, update the results (issue 937)
820 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aFeature->results();
821 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
822 for (; aRIter != aResults.cend(); aRIter++) {
823 std::shared_ptr<ModelAPI_Result> aRes = *aRIter;
824 if (aRes->data()->isValid() && !aRes->isDisabled()) {
825 ModelAPI_EventCreator::get()->sendUpdated(aRes, anUpdateEvent);
833 // check all features are checked: if not => it was removed
834 NCollection_DataMap<TDF_Label, FeaturePtr>::Iterator aFIter(myFeatures);
835 while (aFIter.More()) {
836 if (aKeptFeatures.find(aFIter.Value()) == aKeptFeatures.end()
837 && aNewFeatures.find(aFIter.Value()) == aNewFeatures.end()) {
838 FeaturePtr aFeature = aFIter.Value();
839 // event: model is updated
840 //if (aFeature->isInHistory()) {
841 ModelAPI_EventCreator::get()->sendDeleted(myDoc, ModelAPI_Feature::group());
843 // results of this feature must be redisplayed (hided)
844 // redisplay also removed feature (used for sketch and AISObject)
845 ModelAPI_EventCreator::get()->sendUpdated(aFeature, aRedispEvent);
846 updateHistory(aFeature);
849 // unbind after the "erase" call: on abort sketch
850 // is removes sub-objects that corrupts aFIter
851 myFeatures.UnBind(aFIter.Key());
852 // reinitialize iterator because unbind may corrupt the previous order in the map
853 aFIter.Initialize(myFeatures);
858 if (theUpdateReferences) {
859 synchronizeBackRefs();
861 // update results of the features (after features created because
862 // they may be connected, like sketch and sub elements)
863 // After synchronisation of back references because sketch
864 // must be set in sub-elements before "execute" by updateResults
865 std::set<FeaturePtr> aProcessed; // composites must be updated after their subs (issue 360)
866 TDF_ChildIDIterator aLabIter2(featuresLabel(), TDataStd_Comment::GetID());
867 for (; aLabIter2.More(); aLabIter2.Next()) {
868 TDF_Label aFeatureLabel = aLabIter2.Value()->Label();
869 if (myFeatures.IsBound(aFeatureLabel)) { // a new feature is inserted
870 FeaturePtr aFeature = myFeatures.Find(aFeatureLabel);
871 updateResults(aFeature, aProcessed);
874 // the synchronize should be done after updateResults
875 // in order to correct back references of updated results
876 if (theUpdateReferences) {
877 synchronizeBackRefs();
879 if (!theUpdated.IsEmpty()) {
880 // this means there is no control what was modified => remove history cash
884 if (theExecuteFeatures)
885 anOwner->executeFeatures() = false;
886 aLoop->activateFlushes(isActive);
889 aLoop->flush(aDeleteEvent);
890 // delete should be emitted before create to reacts to aborted feature
891 aLoop->flush(aCreateEvent);
892 aLoop->flush(anUpdateEvent);
893 aLoop->flush(aCreateEvent); // after update of features, there could be results created
894 aLoop->flush(aDeleteEvent); // or deleted
895 aLoop->flush(aRedispEvent);
896 aLoop->flush(aToHideEvent);
898 if (theExecuteFeatures)
899 anOwner->executeFeatures() = true;
902 /// synchronises back references for the given object basing on the collected data
903 void Model_Objects::synchronizeBackRefsForObject(const std::set<AttributePtr>& theNewRefs,
906 if (!theObject.get() || !theObject->data()->isValid())
908 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(theObject->data());
909 // iterate new list to compare with curent
910 std::set<AttributePtr>::iterator aNewIter = theNewRefs.begin();
911 for(; aNewIter != theNewRefs.end(); aNewIter++) {
912 if (aData->refsToMe().find(*aNewIter) == aData->refsToMe().end()) {
913 FeaturePtr aRefFeat = std::dynamic_pointer_cast<ModelAPI_Feature>((*aNewIter)->owner());
914 aData->addBackReference(aRefFeat, (*aNewIter)->id());
917 if (theNewRefs.size() != aData->refsToMe().size()) { // some back ref must be removed
918 std::set<AttributePtr>::iterator aCurrentIter = aData->refsToMe().begin();
919 while(aCurrentIter != aData->refsToMe().end()) {
920 if (theNewRefs.find(*aCurrentIter) == theNewRefs.end()) {
921 // for external references from other documents this system
922 // is not working: refs are collected from
923 // different Model_Objects, so before remove check this
924 // external object exists and still referenced
925 bool aLeaveIt = false;
926 if ((*aCurrentIter)->owner().get() && (*aCurrentIter)->owner()->document() != myDoc &&
927 (*aCurrentIter)->owner()->data().get() && (*aCurrentIter)->owner()->data()->isValid()) {
928 std::list<std::pair<std::string, std::list<std::shared_ptr<ModelAPI_Object> > > > aRefs;
929 (*aCurrentIter)->owner()->data()->referencesToObjects(aRefs);
930 std::list<std::pair<std::string, std::list<std::shared_ptr<ModelAPI_Object> >>>::iterator
931 aRefIter = aRefs.begin();
932 for(; aRefIter != aRefs.end(); aRefIter++) {
933 if ((*aCurrentIter)->id() == aRefIter->first) {
934 std::list<std::shared_ptr<ModelAPI_Object> >::iterator anOIt;
935 for(anOIt = aRefIter->second.begin(); anOIt != aRefIter->second.end(); anOIt++) {
936 if (*anOIt == theObject) {
944 aData->removeBackReference(*aCurrentIter);
945 aCurrentIter = aData->refsToMe().begin(); // reinitialize iteration after delete
946 } else aCurrentIter++;
947 } else aCurrentIter++;
950 aData->updateConcealmentFlag();
953 void Model_Objects::synchronizeBackRefs()
955 // collect all back references in the separated container: to update everything at once,
956 // without additional Concealment switchin on and off: only the final modification
958 // referenced (slave) objects to referencing attirbutes
959 std::map<ObjectPtr, std::set<AttributePtr> > allRefs;
960 NCollection_DataMap<TDF_Label, FeaturePtr>::Iterator aFeatures(myFeatures);
961 for(; aFeatures.More(); aFeatures.Next()) {
962 FeaturePtr aFeature = aFeatures.Value();
963 std::shared_ptr<Model_Data> aFData = std::dynamic_pointer_cast<Model_Data>(aFeature->data());
965 std::list<std::pair<std::string, std::list<ObjectPtr> > > aRefs;
966 aFData->referencesToObjects(aRefs);
967 std::list<std::pair<std::string, std::list<ObjectPtr> > >::iterator aRefsIt = aRefs.begin();
968 for(; aRefsIt != aRefs.end(); aRefsIt++) {
969 std::list<ObjectPtr>::iterator aRefTo = aRefsIt->second.begin();
970 for(; aRefTo != aRefsIt->second.end(); aRefTo++) {
972 std::map<ObjectPtr, std::set<AttributePtr> >::iterator aFound = allRefs.find(*aRefTo);
973 if (aFound == allRefs.end()) {
974 allRefs[*aRefTo] = std::set<AttributePtr>();
975 aFound = allRefs.find(*aRefTo);
977 aFound->second.insert(aFeature->data()->attribute(aRefsIt->first));
983 // second iteration: just compare back-references with existing in features and results
984 for(aFeatures.Initialize(myFeatures); aFeatures.More(); aFeatures.Next()) {
985 FeaturePtr aFeature = aFeatures.Value();
986 static std::set<AttributePtr> anEmpty;
987 std::map<ObjectPtr, std::set<AttributePtr> >::iterator aFound = allRefs.find(aFeature);
988 if (aFound == allRefs.end()) { // not found => erase all back references
989 synchronizeBackRefsForObject(anEmpty, aFeature);
991 synchronizeBackRefsForObject(aFound->second, aFeature);
992 allRefs.erase(aFound); // to check that all refs are counted
995 std::list<ResultPtr> aResults;
996 ModelAPI_Tools::allResults(aFeature, aResults);
997 std::list<ResultPtr>::iterator aRIter = aResults.begin();
998 for(; aRIter != aResults.cend(); aRIter++) {
999 aFound = allRefs.find(*aRIter);
1000 if (aFound == allRefs.end()) { // not found => erase all back references
1001 synchronizeBackRefsForObject(anEmpty, *aRIter);
1003 synchronizeBackRefsForObject(aFound->second, *aRIter);
1004 allRefs.erase(aFound); // to check that all refs are counted
1008 for(aFeatures.Initialize(myFeatures); aFeatures.More(); aFeatures.Next()) {
1009 FeaturePtr aFeature = aFeatures.Value();
1010 std::list<ResultPtr> aResults;
1011 ModelAPI_Tools::allResults(aFeature, aResults);
1012 // update the concealment status for disply in isConcealed of ResultBody
1013 std::list<ResultPtr>::iterator aRIter = aResults.begin();
1014 for(; aRIter != aResults.cend(); aRIter++) {
1015 (*aRIter)->isConcealed();
1018 // the rest all refs means that feature references to the external document feature:
1019 // process also them
1020 std::map<ObjectPtr, std::set<AttributePtr> >::iterator anExtIter = allRefs.begin();
1021 for(; anExtIter != allRefs.end(); anExtIter++) {
1022 synchronizeBackRefsForObject(anExtIter->second, anExtIter->first);
1026 TDF_Label Model_Objects::resultLabel(
1027 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theResultIndex)
1029 const std::shared_ptr<Model_Data>& aData =
1030 std::dynamic_pointer_cast<Model_Data>(theFeatureData);
1031 return aData->label().Father().FindChild(TAG_FEATURE_RESULTS).FindChild(theResultIndex + 1);
1034 bool Model_Objects::hasCustomName(DataPtr theFeatureData,
1035 ResultPtr theResult,
1037 std::string& theParentName) const
1039 ResultCompSolidPtr aCompSolidRes =
1040 std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(theFeatureData->owner());
1041 if (aCompSolidRes) {
1042 FeaturePtr anOwner = ModelAPI_Feature::feature(theResult->data()->owner());
1044 // names of sub-solids in CompSolid should be default (for example,
1045 // result of boolean operation 'Boolean_1' is a CompSolid which is renamed to 'MyBOOL',
1046 // however, sub-elements of 'MyBOOL' should be named 'Boolean_1_1', 'Boolean_1_2' etc.)
1047 std::ostringstream aDefaultName;
1048 aDefaultName << anOwner->name();
1049 // compute default name of CompSolid (name of feature + index of CompSolid's result)
1050 int aCompSolidResultIndex = 0;
1051 const std::list<ResultPtr>& aResults = anOwner->results();
1052 for (std::list<ResultPtr>::const_iterator anIt = aResults.begin();
1053 anIt != aResults.end(); ++anIt, ++aCompSolidResultIndex)
1054 if (aCompSolidRes == *anIt)
1056 aDefaultName << "_" << (aCompSolidResultIndex + 1);
1057 theParentName = aDefaultName.str();
1061 theParentName = ModelAPI_Tools::getDefaultName(theResult, theResultIndex);
1065 void Model_Objects::storeResult(std::shared_ptr<ModelAPI_Data> theFeatureData,
1066 std::shared_ptr<ModelAPI_Result> theResult,
1067 const int theResultIndex)
1070 theResult->setDoc(myDoc);
1071 initData(theResult, resultLabel(theFeatureData, theResultIndex), TAG_FEATURE_ARGUMENTS);
1072 if (theResult->data()->name().empty()) {
1073 // if was not initialized, generate event and set a name
1074 std::string aNewName = theFeatureData->name();
1075 if (!hasCustomName(theFeatureData, theResult, theResultIndex, aNewName)) {
1076 std::stringstream aName;
1078 // if there are several results (issue #899: any number of result),
1079 // add unique prefix starting from second
1080 if (theResultIndex > 0 || theResult->groupName() == ModelAPI_ResultBody::group())
1081 aName << "_" << theResultIndex + 1;
1082 aNewName = aName.str();
1084 theResult->data()->setName(aNewName);
1088 std::shared_ptr<ModelAPI_ResultConstruction> Model_Objects::createConstruction(
1089 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex)
1091 TDF_Label aLab = resultLabel(theFeatureData, theIndex);
1092 TDataStd_Comment::Set(aLab, ModelAPI_ResultConstruction::group().c_str());
1093 ObjectPtr anOldObject = object(aLab);
1094 std::shared_ptr<ModelAPI_ResultConstruction> aResult;
1095 if (anOldObject.get()) {
1096 aResult = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(anOldObject);
1098 if (!aResult.get()) {
1099 aResult = std::shared_ptr<ModelAPI_ResultConstruction>(new Model_ResultConstruction);
1100 storeResult(theFeatureData, aResult, theIndex);
1105 std::shared_ptr<ModelAPI_ResultBody> Model_Objects::createBody(
1106 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex)
1108 TDF_Label aLab = resultLabel(theFeatureData, theIndex);
1109 // for feature create compsolid, but for result sub create body:
1110 // only one level of recursion is supported now
1111 ResultPtr aResultOwner = std::dynamic_pointer_cast<ModelAPI_Result>(theFeatureData->owner());
1112 ObjectPtr anOldObject;
1113 if (aResultOwner.get()) {
1114 TDataStd_Comment::Set(aLab, ModelAPI_ResultBody::group().c_str());
1115 } else { // in compsolid (higher level result) old object probably may be found
1116 TDataStd_Comment::Set(aLab, ModelAPI_ResultCompSolid::group().c_str());
1117 anOldObject = object(aLab);
1119 std::shared_ptr<ModelAPI_ResultBody> aResult;
1120 if (anOldObject.get()) {
1121 aResult = std::dynamic_pointer_cast<ModelAPI_ResultBody>(anOldObject);
1123 if (!aResult.get()) {
1124 // create compsolid anyway; if it is compsolid, it will create sub-bodies internally
1125 if (aResultOwner.get()) {
1126 aResult = std::shared_ptr<ModelAPI_ResultBody>(new Model_ResultBody);
1128 aResult = std::shared_ptr<ModelAPI_ResultBody>(new Model_ResultCompSolid);
1130 storeResult(theFeatureData, aResult, theIndex);
1135 std::shared_ptr<ModelAPI_ResultPart> Model_Objects::createPart(
1136 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex)
1138 TDF_Label aLab = resultLabel(theFeatureData, theIndex);
1139 TDataStd_Comment::Set(aLab, ModelAPI_ResultPart::group().c_str());
1140 ObjectPtr anOldObject = object(aLab);
1141 std::shared_ptr<ModelAPI_ResultPart> aResult;
1142 if (anOldObject.get()) {
1143 aResult = std::dynamic_pointer_cast<ModelAPI_ResultPart>(anOldObject);
1145 if (!aResult.get()) {
1146 aResult = std::shared_ptr<ModelAPI_ResultPart>(new Model_ResultPart);
1147 storeResult(theFeatureData, aResult, theIndex);
1152 std::shared_ptr<ModelAPI_ResultPart> Model_Objects::copyPart(
1153 const std::shared_ptr<ModelAPI_ResultPart>& theOrigin,
1154 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex)
1156 std::shared_ptr<ModelAPI_ResultPart> aResult = createPart(theFeatureData, theIndex);
1157 aResult->data()->reference(Model_ResultPart::BASE_REF_ID())->setValue(theOrigin);
1161 std::shared_ptr<ModelAPI_ResultGroup> Model_Objects::createGroup(
1162 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex)
1164 TDF_Label aLab = resultLabel(theFeatureData, theIndex);
1165 TDataStd_Comment::Set(aLab, ModelAPI_ResultGroup::group().c_str());
1166 ObjectPtr anOldObject = object(aLab);
1167 std::shared_ptr<ModelAPI_ResultGroup> aResult;
1168 if (anOldObject.get()) {
1169 aResult = std::dynamic_pointer_cast<ModelAPI_ResultGroup>(anOldObject);
1171 if (!aResult.get()) {
1172 aResult = std::shared_ptr<ModelAPI_ResultGroup>(new Model_ResultGroup(theFeatureData));
1173 storeResult(theFeatureData, aResult, theIndex);
1178 std::shared_ptr<ModelAPI_ResultField> Model_Objects::createField(
1179 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex)
1181 TDF_Label aLab = resultLabel(theFeatureData, theIndex);
1182 TDataStd_Comment::Set(aLab, ModelAPI_ResultField::group().c_str());
1183 ObjectPtr anOldObject = object(aLab);
1184 std::shared_ptr<ModelAPI_ResultField> aResult;
1185 if (anOldObject.get()) {
1186 aResult = std::dynamic_pointer_cast<ModelAPI_ResultField>(anOldObject);
1188 if (!aResult.get()) {
1189 aResult = std::shared_ptr<ModelAPI_ResultField>(new Model_ResultField(theFeatureData));
1190 storeResult(theFeatureData, aResult, theIndex);
1195 std::shared_ptr<ModelAPI_ResultParameter> Model_Objects::createParameter(
1196 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex)
1198 TDF_Label aLab = resultLabel(theFeatureData, theIndex);
1199 TDataStd_Comment::Set(aLab, ModelAPI_ResultParameter::group().c_str());
1200 ObjectPtr anOldObject = object(aLab);
1201 std::shared_ptr<ModelAPI_ResultParameter> aResult;
1202 if (anOldObject.get()) {
1203 aResult = std::dynamic_pointer_cast<ModelAPI_ResultParameter>(anOldObject);
1205 if (!aResult.get()) {
1206 aResult = std::shared_ptr<ModelAPI_ResultParameter>(new Model_ResultParameter);
1207 storeResult(theFeatureData, aResult, theIndex);
1212 std::shared_ptr<ModelAPI_Folder> Model_Objects::createFolder(
1213 const std::shared_ptr<ModelAPI_Feature>& theBeforeThis)
1215 FolderPtr aFolder(new ModelAPI_Folder);
1219 TDF_Label aFeaturesLab = featuresLabel();
1220 TDF_Label aFolderLab = aFeaturesLab.NewChild();
1221 // store feature in the features array: before "initData" because in macro features
1222 // in initData it creates new features, appeared later than this
1223 TDF_Label aPrevFeatureLab;
1224 if (theBeforeThis.get()) { // searching for the previous feature label
1225 std::shared_ptr<Model_Data> aPrevData =
1226 std::dynamic_pointer_cast<Model_Data>(theBeforeThis->data());
1227 if (aPrevData.get()) {
1228 aPrevFeatureLab = nextLabel(aPrevData->label().Father(), true);
1230 } else { // find the label of the last feature
1231 Handle(TDataStd_ReferenceArray) aRefs;
1232 if (aFeaturesLab.FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs))
1233 aPrevFeatureLab = aRefs->Value(aRefs->Upper());
1235 AddToRefArray(aFeaturesLab, aFolderLab, aPrevFeatureLab);
1237 // keep the feature ID to restore document later correctly
1238 TDataStd_Comment::Set(aFolderLab, ModelAPI_Folder::ID().c_str());
1239 myFolders.Bind(aFolderLab, aFolder);
1240 // must be before the event sending: for OB the feature is already added
1241 updateHistory(ModelAPI_Folder::group());
1242 updateHistory(ModelAPI_Feature::group());
1244 // must be after binding to the map because of "Box" macro feature that
1245 // creates other features in "initData"
1246 initData(aFolder, aFolderLab, TAG_FEATURE_ARGUMENTS);
1247 // event: folder is added, must be before "initData" to update OB correctly on Duplicate:
1248 // first new part, then the content
1249 static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_CREATED);
1250 ModelAPI_EventCreator::get()->sendUpdated(aFolder, anEvent);
1255 std::shared_ptr<ModelAPI_Feature> Model_Objects::feature(
1256 const std::shared_ptr<ModelAPI_Result>& theResult)
1258 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(theResult->data());
1260 TDF_Label aFeatureLab = aData->label().Father().Father().Father();
1261 FeaturePtr aFeature = feature(aFeatureLab);
1262 if (!aFeature.get() && aFeatureLab.Depth() > 1) { // this may be sub-result of result
1263 aFeatureLab = aFeatureLab.Father().Father();
1264 aFeature = feature(aFeatureLab);
1268 return FeaturePtr();
1271 std::string Model_Objects::featureResultGroup(FeaturePtr theFeature)
1273 if (theFeature->data()->isValid()) {
1274 TDF_ChildIterator aLabIter(resultLabel(theFeature->data(), 0).Father());
1275 if (aLabIter.More()) {
1276 TDF_Label anArgLab = aLabIter.Value();
1277 Handle(TDataStd_Comment) aGroup;
1278 if (aLabIter.Value().FindAttribute(TDataStd_Comment::GetID(), aGroup)) {
1279 return TCollection_AsciiString(aGroup->Get()).ToCString();
1283 static std::string anEmpty;
1284 return anEmpty; // not found
1287 void Model_Objects::updateResults(FeaturePtr theFeature, std::set<FeaturePtr>& theProcessed)
1289 if (theProcessed.find(theFeature) != theProcessed.end())
1291 theProcessed.insert(theFeature);
1292 // for composites update subs recursively (sketch elements results are needed for the sketch)
1293 CompositeFeaturePtr aComp = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theFeature);
1294 if (aComp.get() && aComp->getKind() != "Part") { // don't go inside of parts sub-features
1295 // update subs of composites first
1296 int aSubNum = aComp->numberOfSubs();
1297 for(int a = 0; a < aSubNum; a++) {
1298 FeaturePtr aSub = aComp->subFeature(a);
1299 updateResults(aComp->subFeature(a), theProcessed);
1303 // for not persistent is will be done by parametric updater automatically
1304 //if (!theFeature->isPersistentResult()) return;
1305 // check the existing results and remove them if there is nothing on the label
1306 std::list<ResultPtr>::const_iterator aResIter = theFeature->results().cbegin();
1307 while(aResIter != theFeature->results().cend()) {
1308 ResultPtr aBody = std::dynamic_pointer_cast<ModelAPI_Result>(*aResIter);
1310 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(aBody->data());
1311 if (!aData.get() || !aData->isValid() || (!aBody->isDisabled() && aData->isDeleted())) {
1312 // found a disappeared result => remove it
1313 theFeature->eraseResultFromList(aBody);
1314 // start iterate from beginning because iterator is corrupted by removing
1315 aResIter = theFeature->results().cbegin();
1321 // it may be on undo
1322 if (!theFeature->data() || !theFeature->data()->isValid() || theFeature->isDisabled())
1324 // check that results are presented on all labels
1325 int aResSize = int(theFeature->results().size());
1326 TDF_ChildIterator aLabIter(resultLabel(theFeature->data(), 0).Father());
1327 for(; aLabIter.More(); aLabIter.Next()) {
1328 // here must be GUID of the feature
1329 int aResIndex = aLabIter.Value().Tag() - 1;
1331 if (aResSize <= aResIndex) {
1332 TDF_Label anArgLab = aLabIter.Value();
1333 Handle(TDataStd_Comment) aGroup;
1334 if (anArgLab.FindAttribute(TDataStd_Comment::GetID(), aGroup)) {
1335 if (aGroup->Get() == ModelAPI_ResultBody::group().c_str() ||
1336 aGroup->Get() == ModelAPI_ResultCompSolid::group().c_str()) {
1337 aNewBody = createBody(theFeature->data(), aResIndex);
1338 } else if (aGroup->Get() == ModelAPI_ResultPart::group().c_str()) {
1339 std::shared_ptr<ModelAPI_ResultPart> aNewP = createPart(theFeature->data(), aResIndex);
1340 theFeature->setResult(aNewP, aResIndex);
1341 if (!aNewP->partDoc().get())
1342 // create the part result: it is better to restore the previous result if it is possible
1343 theFeature->execute();
1344 } else if (aGroup->Get() == ModelAPI_ResultConstruction::group().c_str()) {
1345 theFeature->execute(); // construction shapes are needed for sketch solver
1346 } else if (aGroup->Get() == ModelAPI_ResultGroup::group().c_str()) {
1347 aNewBody = createGroup(theFeature->data(), aResIndex);
1348 } else if (aGroup->Get() == ModelAPI_ResultField::group().c_str()) {
1349 aNewBody = createField(theFeature->data(), aResIndex);
1350 } else if (aGroup->Get() == ModelAPI_ResultParameter::group().c_str()) {
1351 theFeature->attributeChanged("expression"); // just produce a value
1353 Events_InfoMessage("Model_Objects", "Unknown type of result is found in the document:")
1354 .arg(TCollection_AsciiString(aGroup->Get()).ToCString()).send();
1357 if (aNewBody && !aNewBody->data()->isDeleted()) {
1358 theFeature->setResult(aNewBody, aResIndex);
1364 ResultPtr Model_Objects::findByName(const std::string theName)
1367 FeaturePtr aResFeature; // keep feature to return the latest one
1368 NCollection_DataMap<TDF_Label, FeaturePtr>::Iterator anObjIter(myFeatures);
1369 for(; anObjIter.More(); anObjIter.Next()) {
1370 FeaturePtr& aFeature = anObjIter.ChangeValue();
1371 if (!aFeature.get() || aFeature->isDisabled()) // may be on close
1373 std::list<ResultPtr> allResults;
1374 ModelAPI_Tools::allResults(aFeature, allResults);
1375 std::list<ResultPtr>::iterator aRIter = allResults.begin();
1376 for (; aRIter != allResults.cend(); aRIter++) {
1377 ResultPtr aRes = *aRIter;
1378 if (aRes.get() && aRes->data() && aRes->data()->isValid() && !aRes->isDisabled() &&
1379 aRes->data()->name() == theName)
1381 if (!aResult.get() || isLater(aFeature, aResFeature)) { // select the latest
1383 aResFeature = aFeature;
1391 TDF_Label Model_Objects::nextLabel(TDF_Label theCurrent, const bool theReverse)
1393 Handle(TDataStd_ReferenceArray) aRefs;
1394 if (featuresLabel().FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
1395 for(int a = aRefs->Lower(); a <= aRefs->Upper(); a++) { // iterate all existing features
1396 TDF_Label aCurLab = aRefs->Value(a);
1397 if (aCurLab.IsEqual(theCurrent)) {
1398 a += theReverse ? -1 : 1;
1399 if (a >= aRefs->Lower() && a <= aRefs->Upper())
1400 return aRefs->Value(a);
1401 break; // finish iiteration: it's last feature
1408 FeaturePtr Model_Objects::nextFeature(FeaturePtr theCurrent, const bool theReverse)
1410 std::shared_ptr<Model_Data> aData = std::static_pointer_cast<Model_Data>(theCurrent->data());
1411 if (aData.get() && aData->isValid()) {
1412 TDF_Label aFeatureLabel = aData->label().Father();
1413 TDF_Label aNextLabel = nextLabel(aFeatureLabel, theReverse);
1414 if (!aNextLabel.IsNull())
1415 return feature(aNextLabel);
1417 return FeaturePtr(); // not found, last, or something is wrong
1420 FeaturePtr Model_Objects::firstFeature()
1422 Handle(TDataStd_ReferenceArray) aRefs;
1423 if (featuresLabel().FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
1424 return feature(aRefs->Value(aRefs->Lower()));
1426 return FeaturePtr(); // no features at all
1429 FeaturePtr Model_Objects::lastFeature()
1431 Handle(TDataStd_ReferenceArray) aRefs;
1432 if (featuresLabel().FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
1433 return feature(aRefs->Value(aRefs->Upper()));
1435 return FeaturePtr(); // no features at all
1438 bool Model_Objects::isLater(FeaturePtr theLater, FeaturePtr theCurrent) const
1440 std::shared_ptr<Model_Data> aLaterD = std::static_pointer_cast<Model_Data>(theLater->data());
1441 std::shared_ptr<Model_Data> aCurrentD = std::static_pointer_cast<Model_Data>(theCurrent->data());
1442 if (aLaterD.get() && aLaterD->isValid() && aCurrentD.get() && aCurrentD->isValid()) {
1443 TDF_Label aLaterL = aLaterD->label().Father();
1444 TDF_Label aCurrentL = aCurrentD->label().Father();
1445 int aLaterI = -1, aCurentI = -1; // not found yet state
1446 Handle(TDataStd_ReferenceArray) aRefs;
1447 if (featuresLabel().FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
1448 for(int a = aRefs->Lower(); a <= aRefs->Upper(); a++) { // iterate all existing features
1449 TDF_Label aCurLab = aRefs->Value(a);
1450 if (aCurLab.IsEqual(aLaterL)) {
1452 } else if (aCurLab.IsEqual(aCurrentL)) {
1455 if (aLaterI != -1 && aCurentI != -1) // both are found
1456 return aLaterI > aCurentI;
1460 return false; // not found, or something is wrong
1463 std::list<std::shared_ptr<ModelAPI_Object> > Model_Objects::allObjects()
1465 std::list<std::shared_ptr<ModelAPI_Object> > aResult;
1466 Handle(TDataStd_ReferenceArray) aRefs;
1467 if (featuresLabel().FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
1468 for(int a = aRefs->Lower(); a <= aRefs->Upper(); a++) {
1469 ObjectPtr anObject = object(aRefs->Value(a));
1470 if (!anObject.get()) // is it a folder?
1471 anObject = folder(aRefs->Value(a));
1473 aResult.push_back(anObject);
1479 std::list<std::shared_ptr<ModelAPI_Feature> > Model_Objects::allFeatures()
1481 std::list<std::shared_ptr<ModelAPI_Feature> > aResult;
1482 Handle(TDataStd_ReferenceArray) aRefs;
1483 if (featuresLabel().FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
1484 for(int a = aRefs->Lower(); a <= aRefs->Upper(); a++) {
1485 FeaturePtr aFeature = feature(aRefs->Value(a));
1487 aResult.push_back(aFeature);
1493 int Model_Objects::numInternalFeatures()
1495 Handle(TDataStd_ReferenceArray) aRefs;
1496 if (featuresLabel().FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
1497 return aRefs->Upper() - aRefs->Lower() + 1;
1499 return 0; // invalid
1502 std::shared_ptr<ModelAPI_Feature> Model_Objects::internalFeature(const int theIndex)
1504 Handle(TDataStd_ReferenceArray) aRefs;
1505 if (featuresLabel().FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
1506 return feature(aRefs->Value(aRefs->Lower() + theIndex));
1508 return FeaturePtr(); // invalid
1511 Standard_Integer HashCode(const TDF_Label& theLab, const Standard_Integer theUpper)
1513 return TDF_LabelMapHasher::HashCode(theLab, theUpper);
1516 Standard_Boolean IsEqual(const TDF_Label& theLab1, const TDF_Label& theLab2)
1518 return TDF_LabelMapHasher::IsEqual(theLab1, theLab2);