1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: Model_Document.cxx
4 // Created: 28 Feb 2014
5 // Author: Mikhail PONIKAROV
7 #include <Model_Document.h>
8 #include <Model_Data.h>
9 #include <Model_Application.h>
10 #include <Model_Session.h>
11 #include <Model_Events.h>
12 #include <Model_ResultPart.h>
13 #include <Model_ResultConstruction.h>
14 #include <Model_ResultBody.h>
15 #include <Model_ResultGroup.h>
16 #include <Model_ResultParameter.h>
17 #include <ModelAPI_Validator.h>
18 #include <ModelAPI_CompositeFeature.h>
20 #include <Events_Loop.h>
21 #include <Events_Error.h>
23 #include <TDataStd_Integer.hxx>
24 #include <TDataStd_Comment.hxx>
25 #include <TDF_ChildIDIterator.hxx>
26 #include <TDataStd_ReferenceArray.hxx>
27 #include <TDataStd_HLabelArray1.hxx>
28 #include <TDataStd_Name.hxx>
29 #include <TDF_Reference.hxx>
30 #include <TDF_ChildIDIterator.hxx>
31 #include <TDF_LabelMapHasher.hxx>
32 #include <OSD_File.hxx>
33 #include <OSD_Path.hxx>
41 # define _separator_ '\\'
43 # define _separator_ '/'
46 static const int UNDO_LIMIT = 1000; // number of possible undo operations (big for sketcher)
48 static const int TAG_GENERAL = 1; // general properties tag
49 static const int TAG_OBJECTS = 2; // tag of the objects sub-tree (features, results)
50 static const int TAG_HISTORY = 3; // tag of the history sub-tree (python dump)
53 static const int TAG_FEATURE_ARGUMENTS = 1; ///< where the arguments are located
54 static const int TAG_FEATURE_RESULTS = 2; ///< where the results are located
57 /// 0:1:2 - where features are located
58 /// 0:1:2:N:1 - data of the feature N
59 /// 0:1:2:N:2:K:1 - data of the K result of the feature N
61 Model_Document::Model_Document(const std::string theID, const std::string theKind)
62 : myID(theID), myKind(theKind),
63 myDoc(new TDocStd_Document("BinOcaf")) // binary OCAF format
65 myDoc->SetUndoLimit(UNDO_LIMIT);
66 myTransactionSave = 0;
67 myExecuteFeatures = true;
68 // to have something in the document and avoid empty doc open/save problem
69 // in transaction for nesting correct working
71 TDataStd_Integer::Set(myDoc->Main().Father(), 0);
72 myDoc->CommitCommand();
75 /// Returns the file name of this document by the nameof directory and identifuer of a document
76 static TCollection_ExtendedString DocFileName(const char* theFileName, const std::string& theID)
78 TCollection_ExtendedString aPath((const Standard_CString) theFileName);
79 // remove end-separators
80 while(aPath.Length() &&
81 (aPath.Value(aPath.Length()) == '\\' || aPath.Value(aPath.Length()) == '/'))
82 aPath.Remove(aPath.Length());
84 aPath += theID.c_str();
85 aPath += ".cbf"; // standard binary file extension
89 bool Model_Document::isRoot() const
91 return this == Model_Session::get()->moduleDocument().get();
94 bool Model_Document::load(const char* theFileName)
96 Handle(Model_Application) anApp = Model_Application::getApplication();
98 anApp->setLoadPath(theFileName);
100 TCollection_ExtendedString aPath(DocFileName(theFileName, myID));
101 PCDM_ReaderStatus aStatus = (PCDM_ReaderStatus) -1;
103 aStatus = anApp->Open(aPath, myDoc);
104 } catch (Standard_Failure) {
105 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
107 std::string("Exception in opening of document: ") + aFail->GetMessageString());
110 bool isError = aStatus != PCDM_RS_OK;
113 case PCDM_RS_UnknownDocument:
114 Events_Error::send(std::string("Can not open document"));
116 case PCDM_RS_AlreadyRetrieved:
117 Events_Error::send(std::string("Can not open document: already opened"));
119 case PCDM_RS_AlreadyRetrievedAndModified:
121 std::string("Can not open document: already opened and modified"));
123 case PCDM_RS_NoDriver:
124 Events_Error::send(std::string("Can not open document: driver library is not found"));
126 case PCDM_RS_UnknownFileDriver:
127 Events_Error::send(std::string("Can not open document: unknown driver for opening"));
129 case PCDM_RS_OpenError:
130 Events_Error::send(std::string("Can not open document: file open error"));
132 case PCDM_RS_NoVersion:
133 Events_Error::send(std::string("Can not open document: invalid version"));
135 case PCDM_RS_NoModel:
136 Events_Error::send(std::string("Can not open document: no data model"));
138 case PCDM_RS_NoDocument:
139 Events_Error::send(std::string("Can not open document: no document inside"));
141 case PCDM_RS_FormatFailure:
142 Events_Error::send(std::string("Can not open document: format failure"));
144 case PCDM_RS_TypeNotFoundInSchema:
145 Events_Error::send(std::string("Can not open document: invalid object"));
147 case PCDM_RS_UnrecognizedFileFormat:
148 Events_Error::send(std::string("Can not open document: unrecognized file format"));
150 case PCDM_RS_MakeFailure:
151 Events_Error::send(std::string("Can not open document: make failure"));
153 case PCDM_RS_PermissionDenied:
154 Events_Error::send(std::string("Can not open document: permission denied"));
156 case PCDM_RS_DriverFailure:
157 Events_Error::send(std::string("Can not open document: driver failure"));
160 Events_Error::send(std::string("Can not open document: unknown error"));
165 myDoc->SetUndoLimit(UNDO_LIMIT);
166 // to avoid the problem that feature is created in the current, not this, document
167 std::shared_ptr<Model_Session> aSession =
168 std::dynamic_pointer_cast<Model_Session>(Model_Session::get());
169 aSession->setActiveDocument(anApp->getDocument(myID), false);
170 aSession->setCheckTransactions(false);
171 synchronizeFeatures(false, true, true);
172 aSession->setCheckTransactions(true);
173 aSession->setActiveDocument(Model_Session::get()->moduleDocument(), false);
174 aSession->setActiveDocument(anApp->getDocument(myID), true);
179 bool Model_Document::save(const char* theFileName, std::list<std::string>& theResults)
181 // create a directory in the root document if it is not yet exist
182 Handle(Model_Application) anApp = Model_Application::getApplication();
185 CreateDirectory(theFileName, NULL);
187 mkdir(theFileName, 0x1ff);
190 // filename in the dir is id of document inside of the given directory
191 TCollection_ExtendedString aPath(DocFileName(theFileName, myID));
192 PCDM_StoreStatus aStatus;
194 aStatus = anApp->SaveAs(myDoc, aPath);
195 } catch (Standard_Failure) {
196 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
198 std::string("Exception in saving of document: ") + aFail->GetMessageString());
201 bool isDone = aStatus == PCDM_SS_OK || aStatus == PCDM_SS_No_Obj;
204 case PCDM_SS_DriverFailure:
205 Events_Error::send(std::string("Can not save document: save driver-library failure"));
207 case PCDM_SS_WriteFailure:
208 Events_Error::send(std::string("Can not save document: file writing failure"));
210 case PCDM_SS_Failure:
212 Events_Error::send(std::string("Can not save document"));
216 myTransactionSave = myTransactions.size();
217 if (isDone) { // save also sub-documents if any
218 theResults.push_back(TCollection_AsciiString(aPath).ToCString());
219 const std::set<std::string> aSubs = subDocuments(false);
220 std::set<std::string>::iterator aSubIter = aSubs.begin();
221 for (; aSubIter != aSubs.end() && isDone; aSubIter++) {
222 if (anApp->isLoadByDemand(*aSubIter)) {
223 // copy not-activated document that is not in the memory
224 std::string aDocName = *aSubIter;
225 if (!aDocName.empty()) {
227 TCollection_AsciiString aSubPath(DocFileName(anApp->loadPath().c_str(), aDocName));
228 OSD_Path aPath(aSubPath);
229 OSD_File aFile(aPath);
230 if (aFile.Exists()) {
231 TCollection_AsciiString aDestinationDir(DocFileName(theFileName, aDocName));
232 OSD_Path aDestination(aDestinationDir);
233 aFile.Copy(aDestination);
234 theResults.push_back(aDestinationDir.ToCString());
237 std::string("Can not open file ") + aSubPath.ToCString() + " for saving");
240 } else { // simply save opened document
241 isDone = subDoc(*aSubIter)->save(theFileName, theResults);
248 void Model_Document::close(const bool theForever)
250 std::shared_ptr<ModelAPI_Session> aPM = Model_Session::get();
251 if (!isRoot() && this == aPM->activeDocument().get()) {
252 aPM->setActiveDocument(aPM->moduleDocument());
253 } else if (isRoot()) {
254 // erase the active document if root is closed
255 aPM->setActiveDocument(DocumentPtr());
258 const std::set<std::string> aSubs = subDocuments(true);
259 std::set<std::string>::iterator aSubIter = aSubs.begin();
260 for (; aSubIter != aSubs.end(); aSubIter++)
261 subDoc(*aSubIter)->close(theForever);
263 // close for thid document needs no transaction in this document
264 std::static_pointer_cast<Model_Session>(Model_Session::get())->setCheckTransactions(false);
266 // delete all features of this document
267 std::shared_ptr<ModelAPI_Document> aThis =
268 Model_Application::getApplication()->getDocument(myID);
269 Events_Loop* aLoop = Events_Loop::loop();
270 NCollection_DataMap<TDF_Label, FeaturePtr>::Iterator aFeaturesIter(myObjs);
271 for(; aFeaturesIter.More(); aFeaturesIter.Next()) {
272 FeaturePtr aFeature = aFeaturesIter.Value();
273 static Events_ID EVENT_DISP = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
274 ModelAPI_EventCreator::get()->sendDeleted(aThis, ModelAPI_Feature::group());
275 ModelAPI_EventCreator::get()->sendUpdated(aFeature, EVENT_DISP);
276 aFeature->eraseResults();
277 if (theForever) { // issue #294: do not delete content of the document until it can be redone
280 aFeature->data()->execState(ModelAPI_StateMustBeUpdated);
286 aLoop->flush(Events_Loop::eventByName(EVENT_OBJECT_DELETED));
287 aLoop->flush(Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
289 // close all only if it is really asked, otherwise it can be undoed/redoed
291 if (myDoc->CanClose() == CDM_CCS_OK)
295 std::static_pointer_cast<Model_Session>(Model_Session::get())->setCheckTransactions(true);
298 void Model_Document::startOperation()
300 if (myDoc->HasOpenCommand()) { // start of nested command
301 if (myDoc->CommitCommand()) { // commit the current: it will contain all nested after compactification
302 myTransactions.rbegin()->myOCAFNum++; // if has open command, the list is not empty
304 myNestedNum.push_back(0); // start of nested operation with zero transactions inside yet
305 myDoc->OpenCommand();
306 } else { // start the simple command
309 // starts a new operation
310 myTransactions.push_back(Transaction());
311 if (!myNestedNum.empty())
312 (*myNestedNum.rbegin())++;
314 // new command for all subs
315 const std::set<std::string> aSubs = subDocuments(true);
316 std::set<std::string>::iterator aSubIter = aSubs.begin();
317 for (; aSubIter != aSubs.end(); aSubIter++)
318 subDoc(*aSubIter)->startOperation();
321 void Model_Document::compactNested()
323 if (!myNestedNum.empty()) {
324 int aNumToCompact = *(myNestedNum.rbegin());
325 int aSumOfTransaction = 0;
326 for(int a = 0; a < aNumToCompact; a++) {
327 aSumOfTransaction += myTransactions.rbegin()->myOCAFNum;
328 myTransactions.pop_back();
330 // the latest transaction is the start of lower-level operation which startes the nested
331 myTransactions.rbegin()->myOCAFNum += aSumOfTransaction;
332 myNestedNum.pop_back();
336 bool Model_Document::finishOperation()
338 bool isNestedClosed = !myDoc->HasOpenCommand() && !myNestedNum.empty();
339 static std::shared_ptr<Model_Session> aSession =
340 std::static_pointer_cast<Model_Session>(Model_Session::get());
341 synchronizeBackRefs();
342 Events_Loop* aLoop = Events_Loop::loop();
343 aLoop->flush(Events_Loop::eventByName(EVENT_OBJECT_CREATED));
344 aLoop->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
345 aLoop->flush(Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
346 aLoop->flush(Events_Loop::eventByName(EVENT_OBJECT_TOHIDE));
347 aLoop->flush(Events_Loop::eventByName(EVENT_OBJECT_DELETED));
348 // this must be here just after everything is finished but before real transaction stop
349 // to avoid messages about modifications outside of the transaction
350 // and to rebuild everything after all updates and creates
351 if (isRoot()) { // once for root document
352 Events_Loop::loop()->autoFlush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
353 static std::shared_ptr<Events_Message> aFinishMsg
354 (new Events_Message(Events_Loop::eventByName("FinishOperation")));
355 Events_Loop::loop()->send(aFinishMsg);
356 Events_Loop::loop()->autoFlush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED), false);
358 // to avoid "updated" message appearance by updater
359 //aLoop->clear(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
361 // finish for all subs first: to avoid nested finishing and "isOperation" calls problems inside
362 bool aResult = false;
363 const std::set<std::string> aSubs = subDocuments(true);
364 std::set<std::string>::iterator aSubIter = aSubs.begin();
365 for (; aSubIter != aSubs.end(); aSubIter++)
366 if (subDoc(*aSubIter)->finishOperation())
369 // transaction may be empty if this document was created during this transaction (create part)
370 if (!myTransactions.empty() && myDoc->CommitCommand()) { // if commit is successfull, just increment counters
371 myTransactions.rbegin()->myOCAFNum++;
375 if (isNestedClosed) {
378 if (!aResult && !myTransactions.empty() /* it can be for just created part document */)
379 aResult = myTransactions.rbegin()->myOCAFNum != 0;
381 if (!aResult && isRoot()) {
382 // nothing inside in all documents, so remove this transaction from the transactions list
383 undoInternal(true, false);
390 void Model_Document::abortOperation()
392 if (!myNestedNum.empty() && !myDoc->HasOpenCommand()) { // abort all what was done in nested
394 undoInternal(false, false);
397 } else { // abort the current
398 int aNumTransactions = myTransactions.rbegin()->myOCAFNum;
399 myTransactions.pop_back();
400 if (!myNestedNum.empty())
401 (*myNestedNum.rbegin())--;
402 // roll back the needed number of transactions
403 myDoc->AbortCommand();
404 for(int a = 0; a < aNumTransactions; a++)
408 // abort for all subs, flushes will be later, in the end of root abort
409 const std::set<std::string> aSubs = subDocuments(true);
410 std::set<std::string>::iterator aSubIter = aSubs.begin();
411 for (; aSubIter != aSubs.end(); aSubIter++)
412 subDoc(*aSubIter)->abortOperation();
413 // references may be changed because they are set in attributes on the fly
414 synchronizeFeatures(true, true, isRoot());
417 bool Model_Document::isOperation() const
419 // operation is opened for all documents: no need to check subs
420 return myDoc->HasOpenCommand() == Standard_True ;
423 bool Model_Document::isModified()
425 // is modified if at least one operation was commited and not undoed
426 return myTransactions.size() != myTransactionSave || isOperation();
429 bool Model_Document::canUndo()
431 // issue 406 : if transaction is opened, but nothing to undo behind, can not undo
432 int aCurrentNum = isOperation() ? 1 : 0;
433 if (myDoc->GetAvailableUndos() > 0 &&
434 (myNestedNum.empty() || *myNestedNum.rbegin() - aCurrentNum > 0) && // there is something to undo in nested
435 myTransactions.size() - aCurrentNum > 0 /* for omitting the first useless transaction */)
437 // check other subs contains operation that can be undoed
438 const std::set<std::string> aSubs = subDocuments(true);
439 std::set<std::string>::iterator aSubIter = aSubs.begin();
440 for (; aSubIter != aSubs.end(); aSubIter++)
441 if (subDoc(*aSubIter)->canUndo())
446 void Model_Document::undoInternal(const bool theWithSubs, const bool theSynchronize)
448 int aNumTransactions = myTransactions.rbegin()->myOCAFNum;
449 myRedos.push_back(*myTransactions.rbegin());
450 myTransactions.pop_back();
451 if (!myNestedNum.empty())
452 (*myNestedNum.rbegin())--;
453 // roll back the needed number of transactions
454 for(int a = 0; a < aNumTransactions; a++)
459 const std::set<std::string> aSubs = subDocuments(true);
460 std::set<std::string>::iterator aSubIter = aSubs.begin();
461 for (; aSubIter != aSubs.end(); aSubIter++)
462 subDoc(*aSubIter)->undoInternal(theWithSubs, theSynchronize);
464 // after redo of all sub-documents to avoid updates on not-modified data (issue 370)
466 synchronizeFeatures(true, true, isRoot());
469 void Model_Document::undo()
471 undoInternal(true, true);
474 bool Model_Document::canRedo()
476 if (!myRedos.empty())
478 // check other subs contains operation that can be redoed
479 const std::set<std::string> aSubs = subDocuments(true);
480 std::set<std::string>::iterator aSubIter = aSubs.begin();
481 for (; aSubIter != aSubs.end(); aSubIter++)
482 if (subDoc(*aSubIter)->canRedo())
487 void Model_Document::redo()
489 if (!myNestedNum.empty())
490 (*myNestedNum.rbegin())++;
491 int aNumRedos = myRedos.rbegin()->myOCAFNum;
492 myTransactions.push_back(*myRedos.rbegin());
494 for(int a = 0; a < aNumRedos; a++)
498 const std::set<std::string> aSubs = subDocuments(true);
499 std::set<std::string>::iterator aSubIter = aSubs.begin();
500 for (; aSubIter != aSubs.end(); aSubIter++)
501 subDoc(*aSubIter)->redo();
503 // after redo of all sub-documents to avoid updates on not-modified data (issue 370)
504 synchronizeFeatures(true, true, isRoot());
507 std::list<std::string> Model_Document::undoList() const
509 std::list<std::string> aResult;
510 // the number of skipped current operations (on undo they will be aborted)
511 int aSkipCurrent = isOperation() ? 1 : 0;
512 std::list<Transaction>::const_reverse_iterator aTrIter = myTransactions.crbegin();
513 int aNumUndo = myTransactions.size();
514 if (!myNestedNum.empty())
515 aNumUndo = *myNestedNum.rbegin();
516 for( ; aNumUndo > 0; aTrIter++, aNumUndo--) {
517 if (aSkipCurrent == 0) aResult.push_back(aTrIter->myId);
523 std::list<std::string> Model_Document::redoList() const
525 std::list<std::string> aResult;
526 std::list<Transaction>::const_reverse_iterator aTrIter = myRedos.crbegin();
527 for( ; aTrIter != myRedos.crend(); aTrIter++) {
528 aResult.push_back(aTrIter->myId);
533 void Model_Document::operationId(const std::string& theId)
535 myTransactions.rbegin()->myId = theId;
538 /// Append to the array of references a new referenced label
539 static void AddToRefArray(TDF_Label& theArrayLab, TDF_Label& theReferenced)
541 Handle(TDataStd_ReferenceArray) aRefs;
542 if (!theArrayLab.FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
543 aRefs = TDataStd_ReferenceArray::Set(theArrayLab, 0, 0);
544 aRefs->SetValue(0, theReferenced);
545 } else { // extend array by one more element
546 Handle(TDataStd_HLabelArray1) aNewArray = new TDataStd_HLabelArray1(aRefs->Lower(),
548 for (int a = aRefs->Lower(); a <= aRefs->Upper(); a++) {
549 aNewArray->SetValue(a, aRefs->Value(a));
551 aNewArray->SetValue(aRefs->Upper() + 1, theReferenced);
552 aRefs->SetInternalArray(aNewArray);
556 FeaturePtr Model_Document::addFeature(std::string theID)
558 TDF_Label anEmptyLab;
559 FeaturePtr anEmptyFeature;
560 FeaturePtr aFeature =
561 std::dynamic_pointer_cast<Model_Session>(ModelAPI_Session::get())->createFeature(theID, this);
564 Model_Document* aDocToAdd;
565 if (aFeature->documentToAdd().get()) { // use the customized document to add
566 aDocToAdd = std::dynamic_pointer_cast<Model_Document>(aFeature->documentToAdd()).get();
567 } else { // if customized is not presented, add to "this" document
571 TDF_Label aFeatureLab;
572 if (!aFeature->isAction()) { // do not add action to the data model
573 TDF_Label aFeaturesLab = aDocToAdd->featuresLabel();
574 aFeatureLab = aFeaturesLab.NewChild();
575 aDocToAdd->initData(aFeature, aFeatureLab, TAG_FEATURE_ARGUMENTS);
576 // keep the feature ID to restore document later correctly
577 TDataStd_Comment::Set(aFeatureLab, aFeature->getKind().c_str());
578 aDocToAdd->myObjs.Bind(aFeatureLab, aFeature);
579 // store feature in the history of features array
580 if (aFeature->isInHistory()) {
581 AddToRefArray(aFeaturesLab, aFeatureLab);
584 if (!aFeature->isAction()) { // do not add action to the data model
585 // event: feature is added
586 static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_CREATED);
587 ModelAPI_EventCreator::get()->sendUpdated(aFeature, anEvent);
588 } else { // feature must be executed
589 // no creation event => updater not working, problem with remove part
596 /// Appenad to the array of references a new referenced label.
597 /// If theIndex is not -1, removes element at this index, not theReferenced.
598 /// \returns the index of removed element
599 static int RemoveFromRefArray(TDF_Label theArrayLab, TDF_Label theReferenced,
600 const int theIndex = -1)
602 int aResult = -1; // no returned
603 Handle(TDataStd_ReferenceArray) aRefs;
604 if (theArrayLab.FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
605 if (aRefs->Length() == 1) { // just erase an array
606 if ((theIndex == -1 && aRefs->Value(0) == theReferenced) || theIndex == 0) {
607 theArrayLab.ForgetAttribute(TDataStd_ReferenceArray::GetID());
610 } else { // reduce the array
611 Handle(TDataStd_HLabelArray1) aNewArray = new TDataStd_HLabelArray1(aRefs->Lower(),
613 int aCount = aRefs->Lower();
614 for (int a = aCount; a <= aRefs->Upper(); a++, aCount++) {
615 if ((theIndex == -1 && aRefs->Value(a) == theReferenced) || theIndex == a) {
619 aNewArray->SetValue(aCount, aRefs->Value(a));
622 aRefs->SetInternalArray(aNewArray);
628 void Model_Document::refsToFeature(FeaturePtr theFeature,
629 std::set<std::shared_ptr<ModelAPI_Feature> >& theRefs,
630 const bool isSendError)
632 // check the feature: it must have no depended objects on it
633 // the dependencies can be in the feature results
634 std::list<ResultPtr>::const_iterator aResIter = theFeature->results().cbegin();
635 for(; aResIter != theFeature->results().cend(); aResIter++) {
636 ResultPtr aResult = (*aResIter);
637 std::shared_ptr<Model_Data> aData =
638 std::dynamic_pointer_cast<Model_Data>(aResult->data());
639 if (aData.get() != NULL) {
640 const std::set<AttributePtr>& aRefs = aData->refsToMe();
641 std::set<AttributePtr>::const_iterator aRefIt = aRefs.begin(), aRefLast = aRefs.end();
642 for(; aRefIt != aRefLast; aRefIt++) {
643 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>((*aRefIt)->owner());
644 if (aFeature.get() != NULL)
645 theRefs.insert(aFeature);
649 // the dependencies can be in the feature itself
650 std::shared_ptr<Model_Data> aData =
651 std::dynamic_pointer_cast<Model_Data>(theFeature->data());
652 if (aData && !aData->refsToMe().empty()) {
653 const std::set<AttributePtr>& aRefs = aData->refsToMe();
654 std::set<AttributePtr>::const_iterator aRefIt = aRefs.begin(), aRefLast = aRefs.end();
655 for(; aRefIt != aRefLast; aRefIt++) {
656 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>((*aRefIt)->owner());
657 if (aFeature.get() != NULL)
658 theRefs.insert(aFeature);
662 if (!theRefs.empty() && isSendError) {
664 "Feature '" + theFeature->data()->name() + "' is used and can not be deleted");
668 void Model_Document::removeFeature(FeaturePtr theFeature/*, const bool theCheck*/)
670 std::shared_ptr<Model_Data> aData = std::static_pointer_cast<Model_Data>(theFeature->data());
672 TDF_Label aFeatureLabel = aData->label().Father();
673 if (myObjs.IsBound(aFeatureLabel))
674 myObjs.UnBind(aFeatureLabel);
676 return; // not found feature => do not remove
678 // checking that the sub-element of composite feature is removed: if yes, inform the owner
679 std::set<std::shared_ptr<ModelAPI_Feature> > aRefs;
680 refsToFeature(theFeature, aRefs, false);
681 std::set<std::shared_ptr<ModelAPI_Feature> >::iterator aRefIter = aRefs.begin();
682 for(; aRefIter != aRefs.end(); aRefIter++) {
683 std::shared_ptr<ModelAPI_CompositeFeature> aComposite =
684 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(*aRefIter);
685 if (aComposite.get()) {
686 aComposite->removeFeature(theFeature);
692 static Events_ID EVENT_DISP = Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
693 ModelAPI_EventCreator::get()->sendUpdated(theFeature, EVENT_DISP);
694 // erase all attributes under the label of feature
695 aFeatureLabel.ForgetAllAttributes();
696 // remove it from the references array
697 if (theFeature->isInHistory()) {
698 RemoveFromRefArray(featuresLabel(), aFeatureLabel);
700 // event: feature is deleted
701 ModelAPI_EventCreator::get()->sendDeleted(theFeature->document(), ModelAPI_Feature::group());
702 // the redisplay signal should be flushed in order to erase the feature presentation in the viewer
703 Events_Loop::loop()->flush(EVENT_DISP);
707 void Model_Document::addToHistory(const std::shared_ptr<ModelAPI_Object> theObject)
709 TDF_Label aFeaturesLab = featuresLabel();
710 std::shared_ptr<Model_Data> aData = std::static_pointer_cast<Model_Data>(theObject->data());
712 return; // not found feature => do not remove
714 TDF_Label aFeatureLabel = aData->label().Father();
715 // store feature in the history of features array
716 if (theObject->isInHistory()) {
717 AddToRefArray(aFeaturesLab, aFeatureLabel);
719 RemoveFromRefArray(aFeaturesLab, aFeatureLabel);
723 FeaturePtr Model_Document::feature(TDF_Label& theLabel) const
725 if (myObjs.IsBound(theLabel))
726 return myObjs.Find(theLabel);
727 return FeaturePtr(); // not found
730 ObjectPtr Model_Document::object(TDF_Label theLabel)
732 // try feature by label
733 FeaturePtr aFeature = feature(theLabel);
735 return feature(theLabel);
736 TDF_Label aFeatureLabel = theLabel.Father().Father(); // let's suppose it is result
737 aFeature = feature(aFeatureLabel);
739 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aFeature->results();
740 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.cbegin();
741 for (; aRIter != aResults.cend(); aRIter++) {
742 std::shared_ptr<Model_Data> aResData = std::dynamic_pointer_cast<Model_Data>(
744 if (aResData->label().Father().IsEqual(theLabel))
748 return FeaturePtr(); // not found
751 std::shared_ptr<ModelAPI_Document> Model_Document::subDocument(std::string theDocID)
753 return Model_Application::getApplication()->getDocument(theDocID);
756 const std::set<std::string> Model_Document::subDocuments(const bool theActivatedOnly) const
758 std::set<std::string> aResult;
759 // comment must be in any feature: it is kind
761 TDF_ChildIDIterator aLabIter(featuresLabel(), TDataStd_Comment::GetID());
762 for (; aLabIter.More(); aLabIter.Next()) {
763 TDF_Label aFLabel = aLabIter.Value()->Label();
764 FeaturePtr aFeature = feature(aFLabel);
765 if (aFeature.get()) { // if document is closed the feature may be not in myObjs map
766 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aFeature->results();
767 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
768 for (; aRIter != aResults.cend(); aRIter++) {
769 if ((*aRIter)->groupName() != ModelAPI_ResultPart::group()) continue;
770 if ((*aRIter)->isInHistory()) {
771 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aRIter);
772 if (aPart && (!theActivatedOnly || aPart->isActivated()))
773 aResult.insert(aPart->data()->name());
781 std::shared_ptr<Model_Document> Model_Document::subDoc(std::string theDocID)
783 // just store sub-document identifier here to manage it later
784 return std::dynamic_pointer_cast<Model_Document>(
785 Model_Application::getApplication()->getDocument(theDocID));
788 ObjectPtr Model_Document::object(const std::string& theGroupID, const int theIndex,
789 const bool theHidden)
791 if (theGroupID == ModelAPI_Feature::group()) {
794 TDF_ChildIDIterator aLabIter(featuresLabel(), TDataStd_Comment::GetID());
795 for (; aLabIter.More(); aLabIter.Next()) {
796 if (theIndex == anIndex) {
797 TDF_Label aFLabel = aLabIter.Value()->Label();
798 return feature(aFLabel);
803 Handle(TDataStd_ReferenceArray) aRefs;
804 if (!featuresLabel().FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs))
806 if (aRefs->Lower() > theIndex || aRefs->Upper() < theIndex)
808 TDF_Label aFeatureLabel = aRefs->Value(theIndex);
809 return feature(aFeatureLabel);
812 // comment must be in any feature: it is kind
814 TDF_ChildIDIterator aLabIter(featuresLabel(), TDataStd_Comment::GetID());
815 for (; aLabIter.More(); aLabIter.Next()) {
816 TDF_Label aFLabel = aLabIter.Value()->Label();
817 FeaturePtr aFeature = feature(aFLabel);
818 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aFeature->results();
819 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
820 for (; aRIter != aResults.cend(); aRIter++) {
821 if ((*aRIter)->groupName() != theGroupID) continue;
822 bool isIn = theHidden && (*aRIter)->isInHistory();
823 if (!isIn && (*aRIter)->isInHistory()) { // check that there is nobody references this result
824 isIn = !(*aRIter)->isConcealed();
827 if (anIndex == theIndex)
838 std::shared_ptr<ModelAPI_Object> Model_Document::objectByName(
839 const std::string& theGroupID, const std::string& theName)
841 if (theGroupID == ModelAPI_Feature::group()) {
843 TDF_ChildIDIterator aLabIter(featuresLabel(), TDataStd_Comment::GetID());
844 for (; aLabIter.More(); aLabIter.Next()) {
845 TDF_Label aFLabel = aLabIter.Value()->Label();
846 FeaturePtr aFeature = feature(aFLabel);
847 if (aFeature && aFeature->name() == theName)
851 // comment must be in any feature: it is kind
853 TDF_ChildIDIterator aLabIter(featuresLabel(), TDataStd_Comment::GetID());
854 for (; aLabIter.More(); aLabIter.Next()) {
855 TDF_Label aFLabel = aLabIter.Value()->Label();
856 FeaturePtr aFeature = feature(aFLabel);
857 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aFeature->results();
858 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
859 for (; aRIter != aResults.cend(); aRIter++) {
860 if ((*aRIter)->groupName() == theGroupID && (*aRIter)->data()->name() == theName)
869 int Model_Document::size(const std::string& theGroupID, const bool theHidden)
872 if (theGroupID == ModelAPI_Feature::group()) {
874 return myObjs.Size();
876 Handle(TDataStd_ReferenceArray) aRefs;
877 if (featuresLabel().FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs))
878 return aRefs->Length();
881 // comment must be in any feature: it is kind
882 TDF_ChildIDIterator aLabIter(featuresLabel(), TDataStd_Comment::GetID());
883 for (; aLabIter.More(); aLabIter.Next()) {
884 TDF_Label aFLabel = aLabIter.Value()->Label();
885 FeaturePtr aFeature = feature(aFLabel);
886 if (!aFeature) // may be on close
888 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aFeature->results();
889 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
890 for (; aRIter != aResults.cend(); aRIter++) {
891 if ((*aRIter)->groupName() != theGroupID) continue;
892 bool isIn = theHidden;
893 if (!isIn && (*aRIter)->isInHistory()) { // check that there is nobody references this result
894 isIn = !(*aRIter)->isConcealed();
901 // group is not found
905 TDF_Label Model_Document::featuresLabel() const
907 return myDoc->Main().FindChild(TAG_OBJECTS);
910 void Model_Document::setUniqueName(FeaturePtr theFeature)
912 if (!theFeature->data()->name().empty())
913 return; // not needed, name is already defined
914 std::string aName; // result
915 // first count all objects of such kind to start with index = count + 1
917 NCollection_DataMap<TDF_Label, FeaturePtr>::Iterator aFIter(myObjs);
918 for (; aFIter.More(); aFIter.Next()) {
919 if (aFIter.Value()->getKind() == theFeature->getKind())
922 // generate candidate name
923 std::stringstream aNameStream;
924 aNameStream << theFeature->getKind() << "_" << aNumObjects + 1;
925 aName = aNameStream.str();
926 // check this is unique, if not, increase index by 1
927 for (aFIter.Initialize(myObjs); aFIter.More();) {
928 FeaturePtr aFeature = aFIter.Value();
929 bool isSameName = aFeature->data()->name() == aName;
930 if (!isSameName) { // check also results to avoid same results names (actual for Parts)
931 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aFeature->results();
932 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
933 for (; aRIter != aResults.cend(); aRIter++) {
934 isSameName = (*aRIter)->data()->name() == aName;
939 std::stringstream aNameStream;
940 aNameStream << theFeature->getKind() << "_" << aNumObjects + 1;
941 aName = aNameStream.str();
942 // reinitialize iterator to make sure a new name is unique
943 aFIter.Initialize(myObjs);
947 theFeature->data()->setName(aName);
950 void Model_Document::initData(ObjectPtr theObj, TDF_Label theLab, const int theTag)
952 std::shared_ptr<ModelAPI_Document> aThis = Model_Application::getApplication()->getDocument(
954 std::shared_ptr<Model_Data> aData(new Model_Data);
955 aData->setLabel(theLab.FindChild(theTag));
956 aData->setObject(theObj);
957 theObj->setDoc(aThis);
958 theObj->setData(aData);
959 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObj);
961 setUniqueName(aFeature); // must be before "initAttributes" because duplicate part uses name
963 theObj->initAttributes();
966 void Model_Document::synchronizeFeatures(
967 const bool theMarkUpdated, const bool theUpdateReferences, const bool theFlush)
969 std::shared_ptr<ModelAPI_Document> aThis =
970 Model_Application::getApplication()->getDocument(myID);
971 // after all updates, sends a message that groups of features were created or updated
972 Events_Loop* aLoop = Events_Loop::loop();
973 static Events_ID aDispEvent = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
974 static Events_ID aCreateEvent = Events_Loop::eventByName(EVENT_OBJECT_CREATED);
975 static Events_ID anUpdateEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
976 static Events_ID aRedispEvent = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
977 static Events_ID aDeleteEvent = Events_Loop::eventByName(EVENT_OBJECT_DELETED);
978 static Events_ID aToHideEvent = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
979 aLoop->activateFlushes(false);
981 // update all objects by checking are they on labels or not
982 std::set<FeaturePtr> aNewFeatures, aKeptFeatures;
983 TDF_ChildIDIterator aLabIter(featuresLabel(), TDataStd_Comment::GetID());
984 for (; aLabIter.More(); aLabIter.Next()) {
985 TDF_Label aFeatureLabel = aLabIter.Value()->Label();
987 if (!myObjs.IsBound(aFeatureLabel)) { // a new feature is inserted
989 aFeature = std::dynamic_pointer_cast<Model_Session>(ModelAPI_Session::get())->createFeature(
990 TCollection_AsciiString(Handle(TDataStd_Comment)::DownCast(aLabIter.Value())->Get())
992 if (!aFeature) { // somethig is wrong, most probably, the opened document has invalid structure
993 Events_Error::send("Invalid type of object in the document");
994 aLabIter.Value()->Label().ForgetAllAttributes();
997 // this must be before "setData" to redo the sketch line correctly
998 myObjs.Bind(aFeatureLabel, aFeature);
999 aNewFeatures.insert(aFeature);
1000 initData(aFeature, aFeatureLabel, TAG_FEATURE_ARGUMENTS);
1002 // event: model is updated
1003 ModelAPI_EventCreator::get()->sendUpdated(aFeature, aCreateEvent);
1004 } else { // nothing is changed, both iterators are incremented
1005 aFeature = myObjs.Find(aFeatureLabel);
1006 aKeptFeatures.insert(aFeature);
1007 if (theMarkUpdated) {
1008 ModelAPI_EventCreator::get()->sendUpdated(aFeature, anUpdateEvent);
1012 // update results of the features (after features created because they may be connected, like sketch and sub elements)
1013 std::list<FeaturePtr> aComposites; // composites must be updated after their subs (issue 360)
1014 TDF_ChildIDIterator aLabIter2(featuresLabel(), TDataStd_Comment::GetID());
1015 for (; aLabIter2.More(); aLabIter2.Next()) {
1016 TDF_Label aFeatureLabel = aLabIter2.Value()->Label();
1017 if (myObjs.IsBound(aFeatureLabel)) { // a new feature is inserted
1018 FeaturePtr aFeature = myObjs.Find(aFeatureLabel);
1019 if (std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aFeature).get())
1020 aComposites.push_back(aFeature);
1021 updateResults(aFeature);
1024 std::list<FeaturePtr>::iterator aComposite = aComposites.begin();
1025 for(; aComposite != aComposites.end(); aComposite++) {
1026 updateResults(*aComposite);
1029 // check all features are checked: if not => it was removed
1030 NCollection_DataMap<TDF_Label, FeaturePtr>::Iterator aFIter(myObjs);
1031 while (aFIter.More()) {
1032 if (aKeptFeatures.find(aFIter.Value()) == aKeptFeatures.end()
1033 && aNewFeatures.find(aFIter.Value()) == aNewFeatures.end()) {
1034 FeaturePtr aFeature = aFIter.Value();
1035 // event: model is updated
1036 //if (aFeature->isInHistory()) {
1037 ModelAPI_EventCreator::get()->sendDeleted(aThis, ModelAPI_Feature::group());
1039 // results of this feature must be redisplayed (hided)
1040 // redisplay also removed feature (used for sketch and AISObject)
1041 ModelAPI_EventCreator::get()->sendUpdated(aFeature, aRedispEvent);
1043 // unbind after the "erase" call: on abort sketch is removes sub-objects that corrupts aFIter
1044 myObjs.UnBind(aFIter.Key());
1045 // reinitialize iterator because unbind may corrupt the previous order in the map
1046 aFIter.Initialize(myObjs);
1051 if (theUpdateReferences) {
1052 synchronizeBackRefs();
1055 myExecuteFeatures = false;
1056 aLoop->activateFlushes(true);
1059 aLoop->flush(aCreateEvent);
1060 aLoop->flush(aDeleteEvent);
1061 aLoop->flush(anUpdateEvent);
1062 aLoop->flush(aRedispEvent);
1063 aLoop->flush(aToHideEvent);
1065 myExecuteFeatures = true;
1068 void Model_Document::synchronizeBackRefs()
1070 std::shared_ptr<ModelAPI_Document> aThis =
1071 Model_Application::getApplication()->getDocument(myID);
1072 // keeps the concealed flags of result to catch the change and create created/deleted events
1073 std::list<std::pair<ResultPtr, bool> > aConcealed;
1074 // first cycle: erase all data about back-references
1075 NCollection_DataMap<TDF_Label, FeaturePtr>::Iterator aFeatures(myObjs);
1076 for(; aFeatures.More(); aFeatures.Next()) {
1077 FeaturePtr aFeature = aFeatures.Value();
1078 std::shared_ptr<Model_Data> aFData =
1079 std::dynamic_pointer_cast<Model_Data>(aFeature->data());
1081 aFData->eraseBackReferences();
1083 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aFeature->results();
1084 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
1085 for (; aRIter != aResults.cend(); aRIter++) {
1086 std::shared_ptr<Model_Data> aResData =
1087 std::dynamic_pointer_cast<Model_Data>((*aRIter)->data());
1089 aConcealed.push_back(std::pair<ResultPtr, bool>(*aRIter, (*aRIter)->isConcealed()));
1090 aResData->eraseBackReferences();
1095 // second cycle: set new back-references: only features may have reference, iterate only them
1096 ModelAPI_ValidatorsFactory* aValidators = ModelAPI_Session::get()->validators();
1097 for(aFeatures.Initialize(myObjs); aFeatures.More(); aFeatures.Next()) {
1098 FeaturePtr aFeature = aFeatures.Value();
1099 std::shared_ptr<Model_Data> aFData =
1100 std::dynamic_pointer_cast<Model_Data>(aFeature->data());
1102 std::list<std::pair<std::string, std::list<ObjectPtr> > > aRefs;
1103 aFData->referencesToObjects(aRefs);
1104 std::list<std::pair<std::string, std::list<ObjectPtr> > >::iterator
1105 aRefsIter = aRefs.begin();
1106 for(; aRefsIter != aRefs.end(); aRefsIter++) {
1107 std::list<ObjectPtr>::iterator aRefTo = aRefsIter->second.begin();
1108 for(; aRefTo != aRefsIter->second.end(); aRefTo++) {
1110 std::shared_ptr<Model_Data> aRefData =
1111 std::dynamic_pointer_cast<Model_Data>((*aRefTo)->data());
1112 aRefData->addBackReference(aFeature, aRefsIter->first); // here the Concealed flag is updated
1118 std::list<std::pair<ResultPtr, bool> >::iterator aCIter = aConcealed.begin();
1119 for(; aCIter != aConcealed.end(); aCIter++) {
1120 if (aCIter->first->isConcealed() != aCIter->second) { // somethign is changed => produce event
1121 if (aCIter->second) { // was concealed become not => creation event
1122 static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_CREATED);
1123 ModelAPI_EventCreator::get()->sendUpdated(aCIter->first, anEvent);
1124 } else { // was not concealed become concealed => delete event
1125 ModelAPI_EventCreator::get()->sendDeleted(aThis, aCIter->first->groupName());
1126 // redisplay for the viewer (it must be disappeared also)
1127 static Events_ID EVENT_DISP =
1128 Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
1129 ModelAPI_EventCreator::get()->sendUpdated(aCIter->first, EVENT_DISP);
1135 TDF_Label Model_Document::resultLabel(
1136 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theResultIndex)
1138 const std::shared_ptr<Model_Data>& aData =
1139 std::dynamic_pointer_cast<Model_Data>(theFeatureData);
1140 return aData->label().Father().FindChild(TAG_FEATURE_RESULTS).FindChild(theResultIndex + 1);
1143 void Model_Document::storeResult(std::shared_ptr<ModelAPI_Data> theFeatureData,
1144 std::shared_ptr<ModelAPI_Result> theResult,
1145 const int theResultIndex)
1147 std::shared_ptr<ModelAPI_Document> aThis =
1148 Model_Application::getApplication()->getDocument(myID);
1149 theResult->setDoc(aThis);
1150 initData(theResult, resultLabel(theFeatureData, theResultIndex), TAG_FEATURE_ARGUMENTS);
1151 if (theResult->data()->name().empty()) { // if was not initialized, generate event and set a name
1152 std::stringstream aNewName;
1153 aNewName<<theFeatureData->name();
1154 if (theResultIndex > 0) // if there are several results, add unique prefix starting from second
1155 aNewName<<"_"<<theResultIndex + 1;
1156 theResult->data()->setName(aNewName.str());
1160 std::shared_ptr<ModelAPI_ResultConstruction> Model_Document::createConstruction(
1161 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex)
1163 TDF_Label aLab = resultLabel(theFeatureData, theIndex);
1164 TDataStd_Comment::Set(aLab, ModelAPI_ResultConstruction::group().c_str());
1165 ObjectPtr anOldObject = object(aLab);
1166 std::shared_ptr<ModelAPI_ResultConstruction> aResult;
1168 aResult = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(anOldObject);
1171 aResult = std::shared_ptr<ModelAPI_ResultConstruction>(new Model_ResultConstruction);
1172 storeResult(theFeatureData, aResult, theIndex);
1177 std::shared_ptr<ModelAPI_ResultBody> Model_Document::createBody(
1178 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex)
1180 TDF_Label aLab = resultLabel(theFeatureData, theIndex);
1181 TDataStd_Comment::Set(aLab, ModelAPI_ResultBody::group().c_str());
1182 ObjectPtr anOldObject = object(aLab);
1183 std::shared_ptr<ModelAPI_ResultBody> aResult;
1185 aResult = std::dynamic_pointer_cast<ModelAPI_ResultBody>(anOldObject);
1188 aResult = std::shared_ptr<ModelAPI_ResultBody>(new Model_ResultBody);
1189 storeResult(theFeatureData, aResult, theIndex);
1194 std::shared_ptr<ModelAPI_ResultPart> Model_Document::createPart(
1195 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex)
1197 TDF_Label aLab = resultLabel(theFeatureData, theIndex);
1198 TDataStd_Comment::Set(aLab, ModelAPI_ResultPart::group().c_str());
1199 ObjectPtr anOldObject = object(aLab);
1200 std::shared_ptr<ModelAPI_ResultPart> aResult;
1202 aResult = std::dynamic_pointer_cast<ModelAPI_ResultPart>(anOldObject);
1205 aResult = std::shared_ptr<ModelAPI_ResultPart>(new Model_ResultPart);
1206 storeResult(theFeatureData, aResult, theIndex);
1211 std::shared_ptr<ModelAPI_ResultGroup> Model_Document::createGroup(
1212 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex)
1214 TDF_Label aLab = resultLabel(theFeatureData, theIndex);
1215 TDataStd_Comment::Set(aLab, ModelAPI_ResultGroup::group().c_str());
1216 ObjectPtr anOldObject = object(aLab);
1217 std::shared_ptr<ModelAPI_ResultGroup> aResult;
1219 aResult = std::dynamic_pointer_cast<ModelAPI_ResultGroup>(anOldObject);
1222 aResult = std::shared_ptr<ModelAPI_ResultGroup>(new Model_ResultGroup(theFeatureData));
1223 storeResult(theFeatureData, aResult, theIndex);
1228 std::shared_ptr<ModelAPI_ResultParameter> Model_Document::createParameter(
1229 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex)
1231 TDF_Label aLab = resultLabel(theFeatureData, theIndex);
1232 TDataStd_Comment::Set(aLab, ModelAPI_ResultParameter::group().c_str());
1233 ObjectPtr anOldObject = object(aLab);
1234 std::shared_ptr<ModelAPI_ResultParameter> aResult;
1236 aResult = std::dynamic_pointer_cast<ModelAPI_ResultParameter>(anOldObject);
1239 aResult = std::shared_ptr<ModelAPI_ResultParameter>(new Model_ResultParameter);
1240 storeResult(theFeatureData, aResult, theIndex);
1245 std::shared_ptr<ModelAPI_Feature> Model_Document::feature(
1246 const std::shared_ptr<ModelAPI_Result>& theResult)
1248 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(theResult->data());
1250 TDF_Label aFeatureLab = aData->label().Father().Father().Father();
1251 return feature(aFeatureLab);
1253 return FeaturePtr();
1256 void Model_Document::updateResults(FeaturePtr theFeature)
1258 // for not persistent is will be done by parametric updater automatically
1259 //if (!theFeature->isPersistentResult()) return;
1260 // check the existing results and remove them if there is nothing on the label
1261 std::list<ResultPtr>::const_iterator aResIter = theFeature->results().cbegin();
1262 while(aResIter != theFeature->results().cend()) {
1263 ResultPtr aBody = std::dynamic_pointer_cast<ModelAPI_Result>(*aResIter);
1265 if (!aBody->data()->isValid()) {
1266 // found a disappeared result => remove it
1267 theFeature->removeResult(aBody);
1268 // start iterate from beginning because iterator is corrupted by removing
1269 aResIter = theFeature->results().cbegin();
1275 // it may be on undo
1276 if (!theFeature->data() || !theFeature->data()->isValid())
1278 // check that results are presented on all labels
1279 int aResSize = theFeature->results().size();
1280 TDF_ChildIterator aLabIter(resultLabel(theFeature->data(), 0).Father());
1281 for(; aLabIter.More(); aLabIter.Next()) {
1282 // here must be GUID of the feature
1283 int aResIndex = aLabIter.Value().Tag() - 1;
1285 if (aResSize <= aResIndex) {
1286 TDF_Label anArgLab = aLabIter.Value();
1287 Handle(TDataStd_Comment) aGroup;
1288 if (anArgLab.FindAttribute(TDataStd_Comment::GetID(), aGroup)) {
1289 if (aGroup->Get() == ModelAPI_ResultBody::group().c_str()) {
1290 aNewBody = createBody(theFeature->data(), aResIndex);
1291 } else if (aGroup->Get() == ModelAPI_ResultPart::group().c_str()) {
1292 aNewBody = createPart(theFeature->data(), aResIndex);
1293 } else if (aGroup->Get() == ModelAPI_ResultConstruction::group().c_str()) {
1294 theFeature->execute(); // construction shapes are needed for sketch solver
1296 } else if (aGroup->Get() == ModelAPI_ResultGroup::group().c_str()) {
1297 aNewBody = createGroup(theFeature->data(), aResIndex);
1299 Events_Error::send(std::string("Unknown type of result is found in the document:") +
1300 TCollection_AsciiString(aGroup->Get()).ToCString());
1304 theFeature->setResult(aNewBody, aResIndex);
1310 Standard_Integer HashCode(const TDF_Label& theLab, const Standard_Integer theUpper)
1312 return TDF_LabelMapHasher::HashCode(theLab, theUpper);
1315 Standard_Boolean IsEqual(const TDF_Label& theLab1, const TDF_Label& theLab2)
1317 return TDF_LabelMapHasher::IsEqual(theLab1, theLab2);
1320 void Model_Document::addNamingName(const TDF_Label theLabel, std::string theName)
1322 myNamingNames[theName] = theLabel;
1325 TDF_Label Model_Document::findNamingName(std::string theName)
1327 std::map<std::string, TDF_Label>::iterator aFind = myNamingNames.find(theName);
1328 if (aFind == myNamingNames.end())
1329 return TDF_Label(); // not found
1330 return aFind->second;
1333 ResultPtr Model_Document::findByName(const std::string theName)
1335 NCollection_DataMap<TDF_Label, FeaturePtr>::Iterator anObjIter(myObjs);
1336 for(; anObjIter.More(); anObjIter.Next()) {
1337 FeaturePtr& aFeature = anObjIter.ChangeValue();
1338 if (!aFeature) // may be on close
1340 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aFeature->results();
1341 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
1342 for (; aRIter != aResults.cend(); aRIter++) {
1343 if (aRIter->get() && (*aRIter)->data() && (*aRIter)->data()->isValid() &&
1344 (*aRIter)->data()->name() == theName) {