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 // erase one by one to avoid access from the feature destructor itself from he map
63 while(!myFeatures.IsEmpty()) {
64 NCollection_DataMap<TDF_Label, FeaturePtr>::Iterator aFeaturesIter(myFeatures);
65 FeaturePtr aFeature = aFeaturesIter.Value();
66 static Events_ID EVENT_DISP = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
67 ModelAPI_EventCreator::get()->sendDeleted(myDoc, ModelAPI_Feature::group());
68 ModelAPI_EventCreator::get()->sendUpdated(aFeature, EVENT_DISP);
69 aFeature->eraseResults();
71 myFeatures.UnBind(aFeaturesIter.Key());
73 aLoop->flush(Events_Loop::eventByName(EVENT_OBJECT_DELETED));
74 aLoop->flush(Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
78 /// Appends to the array of references a new referenced label
79 static void AddToRefArray(TDF_Label& theArrayLab, TDF_Label& theReferenced, TDF_Label& thePrevLab)
81 Handle(TDataStd_ReferenceArray) aRefs;
82 if (!theArrayLab.FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
83 aRefs = TDataStd_ReferenceArray::Set(theArrayLab, 0, 0);
84 aRefs->SetValue(0, theReferenced);
85 } else { // extend array by one more element
86 Handle(TDataStd_HLabelArray1) aNewArray = new TDataStd_HLabelArray1(aRefs->Lower(),
88 int aPassedPrev = 0; // prev feature is found and passed
89 if (thePrevLab.IsNull()) { // null means that inserted feature must be the first
90 aNewArray->SetValue(aRefs->Lower(), theReferenced);
93 for (int a = aRefs->Lower(); a <= aRefs->Upper(); a++) {
94 aNewArray->SetValue(a + aPassedPrev, aRefs->Value(a));
95 if (!aPassedPrev && aRefs->Value(a).IsEqual(thePrevLab)) {
97 aNewArray->SetValue(a + 1, theReferenced);
100 if (!aPassedPrev) // not found: unknown situation
101 aNewArray->SetValue(aRefs->Upper() + 1, theReferenced);
102 aRefs->SetInternalArray(aNewArray);
106 void Model_Objects::addFeature(FeaturePtr theFeature, const FeaturePtr theAfterThis)
108 if (!theFeature->isAction()) { // do not add action to the data model
109 TDF_Label aFeaturesLab = featuresLabel();
110 TDF_Label aFeatureLab = aFeaturesLab.NewChild();
111 // store feature in the features array: before "initData" because in macro features
112 // in initData it creates new features, appeared later than this
113 TDF_Label aPrevFeateureLab;
114 if (theAfterThis.get()) { // searching for the previous feature label
115 std::shared_ptr<Model_Data> aPrevData =
116 std::dynamic_pointer_cast<Model_Data>(theAfterThis->data());
117 if (aPrevData.get()) {
118 aPrevFeateureLab = aPrevData->label().Father();
121 AddToRefArray(aFeaturesLab, aFeatureLab, aPrevFeateureLab);
123 // keep the feature ID to restore document later correctly
124 TDataStd_Comment::Set(aFeatureLab, theFeature->getKind().c_str());
125 myFeatures.Bind(aFeatureLab, theFeature);
126 // must be after binding to the map because of "Box" macro feature that
127 // creates other features in "initData"
128 initData(theFeature, aFeatureLab, TAG_FEATURE_ARGUMENTS);
129 // event: feature is added
130 static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_CREATED);
131 ModelAPI_EventCreator::get()->sendUpdated(theFeature, anEvent);
132 theFeature->setDisabled(false); // by default created feature is enabled
133 updateHistory(ModelAPI_Feature::group());
134 } else { // make feature has not-null data anyway
135 theFeature->setData(Model_Data::invalidData());
136 theFeature->setDoc(myDoc);
140 /// Appends to the array of references a new referenced label.
141 /// If theIndex is not -1, removes element at this index, not theReferenced.
142 /// \returns the index of removed element
143 static int RemoveFromRefArray(TDF_Label theArrayLab, TDF_Label theReferenced,
144 const int theIndex = -1)
146 int aResult = -1; // no returned
147 Handle(TDataStd_ReferenceArray) aRefs;
148 if (theArrayLab.FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
149 if (aRefs->Length() == 1) { // just erase an array
150 if ((theIndex == -1 && aRefs->Value(0) == theReferenced) || theIndex == 0) {
151 theArrayLab.ForgetAttribute(TDataStd_ReferenceArray::GetID());
154 } else { // reduce the array
155 Handle(TDataStd_HLabelArray1) aNewArray = new TDataStd_HLabelArray1(aRefs->Lower(),
157 int aCount = aRefs->Lower();
158 for (int a = aCount; a <= aRefs->Upper(); a++, aCount++) {
159 if ((theIndex == -1 && aRefs->Value(a) == theReferenced) || theIndex == a) {
163 aNewArray->SetValue(aCount, aRefs->Value(a));
166 aRefs->SetInternalArray(aNewArray);
172 void Model_Objects::refsToFeature(FeaturePtr theFeature,
173 std::set<std::shared_ptr<ModelAPI_Feature> >& theRefs, const bool isSendError)
175 // check the feature: it must have no depended objects on it
176 // the dependencies can be in the feature results
177 std::list<ResultPtr>::const_iterator aResIter = theFeature->results().cbegin();
178 for(; aResIter != theFeature->results().cend(); aResIter++) {
179 ResultPtr aResult = (*aResIter);
180 std::shared_ptr<Model_Data> aData =
181 std::dynamic_pointer_cast<Model_Data>(aResult->data());
182 if (aData.get() != NULL) {
183 const std::set<AttributePtr>& aRefs = aData->refsToMe();
184 std::set<AttributePtr>::const_iterator aRefIt = aRefs.begin(), aRefLast = aRefs.end();
185 for(; aRefIt != aRefLast; aRefIt++) {
186 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>((*aRefIt)->owner());
187 if (aFeature.get() != NULL)
188 theRefs.insert(aFeature);
192 // the dependencies can be in the feature itself
193 std::shared_ptr<Model_Data> aData =
194 std::dynamic_pointer_cast<Model_Data>(theFeature->data());
195 if (aData && !aData->refsToMe().empty()) {
196 const std::set<AttributePtr>& aRefs = aData->refsToMe();
197 std::set<AttributePtr>::const_iterator aRefIt = aRefs.begin(), aRefLast = aRefs.end();
198 for(; aRefIt != aRefLast; aRefIt++) {
199 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>((*aRefIt)->owner());
200 if (aFeature.get() != NULL)
201 theRefs.insert(aFeature);
205 if (!theRefs.empty() && isSendError) {
207 "Feature '" + theFeature->data()->name() + "' is used and can not be deleted");
211 void Model_Objects::removeFeature(FeaturePtr theFeature)
213 std::shared_ptr<Model_Data> aData = std::static_pointer_cast<Model_Data>(theFeature->data());
214 if (aData && aData->isValid()) {
215 // checking that the sub-element of composite feature is removed: if yes, inform the owner
216 std::set<std::shared_ptr<ModelAPI_Feature> > aRefs;
217 refsToFeature(theFeature, aRefs, false);
218 std::set<std::shared_ptr<ModelAPI_Feature> >::iterator aRefIter = aRefs.begin();
219 for(; aRefIter != aRefs.end(); aRefIter++) {
220 std::shared_ptr<ModelAPI_CompositeFeature> aComposite =
221 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(*aRefIter);
222 if (aComposite.get()) {
223 aComposite->removeFeature(theFeature);
226 // this must be before erase since theFeature erasing removes all information about
227 // the feature results and groups of results
228 // To reproduce: create sketch, extrusion, remove sketch => constructions tree is not updated
229 clearHistory(theFeature);
233 TDF_Label aFeatureLabel = aData->label().Father();
234 if (myFeatures.IsBound(aFeatureLabel))
235 myFeatures.UnBind(aFeatureLabel);
237 static Events_ID EVENT_DISP = Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
238 ModelAPI_EventCreator::get()->sendUpdated(theFeature, EVENT_DISP);
239 // erase all attributes under the label of feature
240 aFeatureLabel.ForgetAllAttributes();
241 // remove it from the references array
242 RemoveFromRefArray(featuresLabel(), aFeatureLabel);
243 // event: feature is deleted
244 ModelAPI_EventCreator::get()->sendDeleted(theFeature->document(), ModelAPI_Feature::group());
245 // the redisplay signal should be flushed in order to erase the feature presentation in the viewer
246 Events_Loop::loop()->flush(EVENT_DISP);
247 updateHistory(ModelAPI_Feature::group());
251 void Model_Objects::moveFeature(FeaturePtr theMoved, FeaturePtr theAfterThis)
253 TDF_Label aFeaturesLab = featuresLabel();
254 Handle(TDataStd_ReferenceArray) aRefs;
255 if (!aFeaturesLab.FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs))
257 TDF_Label anAfterLab, aMovedLab =
258 std::dynamic_pointer_cast<Model_Data>(theMoved->data())->label().Father();
259 if (theAfterThis.get())
260 anAfterLab = std::dynamic_pointer_cast<Model_Data>(theAfterThis->data())->label().Father();
262 Handle(TDataStd_HLabelArray1) aNewArray =
263 new TDataStd_HLabelArray1(aRefs->Lower(), aRefs->Upper());
264 int aPassedMovedFrom = 0; // the prev feature location is found and passed
265 int aPassedMovedTo = 0; // the feature is added and this location is passed
266 if (!theAfterThis.get()) { // null means that inserted feature must be the first
267 aNewArray->SetValue(aRefs->Lower(), aMovedLab);
270 for (int a = aRefs->Lower(); a <= aRefs->Upper(); a++) {
271 if (aPassedMovedTo == 0 && aRefs->Value(a) == anAfterLab) { // add two
273 aNewArray->SetValue(a - aPassedMovedFrom, anAfterLab);
274 if (a + 1 - aPassedMovedFrom <= aRefs->Upper())
275 aNewArray->SetValue(a + 1 - aPassedMovedFrom, aMovedLab);
276 } else if (aPassedMovedFrom == 0 && aRefs->Value(a) == aMovedLab) { // skip
278 } else { // just copy one
279 if (a - aPassedMovedFrom + aPassedMovedTo <= aRefs->Upper())
280 aNewArray->SetValue(a - aPassedMovedFrom + aPassedMovedTo, aRefs->Value(a));
283 if (!aPassedMovedFrom || !aPassedMovedTo) {// not found: unknown situation
284 if (!aPassedMovedFrom) {
285 static std::string aMovedFromError("The moved feature is not found");
286 Events_Error::send(aMovedFromError);
288 static std::string aMovedToError("The 'after' feature for movement is not found");
289 Events_Error::send(aMovedToError);
293 // store the new array
294 aRefs->SetInternalArray(aNewArray);
295 // update the feature and the history
296 clearHistory(theMoved);
297 static Events_ID EVENT_UPD = Events_Loop::loop()->eventByName(EVENT_OBJECT_UPDATED);
298 ModelAPI_EventCreator::get()->sendUpdated(theMoved, EVENT_UPD);
301 void Model_Objects::clearHistory(ObjectPtr theObj)
304 const std::string aGroup = theObj->groupName();
305 std::map<std::string, std::vector<ObjectPtr> >::iterator aHIter = myHistory.find(aGroup);
306 if (aHIter != myHistory.end())
307 myHistory.erase(aHIter); // erase from map => this means that it is not synchronized
308 if (theObj->groupName() == ModelAPI_Feature::group()) { // clear results group of the feature
309 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObj);
310 std::string aResultGroup = featureResultGroup(aFeature);
311 if (!aResultGroup.empty()) {
312 std::map<std::string, std::vector<ObjectPtr> >::iterator aHIter =
313 myHistory.find(aResultGroup);
314 if (aHIter != myHistory.end())
315 myHistory.erase(aHIter); // erase from map => this means that it is not synchronized
321 void Model_Objects::createHistory(const std::string& theGroupID)
323 std::map<std::string, std::vector<ObjectPtr> >::iterator aHIter = myHistory.find(theGroupID);
324 if (aHIter == myHistory.end()) {
325 myHistory[theGroupID] = std::vector<ObjectPtr>();
326 std::vector<ObjectPtr>& aResult = myHistory[theGroupID];
327 // iterate the array of references and get feature by feature from the array
328 bool isFeature = theGroupID == ModelAPI_Feature::group();
329 Handle(TDataStd_ReferenceArray) aRefs;
330 if (featuresLabel().FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
331 for(int a = aRefs->Lower(); a <= aRefs->Upper(); a++) {
332 FeaturePtr aFeature = feature(aRefs->Value(a));
333 if (aFeature.get()) {
334 // if feature is in sub-component, remove it from history: it is in sub-tree of sub-component
335 if (!ModelAPI_Tools::compositeOwner(aFeature).get()) {
336 if (isFeature) { // here may be also disabled features
337 if (aFeature->isInHistory()) {
338 aResult.push_back(aFeature);
340 } else if (!aFeature->isDisabled()) { // iterate all results of not-disabled feature
341 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aFeature->results();
342 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
343 for (; aRIter != aResults.cend(); aRIter++) {
344 ResultPtr aRes = *aRIter;
345 if (aRes->groupName() != theGroupID) break; // feature have only same group results
346 if (!aRes->isDisabled() && aRes->isInHistory() && !aRes->isConcealed()) {
347 aResult.push_back(*aRIter);
358 void Model_Objects::updateHistory(const std::shared_ptr<ModelAPI_Object> theObject)
360 clearHistory(theObject);
363 void Model_Objects::updateHistory(const std::string theGroup)
365 std::map<std::string, std::vector<ObjectPtr> >::iterator aHIter = myHistory.find(theGroup);
366 if (aHIter != myHistory.end())
367 myHistory.erase(aHIter); // erase from map => this means that it is not synchronized
370 FeaturePtr Model_Objects::feature(TDF_Label theLabel) const
372 if (myFeatures.IsBound(theLabel))
373 return myFeatures.Find(theLabel);
374 return FeaturePtr(); // not found
377 ObjectPtr Model_Objects::object(TDF_Label theLabel)
379 // try feature by label
380 FeaturePtr aFeature = feature(theLabel);
382 return feature(theLabel);
383 TDF_Label aFeatureLabel = theLabel.Father().Father(); // let's suppose it is result
384 aFeature = feature(aFeatureLabel);
386 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aFeature->results();
387 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.cbegin();
388 for (; aRIter != aResults.cend(); aRIter++) {
389 std::shared_ptr<Model_Data> aResData = std::dynamic_pointer_cast<Model_Data>(
391 if (aResData->label().Father().IsEqual(theLabel))
395 return FeaturePtr(); // not found
398 ObjectPtr Model_Objects::object(const std::string& theGroupID, const int theIndex)
402 createHistory(theGroupID);
403 return myHistory[theGroupID][theIndex];
406 std::shared_ptr<ModelAPI_Object> Model_Objects::objectByName(
407 const std::string& theGroupID, const std::string& theName)
409 createHistory(theGroupID);
410 std::vector<ObjectPtr>& allObjs = myHistory[theGroupID];
411 std::vector<ObjectPtr>::iterator anObjIter = allObjs.begin();
412 for(; anObjIter != allObjs.end(); anObjIter++) {
413 if ((*anObjIter)->data()->name() == theName)
420 const int Model_Objects::index(std::shared_ptr<ModelAPI_Object> theObject)
422 std::string aGroup = theObject->groupName();
423 createHistory(aGroup);
424 std::vector<ObjectPtr>& allObjs = myHistory[aGroup];
425 std::vector<ObjectPtr>::iterator anObjIter = allObjs.begin(); // iterate to search object
426 for(int anIndex = 0; anObjIter != allObjs.end(); anObjIter++, anIndex++) {
427 if ((*anObjIter) == theObject)
434 int Model_Objects::size(const std::string& theGroupID)
436 createHistory(theGroupID);
437 return myHistory[theGroupID].size();
440 void Model_Objects::allResults(const std::string& theGroupID, std::list<ResultPtr>& theResults)
442 // iterate the array of references and get feature by feature from the array
443 Handle(TDataStd_ReferenceArray) aRefs;
444 if (featuresLabel().FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
445 for(int a = aRefs->Lower(); a <= aRefs->Upper(); a++) {
446 FeaturePtr aFeature = feature(aRefs->Value(a));
447 if (aFeature.get()) {
448 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aFeature->results();
449 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
450 for (; aRIter != aResults.cend(); aRIter++) {
451 ResultPtr aRes = *aRIter;
452 if (aRes->groupName() != theGroupID) break; // feature have only same group results
453 if (aRes->isInHistory() && !aRes->isConcealed()) {
454 theResults.push_back(*aRIter);
463 TDF_Label Model_Objects::featuresLabel() const
465 return myMain.FindChild(TAG_OBJECTS);
468 void Model_Objects::setUniqueName(FeaturePtr theFeature)
470 if (!theFeature->data()->name().empty())
471 return; // not needed, name is already defined
472 std::string aName; // result
473 // first count all features of such kind to start with index = count + 1
474 int aNumObjects = -1; // this feature is already in this map
475 NCollection_DataMap<TDF_Label, FeaturePtr>::Iterator aFIter(myFeatures);
476 for (; aFIter.More(); aFIter.Next()) {
477 if (aFIter.Value()->getKind() == theFeature->getKind())
480 // generate candidate name
481 std::stringstream aNameStream;
482 aNameStream << theFeature->getKind() << "_" << aNumObjects + 1;
483 aName = aNameStream.str();
484 // check this is unique, if not, increase index by 1
485 for (aFIter.Initialize(myFeatures); aFIter.More();) {
486 FeaturePtr aFeature = aFIter.Value();
487 bool isSameName = aFeature->data()->name() == aName;
488 if (!isSameName) { // check also results to avoid same results names (actual for Parts)
489 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aFeature->results();
490 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
491 for (; aRIter != aResults.cend(); aRIter++) {
492 isSameName = (*aRIter)->data()->name() == aName;
497 std::stringstream aNameStream;
498 aNameStream << theFeature->getKind() << "_" << aNumObjects + 1;
499 aName = aNameStream.str();
500 // reinitialize iterator to make sure a new name is unique
501 aFIter.Initialize(myFeatures);
505 theFeature->data()->setName(aName);
508 void Model_Objects::initData(ObjectPtr theObj, TDF_Label theLab, const int theTag)
510 std::shared_ptr<Model_Data> aData(new Model_Data);
511 aData->setLabel(theLab.FindChild(theTag));
512 aData->setObject(theObj);
513 theObj->setDoc(myDoc);
514 theObj->setData(aData);
515 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObj);
517 setUniqueName(aFeature); // must be before "initAttributes" because duplicate part uses name
519 theObj->initAttributes();
522 void Model_Objects::synchronizeFeatures(
523 const bool theMarkUpdated, const bool theUpdateReferences, const bool theFlush)
525 Model_Document* anOwner = std::dynamic_pointer_cast<Model_Document>(myDoc).get();
526 if (!anOwner) // this may happen on creation of document: nothing there, so nothing to synchronize
528 // after all updates, sends a message that groups of features were created or updated
529 Events_Loop* aLoop = Events_Loop::loop();
530 static Events_ID aDispEvent = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
531 static Events_ID aCreateEvent = Events_Loop::eventByName(EVENT_OBJECT_CREATED);
532 static Events_ID anUpdateEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
533 static Events_ID aRedispEvent = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
534 static Events_ID aDeleteEvent = Events_Loop::eventByName(EVENT_OBJECT_DELETED);
535 static Events_ID aToHideEvent = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
536 bool isActive = aLoop->activateFlushes(false);
538 // update all objects by checking are they on labels or not
539 std::set<FeaturePtr> aNewFeatures, aKeptFeatures;
540 TDF_ChildIDIterator aLabIter(featuresLabel(), TDataStd_Comment::GetID());
541 for (; aLabIter.More(); aLabIter.Next()) {
542 TDF_Label aFeatureLabel = aLabIter.Value()->Label();
544 if (!myFeatures.IsBound(aFeatureLabel)) { // a new feature is inserted
546 aFeature = std::dynamic_pointer_cast<Model_Session>(ModelAPI_Session::get())->createFeature(
547 TCollection_AsciiString(Handle(TDataStd_Comment)::DownCast(aLabIter.Value())->Get())
548 .ToCString(), anOwner);
549 if (!aFeature) { // somethig is wrong, most probably, the opened document has invalid structure
550 Events_Error::send("Invalid type of object in the document");
551 aLabIter.Value()->Label().ForgetAllAttributes();
554 // this must be before "setData" to redo the sketch line correctly
555 myFeatures.Bind(aFeatureLabel, aFeature);
556 aNewFeatures.insert(aFeature);
557 initData(aFeature, aFeatureLabel, TAG_FEATURE_ARGUMENTS);
558 updateHistory(aFeature);
559 aFeature->setDisabled(false); // by default created feature is enabled (this allows to recreate the results before "setCurrent" is called)
561 // event: model is updated
562 ModelAPI_EventCreator::get()->sendUpdated(aFeature, aCreateEvent);
563 } else { // nothing is changed, both iterators are incremented
564 aFeature = myFeatures.Find(aFeatureLabel);
565 aKeptFeatures.insert(aFeature);
566 if (theMarkUpdated) {
567 ModelAPI_EventCreator::get()->sendUpdated(aFeature, anUpdateEvent);
572 // check all features are checked: if not => it was removed
573 NCollection_DataMap<TDF_Label, FeaturePtr>::Iterator aFIter(myFeatures);
574 while (aFIter.More()) {
575 if (aKeptFeatures.find(aFIter.Value()) == aKeptFeatures.end()
576 && aNewFeatures.find(aFIter.Value()) == aNewFeatures.end()) {
577 FeaturePtr aFeature = aFIter.Value();
578 // event: model is updated
579 //if (aFeature->isInHistory()) {
580 ModelAPI_EventCreator::get()->sendDeleted(myDoc, ModelAPI_Feature::group());
582 // results of this feature must be redisplayed (hided)
583 // redisplay also removed feature (used for sketch and AISObject)
584 ModelAPI_EventCreator::get()->sendUpdated(aFeature, aRedispEvent);
585 updateHistory(aFeature);
587 // unbind after the "erase" call: on abort sketch is removes sub-objects that corrupts aFIter
588 myFeatures.UnBind(aFIter.Key());
589 // reinitialize iterator because unbind may corrupt the previous order in the map
590 aFIter.Initialize(myFeatures);
595 if (theUpdateReferences) {
596 synchronizeBackRefs();
598 // update results of the features (after features created because they may be connected, like sketch and sub elements)
599 // After synchronisation of back references because sketch must be set in sub-elements before "execute" by updateResults
600 std::list<FeaturePtr> aComposites; // composites must be updated after their subs (issue 360)
601 TDF_ChildIDIterator aLabIter2(featuresLabel(), TDataStd_Comment::GetID());
602 for (; aLabIter2.More(); aLabIter2.Next()) {
603 TDF_Label aFeatureLabel = aLabIter2.Value()->Label();
604 if (myFeatures.IsBound(aFeatureLabel)) { // a new feature is inserted
605 FeaturePtr aFeature = myFeatures.Find(aFeatureLabel);
606 if (std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aFeature).get())
607 aComposites.push_back(aFeature);
608 updateResults(aFeature);
611 std::list<FeaturePtr>::iterator aComposite = aComposites.begin();
612 for(; aComposite != aComposites.end(); aComposite++) {
613 updateResults(*aComposite);
616 // the synchronize should be done after updateResults in order to correct back references of updated results
617 if (theUpdateReferences) {
618 synchronizeBackRefs();
620 if (theMarkUpdated) { // this means there is no control what was modified => remove history cash
624 anOwner->executeFeatures() = false;
625 aLoop->activateFlushes(isActive);
628 aLoop->flush(aCreateEvent);
629 aLoop->flush(aDeleteEvent);
630 aLoop->flush(anUpdateEvent);
631 aLoop->flush(aRedispEvent);
632 aLoop->flush(aToHideEvent);
634 anOwner->executeFeatures() = true;
637 void Model_Objects::synchronizeBackRefs()
639 // keeps the concealed flags of result to catch the change and create created/deleted events
640 std::list<std::pair<ResultPtr, bool> > aConcealed;
641 // first cycle: erase all data about back-references
642 NCollection_DataMap<TDF_Label, FeaturePtr>::Iterator aFeatures(myFeatures);
643 for(; aFeatures.More(); aFeatures.Next()) {
644 FeaturePtr aFeature = aFeatures.Value();
645 std::shared_ptr<Model_Data> aFData =
646 std::dynamic_pointer_cast<Model_Data>(aFeature->data());
648 aFData->eraseBackReferences();
650 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aFeature->results();
651 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
652 for (; aRIter != aResults.cend(); aRIter++) {
653 std::shared_ptr<Model_Data> aResData =
654 std::dynamic_pointer_cast<Model_Data>((*aRIter)->data());
656 aConcealed.push_back(std::pair<ResultPtr, bool>(*aRIter, (*aRIter)->isConcealed()));
657 aResData->eraseBackReferences();
662 // second cycle: set new back-references: only features may have reference, iterate only them
663 ModelAPI_ValidatorsFactory* aValidators = ModelAPI_Session::get()->validators();
664 for(aFeatures.Initialize(myFeatures); aFeatures.More(); aFeatures.Next()) {
665 FeaturePtr aFeature = aFeatures.Value();
666 std::shared_ptr<Model_Data> aFData =
667 std::dynamic_pointer_cast<Model_Data>(aFeature->data());
669 std::list<std::pair<std::string, std::list<ObjectPtr> > > aRefs;
670 aFData->referencesToObjects(aRefs);
671 std::list<std::pair<std::string, std::list<ObjectPtr> > >::iterator
672 aRefsIter = aRefs.begin();
673 for(; aRefsIter != aRefs.end(); aRefsIter++) {
674 std::list<ObjectPtr>::iterator aRefTo = aRefsIter->second.begin();
675 for(; aRefTo != aRefsIter->second.end(); aRefTo++) {
677 std::shared_ptr<Model_Data> aRefData =
678 std::dynamic_pointer_cast<Model_Data>((*aRefTo)->data());
679 aRefData->addBackReference(aFeature, aRefsIter->first); // here the Concealed flag is updated
680 // update enable/disable status: the nested status must be equal to the composite
681 CompositeFeaturePtr aComp =
682 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aFeature);
684 FeaturePtr aReferenced = std::dynamic_pointer_cast<ModelAPI_Feature>(*aRefTo);
685 if (aReferenced.get()) {
686 aReferenced->setDisabled(aComp->isDisabled());
694 std::list<std::pair<ResultPtr, bool> >::iterator aCIter = aConcealed.begin();
695 for(; aCIter != aConcealed.end(); aCIter++) {
696 if (aCIter->first->isConcealed() != aCIter->second) { // somethign is changed => produce event
697 if (aCIter->second) { // was concealed become not => creation event
698 static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_CREATED);
699 ModelAPI_EventCreator::get()->sendUpdated(aCIter->first, anEvent);
700 } else { // was not concealed become concealed => delete event
701 ModelAPI_EventCreator::get()->sendDeleted(myDoc, aCIter->first->groupName());
702 // redisplay for the viewer (it must be disappeared also)
703 static Events_ID EVENT_DISP =
704 Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
705 ModelAPI_EventCreator::get()->sendUpdated(aCIter->first, EVENT_DISP);
711 TDF_Label Model_Objects::resultLabel(
712 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theResultIndex)
714 const std::shared_ptr<Model_Data>& aData =
715 std::dynamic_pointer_cast<Model_Data>(theFeatureData);
716 return aData->label().Father().FindChild(TAG_FEATURE_RESULTS).FindChild(theResultIndex + 1);
719 void Model_Objects::storeResult(std::shared_ptr<ModelAPI_Data> theFeatureData,
720 std::shared_ptr<ModelAPI_Result> theResult,
721 const int theResultIndex)
723 theResult->setDoc(myDoc);
724 initData(theResult, resultLabel(theFeatureData, theResultIndex), TAG_FEATURE_ARGUMENTS);
725 if (theResult->data()->name().empty()) { // if was not initialized, generate event and set a name
726 std::stringstream aNewName;
727 aNewName<<theFeatureData->name();
728 if (theResultIndex > 0) // if there are several results, add unique prefix starting from second
729 aNewName<<"_"<<theResultIndex + 1;
730 theResult->data()->setName(aNewName.str());
734 std::shared_ptr<ModelAPI_ResultConstruction> Model_Objects::createConstruction(
735 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex)
737 TDF_Label aLab = resultLabel(theFeatureData, theIndex);
738 TDataStd_Comment::Set(aLab, ModelAPI_ResultConstruction::group().c_str());
739 ObjectPtr anOldObject = object(aLab);
740 std::shared_ptr<ModelAPI_ResultConstruction> aResult;
742 aResult = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(anOldObject);
745 aResult = std::shared_ptr<ModelAPI_ResultConstruction>(new Model_ResultConstruction);
746 storeResult(theFeatureData, aResult, theIndex);
751 std::shared_ptr<ModelAPI_ResultBody> Model_Objects::createBody(
752 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex)
754 TDF_Label aLab = resultLabel(theFeatureData, theIndex);
755 TDataStd_Comment::Set(aLab, ModelAPI_ResultBody::group().c_str());
756 ObjectPtr anOldObject = object(aLab);
757 std::shared_ptr<ModelAPI_ResultBody> aResult;
759 aResult = std::dynamic_pointer_cast<ModelAPI_ResultBody>(anOldObject);
762 aResult = std::shared_ptr<ModelAPI_ResultBody>(new Model_ResultBody);
763 storeResult(theFeatureData, aResult, theIndex);
768 std::shared_ptr<ModelAPI_ResultCompSolid> Model_Objects::createCompSolid(
769 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex)
771 TDF_Label aLab = resultLabel(theFeatureData, theIndex);
772 TDataStd_Comment::Set(aLab, ModelAPI_ResultCompSolid::group().c_str());
773 ObjectPtr anOldObject = object(aLab);
774 std::shared_ptr<ModelAPI_ResultCompSolid> aResult;
776 aResult = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(anOldObject);
779 aResult = std::shared_ptr<ModelAPI_ResultCompSolid>(new Model_ResultCompSolid);
780 storeResult(theFeatureData, aResult, theIndex);
785 std::shared_ptr<ModelAPI_ResultPart> Model_Objects::createPart(
786 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex)
788 TDF_Label aLab = resultLabel(theFeatureData, theIndex);
789 TDataStd_Comment::Set(aLab, ModelAPI_ResultPart::group().c_str());
790 ObjectPtr anOldObject = object(aLab);
791 std::shared_ptr<ModelAPI_ResultPart> aResult;
793 aResult = std::dynamic_pointer_cast<ModelAPI_ResultPart>(anOldObject);
796 aResult = std::shared_ptr<ModelAPI_ResultPart>(new Model_ResultPart);
797 storeResult(theFeatureData, aResult, theIndex);
802 std::shared_ptr<ModelAPI_ResultPart> Model_Objects::copyPart(
803 const std::shared_ptr<ModelAPI_ResultPart>& theOrigin,
804 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex)
806 std::shared_ptr<ModelAPI_ResultPart> aResult = createPart(theFeatureData, theIndex);
807 aResult->data()->reference(Model_ResultPart::BASE_REF_ID())->setValue(theOrigin);
811 std::shared_ptr<ModelAPI_ResultGroup> Model_Objects::createGroup(
812 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex)
814 TDF_Label aLab = resultLabel(theFeatureData, theIndex);
815 TDataStd_Comment::Set(aLab, ModelAPI_ResultGroup::group().c_str());
816 ObjectPtr anOldObject = object(aLab);
817 std::shared_ptr<ModelAPI_ResultGroup> aResult;
819 aResult = std::dynamic_pointer_cast<ModelAPI_ResultGroup>(anOldObject);
822 aResult = std::shared_ptr<ModelAPI_ResultGroup>(new Model_ResultGroup(theFeatureData));
823 storeResult(theFeatureData, aResult, theIndex);
828 std::shared_ptr<ModelAPI_ResultParameter> Model_Objects::createParameter(
829 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex)
831 TDF_Label aLab = resultLabel(theFeatureData, theIndex);
832 TDataStd_Comment::Set(aLab, ModelAPI_ResultParameter::group().c_str());
833 ObjectPtr anOldObject = object(aLab);
834 std::shared_ptr<ModelAPI_ResultParameter> aResult;
836 aResult = std::dynamic_pointer_cast<ModelAPI_ResultParameter>(anOldObject);
839 aResult = std::shared_ptr<ModelAPI_ResultParameter>(new Model_ResultParameter);
840 storeResult(theFeatureData, aResult, theIndex);
845 std::shared_ptr<ModelAPI_Feature> Model_Objects::feature(
846 const std::shared_ptr<ModelAPI_Result>& theResult)
848 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(theResult->data());
850 TDF_Label aFeatureLab = aData->label().Father().Father().Father();
851 return feature(aFeatureLab);
856 std::string Model_Objects::featureResultGroup(FeaturePtr theFeature)
858 if (theFeature->data()->isValid()) {
859 TDF_ChildIterator aLabIter(resultLabel(theFeature->data(), 0).Father());
860 if (aLabIter.More()) {
861 TDF_Label anArgLab = aLabIter.Value();
862 Handle(TDataStd_Comment) aGroup;
863 if (aLabIter.Value().FindAttribute(TDataStd_Comment::GetID(), aGroup)) {
864 return TCollection_AsciiString(aGroup->Get()).ToCString();
868 static std::string anEmpty;
869 return anEmpty; // not found
872 void Model_Objects::updateResults(FeaturePtr theFeature)
874 // for not persistent is will be done by parametric updater automatically
875 //if (!theFeature->isPersistentResult()) return;
876 // check the existing results and remove them if there is nothing on the label
877 std::list<ResultPtr>::const_iterator aResIter = theFeature->results().cbegin();
878 while(aResIter != theFeature->results().cend()) {
879 ResultPtr aBody = std::dynamic_pointer_cast<ModelAPI_Result>(*aResIter);
881 if (!aBody->data()->isValid()) {
882 // found a disappeared result => remove it
883 theFeature->eraseResultFromList(aBody);
884 // start iterate from beginning because iterator is corrupted by removing
885 aResIter = theFeature->results().cbegin();
892 if (!theFeature->data() || !theFeature->data()->isValid() || theFeature->isDisabled())
894 // check that results are presented on all labels
895 int aResSize = theFeature->results().size();
896 TDF_ChildIterator aLabIter(resultLabel(theFeature->data(), 0).Father());
897 for(; aLabIter.More(); aLabIter.Next()) {
898 // here must be GUID of the feature
899 int aResIndex = aLabIter.Value().Tag() - 1;
901 if (aResSize <= aResIndex) {
902 TDF_Label anArgLab = aLabIter.Value();
903 Handle(TDataStd_Comment) aGroup;
904 if (anArgLab.FindAttribute(TDataStd_Comment::GetID(), aGroup)) {
905 if (aGroup->Get() == ModelAPI_ResultBody::group().c_str()) {
906 aNewBody = createBody(theFeature->data(), aResIndex);
907 } else if (aGroup->Get() == ModelAPI_ResultPart::group().c_str()) {
908 std::shared_ptr<ModelAPI_ResultPart> aNewP = createPart(theFeature->data(), aResIndex);
909 theFeature->setResult(aNewP, aResIndex);
910 if (!aNewP->partDoc().get())
911 theFeature->execute(); // create the part result: it is better to restore the previous result if it is possible
913 } else if (aGroup->Get() == ModelAPI_ResultConstruction::group().c_str()) {
914 theFeature->execute(); // construction shapes are needed for sketch solver
916 } else if (aGroup->Get() == ModelAPI_ResultGroup::group().c_str()) {
917 aNewBody = createGroup(theFeature->data(), aResIndex);
918 } else if (aGroup->Get() == ModelAPI_ResultParameter::group().c_str()) {
919 theFeature->attributeChanged("expression"); // just produce a value
922 Events_Error::send(std::string("Unknown type of result is found in the document:") +
923 TCollection_AsciiString(aGroup->Get()).ToCString());
927 theFeature->setResult(aNewBody, aResIndex);
933 ResultPtr Model_Objects::findByName(const std::string theName)
935 NCollection_DataMap<TDF_Label, FeaturePtr>::Iterator anObjIter(myFeatures);
936 for(; anObjIter.More(); anObjIter.Next()) {
937 FeaturePtr& aFeature = anObjIter.ChangeValue();
938 if (!aFeature.get() || aFeature->isDisabled()) // may be on close
940 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aFeature->results();
941 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
942 for (; aRIter != aResults.cend(); aRIter++) {
943 ResultPtr aRes = *aRIter;
944 if (aRes.get() && aRes->data() && aRes->data()->isValid() && !aRes->isDisabled() &&
945 aRes->data()->name() == theName) {
954 FeaturePtr Model_Objects::nextFeature(FeaturePtr theCurrent, const bool theReverse)
956 std::shared_ptr<Model_Data> aData = std::static_pointer_cast<Model_Data>(theCurrent->data());
957 if (aData && aData->isValid()) {
958 TDF_Label aFeatureLabel = aData->label().Father();
959 Handle(TDataStd_ReferenceArray) aRefs;
960 if (featuresLabel().FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
961 for(int a = aRefs->Lower(); a <= aRefs->Upper(); a++) { // iterate all existing features
962 if (aRefs->Value(a).IsEqual(aFeatureLabel)) {
963 a += theReverse ? -1 : 1;
964 if (a >= aRefs->Lower() && a <= aRefs->Upper())
965 return feature(aRefs->Value(a));
966 break; // finish iiteration: it's last feature
971 return FeaturePtr(); // not found, last, or something is wrong
974 FeaturePtr Model_Objects::firstFeature()
976 Handle(TDataStd_ReferenceArray) aRefs;
977 if (featuresLabel().FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
978 return feature(aRefs->Value(aRefs->Lower()));
980 return FeaturePtr(); // no features at all
983 FeaturePtr Model_Objects::lastFeature()
985 Handle(TDataStd_ReferenceArray) aRefs;
986 if (featuresLabel().FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
987 return feature(aRefs->Value(aRefs->Upper()));
989 return FeaturePtr(); // no features at all
992 std::list<std::shared_ptr<ModelAPI_Feature> > Model_Objects::allFeatures()
994 std::list<std::shared_ptr<ModelAPI_Feature> > aResult;
995 Handle(TDataStd_ReferenceArray) aRefs;
996 if (featuresLabel().FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
997 for(int a = aRefs->Lower(); a <= aRefs->Upper(); a++) {
998 FeaturePtr aFeature = feature(aRefs->Value(a));
1000 aResult.push_back(aFeature);
1006 int Model_Objects::numInternalFeatures()
1008 Handle(TDataStd_ReferenceArray) aRefs;
1009 if (featuresLabel().FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
1010 return aRefs->Upper() - aRefs->Lower() + 1;
1012 return 0; // invalid
1015 std::shared_ptr<ModelAPI_Feature> Model_Objects::internalFeature(const int theIndex)
1017 Handle(TDataStd_ReferenceArray) aRefs;
1018 if (featuresLabel().FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
1019 return feature(aRefs->Value(aRefs->Lower() + theIndex));
1021 return FeaturePtr(); // invalid
1024 Standard_Integer HashCode(const TDF_Label& theLab, const Standard_Integer theUpper)
1026 return TDF_LabelMapHasher::HashCode(theLab, theUpper);
1029 Standard_Boolean IsEqual(const TDF_Label& theLab1, const TDF_Label& theLab2)
1031 return TDF_LabelMapHasher::IsEqual(theLab1, theLab2);