From 120b69c74f7dac8c4fa6dbc0a7e10674bde9216c Mon Sep 17 00:00:00 2001 From: wmayer Date: Sat, 18 Jan 2020 15:29:44 +0100 Subject: [PATCH] App: add methods to change import/export module of a registered filetype --- src/App/Application.cpp | 38 +++++++++++++++++++++++++++++--------- src/App/Application.h | 6 ++++++ src/App/ApplicationPy.cpp | 28 ++++++++++++++++++++++++++++ src/Mod/Import/InitGui.py | 4 ++++ src/Mod/Part/Init.py | 4 ++-- 5 files changed, 69 insertions(+), 11 deletions(-) diff --git a/src/App/Application.cpp b/src/App/Application.cpp index 5624e8e4e7..679e4753e1 100644 --- a/src/App/Application.cpp +++ b/src/App/Application.cpp @@ -130,13 +130,13 @@ using namespace App; using namespace std; using namespace boost; -using namespace boost::program_options; - - -// scriptings (scripts are built-in but can be overridden by command line option) -#include -#include -#include +using namespace boost::program_options; + + +// scriptings (scripts are built-in but can be overridden by command line option) +#include +#include +#include #ifdef _MSC_VER // New handler for Microsoft Visual C++ compiler # pragma warning( disable : 4535 ) @@ -1082,6 +1082,16 @@ void Application::addImportType(const char* Type, const char* ModuleName) } } +void Application::changeImportModule(const char* Type, const char* OldModuleName, const char* NewModuleName) +{ + for (auto& it : _mImportTypes) { + if (it.filter == Type && it.module == OldModuleName) { + it.module = NewModuleName; + break; + } + } +} + std::vector Application::getImportModules(const char* Type) const { std::vector modules; @@ -1195,6 +1205,16 @@ void Application::addExportType(const char* Type, const char* ModuleName) } } +void Application::changeExportModule(const char* Type, const char* OldModuleName, const char* NewModuleName) +{ + for (auto& it : _mExportTypes) { + if (it.filter == Type && it.module == OldModuleName) { + it.module = NewModuleName; + break; + } + } +} + std::vector Application::getExportModules(const char* Type) const { std::vector modules; @@ -2256,7 +2276,7 @@ void Application::LoadParameters(void) #if defined(_MSC_VER) // fix weird error while linking boost (all versions of VC) -// VS2010: https://forum.freecadweb.org/viewtopic.php?f=4&t=1886&p=12553&hilit=boost%3A%3Afilesystem%3A%3Aget#p12553 +// VS2010: https://forum.freecadweb.org/viewtopic.php?f=4&t=1886&p=12553&hilit=boost%3A%3Afilesystem%3A%3Aget#p12553 namespace boost { namespace program_options { std::string arg="arg"; } } #if (defined (BOOST_VERSION) && (BOOST_VERSION >= 104100)) namespace boost { namespace program_options { @@ -2946,7 +2966,7 @@ std::string Application::FindHomePath(const char* sCall) binPath += L"bin"; SetDllDirectoryW(binPath.c_str()); - // https://stackoverflow.com/questions/5625884/conversion-of-stdwstring-to-qstring-throws-linker-error + // https://stackoverflow.com/questions/5625884/conversion-of-stdwstring-to-qstring-throws-linker-error #ifdef _MSC_VER QString str = QString::fromUtf16(reinterpret_cast(homePath.c_str())); #else diff --git a/src/App/Application.h b/src/App/Application.h index ba4d941643..1605a48ce4 100644 --- a/src/App/Application.h +++ b/src/App/Application.h @@ -300,6 +300,8 @@ public: //@{ /// Register an import filetype and a module name void addImportType(const char* Type, const char* ModuleName); + /// Change the module name of a registered filetype + void changeImportModule(const char* Type, const char* OldModuleName, const char* NewModuleName); /// Return a list of modules that support the given filetype. std::vector getImportModules(const char* Type) const; /// Return a list of all modules. @@ -316,6 +318,8 @@ public: //@{ /// Register an export filetype and a module name void addExportType(const char* Type, const char* ModuleName); + /// Change the module name of a registered filetype + void changeExportModule(const char* Type, const char* OldModuleName, const char* NewModuleName); /// Return a list of modules that support the given filetype. std::vector getExportModules(const char* Type) const; /// Return a list of all modules. @@ -458,8 +462,10 @@ private: static PyObject* sSetConfig (PyObject *self,PyObject *args); static PyObject* sDumpConfig (PyObject *self,PyObject *args); static PyObject* sAddImportType (PyObject *self,PyObject *args); + static PyObject* sChangeImportModule(PyObject *self,PyObject *args); static PyObject* sGetImportType (PyObject *self,PyObject *args); static PyObject* sAddExportType (PyObject *self,PyObject *args); + static PyObject* sChangeExportModule(PyObject *self,PyObject *args); static PyObject* sGetExportType (PyObject *self,PyObject *args); static PyObject* sGetResourceDir (PyObject *self,PyObject *args); static PyObject* sGetUserAppDataDir (PyObject *self,PyObject *args); diff --git a/src/App/ApplicationPy.cpp b/src/App/ApplicationPy.cpp index 75e943824e..44687ac0ad 100644 --- a/src/App/ApplicationPy.cpp +++ b/src/App/ApplicationPy.cpp @@ -72,6 +72,8 @@ PyMethodDef Application::Methods[] = { "Dump the configuration to the output."}, {"addImportType", (PyCFunction) Application::sAddImportType, METH_VARARGS, "Register filetype for import"}, + {"changeImportModule", (PyCFunction) Application::sChangeImportModule, METH_VARARGS, + "Change the import module name of a registered filetype"}, {"getImportType", (PyCFunction) Application::sGetImportType, METH_VARARGS, "Get the name of the module that can import the filetype"}, {"EndingAdd", (PyCFunction) Application::sAddImportType, METH_VARARGS, // deprecated @@ -80,6 +82,8 @@ PyMethodDef Application::Methods[] = { "deprecated -- use getImportType"}, {"addExportType", (PyCFunction) Application::sAddExportType, METH_VARARGS, "Register filetype for export"}, + {"changeExportModule", (PyCFunction) Application::sChangeExportModule, METH_VARARGS, + "Change the export module name of a registered filetype"}, {"getExportType", (PyCFunction) Application::sGetExportType, METH_VARARGS, "Get the name of the module that can export the filetype"}, {"getResourceDir", (PyCFunction) Application::sGetResourceDir, METH_VARARGS, @@ -526,6 +530,18 @@ PyObject* Application::sAddImportType(PyObject * /*self*/, PyObject *args) Py_Return; } +PyObject* Application::sChangeImportModule(PyObject * /*self*/, PyObject *args) +{ + char *key,*oldMod,*newMod; + + if (!PyArg_ParseTuple(args, "sss", &key,&oldMod,&newMod)) + return nullptr; + + GetApplication().changeImportModule(key,oldMod,newMod); + + Py_Return; +} + PyObject* Application::sGetImportType(PyObject * /*self*/, PyObject *args) { char* psKey=0; @@ -578,6 +594,18 @@ PyObject* Application::sAddExportType(PyObject * /*self*/, PyObject *args) Py_Return; } +PyObject* Application::sChangeExportModule(PyObject * /*self*/, PyObject *args) +{ + char *key,*oldMod,*newMod; + + if (!PyArg_ParseTuple(args, "sss", &key,&oldMod,&newMod)) + return nullptr; + + GetApplication().changeExportModule(key,oldMod,newMod); + + Py_Return; +} + PyObject* Application::sGetExportType(PyObject * /*self*/, PyObject *args) { char* psKey=0; diff --git a/src/Mod/Import/InitGui.py b/src/Mod/Import/InitGui.py index ee078669e3..d55f9c0d2c 100644 --- a/src/Mod/Import/InitGui.py +++ b/src/Mod/Import/InitGui.py @@ -29,6 +29,10 @@ #***************************************************************************/ +# Registered in Part's Init.py file +FreeCAD.changeImportModule("STEP with colors (*.step *.stp)","Import","ImportGui") +FreeCAD.changeExportModule("STEP with colors (*.step *.stp)","Import","ImportGui") + """ class ImportWorkbench ( Workbench ): "Import workbench object" diff --git a/src/Mod/Part/Init.py b/src/Mod/Part/Init.py index 23ecd7cb1b..a0ba689b53 100644 --- a/src/Mod/Part/Init.py +++ b/src/Mod/Part/Init.py @@ -32,7 +32,7 @@ FreeCAD.addImportType("BREP format (*.brep *.brp)","Part") FreeCAD.addExportType("BREP format (*.brep *.brp)","Part") FreeCAD.addImportType("IGES format (*.iges *.igs)","Part") FreeCAD.addExportType("IGES format (*.iges *.igs)","Part") -FreeCAD.addImportType("STEP with colors (*.step *.stp)","ImportGui") -FreeCAD.addExportType("STEP with colors (*.step *.stp)","ImportGui") +FreeCAD.addImportType("STEP with colors (*.step *.stp)","Import") +FreeCAD.addExportType("STEP with colors (*.step *.stp)","Import") FreeCAD.__unit_test__ += [ "TestPartApp" ]