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_CURRENT_FEATURE = 1; ///< where the reference to the current feature label is located (or no attribute if null feature)
56 static const int TAG_FEATURE_ARGUMENTS = 1; ///< where the arguments are located
57 static const int TAG_FEATURE_RESULTS = 2; ///< where the results are located
60 /// 0:1:2 - where features are located
61 /// 0:1:2:N:1 - data of the feature N
62 /// 0:1:2:N:2:K:1 - data of the K result of the feature N
64 Model_Document::Model_Document(const std::string theID, const std::string theKind)
65 : myID(theID), myKind(theKind),
66 myDoc(new TDocStd_Document("BinOcaf")) // binary OCAF format
68 myDoc->SetUndoLimit(UNDO_LIMIT);
69 myTransactionSave = 0;
70 myExecuteFeatures = true;
71 // to have something in the document and avoid empty doc open/save problem
72 // in transaction for nesting correct working
74 TDataStd_Integer::Set(myDoc->Main().Father(), 0);
75 myDoc->CommitCommand();
78 /// Returns the file name of this document by the nameof directory and identifuer of a document
79 static TCollection_ExtendedString DocFileName(const char* theFileName, const std::string& theID)
81 TCollection_ExtendedString aPath((const Standard_CString) theFileName);
82 // remove end-separators
83 while(aPath.Length() &&
84 (aPath.Value(aPath.Length()) == '\\' || aPath.Value(aPath.Length()) == '/'))
85 aPath.Remove(aPath.Length());
87 aPath += theID.c_str();
88 aPath += ".cbf"; // standard binary file extension
92 bool Model_Document::isRoot() const
94 return this == Model_Session::get()->moduleDocument().get();
97 bool Model_Document::load(const char* theFileName)
99 Handle(Model_Application) anApp = Model_Application::getApplication();
101 anApp->setLoadPath(theFileName);
103 TCollection_ExtendedString aPath(DocFileName(theFileName, myID));
104 PCDM_ReaderStatus aStatus = (PCDM_ReaderStatus) -1;
106 aStatus = anApp->Open(aPath, myDoc);
107 } catch (Standard_Failure) {
108 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
110 std::string("Exception in opening of document: ") + aFail->GetMessageString());
113 bool isError = aStatus != PCDM_RS_OK;
116 case PCDM_RS_UnknownDocument:
117 Events_Error::send(std::string("Can not open document"));
119 case PCDM_RS_AlreadyRetrieved:
120 Events_Error::send(std::string("Can not open document: already opened"));
122 case PCDM_RS_AlreadyRetrievedAndModified:
124 std::string("Can not open document: already opened and modified"));
126 case PCDM_RS_NoDriver:
127 Events_Error::send(std::string("Can not open document: driver library is not found"));
129 case PCDM_RS_UnknownFileDriver:
130 Events_Error::send(std::string("Can not open document: unknown driver for opening"));
132 case PCDM_RS_OpenError:
133 Events_Error::send(std::string("Can not open document: file open error"));
135 case PCDM_RS_NoVersion:
136 Events_Error::send(std::string("Can not open document: invalid version"));
138 case PCDM_RS_NoModel:
139 Events_Error::send(std::string("Can not open document: no data model"));
141 case PCDM_RS_NoDocument:
142 Events_Error::send(std::string("Can not open document: no document inside"));
144 case PCDM_RS_FormatFailure:
145 Events_Error::send(std::string("Can not open document: format failure"));
147 case PCDM_RS_TypeNotFoundInSchema:
148 Events_Error::send(std::string("Can not open document: invalid object"));
150 case PCDM_RS_UnrecognizedFileFormat:
151 Events_Error::send(std::string("Can not open document: unrecognized file format"));
153 case PCDM_RS_MakeFailure:
154 Events_Error::send(std::string("Can not open document: make failure"));
156 case PCDM_RS_PermissionDenied:
157 Events_Error::send(std::string("Can not open document: permission denied"));
159 case PCDM_RS_DriverFailure:
160 Events_Error::send(std::string("Can not open document: driver failure"));
163 Events_Error::send(std::string("Can not open document: unknown error"));
168 myDoc->SetUndoLimit(UNDO_LIMIT);
169 // to avoid the problem that feature is created in the current, not this, document
170 std::shared_ptr<Model_Session> aSession =
171 std::dynamic_pointer_cast<Model_Session>(Model_Session::get());
172 aSession->setActiveDocument(anApp->getDocument(myID), false);
173 aSession->setCheckTransactions(false);
174 synchronizeFeatures(false, true, true);
175 aSession->setCheckTransactions(true);
176 aSession->setActiveDocument(Model_Session::get()->moduleDocument(), false);
177 aSession->setActiveDocument(anApp->getDocument(myID), true);
182 bool Model_Document::save(const char* theFileName, std::list<std::string>& theResults)
184 // create a directory in the root document if it is not yet exist
185 Handle(Model_Application) anApp = Model_Application::getApplication();
188 CreateDirectory(theFileName, NULL);
190 mkdir(theFileName, 0x1ff);
193 // filename in the dir is id of document inside of the given directory
194 TCollection_ExtendedString aPath(DocFileName(theFileName, myID));
195 PCDM_StoreStatus aStatus;
197 aStatus = anApp->SaveAs(myDoc, aPath);
198 } catch (Standard_Failure) {
199 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
201 std::string("Exception in saving of document: ") + aFail->GetMessageString());
204 bool isDone = aStatus == PCDM_SS_OK || aStatus == PCDM_SS_No_Obj;
207 case PCDM_SS_DriverFailure:
208 Events_Error::send(std::string("Can not save document: save driver-library failure"));
210 case PCDM_SS_WriteFailure:
211 Events_Error::send(std::string("Can not save document: file writing failure"));
213 case PCDM_SS_Failure:
215 Events_Error::send(std::string("Can not save document"));
219 myTransactionSave = myTransactions.size();
220 if (isDone) { // save also sub-documents if any
221 theResults.push_back(TCollection_AsciiString(aPath).ToCString());
222 const std::set<std::string> aSubs = subDocuments(false);
223 std::set<std::string>::iterator aSubIter = aSubs.begin();
224 for (; aSubIter != aSubs.end() && isDone; aSubIter++) {
225 if (anApp->isLoadByDemand(*aSubIter)) {
226 // copy not-activated document that is not in the memory
227 std::string aDocName = *aSubIter;
228 if (!aDocName.empty()) {
230 TCollection_AsciiString aSubPath(DocFileName(anApp->loadPath().c_str(), aDocName));
231 OSD_Path aPath(aSubPath);
232 OSD_File aFile(aPath);
233 if (aFile.Exists()) {
234 TCollection_AsciiString aDestinationDir(DocFileName(theFileName, aDocName));
235 OSD_Path aDestination(aDestinationDir);
236 aFile.Copy(aDestination);
237 theResults.push_back(aDestinationDir.ToCString());
240 std::string("Can not open file ") + aSubPath.ToCString() + " for saving");
243 } else { // simply save opened document
244 isDone = subDoc(*aSubIter)->save(theFileName, theResults);
251 void Model_Document::close(const bool theForever)
253 std::shared_ptr<ModelAPI_Session> aPM = Model_Session::get();
254 if (!isRoot() && this == aPM->activeDocument().get()) {
255 aPM->setActiveDocument(aPM->moduleDocument());
256 } else if (isRoot()) {
257 // erase the active document if root is closed
258 aPM->setActiveDocument(DocumentPtr());
261 const std::set<std::string> aSubs = subDocuments(true);
262 std::set<std::string>::iterator aSubIter = aSubs.begin();
263 for (; aSubIter != aSubs.end(); aSubIter++)
264 subDoc(*aSubIter)->close(theForever);
266 // close for thid document needs no transaction in this document
267 std::static_pointer_cast<Model_Session>(Model_Session::get())->setCheckTransactions(false);
269 // delete all features of this document
270 std::shared_ptr<ModelAPI_Document> aThis =
271 Model_Application::getApplication()->getDocument(myID);
272 Events_Loop* aLoop = Events_Loop::loop();
273 NCollection_DataMap<TDF_Label, FeaturePtr>::Iterator aFeaturesIter(myObjs);
274 for(; aFeaturesIter.More(); aFeaturesIter.Next()) {
275 FeaturePtr aFeature = aFeaturesIter.Value();
276 static Events_ID EVENT_DISP = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
277 ModelAPI_EventCreator::get()->sendDeleted(aThis, ModelAPI_Feature::group());
278 ModelAPI_EventCreator::get()->sendUpdated(aFeature, EVENT_DISP);
279 aFeature->eraseResults();
280 if (theForever) { // issue #294: do not delete content of the document until it can be redone
283 aFeature->data()->execState(ModelAPI_StateMustBeUpdated);
289 aLoop->flush(Events_Loop::eventByName(EVENT_OBJECT_DELETED));
290 aLoop->flush(Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
292 // close all only if it is really asked, otherwise it can be undoed/redoed
294 if (myDoc->CanClose() == CDM_CCS_OK)
298 std::static_pointer_cast<Model_Session>(Model_Session::get())->setCheckTransactions(true);
301 void Model_Document::startOperation()
303 if (myDoc->HasOpenCommand()) { // start of nested command
304 if (myDoc->CommitCommand()) { // commit the current: it will contain all nested after compactification
305 myTransactions.rbegin()->myOCAFNum++; // if has open command, the list is not empty
307 myNestedNum.push_back(0); // start of nested operation with zero transactions inside yet
308 myDoc->OpenCommand();
309 } else { // start the simple command
312 // starts a new operation
313 myTransactions.push_back(Transaction());
314 if (!myNestedNum.empty())
315 (*myNestedNum.rbegin())++;
317 // new command for all subs
318 const std::set<std::string> aSubs = subDocuments(true);
319 std::set<std::string>::iterator aSubIter = aSubs.begin();
320 for (; aSubIter != aSubs.end(); aSubIter++)
321 subDoc(*aSubIter)->startOperation();
324 void Model_Document::compactNested()
326 if (!myNestedNum.empty()) {
327 int aNumToCompact = *(myNestedNum.rbegin());
328 int aSumOfTransaction = 0;
329 for(int a = 0; a < aNumToCompact; a++) {
330 aSumOfTransaction += myTransactions.rbegin()->myOCAFNum;
331 myTransactions.pop_back();
333 // the latest transaction is the start of lower-level operation which startes the nested
334 myTransactions.rbegin()->myOCAFNum += aSumOfTransaction;
335 myNestedNum.pop_back();
339 bool Model_Document::finishOperation()
341 bool isNestedClosed = !myDoc->HasOpenCommand() && !myNestedNum.empty();
342 static std::shared_ptr<Model_Session> aSession =
343 std::static_pointer_cast<Model_Session>(Model_Session::get());
344 synchronizeBackRefs();
345 Events_Loop* aLoop = Events_Loop::loop();
346 aLoop->flush(Events_Loop::eventByName(EVENT_OBJECT_CREATED));
347 aLoop->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
348 aLoop->flush(Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
349 aLoop->flush(Events_Loop::eventByName(EVENT_OBJECT_TOHIDE));
350 aLoop->flush(Events_Loop::eventByName(EVENT_OBJECT_DELETED));
351 // this must be here just after everything is finished but before real transaction stop
352 // to avoid messages about modifications outside of the transaction
353 // and to rebuild everything after all updates and creates
354 if (isRoot()) { // once for root document
355 Events_Loop::loop()->autoFlush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
356 static std::shared_ptr<Events_Message> aFinishMsg
357 (new Events_Message(Events_Loop::eventByName("FinishOperation")));
358 Events_Loop::loop()->send(aFinishMsg);
359 Events_Loop::loop()->autoFlush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED), false);
361 // to avoid "updated" message appearance by updater
362 //aLoop->clear(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
364 // finish for all subs first: to avoid nested finishing and "isOperation" calls problems inside
365 bool aResult = false;
366 const std::set<std::string> aSubs = subDocuments(true);
367 std::set<std::string>::iterator aSubIter = aSubs.begin();
368 for (; aSubIter != aSubs.end(); aSubIter++)
369 if (subDoc(*aSubIter)->finishOperation())
372 // transaction may be empty if this document was created during this transaction (create part)
373 if (!myTransactions.empty() && myDoc->CommitCommand()) { // if commit is successfull, just increment counters
374 myTransactions.rbegin()->myOCAFNum++;
378 if (isNestedClosed) {
381 if (!aResult && !myTransactions.empty() /* it can be for just created part document */)
382 aResult = myTransactions.rbegin()->myOCAFNum != 0;
384 if (!aResult && isRoot()) {
385 // nothing inside in all documents, so remove this transaction from the transactions list
386 undoInternal(true, false);
388 // on finish clear redos in any case (issue 446) and for all subs (issue 408)
391 for (aSubIter = aSubs.begin(); aSubIter != aSubs.end(); aSubIter++) {
392 subDoc(*aSubIter)->myDoc->ClearRedos();
393 subDoc(*aSubIter)->myRedos.clear();
399 void Model_Document::abortOperation()
401 if (!myNestedNum.empty() && !myDoc->HasOpenCommand()) { // abort all what was done in nested
403 undoInternal(false, false);
406 } else { // abort the current
407 int aNumTransactions = myTransactions.rbegin()->myOCAFNum;
408 myTransactions.pop_back();
409 if (!myNestedNum.empty())
410 (*myNestedNum.rbegin())--;
411 // roll back the needed number of transactions
412 myDoc->AbortCommand();
413 for(int a = 0; a < aNumTransactions; a++)
417 // abort for all subs, flushes will be later, in the end of root abort
418 const std::set<std::string> aSubs = subDocuments(true);
419 std::set<std::string>::iterator aSubIter = aSubs.begin();
420 for (; aSubIter != aSubs.end(); aSubIter++)
421 subDoc(*aSubIter)->abortOperation();
422 // references may be changed because they are set in attributes on the fly
423 synchronizeFeatures(true, true, isRoot());
426 bool Model_Document::isOperation() const
428 // operation is opened for all documents: no need to check subs
429 return myDoc->HasOpenCommand() == Standard_True ;
432 bool Model_Document::isModified()
434 // is modified if at least one operation was commited and not undoed
435 return myTransactions.size() != myTransactionSave || isOperation();
438 bool Model_Document::canUndo()
440 // issue 406 : if transaction is opened, but nothing to undo behind, can not undo
441 int aCurrentNum = isOperation() ? 1 : 0;
442 if (myDoc->GetAvailableUndos() > 0 &&
443 (myNestedNum.empty() || *myNestedNum.rbegin() - aCurrentNum > 0) && // there is something to undo in nested
444 myTransactions.size() - aCurrentNum > 0 /* for omitting the first useless transaction */)
446 // check other subs contains operation that can be undoed
447 const std::set<std::string> aSubs = subDocuments(true);
448 std::set<std::string>::iterator aSubIter = aSubs.begin();
449 for (; aSubIter != aSubs.end(); aSubIter++)
450 if (subDoc(*aSubIter)->canUndo())
455 void Model_Document::undoInternal(const bool theWithSubs, const bool theSynchronize)
457 int aNumTransactions = myTransactions.rbegin()->myOCAFNum;
458 myRedos.push_back(*myTransactions.rbegin());
459 myTransactions.pop_back();
460 if (!myNestedNum.empty())
461 (*myNestedNum.rbegin())--;
462 // roll back the needed number of transactions
463 for(int a = 0; a < aNumTransactions; a++)
468 const std::set<std::string> aSubs = subDocuments(true);
469 std::set<std::string>::iterator aSubIter = aSubs.begin();
470 for (; aSubIter != aSubs.end(); aSubIter++)
471 subDoc(*aSubIter)->undoInternal(theWithSubs, theSynchronize);
473 // after redo of all sub-documents to avoid updates on not-modified data (issue 370)
475 synchronizeFeatures(true, true, isRoot());
478 void Model_Document::undo()
480 undoInternal(true, true);
483 bool Model_Document::canRedo()
485 if (!myRedos.empty())
487 // check other subs contains operation that can be redoed
488 const std::set<std::string> aSubs = subDocuments(true);
489 std::set<std::string>::iterator aSubIter = aSubs.begin();
490 for (; aSubIter != aSubs.end(); aSubIter++)
491 if (subDoc(*aSubIter)->canRedo())
496 void Model_Document::redo()
498 if (!myNestedNum.empty())
499 (*myNestedNum.rbegin())++;
500 int aNumRedos = myRedos.rbegin()->myOCAFNum;
501 myTransactions.push_back(*myRedos.rbegin());
503 for(int a = 0; a < aNumRedos; a++)
507 const std::set<std::string> aSubs = subDocuments(true);
508 std::set<std::string>::iterator aSubIter = aSubs.begin();
509 for (; aSubIter != aSubs.end(); aSubIter++)
510 subDoc(*aSubIter)->redo();
512 // after redo of all sub-documents to avoid updates on not-modified data (issue 370)
513 synchronizeFeatures(true, true, isRoot());
516 std::list<std::string> Model_Document::undoList() const
518 std::list<std::string> aResult;
519 // the number of skipped current operations (on undo they will be aborted)
520 int aSkipCurrent = isOperation() ? 1 : 0;
521 std::list<Transaction>::const_reverse_iterator aTrIter = myTransactions.crbegin();
522 int aNumUndo = myTransactions.size();
523 if (!myNestedNum.empty())
524 aNumUndo = *myNestedNum.rbegin();
525 for( ; aNumUndo > 0; aTrIter++, aNumUndo--) {
526 if (aSkipCurrent == 0) aResult.push_back(aTrIter->myId);
532 std::list<std::string> Model_Document::redoList() const
534 std::list<std::string> aResult;
535 std::list<Transaction>::const_reverse_iterator aTrIter = myRedos.crbegin();
536 for( ; aTrIter != myRedos.crend(); aTrIter++) {
537 aResult.push_back(aTrIter->myId);
542 void Model_Document::operationId(const std::string& theId)
544 myTransactions.rbegin()->myId = theId;
547 /// Append to the array of references a new referenced label
548 static void AddToRefArray(TDF_Label& theArrayLab, TDF_Label& theReferenced)
550 Handle(TDataStd_ReferenceArray) aRefs;
551 if (!theArrayLab.FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
552 aRefs = TDataStd_ReferenceArray::Set(theArrayLab, 0, 0);
553 aRefs->SetValue(0, theReferenced);
554 } else { // extend array by one more element
555 Handle(TDataStd_HLabelArray1) aNewArray = new TDataStd_HLabelArray1(aRefs->Lower(),
557 for (int a = aRefs->Lower(); a <= aRefs->Upper(); a++) {
558 aNewArray->SetValue(a, aRefs->Value(a));
560 aNewArray->SetValue(aRefs->Upper() + 1, theReferenced);
561 aRefs->SetInternalArray(aNewArray);
565 FeaturePtr Model_Document::addFeature(std::string theID)
567 TDF_Label anEmptyLab;
568 FeaturePtr anEmptyFeature;
569 std::shared_ptr<Model_Session> aSession =
570 std::dynamic_pointer_cast<Model_Session>(ModelAPI_Session::get());
571 FeaturePtr aFeature = aSession->createFeature(theID, this);
574 Model_Document* aDocToAdd;
575 if (!aFeature->documentToAdd().empty()) { // use the customized document to add
576 if (aFeature->documentToAdd() != kind()) { // the root document by default
577 aDocToAdd = std::dynamic_pointer_cast<Model_Document>(aSession->moduleDocument()).get();
581 } else { // if customized is not presented, add to "this" document
585 TDF_Label aFeatureLab;
586 if (!aFeature->isAction()) { // do not add action to the data model
587 TDF_Label aFeaturesLab = aDocToAdd->featuresLabel();
588 aFeatureLab = aFeaturesLab.NewChild();
589 aDocToAdd->initData(aFeature, aFeatureLab, TAG_FEATURE_ARGUMENTS);
590 // keep the feature ID to restore document later correctly
591 TDataStd_Comment::Set(aFeatureLab, aFeature->getKind().c_str());
592 aDocToAdd->myObjs.Bind(aFeatureLab, aFeature);
593 // store feature in the history of features array
594 if (aFeature->isInHistory()) {
595 AddToRefArray(aFeaturesLab, aFeatureLab);
598 if (!aFeature->isAction()) { // do not add action to the data model
599 // event: feature is added
600 static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_CREATED);
601 ModelAPI_EventCreator::get()->sendUpdated(aFeature, anEvent);
602 setCurrentFeature(aFeature); // after all this feature stays in the document, so make it current
603 } else { // feature must be executed
604 // no creation event => updater not working, problem with remove part
611 /// Appenad to the array of references a new referenced label.
612 /// If theIndex is not -1, removes element at this index, not theReferenced.
613 /// \returns the index of removed element
614 static int RemoveFromRefArray(TDF_Label theArrayLab, TDF_Label theReferenced,
615 const int theIndex = -1)
617 int aResult = -1; // no returned
618 Handle(TDataStd_ReferenceArray) aRefs;
619 if (theArrayLab.FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
620 if (aRefs->Length() == 1) { // just erase an array
621 if ((theIndex == -1 && aRefs->Value(0) == theReferenced) || theIndex == 0) {
622 theArrayLab.ForgetAttribute(TDataStd_ReferenceArray::GetID());
625 } else { // reduce the array
626 Handle(TDataStd_HLabelArray1) aNewArray = new TDataStd_HLabelArray1(aRefs->Lower(),
628 int aCount = aRefs->Lower();
629 for (int a = aCount; a <= aRefs->Upper(); a++, aCount++) {
630 if ((theIndex == -1 && aRefs->Value(a) == theReferenced) || theIndex == a) {
634 aNewArray->SetValue(aCount, aRefs->Value(a));
637 aRefs->SetInternalArray(aNewArray);
643 void Model_Document::refsToFeature(FeaturePtr theFeature,
644 std::set<std::shared_ptr<ModelAPI_Feature> >& theRefs,
645 const bool isSendError)
647 // check the feature: it must have no depended objects on it
648 // the dependencies can be in the feature results
649 std::list<ResultPtr>::const_iterator aResIter = theFeature->results().cbegin();
650 for(; aResIter != theFeature->results().cend(); aResIter++) {
651 ResultPtr aResult = (*aResIter);
652 std::shared_ptr<Model_Data> aData =
653 std::dynamic_pointer_cast<Model_Data>(aResult->data());
654 if (aData.get() != NULL) {
655 const std::set<AttributePtr>& aRefs = aData->refsToMe();
656 std::set<AttributePtr>::const_iterator aRefIt = aRefs.begin(), aRefLast = aRefs.end();
657 for(; aRefIt != aRefLast; aRefIt++) {
658 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>((*aRefIt)->owner());
659 if (aFeature.get() != NULL)
660 theRefs.insert(aFeature);
664 // the dependencies can be in the feature itself
665 std::shared_ptr<Model_Data> aData =
666 std::dynamic_pointer_cast<Model_Data>(theFeature->data());
667 if (aData && !aData->refsToMe().empty()) {
668 const std::set<AttributePtr>& aRefs = aData->refsToMe();
669 std::set<AttributePtr>::const_iterator aRefIt = aRefs.begin(), aRefLast = aRefs.end();
670 for(; aRefIt != aRefLast; aRefIt++) {
671 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>((*aRefIt)->owner());
672 if (aFeature.get() != NULL)
673 theRefs.insert(aFeature);
677 if (!theRefs.empty() && isSendError) {
679 "Feature '" + theFeature->data()->name() + "' is used and can not be deleted");
683 void Model_Document::removeFeature(FeaturePtr theFeature/*, const bool theCheck*/)
685 std::shared_ptr<Model_Data> aData = std::static_pointer_cast<Model_Data>(theFeature->data());
687 TDF_Label aFeatureLabel = aData->label().Father();
688 if (myObjs.IsBound(aFeatureLabel))
689 myObjs.UnBind(aFeatureLabel);
691 return; // not found feature => do not remove
693 // checking that the sub-element of composite feature is removed: if yes, inform the owner
694 std::set<std::shared_ptr<ModelAPI_Feature> > aRefs;
695 refsToFeature(theFeature, aRefs, false);
696 std::set<std::shared_ptr<ModelAPI_Feature> >::iterator aRefIter = aRefs.begin();
697 for(; aRefIter != aRefs.end(); aRefIter++) {
698 std::shared_ptr<ModelAPI_CompositeFeature> aComposite =
699 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(*aRefIter);
700 if (aComposite.get()) {
701 aComposite->removeFeature(theFeature);
704 // if this feature is current, make the current the previous feature
705 if (theFeature == currentFeature()) {
706 int aCurrentIndex = index(theFeature);
707 if (aCurrentIndex != -1) {
708 setCurrentFeature(std::dynamic_pointer_cast<ModelAPI_Feature>(
709 object(ModelAPI_Feature::group(), aCurrentIndex - 1)));
715 static Events_ID EVENT_DISP = Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
716 ModelAPI_EventCreator::get()->sendUpdated(theFeature, EVENT_DISP);
717 // erase all attributes under the label of feature
718 aFeatureLabel.ForgetAllAttributes();
719 // remove it from the references array
720 if (theFeature->isInHistory()) {
721 RemoveFromRefArray(featuresLabel(), aFeatureLabel);
723 // event: feature is deleted
724 ModelAPI_EventCreator::get()->sendDeleted(theFeature->document(), ModelAPI_Feature::group());
725 // the redisplay signal should be flushed in order to erase the feature presentation in the viewer
726 Events_Loop::loop()->flush(EVENT_DISP);
730 void Model_Document::addToHistory(const std::shared_ptr<ModelAPI_Object> theObject)
732 TDF_Label aFeaturesLab = featuresLabel();
733 std::shared_ptr<Model_Data> aData = std::static_pointer_cast<Model_Data>(theObject->data());
735 return; // not found feature => do not remove
737 TDF_Label aFeatureLabel = aData->label().Father();
738 // store feature in the history of features array
739 if (theObject->isInHistory()) {
740 AddToRefArray(aFeaturesLab, aFeatureLabel);
742 RemoveFromRefArray(aFeaturesLab, aFeatureLabel);
746 FeaturePtr Model_Document::feature(TDF_Label& theLabel) const
748 if (myObjs.IsBound(theLabel))
749 return myObjs.Find(theLabel);
750 return FeaturePtr(); // not found
753 ObjectPtr Model_Document::object(TDF_Label theLabel)
755 // try feature by label
756 FeaturePtr aFeature = feature(theLabel);
758 return feature(theLabel);
759 TDF_Label aFeatureLabel = theLabel.Father().Father(); // let's suppose it is result
760 aFeature = feature(aFeatureLabel);
762 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aFeature->results();
763 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.cbegin();
764 for (; aRIter != aResults.cend(); aRIter++) {
765 std::shared_ptr<Model_Data> aResData = std::dynamic_pointer_cast<Model_Data>(
767 if (aResData->label().Father().IsEqual(theLabel))
771 return FeaturePtr(); // not found
774 std::shared_ptr<ModelAPI_Document> Model_Document::subDocument(std::string theDocID)
776 return Model_Application::getApplication()->getDocument(theDocID);
779 const std::set<std::string> Model_Document::subDocuments(const bool theActivatedOnly) const
781 std::set<std::string> aResult;
782 // comment must be in any feature: it is kind
784 TDF_ChildIDIterator aLabIter(featuresLabel(), TDataStd_Comment::GetID());
785 for (; aLabIter.More(); aLabIter.Next()) {
786 TDF_Label aFLabel = aLabIter.Value()->Label();
787 FeaturePtr aFeature = feature(aFLabel);
788 if (aFeature.get()) { // if document is closed the feature may be not in myObjs map
789 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aFeature->results();
790 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
791 for (; aRIter != aResults.cend(); aRIter++) {
792 if ((*aRIter)->groupName() != ModelAPI_ResultPart::group()) continue;
793 if ((*aRIter)->isInHistory()) {
794 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aRIter);
795 if (aPart && (!theActivatedOnly || aPart->isActivated()))
796 aResult.insert(aPart->data()->name());
804 std::shared_ptr<Model_Document> Model_Document::subDoc(std::string theDocID)
806 // just store sub-document identifier here to manage it later
807 return std::dynamic_pointer_cast<Model_Document>(
808 Model_Application::getApplication()->getDocument(theDocID));
811 ObjectPtr Model_Document::object(const std::string& theGroupID, const int theIndex,
812 const bool theHidden)
814 if (theGroupID == ModelAPI_Feature::group()) {
817 TDF_ChildIDIterator aLabIter(featuresLabel(), TDataStd_Comment::GetID());
818 for (; aLabIter.More(); aLabIter.Next()) {
819 if (theIndex == anIndex) {
820 TDF_Label aFLabel = aLabIter.Value()->Label();
821 return feature(aFLabel);
826 Handle(TDataStd_ReferenceArray) aRefs;
827 if (!featuresLabel().FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs))
829 if (aRefs->Lower() > theIndex || aRefs->Upper() < theIndex)
831 TDF_Label aFeatureLabel = aRefs->Value(theIndex);
832 return feature(aFeatureLabel);
835 // comment must be in any feature: it is kind
837 TDF_ChildIDIterator aLabIter(featuresLabel(), TDataStd_Comment::GetID());
838 for (; aLabIter.More(); aLabIter.Next()) {
839 TDF_Label aFLabel = aLabIter.Value()->Label();
840 FeaturePtr aFeature = feature(aFLabel);
841 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aFeature->results();
842 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
843 for (; aRIter != aResults.cend(); aRIter++) {
844 if ((*aRIter)->groupName() != theGroupID) continue;
845 bool isIn = theHidden && (*aRIter)->isInHistory();
846 if (!isIn && (*aRIter)->isInHistory()) { // check that there is nobody references this result
847 isIn = !(*aRIter)->isConcealed();
850 if (anIndex == theIndex)
861 std::shared_ptr<ModelAPI_Object> Model_Document::objectByName(
862 const std::string& theGroupID, const std::string& theName)
864 if (theGroupID == ModelAPI_Feature::group()) {
866 TDF_ChildIDIterator aLabIter(featuresLabel(), TDataStd_Comment::GetID());
867 for (; aLabIter.More(); aLabIter.Next()) {
868 TDF_Label aFLabel = aLabIter.Value()->Label();
869 FeaturePtr aFeature = feature(aFLabel);
870 if (aFeature && aFeature->name() == theName)
874 // comment must be in any feature: it is kind
876 TDF_ChildIDIterator aLabIter(featuresLabel(), TDataStd_Comment::GetID());
877 for (; aLabIter.More(); aLabIter.Next()) {
878 TDF_Label aFLabel = aLabIter.Value()->Label();
879 FeaturePtr aFeature = feature(aFLabel);
880 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aFeature->results();
881 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
882 for (; aRIter != aResults.cend(); aRIter++) {
883 if ((*aRIter)->groupName() == theGroupID && (*aRIter)->data()->name() == theName)
892 const int Model_Document::index(std::shared_ptr<ModelAPI_Object> theObject)
894 const std::string aGroupName = theObject->groupName();
895 if (aGroupName == ModelAPI_Feature::group()) {
897 TDF_ChildIDIterator aLabIter(featuresLabel(), TDataStd_Comment::GetID());
898 for (; aLabIter.More(); aLabIter.Next()) {
899 TDF_Label aFLabel = aLabIter.Value()->Label();
900 FeaturePtr aFeature = feature(aFLabel);
901 if (aFeature == theObject)
903 if (aFeature->isInHistory())
907 // comment must be in any feature: it is kind
909 TDF_ChildIDIterator aLabIter(featuresLabel(), TDataStd_Comment::GetID());
910 for (; aLabIter.More(); aLabIter.Next()) {
911 TDF_Label aFLabel = aLabIter.Value()->Label();
912 FeaturePtr aFeature = feature(aFLabel);
913 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aFeature->results();
914 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
915 for (; aRIter != aResults.cend(); aRIter++) {
916 if (*aRIter == theObject)
918 if ((*aRIter)->groupName() == aGroupName) {
919 bool isIn = (*aRIter)->isInHistory() && !(*aRIter)->isConcealed();
931 int Model_Document::size(const std::string& theGroupID, const bool theHidden)
934 if (theGroupID == ModelAPI_Feature::group()) {
936 return myObjs.Size();
938 Handle(TDataStd_ReferenceArray) aRefs;
939 if (featuresLabel().FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs))
940 return aRefs->Length();
943 // comment must be in any feature: it is kind
944 TDF_ChildIDIterator aLabIter(featuresLabel(), TDataStd_Comment::GetID());
945 for (; aLabIter.More(); aLabIter.Next()) {
946 TDF_Label aFLabel = aLabIter.Value()->Label();
947 FeaturePtr aFeature = feature(aFLabel);
948 if (!aFeature) // may be on close
950 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aFeature->results();
951 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
952 for (; aRIter != aResults.cend(); aRIter++) {
953 if ((*aRIter)->groupName() != theGroupID) continue;
954 bool isIn = theHidden;
955 if (!isIn && (*aRIter)->isInHistory()) { // check that there is nobody references this result
956 isIn = !(*aRIter)->isConcealed();
963 // group is not found
967 std::shared_ptr<ModelAPI_Feature> Model_Document::currentFeature()
969 static TDF_Label aRefLab = generalLabel().FindChild(TAG_CURRENT_FEATURE);
970 Handle(TDF_Reference) aRef;
971 if (aRefLab.FindAttribute(TDF_Reference::GetID(), aRef)) {
972 TDF_Label aLab = aRef->Get();
973 return feature(aLab);
975 return std::shared_ptr<ModelAPI_Feature>(); // null feature means the higher than first
978 void Model_Document::setCurrentFeature(std::shared_ptr<ModelAPI_Feature> theCurrent)
980 static TDF_Label aRefLab = generalLabel().FindChild(TAG_CURRENT_FEATURE);
981 if (theCurrent.get()) {
982 std::shared_ptr<Model_Data> aData = std::static_pointer_cast<Model_Data>(theCurrent->data());
984 TDF_Label aFeatureLabel = aData->label().Father();
985 Handle(TDF_Reference) aRef;
986 if (aRefLab.FindAttribute(TDF_Reference::GetID(), aRef)) {
987 aRef->Set(aFeatureLabel);
989 aRef = TDF_Reference::Set(aRefLab, aFeatureLabel);
992 } else { // remove reference for the null feature
993 aRefLab.ForgetAttribute(TDF_Reference::GetID());
995 // make all features after this feature disabled in reversed order (to remove results without deps)
996 bool aPassed = false; // flag that the current object is already passed in cycle
997 int aSize = size(ModelAPI_Feature::group(), true);
998 for(int a = aSize - 1; a >= 0; a--) {
999 FeaturePtr aFeature =
1000 std::dynamic_pointer_cast<ModelAPI_Feature>(object(ModelAPI_Feature::group(), a, true));
1001 if (aFeature->setDisabled(!aPassed)) {
1002 // state of feature is changed => so feature become updated
1003 static Events_ID anUpdateEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
1004 ModelAPI_EventCreator::get()->sendUpdated(aFeature, anUpdateEvent);
1007 // check this only after passed become enabled: the current feature is enabled!
1008 if (aFeature == theCurrent) aPassed = true;
1012 TDF_Label Model_Document::featuresLabel() const
1014 return myDoc->Main().FindChild(TAG_OBJECTS);
1017 TDF_Label Model_Document::generalLabel() const
1019 return myDoc->Main().FindChild(TAG_GENERAL);
1022 void Model_Document::setUniqueName(FeaturePtr theFeature)
1024 if (!theFeature->data()->name().empty())
1025 return; // not needed, name is already defined
1026 std::string aName; // result
1027 // first count all objects of such kind to start with index = count + 1
1028 int aNumObjects = 0;
1029 NCollection_DataMap<TDF_Label, FeaturePtr>::Iterator aFIter(myObjs);
1030 for (; aFIter.More(); aFIter.Next()) {
1031 if (aFIter.Value()->getKind() == theFeature->getKind())
1034 // generate candidate name
1035 std::stringstream aNameStream;
1036 aNameStream << theFeature->getKind() << "_" << aNumObjects + 1;
1037 aName = aNameStream.str();
1038 // check this is unique, if not, increase index by 1
1039 for (aFIter.Initialize(myObjs); aFIter.More();) {
1040 FeaturePtr aFeature = aFIter.Value();
1041 bool isSameName = aFeature->data()->name() == aName;
1042 if (!isSameName) { // check also results to avoid same results names (actual for Parts)
1043 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aFeature->results();
1044 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
1045 for (; aRIter != aResults.cend(); aRIter++) {
1046 isSameName = (*aRIter)->data()->name() == aName;
1051 std::stringstream aNameStream;
1052 aNameStream << theFeature->getKind() << "_" << aNumObjects + 1;
1053 aName = aNameStream.str();
1054 // reinitialize iterator to make sure a new name is unique
1055 aFIter.Initialize(myObjs);
1059 theFeature->data()->setName(aName);
1062 void Model_Document::initData(ObjectPtr theObj, TDF_Label theLab, const int theTag)
1064 std::shared_ptr<ModelAPI_Document> aThis = Model_Application::getApplication()->getDocument(
1066 std::shared_ptr<Model_Data> aData(new Model_Data);
1067 aData->setLabel(theLab.FindChild(theTag));
1068 aData->setObject(theObj);
1069 theObj->setDoc(aThis);
1070 theObj->setData(aData);
1071 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObj);
1073 setUniqueName(aFeature); // must be before "initAttributes" because duplicate part uses name
1075 theObj->initAttributes();
1078 void Model_Document::synchronizeFeatures(
1079 const bool theMarkUpdated, const bool theUpdateReferences, const bool theFlush)
1081 std::shared_ptr<ModelAPI_Document> aThis =
1082 Model_Application::getApplication()->getDocument(myID);
1083 // after all updates, sends a message that groups of features were created or updated
1084 Events_Loop* aLoop = Events_Loop::loop();
1085 static Events_ID aDispEvent = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
1086 static Events_ID aCreateEvent = Events_Loop::eventByName(EVENT_OBJECT_CREATED);
1087 static Events_ID anUpdateEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
1088 static Events_ID aRedispEvent = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
1089 static Events_ID aDeleteEvent = Events_Loop::eventByName(EVENT_OBJECT_DELETED);
1090 static Events_ID aToHideEvent = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
1091 aLoop->activateFlushes(false);
1093 // update all objects by checking are they on labels or not
1094 std::set<FeaturePtr> aNewFeatures, aKeptFeatures;
1095 TDF_ChildIDIterator aLabIter(featuresLabel(), TDataStd_Comment::GetID());
1096 for (; aLabIter.More(); aLabIter.Next()) {
1097 TDF_Label aFeatureLabel = aLabIter.Value()->Label();
1098 FeaturePtr aFeature;
1099 if (!myObjs.IsBound(aFeatureLabel)) { // a new feature is inserted
1101 aFeature = std::dynamic_pointer_cast<Model_Session>(ModelAPI_Session::get())->createFeature(
1102 TCollection_AsciiString(Handle(TDataStd_Comment)::DownCast(aLabIter.Value())->Get())
1103 .ToCString(), this);
1104 if (!aFeature) { // somethig is wrong, most probably, the opened document has invalid structure
1105 Events_Error::send("Invalid type of object in the document");
1106 aLabIter.Value()->Label().ForgetAllAttributes();
1109 // this must be before "setData" to redo the sketch line correctly
1110 myObjs.Bind(aFeatureLabel, aFeature);
1111 aNewFeatures.insert(aFeature);
1112 initData(aFeature, aFeatureLabel, TAG_FEATURE_ARGUMENTS);
1114 // event: model is updated
1115 ModelAPI_EventCreator::get()->sendUpdated(aFeature, aCreateEvent);
1116 } else { // nothing is changed, both iterators are incremented
1117 aFeature = myObjs.Find(aFeatureLabel);
1118 aKeptFeatures.insert(aFeature);
1119 if (theMarkUpdated) {
1120 ModelAPI_EventCreator::get()->sendUpdated(aFeature, anUpdateEvent);
1124 // update results of the features (after features created because they may be connected, like sketch and sub elements)
1125 std::list<FeaturePtr> aComposites; // composites must be updated after their subs (issue 360)
1126 TDF_ChildIDIterator aLabIter2(featuresLabel(), TDataStd_Comment::GetID());
1127 for (; aLabIter2.More(); aLabIter2.Next()) {
1128 TDF_Label aFeatureLabel = aLabIter2.Value()->Label();
1129 if (myObjs.IsBound(aFeatureLabel)) { // a new feature is inserted
1130 FeaturePtr aFeature = myObjs.Find(aFeatureLabel);
1131 if (std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aFeature).get())
1132 aComposites.push_back(aFeature);
1133 updateResults(aFeature);
1136 std::list<FeaturePtr>::iterator aComposite = aComposites.begin();
1137 for(; aComposite != aComposites.end(); aComposite++) {
1138 updateResults(*aComposite);
1141 // check all features are checked: if not => it was removed
1142 NCollection_DataMap<TDF_Label, FeaturePtr>::Iterator aFIter(myObjs);
1143 while (aFIter.More()) {
1144 if (aKeptFeatures.find(aFIter.Value()) == aKeptFeatures.end()
1145 && aNewFeatures.find(aFIter.Value()) == aNewFeatures.end()) {
1146 FeaturePtr aFeature = aFIter.Value();
1147 // event: model is updated
1148 //if (aFeature->isInHistory()) {
1149 ModelAPI_EventCreator::get()->sendDeleted(aThis, ModelAPI_Feature::group());
1151 // results of this feature must be redisplayed (hided)
1152 // redisplay also removed feature (used for sketch and AISObject)
1153 ModelAPI_EventCreator::get()->sendUpdated(aFeature, aRedispEvent);
1155 // unbind after the "erase" call: on abort sketch is removes sub-objects that corrupts aFIter
1156 myObjs.UnBind(aFIter.Key());
1157 // reinitialize iterator because unbind may corrupt the previous order in the map
1158 aFIter.Initialize(myObjs);
1163 if (theUpdateReferences) {
1164 synchronizeBackRefs();
1167 myExecuteFeatures = false;
1168 aLoop->activateFlushes(true);
1171 aLoop->flush(aCreateEvent);
1172 aLoop->flush(aDeleteEvent);
1173 aLoop->flush(anUpdateEvent);
1174 aLoop->flush(aRedispEvent);
1175 aLoop->flush(aToHideEvent);
1177 myExecuteFeatures = true;
1180 void Model_Document::synchronizeBackRefs()
1182 std::shared_ptr<ModelAPI_Document> aThis =
1183 Model_Application::getApplication()->getDocument(myID);
1184 // keeps the concealed flags of result to catch the change and create created/deleted events
1185 std::list<std::pair<ResultPtr, bool> > aConcealed;
1186 // first cycle: erase all data about back-references
1187 NCollection_DataMap<TDF_Label, FeaturePtr>::Iterator aFeatures(myObjs);
1188 for(; aFeatures.More(); aFeatures.Next()) {
1189 FeaturePtr aFeature = aFeatures.Value();
1190 std::shared_ptr<Model_Data> aFData =
1191 std::dynamic_pointer_cast<Model_Data>(aFeature->data());
1193 aFData->eraseBackReferences();
1195 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aFeature->results();
1196 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
1197 for (; aRIter != aResults.cend(); aRIter++) {
1198 std::shared_ptr<Model_Data> aResData =
1199 std::dynamic_pointer_cast<Model_Data>((*aRIter)->data());
1201 aConcealed.push_back(std::pair<ResultPtr, bool>(*aRIter, (*aRIter)->isConcealed()));
1202 aResData->eraseBackReferences();
1207 // second cycle: set new back-references: only features may have reference, iterate only them
1208 ModelAPI_ValidatorsFactory* aValidators = ModelAPI_Session::get()->validators();
1209 for(aFeatures.Initialize(myObjs); aFeatures.More(); aFeatures.Next()) {
1210 FeaturePtr aFeature = aFeatures.Value();
1211 std::shared_ptr<Model_Data> aFData =
1212 std::dynamic_pointer_cast<Model_Data>(aFeature->data());
1214 std::list<std::pair<std::string, std::list<ObjectPtr> > > aRefs;
1215 aFData->referencesToObjects(aRefs);
1216 std::list<std::pair<std::string, std::list<ObjectPtr> > >::iterator
1217 aRefsIter = aRefs.begin();
1218 for(; aRefsIter != aRefs.end(); aRefsIter++) {
1219 std::list<ObjectPtr>::iterator aRefTo = aRefsIter->second.begin();
1220 for(; aRefTo != aRefsIter->second.end(); aRefTo++) {
1222 std::shared_ptr<Model_Data> aRefData =
1223 std::dynamic_pointer_cast<Model_Data>((*aRefTo)->data());
1224 aRefData->addBackReference(aFeature, aRefsIter->first); // here the Concealed flag is updated
1230 std::list<std::pair<ResultPtr, bool> >::iterator aCIter = aConcealed.begin();
1231 for(; aCIter != aConcealed.end(); aCIter++) {
1232 if (aCIter->first->isConcealed() != aCIter->second) { // somethign is changed => produce event
1233 if (aCIter->second) { // was concealed become not => creation event
1234 static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_CREATED);
1235 ModelAPI_EventCreator::get()->sendUpdated(aCIter->first, anEvent);
1236 } else { // was not concealed become concealed => delete event
1237 ModelAPI_EventCreator::get()->sendDeleted(aThis, aCIter->first->groupName());
1238 // redisplay for the viewer (it must be disappeared also)
1239 static Events_ID EVENT_DISP =
1240 Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
1241 ModelAPI_EventCreator::get()->sendUpdated(aCIter->first, EVENT_DISP);
1247 TDF_Label Model_Document::resultLabel(
1248 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theResultIndex)
1250 const std::shared_ptr<Model_Data>& aData =
1251 std::dynamic_pointer_cast<Model_Data>(theFeatureData);
1252 return aData->label().Father().FindChild(TAG_FEATURE_RESULTS).FindChild(theResultIndex + 1);
1255 void Model_Document::storeResult(std::shared_ptr<ModelAPI_Data> theFeatureData,
1256 std::shared_ptr<ModelAPI_Result> theResult,
1257 const int theResultIndex)
1259 std::shared_ptr<ModelAPI_Document> aThis =
1260 Model_Application::getApplication()->getDocument(myID);
1261 theResult->setDoc(aThis);
1262 initData(theResult, resultLabel(theFeatureData, theResultIndex), TAG_FEATURE_ARGUMENTS);
1263 if (theResult->data()->name().empty()) { // if was not initialized, generate event and set a name
1264 std::stringstream aNewName;
1265 aNewName<<theFeatureData->name();
1266 if (theResultIndex > 0) // if there are several results, add unique prefix starting from second
1267 aNewName<<"_"<<theResultIndex + 1;
1268 theResult->data()->setName(aNewName.str());
1272 std::shared_ptr<ModelAPI_ResultConstruction> Model_Document::createConstruction(
1273 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex)
1275 TDF_Label aLab = resultLabel(theFeatureData, theIndex);
1276 TDataStd_Comment::Set(aLab, ModelAPI_ResultConstruction::group().c_str());
1277 ObjectPtr anOldObject = object(aLab);
1278 std::shared_ptr<ModelAPI_ResultConstruction> aResult;
1280 aResult = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(anOldObject);
1283 aResult = std::shared_ptr<ModelAPI_ResultConstruction>(new Model_ResultConstruction);
1284 storeResult(theFeatureData, aResult, theIndex);
1289 std::shared_ptr<ModelAPI_ResultBody> Model_Document::createBody(
1290 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex)
1292 TDF_Label aLab = resultLabel(theFeatureData, theIndex);
1293 TDataStd_Comment::Set(aLab, ModelAPI_ResultBody::group().c_str());
1294 ObjectPtr anOldObject = object(aLab);
1295 std::shared_ptr<ModelAPI_ResultBody> aResult;
1297 aResult = std::dynamic_pointer_cast<ModelAPI_ResultBody>(anOldObject);
1300 aResult = std::shared_ptr<ModelAPI_ResultBody>(new Model_ResultBody);
1301 storeResult(theFeatureData, aResult, theIndex);
1306 std::shared_ptr<ModelAPI_ResultPart> Model_Document::createPart(
1307 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex)
1309 TDF_Label aLab = resultLabel(theFeatureData, theIndex);
1310 TDataStd_Comment::Set(aLab, ModelAPI_ResultPart::group().c_str());
1311 ObjectPtr anOldObject = object(aLab);
1312 std::shared_ptr<ModelAPI_ResultPart> aResult;
1314 aResult = std::dynamic_pointer_cast<ModelAPI_ResultPart>(anOldObject);
1317 aResult = std::shared_ptr<ModelAPI_ResultPart>(new Model_ResultPart);
1318 storeResult(theFeatureData, aResult, theIndex);
1323 std::shared_ptr<ModelAPI_ResultGroup> Model_Document::createGroup(
1324 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex)
1326 TDF_Label aLab = resultLabel(theFeatureData, theIndex);
1327 TDataStd_Comment::Set(aLab, ModelAPI_ResultGroup::group().c_str());
1328 ObjectPtr anOldObject = object(aLab);
1329 std::shared_ptr<ModelAPI_ResultGroup> aResult;
1331 aResult = std::dynamic_pointer_cast<ModelAPI_ResultGroup>(anOldObject);
1334 aResult = std::shared_ptr<ModelAPI_ResultGroup>(new Model_ResultGroup(theFeatureData));
1335 storeResult(theFeatureData, aResult, theIndex);
1340 std::shared_ptr<ModelAPI_ResultParameter> Model_Document::createParameter(
1341 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex)
1343 TDF_Label aLab = resultLabel(theFeatureData, theIndex);
1344 TDataStd_Comment::Set(aLab, ModelAPI_ResultParameter::group().c_str());
1345 ObjectPtr anOldObject = object(aLab);
1346 std::shared_ptr<ModelAPI_ResultParameter> aResult;
1348 aResult = std::dynamic_pointer_cast<ModelAPI_ResultParameter>(anOldObject);
1351 aResult = std::shared_ptr<ModelAPI_ResultParameter>(new Model_ResultParameter);
1352 storeResult(theFeatureData, aResult, theIndex);
1357 std::shared_ptr<ModelAPI_Feature> Model_Document::feature(
1358 const std::shared_ptr<ModelAPI_Result>& theResult)
1360 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(theResult->data());
1362 TDF_Label aFeatureLab = aData->label().Father().Father().Father();
1363 return feature(aFeatureLab);
1365 return FeaturePtr();
1368 void Model_Document::updateResults(FeaturePtr theFeature)
1370 // for not persistent is will be done by parametric updater automatically
1371 //if (!theFeature->isPersistentResult()) return;
1372 // check the existing results and remove them if there is nothing on the label
1373 std::list<ResultPtr>::const_iterator aResIter = theFeature->results().cbegin();
1374 while(aResIter != theFeature->results().cend()) {
1375 ResultPtr aBody = std::dynamic_pointer_cast<ModelAPI_Result>(*aResIter);
1377 if (!aBody->data()->isValid()) {
1378 // found a disappeared result => remove it
1379 theFeature->removeResult(aBody);
1380 // start iterate from beginning because iterator is corrupted by removing
1381 aResIter = theFeature->results().cbegin();
1387 // it may be on undo
1388 if (!theFeature->data() || !theFeature->data()->isValid())
1390 // check that results are presented on all labels
1391 int aResSize = theFeature->results().size();
1392 TDF_ChildIterator aLabIter(resultLabel(theFeature->data(), 0).Father());
1393 for(; aLabIter.More(); aLabIter.Next()) {
1394 // here must be GUID of the feature
1395 int aResIndex = aLabIter.Value().Tag() - 1;
1397 if (aResSize <= aResIndex) {
1398 TDF_Label anArgLab = aLabIter.Value();
1399 Handle(TDataStd_Comment) aGroup;
1400 if (anArgLab.FindAttribute(TDataStd_Comment::GetID(), aGroup)) {
1401 if (aGroup->Get() == ModelAPI_ResultBody::group().c_str()) {
1402 aNewBody = createBody(theFeature->data(), aResIndex);
1403 } else if (aGroup->Get() == ModelAPI_ResultPart::group().c_str()) {
1404 aNewBody = createPart(theFeature->data(), aResIndex);
1405 } else if (aGroup->Get() == ModelAPI_ResultConstruction::group().c_str()) {
1406 theFeature->execute(); // construction shapes are needed for sketch solver
1408 } else if (aGroup->Get() == ModelAPI_ResultGroup::group().c_str()) {
1409 aNewBody = createGroup(theFeature->data(), aResIndex);
1410 } else if (aGroup->Get() == ModelAPI_ResultParameter::group().c_str()) {
1411 theFeature->attributeChanged("expression"); // just produce a value
1414 Events_Error::send(std::string("Unknown type of result is found in the document:") +
1415 TCollection_AsciiString(aGroup->Get()).ToCString());
1419 theFeature->setResult(aNewBody, aResIndex);
1425 Standard_Integer HashCode(const TDF_Label& theLab, const Standard_Integer theUpper)
1427 return TDF_LabelMapHasher::HashCode(theLab, theUpper);
1430 Standard_Boolean IsEqual(const TDF_Label& theLab1, const TDF_Label& theLab2)
1432 return TDF_LabelMapHasher::IsEqual(theLab1, theLab2);
1435 void Model_Document::addNamingName(const TDF_Label theLabel, std::string theName)
1437 myNamingNames[theName] = theLabel;
1440 TDF_Label Model_Document::findNamingName(std::string theName)
1442 std::map<std::string, TDF_Label>::iterator aFind = myNamingNames.find(theName);
1443 if (aFind == myNamingNames.end())
1444 return TDF_Label(); // not found
1445 return aFind->second;
1448 ResultPtr Model_Document::findByName(const std::string theName)
1450 NCollection_DataMap<TDF_Label, FeaturePtr>::Iterator anObjIter(myObjs);
1451 for(; anObjIter.More(); anObjIter.Next()) {
1452 FeaturePtr& aFeature = anObjIter.ChangeValue();
1453 if (!aFeature) // may be on close
1455 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aFeature->results();
1456 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
1457 for (; aRIter != aResults.cend(); aRIter++) {
1458 if (aRIter->get() && (*aRIter)->data() && (*aRIter)->data()->isValid() &&
1459 (*aRIter)->data()->name() == theName) {