diff --git a/src/Mod/Raytracing/App/PovTools.cpp b/src/Mod/Raytracing/App/PovTools.cpp index 39e751f03..862d6c621 100644 --- a/src/Mod/Raytracing/App/PovTools.cpp +++ b/src/Mod/Raytracing/App/PovTools.cpp @@ -54,7 +54,7 @@ using namespace std; // location CamPos // look_at LookAt // sky Up -// angle 50 +// angle 45 //} std::string PovTools::getCamera(const CamDef& Cam) @@ -73,7 +73,7 @@ std::string PovTools::getCamera(const CamDef& Cam) << "#declare cam_sky = <" << Cam.Up.X() <<"," << Cam.Up.Z() <<"," << Cam.Up.Y() <<">;"<< endl // array of zoom factors - << "#declare cam_angle = 50; " << endl + << "#declare cam_angle = 45; " << endl // instance of the camera << "camera {" << endl << " location cam_location" << endl @@ -139,7 +139,7 @@ void PovTools::writeCameraVec(const char* FileName, const std::vector& C << "#declare CamZoom = array[" << CamVec.size() << "] {\n"; for (It = CamVec.begin(); It != CamVec.end(); It++) - out << " 50,\n"; + out << " 45,\n"; out << "};\n"; diff --git a/src/Mod/Raytracing/App/RayFeature.cpp b/src/Mod/Raytracing/App/RayFeature.cpp index 1975f62f7..b66eeb97b 100644 --- a/src/Mod/Raytracing/App/RayFeature.cpp +++ b/src/Mod/Raytracing/App/RayFeature.cpp @@ -46,6 +46,7 @@ PROPERTY_SOURCE(Raytracing::RayFeature, Raytracing::RaySegment) RayFeature::RayFeature(void) { ADD_PROPERTY(Source,(0)); + ADD_PROPERTY(Color,(App::Color(0.5f,0.5f,0.5f))); } App::DocumentObjectExecReturn *RayFeature::execute(void) @@ -65,6 +66,16 @@ App::DocumentObjectExecReturn *RayFeature::execute(void) PovTools::writeShape(result,Name.c_str(),shape); + // This must not be done in PovTools::writeShape! + const App::Color& c = Color.getValue(); + result << "// instance to render" << endl + << "object {" << Name << endl + << " texture {" << endl + << " pigment {color rgb <"<}" << endl + << " finish {StdFinish } //definition on top of the project" << endl + << " }" << endl + << "}" << endl ; + // Apply the resulting fragment Result.setValue(result.str().c_str()); diff --git a/src/Mod/Raytracing/App/RayFeature.h b/src/Mod/Raytracing/App/RayFeature.h index 187c2ff41..6dfc4855d 100644 --- a/src/Mod/Raytracing/App/RayFeature.h +++ b/src/Mod/Raytracing/App/RayFeature.h @@ -20,8 +20,6 @@ * * ***************************************************************************/ - - #ifndef _RayFeature_h_ #define _RayFeature_h_ @@ -29,6 +27,7 @@ #include #include #include +#include #include "RaySegment.h" @@ -45,27 +44,24 @@ class AppRaytracingExport RayFeature: public Raytracing::RaySegment { PROPERTY_HEADER(Raytracing::RayFeature); public: - /// Constructor - RayFeature(void); + /// Constructor + RayFeature(void); - App::PropertyLink Source; + App::PropertyLink Source; + App::PropertyColor Color; /** @name methods overide Feature */ //@{ /// recalculate the Feature App::DocumentObjectExecReturn *execute(void); - /// returns the type name of the ViewProvider + /// returns the type name of the ViewProvider const char* getViewProviderName(void) const { return "Gui::ViewProviderDocumentObject"; } //@} - }; - } //namespace Raytracing - - #endif //_RayFeature_h_ diff --git a/src/Mod/Raytracing/App/RayProject.cpp b/src/Mod/Raytracing/App/RayProject.cpp index 6273c38ab..66aa3229f 100644 --- a/src/Mod/Raytracing/App/RayProject.cpp +++ b/src/Mod/Raytracing/App/RayProject.cpp @@ -31,7 +31,7 @@ #include #include #include "RayProject.h" -#include "RaySegment.h" +#include "RayFeature.h" using namespace Raytracing; using namespace std; @@ -45,7 +45,8 @@ PROPERTY_SOURCE(Raytracing::RayProject, App::DocumentObjectGroup) RayProject::RayProject(void) { ADD_PROPERTY_TYPE(PageResult ,(0),0,App::Prop_Output,"Resulting povray Project file"); - ADD_PROPERTY_TYPE(Template ,(""),0,App::Prop_None ,"Template for the Povray project"); + ADD_PROPERTY_TYPE(Template ,(""),0,App::Prop_None ,"Template for the Povray project"); + ADD_PROPERTY_TYPE(Camera ,(""),0,App::Prop_None ,"Camera settings"); } App::DocumentObjectExecReturn *RayProject::execute(void) @@ -67,20 +68,21 @@ App::DocumentObjectExecReturn *RayProject::execute(void) string tempName = PageResult.getExchangeTempFile(); ofstream ofile(tempName.c_str()); + // copy the input of the resource file while (!file.eof()) { getline (file,line); - if (line != "") - ofile << line << endl; - else { - // get through the children and collect all the views - const std::vector &Grp = Group.getValues(); - for (std::vector::const_iterator It= Grp.begin();It!=Grp.end();++It) { - if ((*It)->getTypeId().isDerivedFrom(Raytracing::RaySegment::getClassTypeId())) { - Raytracing::RaySegment *View = dynamic_cast(*It); - ofile << View->Result.getValue(); - ofile << endl << endl << endl; - } - } + ofile << line << endl; + } + + ofile << Camera.getValue(); + + // get through the children and collect all the views + const std::vector &Grp = Group.getValues(); + for (std::vector::const_iterator It= Grp.begin();It!=Grp.end();++It) { + if ((*It)->getTypeId().isDerivedFrom(Raytracing::RayFeature::getClassTypeId())) { + Raytracing::RayFeature *View = dynamic_cast(*It); + ofile << View->Result.getValue(); + ofile << endl << endl << endl; } } diff --git a/src/Mod/Raytracing/App/RayProject.h b/src/Mod/Raytracing/App/RayProject.h index b64f97af5..cc80b10e9 100644 --- a/src/Mod/Raytracing/App/RayProject.h +++ b/src/Mod/Raytracing/App/RayProject.h @@ -48,6 +48,7 @@ public: App::PropertyFileIncluded PageResult; App::PropertyFile Template; + App::PropertyString Camera; /** @name methods overide Feature */ diff --git a/src/Mod/Raytracing/Gui/AppRaytracingGuiPy.cpp b/src/Mod/Raytracing/Gui/AppRaytracingGuiPy.cpp index e1a4417fd..69c31cad1 100644 --- a/src/Mod/Raytracing/Gui/AppRaytracingGuiPy.cpp +++ b/src/Mod/Raytracing/Gui/AppRaytracingGuiPy.cpp @@ -91,49 +91,49 @@ povViewCamera(PyObject *self, PyObject *args) if (!PyArg_ParseTuple(args, "")) return NULL; PY_TRY { - std::string out; - const char* ppReturn=0; + std::string out; + const char* ppReturn=0; - Gui::Application::Instance->sendMsgToActiveView("GetCamera",&ppReturn); + Gui::Application::Instance->sendMsgToActiveView("GetCamera",&ppReturn); - SoNode* rootNode; - SoInput in; - in.setBuffer((void*)ppReturn,std::strlen(ppReturn)); - SoDB::read(&in,rootNode); + 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())) + throw Base::Exception("CmdRaytracingWriteCamera::activated(): Could not read " + "camera information from ASCII stream....\n"); - // root-node returned from SoDB::readAll() has initial zero - // ref-count, so reference it before we start using it to - // avoid premature destruction. - SoCamera * Cam = static_cast(rootNode); - Cam->ref(); + // root-node returned from SoDB::readAll() has initial zero + // ref-count, so reference it before we start using it to + // avoid premature destruction. + SoCamera * Cam = static_cast(rootNode); + Cam->ref(); - SbRotation camrot = Cam->orientation.getValue(); + SbRotation camrot = Cam->orientation.getValue(); - SbVec3f upvec(0, 1, 0); // init to default up vector - camrot.multVec(upvec, upvec); + SbVec3f upvec(0, 1, 0); // init to default up vector + camrot.multVec(upvec, upvec); - SbVec3f lookat(0, 0, -1); // init to default view direction vector - camrot.multVec(lookat, lookat); + SbVec3f lookat(0, 0, -1); // init to default view direction vector + camrot.multVec(lookat, lookat); - SbVec3f pos = Cam->position.getValue(); - float Dist = Cam->focalDistance.getValue(); + SbVec3f pos = Cam->position.getValue(); + float Dist = Cam->focalDistance.getValue(); - // making gp out of the Coin stuff - gp_Vec gpPos(pos.getValue()[0],pos.getValue()[1],pos.getValue()[2]); - gp_Vec gpDir(lookat.getValue()[0],lookat.getValue()[1],lookat.getValue()[2]); - lookat *= Dist; - lookat += pos; - gp_Vec gpLookAt(lookat.getValue()[0],lookat.getValue()[1],lookat.getValue()[2]); - gp_Vec gpUp(upvec.getValue()[0],upvec.getValue()[1],upvec.getValue()[2]); + // making gp out of the Coin stuff + gp_Vec gpPos(pos.getValue()[0],pos.getValue()[1],pos.getValue()[2]); + gp_Vec gpDir(lookat.getValue()[0],lookat.getValue()[1],lookat.getValue()[2]); + lookat *= Dist; + lookat += pos; + gp_Vec gpLookAt(lookat.getValue()[0],lookat.getValue()[1],lookat.getValue()[2]); + gp_Vec gpUp(upvec.getValue()[0],upvec.getValue()[1],upvec.getValue()[2]); - // call the write method of PovTools.... - out = PovTools::getCamera(CamDef(gpPos,gpDir,gpLookAt,gpUp)); + // call the write method of PovTools.... + out = PovTools::getCamera(CamDef(gpPos,gpDir,gpLookAt,gpUp)); - return Py::new_reference_to(Py::String(out)); + return Py::new_reference_to(Py::String(out)); } PY_CATCH; } diff --git a/src/Mod/Raytracing/Gui/Command.cpp b/src/Mod/Raytracing/Gui/Command.cpp index 570cab6d7..b049c083a 100644 --- a/src/Mod/Raytracing/Gui/Command.cpp +++ b/src/Mod/Raytracing/Gui/Command.cpp @@ -33,6 +33,7 @@ # include # include # include +# include # include #endif @@ -83,10 +84,20 @@ CmdRaytracingWriteCamera::CmdRaytracingWriteCamera() void CmdRaytracingWriteCamera::activated(int iMsg) { const char* ppReturn=0; - getGuiApplication()->sendMsgToActiveView("GetCamera",&ppReturn); - - Base::Console().Log("GetCamera MSG send:\n%s",ppReturn); + if (ppReturn) { + std::string str(ppReturn); + if (str.find("PerspectiveCamera") == std::string::npos) { + int ret = QMessageBox::warning(Gui::getMainWindow(), + qApp->translate("CmdRaytracingWriteView","No perspective camera"), + qApp->translate("CmdRaytracingWriteView","The current view camera is not perspective" + " and thus the result of the povray image later might look different to" + " what you expect.\nDo you want to continue?"), + QMessageBox::Yes|QMessageBox::No); + if (ret != QMessageBox::Yes) + return; + } + } SoInput in; in.setBuffer((void*)ppReturn,std::strlen(ppReturn)); @@ -214,10 +225,27 @@ CmdRaytracingWriteView::CmdRaytracingWriteView() void CmdRaytracingWriteView::activated(int iMsg) { + const char* ppReturn=0; + Gui::Application::Instance->sendMsgToActiveView("GetCamera",&ppReturn); + if (ppReturn) { + std::string str(ppReturn); + if (str.find("PerspectiveCamera") == std::string::npos) { + int ret = QMessageBox::warning(Gui::getMainWindow(), + qApp->translate("CmdRaytracingWriteView","No perspective camera"), + qApp->translate("CmdRaytracingWriteView","The current view camera is not perspective" + " and thus the result of the povray image later might look different to" + " what you expect.\nDo you want to continue?"), + QMessageBox::Yes|QMessageBox::No); + if (ret != QMessageBox::Yes) + return; + } + } + QStringList filter; filter << QObject::tr("Povray(*.pov)"); filter << QObject::tr("All Files (*.*)"); - QString fn = Gui::FileDialog::getSaveFileName(Gui::getMainWindow(), QObject::tr("Export page"), QString(), filter.join(QLatin1String(";;"))); + QString fn = Gui::FileDialog::getSaveFileName(Gui::getMainWindow(), + QObject::tr("Export page"), QString(), filter.join(QLatin1String(";;"))); if (fn.isEmpty()) return; std::string cFullName = (const char*)fn.toUtf8(); @@ -234,10 +262,13 @@ void CmdRaytracingWriteView::activated(int iMsg) doCommand(Doc,"OutFile.write(RaytracingGui.povViewCamera())"); // go through all document objects for (std::vector::const_iterator it=DocObjects.begin();it!=DocObjects.end();++it) { - App::PropertyColor *pcColor = dynamic_cast(getActiveGuiDocument()->getViewProvider(*it)->getPropertyByName("ShapeColor")); - App::Color col = pcColor->getValue(); - doCommand(Doc,"OutFile.write(Raytracing.getPartAsPovray('%s',App.activeDocument().%s.Shape,%f,%f,%f))", - (*it)->getNameInDocument(),(*it)->getNameInDocument(),col.r,col.g,col.b); + 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,"OutFile.write(Raytracing.getPartAsPovray('%s',App.activeDocument().%s.Shape,%f,%f,%f))", + (*it)->getNameInDocument(),(*it)->getNameInDocument(),col.r,col.g,col.b); + } } doCommand(Doc,"OutFile.close()"); @@ -258,99 +289,6 @@ bool CmdRaytracingWriteView::isActive(void) } -////=========================================================================== -//// CmdRaytracingNewProject -////=========================================================================== -//DEF_STD_CMD_A(CmdRaytracingNewProject); -// -//CmdRaytracingNewProject::CmdRaytracingNewProject() -// :Command("Raytracing_NewProject") -//{ -// sAppModule = "Raytracing"; -// sGroup = QT_TR_NOOP("Raytracing"); -// sMenuText = QT_TR_NOOP("New project"); -// sToolTipText = QT_TR_NOOP("Write the initial povray file to render a part"); -// sWhatsThis = sToolTipText; -// sStatusTip = sToolTipText; -// sPixmap = "Test1"; -// sAccel = "Ctrl+P"; -//} -// -//void CmdRaytracingNewProject::activated(int iMsg) -//{ -// // getting standard parameter -// ParameterGrp::handle hGrp = App::GetApplication().GetParameterGroupByPath("User parameter:BaseApp/Preferences/Mod/Raytracing"); -// std::string cDir = hGrp->GetASCII("ProjectPath", ""); -// // xorx: The following has to be implemented as a setting -// std::string cPovRayName = hGrp->GetASCII("SceneFilename", "PovrayScene.pov"); -// // HACK: This is the workaround -// //std::string cPovRayName="PovrayScene.pov"; -// -// if (cDir!="" && cDir[cDir.size()-1] != PATHSEP) -// cDir += PATHSEP; -// std::string cFullName = cDir+cPovRayName; -// -// // Open RayTracing module -// doCommand(Doc,"import Raytracing"); -// // Get the default scene file and write it to the Project directory -// doCommand(Doc,"Raytracing.copyResource(\"FCSimple.pov\",\"%s\")",strToPython(cFullName).c_str()); -//} -// -//bool CmdRaytracingNewProject::isActive(void) -//{ -// //if( getActiveDocument() ) -// return true; -// //else -// // return false; -//} -// -////=========================================================================== -//// CmdRaytracingQuickRender -////=========================================================================== -//DEF_STD_CMD_A(CmdRaytracingQuickRender); -// -//CmdRaytracingQuickRender::CmdRaytracingQuickRender() -// :Command("Raytracing_QuickRender") -//{ -// sAppModule = "Raytracing"; -// sGroup = QT_TR_NOOP("Raytracing"); -// sMenuText = QT_TR_NOOP("Render"); -// sToolTipText = QT_TR_NOOP("Renders the actual view"); -// sWhatsThis = sToolTipText; -// sStatusTip = sToolTipText; -// sPixmap = "Test1"; -// sAccel = "Ctrl+P"; -//} -// -//void CmdRaytracingQuickRender::activated(int iMsg) -//{ -// // get the preferences -// ParameterGrp::handle hGrp = App::GetApplication().GetParameterGroupByPath("User parameter:BaseApp/Preferences/Mod/Raytracing"); -// std::string cDir = hGrp->GetASCII("ProjectPath", ""); -// -// //cDir = Gui::FileDialog::getExistingDirectory(cDir.c_str()).latin1(); -// -// if (cDir!="" && cDir[cDir.size()-1] != PATHSEP) -// cDir += PATHSEP; -// -// std::string cFullName = cDir+"FreeCAD.pov"; -// Base::Console().Log("Using file name:%s",cFullName.c_str()); -// -// // open the file and write -// std::ofstream fout(cFullName.c_str()); -// fout << FreeCAD ; -// fout.close(); -//} -// -//bool CmdRaytracingQuickRender::isActive(void) -//{ -// //if( getActiveDocument() ) -// return true; -// //else -// // return false; -//} -// - //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ //=========================================================================== // Raytracing_NewPovrayProject @@ -372,11 +310,29 @@ CmdRaytracingNewPovrayProject::CmdRaytracingNewPovrayProject() void CmdRaytracingNewPovrayProject::activated(int iMsg) { + const char* ppReturn=0; + Gui::Application::Instance->sendMsgToActiveView("GetCamera",&ppReturn); + if (ppReturn) { + std::string str(ppReturn); + if (str.find("PerspectiveCamera") == std::string::npos) { + int ret = QMessageBox::warning(Gui::getMainWindow(), + qApp->translate("CmdRaytracingWriteView","No perspective camera"), + qApp->translate("CmdRaytracingWriteView","The current view camera is not perspective" + " and thus the result of the povray image later might look different to" + " what you expect.\nDo you want to continue?"), + QMessageBox::Yes|QMessageBox::No); + if (ret != QMessageBox::Yes) + return; + } + } + std::string FeatName = getUniqueObjectName("PovProject"); openCommand("Raytracing create project"); + doCommand(Doc,"import Raytracing,RaytracingGui"); doCommand(Doc,"App.activeDocument().addObject('Raytracing::RayProject','%s')",FeatName.c_str()); doCommand(Doc,"App.activeDocument().%s.Template = App.getResourceDir()+'Mod/Raytracing/Templates/ProjectStd.pov'",FeatName.c_str()); + doCommand(Doc,"App.activeDocument().%s.Camera = RaytracingGui.povViewCamera()",FeatName.c_str()); commitCommand(); } @@ -424,7 +380,17 @@ void CmdRaytracingNewPartSegment::activated(int iMsg) return; } - std::string ProjName = pages.front()->getNameInDocument(); + std::string ProjName; + if (pages.size() > 1) { + pages = Gui::Selection().getObjectsOfType(Raytracing::RayProject::getClassTypeId()); + if (pages.size() != 1) { + QMessageBox::warning(Gui::getMainWindow(), QObject::tr("No Povray project to insert"), + QObject::tr("Select a Povray project to insert the view.")); + return; + } + } + + ProjName = pages.front()->getNameInDocument(); openCommand("Create view"); for (std::vector::iterator it = parts.begin(); it != parts.end(); ++it) { @@ -433,6 +399,7 @@ void CmdRaytracingNewPartSegment::activated(int iMsg) FeatName = getUniqueObjectName(FeatName.c_str()); doCommand(Doc,"App.activeDocument().addObject('Raytracing::RayFeature','%s')",FeatName.c_str()); doCommand(Doc,"App.activeDocument().%s.Source = App.activeDocument().%s",FeatName.c_str(),(*it)->getNameInDocument()); + doCommand(Doc,"App.activeDocument().%s.Color = Gui.activeDocument().%s.ShapeColor",FeatName.c_str(),(*it)->getNameInDocument()); doCommand(Doc,"App.activeDocument().%s.addObject(App.activeDocument().%s)",ProjName.c_str(), FeatName.c_str()); } updateActive();