1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: Model_Objects.cxx
4 // Created: 15 May 2015
5 // Author: Mikhail PONIKAROV
7 #include <Model_Objects.h>
8 #include <Model_Data.h>
9 #include <Model_Document.h>
10 #include <Model_Events.h>
11 #include <Model_Session.h>
12 #include <Model_ResultPart.h>
13 #include <Model_ResultConstruction.h>
14 #include <Model_ResultBody.h>
15 #include <Model_ResultCompSolid.h>
16 #include <Model_ResultGroup.h>
17 #include <Model_ResultParameter.h>
18 #include <ModelAPI_Validator.h>
19 #include <ModelAPI_CompositeFeature.h>
20 #include <ModelAPI_Tools.h>
22 #include <Events_Loop.h>
23 #include <Events_Error.h>
25 #include <TDataStd_Integer.hxx>
26 #include <TDataStd_Comment.hxx>
27 #include <TDF_ChildIDIterator.hxx>
28 #include <TDataStd_ReferenceArray.hxx>
29 #include <TDataStd_HLabelArray1.hxx>
30 #include <TDataStd_Name.hxx>
31 #include <TDF_Reference.hxx>
32 #include <TDF_ChildIDIterator.hxx>
33 #include <TDF_LabelMapHasher.hxx>
35 static const int TAG_OBJECTS = 2; // tag of the objects sub-tree (features, results)
38 static const int TAG_FEATURE_ARGUMENTS = 1; ///< where the arguments are located
39 static const int TAG_FEATURE_RESULTS = 2; ///< where the results are located
42 /// 0:1:2 - where features are located
43 /// 0:1:2:N:1 - data of the feature N
44 /// 0:1:2:N:2:K:1 - data of the K result of the feature N
46 Model_Objects::Model_Objects(TDF_Label theMainLab) : myMain(theMainLab)
50 void Model_Objects::setOwner(DocumentPtr theDoc)
53 // update all fields and recreate features and result objects if needed
54 synchronizeFeatures(false, true, true);
58 Model_Objects::~Model_Objects()
60 // delete all features of this document
61 Events_Loop* aLoop = Events_Loop::loop();
62 NCollection_DataMap<TDF_Label, FeaturePtr>::Iterator aFeaturesIter(myFeatures);
63 for(; aFeaturesIter.More(); aFeaturesIter.Next()) {
64 FeaturePtr aFeature = aFeaturesIter.Value();
65 static Events_ID EVENT_DISP = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
66 ModelAPI_EventCreator::get()->sendDeleted(myDoc, ModelAPI_Feature::group());
67 ModelAPI_EventCreator::get()->sendUpdated(aFeature, EVENT_DISP);
68 aFeature->eraseResults();
72 aLoop->flush(Events_Loop::eventByName(EVENT_OBJECT_DELETED));
73 aLoop->flush(Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
77 /// Appends to the array of references a new referenced label
78 static void AddToRefArray(TDF_Label& theArrayLab, TDF_Label& theReferenced, TDF_Label& thePrevLab)
80 Handle(TDataStd_ReferenceArray) aRefs;
81 if (!theArrayLab.FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
82 aRefs = TDataStd_ReferenceArray::Set(theArrayLab, 0, 0);
83 aRefs->SetValue(0, theReferenced);
84 } else { // extend array by one more element
85 Handle(TDataStd_HLabelArray1) aNewArray = new TDataStd_HLabelArray1(aRefs->Lower(),
87 int aPassedPrev = 0; // prev feature is found and passed
88 if (thePrevLab.IsNull()) { // null means that inserted feature must be the first
89 aNewArray->SetValue(aRefs->Lower(), theReferenced);
92 for (int a = aRefs->Lower(); a <= aRefs->Upper(); a++) {
93 aNewArray->SetValue(a + aPassedPrev, aRefs->Value(a));
94 if (!aPassedPrev && aRefs->Value(a).IsEqual(thePrevLab)) {
96 aNewArray->SetValue(a + 1, theReferenced);
99 if (!aPassedPrev) // not found: unknown situation
100 aNewArray->SetValue(aRefs->Upper() + 1, theReferenced);
101 aRefs->SetInternalArray(aNewArray);
105 void Model_Objects::addFeature(FeaturePtr theFeature, const FeaturePtr theAfterThis)
107 if (!theFeature->isAction()) { // do not add action to the data model
108 TDF_Label aFeaturesLab = featuresLabel();
109 TDF_Label aFeatureLab = aFeaturesLab.NewChild();
110 // store feature in the features array: before "initData" because in macro features
111 // in initData it creates new features, appeared later than this
112 TDF_Label aPrevFeateureLab;
113 if (theAfterThis.get()) { // searching for the previous feature label
114 std::shared_ptr<Model_Data> aPrevData =
115 std::dynamic_pointer_cast<Model_Data>(theAfterThis->data());
116 if (aPrevData.get()) {
117 aPrevFeateureLab = aPrevData->label().Father();
120 AddToRefArray(aFeaturesLab, aFeatureLab, aPrevFeateureLab);
122 // keep the feature ID to restore document later correctly
123 TDataStd_Comment::Set(aFeatureLab, theFeature->getKind().c_str());
124 myFeatures.Bind(aFeatureLab, theFeature);
125 // must be after binding to the map because of "Box" macro feature that
126 // creates other features in "initData"
127 initData(theFeature, aFeatureLab, TAG_FEATURE_ARGUMENTS);
128 // event: feature is added
129 static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_CREATED);
130 ModelAPI_EventCreator::get()->sendUpdated(theFeature, anEvent);
131 theFeature->setDisabled(false); // by default created feature is enabled
132 updateHistory(ModelAPI_Feature::group());
133 } else { // make feature has not-null data anyway
134 theFeature->setData(Model_Data::invalidData());
135 theFeature->setDoc(myDoc);
139 /// Appends to the array of references a new referenced label.
140 /// If theIndex is not -1, removes element at this index, not theReferenced.
141 /// \returns the index of removed element
142 static int RemoveFromRefArray(TDF_Label theArrayLab, TDF_Label theReferenced,
143 const int theIndex = -1)
145 int aResult = -1; // no returned
146 Handle(TDataStd_ReferenceArray) aRefs;
147 if (theArrayLab.FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
148 if (aRefs->Length() == 1) { // just erase an array
149 if ((theIndex == -1 && aRefs->Value(0) == theReferenced) || theIndex == 0) {
150 theArrayLab.ForgetAttribute(TDataStd_ReferenceArray::GetID());
153 } else { // reduce the array
154 Handle(TDataStd_HLabelArray1) aNewArray = new TDataStd_HLabelArray1(aRefs->Lower(),
156 int aCount = aRefs->Lower();
157 for (int a = aCount; a <= aRefs->Upper(); a++, aCount++) {
158 if ((theIndex == -1 && aRefs->Value(a) == theReferenced) || theIndex == a) {
162 aNewArray->SetValue(aCount, aRefs->Value(a));
165 aRefs->SetInternalArray(aNewArray);
171 void Model_Objects::refsToFeature(FeaturePtr theFeature,
172 std::set<std::shared_ptr<ModelAPI_Feature> >& theRefs, const bool isSendError)
174 // check the feature: it must have no depended objects on it
175 // the dependencies can be in the feature results
176 std::list<ResultPtr>::const_iterator aResIter = theFeature->results().cbegin();
177 for(; aResIter != theFeature->results().cend(); aResIter++) {
178 ResultPtr aResult = (*aResIter);
179 std::shared_ptr<Model_Data> aData =
180 std::dynamic_pointer_cast<Model_Data>(aResult->data());
181 if (aData.get() != NULL) {
182 const std::set<AttributePtr>& aRefs = aData->refsToMe();
183 std::set<AttributePtr>::const_iterator aRefIt = aRefs.begin(), aRefLast = aRefs.end();
184 for(; aRefIt != aRefLast; aRefIt++) {
185 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>((*aRefIt)->owner());
186 if (aFeature.get() != NULL)
187 theRefs.insert(aFeature);
191 // the dependencies can be in the feature itself
192 std::shared_ptr<Model_Data> aData =
193 std::dynamic_pointer_cast<Model_Data>(theFeature->data());
194 if (aData && !aData->refsToMe().empty()) {
195 const std::set<AttributePtr>& aRefs = aData->refsToMe();
196 std::set<AttributePtr>::const_iterator aRefIt = aRefs.begin(), aRefLast = aRefs.end();
197 for(; aRefIt != aRefLast; aRefIt++) {
198 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>((*aRefIt)->owner());
199 if (aFeature.get() != NULL)
200 theRefs.insert(aFeature);
204 if (!theRefs.empty() && isSendError) {
206 "Feature '" + theFeature->data()->name() + "' is used and can not be deleted");
210 void Model_Objects::removeFeature(FeaturePtr theFeature)
212 std::shared_ptr<Model_Data> aData = std::static_pointer_cast<Model_Data>(theFeature->data());
213 if (aData && aData->isValid()) {
214 // checking that the sub-element of composite feature is removed: if yes, inform the owner
215 std::set<std::shared_ptr<ModelAPI_Feature> > aRefs;
216 refsToFeature(theFeature, aRefs, false);
217 std::set<std::shared_ptr<ModelAPI_Feature> >::iterator aRefIter = aRefs.begin();
218 for(; aRefIter != aRefs.end(); aRefIter++) {
219 std::shared_ptr<ModelAPI_CompositeFeature> aComposite =
220 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(*aRefIter);
221 if (aComposite.get()) {
222 aComposite->removeFeature(theFeature);
225 // this must be before erase since theFeature erasing removes all information about
226 // the feature results and groups of results
227 // To reproduce: create sketch, extrusion, remove sketch => constructions tree is not updated
228 clearHistory(theFeature);
232 TDF_Label aFeatureLabel = aData->label().Father();
233 if (myFeatures.IsBound(aFeatureLabel))
234 myFeatures.UnBind(aFeatureLabel);
236 static Events_ID EVENT_DISP = Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
237 ModelAPI_EventCreator::get()->sendUpdated(theFeature, EVENT_DISP);
238 // erase all attributes under the label of feature
239 aFeatureLabel.ForgetAllAttributes();
240 // remove it from the references array
241 RemoveFromRefArray(featuresLabel(), aFeatureLabel);
242 // event: feature is deleted
243 ModelAPI_EventCreator::get()->sendDeleted(theFeature->document(), ModelAPI_Feature::group());
244 // the redisplay signal should be flushed in order to erase the feature presentation in the viewer
245 Events_Loop::loop()->flush(EVENT_DISP);
246 updateHistory(ModelAPI_Feature::group());
250 void Model_Objects::moveFeature(FeaturePtr theMoved, FeaturePtr theAfterThis)
252 TDF_Label aFeaturesLab = featuresLabel();
253 Handle(TDataStd_ReferenceArray) aRefs;
254 if (!aFeaturesLab.FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs))
256 TDF_Label anAfterLab, aMovedLab =
257 std::dynamic_pointer_cast<Model_Data>(theMoved->data())->label().Father();
258 if (theAfterThis.get())
259 anAfterLab = std::dynamic_pointer_cast<Model_Data>(theAfterThis->data())->label().Father();
261 Handle(TDataStd_HLabelArray1) aNewArray =
262 new TDataStd_HLabelArray1(aRefs->Lower(), aRefs->Upper());
263 int aPassedMovedFrom = 0; // the prev feature location is found and passed
264 int aPassedMovedTo = 0; // the feature is added and this location is passed
265 if (!theAfterThis.get()) { // null means that inserted feature must be the first
266 aNewArray->SetValue(aRefs->Lower(), aMovedLab);
269 for (int a = aRefs->Lower(); a <= aRefs->Upper(); a++) {
270 if (aPassedMovedTo == 0 && aRefs->Value(a) == anAfterLab) { // add two
272 aNewArray->SetValue(a - aPassedMovedFrom, anAfterLab);
273 if (a + 1 - aPassedMovedFrom <= aRefs->Upper())
274 aNewArray->SetValue(a + 1 - aPassedMovedFrom, aMovedLab);
275 } else if (aPassedMovedFrom == 0 && aRefs->Value(a) == aMovedLab) { // skip
277 } else { // just copy one
278 if (a - aPassedMovedFrom + aPassedMovedTo <= aRefs->Upper())
279 aNewArray->SetValue(a - aPassedMovedFrom + aPassedMovedTo, aRefs->Value(a));
282 if (!aPassedMovedFrom || !aPassedMovedTo) {// not found: unknown situation
283 if (!aPassedMovedFrom) {
284 static std::string aMovedFromError("The moved feature is not found");
285 Events_Error::send(aMovedFromError);
287 static std::string aMovedToError("The 'after' feature for movement is not found");
288 Events_Error::send(aMovedToError);
292 // store the new array
293 aRefs->SetInternalArray(aNewArray);
294 // update the feature and the history
295 clearHistory(theMoved);
296 static Events_ID EVENT_UPD = Events_Loop::loop()->eventByName(EVENT_OBJECT_UPDATED);
297 ModelAPI_EventCreator::get()->sendUpdated(theMoved, EVENT_UPD);
300 void Model_Objects::clearHistory(ObjectPtr theObj)
303 const std::string aGroup = theObj->groupName();
304 std::map<std::string, std::vector<ObjectPtr> >::iterator aHIter = myHistory.find(aGroup);
305 if (aHIter != myHistory.end())
306 myHistory.erase(aHIter); // erase from map => this means that it is not synchronized
307 if (theObj->groupName() == ModelAPI_Feature::group()) { // clear results group of the feature
308 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObj);
309 std::string aResultGroup = featureResultGroup(aFeature);
310 if (!aResultGroup.empty()) {
311 std::map<std::string, std::vector<ObjectPtr> >::iterator aHIter =
312 myHistory.find(aResultGroup);
313 if (aHIter != myHistory.end())
314 myHistory.erase(aHIter); // erase from map => this means that it is not synchronized
320 void Model_Objects::createHistory(const std::string& theGroupID)
322 std::map<std::string, std::vector<ObjectPtr> >::iterator aHIter = myHistory.find(theGroupID);
323 if (aHIter == myHistory.end()) {
324 myHistory[theGroupID] = std::vector<ObjectPtr>();
325 std::vector<ObjectPtr>& aResult = myHistory[theGroupID];
326 // iterate the array of references and get feature by feature from the array
327 bool isFeature = theGroupID == ModelAPI_Feature::group();
328 Handle(TDataStd_ReferenceArray) aRefs;
329 if (featuresLabel().FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
330 for(int a = aRefs->Lower(); a <= aRefs->Upper(); a++) {
331 FeaturePtr aFeature = feature(aRefs->Value(a));
332 if (aFeature.get()) {
333 // if feature is in sub-component, remove it from history: it is in sub-tree of sub-component
334 if (!ModelAPI_Tools::compositeOwner(aFeature).get()) {
335 if (isFeature) { // here may be also disabled features
336 if (aFeature->isInHistory()) {
337 aResult.push_back(aFeature);
339 } else if (!aFeature->isDisabled()) { // iterate all results of not-disabled feature
340 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aFeature->results();
341 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
342 for (; aRIter != aResults.cend(); aRIter++) {
343 ResultPtr aRes = *aRIter;
344 if (aRes->groupName() != theGroupID) break; // feature have only same group results
345 if (!aRes->isDisabled() && aRes->isInHistory() && !aRes->isConcealed()) {
346 aResult.push_back(*aRIter);
357 void Model_Objects::updateHistory(const std::shared_ptr<ModelAPI_Object> theObject)
359 clearHistory(theObject);
362 void Model_Objects::updateHistory(const std::string theGroup)
364 std::map<std::string, std::vector<ObjectPtr> >::iterator aHIter = myHistory.find(theGroup);
365 if (aHIter != myHistory.end())
366 myHistory.erase(aHIter); // erase from map => this means that it is not synchronized
369 FeaturePtr Model_Objects::feature(TDF_Label theLabel) const
371 if (myFeatures.IsBound(theLabel))
372 return myFeatures.Find(theLabel);
373 return FeaturePtr(); // not found
376 ObjectPtr Model_Objects::object(TDF_Label theLabel)
378 // try feature by label
379 FeaturePtr aFeature = feature(theLabel);
381 return feature(theLabel);
382 TDF_Label aFeatureLabel = theLabel.Father().Father(); // let's suppose it is result
383 aFeature = feature(aFeatureLabel);
385 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aFeature->results();
386 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.cbegin();
387 for (; aRIter != aResults.cend(); aRIter++) {
388 std::shared_ptr<Model_Data> aResData = std::dynamic_pointer_cast<Model_Data>(
390 if (aResData->label().Father().IsEqual(theLabel))
394 return FeaturePtr(); // not found
397 ObjectPtr Model_Objects::object(const std::string& theGroupID, const int theIndex)
401 createHistory(theGroupID);
402 return myHistory[theGroupID][theIndex];
405 std::shared_ptr<ModelAPI_Object> Model_Objects::objectByName(
406 const std::string& theGroupID, const std::string& theName)
408 createHistory(theGroupID);
409 std::vector<ObjectPtr>& allObjs = myHistory[theGroupID];
410 std::vector<ObjectPtr>::iterator anObjIter = allObjs.begin();
411 for(; anObjIter != allObjs.end(); anObjIter++) {
412 if ((*anObjIter)->data()->name() == theName)
419 const int Model_Objects::index(std::shared_ptr<ModelAPI_Object> theObject)
421 std::string aGroup = theObject->groupName();
422 createHistory(aGroup);
423 std::vector<ObjectPtr>& allObjs = myHistory[aGroup];
424 std::vector<ObjectPtr>::iterator anObjIter = allObjs.begin(); // iterate to search object
425 for(int anIndex = 0; anObjIter != allObjs.end(); anObjIter++, anIndex++) {
426 if ((*anObjIter) == theObject)
433 int Model_Objects::size(const std::string& theGroupID)
435 createHistory(theGroupID);
436 return myHistory[theGroupID].size();
439 void Model_Objects::allResults(const std::string& theGroupID, std::list<ResultPtr>& theResults)
441 // iterate the array of references and get feature by feature from the array
442 Handle(TDataStd_ReferenceArray) aRefs;
443 if (featuresLabel().FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
444 for(int a = aRefs->Lower(); a <= aRefs->Upper(); a++) {
445 FeaturePtr aFeature = feature(aRefs->Value(a));
446 if (aFeature.get()) {
447 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aFeature->results();
448 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
449 for (; aRIter != aResults.cend(); aRIter++) {
450 ResultPtr aRes = *aRIter;
451 if (aRes->groupName() != theGroupID) break; // feature have only same group results
452 if (aRes->isInHistory() && !aRes->isConcealed()) {
453 theResults.push_back(*aRIter);
462 TDF_Label Model_Objects::featuresLabel() const
464 return myMain.FindChild(TAG_OBJECTS);
467 void Model_Objects::setUniqueName(FeaturePtr theFeature)
469 if (!theFeature->data()->name().empty())
470 return; // not needed, name is already defined
471 std::string aName; // result
472 // first count all features of such kind to start with index = count + 1
473 int aNumObjects = -1; // this feature is already in this map
474 NCollection_DataMap<TDF_Label, FeaturePtr>::Iterator aFIter(myFeatures);
475 for (; aFIter.More(); aFIter.Next()) {
476 if (aFIter.Value()->getKind() == theFeature->getKind())
479 // generate candidate name
480 std::stringstream aNameStream;
481 aNameStream << theFeature->getKind() << "_" << aNumObjects + 1;
482 aName = aNameStream.str();
483 // check this is unique, if not, increase index by 1
484 for (aFIter.Initialize(myFeatures); aFIter.More();) {
485 FeaturePtr aFeature = aFIter.Value();
486 bool isSameName = aFeature->data()->name() == aName;
487 if (!isSameName) { // check also results to avoid same results names (actual for Parts)
488 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aFeature->results();
489 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
490 for (; aRIter != aResults.cend(); aRIter++) {
491 isSameName = (*aRIter)->data()->name() == aName;
496 std::stringstream aNameStream;
497 aNameStream << theFeature->getKind() << "_" << aNumObjects + 1;
498 aName = aNameStream.str();
499 // reinitialize iterator to make sure a new name is unique
500 aFIter.Initialize(myFeatures);
504 theFeature->data()->setName(aName);
507 void Model_Objects::initData(ObjectPtr theObj, TDF_Label theLab, const int theTag)
509 std::shared_ptr<Model_Data> aData(new Model_Data);
510 aData->setLabel(theLab.FindChild(theTag));
511 aData->setObject(theObj);
512 theObj->setDoc(myDoc);
513 theObj->setData(aData);
514 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObj);
516 setUniqueName(aFeature); // must be before "initAttributes" because duplicate part uses name
518 theObj->initAttributes();
521 void Model_Objects::synchronizeFeatures(
522 const bool theMarkUpdated, const bool theUpdateReferences, const bool theFlush)
524 Model_Document* anOwner = std::dynamic_pointer_cast<Model_Document>(myDoc).get();
525 if (!anOwner) // this may happen on creation of document: nothing there, so nothing to synchronize
527 // after all updates, sends a message that groups of features were created or updated
528 Events_Loop* aLoop = Events_Loop::loop();
529 static Events_ID aDispEvent = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
530 static Events_ID aCreateEvent = Events_Loop::eventByName(EVENT_OBJECT_CREATED);
531 static Events_ID anUpdateEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
532 static Events_ID aRedispEvent = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
533 static Events_ID aDeleteEvent = Events_Loop::eventByName(EVENT_OBJECT_DELETED);
534 static Events_ID aToHideEvent = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
535 bool isActive = aLoop->activateFlushes(false);
537 // update all objects by checking are they on labels or not
538 std::set<FeaturePtr> aNewFeatures, aKeptFeatures;
539 TDF_ChildIDIterator aLabIter(featuresLabel(), TDataStd_Comment::GetID());
540 for (; aLabIter.More(); aLabIter.Next()) {
541 TDF_Label aFeatureLabel = aLabIter.Value()->Label();
543 if (!myFeatures.IsBound(aFeatureLabel)) { // a new feature is inserted
545 aFeature = std::dynamic_pointer_cast<Model_Session>(ModelAPI_Session::get())->createFeature(
546 TCollection_AsciiString(Handle(TDataStd_Comment)::DownCast(aLabIter.Value())->Get())
547 .ToCString(), anOwner);
548 if (!aFeature) { // somethig is wrong, most probably, the opened document has invalid structure
549 Events_Error::send("Invalid type of object in the document");
550 aLabIter.Value()->Label().ForgetAllAttributes();
553 // this must be before "setData" to redo the sketch line correctly
554 myFeatures.Bind(aFeatureLabel, aFeature);
555 aNewFeatures.insert(aFeature);
556 initData(aFeature, aFeatureLabel, TAG_FEATURE_ARGUMENTS);
557 updateHistory(aFeature);
558 aFeature->setDisabled(false); // by default created feature is enabled (this allows to recreate the results before "setCurrent" is called)
560 // event: model is updated
561 ModelAPI_EventCreator::get()->sendUpdated(aFeature, aCreateEvent);
562 } else { // nothing is changed, both iterators are incremented
563 aFeature = myFeatures.Find(aFeatureLabel);
564 aKeptFeatures.insert(aFeature);
565 if (theMarkUpdated) {
566 ModelAPI_EventCreator::get()->sendUpdated(aFeature, anUpdateEvent);
571 // check all features are checked: if not => it was removed
572 NCollection_DataMap<TDF_Label, FeaturePtr>::Iterator aFIter(myFeatures);
573 while (aFIter.More()) {
574 if (aKeptFeatures.find(aFIter.Value()) == aKeptFeatures.end()
575 && aNewFeatures.find(aFIter.Value()) == aNewFeatures.end()) {
576 FeaturePtr aFeature = aFIter.Value();
577 // event: model is updated
578 //if (aFeature->isInHistory()) {
579 ModelAPI_EventCreator::get()->sendDeleted(myDoc, ModelAPI_Feature::group());
581 // results of this feature must be redisplayed (hided)
582 // redisplay also removed feature (used for sketch and AISObject)
583 ModelAPI_EventCreator::get()->sendUpdated(aFeature, aRedispEvent);
584 updateHistory(aFeature);
586 // unbind after the "erase" call: on abort sketch is removes sub-objects that corrupts aFIter
587 myFeatures.UnBind(aFIter.Key());
588 // reinitialize iterator because unbind may corrupt the previous order in the map
589 aFIter.Initialize(myFeatures);
594 if (theUpdateReferences) {
595 synchronizeBackRefs();
597 // update results of the features (after features created because they may be connected, like sketch and sub elements)
598 // After synchronisation of back references because sketch must be set in sub-elements before "execute" by updateResults
599 std::list<FeaturePtr> aComposites; // composites must be updated after their subs (issue 360)
600 TDF_ChildIDIterator aLabIter2(featuresLabel(), TDataStd_Comment::GetID());
601 for (; aLabIter2.More(); aLabIter2.Next()) {
602 TDF_Label aFeatureLabel = aLabIter2.Value()->Label();
603 if (myFeatures.IsBound(aFeatureLabel)) { // a new feature is inserted
604 FeaturePtr aFeature = myFeatures.Find(aFeatureLabel);
605 if (std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aFeature).get())
606 aComposites.push_back(aFeature);
607 updateResults(aFeature);
610 std::list<FeaturePtr>::iterator aComposite = aComposites.begin();
611 for(; aComposite != aComposites.end(); aComposite++) {
612 updateResults(*aComposite);
615 // the synchronize should be done after updateResults in order to correct back references of updated results
616 if (theUpdateReferences) {
617 synchronizeBackRefs();
619 if (theMarkUpdated) { // this means there is no control what was modified => remove history cash
623 anOwner->executeFeatures() = false;
624 aLoop->activateFlushes(isActive);
627 aLoop->flush(aCreateEvent);
628 aLoop->flush(aDeleteEvent);
629 aLoop->flush(anUpdateEvent);
630 aLoop->flush(aRedispEvent);
631 aLoop->flush(aToHideEvent);
633 anOwner->executeFeatures() = true;
636 void Model_Objects::synchronizeBackRefs()
638 // keeps the concealed flags of result to catch the change and create created/deleted events
639 std::list<std::pair<ResultPtr, bool> > aConcealed;
640 // first cycle: erase all data about back-references
641 NCollection_DataMap<TDF_Label, FeaturePtr>::Iterator aFeatures(myFeatures);
642 for(; aFeatures.More(); aFeatures.Next()) {
643 FeaturePtr aFeature = aFeatures.Value();
644 std::shared_ptr<Model_Data> aFData =
645 std::dynamic_pointer_cast<Model_Data>(aFeature->data());
647 aFData->eraseBackReferences();
649 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aFeature->results();
650 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
651 for (; aRIter != aResults.cend(); aRIter++) {
652 std::shared_ptr<Model_Data> aResData =
653 std::dynamic_pointer_cast<Model_Data>((*aRIter)->data());
655 aConcealed.push_back(std::pair<ResultPtr, bool>(*aRIter, (*aRIter)->isConcealed()));
656 aResData->eraseBackReferences();
661 // second cycle: set new back-references: only features may have reference, iterate only them
662 ModelAPI_ValidatorsFactory* aValidators = ModelAPI_Session::get()->validators();
663 for(aFeatures.Initialize(myFeatures); aFeatures.More(); aFeatures.Next()) {
664 FeaturePtr aFeature = aFeatures.Value();
665 std::shared_ptr<Model_Data> aFData =
666 std::dynamic_pointer_cast<Model_Data>(aFeature->data());
668 std::list<std::pair<std::string, std::list<ObjectPtr> > > aRefs;
669 aFData->referencesToObjects(aRefs);
670 std::list<std::pair<std::string, std::list<ObjectPtr> > >::iterator
671 aRefsIter = aRefs.begin();
672 for(; aRefsIter != aRefs.end(); aRefsIter++) {
673 std::list<ObjectPtr>::iterator aRefTo = aRefsIter->second.begin();
674 for(; aRefTo != aRefsIter->second.end(); aRefTo++) {
676 std::shared_ptr<Model_Data> aRefData =
677 std::dynamic_pointer_cast<Model_Data>((*aRefTo)->data());
678 aRefData->addBackReference(aFeature, aRefsIter->first); // here the Concealed flag is updated
679 // update enable/disable status: the nested status must be equal to the composite
680 CompositeFeaturePtr aComp =
681 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aFeature);
683 FeaturePtr aReferenced = std::dynamic_pointer_cast<ModelAPI_Feature>(*aRefTo);
684 if (aReferenced.get()) {
685 aReferenced->setDisabled(aComp->isDisabled());
693 std::list<std::pair<ResultPtr, bool> >::iterator aCIter = aConcealed.begin();
694 for(; aCIter != aConcealed.end(); aCIter++) {
695 if (aCIter->first->isConcealed() != aCIter->second) { // somethign is changed => produce event
696 if (aCIter->second) { // was concealed become not => creation event
697 static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_CREATED);
698 ModelAPI_EventCreator::get()->sendUpdated(aCIter->first, anEvent);
699 } else { // was not concealed become concealed => delete event
700 ModelAPI_EventCreator::get()->sendDeleted(myDoc, aCIter->first->groupName());
701 // redisplay for the viewer (it must be disappeared also)
702 static Events_ID EVENT_DISP =
703 Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
704 ModelAPI_EventCreator::get()->sendUpdated(aCIter->first, EVENT_DISP);
710 TDF_Label Model_Objects::resultLabel(
711 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theResultIndex)
713 const std::shared_ptr<Model_Data>& aData =
714 std::dynamic_pointer_cast<Model_Data>(theFeatureData);
715 return aData->label().Father().FindChild(TAG_FEATURE_RESULTS).FindChild(theResultIndex + 1);
718 void Model_Objects::storeResult(std::shared_ptr<ModelAPI_Data> theFeatureData,
719 std::shared_ptr<ModelAPI_Result> theResult,
720 const int theResultIndex)
722 theResult->setDoc(myDoc);
723 initData(theResult, resultLabel(theFeatureData, theResultIndex), TAG_FEATURE_ARGUMENTS);
724 if (theResult->data()->name().empty()) { // if was not initialized, generate event and set a name
725 std::stringstream aNewName;
726 aNewName<<theFeatureData->name();
727 if (theResultIndex > 0) // if there are several results, add unique prefix starting from second
728 aNewName<<"_"<<theResultIndex + 1;
729 theResult->data()->setName(aNewName.str());
733 std::shared_ptr<ModelAPI_ResultConstruction> Model_Objects::createConstruction(
734 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex)
736 TDF_Label aLab = resultLabel(theFeatureData, theIndex);
737 TDataStd_Comment::Set(aLab, ModelAPI_ResultConstruction::group().c_str());
738 ObjectPtr anOldObject = object(aLab);
739 std::shared_ptr<ModelAPI_ResultConstruction> aResult;
741 aResult = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(anOldObject);
744 aResult = std::shared_ptr<ModelAPI_ResultConstruction>(new Model_ResultConstruction);
745 storeResult(theFeatureData, aResult, theIndex);
750 std::shared_ptr<ModelAPI_ResultBody> Model_Objects::createBody(
751 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex)
753 TDF_Label aLab = resultLabel(theFeatureData, theIndex);
754 TDataStd_Comment::Set(aLab, ModelAPI_ResultBody::group().c_str());
755 ObjectPtr anOldObject = object(aLab);
756 std::shared_ptr<ModelAPI_ResultBody> aResult;
758 aResult = std::dynamic_pointer_cast<ModelAPI_ResultBody>(anOldObject);
761 aResult = std::shared_ptr<ModelAPI_ResultBody>(new Model_ResultBody);
762 storeResult(theFeatureData, aResult, theIndex);
767 std::shared_ptr<ModelAPI_ResultCompSolid> Model_Objects::createCompSolid(
768 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex)
770 TDF_Label aLab = resultLabel(theFeatureData, theIndex);
771 TDataStd_Comment::Set(aLab, ModelAPI_ResultCompSolid::group().c_str());
772 ObjectPtr anOldObject = object(aLab);
773 std::shared_ptr<ModelAPI_ResultCompSolid> aResult;
775 aResult = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(anOldObject);
778 aResult = std::shared_ptr<ModelAPI_ResultCompSolid>(new Model_ResultCompSolid);
779 storeResult(theFeatureData, aResult, theIndex);
784 std::shared_ptr<ModelAPI_ResultPart> Model_Objects::createPart(
785 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex)
787 TDF_Label aLab = resultLabel(theFeatureData, theIndex);
788 TDataStd_Comment::Set(aLab, ModelAPI_ResultPart::group().c_str());
789 ObjectPtr anOldObject = object(aLab);
790 std::shared_ptr<ModelAPI_ResultPart> aResult;
792 aResult = std::dynamic_pointer_cast<ModelAPI_ResultPart>(anOldObject);
795 aResult = std::shared_ptr<ModelAPI_ResultPart>(new Model_ResultPart);
796 storeResult(theFeatureData, aResult, theIndex);
801 std::shared_ptr<ModelAPI_ResultPart> Model_Objects::copyPart(
802 const std::shared_ptr<ModelAPI_Result>& theOldPart,
803 const std::shared_ptr<ModelAPI_ResultPart>& theOrigin, const int theIndex)
805 std::shared_ptr<ModelAPI_ResultPart> aResult;
806 if (theOldPart.get()) {
807 aResult = std::dynamic_pointer_cast<ModelAPI_ResultPart>(theOldPart);
810 aResult = std::shared_ptr<ModelAPI_ResultPart>(new Model_ResultPart);
811 aResult->setDoc(myDoc);
812 aResult->setData(theOrigin->data());
817 std::shared_ptr<ModelAPI_ResultGroup> Model_Objects::createGroup(
818 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex)
820 TDF_Label aLab = resultLabel(theFeatureData, theIndex);
821 TDataStd_Comment::Set(aLab, ModelAPI_ResultGroup::group().c_str());
822 ObjectPtr anOldObject = object(aLab);
823 std::shared_ptr<ModelAPI_ResultGroup> aResult;
825 aResult = std::dynamic_pointer_cast<ModelAPI_ResultGroup>(anOldObject);
828 aResult = std::shared_ptr<ModelAPI_ResultGroup>(new Model_ResultGroup(theFeatureData));
829 storeResult(theFeatureData, aResult, theIndex);
834 std::shared_ptr<ModelAPI_ResultParameter> Model_Objects::createParameter(
835 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex)
837 TDF_Label aLab = resultLabel(theFeatureData, theIndex);
838 TDataStd_Comment::Set(aLab, ModelAPI_ResultParameter::group().c_str());
839 ObjectPtr anOldObject = object(aLab);
840 std::shared_ptr<ModelAPI_ResultParameter> aResult;
842 aResult = std::dynamic_pointer_cast<ModelAPI_ResultParameter>(anOldObject);
845 aResult = std::shared_ptr<ModelAPI_ResultParameter>(new Model_ResultParameter);
846 storeResult(theFeatureData, aResult, theIndex);
851 std::shared_ptr<ModelAPI_Feature> Model_Objects::feature(
852 const std::shared_ptr<ModelAPI_Result>& theResult)
854 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(theResult->data());
856 TDF_Label aFeatureLab = aData->label().Father().Father().Father();
857 return feature(aFeatureLab);
862 std::string Model_Objects::featureResultGroup(FeaturePtr theFeature)
864 if (theFeature->data()->isValid()) {
865 TDF_ChildIterator aLabIter(resultLabel(theFeature->data(), 0).Father());
866 if (aLabIter.More()) {
867 TDF_Label anArgLab = aLabIter.Value();
868 Handle(TDataStd_Comment) aGroup;
869 if (aLabIter.Value().FindAttribute(TDataStd_Comment::GetID(), aGroup)) {
870 return TCollection_AsciiString(aGroup->Get()).ToCString();
874 static std::string anEmpty;
875 return anEmpty; // not found
878 void Model_Objects::updateResults(FeaturePtr theFeature)
880 // for not persistent is will be done by parametric updater automatically
881 //if (!theFeature->isPersistentResult()) return;
882 // check the existing results and remove them if there is nothing on the label
883 std::list<ResultPtr>::const_iterator aResIter = theFeature->results().cbegin();
884 while(aResIter != theFeature->results().cend()) {
885 ResultPtr aBody = std::dynamic_pointer_cast<ModelAPI_Result>(*aResIter);
887 if (!aBody->data()->isValid()) {
888 // found a disappeared result => remove it
889 theFeature->eraseResultFromList(aBody);
890 // start iterate from beginning because iterator is corrupted by removing
891 aResIter = theFeature->results().cbegin();
898 if (!theFeature->data() || !theFeature->data()->isValid() || theFeature->isDisabled())
900 // check that results are presented on all labels
901 int aResSize = theFeature->results().size();
902 TDF_ChildIterator aLabIter(resultLabel(theFeature->data(), 0).Father());
903 for(; aLabIter.More(); aLabIter.Next()) {
904 // here must be GUID of the feature
905 int aResIndex = aLabIter.Value().Tag() - 1;
907 if (aResSize <= aResIndex) {
908 TDF_Label anArgLab = aLabIter.Value();
909 Handle(TDataStd_Comment) aGroup;
910 if (anArgLab.FindAttribute(TDataStd_Comment::GetID(), aGroup)) {
911 if (aGroup->Get() == ModelAPI_ResultBody::group().c_str()) {
912 aNewBody = createBody(theFeature->data(), aResIndex);
913 } else if (aGroup->Get() == ModelAPI_ResultPart::group().c_str()) {
914 std::shared_ptr<ModelAPI_ResultPart> aNewP = createPart(theFeature->data(), aResIndex);
915 theFeature->setResult(aNewP, aResIndex);
916 if (!aNewP->partDoc().get())
917 theFeature->execute(); // create the part result: it is better to restore the previous result if it is possible
919 } else if (aGroup->Get() == ModelAPI_ResultConstruction::group().c_str()) {
920 theFeature->execute(); // construction shapes are needed for sketch solver
922 } else if (aGroup->Get() == ModelAPI_ResultGroup::group().c_str()) {
923 aNewBody = createGroup(theFeature->data(), aResIndex);
924 } else if (aGroup->Get() == ModelAPI_ResultParameter::group().c_str()) {
925 theFeature->attributeChanged("expression"); // just produce a value
928 Events_Error::send(std::string("Unknown type of result is found in the document:") +
929 TCollection_AsciiString(aGroup->Get()).ToCString());
933 theFeature->setResult(aNewBody, aResIndex);
939 ResultPtr Model_Objects::findByName(const std::string theName)
941 NCollection_DataMap<TDF_Label, FeaturePtr>::Iterator anObjIter(myFeatures);
942 for(; anObjIter.More(); anObjIter.Next()) {
943 FeaturePtr& aFeature = anObjIter.ChangeValue();
944 if (!aFeature.get() || aFeature->isDisabled()) // may be on close
946 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aFeature->results();
947 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
948 for (; aRIter != aResults.cend(); aRIter++) {
949 ResultPtr aRes = *aRIter;
950 if (aRes.get() && aRes->data() && aRes->data()->isValid() && !aRes->isDisabled() &&
951 aRes->data()->name() == theName) {
960 FeaturePtr Model_Objects::nextFeature(FeaturePtr theCurrent, const bool theReverse)
962 std::shared_ptr<Model_Data> aData = std::static_pointer_cast<Model_Data>(theCurrent->data());
963 if (aData && aData->isValid()) {
964 TDF_Label aFeatureLabel = aData->label().Father();
965 Handle(TDataStd_ReferenceArray) aRefs;
966 if (featuresLabel().FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
967 for(int a = aRefs->Lower(); a <= aRefs->Upper(); a++) { // iterate all existing features
968 if (aRefs->Value(a).IsEqual(aFeatureLabel)) {
969 a += theReverse ? -1 : 1;
970 if (a >= aRefs->Lower() && a <= aRefs->Upper())
971 return feature(aRefs->Value(a));
972 break; // finish iiteration: it's last feature
977 return FeaturePtr(); // not found, last, or something is wrong
980 FeaturePtr Model_Objects::firstFeature()
982 Handle(TDataStd_ReferenceArray) aRefs;
983 if (featuresLabel().FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
984 return feature(aRefs->Value(aRefs->Lower()));
986 return FeaturePtr(); // no features at all
989 FeaturePtr Model_Objects::lastFeature()
991 Handle(TDataStd_ReferenceArray) aRefs;
992 if (featuresLabel().FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
993 return feature(aRefs->Value(aRefs->Upper()));
995 return FeaturePtr(); // no features at all
998 std::list<std::shared_ptr<ModelAPI_Feature> > Model_Objects::allFeatures()
1000 std::list<std::shared_ptr<ModelAPI_Feature> > aResult;
1001 Handle(TDataStd_ReferenceArray) aRefs;
1002 if (featuresLabel().FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
1003 for(int a = aRefs->Lower(); a <= aRefs->Upper(); a++) {
1004 FeaturePtr aFeature = feature(aRefs->Value(a));
1006 aResult.push_back(aFeature);
1012 int Model_Objects::numInternalFeatures()
1014 Handle(TDataStd_ReferenceArray) aRefs;
1015 if (featuresLabel().FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
1016 return aRefs->Upper() - aRefs->Lower() + 1;
1018 return 0; // invalid
1021 std::shared_ptr<ModelAPI_Feature> Model_Objects::internalFeature(const int theIndex)
1023 Handle(TDataStd_ReferenceArray) aRefs;
1024 if (featuresLabel().FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
1025 return feature(aRefs->Value(aRefs->Lower() + theIndex));
1027 return FeaturePtr(); // invalid
1030 Standard_Integer HashCode(const TDF_Label& theLab, const Standard_Integer theUpper)
1032 return TDF_LabelMapHasher::HashCode(theLab, theUpper);
1035 Standard_Boolean IsEqual(const TDF_Label& theLab1, const TDF_Label& theLab2)
1037 return TDF_LabelMapHasher::IsEqual(theLab1, theLab2);