From 76c0a81fe332775c564c44aab38505352c33a008 Mon Sep 17 00:00:00 2001 From: DeepSOIC Date: Fri, 13 May 2016 02:09:42 +0300 Subject: [PATCH] Attacher: Py: GUI resources interface Routines to get UI strings for attacher: mode names, mode tooltips, ref.type names --- src/Mod/Part/Gui/AppPartGui.cpp | 9 ++++- src/Mod/Part/Gui/AttacherTexts.cpp | 64 +++++++++++++++++++++++++++++- src/Mod/Part/Gui/AttacherTexts.h | 12 +++++- 3 files changed, 82 insertions(+), 3 deletions(-) diff --git a/src/Mod/Part/Gui/AppPartGui.cpp b/src/Mod/Part/Gui/AppPartGui.cpp index ec119b937ea7..c9913200c5f8 100644 --- a/src/Mod/Part/Gui/AppPartGui.cpp +++ b/src/Mod/Part/Gui/AppPartGui.cpp @@ -28,6 +28,7 @@ #include +#include "AttacherTexts.h" #include "SoBrepFaceSet.h" #include "SoBrepEdgeSet.h" #include "SoBrepPointSet.h" @@ -117,9 +118,15 @@ PyMODINIT_FUNC initPartGui() return; } - (void)PartGui::initModule(); + PyObject* partGuiModule = PartGui::initModule(); Base::Console().Log("Loading GUI of Part module... done\n"); + PyObject* pAttachEngineTextsModule = Py_InitModule3("AttachEngineResources", AttacherGui::AttacherGuiPy::Methods, + "AttachEngine Gui resources"); + Py_INCREF(pAttachEngineTextsModule); + PyModule_AddObject(partGuiModule, "AttachEngineResources", pAttachEngineTextsModule); + + PartGui::SoBrepFaceSet ::initClass(); PartGui::SoBrepEdgeSet ::initClass(); PartGui::SoBrepPointSet ::initClass(); diff --git a/src/Mod/Part/Gui/AttacherTexts.cpp b/src/Mod/Part/Gui/AttacherTexts.cpp index 742b74c484e7..8ffc1a5e3ffb 100644 --- a/src/Mod/Part/Gui/AttacherTexts.cpp +++ b/src/Mod/Part/Gui/AttacherTexts.cpp @@ -25,6 +25,8 @@ # include #endif #include "AttacherTexts.h" +#include +#include using namespace Attacher; @@ -262,7 +264,7 @@ TextSet getUIStrings(Base::Type attacherType, eMapMode mmode) } } - assert(false && "No user-friendly string defined for this attachment mode."); + Base::Console().Warning("No user-friendly string defined for this attachment mode and attacher type: %s %s \n",AttachEngine::getModeName(mmode).c_str(), attacherType.getName()); return TwoStrings(QString::fromStdString(AttachEngine::getModeName(mmode)), QString()); } @@ -325,4 +327,64 @@ QStringList getRefListForMode(AttachEngine &attacher, eMapMode mmode) return strlist; } + +// --------------------Py interface--------------------- + +PyObject* AttacherGuiPy::sGetModeStrings(PyObject * /*self*/, PyObject *args, PyObject * /*kwd*/) +{ + int modeIndex = 0; + char* attacherType; + if (!PyArg_ParseTuple(args, "si", &attacherType, &modeIndex)) + return NULL; + + try { + Base::Type t = Base::Type::fromName(attacherType); + if (! t.isDerivedFrom(AttachEngine::getClassTypeId())){ + std::stringstream ss; + ss << "Object of this type is not derived from AttachEngine: "; + ss << attacherType; + throw Py::TypeError(ss.str()); + } + TextSet strs = getUIStrings(t,eMapMode(modeIndex)); + Py::List result; + for(QString &s : strs){ + QByteArray ba_utf8 = s.toUtf8(); + result.append(Py::String(ba_utf8.data(), "utf-8")); + } + + return Py::new_reference_to(result); + } catch (const Py::Exception&) { + return 0; + } catch (const Base::Exception& e) { + PyErr_SetString(Base::BaseExceptionFreeCADError, e.what()); + return 0; + } +} + +PyObject* AttacherGuiPy::sGetRefTypeUserFriendlyName(PyObject * /*self*/, PyObject *args, PyObject * /*kwd*/) +{ + int refTypeIndex = 0; + if (!PyArg_ParseTuple(args, "i", &refTypeIndex)) + return NULL; + + try { + QByteArray ba_utf8 = getShapeTypeText(eRefType(refTypeIndex)).toUtf8(); + return Py::new_reference_to(Py::String(ba_utf8.data(), "utf-8")); + } catch (const Py::Exception&) { + return 0; + } catch (const Base::Exception& e) { + PyErr_SetString(Base::BaseExceptionFreeCADError, e.what()); + return 0; + } +} + + +PyMethodDef AttacherGuiPy::Methods[] = { + {"getModeStrings", (PyCFunction) AttacherGuiPy::sGetModeStrings, 1, + "getModeStrings(attacher_type, mode_index) - gets mode user-friendly name and brief description."}, + {"getRefTypeUserFriendlyName", (PyCFunction) AttacherGuiPy::sGetRefTypeUserFriendlyName, 1, + "getRefTypeUserFriendlyName(type_index) - gets user-friendly name of AttachEngine's shape type."}, +}; + + } //namespace AttacherGui diff --git a/src/Mod/Part/Gui/AttacherTexts.h b/src/Mod/Part/Gui/AttacherTexts.h index 91bdee8bd3ab..903f604b17b0 100644 --- a/src/Mod/Part/Gui/AttacherTexts.h +++ b/src/Mod/Part/Gui/AttacherTexts.h @@ -33,8 +33,10 @@ #include #include #include +#include #include + namespace AttacherGui { typedef std::vector TextSet; @@ -54,6 +56,14 @@ QString PartGuiExport getShapeTypeText(Attacher::eRefType type); QStringList PartGuiExport getRefListForMode(Attacher::AttachEngine &attacher, Attacher::eMapMode mmode); -} +// Python interface +class PartGuiExport AttacherGuiPy{ +public: + static PyMethodDef Methods[]; + static PyObject* sGetModeStrings(PyObject * /*self*/, PyObject *args, PyObject * /*kwd*/); + static PyObject* sGetRefTypeUserFriendlyName(PyObject * /*self*/, PyObject *args, PyObject * /*kwd*/); +}; + +} #endif