~ubuntu-branches/ubuntu/maverick/freecad/maverick

« back to all changes in this revision

Viewing changes to src/Mod/Raytracing/Gui/Command.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Teemu Ikonen
  • Date: 2009-07-16 18:37:41 UTC
  • Revision ID: james.westby@ubuntu.com-20090716183741-oww9kcxqrk991i1n
Tags: upstream-0.8.2237
ImportĀ upstreamĀ versionĀ 0.8.2237

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
 *   Copyright (c) Jļæ½rgen Riegel          (juergen.riegel@web.de) 2002     *
 
3
 *                                                                         *
 
4
 *   This file is part of the FreeCAD CAx development system.              *
 
5
 *                                                                         *
 
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.      *
 
10
 *                                                                         *
 
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.                  *
 
15
 *                                                                         *
 
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                                *
 
20
 *                                                                         *
 
21
 ***************************************************************************/
 
22
 
 
23
 
 
24
#include "PreCompiled.h"
 
25
#ifndef _PreComp_
 
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>
 
31
#endif
 
32
 
 
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>
 
40
#include <Gui/View.h>
 
41
 
 
42
#include <Mod/Part/App/PartFeature.h>
 
43
  
 
44
#include "FreeCADpov.h"
 
45
 
 
46
 
 
47
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 
48
 
 
49
 
 
50
//===========================================================================
 
51
// CmdRaytracingWriteCamera
 
52
//===========================================================================
 
53
DEF_STD_CMD_A(CmdRaytracingWriteCamera);
 
54
 
 
55
CmdRaytracingWriteCamera::CmdRaytracingWriteCamera()
 
56
  :Command("Raytracing_WriteCamera")
 
57
{
 
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;
 
64
    sPixmap       = "Test1";
 
65
    iAccel        = 0;
 
66
}
 
67
 
 
68
void CmdRaytracingWriteCamera::activated(int iMsg)
 
69
{
 
70
    const char* ppReturn=0;
 
71
 
 
72
    getGuiApplication()->sendMsgToActiveView("GetCamera",&ppReturn);
 
73
 
 
74
    Base::Console().Log("GetCamera MSG send:\n%s",ppReturn);
 
75
 
 
76
    SoInput in;
 
77
    in.setBuffer((void*)ppReturn,std::strlen(ppReturn));
 
78
 
 
79
    //if (!in.openFile(filename)) { exit(1); }
 
80
 
 
81
    SoNode* rootNode;
 
82
    SoDB::read(&in,rootNode);
 
83
 
 
84
    if (!rootNode || !rootNode->getTypeId().isDerivedFrom(SoCamera::getClassTypeId()))
 
85
        throw Base::Exception("CmdRaytracingWriteCamera::activated(): Could not read "
 
86
                              "camera information from ASCII stream....\n");
 
87
 
 
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);
 
92
    Cam->ref();
 
93
 
 
94
    SbRotation camrot = Cam->orientation.getValue();
 
95
 
 
96
    SbVec3f upvec(0, 1, 0); // init to default up vector
 
97
    camrot.multVec(upvec, upvec);
 
98
 
 
99
    SbVec3f lookat(0, 0, -1); // init to default view direction vector
 
100
    camrot.multVec(lookat, lookat);
 
101
 
 
102
    SbVec3f pos = Cam->position.getValue();
 
103
    float Dist = Cam->focalDistance.getValue();
 
104
 
 
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");
 
109
 
 
110
    if (cDir!="" && cDir[cDir.size()-1] != PATHSEP)
 
111
        cDir += PATHSEP;
 
112
    std::string cFullName = cDir+cCameraName;
 
113
 
 
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] <<")," ;
 
119
    lookat *= Dist;
 
120
    lookat += pos;
 
121
    out << "(" << lookat.getValue()[0] <<"," << lookat.getValue()[1] <<"," << lookat.getValue()[2] <<")," 
 
122
        << "(" << upvec.getValue()[0]  <<"," << upvec.getValue()[1]  <<"," << upvec.getValue()[2]  <<") )" ;
 
123
 
 
124
    doCommand(Doc,"import Raytracing");
 
125
    doCommand(Gui,out.str().c_str());
 
126
 
 
127
 
 
128
    // Bring ref-count of root-node back to zero to cause the
 
129
    // destruction of the camera.
 
130
    Cam->unref();
 
131
}
 
132
 
 
133
bool CmdRaytracingWriteCamera::isActive(void)
 
134
{
 
135
    return getGuiApplication()->sendHasMsgToActiveView("GetCamera");
 
136
}
 
137
 
 
138
//===========================================================================
 
139
// CmdRaytracingWritePart
 
140
//===========================================================================
 
141
DEF_STD_CMD_A(CmdRaytracingWritePart);
 
142
 
 
143
CmdRaytracingWritePart::CmdRaytracingWritePart()
 
144
  :Command("Raytracing_WritePart")
 
145
{
 
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;
 
152
    sPixmap       = "Test1";
 
153
    iAccel        = 0;
 
154
}
 
155
 
 
156
void CmdRaytracingWritePart::activated(int iMsg)
 
157
{
 
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);
 
164
 
 
165
    // name of the objects in the pov file
 
166
    std::string Name = "Part";
 
167
 
 
168
    if (cDir!="" && cDir[cDir.size()-1] != PATHSEP)
 
169
        cDir += PATHSEP;
 
170
 
 
171
    std::string cFullName = cDir+cPartName;
 
172
 
 
173
    std::stringstream out;
 
174
    //Raytracing.writePartFile(App.document().GetActiveFeature().getShape())
 
175
    out << "Raytracing.writePartFile(\"" << strToPython(cFullName) << "\",\"" << Name << "\",App.ActiveDocument.ActiveObject.Shape)";
 
176
 
 
177
    doCommand(Doc,"import Raytracing");
 
178
    doCommand(Doc,out.str().c_str());
 
179
}
 
180
 
 
181
bool CmdRaytracingWritePart::isActive(void)
 
182
{
 
183
    if (getActiveGuiDocument()) {
 
184
        App::DocumentObject* obj = getActiveGuiDocument()->getDocument()->getActiveObject();
 
185
        if (obj && obj->getTypeId().isDerivedFrom(Part::Feature::getClassTypeId()))
 
186
            return true;
 
187
    }
 
188
 
 
189
    return false;
 
190
}
 
191
 
 
192
 
 
193
//===========================================================================
 
194
// CmdRaytracingNewProject
 
195
//===========================================================================
 
196
DEF_STD_CMD_A(CmdRaytracingNewProject);
 
197
 
 
198
CmdRaytracingNewProject::CmdRaytracingNewProject()
 
199
  :Command("Raytracing_NewProject")
 
200
{
 
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;
 
207
    sPixmap       = "Test1";
 
208
    iAccel        = 0;
 
209
}
 
210
 
 
211
void CmdRaytracingNewProject::activated(int iMsg)
 
212
{
 
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";
 
220
 
 
221
    if (cDir!="" && cDir[cDir.size()-1] != PATHSEP)
 
222
        cDir += PATHSEP;
 
223
    std::string cFullName = cDir+cPovRayName;
 
224
 
 
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());
 
229
}
 
230
 
 
231
bool CmdRaytracingNewProject::isActive(void)
 
232
{
 
233
    //if( getActiveDocument() )
 
234
        return true;
 
235
    //else
 
236
    //  return false;
 
237
}
 
238
 
 
239
//===========================================================================
 
240
// CmdRaytracingQuickRender
 
241
//===========================================================================
 
242
DEF_STD_CMD_A(CmdRaytracingQuickRender);
 
243
 
 
244
CmdRaytracingQuickRender::CmdRaytracingQuickRender()
 
245
  :Command("Raytracing_QuickRender")
 
246
{
 
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;
 
253
    sPixmap       = "Test1";
 
254
    iAccel        = 0;
 
255
}
 
256
 
 
257
void CmdRaytracingQuickRender::activated(int iMsg)
 
258
{
 
259
    // get the preferences
 
260
    ParameterGrp::handle hGrp = App::GetApplication().GetParameterGroupByPath("User parameter:BaseApp/Preferences/Mod/Raytracing");
 
261
    std::string cDir             = hGrp->GetASCII("ProjectPath", "");
 
262
 
 
263
    //cDir = Gui::FileDialog::getExistingDirectory(cDir.c_str()).latin1();
 
264
 
 
265
    if (cDir!="" && cDir[cDir.size()-1] != PATHSEP)
 
266
        cDir += PATHSEP;
 
267
 
 
268
    std::string cFullName = cDir+"FreeCAD.pov";
 
269
    Base::Console().Log("Using file name:%s",cFullName.c_str());
 
270
 
 
271
    // open the file and write
 
272
    std::ofstream fout(cFullName.c_str());
 
273
    fout << FreeCAD ;
 
274
    fout.close();
 
275
}
 
276
 
 
277
bool CmdRaytracingQuickRender::isActive(void)
 
278
{
 
279
    //if( getActiveDocument() )
 
280
        return true;
 
281
    //else
 
282
    //  return false;
 
283
}
 
284
 
 
285
 
 
286
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 
287
 
 
288
 
 
289
void CreateRaytracingCommands(void)
 
290
{
 
291
    Gui::CommandManager &rcCmdMgr = Gui::Application::Instance->commandManager();
 
292
    rcCmdMgr.addCommand(new CmdRaytracingWriteCamera());
 
293
    rcCmdMgr.addCommand(new CmdRaytracingWritePart());
 
294
    rcCmdMgr.addCommand(new CmdRaytracingNewProject());
 
295
}