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 <ModelAPI_Validator.h>
17 #include <ModelAPI_CompositeFeature.h>
18 #include <Events_Loop.h>
19 #include <Events_Error.h>
21 #include <TDataStd_Integer.hxx>
22 #include <TDataStd_Comment.hxx>
23 #include <TDF_ChildIDIterator.hxx>
24 #include <TDataStd_ReferenceArray.hxx>
25 #include <TDataStd_HLabelArray1.hxx>
26 #include <TDataStd_Name.hxx>
27 #include <TDF_Reference.hxx>
28 #include <TDF_ChildIDIterator.hxx>
29 #include <TDF_LabelMapHasher.hxx>
30 #include <OSD_File.hxx>
31 #include <OSD_Path.hxx>
39 # define _separator_ '\\'
41 # define _separator_ '/'
44 static const int UNDO_LIMIT = 1000; // number of possible undo operations (big for sketcher)
46 static const int TAG_GENERAL = 1; // general properties tag
47 static const int TAG_OBJECTS = 2; // tag of the objects sub-tree (features, results)
48 static const int TAG_HISTORY = 3; // tag of the history sub-tree (python dump)
51 static const int TAG_FEATURE_ARGUMENTS = 1; ///< where the arguments are located
52 static const int TAG_FEATURE_RESULTS = 2; ///< where the results are located
55 /// 0:1:2 - where features are located
56 /// 0:1:2:N:1 - data of the feature N
57 /// 0:1:2:N:2:K:1 - data of the K result of the feature N
59 Model_Document::Model_Document(const std::string theID, const std::string theKind)
60 : myID(theID), myKind(theKind),
61 myDoc(new TDocStd_Document("BinOcaf")) // binary OCAF format
63 myDoc->SetUndoLimit(UNDO_LIMIT);
64 myTransactionSave = 0;
65 myExecuteFeatures = true;
66 // to have something in the document and avoid empty doc open/save problem
67 // in transaction for nesting correct working
69 TDataStd_Integer::Set(myDoc->Main().Father(), 0);
70 myDoc->CommitCommand();
73 /// Returns the file name of this document by the nameof directory and identifuer of a document
74 static TCollection_ExtendedString DocFileName(const char* theFileName, const std::string& theID)
76 TCollection_ExtendedString aPath((const Standard_CString) theFileName);
77 // remove end-separators
78 while(aPath.Length() &&
79 (aPath.Value(aPath.Length()) == '\\' || aPath.Value(aPath.Length()) == '/'))
80 aPath.Remove(aPath.Length());
82 aPath += theID.c_str();
83 aPath += ".cbf"; // standard binary file extension
87 bool Model_Document::load(const char* theFileName)
89 Handle(Model_Application) anApp = Model_Application::getApplication();
90 if (this == Model_Session::get()->moduleDocument().get()) {
91 anApp->setLoadPath(theFileName);
93 TCollection_ExtendedString aPath(DocFileName(theFileName, myID));
94 PCDM_ReaderStatus aStatus = (PCDM_ReaderStatus) -1;
96 aStatus = anApp->Open(aPath, myDoc);
97 } catch (Standard_Failure) {
98 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
100 std::string("Exception in opening of document: ") + aFail->GetMessageString());
103 bool isError = aStatus != PCDM_RS_OK;
106 case PCDM_RS_UnknownDocument:
107 Events_Error::send(std::string("Can not open document"));
109 case PCDM_RS_AlreadyRetrieved:
110 Events_Error::send(std::string("Can not open document: already opened"));
112 case PCDM_RS_AlreadyRetrievedAndModified:
114 std::string("Can not open document: already opened and modified"));
116 case PCDM_RS_NoDriver:
117 Events_Error::send(std::string("Can not open document: driver library is not found"));
119 case PCDM_RS_UnknownFileDriver:
120 Events_Error::send(std::string("Can not open document: unknown driver for opening"));
122 case PCDM_RS_OpenError:
123 Events_Error::send(std::string("Can not open document: file open error"));
125 case PCDM_RS_NoVersion:
126 Events_Error::send(std::string("Can not open document: invalid version"));
128 case PCDM_RS_NoModel:
129 Events_Error::send(std::string("Can not open document: no data model"));
131 case PCDM_RS_NoDocument:
132 Events_Error::send(std::string("Can not open document: no document inside"));
134 case PCDM_RS_FormatFailure:
135 Events_Error::send(std::string("Can not open document: format failure"));
137 case PCDM_RS_TypeNotFoundInSchema:
138 Events_Error::send(std::string("Can not open document: invalid object"));
140 case PCDM_RS_UnrecognizedFileFormat:
141 Events_Error::send(std::string("Can not open document: unrecognized file format"));
143 case PCDM_RS_MakeFailure:
144 Events_Error::send(std::string("Can not open document: make failure"));
146 case PCDM_RS_PermissionDenied:
147 Events_Error::send(std::string("Can not open document: permission denied"));
149 case PCDM_RS_DriverFailure:
150 Events_Error::send(std::string("Can not open document: driver failure"));
153 Events_Error::send(std::string("Can not open document: unknown error"));
158 myDoc->SetUndoLimit(UNDO_LIMIT);
159 // to avoid the problem that feature is created in the current, not this, document
160 std::shared_ptr<Model_Session> aSession =
161 std::dynamic_pointer_cast<Model_Session>(Model_Session::get());
162 aSession->setActiveDocument(anApp->getDocument(myID), false);
163 aSession->setCheckTransactions(false);
164 synchronizeFeatures(false, true);
165 aSession->setCheckTransactions(true);
166 aSession->setActiveDocument(Model_Session::get()->moduleDocument(), false);
167 aSession->setActiveDocument(anApp->getDocument(myID), true);
172 bool Model_Document::save(const char* theFileName, std::list<std::string>& theResults)
174 // create a directory in the root document if it is not yet exist
175 Handle(Model_Application) anApp = Model_Application::getApplication();
176 if (this == Model_Session::get()->moduleDocument().get()) {
178 CreateDirectory(theFileName, NULL);
180 mkdir(theFileName, 0x1ff);
183 // filename in the dir is id of document inside of the given directory
184 TCollection_ExtendedString aPath(DocFileName(theFileName, myID));
185 PCDM_StoreStatus aStatus;
187 aStatus = anApp->SaveAs(myDoc, aPath);
188 } catch (Standard_Failure) {
189 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
191 std::string("Exception in saving of document: ") + aFail->GetMessageString());
194 bool isDone = aStatus == PCDM_SS_OK || aStatus == PCDM_SS_No_Obj;
197 case PCDM_SS_DriverFailure:
198 Events_Error::send(std::string("Can not save document: save driver-library failure"));
200 case PCDM_SS_WriteFailure:
201 Events_Error::send(std::string("Can not save document: file writing failure"));
203 case PCDM_SS_Failure:
205 Events_Error::send(std::string("Can not save document"));
209 myTransactionSave = myTransactions.size();
210 if (isDone) { // save also sub-documents if any
211 theResults.push_back(TCollection_AsciiString(aPath).ToCString());
212 const std::set<std::string> aSubs = subDocuments(false);
213 std::set<std::string>::iterator aSubIter = aSubs.begin();
214 for (; aSubIter != aSubs.end() && isDone; aSubIter++) {
215 if (anApp->isLoadByDemand(*aSubIter)) {
216 // copy not-activated document that is not in the memory
217 std::string aDocName = *aSubIter;
218 if (!aDocName.empty()) {
220 TCollection_AsciiString aSubPath(DocFileName(anApp->loadPath().c_str(), aDocName));
221 OSD_Path aPath(aSubPath);
222 OSD_File aFile(aPath);
223 if (aFile.Exists()) {
224 TCollection_AsciiString aDestinationDir(DocFileName(theFileName, aDocName));
225 OSD_Path aDestination(aDestinationDir);
226 aFile.Copy(aDestination);
227 theResults.push_back(aDestinationDir.ToCString());
230 std::string("Can not open file ") + aSubPath.ToCString() + " for saving");
233 } else { // simply save opened document
234 isDone = subDoc(*aSubIter)->save(theFileName, theResults);
241 void Model_Document::close(const bool theForever)
243 std::shared_ptr<ModelAPI_Session> aPM = Model_Session::get();
244 if (this != aPM->moduleDocument().get() && this == aPM->activeDocument().get()) {
245 aPM->setActiveDocument(aPM->moduleDocument());
246 } else if (this == aPM->moduleDocument().get()) {
247 // erase the active document if root is closed
248 aPM->setActiveDocument(DocumentPtr());
251 const std::set<std::string> aSubs = subDocuments(true);
252 std::set<std::string>::iterator aSubIter = aSubs.begin();
253 for (; aSubIter != aSubs.end(); aSubIter++)
254 subDoc(*aSubIter)->close(theForever);
256 // close for thid document needs no transaction in this document
257 std::static_pointer_cast<Model_Session>(Model_Session::get())->setCheckTransactions(false);
259 // delete all features of this document
260 std::shared_ptr<ModelAPI_Document> aThis =
261 Model_Application::getApplication()->getDocument(myID);
262 Events_Loop* aLoop = Events_Loop::loop();
263 NCollection_DataMap<TDF_Label, FeaturePtr>::Iterator aFeaturesIter(myObjs);
264 for(; aFeaturesIter.More(); aFeaturesIter.Next()) {
265 FeaturePtr aFeature = aFeaturesIter.Value();
266 static Events_ID EVENT_DISP = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
267 ModelAPI_EventCreator::get()->sendDeleted(aThis, ModelAPI_Feature::group());
268 ModelAPI_EventCreator::get()->sendUpdated(aFeature, EVENT_DISP);
269 aFeature->eraseResults();
270 if (theForever) { // issue #294: do not delete content of the document until it can be redone
273 aFeature->data()->execState(ModelAPI_StateMustBeUpdated);
279 aLoop->flush(Events_Loop::eventByName(EVENT_OBJECT_DELETED));
280 aLoop->flush(Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
282 // close all only if it is really asked, otherwise it can be undoed/redoed
284 if (myDoc->CanClose() == CDM_CCS_OK)
288 std::static_pointer_cast<Model_Session>(Model_Session::get())->setCheckTransactions(true);
291 void Model_Document::startOperation()
293 if (myDoc->HasOpenCommand()) { // start of nested command
294 if (myDoc->CommitCommand()) { // commit the current: it will contain all nested after compactification
295 myTransactions.rbegin()->myOCAFNum++; // if has open command, the list is not empty
297 myNestedNum.push_back(0); // start of nested operation with zero transactions inside yet
298 myDoc->OpenCommand();
299 } else { // start the simple command
302 // starts a new operation
303 myTransactions.push_back(Transaction());
304 if (!myNestedNum.empty())
305 (*myNestedNum.rbegin())++;
307 // new command for all subs
308 const std::set<std::string> aSubs = subDocuments(true);
309 std::set<std::string>::iterator aSubIter = aSubs.begin();
310 for (; aSubIter != aSubs.end(); aSubIter++)
311 subDoc(*aSubIter)->startOperation();
314 void Model_Document::compactNested()
316 if (!myNestedNum.empty()) {
317 int aNumToCompact = *(myNestedNum.rbegin());
318 int aSumOfTransaction = 0;
319 for(int a = 0; a < aNumToCompact; a++) {
320 aSumOfTransaction += myTransactions.rbegin()->myOCAFNum;
321 myTransactions.pop_back();
323 // the latest transaction is the start of lower-level operation which startes the nested
324 myTransactions.rbegin()->myOCAFNum += aSumOfTransaction;
325 myNestedNum.pop_back();
329 bool Model_Document::finishOperation()
331 bool isNestedClosed = !myDoc->HasOpenCommand() && !myNestedNum.empty();
332 static std::shared_ptr<Model_Session> aSession =
333 std::static_pointer_cast<Model_Session>(Model_Session::get());
334 synchronizeBackRefs();
335 Events_Loop* aLoop = Events_Loop::loop();
336 aLoop->flush(Events_Loop::eventByName(EVENT_OBJECT_CREATED));
337 aLoop->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
338 aLoop->flush(Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
339 aLoop->flush(Events_Loop::eventByName(EVENT_OBJECT_TOHIDE));
340 aLoop->flush(Events_Loop::eventByName(EVENT_OBJECT_DELETED));
341 // this must be here just after everything is finished but before real transaction stop
342 // to avoid messages about modifications outside of the transaction
343 // and to rebuild everything after all updates and creates
344 if (Model_Session::get()->moduleDocument().get() == this) { // once for root document
345 Events_Loop::loop()->autoFlush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
346 static std::shared_ptr<Events_Message> aFinishMsg
347 (new Events_Message(Events_Loop::eventByName("FinishOperation")));
348 Events_Loop::loop()->send(aFinishMsg);
349 Events_Loop::loop()->autoFlush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED), false);
351 // to avoid "updated" message appearance by updater
352 //aLoop->clear(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
354 // finish for all subs first: to avoid nested finishing and "isOperation" calls problems inside
355 bool aResult = false;
356 const std::set<std::string> aSubs = subDocuments(true);
357 std::set<std::string>::iterator aSubIter = aSubs.begin();
358 for (; aSubIter != aSubs.end(); aSubIter++)
359 if (subDoc(*aSubIter)->finishOperation())
362 // transaction may be empty if this document was created during this transaction (create part)
363 if (!myTransactions.empty() && myDoc->CommitCommand()) { // if commit is successfull, just increment counters
364 myTransactions.rbegin()->myOCAFNum++;
368 if (isNestedClosed) {
371 if (!aResult && !myTransactions.empty() /* it can be for just created part document */)
372 aResult = myTransactions.rbegin()->myOCAFNum != 0;
374 if (!aResult && Model_Session::get()->moduleDocument().get() == this) {
375 // nothing inside in all documents, so remove this transaction from the transactions list
376 undoInternal(true, false);
383 void Model_Document::abortOperation()
385 if (!myNestedNum.empty() && !myDoc->HasOpenCommand()) { // abort all what was done in nested
387 undoInternal(false, false);
390 } else { // abort the current
391 int aNumTransactions = myTransactions.rbegin()->myOCAFNum;
392 myTransactions.pop_back();
393 if (!myNestedNum.empty())
394 (*myNestedNum.rbegin())--;
395 // roll back the needed number of transactions
396 myDoc->AbortCommand();
397 for(int a = 0; a < aNumTransactions; a++)
401 synchronizeFeatures(true, false); // references were not changed since transaction start
402 // abort for all subs
403 const std::set<std::string> aSubs = subDocuments(true);
404 std::set<std::string>::iterator aSubIter = aSubs.begin();
405 for (; aSubIter != aSubs.end(); aSubIter++)
406 subDoc(*aSubIter)->abortOperation();
409 bool Model_Document::isOperation() const
411 // operation is opened for all documents: no need to check subs
412 return myDoc->HasOpenCommand() == Standard_True ;
415 bool Model_Document::isModified()
417 // is modified if at least one operation was commited and not undoed
418 return myTransactions.size() != myTransactionSave || isOperation();
421 bool Model_Document::canUndo()
423 // issue 406 : if transaction is opened, but nothing to undo behind, can not undo
424 int aCurrentNum = isOperation() ? 1 : 0;
425 if (myDoc->GetAvailableUndos() > 0 &&
426 (myNestedNum.empty() || *myNestedNum.rbegin() - aCurrentNum > 0) && // there is something to undo in nested
427 myTransactions.size() - aCurrentNum > 0 /* for omitting the first useless transaction */)
429 // check other subs contains operation that can be undoed
430 const std::set<std::string> aSubs = subDocuments(true);
431 std::set<std::string>::iterator aSubIter = aSubs.begin();
432 for (; aSubIter != aSubs.end(); aSubIter++)
433 if (subDoc(*aSubIter)->canUndo())
438 void Model_Document::undoInternal(const bool theWithSubs, const bool theSynchronize)
440 int aNumTransactions = myTransactions.rbegin()->myOCAFNum;
441 myRedos.push_back(*myTransactions.rbegin());
442 myTransactions.pop_back();
443 if (!myNestedNum.empty())
444 (*myNestedNum.rbegin())--;
445 // roll back the needed number of transactions
446 for(int a = 0; a < aNumTransactions; a++)
450 synchronizeFeatures(true, true);
453 const std::set<std::string> aSubs = subDocuments(true);
454 std::set<std::string>::iterator aSubIter = aSubs.begin();
455 for (; aSubIter != aSubs.end(); aSubIter++)
456 subDoc(*aSubIter)->undoInternal(theWithSubs, theSynchronize);
460 void Model_Document::undo()
462 undoInternal(true, true);
465 bool Model_Document::canRedo()
467 if (!myRedos.empty())
469 // check other subs contains operation that can be redoed
470 const std::set<std::string> aSubs = subDocuments(true);
471 std::set<std::string>::iterator aSubIter = aSubs.begin();
472 for (; aSubIter != aSubs.end(); aSubIter++)
473 if (subDoc(*aSubIter)->canRedo())
478 void Model_Document::redo()
480 if (!myNestedNum.empty())
481 (*myNestedNum.rbegin())++;
482 int aNumRedos = myRedos.rbegin()->myOCAFNum;
483 myTransactions.push_back(*myRedos.rbegin());
485 for(int a = 0; a < aNumRedos; a++)
488 synchronizeFeatures(true, true);
490 const std::set<std::string> aSubs = subDocuments(true);
491 std::set<std::string>::iterator aSubIter = aSubs.begin();
492 for (; aSubIter != aSubs.end(); aSubIter++)
493 subDoc(*aSubIter)->redo();
496 std::list<std::string> Model_Document::undoList() const
498 std::list<std::string> aResult;
499 // the number of skipped current operations (on undo they will be aborted)
500 int aSkipCurrent = isOperation() ? 1 : 0;
501 std::list<Transaction>::const_reverse_iterator aTrIter = myTransactions.crbegin();
502 int aNumUndo = myTransactions.size();
503 if (!myNestedNum.empty())
504 aNumUndo = *myNestedNum.rbegin();
505 for( ; aNumUndo > 0; aTrIter++, aNumUndo--) {
506 if (aSkipCurrent == 0) aResult.push_back(aTrIter->myId);
512 std::list<std::string> Model_Document::redoList() const
514 std::list<std::string> aResult;
515 std::list<Transaction>::const_reverse_iterator aTrIter = myRedos.crbegin();
516 for( ; aTrIter != myRedos.crend(); aTrIter++) {
517 aResult.push_back(aTrIter->myId);
522 void Model_Document::operationId(const std::string& theId)
524 myTransactions.rbegin()->myId = theId;
527 /// Append to the array of references a new referenced label
528 static void AddToRefArray(TDF_Label& theArrayLab, TDF_Label& theReferenced)
530 Handle(TDataStd_ReferenceArray) aRefs;
531 if (!theArrayLab.FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
532 aRefs = TDataStd_ReferenceArray::Set(theArrayLab, 0, 0);
533 aRefs->SetValue(0, theReferenced);
534 } else { // extend array by one more element
535 Handle(TDataStd_HLabelArray1) aNewArray = new TDataStd_HLabelArray1(aRefs->Lower(),
537 for (int a = aRefs->Lower(); a <= aRefs->Upper(); a++) {
538 aNewArray->SetValue(a, aRefs->Value(a));
540 aNewArray->SetValue(aRefs->Upper() + 1, theReferenced);
541 aRefs->SetInternalArray(aNewArray);
545 FeaturePtr Model_Document::addFeature(std::string theID)
547 TDF_Label anEmptyLab;
548 FeaturePtr anEmptyFeature;
549 FeaturePtr aFeature = ModelAPI_Session::get()->createFeature(theID);
552 Model_Document* aDocToAdd;
553 if (aFeature->documentToAdd().get()) { // use the customized document to add
554 aDocToAdd = std::dynamic_pointer_cast<Model_Document>(aFeature->documentToAdd()).get();
555 } else { // if customized is not presented, add to "this" document
559 TDF_Label aFeatureLab;
560 if (!aFeature->isAction()) { // do not add action to the data model
561 TDF_Label aFeaturesLab = aDocToAdd->featuresLabel();
562 aFeatureLab = aFeaturesLab.NewChild();
563 aDocToAdd->initData(aFeature, aFeatureLab, TAG_FEATURE_ARGUMENTS);
564 // keep the feature ID to restore document later correctly
565 TDataStd_Comment::Set(aFeatureLab, aFeature->getKind().c_str());
566 aDocToAdd->myObjs.Bind(aFeatureLab, aFeature);
567 // store feature in the history of features array
568 if (aFeature->isInHistory()) {
569 AddToRefArray(aFeaturesLab, aFeatureLab);
572 if (!aFeature->isAction()) { // do not add action to the data model
573 // event: feature is added
574 static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_CREATED);
575 ModelAPI_EventCreator::get()->sendUpdated(aFeature, anEvent);
576 } else { // feature must be executed
577 // no creation event => updater not working, problem with remove part
584 /// Appenad to the array of references a new referenced label.
585 /// If theIndex is not -1, removes element at this index, not theReferenced.
586 /// \returns the index of removed element
587 static int RemoveFromRefArray(TDF_Label theArrayLab, TDF_Label theReferenced,
588 const int theIndex = -1)
590 int aResult = -1; // no returned
591 Handle(TDataStd_ReferenceArray) aRefs;
592 if (theArrayLab.FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
593 if (aRefs->Length() == 1) { // just erase an array
594 if ((theIndex == -1 && aRefs->Value(0) == theReferenced) || theIndex == 0) {
595 theArrayLab.ForgetAttribute(TDataStd_ReferenceArray::GetID());
598 } else { // reduce the array
599 Handle(TDataStd_HLabelArray1) aNewArray = new TDataStd_HLabelArray1(aRefs->Lower(),
601 int aCount = aRefs->Lower();
602 for (int a = aCount; a <= aRefs->Upper(); a++, aCount++) {
603 if ((theIndex == -1 && aRefs->Value(a) == theReferenced) || theIndex == a) {
607 aNewArray->SetValue(aCount, aRefs->Value(a));
610 aRefs->SetInternalArray(aNewArray);
616 void Model_Document::refsToFeature(FeaturePtr theFeature,
617 std::set<std::shared_ptr<ModelAPI_Feature> >& theRefs,
618 const bool isSendError)
620 // check the feature: it must have no depended objects on it
621 // the dependencies can be in the feature results
622 std::list<ResultPtr>::const_iterator aResIter = theFeature->results().cbegin();
623 for(; aResIter != theFeature->results().cend(); aResIter++) {
624 ResultPtr aResult = (*aResIter);
625 std::shared_ptr<Model_Data> aData =
626 std::dynamic_pointer_cast<Model_Data>(aResult->data());
627 if (aData.get() != NULL) {
628 const std::set<AttributePtr>& aRefs = aData->refsToMe();
629 std::set<AttributePtr>::const_iterator aRefIt = aRefs.begin(), aRefLast = aRefs.end();
630 for(; aRefIt != aRefLast; aRefIt++) {
631 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>((*aRefIt)->owner());
632 if (aFeature.get() != NULL)
633 theRefs.insert(aFeature);
637 // the dependencies can be in the feature itself
638 std::shared_ptr<Model_Data> aData =
639 std::dynamic_pointer_cast<Model_Data>(theFeature->data());
640 if (aData && !aData->refsToMe().empty()) {
641 const std::set<AttributePtr>& aRefs = aData->refsToMe();
642 std::set<AttributePtr>::const_iterator aRefIt = aRefs.begin(), aRefLast = aRefs.end();
643 for(; aRefIt != aRefLast; aRefIt++) {
644 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>((*aRefIt)->owner());
645 if (aFeature.get() != NULL)
646 theRefs.insert(aFeature);
650 if (!theRefs.empty() && isSendError) {
652 "Feature '" + theFeature->data()->name() + "' is used and can not be deleted");
656 void Model_Document::removeFeature(FeaturePtr theFeature/*, const bool theCheck*/)
658 std::shared_ptr<Model_Data> aData = std::static_pointer_cast<Model_Data>(theFeature->data());
660 TDF_Label aFeatureLabel = aData->label().Father();
661 if (myObjs.IsBound(aFeatureLabel))
662 myObjs.UnBind(aFeatureLabel);
664 return; // not found feature => do not remove
666 // checking that the sub-element of composite feature is removed: if yes, inform the owner
667 std::set<std::shared_ptr<ModelAPI_Feature> > aRefs;
668 refsToFeature(theFeature, aRefs, false);
669 std::set<std::shared_ptr<ModelAPI_Feature> >::iterator aRefIter = aRefs.begin();
670 for(; aRefIter != aRefs.end(); aRefIter++) {
671 std::shared_ptr<ModelAPI_CompositeFeature> aComposite =
672 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(*aRefIter);
673 if (aComposite.get()) {
674 aComposite->removeFeature(theFeature);
680 static Events_ID EVENT_DISP = Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
681 ModelAPI_EventCreator::get()->sendUpdated(theFeature, EVENT_DISP);
682 // erase all attributes under the label of feature
683 aFeatureLabel.ForgetAllAttributes();
684 // remove it from the references array
685 if (theFeature->isInHistory()) {
686 RemoveFromRefArray(featuresLabel(), aFeatureLabel);
688 // event: feature is deleted
689 ModelAPI_EventCreator::get()->sendDeleted(theFeature->document(), ModelAPI_Feature::group());
693 void Model_Document::addToHistory(const std::shared_ptr<ModelAPI_Object> theObject)
695 TDF_Label aFeaturesLab = featuresLabel();
696 std::shared_ptr<Model_Data> aData = std::static_pointer_cast<Model_Data>(theObject->data());
698 return; // not found feature => do not remove
700 TDF_Label aFeatureLabel = aData->label().Father();
701 // store feature in the history of features array
702 if (theObject->isInHistory()) {
703 AddToRefArray(aFeaturesLab, aFeatureLabel);
705 RemoveFromRefArray(aFeaturesLab, aFeatureLabel);
709 FeaturePtr Model_Document::feature(TDF_Label& theLabel) const
711 if (myObjs.IsBound(theLabel))
712 return myObjs.Find(theLabel);
713 return FeaturePtr(); // not found
716 ObjectPtr Model_Document::object(TDF_Label theLabel)
718 // try feature by label
719 FeaturePtr aFeature = feature(theLabel);
721 return feature(theLabel);
722 TDF_Label aFeatureLabel = theLabel.Father().Father(); // let's suppose it is result
723 aFeature = feature(aFeatureLabel);
725 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aFeature->results();
726 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.cbegin();
727 for (; aRIter != aResults.cend(); aRIter++) {
728 std::shared_ptr<Model_Data> aResData = std::dynamic_pointer_cast<Model_Data>(
730 if (aResData->label().Father().IsEqual(theLabel))
734 return FeaturePtr(); // not found
737 std::shared_ptr<ModelAPI_Document> Model_Document::subDocument(std::string theDocID)
739 return Model_Application::getApplication()->getDocument(theDocID);
742 const std::set<std::string> Model_Document::subDocuments(const bool theActivatedOnly) const
744 std::set<std::string> aResult;
745 // comment must be in any feature: it is kind
747 TDF_ChildIDIterator aLabIter(featuresLabel(), TDataStd_Comment::GetID());
748 for (; aLabIter.More(); aLabIter.Next()) {
749 TDF_Label aFLabel = aLabIter.Value()->Label();
750 FeaturePtr aFeature = feature(aFLabel);
751 if (aFeature.get()) { // if document is closed the feature may be not in myObjs map
752 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aFeature->results();
753 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
754 for (; aRIter != aResults.cend(); aRIter++) {
755 if ((*aRIter)->groupName() != ModelAPI_ResultPart::group()) continue;
756 if ((*aRIter)->isInHistory()) {
757 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aRIter);
758 if (aPart && (!theActivatedOnly || aPart->isActivated()))
759 aResult.insert(aPart->data()->name());
767 std::shared_ptr<Model_Document> Model_Document::subDoc(std::string theDocID)
769 // just store sub-document identifier here to manage it later
770 return std::dynamic_pointer_cast<Model_Document>(
771 Model_Application::getApplication()->getDocument(theDocID));
774 ObjectPtr Model_Document::object(const std::string& theGroupID, const int theIndex,
775 const bool theHidden)
777 if (theGroupID == ModelAPI_Feature::group()) {
780 TDF_ChildIDIterator aLabIter(featuresLabel(), TDataStd_Comment::GetID());
781 for (; aLabIter.More(); aLabIter.Next()) {
782 if (theIndex == anIndex) {
783 TDF_Label aFLabel = aLabIter.Value()->Label();
784 return feature(aFLabel);
789 Handle(TDataStd_ReferenceArray) aRefs;
790 if (!featuresLabel().FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs))
792 if (aRefs->Lower() > theIndex || aRefs->Upper() < theIndex)
794 TDF_Label aFeatureLabel = aRefs->Value(theIndex);
795 return feature(aFeatureLabel);
798 // comment must be in any feature: it is kind
800 TDF_ChildIDIterator aLabIter(featuresLabel(), TDataStd_Comment::GetID());
801 for (; aLabIter.More(); aLabIter.Next()) {
802 TDF_Label aFLabel = aLabIter.Value()->Label();
803 FeaturePtr aFeature = feature(aFLabel);
804 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aFeature->results();
805 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
806 for (; aRIter != aResults.cend(); aRIter++) {
807 if ((*aRIter)->groupName() != theGroupID) continue;
808 bool isIn = theHidden && (*aRIter)->isInHistory();
809 if (!isIn && (*aRIter)->isInHistory()) { // check that there is nobody references this result
810 isIn = !(*aRIter)->isConcealed();
813 if (anIndex == theIndex)
824 std::shared_ptr<ModelAPI_Object> Model_Document::objectByName(
825 const std::string& theGroupID, const std::string& theName)
827 if (theGroupID == ModelAPI_Feature::group()) {
829 TDF_ChildIDIterator aLabIter(featuresLabel(), TDataStd_Comment::GetID());
830 for (; aLabIter.More(); aLabIter.Next()) {
831 TDF_Label aFLabel = aLabIter.Value()->Label();
832 FeaturePtr aFeature = feature(aFLabel);
833 if (aFeature && aFeature->name() == theName)
837 // comment must be in any feature: it is kind
839 TDF_ChildIDIterator aLabIter(featuresLabel(), TDataStd_Comment::GetID());
840 for (; aLabIter.More(); aLabIter.Next()) {
841 TDF_Label aFLabel = aLabIter.Value()->Label();
842 FeaturePtr aFeature = feature(aFLabel);
843 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aFeature->results();
844 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
845 for (; aRIter != aResults.cend(); aRIter++) {
846 if ((*aRIter)->groupName() == theGroupID && (*aRIter)->data()->name() == theName)
855 int Model_Document::size(const std::string& theGroupID, const bool theHidden)
858 if (theGroupID == ModelAPI_Feature::group()) {
860 return myObjs.Size();
862 Handle(TDataStd_ReferenceArray) aRefs;
863 if (featuresLabel().FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs))
864 return aRefs->Length();
867 // comment must be in any feature: it is kind
868 TDF_ChildIDIterator aLabIter(featuresLabel(), TDataStd_Comment::GetID());
869 for (; aLabIter.More(); aLabIter.Next()) {
870 TDF_Label aFLabel = aLabIter.Value()->Label();
871 FeaturePtr aFeature = feature(aFLabel);
872 if (!aFeature) // may be on close
874 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aFeature->results();
875 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
876 for (; aRIter != aResults.cend(); aRIter++) {
877 if ((*aRIter)->groupName() != theGroupID) continue;
878 bool isIn = theHidden;
879 if (!isIn && (*aRIter)->isInHistory()) { // check that there is nobody references this result
880 isIn = !(*aRIter)->isConcealed();
887 // group is not found
891 TDF_Label Model_Document::featuresLabel() const
893 return myDoc->Main().FindChild(TAG_OBJECTS);
896 void Model_Document::setUniqueName(FeaturePtr theFeature)
898 if (!theFeature->data()->name().empty())
899 return; // not needed, name is already defined
900 std::string aName; // result
901 // first count all objects of such kind to start with index = count + 1
903 NCollection_DataMap<TDF_Label, FeaturePtr>::Iterator aFIter(myObjs);
904 for (; aFIter.More(); aFIter.Next()) {
905 if (aFIter.Value()->getKind() == theFeature->getKind())
908 // generate candidate name
909 std::stringstream aNameStream;
910 aNameStream << theFeature->getKind() << "_" << aNumObjects + 1;
911 aName = aNameStream.str();
912 // check this is unique, if not, increase index by 1
913 for (aFIter.Initialize(myObjs); aFIter.More();) {
914 FeaturePtr aFeature = aFIter.Value();
915 bool isSameName = aFeature->data()->name() == aName;
916 if (!isSameName) { // check also results to avoid same results names (actual for Parts)
917 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aFeature->results();
918 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
919 for (; aRIter != aResults.cend(); aRIter++) {
920 isSameName = (*aRIter)->data()->name() == aName;
925 std::stringstream aNameStream;
926 aNameStream << theFeature->getKind() << "_" << aNumObjects + 1;
927 aName = aNameStream.str();
928 // reinitialize iterator to make sure a new name is unique
929 aFIter.Initialize(myObjs);
933 theFeature->data()->setName(aName);
936 void Model_Document::initData(ObjectPtr theObj, TDF_Label theLab, const int theTag)
938 std::shared_ptr<ModelAPI_Document> aThis = Model_Application::getApplication()->getDocument(
940 std::shared_ptr<Model_Data> aData(new Model_Data);
941 aData->setLabel(theLab.FindChild(theTag));
942 aData->setObject(theObj);
943 theObj->setDoc(aThis);
944 theObj->setData(aData);
945 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObj);
947 setUniqueName(aFeature); // must be before "initAttributes" because duplicate part uses name
949 theObj->initAttributes();
952 void Model_Document::synchronizeFeatures(const bool theMarkUpdated, const bool theUpdateReferences)
954 std::shared_ptr<ModelAPI_Document> aThis =
955 Model_Application::getApplication()->getDocument(myID);
956 // after all updates, sends a message that groups of features were created or updated
957 Events_Loop* aLoop = Events_Loop::loop();
958 static Events_ID aDispEvent = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
959 static Events_ID aCreateEvent = Events_Loop::eventByName(EVENT_OBJECT_CREATED);
960 static Events_ID anUpdateEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
961 static Events_ID aRedispEvent = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
962 static Events_ID aDeleteEvent = Events_Loop::eventByName(EVENT_OBJECT_DELETED);
963 static Events_ID aToHideEvent = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
964 aLoop->activateFlushes(false);
966 // update all objects by checking are they on labels or not
967 std::set<FeaturePtr> aNewFeatures, aKeptFeatures;
968 TDF_ChildIDIterator aLabIter(featuresLabel(), TDataStd_Comment::GetID());
969 for (; aLabIter.More(); aLabIter.Next()) {
970 TDF_Label aFeatureLabel = aLabIter.Value()->Label();
972 if (!myObjs.IsBound(aFeatureLabel)) { // a new feature is inserted
974 aFeature = ModelAPI_Session::get()->createFeature(
975 TCollection_AsciiString(Handle(TDataStd_Comment)::DownCast(aLabIter.Value())->Get())
977 if (!aFeature) { // somethig is wrong, most probably, the opened document has invalid structure
978 Events_Error::send("Invalid type of object in the document");
979 aLabIter.Value()->Label().ForgetAllAttributes();
982 // this must be before "setData" to redo the sketch line correctly
983 myObjs.Bind(aFeatureLabel, aFeature);
984 aNewFeatures.insert(aFeature);
985 initData(aFeature, aFeatureLabel, TAG_FEATURE_ARGUMENTS);
987 // event: model is updated
988 ModelAPI_EventCreator::get()->sendUpdated(aFeature, aCreateEvent);
989 } else { // nothing is changed, both iterators are incremented
990 aFeature = myObjs.Find(aFeatureLabel);
991 aKeptFeatures.insert(aFeature);
992 if (theMarkUpdated) {
993 ModelAPI_EventCreator::get()->sendUpdated(aFeature, anUpdateEvent);
997 // update results of the features (after features created because they may be connected, like sketch and sub elements)
998 std::list<FeaturePtr> aComposites; // composites must be updated after their subs (issue 360)
999 TDF_ChildIDIterator aLabIter2(featuresLabel(), TDataStd_Comment::GetID());
1000 for (; aLabIter2.More(); aLabIter2.Next()) {
1001 TDF_Label aFeatureLabel = aLabIter2.Value()->Label();
1002 if (myObjs.IsBound(aFeatureLabel)) { // a new feature is inserted
1003 FeaturePtr aFeature = myObjs.Find(aFeatureLabel);
1004 if (std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aFeature).get())
1005 aComposites.push_back(aFeature);
1006 updateResults(aFeature);
1009 std::list<FeaturePtr>::iterator aComposite = aComposites.begin();
1010 for(; aComposite != aComposites.end(); aComposite++) {
1011 updateResults(*aComposite);
1014 // check all features are checked: if not => it was removed
1015 NCollection_DataMap<TDF_Label, FeaturePtr>::Iterator aFIter(myObjs);
1016 while (aFIter.More()) {
1017 if (aKeptFeatures.find(aFIter.Value()) == aKeptFeatures.end()
1018 && aNewFeatures.find(aFIter.Value()) == aNewFeatures.end()) {
1019 FeaturePtr aFeature = aFIter.Value();
1020 // event: model is updated
1021 //if (aFeature->isInHistory()) {
1022 ModelAPI_EventCreator::get()->sendDeleted(aThis, ModelAPI_Feature::group());
1024 // results of this feature must be redisplayed (hided)
1025 // redisplay also removed feature (used for sketch and AISObject)
1026 ModelAPI_EventCreator::get()->sendUpdated(aFeature, aRedispEvent);
1028 // unbind after the "erase" call: on abort sketch is removes sub-objects that corrupts aFIter
1029 myObjs.UnBind(aFIter.Key());
1030 // reinitialize iterator because unbind may corrupt the previous order in the map
1031 aFIter.Initialize(myObjs);
1036 if (theUpdateReferences) {
1037 synchronizeBackRefs();
1040 myExecuteFeatures = false;
1041 aLoop->activateFlushes(true);
1043 aLoop->flush(aCreateEvent);
1044 aLoop->flush(aDeleteEvent);
1045 aLoop->flush(anUpdateEvent);
1046 aLoop->flush(aRedispEvent);
1047 aLoop->flush(aToHideEvent);
1048 myExecuteFeatures = true;
1051 void Model_Document::synchronizeBackRefs()
1053 std::shared_ptr<ModelAPI_Document> aThis =
1054 Model_Application::getApplication()->getDocument(myID);
1055 // keeps the concealed flags of result to catch the change and create created/deleted events
1056 std::list<std::pair<ResultPtr, bool> > aConcealed;
1057 // first cycle: erase all data about back-references
1058 NCollection_DataMap<TDF_Label, FeaturePtr>::Iterator aFeatures(myObjs);
1059 for(; aFeatures.More(); aFeatures.Next()) {
1060 FeaturePtr aFeature = aFeatures.Value();
1061 std::shared_ptr<Model_Data> aFData =
1062 std::dynamic_pointer_cast<Model_Data>(aFeature->data());
1064 aFData->eraseBackReferences();
1066 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aFeature->results();
1067 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
1068 for (; aRIter != aResults.cend(); aRIter++) {
1069 std::shared_ptr<Model_Data> aResData =
1070 std::dynamic_pointer_cast<Model_Data>((*aRIter)->data());
1072 aConcealed.push_back(std::pair<ResultPtr, bool>(*aRIter, (*aRIter)->isConcealed()));
1073 aResData->eraseBackReferences();
1078 // second cycle: set new back-references: only features may have reference, iterate only them
1079 ModelAPI_ValidatorsFactory* aValidators = ModelAPI_Session::get()->validators();
1080 for(aFeatures.Initialize(myObjs); aFeatures.More(); aFeatures.Next()) {
1081 FeaturePtr aFeature = aFeatures.Value();
1082 std::shared_ptr<Model_Data> aFData =
1083 std::dynamic_pointer_cast<Model_Data>(aFeature->data());
1085 std::list<std::pair<std::string, std::list<ObjectPtr> > > aRefs;
1086 aFData->referencesToObjects(aRefs);
1087 std::list<std::pair<std::string, std::list<ObjectPtr> > >::iterator
1088 aRefsIter = aRefs.begin();
1089 for(; aRefsIter != aRefs.end(); aRefsIter++) {
1090 std::list<ObjectPtr>::iterator aRefTo = aRefsIter->second.begin();
1091 for(; aRefTo != aRefsIter->second.end(); aRefTo++) {
1093 std::shared_ptr<Model_Data> aRefData =
1094 std::dynamic_pointer_cast<Model_Data>((*aRefTo)->data());
1095 aRefData->addBackReference(aFeature, aRefsIter->first); // here the Concealed flag is updated
1101 std::list<std::pair<ResultPtr, bool> >::iterator aCIter = aConcealed.begin();
1102 for(; aCIter != aConcealed.end(); aCIter++) {
1103 if (aCIter->first->isConcealed() != aCIter->second) { // somethign is changed => produce event
1104 if (aCIter->second) { // was concealed become not => creation event
1105 static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_CREATED);
1106 ModelAPI_EventCreator::get()->sendUpdated(aCIter->first, anEvent);
1107 } else { // was not concealed become concealed => delete event
1108 ModelAPI_EventCreator::get()->sendDeleted(aThis, aCIter->first->groupName());
1109 // redisplay for the viewer (it must be disappeared also)
1110 static Events_ID EVENT_DISP =
1111 Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
1112 ModelAPI_EventCreator::get()->sendUpdated(aCIter->first, EVENT_DISP);
1118 TDF_Label Model_Document::resultLabel(
1119 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theResultIndex)
1121 const std::shared_ptr<Model_Data>& aData =
1122 std::dynamic_pointer_cast<Model_Data>(theFeatureData);
1123 return aData->label().Father().FindChild(TAG_FEATURE_RESULTS).FindChild(theResultIndex + 1);
1126 void Model_Document::storeResult(std::shared_ptr<ModelAPI_Data> theFeatureData,
1127 std::shared_ptr<ModelAPI_Result> theResult,
1128 const int theResultIndex)
1130 std::shared_ptr<ModelAPI_Document> aThis =
1131 Model_Application::getApplication()->getDocument(myID);
1132 theResult->setDoc(aThis);
1133 initData(theResult, resultLabel(theFeatureData, theResultIndex), TAG_FEATURE_ARGUMENTS);
1134 if (theResult->data()->name().empty()) { // if was not initialized, generate event and set a name
1135 std::stringstream aNewName;
1136 aNewName<<theFeatureData->name();
1137 if (theResultIndex > 0) // if there are several results, add unique prefix starting from second
1138 aNewName<<"_"<<theResultIndex + 1;
1139 theResult->data()->setName(aNewName.str());
1143 std::shared_ptr<ModelAPI_ResultConstruction> Model_Document::createConstruction(
1144 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex)
1146 TDF_Label aLab = resultLabel(theFeatureData, theIndex);
1147 TDataStd_Comment::Set(aLab, ModelAPI_ResultConstruction::group().c_str());
1148 ObjectPtr anOldObject = object(aLab);
1149 std::shared_ptr<ModelAPI_ResultConstruction> aResult;
1151 aResult = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(anOldObject);
1154 aResult = std::shared_ptr<ModelAPI_ResultConstruction>(new Model_ResultConstruction);
1155 storeResult(theFeatureData, aResult, theIndex);
1160 std::shared_ptr<ModelAPI_ResultBody> Model_Document::createBody(
1161 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex)
1163 TDF_Label aLab = resultLabel(theFeatureData, theIndex);
1164 TDataStd_Comment::Set(aLab, ModelAPI_ResultBody::group().c_str());
1165 ObjectPtr anOldObject = object(aLab);
1166 std::shared_ptr<ModelAPI_ResultBody> aResult;
1168 aResult = std::dynamic_pointer_cast<ModelAPI_ResultBody>(anOldObject);
1171 aResult = std::shared_ptr<ModelAPI_ResultBody>(new Model_ResultBody);
1172 storeResult(theFeatureData, aResult, theIndex);
1177 std::shared_ptr<ModelAPI_ResultPart> Model_Document::createPart(
1178 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex)
1180 TDF_Label aLab = resultLabel(theFeatureData, theIndex);
1181 TDataStd_Comment::Set(aLab, ModelAPI_ResultPart::group().c_str());
1182 ObjectPtr anOldObject = object(aLab);
1183 std::shared_ptr<ModelAPI_ResultPart> aResult;
1185 aResult = std::dynamic_pointer_cast<ModelAPI_ResultPart>(anOldObject);
1188 aResult = std::shared_ptr<ModelAPI_ResultPart>(new Model_ResultPart);
1189 storeResult(theFeatureData, aResult, theIndex);
1194 std::shared_ptr<ModelAPI_ResultGroup> Model_Document::createGroup(
1195 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex)
1197 TDF_Label aLab = resultLabel(theFeatureData, theIndex);
1198 TDataStd_Comment::Set(aLab, ModelAPI_ResultGroup::group().c_str());
1199 ObjectPtr anOldObject = object(aLab);
1200 std::shared_ptr<ModelAPI_ResultGroup> aResult;
1202 aResult = std::dynamic_pointer_cast<ModelAPI_ResultGroup>(anOldObject);
1205 aResult = std::shared_ptr<ModelAPI_ResultGroup>(new Model_ResultGroup(theFeatureData));
1206 storeResult(theFeatureData, aResult, theIndex);
1211 std::shared_ptr<ModelAPI_Feature> Model_Document::feature(
1212 const std::shared_ptr<ModelAPI_Result>& theResult)
1214 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(theResult->data());
1216 TDF_Label aFeatureLab = aData->label().Father().Father().Father();
1217 return feature(aFeatureLab);
1219 return FeaturePtr();
1222 void Model_Document::updateResults(FeaturePtr theFeature)
1224 // for not persistent is will be done by parametric updater automatically
1225 //if (!theFeature->isPersistentResult()) return;
1226 // check the existing results and remove them if there is nothing on the label
1227 std::list<ResultPtr>::const_iterator aResIter = theFeature->results().cbegin();
1228 while(aResIter != theFeature->results().cend()) {
1229 ResultPtr aBody = std::dynamic_pointer_cast<ModelAPI_Result>(*aResIter);
1231 if (!aBody->data()->isValid()) {
1232 // found a disappeared result => remove it
1233 theFeature->removeResult(aBody);
1234 // start iterate from beginning because iterator is corrupted by removing
1235 aResIter = theFeature->results().cbegin();
1241 // it may be on undo
1242 if (!theFeature->data() || !theFeature->data()->isValid())
1244 // check that results are presented on all labels
1245 int aResSize = theFeature->results().size();
1246 TDF_ChildIterator aLabIter(resultLabel(theFeature->data(), 0).Father());
1247 for(; aLabIter.More(); aLabIter.Next()) {
1248 // here must be GUID of the feature
1249 int aResIndex = aLabIter.Value().Tag() - 1;
1251 if (aResSize <= aResIndex) {
1252 TDF_Label anArgLab = aLabIter.Value();
1253 Handle(TDataStd_Comment) aGroup;
1254 if (anArgLab.FindAttribute(TDataStd_Comment::GetID(), aGroup)) {
1255 if (aGroup->Get() == ModelAPI_ResultBody::group().c_str()) {
1256 aNewBody = createBody(theFeature->data(), aResIndex);
1257 } else if (aGroup->Get() == ModelAPI_ResultPart::group().c_str()) {
1258 aNewBody = createPart(theFeature->data(), aResIndex);
1259 } else if (aGroup->Get() == ModelAPI_ResultConstruction::group().c_str()) {
1260 theFeature->execute(); // construction shapes are needed for sketch solver
1262 } else if (aGroup->Get() == ModelAPI_ResultGroup::group().c_str()) {
1263 aNewBody = createGroup(theFeature->data(), aResIndex);
1265 Events_Error::send(std::string("Unknown type of result is found in the document:") +
1266 TCollection_AsciiString(aGroup->Get()).ToCString());
1270 theFeature->setResult(aNewBody, aResIndex);
1276 Standard_Integer HashCode(const TDF_Label& theLab, const Standard_Integer theUpper)
1278 return TDF_LabelMapHasher::HashCode(theLab, theUpper);
1281 Standard_Boolean IsEqual(const TDF_Label& theLab1, const TDF_Label& theLab2)
1283 return TDF_LabelMapHasher::IsEqual(theLab1, theLab2);
1286 void Model_Document::addNamingName(const TDF_Label theLabel, std::string theName)
1288 myNamingNames[theName] = theLabel;
1291 TDF_Label Model_Document::findNamingName(std::string theName)
1293 std::map<std::string, TDF_Label>::iterator aFind = myNamingNames.find(theName);
1294 if (aFind == myNamingNames.end())
1295 return TDF_Label(); // not found
1296 return aFind->second;
1299 ResultPtr Model_Document::findByName(const std::string theName)
1301 NCollection_DataMap<TDF_Label, FeaturePtr>::Iterator anObjIter(myObjs);
1302 for(; anObjIter.More(); anObjIter.Next()) {
1303 FeaturePtr& aFeature = anObjIter.ChangeValue();
1304 if (!aFeature) // may be on close
1306 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aFeature->results();
1307 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
1308 for (; aRIter != aResults.cend(); aRIter++) {
1309 if (aRIter->get() && (*aRIter)->data() && (*aRIter)->data()->isValid() &&
1310 (*aRIter)->data()->name() == theName) {