From 0063b8093768825ba954ec84cc9ebd19f766b50a Mon Sep 17 00:00:00 2001 From: wmayer Date: Thu, 12 Mar 2015 12:45:37 +0100 Subject: [PATCH] + fixes #0002001: Crash inserting Raytracing Template when in Image View --- src/Mod/Raytracing/Gui/AppRaytracingGuiPy.cpp | 64 +++++++-- src/Mod/Raytracing/Gui/Command.cpp | 124 +++++++++++------- 2 files changed, 133 insertions(+), 55 deletions(-) diff --git a/src/Mod/Raytracing/Gui/AppRaytracingGuiPy.cpp b/src/Mod/Raytracing/Gui/AppRaytracingGuiPy.cpp index 91bb8b063..acf9f2422 100644 --- a/src/Mod/Raytracing/Gui/AppRaytracingGuiPy.cpp +++ b/src/Mod/Raytracing/Gui/AppRaytracingGuiPy.cpp @@ -35,10 +35,10 @@ #include #include #include +#include #include #include #include -#include #include #include @@ -96,16 +96,39 @@ povViewCamera(PyObject *self, PyObject *args) std::string out; const char* ppReturn=0; - Gui::Application::Instance->sendMsgToActiveView("GetCamera",&ppReturn); + Gui::Document* doc = Gui::Application::Instance->activeDocument(); + if (doc) { + // try active view first + Gui::MDIView* view = doc->getActiveView(); + if (!(view && view->onMsg("GetCamera",&ppReturn))) { + // now try all views + std::list views = doc->getMDIViews(); + for (std::list::iterator it = views.begin(); it != views.end(); ++it) { + if ((*it)->onMsg("GetCamera",&ppReturn)) { + break; + } + } + } + } + else { + PyErr_SetString(PyExc_RuntimeError, "No active document found"); + return 0; + } + + if (!ppReturn) { + PyErr_SetString(PyExc_RuntimeError, "Could not read camera information from active view"); + return 0; + } SoNode* rootNode; SoInput in; in.setBuffer((void*)ppReturn,std::strlen(ppReturn)); SoDB::read(&in,rootNode); - if (!rootNode || !rootNode->getTypeId().isDerivedFrom(SoCamera::getClassTypeId())) - throw Base::Exception("CmdRaytracingWriteCamera::activated(): Could not read " - "camera information from ASCII stream....\n"); + if (!rootNode || !rootNode->getTypeId().isDerivedFrom(SoCamera::getClassTypeId())) { + PyErr_SetString(PyExc_RuntimeError, "Could not read camera information from ASCII stream"); + return 0; + } // root-node returned from SoDB::readAll() has initial zero // ref-count, so reference it before we start using it to @@ -155,16 +178,39 @@ luxViewCamera(PyObject *self, PyObject *args) std::string out; const char* ppReturn=0; - Gui::Application::Instance->sendMsgToActiveView("GetCamera",&ppReturn); + Gui::Document* doc = Gui::Application::Instance->activeDocument(); + if (doc) { + // try active view first + Gui::MDIView* view = doc->getActiveView(); + if (!(view && view->onMsg("GetCamera",&ppReturn))) { + // now try all views + std::list views = doc->getMDIViews(); + for (std::list::iterator it = views.begin(); it != views.end(); ++it) { + if ((*it)->onMsg("GetCamera",&ppReturn)) { + break; + } + } + } + } + else { + PyErr_SetString(PyExc_RuntimeError, "No active document found"); + return 0; + } + + if (!ppReturn) { + PyErr_SetString(PyExc_RuntimeError, "Could not read camera information from active view"); + return 0; + } SoNode* rootNode; SoInput in; in.setBuffer((void*)ppReturn,std::strlen(ppReturn)); SoDB::read(&in,rootNode); - if (!rootNode || !rootNode->getTypeId().isDerivedFrom(SoCamera::getClassTypeId())) - throw Base::Exception("CmdRaytracingWriteCamera::activated(): Could not read " - "camera information from ASCII stream....\n"); + if (!rootNode || !rootNode->getTypeId().isDerivedFrom(SoCamera::getClassTypeId())) { + PyErr_SetString(PyExc_RuntimeError, "Could not read camera information from ASCII stream"); + return 0; + } // root-node returned from SoDB::readAll() has initial zero // ref-count, so reference it before we start using it to diff --git a/src/Mod/Raytracing/Gui/Command.cpp b/src/Mod/Raytracing/Gui/Command.cpp index 94b0dbb55..e3472a78e 100644 --- a/src/Mod/Raytracing/Gui/Command.cpp +++ b/src/Mod/Raytracing/Gui/Command.cpp @@ -261,26 +261,32 @@ void CmdRaytracingWriteView::activated(int iMsg) openCommand("Write view"); doCommand(Doc,"import Raytracing,RaytracingGui"); doCommand(Doc,"OutFile = open(unicode(\"%s\",\"utf-8\"),\"w\")",cFullName.c_str()); - doCommand(Doc,"result = open(App.getResourceDir()+'Mod/Raytracing/Templates/ProjectStd.pov').read()"); - doCommand(Doc,"content = ''"); - doCommand(Doc,"content += RaytracingGui.povViewCamera()"); - // go through all document objects - for (std::vector::const_iterator it=DocObjects.begin();it!=DocObjects.end();++it) { - Gui::ViewProvider* vp = getActiveGuiDocument()->getViewProvider(*it); - if (vp && vp->isVisible()) { - App::PropertyColor *pcColor = dynamic_cast(vp->getPropertyByName("ShapeColor")); - App::Color col = pcColor->getValue(); - doCommand(Doc,"content += Raytracing.getPartAsPovray('%s',App.activeDocument().%s.Shape,%f,%f,%f)", - (*it)->getNameInDocument(),(*it)->getNameInDocument(),col.r,col.g,col.b); + try { + doCommand(Doc,"result = open(App.getResourceDir()+'Mod/Raytracing/Templates/ProjectStd.pov').read()"); + doCommand(Doc,"content = ''"); + doCommand(Doc,"content += RaytracingGui.povViewCamera()"); + // go through all document objects + for (std::vector::const_iterator it=DocObjects.begin();it!=DocObjects.end();++it) { + Gui::ViewProvider* vp = getActiveGuiDocument()->getViewProvider(*it); + if (vp && vp->isVisible()) { + App::PropertyColor *pcColor = dynamic_cast(vp->getPropertyByName("ShapeColor")); + App::Color col = pcColor->getValue(); + doCommand(Doc,"content += Raytracing.getPartAsPovray('%s',App.activeDocument().%s.Shape,%f,%f,%f)", + (*it)->getNameInDocument(),(*it)->getNameInDocument(),col.r,col.g,col.b); + } } + doCommand(Doc,"result = result.replace('//RaytracingContent',content)"); + doCommand(Doc,"OutFile.write(result)"); + doCommand(Doc,"OutFile.close()"); + doCommand(Doc,"del OutFile"); + commitCommand(); + } + catch (...) { + doCommand(Doc,"OutFile.close()"); + doCommand(Doc,"del OutFile"); + abortCommand(); + throw; } - doCommand(Doc,"result = result.replace('//RaytracingContent',content)"); - doCommand(Doc,"OutFile.write(result)"); - doCommand(Doc,"OutFile.close()"); - doCommand(Doc,"del OutFile"); - - updateActive(); - commitCommand(); } bool CmdRaytracingWriteView::isActive(void) @@ -337,12 +343,18 @@ void CmdRaytracingNewPovrayProject::activated(int iMsg) QAction* a = pcAction->actions()[iMsg]; QFileInfo tfi(a->property("Template").toString()); if (tfi.isReadable()) { - openCommand("Create POV-Ray project"); - doCommand(Doc,"import Raytracing,RaytracingGui"); - doCommand(Doc,"App.activeDocument().addObject('Raytracing::RayProject','%s')",FeatName.c_str()); - doCommand(Doc,"App.activeDocument().%s.Template = '%s'",FeatName.c_str(), (const char*)tfi.filePath().toUtf8()); - doCommand(Doc,"App.activeDocument().%s.Camera = RaytracingGui.povViewCamera()",FeatName.c_str()); - commitCommand(); + try { + openCommand("Create POV-Ray project"); + doCommand(Doc,"import Raytracing,RaytracingGui"); + doCommand(Doc,"App.activeDocument().addObject('Raytracing::RayProject','%s')",FeatName.c_str()); + doCommand(Doc,"App.activeDocument().%s.Template = '%s'",FeatName.c_str(), (const char*)tfi.filePath().toUtf8()); + doCommand(Doc,"App.activeDocument().%s.Camera = RaytracingGui.povViewCamera()",FeatName.c_str()); + commitCommand(); + } + catch (...) { + abortCommand(); + throw; + } } else { QMessageBox::critical(Gui::getMainWindow(), @@ -726,12 +738,18 @@ void CmdRaytracingNewLuxProject::activated(int iMsg) QAction* a = pcAction->actions()[iMsg]; QFileInfo tfi(a->property("Template").toString()); if (tfi.isReadable()) { - openCommand("Create LuxRender project"); - doCommand(Doc,"import Raytracing,RaytracingGui"); - doCommand(Doc,"App.activeDocument().addObject('Raytracing::LuxProject','%s')",FeatName.c_str()); - doCommand(Doc,"App.activeDocument().%s.Template = '%s'",FeatName.c_str(), (const char*)tfi.filePath().toUtf8()); - doCommand(Doc,"App.activeDocument().%s.Camera = RaytracingGui.luxViewCamera()",FeatName.c_str()); - commitCommand(); + try { + openCommand("Create LuxRender project"); + doCommand(Doc,"import Raytracing,RaytracingGui"); + doCommand(Doc,"App.activeDocument().addObject('Raytracing::LuxProject','%s')",FeatName.c_str()); + doCommand(Doc,"App.activeDocument().%s.Template = '%s'",FeatName.c_str(), (const char*)tfi.filePath().toUtf8()); + doCommand(Doc,"App.activeDocument().%s.Camera = RaytracingGui.luxViewCamera()",FeatName.c_str()); + commitCommand(); + } + catch (...) { + abortCommand(); + throw; + } } else { QMessageBox::critical(Gui::getMainWindow(), @@ -803,29 +821,43 @@ CmdRaytracingResetCamera::CmdRaytracingResetCamera() void CmdRaytracingResetCamera::activated(int iMsg) { - std::vector Sel = getSelection().getSelection(); - unsigned int n = getSelection().countObjectsOfType(Raytracing::RayProject::getClassTypeId()); - if (n != 1) { - n = getSelection().countObjectsOfType(Raytracing::LuxProject::getClassTypeId()); - if (n != 1) { + std::vector sel = getSelection().getObjectsOfType(Raytracing::RayProject::getClassTypeId()); + if (sel.size() != 1) { + sel = getSelection().getObjectsOfType(Raytracing::LuxProject::getClassTypeId()); + if (sel.size() != 1) { QMessageBox::warning(Gui::getMainWindow(), QObject::tr("Wrong selection"), QObject::tr("Select one Raytracing project object.")); return; - } else { - //luxrender + } + } + + if (sel.front()->getTypeId().isDerivedFrom(Raytracing::RayProject::getClassTypeId())) { + //povray + try { openCommand("Reset Raytracing Camera"); doCommand(Doc,"import RaytracingGui"); - doCommand(Doc,"App.activeDocument().%s.Camera = RaytracingGui.luxViewCamera()",Sel[0].FeatName); + doCommand(Doc,"App.activeDocument().%s.Camera = RaytracingGui.povViewCamera()",sel.front()->getNameInDocument()); commitCommand(); - doCommand(Doc,"App.activeDocument().recompute()"); + updateActive(); + } + catch (...) { + abortCommand(); + throw; + } + } + else if (sel.front()->getTypeId().isDerivedFrom(Raytracing::LuxProject::getClassTypeId())) { + //luxrender + try { + openCommand("Reset Raytracing Camera"); + doCommand(Doc,"import RaytracingGui"); + doCommand(Doc,"App.activeDocument().%s.Camera = RaytracingGui.luxViewCamera()",sel.front()->getNameInDocument()); + commitCommand(); + updateActive(); + } + catch (...) { + abortCommand(); + throw; } - } else { - //povray - openCommand("Reset Raytracing Camera"); - doCommand(Doc,"import RaytracingGui"); - doCommand(Doc,"App.activeDocument().%s.Camera = RaytracingGui.povViewCamera()",Sel[0].FeatName); - commitCommand(); - doCommand(Doc,"App.activeDocument().recompute()"); } }