00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
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
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
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
00104
00105
00106 SoCamera * Cam = static_cast<SoCamera*>(rootNode);
00107 Cam->ref();
00108
00109 SbRotation camrot = Cam->orientation.getValue();
00110
00111 SbVec3f upvec(0, 1, 0);
00112 camrot.multVec(upvec, upvec);
00113
00114 SbVec3f lookat(0, 0, -1);
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
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
00143
00144 Cam->unref();
00145 }
00146
00147 bool CmdRaytracingWriteCamera::isActive(void)
00148 {
00149 return getGuiApplication()->sendHasMsgToActiveView("GetCamera");
00150 }
00151
00152
00153
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
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
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
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
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
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
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
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
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
00444
00445
00446 DEF_STD_CMD_A(CmdRaytracingExportProject);
00447
00448 CmdRaytracingExportProject::CmdRaytracingExportProject()
00449 : Command("Raytracing_ExportProject")
00450 {
00451
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 }