Mod/Raytracing/Gui/Command.cpp

Go to the documentation of this file.
00001  /***************************************************************************
00002  *   Copyright (c) Jürgen Riegel          (juergen.riegel@web.de) 2002     *
00003  *                                                                         *
00004  *   This file is part of the FreeCAD CAx development system.              *
00005  *                                                                         *
00006  *   This library is free software; you can redistribute it and/or         *
00007  *   modify it under the terms of the GNU Library General Public           *
00008  *   License as published by the Free Software Foundation; either          *
00009  *   version 2 of the License, or (at your option) any later version.      *
00010  *                                                                         *
00011  *   This library  is distributed in the hope that it will be useful,      *
00012  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00013  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00014  *   GNU Library General Public License for more details.                  *
00015  *                                                                         *
00016  *   You should have received a copy of the GNU Library General Public     *
00017  *   License along with this library; see the file COPYING.LIB. If not,    *
00018  *   write to the Free Software Foundation, Inc., 59 Temple Place,         *
00019  *   Suite 330, Boston, MA  02111-1307, USA                                *
00020  *                                                                         *
00021  ***************************************************************************/
00022 
00023 
00024 #include "PreCompiled.h"
00025 #ifndef _PreComp_
00026 # include <BRep_Tool.hxx>
00027 # include <GeomAPI_ProjectPointOnSurf.hxx>
00028 # include <GeomLProp_SLProps.hxx>
00029 # include <Poly_Triangulation.hxx>
00030 # include <TopoDS_Face.hxx>
00031 # include <Inventor/SoInput.h>
00032 # include <Inventor/nodes/SoNode.h>
00033 # include <Inventor/nodes/SoOrthographicCamera.h>
00034 # include <vector>
00035 # include <Inventor/nodes/SoPerspectiveCamera.h>
00036 # include <QMessageBox>
00037 #endif
00038 
00039 #include <Base/Console.h>
00040 #include <Base/Exception.h>
00041 #include <App/Application.h>
00042 #include <App/Document.h>
00043 #include <App/DocumentObject.h>
00044 #include <App/Material.h>
00045 #include <Gui/Application.h>
00046 #include <Gui/Document.h>
00047 #include <Gui/Command.h>
00048 #include <Gui/FileDialog.h>
00049 #include <Gui/View.h>
00050 #include <Gui/ViewProvider.h>
00051 #include <Gui/Selection.h>
00052 #include <Gui/FileDialog.h>
00053 #include <Gui/MainWindow.h>
00054 
00055 #include <Mod/Raytracing/App/RayFeature.h>
00056 #include <Mod/Raytracing/App/RaySegment.h>
00057 #include <Mod/Raytracing/App/RayProject.h>
00058 #include <Mod/Part/App/PartFeature.h>
00059   
00060 #include "FreeCADpov.h"
00061 
00062 
00063 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00064 
00065 
00066 //===========================================================================
00067 // CmdRaytracingWriteCamera
00068 //===========================================================================
00069 DEF_STD_CMD_A(CmdRaytracingWriteCamera);
00070 
00071 CmdRaytracingWriteCamera::CmdRaytracingWriteCamera()
00072   :Command("Raytracing_WriteCamera")
00073 {
00074     sAppModule    = "Raytracing";
00075     sGroup        = QT_TR_NOOP("Raytracing");
00076     sMenuText     = QT_TR_NOOP("Export camera to povray...");
00077     sToolTipText  = QT_TR_NOOP("Export the camera positon of the active 3D view in PovRay format to a file");
00078     sWhatsThis    = sToolTipText;
00079     sStatusTip    = sToolTipText;
00080     sPixmap       = "Raytrace_Camera";
00081 }
00082 
00083 void CmdRaytracingWriteCamera::activated(int iMsg)
00084 {
00085     const char* ppReturn=0;
00086 
00087     getGuiApplication()->sendMsgToActiveView("GetCamera",&ppReturn);
00088 
00089     Base::Console().Log("GetCamera MSG send:\n%s",ppReturn);
00090 
00091     SoInput in;
00092     in.setBuffer((void*)ppReturn,std::strlen(ppReturn));
00093 
00094     //if (!in.openFile(filename)) { exit(1); }
00095 
00096     SoNode* rootNode;
00097     SoDB::read(&in,rootNode);
00098 
00099     if (!rootNode || !rootNode->getTypeId().isDerivedFrom(SoCamera::getClassTypeId()))
00100         throw Base::Exception("CmdRaytracingWriteCamera::activated(): Could not read "
00101                               "camera information from ASCII stream....\n");
00102 
00103     // root-node returned from SoDB::readAll() has initial zero
00104     // ref-count, so reference it before we start using it to
00105     // avoid premature destruction.
00106     SoCamera * Cam = static_cast<SoCamera*>(rootNode);
00107     Cam->ref();
00108 
00109     SbRotation camrot = Cam->orientation.getValue();
00110 
00111     SbVec3f upvec(0, 1, 0); // init to default up vector
00112     camrot.multVec(upvec, upvec);
00113 
00114     SbVec3f lookat(0, 0, -1); // init to default view direction vector
00115     camrot.multVec(lookat, lookat);
00116 
00117     SbVec3f pos = Cam->position.getValue();
00118     float Dist = Cam->focalDistance.getValue();
00119 
00120     QStringList filter;
00121     filter << QObject::tr("Povray(*.pov)");
00122     filter << QObject::tr("All Files (*.*)");
00123     QString fn = Gui::FileDialog::getSaveFileName(Gui::getMainWindow(), QObject::tr("Export page"), QString(), filter.join(QLatin1String(";;")));
00124     if (fn.isEmpty()) 
00125         return;
00126     std::string cFullName = (const char*)fn.toUtf8();
00127 
00128     // building up the python string
00129     std::stringstream out;
00130     out << "Raytracing.writeCameraFile(\"" << strToPython(cFullName) << "\"," 
00131         << "(" << pos.getValue()[0]    <<"," << pos.getValue()[1]    <<"," << pos.getValue()[2]    <<")," 
00132         << "(" << lookat.getValue()[0] <<"," << lookat.getValue()[1] <<"," << lookat.getValue()[2] <<")," ;
00133     lookat *= Dist;
00134     lookat += pos;
00135     out << "(" << lookat.getValue()[0] <<"," << lookat.getValue()[1] <<"," << lookat.getValue()[2] <<")," 
00136         << "(" << upvec.getValue()[0]  <<"," << upvec.getValue()[1]  <<"," << upvec.getValue()[2]  <<") )" ;
00137 
00138     doCommand(Doc,"import Raytracing");
00139     doCommand(Gui,out.str().c_str());
00140 
00141 
00142     // Bring ref-count of root-node back to zero to cause the
00143     // destruction of the camera.
00144     Cam->unref();
00145 }
00146 
00147 bool CmdRaytracingWriteCamera::isActive(void)
00148 {
00149     return getGuiApplication()->sendHasMsgToActiveView("GetCamera");
00150 }
00151 
00152 //===========================================================================
00153 // CmdRaytracingWritePart
00154 //===========================================================================
00155 DEF_STD_CMD_A(CmdRaytracingWritePart);
00156 
00157 CmdRaytracingWritePart::CmdRaytracingWritePart()
00158   :Command("Raytracing_WritePart")
00159 {
00160     sAppModule    = "Raytracing";
00161     sGroup        = QT_TR_NOOP("Raytracing");
00162     sMenuText     = QT_TR_NOOP("Export part to povray...");
00163     sToolTipText  = QT_TR_NOOP("Write the selected Part (object) as a povray file");
00164     sWhatsThis    = sToolTipText;
00165     sStatusTip    = sToolTipText;
00166     sPixmap       = "Raytrace_Part";
00167 }
00168 
00169 void CmdRaytracingWritePart::activated(int iMsg)
00170 {
00171     QStringList filter;
00172     filter << QObject::tr("Povray(*.pov)");
00173     filter << QObject::tr("All Files (*.*)");
00174     QString fn = Gui::FileDialog::getSaveFileName(Gui::getMainWindow(), QObject::tr("Export page"), QString(), filter.join(QLatin1String(";;")));
00175     if (fn.isEmpty()) 
00176         return;
00177     std::string cFullName = (const char*)fn.toUtf8();
00178 
00179     // name of the objects in the pov file
00180     std::string Name = "Part";
00181     std::vector<App::DocumentObject*> obj = Gui::Selection().getObjectsOfType(Part::Feature::getClassTypeId());
00182     if (obj.empty()) return;
00183 
00184     std::stringstream out;
00185     //Raytracing.writePartFile(App.document().GetActiveFeature().getShape())
00186     out << "Raytracing.writePartFile(\"" << strToPython(cFullName) << "\",\""
00187         << Name << "\",App.ActiveDocument." << obj.front()->getNameInDocument() << ".Shape)";
00188 
00189     doCommand(Doc,"import Raytracing");
00190     doCommand(Doc,out.str().c_str());
00191 }
00192 
00193 bool CmdRaytracingWritePart::isActive(void)
00194 {
00195     return Gui::Selection().countObjectsOfType(Part::Feature::getClassTypeId()) == 1;
00196 }
00197 
00198 //===========================================================================
00199 // CmdRaytracingWriteView
00200 //===========================================================================
00201 DEF_STD_CMD_A(CmdRaytracingWriteView);
00202 
00203 CmdRaytracingWriteView::CmdRaytracingWriteView()
00204   :Command("Raytracing_WriteView")
00205 {
00206     sAppModule    = "Raytracing";
00207     sGroup        = QT_TR_NOOP("Raytracing");
00208     sMenuText     = QT_TR_NOOP("Export view to povray...");
00209     sToolTipText  = QT_TR_NOOP("Write the active 3D view with camera and all its content to a povray file");
00210     sWhatsThis    = sToolTipText;
00211     sStatusTip    = sToolTipText;
00212     sPixmap       = "Raytrace_Export";
00213 }
00214 
00215 void CmdRaytracingWriteView::activated(int iMsg)
00216 {
00217     QStringList filter;
00218     filter << QObject::tr("Povray(*.pov)");
00219     filter << QObject::tr("All Files (*.*)");
00220     QString fn = Gui::FileDialog::getSaveFileName(Gui::getMainWindow(), QObject::tr("Export page"), QString(), filter.join(QLatin1String(";;")));
00221     if (fn.isEmpty()) 
00222         return;
00223     std::string cFullName = (const char*)fn.toUtf8();
00224 
00225 
00226     // get all objects of the active document
00227     std::vector<Part::Feature*> DocObjects = getActiveGuiDocument()->getDocument()->
00228         getObjectsOfType<Part::Feature>();
00229 
00230     openCommand("Write view");
00231     doCommand(Doc,"import Raytracing,RaytracingGui");
00232     doCommand(Doc,"OutFile = open(unicode('%s','utf-8'),'w')",cFullName.c_str());
00233     doCommand(Doc,"OutFile.write(open(App.getResourceDir()+'Mod/Raytracing/Templates/ProjectStd.pov').read())");
00234     doCommand(Doc,"OutFile.write(RaytracingGui.povViewCamera())");
00235     // go through all document objects
00236     for (std::vector<Part::Feature*>::const_iterator it=DocObjects.begin();it!=DocObjects.end();++it) {
00237         App::PropertyColor *pcColor = dynamic_cast<App::PropertyColor *>(getActiveGuiDocument()->getViewProvider(*it)->getPropertyByName("ShapeColor"));
00238         App::Color col = pcColor->getValue();
00239         doCommand(Doc,"OutFile.write(Raytracing.getPartAsPovray('%s',App.activeDocument().%s.Shape,%f,%f,%f))",
00240                  (*it)->getNameInDocument(),(*it)->getNameInDocument(),col.r,col.g,col.b);
00241     }
00242 
00243     doCommand(Doc,"OutFile.close()");
00244     doCommand(Doc,"del OutFile");
00245 
00246     updateActive();
00247     commitCommand();
00248 }
00249 
00250 bool CmdRaytracingWriteView::isActive(void)
00251 {
00252     App::Document* doc = App::GetApplication().getActiveDocument();
00253     if (doc) {
00254         return doc->countObjectsOfType(Part::Feature::getClassTypeId()) > 0;
00255     }
00256 
00257     return false;
00258 }
00259 
00260 
00264 //DEF_STD_CMD_A(CmdRaytracingNewProject);
00265 //
00266 //CmdRaytracingNewProject::CmdRaytracingNewProject()
00267 //  :Command("Raytracing_NewProject")
00268 //{
00269 //    sAppModule    = "Raytracing";
00270 //    sGroup        = QT_TR_NOOP("Raytracing");
00271 //    sMenuText     = QT_TR_NOOP("New project");
00272 //    sToolTipText  = QT_TR_NOOP("Write the initial povray file to render a part");
00273 //    sWhatsThis    = sToolTipText;
00274 //    sStatusTip    = sToolTipText;
00275 //    sPixmap       = "Test1";
00276 //    sAccel        = "Ctrl+P";
00277 //}
00278 //
00279 //void CmdRaytracingNewProject::activated(int iMsg)
00280 //{
00281 //    // getting standard parameter
00282 //    ParameterGrp::handle hGrp = App::GetApplication().GetParameterGroupByPath("User parameter:BaseApp/Preferences/Mod/Raytracing");
00283 //    std::string cDir             = hGrp->GetASCII("ProjectPath", "");
00284 //    // xorx: The following has to be implemented as a setting
00285 //    std::string cPovRayName      = hGrp->GetASCII("SceneFilename", "PovrayScene.pov");
00286 //    // HACK: This is the workaround
00287 //    //std::string cPovRayName="PovrayScene.pov";
00288 //
00289 //    if (cDir!="" && cDir[cDir.size()-1] != PATHSEP)
00290 //        cDir += PATHSEP;
00291 //    std::string cFullName = cDir+cPovRayName;
00292 //
00293 //    // Open RayTracing module
00294 //    doCommand(Doc,"import Raytracing");
00295 //    // Get the default scene file and write it to the Project directory
00296 //    doCommand(Doc,"Raytracing.copyResource(\"FCSimple.pov\",\"%s\")",strToPython(cFullName).c_str());
00297 //}
00298 //
00299 //bool CmdRaytracingNewProject::isActive(void)
00300 //{
00301 //    //if( getActiveDocument() )
00302 //        return true;
00303 //    //else
00304 //    //  return false;
00305 //}
00306 //
00310 //DEF_STD_CMD_A(CmdRaytracingQuickRender);
00311 //
00312 //CmdRaytracingQuickRender::CmdRaytracingQuickRender()
00313 //  :Command("Raytracing_QuickRender")
00314 //{
00315 //    sAppModule    = "Raytracing";
00316 //    sGroup        = QT_TR_NOOP("Raytracing");
00317 //    sMenuText     = QT_TR_NOOP("Render");
00318 //    sToolTipText  = QT_TR_NOOP("Renders the actual view");
00319 //    sWhatsThis    = sToolTipText;
00320 //    sStatusTip    = sToolTipText;
00321 //    sPixmap       = "Test1";
00322 //    sAccel        = "Ctrl+P";
00323 //}
00324 //
00325 //void CmdRaytracingQuickRender::activated(int iMsg)
00326 //{
00327 //    // get the preferences
00328 //    ParameterGrp::handle hGrp = App::GetApplication().GetParameterGroupByPath("User parameter:BaseApp/Preferences/Mod/Raytracing");
00329 //    std::string cDir             = hGrp->GetASCII("ProjectPath", "");
00330 //
00331 //    //cDir = Gui::FileDialog::getExistingDirectory(cDir.c_str()).latin1();
00332 //
00333 //    if (cDir!="" && cDir[cDir.size()-1] != PATHSEP)
00334 //        cDir += PATHSEP;
00335 //
00336 //    std::string cFullName = cDir+"FreeCAD.pov";
00337 //    Base::Console().Log("Using file name:%s",cFullName.c_str());
00338 //
00339 //    // open the file and write
00340 //    std::ofstream fout(cFullName.c_str());
00341 //    fout << FreeCAD ;
00342 //    fout.close();
00343 //}
00344 //
00345 //bool CmdRaytracingQuickRender::isActive(void)
00346 //{
00347 //    //if( getActiveDocument() )
00348 //        return true;
00349 //    //else
00350 //    //  return false;
00351 //}
00352 //
00353 
00354 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00355 //===========================================================================
00356 // Raytracing_NewPovrayProject
00357 //===========================================================================
00358 
00359 DEF_STD_CMD_A(CmdRaytracingNewPovrayProject);
00360 
00361 CmdRaytracingNewPovrayProject::CmdRaytracingNewPovrayProject()
00362   : Command("Raytracing_NewPovrayProject")
00363 {
00364     sAppModule      = "Raytracing";
00365     sGroup          = QT_TR_NOOP("Raytracing");
00366     sMenuText       = QT_TR_NOOP("New Povray project");
00367     sToolTipText    = QT_TR_NOOP("Insert new Povray project into the document");
00368     sWhatsThis      = "Raytracing_NewPovrayProject";
00369     sStatusTip      = sToolTipText;
00370     sPixmap         = "Raytrace_New";
00371 }
00372 
00373 void CmdRaytracingNewPovrayProject::activated(int iMsg)
00374 {
00375     std::string FeatName = getUniqueObjectName("PovProject");
00376 
00377     openCommand("Raytracing create project");
00378     doCommand(Doc,"App.activeDocument().addObject('Raytracing::RayProject','%s')",FeatName.c_str());
00379     doCommand(Doc,"App.activeDocument().%s.Template = App.getResourceDir()+'Mod/Raytracing/Templates/ProjectStd.pov'",FeatName.c_str());
00380     commitCommand();
00381 }
00382 
00383 bool CmdRaytracingNewPovrayProject::isActive(void)
00384 {
00385     if (getActiveGuiDocument())
00386         return true;
00387     else
00388         return false;
00389 }
00390 
00391 
00392 //===========================================================================
00393 // Raytracing_NewPartView
00394 //===========================================================================
00395 
00396 DEF_STD_CMD(CmdRaytracingNewPartSegment);
00397 
00398 CmdRaytracingNewPartSegment::CmdRaytracingNewPartSegment()
00399   : Command("Raytracing_NewPartSegment")
00400 {
00401     sAppModule      = "Raytracing";
00402     sGroup          = QT_TR_NOOP("Raytracing");
00403     sMenuText       = QT_TR_NOOP("Insert part");
00404     sToolTipText    = QT_TR_NOOP("Insert a new part object into a Povray project");
00405     sWhatsThis      = "Raytracing_NewPartSegment";
00406     sStatusTip      = sToolTipText;
00407     sPixmap         = 0;
00408 }
00409 
00410 void CmdRaytracingNewPartSegment::activated(int iMsg)
00411 {
00412     std::vector<Part::Feature*> parts = Gui::Selection().getObjectsOfType<Part::Feature>();
00413     if (parts.empty()) {
00414         QMessageBox::warning(Gui::getMainWindow(), QObject::tr("Wrong selection"),
00415             QObject::tr("Select a Part object."));
00416         return;
00417     }
00418 
00419     std::vector<App::DocumentObject*> pages = App::GetApplication().getActiveDocument()
00420         ->getObjectsOfType(Raytracing::RayProject::getClassTypeId());
00421     if (pages.empty()) {
00422         QMessageBox::warning(Gui::getMainWindow(), QObject::tr("No Povray project to insert"),
00423             QObject::tr("Create a Povray project to insert a view."));
00424         return;
00425     }
00426 
00427     std::string ProjName = pages.front()->getNameInDocument();
00428 
00429     openCommand("Create view");
00430     for (std::vector<Part::Feature*>::iterator it = parts.begin(); it != parts.end(); ++it) {
00431         std::string FeatName = (*it)->getNameInDocument();
00432         FeatName += "_View";
00433         FeatName = getUniqueObjectName(FeatName.c_str());
00434         doCommand(Doc,"App.activeDocument().addObject('Raytracing::RayFeature','%s')",FeatName.c_str());
00435         doCommand(Doc,"App.activeDocument().%s.Source = App.activeDocument().%s",FeatName.c_str(),(*it)->getNameInDocument());
00436         doCommand(Doc,"App.activeDocument().%s.addObject(App.activeDocument().%s)",ProjName.c_str(), FeatName.c_str());
00437     }
00438     updateActive();
00439     commitCommand();
00440 }
00441 
00442 //===========================================================================
00443 // Raytracing_ExportProject
00444 //===========================================================================
00445 
00446 DEF_STD_CMD_A(CmdRaytracingExportProject);
00447 
00448 CmdRaytracingExportProject::CmdRaytracingExportProject()
00449   : Command("Raytracing_ExportProject")
00450 {
00451     // seting the
00452     sGroup        = QT_TR_NOOP("File");
00453     sMenuText     = QT_TR_NOOP("&Export project...");
00454     sToolTipText  = QT_TR_NOOP("Export the Povray project file");
00455     sWhatsThis    = "Raytracing_ExportProject";
00456     sStatusTip    = sToolTipText;
00457     sPixmap       = "Raytrace_Export";
00458 }
00459 
00460 void CmdRaytracingExportProject::activated(int iMsg)
00461 {
00462     unsigned int n = getSelection().countObjectsOfType(Raytracing::RayProject::getClassTypeId());
00463     if (n != 1) {
00464         QMessageBox::warning(Gui::getMainWindow(), QObject::tr("Wrong selection"),
00465             QObject::tr("Select one Povray project object."));
00466         return;
00467     }
00468 
00469     QStringList filter;
00470     filter << QObject::tr("Povray(*.pov)");
00471     filter << QObject::tr("All Files (*.*)");
00472 
00473     QString fn = Gui::FileDialog::getSaveFileName(Gui::getMainWindow(), QObject::tr("Export page"), QString(), filter.join(QLatin1String(";;")));
00474     if (!fn.isEmpty()) {
00475         std::vector<Gui::SelectionSingleton::SelObj> Sel = getSelection().getSelection();
00476         openCommand("Raytracing export project");
00477 
00478         doCommand(Doc,"PageFile = open(App.activeDocument().%s.PageResult,'r')",Sel[0].FeatName);
00479         std::string fname = (const char*)fn.toUtf8();
00480         doCommand(Doc,"OutFile = open(unicode('%s','utf-8'),'w')",fname.c_str());
00481         doCommand(Doc,"OutFile.write(PageFile.read())");
00482         doCommand(Doc,"del OutFile,PageFile");
00483 
00484         commitCommand();
00485     }
00486 }
00487 
00488 bool CmdRaytracingExportProject::isActive(void)
00489 {
00490     return (getActiveGuiDocument() ? true : false);
00491 }
00492 
00493 
00494 void CreateRaytracingCommands(void)
00495 {
00496     Gui::CommandManager &rcCmdMgr = Gui::Application::Instance->commandManager();
00497     rcCmdMgr.addCommand(new CmdRaytracingWriteCamera());
00498     rcCmdMgr.addCommand(new CmdRaytracingWritePart());
00499     rcCmdMgr.addCommand(new CmdRaytracingWriteView());
00500     rcCmdMgr.addCommand(new CmdRaytracingNewPovrayProject());
00501     rcCmdMgr.addCommand(new CmdRaytracingExportProject());
00502     rcCmdMgr.addCommand(new CmdRaytracingNewPartSegment());
00503 }

Generated on Wed Nov 23 19:00:02 2011 for FreeCAD by  doxygen 1.6.1