X-Git-Url: http://git.salome-platform.org/gitweb/?a=blobdiff_plain;f=src%2FModel%2FModel_Document.cpp;h=274fc6f1b02719f206957a6048cf024abb870d8a;hb=2af52b19b485181c61d1065fa45ec24b94cb1449;hp=8c305effa4f45ff18eee37728e9f1cf8a144e732;hpb=b2392a7380b299a0c0687edc26aa58c1223199ff;p=modules%2Fshaper.git diff --git a/src/Model/Model_Document.cpp b/src/Model/Model_Document.cpp index 8c305effa..274fc6f1b 100644 --- a/src/Model/Model_Document.cpp +++ b/src/Model/Model_Document.cpp @@ -9,6 +9,7 @@ #include #include #include +#include #include #include @@ -18,6 +19,12 @@ #include +#ifdef WIN32 +# define _separator_ '\\' +#else +# define _separator_ '/' +#endif + static const int UNDO_LIMIT = 10; // number of possible undo operations static const int TAG_GENERAL = 1; // general properties tag @@ -26,83 +33,125 @@ static const int TAG_HISTORY = 3; // tag of the history sub-tree (python dump) using namespace std; +/// Returns the file name of this document by the nameof directory and identifuer of a document +static TCollection_ExtendedString DocFileName(const char* theFileName, const string& theID) +{ + TCollection_ExtendedString aPath ((const Standard_CString)theFileName); + aPath += _separator_; + aPath += theID.c_str(); + aPath += ".cbf"; // standard binary file extension + return aPath; +} + bool Model_Document::load(const char* theFileName) { - bool myIsError = Standard_False; - /* - TCollection_ExtendedString aPath ((const Standard_CString)theFileName); - PCDM_ReaderStatus aStatus = (PCDM_ReaderStatus) -1; - try - { - Handle(TDocStd_Document) aDoc = this; - aStatus = Model_Application::GetApplication()->Open(aPath, aDoc); - } - catch (Standard_Failure) - {} - myIsError = aStatus != PCDM_RS_OK; - if (myIsError) - { - switch (aStatus) - { - case PCDM_RS_UnknownDocument: cout<<"OCAFApp_Appl_RUnknownDocument"<rootDocument().get()) { + anApp->setLoadPath(theFileName); + } + TCollection_ExtendedString aPath (DocFileName(theFileName, myID)); + PCDM_ReaderStatus aStatus = (PCDM_ReaderStatus) -1; + try + { + aStatus = anApp->Open(aPath, myDoc); + } + catch (Standard_Failure) + { + Handle(Standard_Failure) aFail = Standard_Failure::Caught(); + Events_Error::send(string("Exception in opening of document: ") + aFail->GetMessageString()); + return false; + } + bool isError = aStatus != PCDM_RS_OK; + if (isError) + { + switch (aStatus) + { + case PCDM_RS_UnknownDocument: + Events_Error::send(string("Can not open document: PCDM_RS_UnknownDocument")); break; + case PCDM_RS_AlreadyRetrieved: + Events_Error::send(string("Can not open document: PCDM_RS_AlreadyRetrieved")); break; + case PCDM_RS_AlreadyRetrievedAndModified: + Events_Error::send(string("Can not open document: PCDM_RS_AlreadyRetrievedAndModified")); break; + case PCDM_RS_NoDriver: + Events_Error::send(string("Can not open document: PCDM_RS_NoDriver")); break; + case PCDM_RS_UnknownFileDriver: + Events_Error::send(string("Can not open document: PCDM_RS_UnknownFileDriver")); break; + case PCDM_RS_OpenError: + Events_Error::send(string("Can not open document: PCDM_RS_OpenError")); break; + case PCDM_RS_NoVersion: + Events_Error::send(string("Can not open document: PCDM_RS_NoVersion")); break; + case PCDM_RS_NoModel: + Events_Error::send(string("Can not open document: PCDM_RS_NoModel")); break; + case PCDM_RS_NoDocument: + Events_Error::send(string("Can not open document: PCDM_RS_NoDocument")); break; + case PCDM_RS_FormatFailure: + Events_Error::send(string("Can not open document: PCDM_RS_FormatFailure")); break; + case PCDM_RS_TypeNotFoundInSchema: + Events_Error::send(string("Can not open document: PCDM_RS_TypeNotFoundInSchema")); break; + case PCDM_RS_UnrecognizedFileFormat: + Events_Error::send(string("Can not open document: PCDM_RS_UnrecognizedFileFormat")); break; + case PCDM_RS_MakeFailure: + Events_Error::send(string("Can not open document: PCDM_RS_MakeFailure")); break; + case PCDM_RS_PermissionDenied: + Events_Error::send(string("Can not open document: PCDM_RS_PermissionDenied")); break; + case PCDM_RS_DriverFailure: + Events_Error::send(string("Can not open document: PCDM_RS_DriverFailure")); break; + default: + Events_Error::send(string("Can not open document: unknown error")); break; + } + } + if (!isError) { + myDoc->SetUndoLimit(UNDO_LIMIT); + synchronizeFeatures(); + } + return !isError; } bool Model_Document::save(const char* theFileName) { - bool myIsError = true; - /* - TCollection_ExtendedString aPath ((const Standard_CString)theFileName); - PCDM_StoreStatus aStatus; - try { - Handle(TDocStd_Document) aDoc = this; - aStatus = Model_Application::GetApplication()->SaveAs (aDoc, aPath); - } - catch (Standard_Failure) { - Handle(Standard_Failure) aFail = Standard_Failure::Caught(); - cout<<"OCAFApp_Engine:save Error: "<GetMessageString()<rootDocument().get()) { +#ifdef WIN32 + CreateDirectory(theFileName, NULL); +#else + mkdir(theFileName, 0x1ff); +#endif + } + // filename in the dir is id of document inside of the given directory + TCollection_ExtendedString aPath(DocFileName(theFileName, myID)); + PCDM_StoreStatus aStatus; + try { + aStatus = Model_Application::getApplication()->SaveAs(myDoc, aPath); + } + catch (Standard_Failure) { + Handle(Standard_Failure) aFail = Standard_Failure::Caught(); + Events_Error::send(string("Exception in saving of document: ") + aFail->GetMessageString()); + return false; + } + bool isDone = aStatus == PCDM_SS_OK || aStatus == PCDM_SS_No_Obj; + if (!isDone) + { + switch (aStatus) + { + case PCDM_SS_DriverFailure: + Events_Error::send(string("Can not save document: PCDM_SS_DriverFailure")); + break; + case PCDM_SS_WriteFailure: + Events_Error::send(string("Can not save document: PCDM_SS_WriteFailure")); + break; + case PCDM_SS_Failure: + default: + Events_Error::send(string("Can not save document: PCDM_SS_Failure")); + break; + } + } + myTransactionsAfterSave = 0; + if (isDone) { // save also sub-documents if any + set::iterator aSubIter = mySubs.begin(); + for(; aSubIter != mySubs.end() && isDone; aSubIter++) + isDone = subDocument(*aSubIter)->save(theFileName); + } + return isDone; } void Model_Document::close() @@ -119,6 +168,10 @@ void Model_Document::close() void Model_Document::startOperation() { + // check is it nested or not + if (myDoc->HasOpenCommand()) { + myIsNested = true; + } // new command for this myDoc->NewCommand(); // new command for all subs @@ -132,6 +185,7 @@ void Model_Document::finishOperation() // returns false if delta is empty and no transaction was made myIsEmptyTr[myTransactionsAfterSave] = !myDoc->CommitCommand(); myTransactionsAfterSave++; + myIsNested = false; // finish for all subs set::iterator aSubIter = mySubs.begin(); for(; aSubIter != mySubs.end(); aSubIter++) @@ -344,6 +398,10 @@ Model_Document::Model_Document(const std::string theID) { myDoc->SetUndoLimit(UNDO_LIMIT); myTransactionsAfterSave = 0; + myIsNested = false; + myDoc->SetNestedTransactionMode(); + // to have something in the document and avoid empty doc open/save problem + TDataStd_Integer::Set(myDoc->Main().Father(), 0); } TDF_Label Model_Document::groupLabel(const string theGroup) @@ -458,10 +516,6 @@ void Model_Document::synchronizeFeatures() aFeature->setDoc(aThis); aFeature->setData(aData); aFeature->initAttributes(); - // event: model is updated - static Events_ID anEvent = Events_Loop::eventByName(EVENT_FEATURE_CREATED); - Model_FeatureUpdatedMessage aMsg(aFeature, anEvent); - Events_Loop::loop()->send(aMsg); if (aFIter == aFeatures.end()) { aFeatures.push_back(aFeature); @@ -470,6 +524,11 @@ void Model_Document::synchronizeFeatures() aFIter++; aFeatures.insert(aFIter, aFeature); } + // event: model is updated + static Events_ID anEvent = Events_Loop::eventByName(EVENT_FEATURE_CREATED); + Model_FeatureUpdatedMessage aMsg(aFeature, anEvent); + Events_Loop::loop()->send(aMsg); + // feature for this label is added, so go to the next label aFLabIter.Next(); } else { // nothing is changed, both iterators are incremented