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 int TAG_OBJECTS = 2; // tag of the objects sub-tree (features, results)
55 static const int TAG_FEATURE_ARGUMENTS = 1; ///< where the arguments are located
56 static const int TAG_FEATURE_RESULTS = 2; ///< where the results are located
59 /// 0:1:2 - where features are located
60 /// 0:1:2:N:1 - data of the feature N
61 /// 0:1:2:N:2:K:1 - data of the K result of the feature N
63 Model_Objects::Model_Objects(TDF_Label theMainLab) : myMain(theMainLab)
67 void Model_Objects::setOwner(DocumentPtr theDoc)
70 // update all fields and recreate features and result objects if needed
71 TDF_LabelList aNoUpdated;
72 synchronizeFeatures(aNoUpdated, true, true, true, true);
76 Model_Objects::~Model_Objects()
78 // delete all features of this document
79 Events_Loop* aLoop = Events_Loop::loop();
80 // erase one by one to avoid access from the feature destructor itself from he map
81 // blocks the flush signals to avoid the temporary objects visualization in the viewer
82 // they should not be shown in order to do not lose highlight by erasing them
83 bool isActive = aLoop->activateFlushes(false);
85 while(!myFeatures.IsEmpty()) {
86 NCollection_DataMap<TDF_Label, FeaturePtr>::Iterator aFeaturesIter(myFeatures);
87 FeaturePtr aFeature = aFeaturesIter.Value();
88 static Events_ID EVENT_DISP = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
89 ModelAPI_EventCreator::get()->sendDeleted(myDoc, ModelAPI_Feature::group());
90 ModelAPI_EventCreator::get()->sendUpdated(aFeature, EVENT_DISP);
91 aFeature->removeResults(0, false);
93 myFeatures.UnBind(aFeaturesIter.Key());
96 aLoop->activateFlushes(isActive);
97 // erase update, because features are destroyed and update should not performed for them anywhere
98 aLoop->eraseMessages(Events_Loop::eventByName(EVENT_OBJECT_CREATED));
99 aLoop->eraseMessages(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
100 // deleted and redisplayed is correctly performed: they know that features are destroyed
101 aLoop->flush(Events_Loop::eventByName(EVENT_OBJECT_DELETED));
102 aLoop->flush(Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
106 /// Appends to the array of references a new referenced label
107 static void AddToRefArray(TDF_Label& theArrayLab, TDF_Label& theReferenced, TDF_Label& thePrevLab)
109 Handle(TDataStd_ReferenceArray) aRefs;
110 if (!theArrayLab.FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
111 aRefs = TDataStd_ReferenceArray::Set(theArrayLab, 0, 0);
112 aRefs->SetValue(0, theReferenced);
113 } else { // extend array by one more element
114 Handle(TDataStd_HLabelArray1) aNewArray = new TDataStd_HLabelArray1(aRefs->Lower(),
116 int aPassedPrev = 0; // prev feature is found and passed
117 if (thePrevLab.IsNull()) { // null means that inserted feature must be the first
118 aNewArray->SetValue(aRefs->Lower(), theReferenced);
121 for (int a = aRefs->Lower(); a <= aRefs->Upper(); a++) {
122 aNewArray->SetValue(a + aPassedPrev, aRefs->Value(a));
123 if (!aPassedPrev && aRefs->Value(a).IsEqual(thePrevLab)) {
125 aNewArray->SetValue(a + 1, theReferenced);
128 if (!aPassedPrev) // not found: unknown situation
129 aNewArray->SetValue(aRefs->Upper() + 1, theReferenced);
130 aRefs->SetInternalArray(aNewArray);
134 void Model_Objects::addFeature(FeaturePtr theFeature, const FeaturePtr theAfterThis)
136 if (!theFeature->isAction()) { // do not add action to the data model
137 TDF_Label aFeaturesLab = featuresLabel();
138 TDF_Label aFeatureLab = aFeaturesLab.NewChild();
139 // store feature in the features array: before "initData" because in macro features
140 // in initData it creates new features, appeared later than this
141 TDF_Label aPrevFeateureLab;
142 if (theAfterThis.get()) { // searching for the previous feature label
143 std::shared_ptr<Model_Data> aPrevData =
144 std::dynamic_pointer_cast<Model_Data>(theAfterThis->data());
145 if (aPrevData.get()) {
146 aPrevFeateureLab = aPrevData->label().Father();
149 AddToRefArray(aFeaturesLab, aFeatureLab, aPrevFeateureLab);
151 // keep the feature ID to restore document later correctly
152 TDataStd_Comment::Set(aFeatureLab, theFeature->getKind().c_str());
153 myFeatures.Bind(aFeatureLab, theFeature);
154 // must be before the event sending: for OB the feature is already added
155 updateHistory(ModelAPI_Feature::group());
156 // do not change the order:
159 // during python script with fillet constraint feature data should be
160 // initialized before using it in GUI
162 // must be after binding to the map because of "Box" macro feature that
163 // creates other features in "initData"
164 initData(theFeature, aFeatureLab, TAG_FEATURE_ARGUMENTS);
165 // event: feature is added, mist be before "initData" to update OB correctly on Duplicate:
166 // first new part, then the content
167 static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_CREATED);
168 ModelAPI_EventCreator::get()->sendUpdated(theFeature, anEvent);
169 } else { // make feature has not-null data anyway
170 theFeature->setData(Model_Data::invalidData());
171 theFeature->setDoc(myDoc);
175 /// Appends to the array of references a new referenced label.
176 /// If theIndex is not -1, removes element at this index, not theReferenced.
177 /// \returns the index of removed element
178 static int RemoveFromRefArray(TDF_Label theArrayLab, TDF_Label theReferenced,
179 const int theIndex = -1)
181 int aResult = -1; // no returned
182 Handle(TDataStd_ReferenceArray) aRefs;
183 if (theArrayLab.FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
184 if (aRefs->Length() == 1) { // just erase an array
185 if ((theIndex == -1 && aRefs->Value(0) == theReferenced) || theIndex == 0) {
186 theArrayLab.ForgetAttribute(TDataStd_ReferenceArray::GetID());
189 } else { // reduce the array
190 Handle(TDataStd_HLabelArray1) aNewArray = new TDataStd_HLabelArray1(aRefs->Lower(),
192 int aCount = aRefs->Lower();
193 for (int a = aCount; a <= aRefs->Upper(); a++, aCount++) {
194 if ((theIndex == -1 && aRefs->Value(a) == theReferenced) || theIndex == a) {
198 aNewArray->SetValue(aCount, aRefs->Value(a));
201 aRefs->SetInternalArray(aNewArray);
207 void Model_Objects::refsToFeature(FeaturePtr theFeature,
208 std::set<std::shared_ptr<ModelAPI_Feature> >& theRefs, const bool isSendError)
210 // check the feature: it must have no depended objects on it
211 // the dependencies can be in the feature results
212 std::list<ResultPtr> aResults;
213 ModelAPI_Tools::allResults(theFeature, aResults);
214 std::list<ResultPtr>::const_iterator aResIter = aResults.cbegin();
215 for (; aResIter != aResults.cend(); aResIter++) {
216 ResultPtr aResult = (*aResIter);
217 std::shared_ptr<Model_Data> aData =
218 std::dynamic_pointer_cast<Model_Data>(aResult->data());
219 if (aData.get() != NULL) {
220 const std::set<AttributePtr>& aRefs = aData->refsToMe();
221 std::set<AttributePtr>::const_iterator aRefIt = aRefs.begin(), aRefLast = aRefs.end();
222 for (; aRefIt != aRefLast; aRefIt++) {
223 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>((*aRefIt)->owner());
224 if (aFeature.get() != NULL)
225 theRefs.insert(aFeature);
229 // the dependencies can be in the feature itself
230 std::shared_ptr<Model_Data> aData =
231 std::dynamic_pointer_cast<Model_Data>(theFeature->data());
232 if (aData.get() && !aData->refsToMe().empty()) {
233 const std::set<AttributePtr>& aRefs = aData->refsToMe();
234 std::set<AttributePtr>::const_iterator aRefIt = aRefs.begin(), aRefLast = aRefs.end();
235 for (; aRefIt != aRefLast; aRefIt++) {
236 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>((*aRefIt)->owner());
237 if (aFeature.get() != NULL)
238 theRefs.insert(aFeature);
242 if (!theRefs.empty() && isSendError) {
243 Events_InfoMessage("Model_Objects",
244 "Feature '%1' is used and can not be deleted").arg(theFeature->data()->name()).send();
248 void Model_Objects::removeFeature(FeaturePtr theFeature)
250 std::shared_ptr<Model_Data> aData = std::static_pointer_cast<Model_Data>(theFeature->data());
251 if (aData.get() && aData->isValid()) {
252 // checking that the sub-element of composite feature is removed: if yes, inform the owner
253 std::set<std::shared_ptr<ModelAPI_Feature> > aRefs;
254 refsToFeature(theFeature, aRefs, false);
255 std::set<std::shared_ptr<ModelAPI_Feature> >::iterator aRefIter = aRefs.begin();
256 for(; aRefIter != aRefs.end(); aRefIter++) {
257 std::shared_ptr<ModelAPI_CompositeFeature> aComposite =
258 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(*aRefIter);
259 if (aComposite.get() && aComposite->isSub(theFeature)) {
260 aComposite->removeFeature(theFeature);
263 // this must be before erase since theFeature erasing removes all information about
264 // the feature results and groups of results
265 // To reproduce: create sketch, extrusion, remove sketch => constructions tree is not updated
266 clearHistory(theFeature);
270 TDF_Label aFeatureLabel = aData->label().Father();
271 if (myFeatures.IsBound(aFeatureLabel))
272 myFeatures.UnBind(aFeatureLabel);
274 static Events_ID EVENT_DISP = Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
275 ModelAPI_EventCreator::get()->sendUpdated(theFeature, EVENT_DISP);
276 // erase all attributes under the label of feature
277 aFeatureLabel.ForgetAllAttributes();
278 // remove it from the references array
279 RemoveFromRefArray(featuresLabel(), aFeatureLabel);
280 // event: feature is deleted
281 ModelAPI_EventCreator::get()->sendDeleted(theFeature->document(), ModelAPI_Feature::group());
282 updateHistory(ModelAPI_Feature::group());
286 void Model_Objects::eraseAllFeatures()
288 static Events_ID kDispEvent = Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
289 static const ModelAPI_EventCreator* kCreator = ModelAPI_EventCreator::get();
290 // make all features invalid (like deleted)
291 NCollection_DataMap<TDF_Label, FeaturePtr>::Iterator aFIter(myFeatures);
292 for(; aFIter.More(); aFIter.Next()) {
293 FeaturePtr aFeature = aFIter.Value();
294 std::list<ResultPtr> aResList;
295 ModelAPI_Tools::allResults(aFeature, aResList);
296 std::list<ResultPtr>::iterator aRIter = aResList.begin();
297 for(; aRIter != aResList.end(); aRIter++) {
298 ResultPtr aRes = *aRIter;
299 if (aRes && aRes->data()->isValid()) {
300 kCreator->sendDeleted(myDoc, aRes->groupName());
301 kCreator->sendUpdated(aRes, kDispEvent);
302 aRes->setData(aRes->data()->invalidPtr());
306 kCreator->sendUpdated(aFeature, kDispEvent);
307 aFeature->setData(aFeature->data()->invalidPtr());
309 kCreator->sendDeleted(myDoc, ModelAPI_Feature::group());
310 myFeatures.Clear(); // just remove features without modification of DS
311 updateHistory(ModelAPI_Feature::group());
314 void Model_Objects::moveFeature(FeaturePtr theMoved, FeaturePtr theAfterThis)
316 TDF_Label aFeaturesLab = featuresLabel();
317 Handle(TDataStd_ReferenceArray) aRefs;
318 if (!aFeaturesLab.FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs))
320 TDF_Label anAfterLab, aMovedLab =
321 std::dynamic_pointer_cast<Model_Data>(theMoved->data())->label().Father();
322 if (theAfterThis.get())
323 anAfterLab = std::dynamic_pointer_cast<Model_Data>(theAfterThis->data())->label().Father();
325 Handle(TDataStd_HLabelArray1) aNewArray =
326 new TDataStd_HLabelArray1(aRefs->Lower(), aRefs->Upper());
327 int aPassedMovedFrom = 0; // the prev feature location is found and passed
328 int aPassedMovedTo = 0; // the feature is added and this location is passed
329 if (!theAfterThis.get()) { // null means that inserted feature must be the first
330 aNewArray->SetValue(aRefs->Lower(), aMovedLab);
333 for (int a = aRefs->Lower(); a <= aRefs->Upper(); a++) {
334 if (aPassedMovedTo == 0 && aRefs->Value(a) == anAfterLab) { // add two
336 aNewArray->SetValue(a - aPassedMovedFrom, anAfterLab);
337 if (a + 1 - aPassedMovedFrom <= aRefs->Upper())
338 aNewArray->SetValue(a + 1 - aPassedMovedFrom, aMovedLab);
339 } else if (aPassedMovedFrom == 0 && aRefs->Value(a) == aMovedLab) { // skip
341 } else { // just copy one
342 if (a - aPassedMovedFrom + aPassedMovedTo <= aRefs->Upper())
343 aNewArray->SetValue(a - aPassedMovedFrom + aPassedMovedTo, aRefs->Value(a));
346 if (!aPassedMovedFrom || !aPassedMovedTo) {// not found: unknown situation
347 if (!aPassedMovedFrom) {
348 static std::string aMovedFromError("The moved feature is not found");
349 Events_InfoMessage("Model_Objects", aMovedFromError).send();
351 static std::string aMovedToError("The 'after' feature for movement is not found");
352 Events_InfoMessage("Model_Objects", aMovedToError).send();
356 // store the new array
357 aRefs->SetInternalArray(aNewArray);
358 // update the feature and the history
359 clearHistory(theMoved);
360 // make sure all (selection) attributes of moved feature will be updated
361 static Events_ID kUpdateSelection = Events_Loop::loop()->eventByName(EVENT_UPDATE_SELECTION);
362 ModelAPI_EventCreator::get()->sendUpdated(theMoved, kUpdateSelection, false);
363 ModelAPI_EventCreator::get()->sendReordered(theMoved);
366 void Model_Objects::clearHistory(ObjectPtr theObj)
369 const std::string aGroup = theObj->groupName();
370 std::map<std::string, std::vector<ObjectPtr> >::iterator aHIter = myHistory.find(aGroup);
371 if (aHIter != myHistory.end())
372 myHistory.erase(aHIter); // erase from map => this means that it is not synchronized
373 if (theObj->groupName() == ModelAPI_Feature::group()) { // clear results group of the feature
374 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObj);
375 std::string aResultGroup = featureResultGroup(aFeature);
376 if (!aResultGroup.empty()) {
377 std::map<std::string, std::vector<ObjectPtr> >::iterator aHIter =
378 myHistory.find(aResultGroup);
379 if (aHIter != myHistory.end())
380 myHistory.erase(aHIter); // erase from map => this means that it is not synchronized
386 void Model_Objects::createHistory(const std::string& theGroupID)
388 std::map<std::string, std::vector<ObjectPtr> >::iterator aHIter = myHistory.find(theGroupID);
389 if (aHIter == myHistory.end()) {
390 std::vector<ObjectPtr> aResult = std::vector<ObjectPtr>();
391 // iterate the array of references and get feature by feature from the array
392 bool isFeature = theGroupID == ModelAPI_Feature::group();
393 Handle(TDataStd_ReferenceArray) aRefs;
394 if (featuresLabel().FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
395 for(int a = aRefs->Lower(); a <= aRefs->Upper(); a++) {
396 FeaturePtr aFeature = feature(aRefs->Value(a));
397 if (aFeature.get()) {
398 // if feature is in sub-component, remove it from history:
399 // it is in sub-tree of sub-component
400 bool isSub = ModelAPI_Tools::compositeOwner(aFeature).get() != NULL;
401 if (isFeature) { // here may be also disabled features
402 if (!isSub && aFeature->isInHistory()) {
403 aResult.push_back(aFeature);
405 } else if (!aFeature->isDisabled()) { // iterate all results of not-disabled feature
406 // construction results of sub-features should not be in the tree
407 if (!isSub || theGroupID != ModelAPI_ResultConstruction::group()) {
408 // do not use reference to the list here since results can be changed by "isConcealed"
409 const std::list<std::shared_ptr<ModelAPI_Result> > aResults = aFeature->results();
410 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator
411 aRIter = aResults.begin();
412 for (; aRIter != aResults.cend(); aRIter++) {
413 ResultPtr aRes = *aRIter;
414 if (aRes->groupName() != theGroupID) break; // feature have only same group results
415 if (!aRes->isDisabled() && aRes->isInHistory() && !aRes->isConcealed()) {
416 aResult.push_back(*aRIter);
424 // to be sure that isConcealed did not update the history (issue 1089) during the iteration
425 if (myHistory.find(theGroupID) == myHistory.end())
426 myHistory[theGroupID] = aResult;
430 void Model_Objects::updateHistory(const std::shared_ptr<ModelAPI_Object> theObject)
432 clearHistory(theObject);
435 void Model_Objects::updateHistory(const std::string theGroup)
437 std::map<std::string, std::vector<ObjectPtr> >::iterator aHIter = myHistory.find(theGroup);
438 if (aHIter != myHistory.end())
439 myHistory.erase(aHIter); // erase from map => this means that it is not synchronized
442 FeaturePtr Model_Objects::feature(TDF_Label theLabel) const
444 if (myFeatures.IsBound(theLabel))
445 return myFeatures.Find(theLabel);
446 return FeaturePtr(); // not found
449 ObjectPtr Model_Objects::object(TDF_Label theLabel)
451 // try feature by label
452 FeaturePtr aFeature = feature(theLabel);
454 return feature(theLabel);
455 TDF_Label aFeatureLabel = theLabel.Father().Father(); // let's suppose it is result
456 aFeature = feature(aFeatureLabel);
457 bool isSubResult = false;
458 if (!aFeature.get() && aFeatureLabel.Depth() > 1) { // let's suppose this is sub-result of result
459 aFeatureLabel = aFeatureLabel.Father().Father();
460 aFeature = feature(aFeatureLabel);
463 if (aFeature.get()) {
464 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aFeature->results();
465 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.cbegin();
466 for (; aRIter != aResults.cend(); aRIter++) {
468 ResultCompSolidPtr aCompRes = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(*aRIter);
469 if (aCompRes.get()) {
470 int aNumSubs = aCompRes->numberOfSubs();
471 for(int a = 0; a < aNumSubs; a++) {
472 ResultPtr aSub = aCompRes->subResult(a);
474 std::shared_ptr<Model_Data> aSubData = std::dynamic_pointer_cast<Model_Data>(
476 if (aSubData->label().Father().IsEqual(theLabel))
482 std::shared_ptr<Model_Data> aResData = std::dynamic_pointer_cast<Model_Data>(
484 if (aResData->label().Father().IsEqual(theLabel))
489 return FeaturePtr(); // not found
492 ObjectPtr Model_Objects::object(const std::string& theGroupID, const int theIndex)
496 createHistory(theGroupID);
497 return myHistory[theGroupID][theIndex];
500 std::shared_ptr<ModelAPI_Object> Model_Objects::objectByName(
501 const std::string& theGroupID, const std::string& theName)
503 createHistory(theGroupID);
504 if (theGroupID == ModelAPI_Feature::group()) { // searching among features (in history or not)
505 std::list<std::shared_ptr<ModelAPI_Feature> > allObjs = allFeatures();
506 std::list<std::shared_ptr<ModelAPI_Feature> >::iterator anObjIter = allObjs.begin();
507 for(; anObjIter != allObjs.end(); anObjIter++) {
508 if ((*anObjIter)->data()->name() == theName)
511 } else { // searching among results (concealed or not)
512 std::list<std::shared_ptr<ModelAPI_Feature> > allObjs = allFeatures();
513 std::list<std::shared_ptr<ModelAPI_Feature> >::iterator anObjIter = allObjs.begin();
514 for(; anObjIter != allObjs.end(); anObjIter++) {
515 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = (*anObjIter)->results();
516 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.cbegin();
517 for (; aRIter != aResults.cend(); aRIter++) {
518 if (aRIter->get() && (*aRIter)->groupName() == theGroupID) {
519 if ((*aRIter)->data()->name() == theName)
521 ResultCompSolidPtr aCompRes =
522 std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(*aRIter);
523 if (aCompRes.get()) {
524 int aNumSubs = aCompRes->numberOfSubs();
525 for(int a = 0; a < aNumSubs; a++) {
526 ResultPtr aSub = aCompRes->subResult(a);
527 if (aSub.get() && aSub->groupName() == theGroupID) {
528 if (aSub->data()->name() == theName)
541 const int Model_Objects::index(std::shared_ptr<ModelAPI_Object> theObject)
543 std::string aGroup = theObject->groupName();
544 createHistory(aGroup);
545 std::vector<ObjectPtr>& allObjs = myHistory[aGroup];
546 std::vector<ObjectPtr>::iterator anObjIter = allObjs.begin(); // iterate to search object
547 for(int anIndex = 0; anObjIter != allObjs.end(); anObjIter++, anIndex++) {
548 if ((*anObjIter) == theObject)
555 int Model_Objects::size(const std::string& theGroupID)
557 createHistory(theGroupID);
558 return int(myHistory[theGroupID].size());
561 void Model_Objects::allResults(const std::string& theGroupID, std::list<ResultPtr>& theResults)
563 // iterate the array of references and get feature by feature from the array
564 Handle(TDataStd_ReferenceArray) aRefs;
565 if (featuresLabel().FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
566 for(int a = aRefs->Lower(); a <= aRefs->Upper(); a++) {
567 FeaturePtr aFeature = feature(aRefs->Value(a));
568 if (aFeature.get()) {
569 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aFeature->results();
570 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
571 for (; aRIter != aResults.cend(); aRIter++) {
572 ResultPtr aRes = *aRIter;
573 if (aRes->groupName() != theGroupID) break; // feature have only same group results
574 // iterate also concealed: ALL RESULTS (for translation parts undo/redo management)
575 //if (aRes->isInHistory() && !aRes->isConcealed()) {
576 theResults.push_back(*aRIter);
585 TDF_Label Model_Objects::featuresLabel() const
587 return myMain.FindChild(TAG_OBJECTS);
590 void Model_Objects::setUniqueName(FeaturePtr theFeature)
592 if (!theFeature->data()->name().empty())
593 return; // not needed, name is already defined
594 std::string aName; // result
595 // first count all features of such kind to start with index = count + 1
596 int aNumObjects = -1; // this feature is already in this map
597 NCollection_DataMap<TDF_Label, FeaturePtr>::Iterator aFIter(myFeatures);
598 for (; aFIter.More(); aFIter.Next()) {
599 if (aFIter.Value()->getKind() == theFeature->getKind())
602 // generate candidate name
603 std::stringstream aNameStream;
604 aNameStream << theFeature->getKind() << "_" << aNumObjects + 1;
605 aName = aNameStream.str();
606 // check this is unique, if not, increase index by 1
607 for (aFIter.Initialize(myFeatures); aFIter.More();) {
608 FeaturePtr aFeature = aFIter.Value();
609 bool isSameName = aFeature->data()->name() == aName;
610 if (!isSameName) { // check also results to avoid same results names (actual for Parts)
611 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aFeature->results();
612 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
613 for (; aRIter != aResults.cend(); aRIter++) {
614 isSameName = (*aRIter)->data()->name() == aName;
620 std::stringstream aNameStream;
621 aNameStream << theFeature->getKind() << "_" << aNumObjects + 1;
622 aName = aNameStream.str();
623 // reinitialize iterator to make sure a new name is unique
624 aFIter.Initialize(myFeatures);
628 theFeature->data()->setName(aName);
631 void Model_Objects::initData(ObjectPtr theObj, TDF_Label theLab, const int theTag)
633 std::shared_ptr<Model_Data> aData(new Model_Data);
634 aData->setLabel(theLab.FindChild(theTag));
635 aData->setObject(theObj);
636 theObj->setDoc(myDoc);
637 theObj->setData(aData);
638 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObj);
639 if (aFeature.get()) {
640 setUniqueName(aFeature); // must be before "initAttributes" because duplicate part uses name
642 theObj->initAttributes();
645 std::shared_ptr<ModelAPI_Feature> Model_Objects::featureById(const int theId)
648 TDF_Label aLab = featuresLabel().FindChild(theId, Standard_False);
649 return feature(aLab);
651 return std::shared_ptr<ModelAPI_Feature>(); // not found
654 void Model_Objects::synchronizeFeatures(
655 const TDF_LabelList& theUpdated, const bool theUpdateReferences,
656 const bool theExecuteFeatures, const bool theOpen, const bool theFlush)
658 Model_Document* anOwner = std::dynamic_pointer_cast<Model_Document>(myDoc).get();
659 if (!anOwner) // this may happen on creation of document: nothing there, so nothing to synchronize
661 // after all updates, sends a message that groups of features were created or updated
662 Events_Loop* aLoop = Events_Loop::loop();
663 static Events_ID aDispEvent = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
664 static Events_ID aCreateEvent = Events_Loop::eventByName(EVENT_OBJECT_CREATED);
665 static Events_ID anUpdateEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
666 static Events_ID aRedispEvent = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
667 static Events_ID aDeleteEvent = Events_Loop::eventByName(EVENT_OBJECT_DELETED);
668 static Events_ID aToHideEvent = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
669 bool isActive = aLoop->activateFlushes(false);
671 // collect all updated labels map
672 TDF_LabelMap anUpdatedMap;
673 TDF_ListIteratorOfLabelList anUpdatedIter(theUpdated);
674 for(; anUpdatedIter.More(); anUpdatedIter.Next()) {
675 TDF_Label& aFeatureLab = anUpdatedIter.Value();
676 while(aFeatureLab.Depth() > 3)
677 aFeatureLab = aFeatureLab.Father();
678 if (myFeatures.IsBound(aFeatureLab))
679 anUpdatedMap.Add(aFeatureLab);
682 // update all objects by checking are they on labels or not
683 std::set<FeaturePtr> aNewFeatures, aKeptFeatures;
684 TDF_ChildIDIterator aLabIter(featuresLabel(), TDataStd_Comment::GetID());
685 for (; aLabIter.More(); aLabIter.Next()) {
686 TDF_Label aFeatureLabel = aLabIter.Value()->Label();
688 if (!myFeatures.IsBound(aFeatureLabel)) { // a new feature is inserted
690 aFeature = std::dynamic_pointer_cast<Model_Session>(ModelAPI_Session::get())->createFeature(
691 TCollection_AsciiString(Handle(TDataStd_Comment)::DownCast(aLabIter.Value())->Get())
692 .ToCString(), anOwner);
693 if (!aFeature.get()) {
694 // somethig is wrong, most probably, the opened document has invalid structure
695 Events_InfoMessage("Model_Objects", "Invalid type of object in the document").send();
696 aLabIter.Value()->Label().ForgetAllAttributes();
700 // this must be before "setData" to redo the sketch line correctly
701 myFeatures.Bind(aFeatureLabel, aFeature);
702 aNewFeatures.insert(aFeature);
703 initData(aFeature, aFeatureLabel, TAG_FEATURE_ARGUMENTS);
704 updateHistory(aFeature);
706 // event: model is updated
707 ModelAPI_EventCreator::get()->sendUpdated(aFeature, aCreateEvent);
708 } else { // nothing is changed, both iterators are incremented
709 aFeature = myFeatures.Find(aFeatureLabel);
710 aKeptFeatures.insert(aFeature);
711 if (anUpdatedMap.Contains(aFeatureLabel)) {
712 if (!theOpen) { // on abort/undo/redo reinitialize attributes if something is changed
713 std::list<std::shared_ptr<ModelAPI_Attribute> > anAttrs =
714 aFeature->data()->attributes("");
715 std::list<std::shared_ptr<ModelAPI_Attribute> >::iterator anAttr = anAttrs.begin();
716 for(; anAttr != anAttrs.end(); anAttr++)
719 ModelAPI_EventCreator::get()->sendUpdated(aFeature, anUpdateEvent);
720 if (aFeature->getKind() == "Parameter") {
721 // if parameters are changed, update the results (issue 937)
722 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aFeature->results();
723 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
724 for (; aRIter != aResults.cend(); aRIter++) {
725 std::shared_ptr<ModelAPI_Result> aRes = *aRIter;
726 if (aRes->data()->isValid() && !aRes->isDisabled()) {
727 ModelAPI_EventCreator::get()->sendUpdated(aRes, anUpdateEvent);
735 // check all features are checked: if not => it was removed
736 NCollection_DataMap<TDF_Label, FeaturePtr>::Iterator aFIter(myFeatures);
737 while (aFIter.More()) {
738 if (aKeptFeatures.find(aFIter.Value()) == aKeptFeatures.end()
739 && aNewFeatures.find(aFIter.Value()) == aNewFeatures.end()) {
740 FeaturePtr aFeature = aFIter.Value();
741 // event: model is updated
742 //if (aFeature->isInHistory()) {
743 ModelAPI_EventCreator::get()->sendDeleted(myDoc, ModelAPI_Feature::group());
745 // results of this feature must be redisplayed (hided)
746 // redisplay also removed feature (used for sketch and AISObject)
747 ModelAPI_EventCreator::get()->sendUpdated(aFeature, aRedispEvent);
748 updateHistory(aFeature);
751 // unbind after the "erase" call: on abort sketch
752 // is removes sub-objects that corrupts aFIter
753 myFeatures.UnBind(aFIter.Key());
754 // reinitialize iterator because unbind may corrupt the previous order in the map
755 aFIter.Initialize(myFeatures);
760 if (theUpdateReferences) {
761 synchronizeBackRefs();
763 // update results of the features (after features created because
764 // they may be connected, like sketch and sub elements)
765 // After synchronisation of back references because sketch
766 // must be set in sub-elements before "execute" by updateResults
767 std::set<FeaturePtr> aProcessed; // composites must be updated after their subs (issue 360)
768 TDF_ChildIDIterator aLabIter2(featuresLabel(), TDataStd_Comment::GetID());
769 for (; aLabIter2.More(); aLabIter2.Next()) {
770 TDF_Label aFeatureLabel = aLabIter2.Value()->Label();
771 if (myFeatures.IsBound(aFeatureLabel)) { // a new feature is inserted
772 FeaturePtr aFeature = myFeatures.Find(aFeatureLabel);
773 updateResults(aFeature, aProcessed);
776 // the synchronize should be done after updateResults
777 // in order to correct back references of updated results
778 if (theUpdateReferences) {
779 synchronizeBackRefs();
781 if (!theUpdated.IsEmpty()) {
782 // this means there is no control what was modified => remove history cash
786 if (theExecuteFeatures)
787 anOwner->executeFeatures() = false;
788 aLoop->activateFlushes(isActive);
791 aLoop->flush(aDeleteEvent);
792 // delete should be emitted before create to reacts to aborted feature
793 aLoop->flush(aCreateEvent);
794 aLoop->flush(anUpdateEvent);
795 aLoop->flush(aCreateEvent); // after update of features, there could be results created
796 aLoop->flush(aDeleteEvent); // or deleted
797 aLoop->flush(aRedispEvent);
798 aLoop->flush(aToHideEvent);
800 if (theExecuteFeatures)
801 anOwner->executeFeatures() = true;
804 /// synchronises back references for the given object basing on the collected data
805 void Model_Objects::synchronizeBackRefsForObject(const std::set<AttributePtr>& theNewRefs,
808 if (!theObject.get() || !theObject->data()->isValid())
810 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(theObject->data());
811 // iterate new list to compare with curent
812 std::set<AttributePtr>::iterator aNewIter = theNewRefs.begin();
813 for(; aNewIter != theNewRefs.end(); aNewIter++) {
814 if (aData->refsToMe().find(*aNewIter) == aData->refsToMe().end()) {
815 FeaturePtr aRefFeat = std::dynamic_pointer_cast<ModelAPI_Feature>((*aNewIter)->owner());
816 aData->addBackReference(aRefFeat, (*aNewIter)->id());
819 if (theNewRefs.size() != aData->refsToMe().size()) { // some back ref must be removed
820 std::set<AttributePtr>::iterator aCurrentIter = aData->refsToMe().begin();
821 while(aCurrentIter != aData->refsToMe().end()) {
822 if (theNewRefs.find(*aCurrentIter) == theNewRefs.end()) {
823 // for external references from other documents this system
824 // is not working: refs are collected from
825 // different Model_Objects, so before remove check this
826 // external object exists and still referenced
827 bool aLeaveIt = false;
828 if ((*aCurrentIter)->owner().get() && (*aCurrentIter)->owner()->document() != myDoc &&
829 (*aCurrentIter)->owner()->data().get() && (*aCurrentIter)->owner()->data()->isValid()) {
830 std::list<std::pair<std::string, std::list<std::shared_ptr<ModelAPI_Object> > > > aRefs;
831 (*aCurrentIter)->owner()->data()->referencesToObjects(aRefs);
832 std::list<std::pair<std::string, std::list<std::shared_ptr<ModelAPI_Object> >>>::iterator
833 aRefIter = aRefs.begin();
834 for(; aRefIter != aRefs.end(); aRefIter++) {
835 if ((*aCurrentIter)->id() == aRefIter->first) {
836 std::list<std::shared_ptr<ModelAPI_Object> >::iterator anOIt;
837 for(anOIt = aRefIter->second.begin(); anOIt != aRefIter->second.end(); anOIt++) {
838 if (*anOIt == theObject) {
846 aData->removeBackReference(*aCurrentIter);
847 aCurrentIter = aData->refsToMe().begin(); // reinitialize iteration after delete
848 } else aCurrentIter++;
849 } else aCurrentIter++;
852 aData->updateConcealmentFlag();
855 void Model_Objects::synchronizeBackRefs()
857 // collect all back references in the separated container: to update everything at once,
858 // without additional Concealment switchin on and off: only the final modification
860 // referenced (slave) objects to referencing attirbutes
861 std::map<ObjectPtr, std::set<AttributePtr> > allRefs;
862 NCollection_DataMap<TDF_Label, FeaturePtr>::Iterator aFeatures(myFeatures);
863 for(; aFeatures.More(); aFeatures.Next()) {
864 FeaturePtr aFeature = aFeatures.Value();
865 std::shared_ptr<Model_Data> aFData = std::dynamic_pointer_cast<Model_Data>(aFeature->data());
867 std::list<std::pair<std::string, std::list<ObjectPtr> > > aRefs;
868 aFData->referencesToObjects(aRefs);
869 std::list<std::pair<std::string, std::list<ObjectPtr> > >::iterator aRefsIt = aRefs.begin();
870 for(; aRefsIt != aRefs.end(); aRefsIt++) {
871 std::list<ObjectPtr>::iterator aRefTo = aRefsIt->second.begin();
872 for(; aRefTo != aRefsIt->second.end(); aRefTo++) {
874 std::map<ObjectPtr, std::set<AttributePtr> >::iterator aFound = allRefs.find(*aRefTo);
875 if (aFound == allRefs.end()) {
876 allRefs[*aRefTo] = std::set<AttributePtr>();
877 aFound = allRefs.find(*aRefTo);
879 aFound->second.insert(aFeature->data()->attribute(aRefsIt->first));
885 // second iteration: just compare back-references with existing in features and results
886 for(aFeatures.Initialize(myFeatures); aFeatures.More(); aFeatures.Next()) {
887 FeaturePtr aFeature = aFeatures.Value();
888 static std::set<AttributePtr> anEmpty;
889 std::map<ObjectPtr, std::set<AttributePtr> >::iterator aFound = allRefs.find(aFeature);
890 if (aFound == allRefs.end()) { // not found => erase all back references
891 synchronizeBackRefsForObject(anEmpty, aFeature);
893 synchronizeBackRefsForObject(aFound->second, aFeature);
894 allRefs.erase(aFound); // to check that all refs are counted
897 std::list<ResultPtr> aResults;
898 ModelAPI_Tools::allResults(aFeature, aResults);
899 std::list<ResultPtr>::iterator aRIter = aResults.begin();
900 for(; aRIter != aResults.cend(); aRIter++) {
901 aFound = allRefs.find(*aRIter);
902 if (aFound == allRefs.end()) { // not found => erase all back references
903 synchronizeBackRefsForObject(anEmpty, *aRIter);
905 synchronizeBackRefsForObject(aFound->second, *aRIter);
906 allRefs.erase(aFound); // to check that all refs are counted
910 for(aFeatures.Initialize(myFeatures); aFeatures.More(); aFeatures.Next()) {
911 FeaturePtr aFeature = aFeatures.Value();
912 std::list<ResultPtr> aResults;
913 ModelAPI_Tools::allResults(aFeature, aResults);
914 // update the concealment status for disply in isConcealed of ResultBody
915 std::list<ResultPtr>::iterator aRIter = aResults.begin();
916 for(; aRIter != aResults.cend(); aRIter++) {
917 (*aRIter)->isConcealed();
920 // the rest all refs means that feature references to the external document feature:
922 std::map<ObjectPtr, std::set<AttributePtr> >::iterator anExtIter = allRefs.begin();
923 for(; anExtIter != allRefs.end(); anExtIter++) {
924 synchronizeBackRefsForObject(anExtIter->second, anExtIter->first);
928 TDF_Label Model_Objects::resultLabel(
929 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theResultIndex)
931 const std::shared_ptr<Model_Data>& aData =
932 std::dynamic_pointer_cast<Model_Data>(theFeatureData);
933 return aData->label().Father().FindChild(TAG_FEATURE_RESULTS).FindChild(theResultIndex + 1);
936 void Model_Objects::storeResult(std::shared_ptr<ModelAPI_Data> theFeatureData,
937 std::shared_ptr<ModelAPI_Result> theResult,
938 const int theResultIndex)
941 theResult->setDoc(myDoc);
942 initData(theResult, resultLabel(theFeatureData, theResultIndex), TAG_FEATURE_ARGUMENTS);
943 if (theResult->data()->name().empty()) {
944 // if was not initialized, generate event and set a name
945 std::stringstream aNewName;
946 aNewName<<theFeatureData->name();
947 // if there are several results (issue #899: any number of result),
948 // add unique prefix starting from second
949 if (theResultIndex > 0 || theResult->groupName() == ModelAPI_ResultBody::group())
950 aNewName<<"_"<<theResultIndex + 1;
951 theResult->data()->setName(aNewName.str());
955 std::shared_ptr<ModelAPI_ResultConstruction> Model_Objects::createConstruction(
956 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex)
958 TDF_Label aLab = resultLabel(theFeatureData, theIndex);
959 TDataStd_Comment::Set(aLab, ModelAPI_ResultConstruction::group().c_str());
960 ObjectPtr anOldObject = object(aLab);
961 std::shared_ptr<ModelAPI_ResultConstruction> aResult;
962 if (anOldObject.get()) {
963 aResult = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(anOldObject);
965 if (!aResult.get()) {
966 aResult = std::shared_ptr<ModelAPI_ResultConstruction>(new Model_ResultConstruction);
967 storeResult(theFeatureData, aResult, theIndex);
972 std::shared_ptr<ModelAPI_ResultBody> Model_Objects::createBody(
973 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex)
975 TDF_Label aLab = resultLabel(theFeatureData, theIndex);
976 // for feature create compsolid, but for result sub create body:
977 // only one level of recursion is supported now
978 ResultPtr aResultOwner = std::dynamic_pointer_cast<ModelAPI_Result>(theFeatureData->owner());
979 ObjectPtr anOldObject;
980 if (aResultOwner.get()) {
981 TDataStd_Comment::Set(aLab, ModelAPI_ResultBody::group().c_str());
982 } else { // in compsolid (higher level result) old object probably may be found
983 TDataStd_Comment::Set(aLab, ModelAPI_ResultCompSolid::group().c_str());
984 anOldObject = object(aLab);
986 std::shared_ptr<ModelAPI_ResultBody> aResult;
987 if (anOldObject.get()) {
988 aResult = std::dynamic_pointer_cast<ModelAPI_ResultBody>(anOldObject);
990 if (!aResult.get()) {
991 // create compsolid anyway; if it is compsolid, it will create sub-bodies internally
992 if (aResultOwner.get()) {
993 aResult = std::shared_ptr<ModelAPI_ResultBody>(new Model_ResultBody);
995 aResult = std::shared_ptr<ModelAPI_ResultBody>(new Model_ResultCompSolid);
997 storeResult(theFeatureData, aResult, theIndex);
1002 std::shared_ptr<ModelAPI_ResultPart> Model_Objects::createPart(
1003 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex)
1005 TDF_Label aLab = resultLabel(theFeatureData, theIndex);
1006 TDataStd_Comment::Set(aLab, ModelAPI_ResultPart::group().c_str());
1007 ObjectPtr anOldObject = object(aLab);
1008 std::shared_ptr<ModelAPI_ResultPart> aResult;
1009 if (anOldObject.get()) {
1010 aResult = std::dynamic_pointer_cast<ModelAPI_ResultPart>(anOldObject);
1012 if (!aResult.get()) {
1013 aResult = std::shared_ptr<ModelAPI_ResultPart>(new Model_ResultPart);
1014 storeResult(theFeatureData, aResult, theIndex);
1019 std::shared_ptr<ModelAPI_ResultPart> Model_Objects::copyPart(
1020 const std::shared_ptr<ModelAPI_ResultPart>& theOrigin,
1021 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex)
1023 std::shared_ptr<ModelAPI_ResultPart> aResult = createPart(theFeatureData, theIndex);
1024 aResult->data()->reference(Model_ResultPart::BASE_REF_ID())->setValue(theOrigin);
1028 std::shared_ptr<ModelAPI_ResultGroup> Model_Objects::createGroup(
1029 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex)
1031 TDF_Label aLab = resultLabel(theFeatureData, theIndex);
1032 TDataStd_Comment::Set(aLab, ModelAPI_ResultGroup::group().c_str());
1033 ObjectPtr anOldObject = object(aLab);
1034 std::shared_ptr<ModelAPI_ResultGroup> aResult;
1035 if (anOldObject.get()) {
1036 aResult = std::dynamic_pointer_cast<ModelAPI_ResultGroup>(anOldObject);
1038 if (!aResult.get()) {
1039 aResult = std::shared_ptr<ModelAPI_ResultGroup>(new Model_ResultGroup(theFeatureData));
1040 storeResult(theFeatureData, aResult, theIndex);
1045 std::shared_ptr<ModelAPI_ResultField> Model_Objects::createField(
1046 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex)
1048 TDF_Label aLab = resultLabel(theFeatureData, theIndex);
1049 TDataStd_Comment::Set(aLab, ModelAPI_ResultField::group().c_str());
1050 ObjectPtr anOldObject = object(aLab);
1051 std::shared_ptr<ModelAPI_ResultField> aResult;
1052 if (anOldObject.get()) {
1053 aResult = std::dynamic_pointer_cast<ModelAPI_ResultField>(anOldObject);
1055 if (!aResult.get()) {
1056 aResult = std::shared_ptr<ModelAPI_ResultField>(new Model_ResultField(theFeatureData));
1057 storeResult(theFeatureData, aResult, theIndex);
1062 std::shared_ptr<ModelAPI_ResultParameter> Model_Objects::createParameter(
1063 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex)
1065 TDF_Label aLab = resultLabel(theFeatureData, theIndex);
1066 TDataStd_Comment::Set(aLab, ModelAPI_ResultParameter::group().c_str());
1067 ObjectPtr anOldObject = object(aLab);
1068 std::shared_ptr<ModelAPI_ResultParameter> aResult;
1069 if (anOldObject.get()) {
1070 aResult = std::dynamic_pointer_cast<ModelAPI_ResultParameter>(anOldObject);
1072 if (!aResult.get()) {
1073 aResult = std::shared_ptr<ModelAPI_ResultParameter>(new Model_ResultParameter);
1074 storeResult(theFeatureData, aResult, theIndex);
1079 std::shared_ptr<ModelAPI_Feature> Model_Objects::feature(
1080 const std::shared_ptr<ModelAPI_Result>& theResult)
1082 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(theResult->data());
1084 TDF_Label aFeatureLab = aData->label().Father().Father().Father();
1085 FeaturePtr aFeature = feature(aFeatureLab);
1086 if (!aFeature.get() && aFeatureLab.Depth() > 1) { // this may be sub-result of result
1087 aFeatureLab = aFeatureLab.Father().Father();
1088 aFeature = feature(aFeatureLab);
1092 return FeaturePtr();
1095 std::string Model_Objects::featureResultGroup(FeaturePtr theFeature)
1097 if (theFeature->data()->isValid()) {
1098 TDF_ChildIterator aLabIter(resultLabel(theFeature->data(), 0).Father());
1099 if (aLabIter.More()) {
1100 TDF_Label anArgLab = aLabIter.Value();
1101 Handle(TDataStd_Comment) aGroup;
1102 if (aLabIter.Value().FindAttribute(TDataStd_Comment::GetID(), aGroup)) {
1103 return TCollection_AsciiString(aGroup->Get()).ToCString();
1107 static std::string anEmpty;
1108 return anEmpty; // not found
1111 void Model_Objects::updateResults(FeaturePtr theFeature, std::set<FeaturePtr>& theProcessed)
1113 if (theProcessed.find(theFeature) != theProcessed.end())
1115 theProcessed.insert(theFeature);
1116 // for composites update subs recursively (sketch elements results are needed for the sketch)
1117 CompositeFeaturePtr aComp = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theFeature);
1118 if (aComp.get() && aComp->getKind() != "Part") { // don't go inside of parts sub-features
1119 // update subs of composites first
1120 int aSubNum = aComp->numberOfSubs();
1121 for(int a = 0; a < aSubNum; a++) {
1122 FeaturePtr aSub = aComp->subFeature(a);
1123 updateResults(aComp->subFeature(a), theProcessed);
1127 // for not persistent is will be done by parametric updater automatically
1128 //if (!theFeature->isPersistentResult()) return;
1129 // check the existing results and remove them if there is nothing on the label
1130 std::list<ResultPtr>::const_iterator aResIter = theFeature->results().cbegin();
1131 while(aResIter != theFeature->results().cend()) {
1132 ResultPtr aBody = std::dynamic_pointer_cast<ModelAPI_Result>(*aResIter);
1134 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(aBody->data());
1135 if (!aData.get() || !aData->isValid() || (!aBody->isDisabled() && aData->isDeleted())) {
1136 // found a disappeared result => remove it
1137 theFeature->eraseResultFromList(aBody);
1138 // start iterate from beginning because iterator is corrupted by removing
1139 aResIter = theFeature->results().cbegin();
1145 // it may be on undo
1146 if (!theFeature->data() || !theFeature->data()->isValid() || theFeature->isDisabled())
1148 // check that results are presented on all labels
1149 int aResSize = int(theFeature->results().size());
1150 TDF_ChildIterator aLabIter(resultLabel(theFeature->data(), 0).Father());
1151 for(; aLabIter.More(); aLabIter.Next()) {
1152 // here must be GUID of the feature
1153 int aResIndex = aLabIter.Value().Tag() - 1;
1155 if (aResSize <= aResIndex) {
1156 TDF_Label anArgLab = aLabIter.Value();
1157 Handle(TDataStd_Comment) aGroup;
1158 if (anArgLab.FindAttribute(TDataStd_Comment::GetID(), aGroup)) {
1159 if (aGroup->Get() == ModelAPI_ResultBody::group().c_str() ||
1160 aGroup->Get() == ModelAPI_ResultCompSolid::group().c_str()) {
1161 aNewBody = createBody(theFeature->data(), aResIndex);
1162 } else if (aGroup->Get() == ModelAPI_ResultPart::group().c_str()) {
1163 std::shared_ptr<ModelAPI_ResultPart> aNewP = createPart(theFeature->data(), aResIndex);
1164 theFeature->setResult(aNewP, aResIndex);
1165 if (!aNewP->partDoc().get())
1166 // create the part result: it is better to restore the previous result if it is possible
1167 theFeature->execute();
1168 } else if (aGroup->Get() == ModelAPI_ResultConstruction::group().c_str()) {
1169 theFeature->execute(); // construction shapes are needed for sketch solver
1170 } else if (aGroup->Get() == ModelAPI_ResultGroup::group().c_str()) {
1171 aNewBody = createGroup(theFeature->data(), aResIndex);
1172 } else if (aGroup->Get() == ModelAPI_ResultField::group().c_str()) {
1173 aNewBody = createField(theFeature->data(), aResIndex);
1174 } else if (aGroup->Get() == ModelAPI_ResultParameter::group().c_str()) {
1175 theFeature->attributeChanged("expression"); // just produce a value
1177 Events_InfoMessage("Model_Objects", "Unknown type of result is found in the document:")
1178 .arg(TCollection_AsciiString(aGroup->Get()).ToCString()).send();
1181 if (aNewBody && !aNewBody->data()->isDeleted()) {
1182 theFeature->setResult(aNewBody, aResIndex);
1188 ResultPtr Model_Objects::findByName(const std::string theName)
1190 NCollection_DataMap<TDF_Label, FeaturePtr>::Iterator anObjIter(myFeatures);
1191 for(; anObjIter.More(); anObjIter.Next()) {
1192 FeaturePtr& aFeature = anObjIter.ChangeValue();
1193 if (!aFeature.get() || aFeature->isDisabled()) // may be on close
1195 std::list<ResultPtr> allResults;
1196 ModelAPI_Tools::allResults(aFeature, allResults);
1197 std::list<ResultPtr>::iterator aRIter = allResults.begin();
1198 for (; aRIter != allResults.cend(); aRIter++) {
1199 ResultPtr aRes = *aRIter;
1200 if (aRes.get() && aRes->data() && aRes->data()->isValid() && !aRes->isDisabled() &&
1201 aRes->data()->name() == theName) {
1210 FeaturePtr Model_Objects::nextFeature(FeaturePtr theCurrent, const bool theReverse)
1212 std::shared_ptr<Model_Data> aData = std::static_pointer_cast<Model_Data>(theCurrent->data());
1213 if (aData.get() && aData->isValid()) {
1214 TDF_Label aFeatureLabel = aData->label().Father();
1215 Handle(TDataStd_ReferenceArray) aRefs;
1216 if (featuresLabel().FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
1217 for(int a = aRefs->Lower(); a <= aRefs->Upper(); a++) { // iterate all existing features
1218 TDF_Label aCurLab = aRefs->Value(a);
1219 if (aCurLab.IsEqual(aFeatureLabel)) {
1220 a += theReverse ? -1 : 1;
1221 if (a >= aRefs->Lower() && a <= aRefs->Upper())
1222 return feature(aRefs->Value(a));
1223 break; // finish iiteration: it's last feature
1228 return FeaturePtr(); // not found, last, or something is wrong
1231 FeaturePtr Model_Objects::firstFeature()
1233 Handle(TDataStd_ReferenceArray) aRefs;
1234 if (featuresLabel().FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
1235 return feature(aRefs->Value(aRefs->Lower()));
1237 return FeaturePtr(); // no features at all
1240 FeaturePtr Model_Objects::lastFeature()
1242 Handle(TDataStd_ReferenceArray) aRefs;
1243 if (featuresLabel().FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
1244 return feature(aRefs->Value(aRefs->Upper()));
1246 return FeaturePtr(); // no features at all
1249 bool Model_Objects::isLater(FeaturePtr theLater, FeaturePtr theCurrent) const
1251 std::shared_ptr<Model_Data> aLaterD = std::static_pointer_cast<Model_Data>(theLater->data());
1252 std::shared_ptr<Model_Data> aCurrentD = std::static_pointer_cast<Model_Data>(theCurrent->data());
1253 if (aLaterD.get() && aLaterD->isValid() && aCurrentD.get() && aCurrentD->isValid()) {
1254 TDF_Label aLaterL = aLaterD->label().Father();
1255 TDF_Label aCurrentL = aCurrentD->label().Father();
1256 int aLaterI = -1, aCurentI = -1; // not found yet state
1257 Handle(TDataStd_ReferenceArray) aRefs;
1258 if (featuresLabel().FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
1259 for(int a = aRefs->Lower(); a <= aRefs->Upper(); a++) { // iterate all existing features
1260 TDF_Label aCurLab = aRefs->Value(a);
1261 if (aCurLab.IsEqual(aLaterL)) {
1263 } else if (aCurLab.IsEqual(aCurrentL)) {
1266 if (aLaterI != -1 && aCurentI != -1) // both are found
1267 return aLaterI > aCurentI;
1271 return false; // not found, or something is wrong
1274 std::list<std::shared_ptr<ModelAPI_Feature> > Model_Objects::allFeatures()
1276 std::list<std::shared_ptr<ModelAPI_Feature> > aResult;
1277 Handle(TDataStd_ReferenceArray) aRefs;
1278 if (featuresLabel().FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
1279 for(int a = aRefs->Lower(); a <= aRefs->Upper(); a++) {
1280 FeaturePtr aFeature = feature(aRefs->Value(a));
1282 aResult.push_back(aFeature);
1288 int Model_Objects::numInternalFeatures()
1290 Handle(TDataStd_ReferenceArray) aRefs;
1291 if (featuresLabel().FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
1292 return aRefs->Upper() - aRefs->Lower() + 1;
1294 return 0; // invalid
1297 std::shared_ptr<ModelAPI_Feature> Model_Objects::internalFeature(const int theIndex)
1299 Handle(TDataStd_ReferenceArray) aRefs;
1300 if (featuresLabel().FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
1301 return feature(aRefs->Value(aRefs->Lower() + theIndex));
1303 return FeaturePtr(); // invalid
1306 Standard_Integer HashCode(const TDF_Label& theLab, const Standard_Integer theUpper)
1308 return TDF_LabelMapHasher::HashCode(theLab, theUpper);
1311 Standard_Boolean IsEqual(const TDF_Label& theLab1, const TDF_Label& theLab2)
1313 return TDF_LabelMapHasher::IsEqual(theLab1, theLab2);