From 992dc49eceda6e743d32c1c17d2eb8501a27fd6d Mon Sep 17 00:00:00 2001 From: "Zheng, Lei" Date: Tue, 7 Jan 2020 08:53:44 +0800 Subject: [PATCH] Do not throw error when open an already opened document --- src/App/Application.cpp | 5 ++-- src/Gui/Application.cpp | 47 +++++++++++++++++++++++------- src/Gui/Application.h | 3 +- src/Gui/ApplicationPy.cpp | 38 ++++++++++++++++++++++++ src/Mod/Start/StartPage/LoadMRU.py | 4 +-- 5 files changed, 81 insertions(+), 16 deletions(-) diff --git a/src/App/Application.cpp b/src/App/Application.cpp index 679e4753e1..53a0bd44f4 100644 --- a/src/App/Application.cpp +++ b/src/App/Application.cpp @@ -776,9 +776,8 @@ Document* Application::openDocumentPrivate(const char * FileName, if(!isMainDoc) return 0; - std::stringstream str; - str << "The project '" << FileName << "' is already open!"; - throw Base::FileSystemError(str.str().c_str()); + + return it->second; } std::string name; diff --git a/src/Gui/Application.cpp b/src/Gui/Application.cpp index 171cdce4dd..9dd7c1db10 100644 --- a/src/Gui/Application.cpp +++ b/src/Gui/Application.cpp @@ -518,18 +518,34 @@ void Application::open(const char* FileName, const char* Module) if (Module != 0) { try { - // issue module loading - Command::doCommand(Command::App, "import %s", Module); + if(File.hasExtension("FCStd")) { + bool handled = false; + std::string filepath = File.filePath(); + for(auto &v : d->documents) { + auto doc = v.second->getDocument(); + std::string fi = Base::FileInfo(doc->FileName.getValue()).filePath(); + if(filepath == fi) { + handled = true; + Command::doCommand(Command::App, "FreeCADGui.reload('%s')", doc->getName()); + break; + } + } + if(!handled) + Command::doCommand(Command::App, "FreeCAD.openDocument('%s')", FileName); + } else { + // issue module loading + Command::doCommand(Command::App, "import %s", Module); - // load the file with the module - Command::doCommand(Command::App, "%s.open(u\"%s\")", Module, unicodepath.c_str()); + // load the file with the module + Command::doCommand(Command::App, "%s.open(u\"%s\")", Module, unicodepath.c_str()); - // ViewFit - if (!File.hasExtension("FCStd") && sendHasMsgToActiveView("ViewFit")) { - ParameterGrp::handle hGrp = App::GetApplication().GetParameterGroupByPath - ("User parameter:BaseApp/Preferences/View"); - if (hGrp->GetBool("AutoFitToView", true)) - Command::doCommand(Command::Gui, "Gui.SendMsgToActiveView(\"ViewFit\")"); + // ViewFit + if (sendHasMsgToActiveView("ViewFit")) { + ParameterGrp::handle hGrp = App::GetApplication().GetParameterGroupByPath + ("User parameter:BaseApp/Preferences/View"); + if (hGrp->GetBool("AutoFitToView", true)) + Command::doCommand(Command::Gui, "Gui.SendMsgToActiveView(\"ViewFit\")"); + } } // the original file name is required @@ -2266,6 +2282,17 @@ App::Document *Application::reopen(App::Document *doc) { || d->testStatus(App::Document::PartialRestore) ) docs.push_back(d->FileName.getValue()); } + + if(docs.empty()) { + Document *gdoc = getDocument(doc); + if(gdoc) { + setActiveDocument(gdoc); + if(!gdoc->setActiveView()) + gdoc->setActiveView(0,View3DInventor::getClassTypeId()); + } + return doc; + } + for(auto &file : docs) App::GetApplication().openDocument(file.c_str(),false); } diff --git a/src/Gui/Application.h b/src/Gui/Application.h index 8218b734cf..98537ddbd4 100644 --- a/src/Gui/Application.h +++ b/src/Gui/Application.h @@ -264,7 +264,8 @@ public: static PyObject* sOpen (PyObject *self,PyObject *args); // open Python scripts static PyObject* sInsert (PyObject *self,PyObject *args); // open Python scripts static PyObject* sExport (PyObject *self,PyObject *args); - static PyObject* sReload (PyObject *self,PyObject *args); + static PyObject* sReload (PyObject *self,PyObject *args); // reload FCStd file + static PyObject* sLoadFile (PyObject *self,PyObject *args); // open all types of files static PyObject* sCoinRemoveAllChildren (PyObject *self,PyObject *args); diff --git a/src/Gui/ApplicationPy.cpp b/src/Gui/ApplicationPy.cpp index 0deb8c2136..f0b02eea6c 100644 --- a/src/Gui/ApplicationPy.cpp +++ b/src/Gui/ApplicationPy.cpp @@ -218,6 +218,13 @@ PyMethodDef Application::Methods[] = { "reload(name) -> doc\n\n" "Reload a partial opened document"}, + {"loadFile", (PyCFunction) Application::sLoadFile, METH_VARARGS, + "loadFile(string=filename,[string=module]) -> None\n\n" + "Loads an arbitrary file by delegating to the given Python module:\n" + "* If no module is given it will be determined by the file extension.\n" + "* If more than one module can load a file the first one one will be taken.\n" + "* If no module exists to load the file an exception will be raised."}, + {"coinRemoveAllChildren", (PyCFunction) Application::sCoinRemoveAllChildren, METH_VARARGS, "Remove all children from a group node"}, @@ -1470,6 +1477,37 @@ PyObject* Application::sReload(PyObject * /*self*/, PyObject *args) Py_Return; } +PyObject* Application::sLoadFile(PyObject * /*self*/, PyObject *args) +{ + char *path, *mod=""; + if (!PyArg_ParseTuple(args, "s|s", &path, &mod)) // convert args: Python->C + return 0; // NULL triggers exception + PY_TRY { + Base::FileInfo fi(path); + if (!fi.isFile() || !fi.exists()) { + PyErr_Format(PyExc_IOError, "File %s doesn't exist.", path); + return 0; + } + + std::string module = mod; + if (module.empty()) { + std::string ext = fi.extension(); + std::vector modules = App::GetApplication().getImportModules(ext.c_str()); + if (modules.empty()) { + PyErr_Format(PyExc_IOError, "Filetype %s is not supported.", ext.c_str()); + return 0; + } + else { + module = modules.front(); + } + } + + Application::Instance->open(path,mod); + + Py_Return; + } PY_CATCH +} + PyObject* Application::sAddDocObserver(PyObject * /*self*/, PyObject *args) { PyObject* o; diff --git a/src/Mod/Start/StartPage/LoadMRU.py b/src/Mod/Start/StartPage/LoadMRU.py index fa5cbf902a..ea1bfd4b4a 100644 --- a/src/Mod/Start/StartPage/LoadMRU.py +++ b/src/Mod/Start/StartPage/LoadMRU.py @@ -20,10 +20,10 @@ #* * #*************************************************************************** -import FreeCAD,sys +import FreeCADGui,sys # MRU will be given before this script is run rf=FreeCAD.ParamGet("User parameter:BaseApp/Preferences/RecentFiles") -FreeCAD.loadFile(rf.GetString("MRU"+str(MRU))) +FreeCADGui.loadFile(rf.GetString("MRU"+str(MRU))) from StartPage import StartPage StartPage.postStart()