Debian Codegraph @

apps/examples/Mentor/CXX/09.1.Print.c++

raw
1 /*
2 *
3 * Copyright (C) 2000 Silicon Graphics, Inc. All Rights Reserved.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * Further, this software is distributed without any warranty that it is
16 * free of the rightful claim of any third person regarding infringement
17 * or the like. Any license provided herein, whether implied or
18 * otherwise, applies only to this software file. Patent licenses, if
19 * any, provided herein do not apply to combinations of this program with
20 * other software, or any other product whatsoever.
21 *
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with this library; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 *
26 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
27 * Mountain View, CA 94043, or:
28 *
29 * http://www.sgi.com
30 *
31 * For further information regarding this notice, see:
32 *
33 * http://oss.sgi.com/projects/GenInfo/NoticeExplan/
34 *
35 */
36
37 /*-----------------------------------------------------------
38 * This is an example from the Inventor Mentor,
39 * chapter 9, example 1.
40 *
41 * Printing example.
42 * Read in an Inventor file and display it in ExaminerViewer. Press
43 * the "p" key and the scene renders into a PostScript
44 * file for printing.
45 *----------------------------------------------------------*/
46
47 #include <cstdlib>
48 #include <Inventor/SbViewportRegion.h>
49 #include <Inventor/SoDB.h>
50 #include <Inventor/SoOffscreenRenderer.h>
51 #include <Inventor/events/SoKeyboardEvent.h>
52 #include <Inventor/nodes/SoSeparator.h>
53 #include <Inventor/nodes/SoEventCallback.h>
54 #include <Inventor/Xt/SoXt.h>
55 #include <Inventor/Xt/viewers/SoXtExaminerViewer.h>
56
57 typedef struct cbdata {
58 SoXtExaminerViewer *vwr;
59 char *filename;
60 SoNode *scene;
61 } callbackData;
62
63 //////////////////////////////////////////////////////////////
64 // CODE FOR The Inventor Mentor STARTS HERE
65
66 SbBool
67 printToPostScript (SoNode *root, FILE *file,
68 SoXtExaminerViewer *viewer, int printerDPI)
69 {
70 // Calculate size of the images in inches which is equal to
71 // the size of the viewport in pixels divided by the number
72 // of pixels per inch of the screen device. This size in
73 // inches will be the size of the Postscript image that will
74 // be generated.
75 const SbViewportRegion &vp = viewer->getViewportRegion();
76 const SbVec2s &imagePixSize = vp.getViewportSizePixels();
77 SbVec2f imageInches;
78 float pixPerInch;
79
80 pixPerInch = SoOffscreenRenderer::getScreenPixelsPerInch();
81 imageInches.setValue((float)imagePixSize[0] / pixPerInch,
82 (float)imagePixSize[1] / pixPerInch);
83
84 // The resolution to render the scene for the printer
85 // is equal to the size of the image in inches times
86 // the printer DPI;
87 SbVec2s postScriptRes;
88 postScriptRes.setValue((short)(imageInches[0]*printerDPI),
89 (short)(imageInches[1]*printerDPI));
90
91 // Create a viewport to render the scene into.
92 SbViewportRegion myViewport;
93 myViewport.setWindowSize(postScriptRes);
94 myViewport.setPixelsPerInch((float)printerDPI);
95
96 // Render the scene
97 SoOffscreenRenderer *myRenderer =
98 new SoOffscreenRenderer(myViewport);
99 if (!myRenderer->render(root)) {
100 delete myRenderer;
101 return FALSE;
102 }
103
104 // Generate PostScript and write it to the given file
105 myRenderer->writeToPostScript(file);
106
107 delete myRenderer;
108 return TRUE;
109 }
110
111 // CODE FOR The Inventor Mentor ENDS HERE
112 //////////////////////////////////////////////////////////////
113
114 static void
115 processKeyEvents( void *data, SoEventCallback *cb )
116 {
117 if (SO_KEY_PRESS_EVENT(cb->getEvent(), P)) {
118
119 callbackData *cbData = (callbackData *)data;
120 FILE *myFile = fopen(cbData->filename, "w");
121
122 if (myFile == NULL) {
123 fprintf(stderr, "Cannot open output file\n");
124 exit(1);
125 }
126
127 printf("Printing scene... ");
128 fflush(stdout);
129 if (!printToPostScript(cbData->scene, myFile,
130 cbData->vwr, 75)) {
131 fprintf(stderr, "Cannot print image\n");
132 fclose(myFile);
133 exit(1);
134 }
135
136 fclose(myFile);
137 printf(" ...done printing.\n");
138 fflush(stdout);
139 cb->setHandled();
140 }
141 }
142
143 int
144 main(int argc, char **argv)
145 {
146 // Initialize Inventor and Xt
147 Widget appWindow = SoXt::init(argv[0]);
148 if (appWindow == NULL)
149 exit(1);
150
151 // Verify the command line arguments
152 if (argc != 3) {
153 fprintf(stderr, "Usage: %s infile.iv outfile.ps\n", argv[0]);
154 exit(1);
155 }
156
157 printf("To print the scene: press the 'p' key while in picking mode\n");
158
159 // Make a scene containing an event callback node
160 SoSeparator *root = new SoSeparator;
161 SoEventCallback *eventCB = new SoEventCallback;
162 root->ref();
163 root->addChild(eventCB);
164
165 // Read the geometry from a file and add to the scene
166 SoInput myInput;
167 if (!myInput.openFile(argv[1]))
168 exit (1);
169 SoSeparator *geomObject = SoDB::readAll(&myInput);
170 if (geomObject == NULL)
171 exit (1);
172 root->addChild(geomObject);
173
174 SoXtExaminerViewer *viewer =
175 new SoXtExaminerViewer(appWindow, NULL, TRUE,
176 SoXtExaminerViewer::BUILD_ALL, SoXtExaminerViewer::EDITOR);
177 viewer->setSceneGraph(root);
178 viewer->setTitle("Print to PostScript");
179
180 // Setup the event callback data and routine for performing the print
181 callbackData *data = new callbackData;
182 data->vwr = viewer;
183 data->filename = argv[2];
184 data->scene = viewer->getSceneGraph();
185 eventCB->addEventCallback(SoKeyboardEvent::getClassTypeId(),
186 processKeyEvents, data);
187 viewer->show();
188
189 SoXt::show(appWindow);
190 SoXt::mainLoop();
191 }
192