diff --git a/src/Gui/ApplicationPy.cpp b/src/Gui/ApplicationPy.cpp index d311b216d..de47034ad 100644 --- a/src/Gui/ApplicationPy.cpp +++ b/src/Gui/ApplicationPy.cpp @@ -901,14 +901,48 @@ PyObject* Application::sAddCommand(PyObject * /*self*/, PyObject *args,PyObject if (!PyArg_ParseTuple(args, "sO|s", &pName,&pcCmdObj,&pSource)) // convert args: Python->C return NULL; // NULL triggers exception + // get the call stack to find the Python module name + // + std::string module, group; try { Base::PyGILStateLocker lock; + Py::Module mod(PyImport_ImportModule("inspect"), true); + Py::Callable inspect(mod.getAttr("stack")); + Py::Tuple args; + Py::List list(inspect.apply(args)); + args = list.getItem(0); + // usually this is the file name of the calling script + module = args.getItem(1).as_string(); + Base::FileInfo fi(module); + module = fi.fileNamePure(); + group = module; + std::string::size_type len = group.size(); + if (len > 3 && group.substr(len-3) == "Gui") + group = group.substr(0,len-3); + } + catch (Py::Exception& e) { + e.clear(); + } + + try { + Base::PyGILStateLocker lock; + Py::Object cmd(pcCmdObj); if (cmd.hasAttr("GetCommands")) { - Application::Instance->commandManager().addCommand(new PythonGroupCommand(pName, pcCmdObj)); + Command* cmd = new PythonGroupCommand(pName, pcCmdObj); + if (!module.empty()) { + cmd->setAppModuleName(module.c_str()); + cmd->setGroupName(group.c_str()); + } + Application::Instance->commandManager().addCommand(cmd); } else { - Application::Instance->commandManager().addCommand(new PythonCommand(pName, pcCmdObj, pSource)); + Command* cmd = new PythonCommand(pName, pcCmdObj, pSource); + if (!module.empty()) { + cmd->setAppModuleName(module.c_str()); + cmd->setGroupName(group.c_str()); + } + Application::Instance->commandManager().addCommand(cmd); } } catch (const Base::Exception& e) { diff --git a/src/Gui/Command.cpp b/src/Gui/Command.cpp index 7d43d883a..a6c3b650e 100644 --- a/src/Gui/Command.cpp +++ b/src/Gui/Command.cpp @@ -367,6 +367,24 @@ std::string Command::getUniqueObjectName(const char *BaseName) const return getActiveGuiDocument()->getDocument()->getUniqueObjectName(BaseName); } +void Command::setAppModuleName(const char* s) +{ +#if defined (_MSC_VER) + this->sAppModule = _strdup(s); +#else + this->sAppModule = strdup(s); +#endif +} + +void Command::setGroupName(const char* s) +{ +#if defined (_MSC_VER) + this->sGroup = _strdup(s); +#else + this->sGroup = strdup(s); +#endif +} + //-------------------------------------------------------------------------- // UNDO REDO transaction handling diff --git a/src/Gui/Command.h b/src/Gui/Command.h index 8bd9a73c3..f9c20fa6d 100644 --- a/src/Gui/Command.h +++ b/src/Gui/Command.h @@ -281,10 +281,12 @@ public: //@{ /// returns the name to which the command belongs const char* getAppModuleName(void) const {return sAppModule;} + void setAppModuleName(const char*); /// Get the command name const char* getName() const { return sName; } /// Get the name of the grouping of the command const char* getGroupName() const { return sGroup; } + void setGroupName(const char*); //@} protected: