23
#include "PreCompiled.h"
25
#include <App/Document.h>
26
#include <App/DocumentObject.h>
27
#include <App/DocumentObjectPy.h>
28
#include <Base/Console.h>
29
#include <Base/Exception.h>
30
#include <Base/FileInfo.h>
31
#include <Gui/Application.h>
32
#include <Gui/Document.h>
33
#include <Gui/ViewProvider.h>
34
#include <Gui/PythonWrapper.h>
35
#include <Mod/Part/App/OCCError.h>
36
#include <Mod/TechDraw/App/DrawPage.h>
37
#include <Mod/TechDraw/App/DrawPagePy.h>
38
#include <Mod/TechDraw/App/DrawViewPy.h>
40
#include "MDIViewPage.h"
43
#include "ViewProviderPage.h"
44
#include "ViewProviderDrawingView.h"
45
#include "PagePrinter.h"
48
namespace TechDrawGui {
50
class Module : public Py::ExtensionModule<Module>
53
Module() : Py::ExtensionModule<Module>("TechDrawGui")
55
add_varargs_method("export", &Module::exporter,
56
"TechDraw hook for FC Gui exporter."
58
add_varargs_method("exportPageAsPdf", &Module::exportPageAsPdf,
59
"exportPageAsPdf(DrawPageObject, FilePath) -- print page as Pdf to file."
61
add_varargs_method("exportPageAsSvg", &Module::exportPageAsSvg,
62
"exportPageAsSvg(DrawPageObject, FilePath) -- print page as Svg to file."
64
add_varargs_method("addQGIToView", &Module::addQGIToView,
65
"addQGIToView(View, QGraphicsItem) -- insert graphics item into view's graphic."
67
add_varargs_method("addQGObjToView", &Module::addQGObjToView,
68
"addQGObjToView(View, QGraphicsObject) -- insert graphics object into view's graphic. Use for QGraphicsItems that have QGraphicsObject as base class."
70
add_varargs_method("addQGIToScene", &Module::addQGIToScene,
71
"addQGIToScene(Page, QGraphicsItem) -- insert graphics item into Page's scene."
73
add_varargs_method("addQGObjToScene", &Module::addQGObjToScene,
74
"addQGObjToScene(Page, QGraphicsObject) -- insert graphics object into Page's scene. Use for QGraphicsItems that have QGraphicsObject as base class."
76
add_varargs_method("getSceneForPage", &Module::getSceneForPage,
77
"QGSPage = getSceneForPage(page) -- get the scene for a DrawPage."
79
initialize("This is a module for displaying drawings");
84
Py::Object invoke_method_varargs(void *method_def, const Py::Tuple &args) override
87
return Py::ExtensionModule<Module>::invoke_method_varargs(method_def, args);
89
catch (const Standard_Failure &e) {
91
Standard_CString msg = e.GetMessageString();
92
str += typeid(e).name();
94
if (msg) {str += msg;}
95
else {str += "No OCCT Exception Message";}
96
Base::Console().Error("%s\n", str.c_str());
97
throw Py::Exception(Part::PartExceptionOCCError, str);
99
catch (const Base::Exception &e) {
101
str += "FreeCAD exception thrown (";
105
throw Py::RuntimeError(str);
107
catch (const std::exception &e) {
109
str += "C++ exception thrown (";
112
Base::Console().Error("%s\n", str.c_str());
113
throw Py::RuntimeError(str);
119
Py::Object exporter(const Py::Tuple& args)
123
if (!PyArg_ParseTuple(args.ptr(), "Oet", &object, "utf-8", &Name))
124
throw Py::Exception();
126
std::string EncodedName = std::string(Name);
129
TechDraw::DrawPage* page = nullptr;
130
Py::Sequence list(object);
131
for (Py::Sequence::iterator it = list.begin(); it != list.end(); ++it) {
132
PyObject* item = (*it).ptr();
133
if (PyObject_TypeCheck(item, &(App::DocumentObjectPy::Type))) {
134
App::DocumentObject* obj = static_cast<App::DocumentObjectPy*>(item)->getDocumentObjectPtr();
135
if (obj->isDerivedFrom<TechDraw::DrawPage>()) {
136
page = static_cast<TechDraw::DrawPage*>(obj);
137
Gui::Document* activeGui = Gui::Application::Instance->getDocument(page->getDocument());
138
Gui::ViewProvider* vp = activeGui->getViewProvider(obj);
139
ViewProviderPage* dvp = dynamic_cast<ViewProviderPage*>(vp);
140
if ( !(dvp && dvp->getMDIViewPage()) ) {
141
throw Py::TypeError("TechDraw can not find Page");
144
Base::FileInfo fi_out(EncodedName.c_str());
146
if (fi_out.hasExtension("svg")) {
147
dvp->getMDIViewPage()->saveSVG(EncodedName);
148
} else if (fi_out.hasExtension("dxf")) {
149
dvp->getMDIViewPage()->saveDXF(EncodedName);
150
} else if (fi_out.hasExtension("pdf")) {
151
dvp->getMDIViewPage()->savePDF(EncodedName);
153
throw Py::TypeError("TechDraw can not export this file format");
157
throw Py::TypeError("No Technical Drawing Page found in selection.");
166
Py::Object exportPageAsPdf(const Py::Tuple& args)
170
if (!PyArg_ParseTuple(args.ptr(), "Oet", &pageObj, "utf-8", &name)) {
171
throw Py::TypeError("expected (Page, path");
174
std::string filePath = std::string(name);
178
App::DocumentObject* obj = nullptr;
179
Gui::ViewProvider* vp = nullptr;
180
MDIViewPage* mdi = nullptr;
181
if (PyObject_TypeCheck(pageObj, &(App::DocumentObjectPy::Type))) {
182
obj = static_cast<App::DocumentObjectPy*>(pageObj)->getDocumentObjectPtr();
183
vp = Gui::Application::Instance->getViewProvider(obj);
185
TechDrawGui::ViewProviderPage* vpp = dynamic_cast<TechDrawGui::ViewProviderPage*>(vp);
187
mdi = vpp->getMDIViewPage();
189
mdi->savePDF(filePath);
191
vpp->showMDIViewPage();
192
mdi = vpp->getMDIViewPage();
194
mdi->savePDF(filePath);
196
throw Py::TypeError("Page not available! Is it Hidden?");
203
catch (Base::Exception &e) {
205
throw Py::Exception();
212
Py::Object exportPageAsSvg(const Py::Tuple& args)
216
if (!PyArg_ParseTuple(args.ptr(), "Oet", &pageObj, "utf-8", &name)) {
217
throw Py::TypeError("expected (Page, path");
220
std::string filePath = std::string(name);
224
App::DocumentObject* obj = nullptr;
225
Gui::ViewProvider* vp = nullptr;
226
MDIViewPage* mdi = nullptr;
227
if (PyObject_TypeCheck(pageObj, &(App::DocumentObjectPy::Type))) {
228
obj = static_cast<App::DocumentObjectPy*>(pageObj)->getDocumentObjectPtr();
229
vp = Gui::Application::Instance->getViewProvider(obj);
231
TechDrawGui::ViewProviderPage* vpp = dynamic_cast<TechDrawGui::ViewProviderPage*>(vp);
233
mdi = vpp->getMDIViewPage();
235
mdi->saveSVG(filePath);
237
vpp->showMDIViewPage();
238
mdi = vpp->getMDIViewPage();
240
mdi->saveSVG(filePath);
242
throw Py::TypeError("Page not available! Is it Hidden?");
249
catch (Base::Exception &e) {
251
throw Py::Exception();
257
Py::Object addQGIToView(const Py::Tuple& args)
259
PyObject *viewPy = nullptr;
260
PyObject *qgiPy = nullptr;
261
if (!PyArg_ParseTuple(args.ptr(), "O!O", &(TechDraw::DrawViewPy::Type), &viewPy, &qgiPy)) {
262
throw Py::TypeError("expected (view, item)");
266
App::DocumentObject* obj = nullptr;
267
Gui::ViewProvider* vp = nullptr;
268
QGIView* qgiv = nullptr;
269
obj = static_cast<App::DocumentObjectPy*>(viewPy)->getDocumentObjectPtr();
270
vp = Gui::Application::Instance->getViewProvider(obj);
272
TechDrawGui::ViewProviderDrawingView* vpdv =
273
dynamic_cast<TechDrawGui::ViewProviderDrawingView*>(vp);
275
qgiv = vpdv->getQView();
277
Gui::PythonWrapper wrap;
278
if (!wrap.loadGuiModule()) {
279
throw Py::RuntimeError("Failed to load Python wrapper for Qt::Gui");
281
QGraphicsItem* item = wrap.toQGraphicsItem(args[1]);
283
qgiv->addArbitraryItem(item);
289
catch (Base::Exception &e) {
291
throw Py::Exception();
297
Py::Object addQGObjToView(const Py::Tuple& args)
299
PyObject *viewPy = nullptr;
300
PyObject *qgiPy = nullptr;
301
if (!PyArg_ParseTuple(args.ptr(), "O!O", &(TechDraw::DrawViewPy::Type), &viewPy, &qgiPy)) {
302
throw Py::TypeError("expected (view, item)");
306
App::DocumentObject* obj = nullptr;
307
Gui::ViewProvider* vp = nullptr;
308
QGIView* qgiv = nullptr;
309
obj = static_cast<App::DocumentObjectPy*>(viewPy)->getDocumentObjectPtr();
310
vp = Gui::Application::Instance->getViewProvider(obj);
312
TechDrawGui::ViewProviderDrawingView* vpdv =
313
dynamic_cast<TechDrawGui::ViewProviderDrawingView*>(vp);
315
qgiv = vpdv->getQView();
317
Gui::PythonWrapper wrap;
318
if (!wrap.loadGuiModule()) {
319
throw Py::RuntimeError("Failed to load Python wrapper for Qt::Gui");
321
QGraphicsObject* item = wrap.toQGraphicsObject(args[1]);
323
qgiv->addArbitraryItem(item);
329
catch (Base::Exception &e) {
331
throw Py::Exception();
339
Py::Object addQGIToScene(const Py::Tuple& args)
341
PyObject *pagePy = nullptr;
342
PyObject *qgiPy = nullptr;
343
if (!PyArg_ParseTuple(args.ptr(), "O!O", &(TechDraw::DrawPagePy::Type), &pagePy, &qgiPy)) {
344
throw Py::TypeError("expected (view, item)");
348
App::DocumentObject* obj = nullptr;
349
Gui::ViewProvider* vp = nullptr;
350
QGSPage* qgsp = nullptr;
351
obj = static_cast<App::DocumentObjectPy*>(pagePy)->getDocumentObjectPtr();
352
vp = Gui::Application::Instance->getViewProvider(obj);
354
TechDrawGui::ViewProviderPage* vpp =
355
dynamic_cast<TechDrawGui::ViewProviderPage*>(vp);
357
qgsp = vpp->getQGSPage();
359
Gui::PythonWrapper wrap;
360
if (!wrap.loadGuiModule()) {
361
throw Py::RuntimeError("Failed to load Python wrapper for Qt::Gui");
363
QGraphicsItem* item = wrap.toQGraphicsItem(args[1]);
371
catch (Base::Exception &e) {
373
throw Py::Exception();
383
Py::Object addQGObjToScene(const Py::Tuple& args)
385
PyObject *pagePy = nullptr;
386
PyObject *qgiPy = nullptr;
387
if (!PyArg_ParseTuple(args.ptr(), "O!O", &(TechDraw::DrawPagePy::Type), &pagePy, &qgiPy)) {
388
throw Py::TypeError("expected (view, item)");
392
App::DocumentObject* obj = nullptr;
393
Gui::ViewProvider* vp = nullptr;
394
QGSPage* qgsp = nullptr;
395
obj = static_cast<App::DocumentObjectPy*>(pagePy)->getDocumentObjectPtr();
396
vp = Gui::Application::Instance->getViewProvider(obj);
398
TechDrawGui::ViewProviderPage* vpp =
399
dynamic_cast<TechDrawGui::ViewProviderPage*>(vp);
401
qgsp = vpp->getQGSPage();
403
Gui::PythonWrapper wrap;
404
if (!wrap.loadGuiModule()) {
405
throw Py::RuntimeError("Failed to load Python wrapper for Qt::Gui");
407
QGraphicsObject* item = wrap.toQGraphicsObject(args[1]);
415
catch (Base::Exception &e) {
417
throw Py::Exception();
423
Py::Object getSceneForPage(const Py::Tuple& args)
425
PyObject *pagePy = nullptr;
426
if (!PyArg_ParseTuple(args.ptr(), "O!", &(TechDraw::DrawPagePy::Type), &pagePy)) {
427
throw Py::TypeError("expected (page)");
431
App::DocumentObject* obj = nullptr;
432
Gui::ViewProvider* vp = nullptr;
433
QGSPage* qgsp = nullptr;
434
obj = static_cast<App::DocumentObjectPy*>(pagePy)->getDocumentObjectPtr();
435
vp = Gui::Application::Instance->getViewProvider(obj);
437
TechDrawGui::ViewProviderPage* vpp =
438
dynamic_cast<TechDrawGui::ViewProviderPage*>(vp);
440
qgsp = vpp->getQGSPage();
442
Gui::PythonWrapper wrap;
443
if (!wrap.loadGuiModule()) {
444
throw Py::RuntimeError("Failed to load Python wrapper for Qt::Gui");
446
return wrap.fromQObject(qgsp, "TechDrawGui::QGSPage");
451
catch (Base::Exception &e) {
453
throw Py::Exception();
460
PyObject* initModule()
462
return Base::Interpreter().addModule(new Module);