diff --git a/src/Gui/CommandT.h b/src/Gui/CommandT.h index 08dfe53647..a618c05c85 100644 --- a/src/Gui/CommandT.h +++ b/src/Gui/CommandT.h @@ -100,6 +100,33 @@ void _cmdDocument(Gui::Command::DoCmd_Type cmdType, const App::Document* doc, co } } +/** Runs a command for accessing document attribute or method + * This function is an alternative to _FCMD_DOC_CMD + * @param doc: document name + * @param mod: module name, "Gui" or "App" + * @param cmd: command string, streamable + * + * Example: + * @code{.cpp} + * _cmdDocument(Gui::Command::Gui, doc, "Gui", std::stringstream() << "getObject('" << objName << "')"); + * @endcode + * + * Translates to command (assuming doc's name is 'DocName', and + * and objName contains value 'ObjName'): + * @code{.py} + * Gui.getDocument('DocName').getObject('ObjName') + * @endcode + */ +template +void _cmdDocument(Gui::Command::DoCmd_Type cmdType, const std::string& doc, const std::string& mod, T&& cmd) { + if (!doc.empty()) { + std::stringstream str; + str << mod << ".getDocument('" << doc << "')." + << FormatString::str(cmd); + Gui::Command::runCommand(cmdType, str.str().c_str()); + } +} + /** Runs a command for accessing App.Document attribute or method * This function is an alternative to FCMD_DOC_CMD * @@ -123,6 +150,29 @@ inline void cmdAppDocument(const App::Document* doc, T&& cmd) { _cmdDocument(Gui::Command::Doc, doc, "App", std::forward(cmd)); } +/** Runs a command for accessing App.Document attribute or method + * This function is an alternative to FCMD_DOC_CMD + * + * @param doc: document name + * @param cmd: command string, streamable + * @sa _cmdDocument() + * + * Example: + * @code{.cpp} + * cmdAppDocument(doc, std::stringstream() << "getObject('" << objName << "')"); + * @endcode + * + * Translates to command (assuming doc's name is 'DocName', and + * and objName contains value 'ObjName'): + * @code{.py} + * App.getDocument('DocName').getObject('ObjName') + * @endcode + */ +template +inline void cmdAppDocument(const std::string& doc, T&& cmd) { + _cmdDocument(Gui::Command::Doc, doc, "App", std::forward(cmd)); +} + /** Runs a command for accessing App.Document attribute or method * * @param doc: pointer to a document @@ -145,6 +195,28 @@ inline void cmdGuiDocument(const App::Document* doc, T&& cmd) { _cmdDocument(Gui::Command::Gui, doc, "Gui", std::forward(cmd)); } +/** Runs a command for accessing App.Document attribute or method + * + * @param doc: document name + * @param cmd: command string, streamable + * @sa _cmdDocument() + * + * Example: + * @code{.cpp} + * cmdGuiDocument(doc, std::stringstream() << "getObject('" << objName << "')"); + * @endcode + * + * Translates to command (assuming doc's name is 'DocName', and + * and objName contains value 'ObjName'): + * @code{.py} + * Gui.getDocument('DocName').getObject('ObjName') + * @endcode + */ +template +inline void cmdGuiDocument(const std::string& doc, T&& cmd) { + _cmdDocument(Gui::Command::Gui, doc, "Gui", std::forward(cmd)); +} + /** Runs a command for accessing an object's document attribute or method * This function is an alternative to _FCMD_OBJ_DOC_CMD * @param obj: pointer to a DocumentObject