Skip to content

Commit

Permalink
Fix #3800 Connect TD export to FC Gui Export
Browse files Browse the repository at this point in the history
- TD did not define export types for the general Export
  command in Gui.
  • Loading branch information
WandererFan authored and wwmayer committed Feb 14, 2019
1 parent 4ced49d commit a2bca22
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 1 deletion.
54 changes: 53 additions & 1 deletion src/Mod/TechDraw/Gui/AppTechDrawGuiPy.cpp
Expand Up @@ -41,10 +41,12 @@
#include <App/DocumentObject.h>
#include <App/DocumentObjectPy.h>
#include <Gui/Application.h>
#include <Gui/Document.h>
#include <Gui/ViewProvider.h>


#include <Mod/Part/App/OCCError.h>
#include <Mod/TechDraw/App/DrawPage.h>

#include "MDIViewPage.h"
#include "ViewProviderPage.h"
Expand All @@ -60,7 +62,10 @@ class Module : public Py::ExtensionModule<Module>
public:
Module() : Py::ExtensionModule<Module>("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,
Expand Down Expand Up @@ -104,6 +109,53 @@ class Module : public Py::ExtensionModule<Module>
}
}

//! 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<App::DocumentObjectPy*>(item)->getDocumentObjectPtr();
if (obj->getTypeId().isDerivedFrom(TechDraw::DrawPage::getClassTypeId())) {
page = static_cast<TechDraw::DrawPage*>(obj);
Gui::Document* activeGui = Gui::Application::Instance->getDocument(page->getDocument());
Gui::ViewProvider* vp = activeGui->getViewProvider(obj);
ViewProviderPage* dvp = dynamic_cast<ViewProviderPage*>(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)
{
Expand Down
45 changes: 45 additions & 0 deletions src/Mod/TechDraw/Gui/MDIViewPage.cpp
Expand Up @@ -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());
Expand Down Expand Up @@ -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());
}

Expand Down Expand Up @@ -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???
Expand Down
6 changes: 6 additions & 0 deletions src/Mod/TechDraw/Gui/MDIViewPage.h
Expand Up @@ -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;};
Expand All @@ -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);
Expand Down Expand Up @@ -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;
Expand Down
3 changes: 3 additions & 0 deletions src/Mod/TechDraw/InitGui.py
Expand Up @@ -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")

0 comments on commit a2bca22

Please sign in to comment.