From a2bca22fc9dbc3cbecc210bbf41d0c920f34f036 Mon Sep 17 00:00:00 2001 From: wandererfan Date: Wed, 13 Feb 2019 09:04:34 -0500 Subject: [PATCH] Fix #3800 Connect TD export to FC Gui Export - TD did not define export types for the general Export command in Gui. --- src/Mod/TechDraw/Gui/AppTechDrawGuiPy.cpp | 54 ++++++++++++++++++++++- src/Mod/TechDraw/Gui/MDIViewPage.cpp | 45 +++++++++++++++++++ src/Mod/TechDraw/Gui/MDIViewPage.h | 6 +++ src/Mod/TechDraw/InitGui.py | 3 ++ 4 files changed, 107 insertions(+), 1 deletion(-) diff --git a/src/Mod/TechDraw/Gui/AppTechDrawGuiPy.cpp b/src/Mod/TechDraw/Gui/AppTechDrawGuiPy.cpp index 3ae34696f670..48c9ff64ff62 100644 --- a/src/Mod/TechDraw/Gui/AppTechDrawGuiPy.cpp +++ b/src/Mod/TechDraw/Gui/AppTechDrawGuiPy.cpp @@ -41,10 +41,12 @@ #include #include #include +#include #include #include +#include #include "MDIViewPage.h" #include "ViewProviderPage.h" @@ -60,7 +62,10 @@ class Module : public Py::ExtensionModule public: Module() : Py::ExtensionModule("TechDrawGui") { - add_varargs_method("exportPageAsPdf",&Module::exportPageAsPdf, + add_varargs_method("export",&Module::exporter, + "TechDraw hook for FC Gui exporter." + ); + add_varargs_method("exportPageAsPdf",&Module::exportPageAsPdf, "exportPageAsPdf(DrawPageObject,FilePath) -- print page as Pdf to file." ); add_varargs_method("exportPageAsSvg",&Module::exportPageAsSvg, @@ -104,6 +109,53 @@ class Module : public Py::ExtensionModule } } +//! hook for FC Gui export function + Py::Object exporter(const Py::Tuple& args) + { + PyObject* object; + char* Name; + if (!PyArg_ParseTuple(args.ptr(), "Oet",&object,"utf-8",&Name)) + throw Py::Exception(); + + std::string EncodedName = std::string(Name); + PyMem_Free(Name); + + TechDraw::DrawPage* page = nullptr; + Py::Sequence list(object); + for (Py::Sequence::iterator it = list.begin(); it != list.end(); ++it) { + PyObject* item = (*it).ptr(); + if (PyObject_TypeCheck(item, &(App::DocumentObjectPy::Type))) { + App::DocumentObject* obj = static_cast(item)->getDocumentObjectPtr(); + if (obj->getTypeId().isDerivedFrom(TechDraw::DrawPage::getClassTypeId())) { + page = static_cast(obj); + Gui::Document* activeGui = Gui::Application::Instance->getDocument(page->getDocument()); + Gui::ViewProvider* vp = activeGui->getViewProvider(obj); + ViewProviderPage* dvp = dynamic_cast(vp); + if ( !(dvp && dvp->getMDIViewPage()) ) { + throw Py::TypeError("TechDraw can not find Page"); + } + + Base::FileInfo fi_out(EncodedName.c_str()); + + if (fi_out.hasExtension("svg")) { + dvp->getMDIViewPage()->saveSVG(EncodedName); + } else if (fi_out.hasExtension("dxf")) { + dvp->getMDIViewPage()->saveDXF(EncodedName); + } else if (fi_out.hasExtension("pdf")) { + dvp->getMDIViewPage()->savePDF(EncodedName); + } else { + throw Py::TypeError("TechDraw can not export this file format"); + } + } + else { + throw Py::TypeError("Export of this object type is not supported by TechDraw module"); + } + } + } + + return Py::None(); + } + //!exportPageAsPdf(PageObject,FullPath) Py::Object exportPageAsPdf(const Py::Tuple& args) { diff --git a/src/Mod/TechDraw/Gui/MDIViewPage.cpp b/src/Mod/TechDraw/Gui/MDIViewPage.cpp index d4d3aa6a6ab5..0f4b08d8e59c 100644 --- a/src/Mod/TechDraw/Gui/MDIViewPage.cpp +++ b/src/Mod/TechDraw/Gui/MDIViewPage.cpp @@ -118,6 +118,12 @@ MDIViewPage::MDIViewPage(ViewProviderPage *pageVp, Gui::Document* doc, QWidget* m_exportSVGAction = new QAction(tr("&Export SVG"), this); connect(m_exportSVGAction, SIGNAL(triggered()), this, SLOT(saveSVG())); + m_exportDXFAction = new QAction(tr("Export DXF"), this); + connect(m_exportDXFAction, SIGNAL(triggered()), this, SLOT(saveDXF())); + + m_exportPDFAction = new QAction(tr("Export PDF"), this); + connect(m_exportPDFAction, SIGNAL(triggered()), this, SLOT(savePDF())); + isSelectionBlocked = false; QString tabText = QString::fromUtf8(pageVp->getDrawPage()->getNameInDocument()); @@ -763,6 +769,8 @@ void MDIViewPage::contextMenuEvent(QContextMenuEvent *event) menu.addAction(m_toggleFrameAction); menu.addAction(m_toggleKeepUpdatedAction); menu.addAction(m_exportSVGAction); + menu.addAction(m_exportDXFAction); + menu.addAction(m_exportPDFAction); menu.exec(event->globalPos()); } @@ -809,6 +817,43 @@ void MDIViewPage::saveSVG(std::string file) m_view->saveSvg(filename); } +void MDIViewPage::saveDXF() +{ +// TechDraw::DrawPage* page = m_vpPage->getDrawPage(); + QString defaultDir; + QString fileName = Gui::FileDialog::getSaveFileName(Gui::getMainWindow(), + QString::fromUtf8(QT_TR_NOOP("Save Dxf File ")), + defaultDir, + QString::fromUtf8(QT_TR_NOOP("Dxf (*.dxf)"))); + if (fileName.isEmpty()) { + return; + } + + std::string sFileName = fileName.toUtf8().constData(); + saveDXF(sFileName); +} + +void MDIViewPage::saveDXF(std::string fileName) +{ + TechDraw::DrawPage* page = m_vpPage->getDrawPage(); + std::string PageName = page->getNameInDocument(); + Gui::Command::openCommand("Save page to dxf"); + Gui::Command::doCommand(Gui::Command::Doc,"import TechDraw"); + Gui::Command::doCommand(Gui::Command::Doc,"TechDraw.writeDXFPage(App.activeDocument().%s,u\"%s\")", + PageName.c_str(),(const char*)fileName.c_str()); + Gui::Command::updateActive(); + Gui::Command::commitCommand(); +} + +void MDIViewPage::savePDF() +{ + printPdf(); +} + +void MDIViewPage::savePDF(std::string file) +{ + printPdf(file); +} /////////////// Selection Routines /////////////////// // wf: this is never executed??? diff --git a/src/Mod/TechDraw/Gui/MDIViewPage.h b/src/Mod/TechDraw/Gui/MDIViewPage.h index 2f1d14f89b55..f8705b75d4f2 100644 --- a/src/Mod/TechDraw/Gui/MDIViewPage.h +++ b/src/Mod/TechDraw/Gui/MDIViewPage.h @@ -80,6 +80,8 @@ class TechDrawGuiExport MDIViewPage : public Gui::MDIView, public Gui::Selection void printPreview(); void saveSVG(std::string file); + void saveDXF(std::string file); + void savePDF(std::string file); void setFrameState(bool state); bool getFrameState(void) {return m_frameState;}; @@ -104,6 +106,8 @@ class TechDrawGuiExport MDIViewPage : public Gui::MDIView, public Gui::Selection public Q_SLOTS: void viewAll(); void saveSVG(void); + void saveDXF(void); + void savePDF(void); void toggleFrame(void); void toggleKeepUpdated(void); // void testAction(void); @@ -137,6 +141,8 @@ public Q_SLOTS: QAction *m_toggleFrameAction; QAction *m_toggleKeepUpdatedAction; QAction *m_exportSVGAction; + QAction *m_exportDXFAction; + QAction *m_exportPDFAction; // QAction* m_testAction; std::string m_objectName; diff --git a/src/Mod/TechDraw/InitGui.py b/src/Mod/TechDraw/InitGui.py index 38d7eddd1dee..e4f7a32c5244 100644 --- a/src/Mod/TechDraw/InitGui.py +++ b/src/Mod/TechDraw/InitGui.py @@ -45,3 +45,6 @@ def GetClassName(self): return "TechDrawGui::Workbench" Gui.addWorkbench(TechDrawWorkbench()) + +# Append the export handler +FreeCAD.addExportType("Technical Drawing (*.svg *.dxf *.pdf)","TechDrawGui")