1
/***************************************************************************
2
* Copyright (c) Jļæ½rgen Riegel (juergen.riegel@web.de) 2002 *
4
* This file is part of the FreeCAD CAx development system. *
6
* This library is free software; you can redistribute it and/or *
7
* modify it under the terms of the GNU Library General Public *
8
* License as published by the Free Software Foundation; either *
9
* version 2 of the License, or (at your option) any later version. *
11
* This library is distributed in the hope that it will be useful, *
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14
* GNU Library General Public License for more details. *
16
* You should have received a copy of the GNU Library General Public *
17
* License along with this library; see the file COPYING.LIB. If not, *
18
* write to the Free Software Foundation, Inc., 59 Temple Place, *
19
* Suite 330, Boston, MA 02111-1307, USA *
21
***************************************************************************/
24
#include "PreCompiled.h"
26
# include <BRep_Tool.hxx>
27
# include <GeomAPI_ProjectPointOnSurf.hxx>
28
# include <GeomLProp_SLProps.hxx>
29
# include <Poly_Triangulation.hxx>
30
# include <TopoDS_Face.hxx>
33
#include <Base/Console.h>
34
#include <Base/Exception.h>
35
#include <App/Document.h>
36
#include <Gui/Application.h>
37
#include <Gui/Document.h>
38
#include <Gui/Command.h>
39
#include <Gui/FileDialog.h>
42
#include <Mod/Part/App/PartFeature.h>
44
#include "FreeCADpov.h"
47
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
50
//===========================================================================
51
// CmdRaytracingWriteCamera
52
//===========================================================================
53
DEF_STD_CMD_A(CmdRaytracingWriteCamera);
55
CmdRaytracingWriteCamera::CmdRaytracingWriteCamera()
56
:Command("Raytracing_WriteCamera")
58
sAppModule = "Raytracing";
59
sGroup = QT_TR_NOOP("Raytracing");
60
sMenuText = QT_TR_NOOP("Write camera position");
61
sToolTipText = QT_TR_NOOP("Write the camera positon of the active 3D view in PovRay format to a file");
62
sWhatsThis = sToolTipText;
63
sStatusTip = sToolTipText;
68
void CmdRaytracingWriteCamera::activated(int iMsg)
70
const char* ppReturn=0;
72
getGuiApplication()->sendMsgToActiveView("GetCamera",&ppReturn);
74
Base::Console().Log("GetCamera MSG send:\n%s",ppReturn);
77
in.setBuffer((void*)ppReturn,std::strlen(ppReturn));
79
//if (!in.openFile(filename)) { exit(1); }
82
SoDB::read(&in,rootNode);
84
if (!rootNode || !rootNode->getTypeId().isDerivedFrom(SoCamera::getClassTypeId()))
85
throw Base::Exception("CmdRaytracingWriteCamera::activated(): Could not read "
86
"camera information from ASCII stream....\n");
88
// root-node returned from SoDB::readAll() has initial zero
89
// ref-count, so reference it before we start using it to
90
// avoid premature destruction.
91
SoCamera * Cam = static_cast<SoCamera*>(rootNode);
94
SbRotation camrot = Cam->orientation.getValue();
96
SbVec3f upvec(0, 1, 0); // init to default up vector
97
camrot.multVec(upvec, upvec);
99
SbVec3f lookat(0, 0, -1); // init to default view direction vector
100
camrot.multVec(lookat, lookat);
102
SbVec3f pos = Cam->position.getValue();
103
float Dist = Cam->focalDistance.getValue();
105
// getting standard parameter
106
ParameterGrp::handle hGrp = App::GetApplication().GetParameterGroupByPath("User parameter:BaseApp/Preferences/Mod/Raytracing");
107
std::string cDir = hGrp->GetASCII("ProjectPath", "");
108
std::string cCameraName = hGrp->GetASCII("CameraName", "TempCamera.inc");
110
if (cDir!="" && cDir[cDir.size()-1] != PATHSEP)
112
std::string cFullName = cDir+cCameraName;
114
// building up the python string
115
std::stringstream out;
116
out << "Raytracing.writeCameraFile(\"" << strToPython(cFullName) << "\","
117
<< "(" << pos.getValue()[0] <<"," << pos.getValue()[1] <<"," << pos.getValue()[2] <<"),"
118
<< "(" << lookat.getValue()[0] <<"," << lookat.getValue()[1] <<"," << lookat.getValue()[2] <<")," ;
121
out << "(" << lookat.getValue()[0] <<"," << lookat.getValue()[1] <<"," << lookat.getValue()[2] <<"),"
122
<< "(" << upvec.getValue()[0] <<"," << upvec.getValue()[1] <<"," << upvec.getValue()[2] <<") )" ;
124
doCommand(Doc,"import Raytracing");
125
doCommand(Gui,out.str().c_str());
128
// Bring ref-count of root-node back to zero to cause the
129
// destruction of the camera.
133
bool CmdRaytracingWriteCamera::isActive(void)
135
return getGuiApplication()->sendHasMsgToActiveView("GetCamera");
138
//===========================================================================
139
// CmdRaytracingWritePart
140
//===========================================================================
141
DEF_STD_CMD_A(CmdRaytracingWritePart);
143
CmdRaytracingWritePart::CmdRaytracingWritePart()
144
:Command("Raytracing_WritePart")
146
sAppModule = "Raytracing";
147
sGroup = QT_TR_NOOP("Raytracing");
148
sMenuText = QT_TR_NOOP("Write the part");
149
sToolTipText = QT_TR_NOOP("Write the Part (object) of the active 3D view in PovRay format to a file");
150
sWhatsThis = sToolTipText;
151
sStatusTip = sToolTipText;
156
void CmdRaytracingWritePart::activated(int iMsg)
158
// get the preferences
159
ParameterGrp::handle hGrp = App::GetApplication().GetParameterGroupByPath("User parameter:BaseApp/Preferences/Mod/Raytracing");
160
std::string cDir = hGrp->GetASCII("ProjectPath", "");
161
std::string cPartName = hGrp->GetASCII("PartName", "TempPart.inc");
162
//bool bNotWriteVertexNormals = hGrp->GetBool("NotWriteVertexNormals",false);
163
//float fMeshDeviation = hGrp->GetFloat("MeshDeviation",0.1);
165
// name of the objects in the pov file
166
std::string Name = "Part";
168
if (cDir!="" && cDir[cDir.size()-1] != PATHSEP)
171
std::string cFullName = cDir+cPartName;
173
std::stringstream out;
174
//Raytracing.writePartFile(App.document().GetActiveFeature().getShape())
175
out << "Raytracing.writePartFile(\"" << strToPython(cFullName) << "\",\"" << Name << "\",App.ActiveDocument.ActiveObject.Shape)";
177
doCommand(Doc,"import Raytracing");
178
doCommand(Doc,out.str().c_str());
181
bool CmdRaytracingWritePart::isActive(void)
183
if (getActiveGuiDocument()) {
184
App::DocumentObject* obj = getActiveGuiDocument()->getDocument()->getActiveObject();
185
if (obj && obj->getTypeId().isDerivedFrom(Part::Feature::getClassTypeId()))
193
//===========================================================================
194
// CmdRaytracingNewProject
195
//===========================================================================
196
DEF_STD_CMD_A(CmdRaytracingNewProject);
198
CmdRaytracingNewProject::CmdRaytracingNewProject()
199
:Command("Raytracing_NewProject")
201
sAppModule = "Raytracing";
202
sGroup = QT_TR_NOOP("Raytracing");
203
sMenuText = QT_TR_NOOP("New project");
204
sToolTipText = QT_TR_NOOP("Write the initial povray file to render a part");
205
sWhatsThis = sToolTipText;
206
sStatusTip = sToolTipText;
211
void CmdRaytracingNewProject::activated(int iMsg)
213
// getting standard parameter
214
ParameterGrp::handle hGrp = App::GetApplication().GetParameterGroupByPath("User parameter:BaseApp/Preferences/Mod/Raytracing");
215
std::string cDir = hGrp->GetASCII("ProjectPath", "");
216
// xorx: The following has to be implemented as a setting
217
std::string cPovRayName = hGrp->GetASCII("SceneFilename", "PovrayScene.pov");
218
// HACK: This is the workaround
219
//std::string cPovRayName="PovrayScene.pov";
221
if (cDir!="" && cDir[cDir.size()-1] != PATHSEP)
223
std::string cFullName = cDir+cPovRayName;
225
// Open RayTracing module
226
doCommand(Doc,"import Raytracing");
227
// Get the default scene file and write it to the Project directory
228
doCommand(Doc,"Raytracing.copyResource(\"FCSimple.pov\",\"%s\")",strToPython(cFullName).c_str());
231
bool CmdRaytracingNewProject::isActive(void)
233
//if( getActiveDocument() )
239
//===========================================================================
240
// CmdRaytracingQuickRender
241
//===========================================================================
242
DEF_STD_CMD_A(CmdRaytracingQuickRender);
244
CmdRaytracingQuickRender::CmdRaytracingQuickRender()
245
:Command("Raytracing_QuickRender")
247
sAppModule = "Raytracing";
248
sGroup = QT_TR_NOOP("Raytracing");
249
sMenuText = QT_TR_NOOP("Render");
250
sToolTipText = QT_TR_NOOP("Renders the actual view");
251
sWhatsThis = sToolTipText;
252
sStatusTip = sToolTipText;
257
void CmdRaytracingQuickRender::activated(int iMsg)
259
// get the preferences
260
ParameterGrp::handle hGrp = App::GetApplication().GetParameterGroupByPath("User parameter:BaseApp/Preferences/Mod/Raytracing");
261
std::string cDir = hGrp->GetASCII("ProjectPath", "");
263
//cDir = Gui::FileDialog::getExistingDirectory(cDir.c_str()).latin1();
265
if (cDir!="" && cDir[cDir.size()-1] != PATHSEP)
268
std::string cFullName = cDir+"FreeCAD.pov";
269
Base::Console().Log("Using file name:%s",cFullName.c_str());
271
// open the file and write
272
std::ofstream fout(cFullName.c_str());
277
bool CmdRaytracingQuickRender::isActive(void)
279
//if( getActiveDocument() )
286
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
289
void CreateRaytracingCommands(void)
291
Gui::CommandManager &rcCmdMgr = Gui::Application::Instance->commandManager();
292
rcCmdMgr.addCommand(new CmdRaytracingWriteCamera());
293
rcCmdMgr.addCommand(new CmdRaytracingWritePart());
294
rcCmdMgr.addCommand(new CmdRaytracingNewProject());