| 1 |
#include "printToPS.h" |
| 2 |
|
| 3 |
#include <Inventor/SbViewportRegion.h> |
| 4 |
#include <Inventor/SoOffscreenRenderer.h> |
| 5 |
|
| 6 |
#if defined(__COIN__) && COIN_MAJOR_VERSION >= 2 && COIN_MINOR_VERSION >= 1 |
| 7 |
#include <Inventor/annex/HardCopy/SoVectorizePSAction.h> |
| 8 |
#endif |
| 9 |
|
| 10 |
SbBool |
| 11 |
printToPostScript (SoNode *root, const char *filename, |
| 12 |
#ifdef USE_SOQT |
| 13 |
SoQtExaminerViewer *viewer, int printerDPI) |
| 14 |
#else |
| 15 |
SoXtExaminerViewer *viewer, int printerDPI) |
| 16 |
#endif |
| 17 |
{ |
| 18 |
#if defined(__COIN__) && COIN_MAJOR_VERSION >= 2 && COIN_MINOR_VERSION >= 1 |
| 19 |
SoVectorizePSAction * ps = new SoVectorizePSAction; |
| 20 |
SoVectorOutput * out = ps->getOutput(); |
| 21 |
|
| 22 |
if (!out->openFile(filename)) { |
| 23 |
return FALSE; // unable to open output file |
| 24 |
} |
| 25 |
|
| 26 |
// to enable gouraud shading. 0.1 is a nice epsilon value |
| 27 |
//ps->setGouraudThreshold(0.1f); |
| 28 |
|
| 29 |
// clear to white background. Not really necessary if you |
| 30 |
// want a white background |
| 31 |
ps->setBackgroundColor(TRUE, SbColor(1.0f, 1.0f, 1.0f)); |
| 32 |
|
| 33 |
// select LANDSCAPE or PORTRAIT orientation |
| 34 |
//ps->setOrientation(SoVectorizeAction::LANDSCAPE); |
| 35 |
ps->setOrientation(SoVectorizeAction::PORTRAIT); |
| 36 |
|
| 37 |
// start creating a new page (A4 page, with 10mm border). |
| 38 |
//ps->beginPage(SbVec2f(10.0f, 10.0f), SbVec2f(190.0f, 277.0f)); |
| 39 |
// start creating a new page (15x15 cm, no border). |
| 40 |
ps->beginPage(SbVec2f(0.0f, 0.0f), SbVec2f(150.0f, 150.0f)); |
| 41 |
|
| 42 |
// There are also enums for A0-A10. Example: |
| 43 |
// ps->beginStandardPage(SoVectorizeAction::A4, 10.0f); |
| 44 |
|
| 45 |
// calibrate so that text, lines, points and images will have the |
| 46 |
// same size in the postscript file as on the monitor. |
| 47 |
ps->calibrate(viewer->getViewportRegion()); |
| 48 |
|
| 49 |
// apply action on the viewer scenegraph. Remember to use |
| 50 |
// SoSceneManager's scene graph so that the camera is included. |
| 51 |
ps->apply(viewer->getSceneManager()->getSceneGraph()); |
| 52 |
|
| 53 |
// this will create the postscript file |
| 54 |
ps->endPage(); |
| 55 |
|
| 56 |
// close file |
| 57 |
out->closeFile(); |
| 58 |
|
| 59 |
delete ps; |
| 60 |
#else |
| 61 |
FILE *file = fopen(filename,"w"); |
| 62 |
if (file == NULL) |
| 63 |
return FALSE; |
| 64 |
const SbViewportRegion &vp = viewer->getViewportRegion(); |
| 65 |
const SbVec2s &imagePixSize = vp.getViewportSizePixels(); |
| 66 |
SbVec2f imageInches; |
| 67 |
float pixPerInch; |
| 68 |
|
| 69 |
pixPerInch = SoOffscreenRenderer::getScreenPixelsPerInch(); |
| 70 |
imageInches.setValue((float)imagePixSize[0] / pixPerInch, |
| 71 |
(float)imagePixSize[1] / pixPerInch); |
| 72 |
|
| 73 |
SbVec2s postScriptRes; |
| 74 |
postScriptRes.setValue((short)(imageInches[0]*printerDPI), |
| 75 |
(short)(imageInches[1]*printerDPI)); |
| 76 |
|
| 77 |
// Create a viewport to render the scene into. |
| 78 |
SbViewportRegion myViewport; |
| 79 |
myViewport.setWindowSize(postScriptRes); |
| 80 |
myViewport.setPixelsPerInch((float)printerDPI); |
| 81 |
|
| 82 |
// Render the scene |
| 83 |
SoOffscreenRenderer *myRenderer = |
| 84 |
new SoOffscreenRenderer(myViewport); |
| 85 |
if (!myRenderer->render(root)) |
| 86 |
{ |
| 87 |
delete myRenderer; |
| 88 |
return FALSE; |
| 89 |
} |
| 90 |
|
| 91 |
// Generate PostScript and write it to the given file |
| 92 |
myRenderer->writeToPostScript(file); |
| 93 |
fclose(file); |
| 94 |
|
| 95 |
delete myRenderer; |
| 96 |
#endif |
| 97 |
return TRUE; |
| 98 |
} |