4 package net.sf.openrocket.gui.print;
6 import java.awt.Graphics2D;
7 import java.io.IOException;
8 import java.text.DecimalFormat;
10 import net.sf.openrocket.document.OpenRocketDocument;
11 import net.sf.openrocket.document.Simulation;
12 import net.sf.openrocket.gui.figureelements.FigureElement;
13 import net.sf.openrocket.gui.figureelements.RocketInfo;
14 import net.sf.openrocket.gui.scalefigure.RocketPanel;
15 import net.sf.openrocket.gui.util.SwingPreferences;
16 import net.sf.openrocket.logging.LogHelper;
17 import net.sf.openrocket.masscalc.BasicMassCalculator;
18 import net.sf.openrocket.masscalc.MassCalculator;
19 import net.sf.openrocket.masscalc.MassCalculator.MassCalcType;
20 import net.sf.openrocket.motor.Motor;
21 import net.sf.openrocket.rocketcomponent.Configuration;
22 import net.sf.openrocket.rocketcomponent.MotorMount;
23 import net.sf.openrocket.rocketcomponent.Rocket;
24 import net.sf.openrocket.rocketcomponent.RocketComponent;
25 import net.sf.openrocket.rocketcomponent.Stage;
26 import net.sf.openrocket.simulation.FlightData;
27 import net.sf.openrocket.simulation.exception.SimulationException;
28 import net.sf.openrocket.startup.Application;
29 import net.sf.openrocket.unit.Unit;
30 import net.sf.openrocket.unit.UnitGroup;
31 import net.sf.openrocket.util.Chars;
32 import net.sf.openrocket.util.Coordinate;
34 import com.itextpdf.text.Document;
35 import com.itextpdf.text.DocumentException;
36 import com.itextpdf.text.Element;
37 import com.itextpdf.text.Paragraph;
38 import com.itextpdf.text.Rectangle;
39 import com.itextpdf.text.pdf.BaseFont;
40 import com.itextpdf.text.pdf.DefaultFontMapper;
41 import com.itextpdf.text.pdf.PdfContentByte;
42 import com.itextpdf.text.pdf.PdfPCell;
43 import com.itextpdf.text.pdf.PdfPTable;
44 import com.itextpdf.text.pdf.PdfWriter;
48 * # Title # Section describing the rocket in general without motors
49 * # Section describing the rocket in general without motors
54 * CP position at 5 degree AOA (or similar)
56 * parachute/streamer sizes
57 * max. diameter (caliber)
58 * velocity at exit of rail/rod
59 * minimum safe velocity reached in x inches/cm
61 * # Section for each motor configuration
63 * a summary of the motors, e.g. 3xC6-0; B4-6
64 * a list of the motors including the manufacturer, designation (maybe also info like burn time, grams of propellant,
66 * total grams of propellant
69 * CG and CP position, stability margin
70 * predicted flight altitude, max. velocity and max. acceleration
71 * predicted velocity at chute deployment
72 * predicted descent rate
73 * Thrust to Weight Ratio of each stage
77 public class DesignReport {
82 private static final LogHelper log = Application.getLogger();
87 private OpenRocketDocument rocketDocument;
90 * A panel used for rendering of the design diagram.
92 final RocketPanel panel;
97 protected Document document;
99 /** The displayed strings. */
100 private static final String STAGES = "Stages: ";
101 private static final String MASS_WITH_MOTORS = "Mass (with motors): ";
102 private static final String MASS_WITH_MOTOR = "Mass (with motor): ";
103 private static final String MASS_EMPTY = "Mass (Empty): ";
104 private static final String STABILITY = "Stability: ";
105 private static final String CG = "CG: ";
106 private static final String CP = "CP: ";
107 private static final String MOTOR = "Motor";
108 private static final String AVG_THRUST = "Avg Thrust";
109 private static final String BURN_TIME = "Burn Time";
110 private static final String MAX_THRUST = "Max Thrust";
111 private static final String TOTAL_IMPULSE = "Total Impulse";
112 private static final String THRUST_TO_WT = "Thrust to Wt";
113 private static final String PROPELLANT_WT = "Propellant Wt";
114 private static final String SIZE = "Size";
115 private static final String ALTITUDE = "Altitude";
116 private static final String FLIGHT_TIME = "Flight Time";
117 private static final String TIME_TO_APOGEE = "Time to Apogee";
118 private static final String VELOCITY_OFF_PAD = "Velocity off Pad";
119 private static final String MAX_VELOCITY = "Max Velocity";
120 private static final String LANDING_VELOCITY = "Landing Velocity";
121 private static final String ROCKET_DESIGN = "Rocket Design";
122 private static final double GRAVITY_CONSTANT = 9.80665d;
127 * @param theRocDoc the OR document
128 * @param theIDoc the iText document
130 public DesignReport(OpenRocketDocument theRocDoc, Document theIDoc) {
132 rocketDocument = theRocDoc;
133 panel = new RocketPanel(rocketDocument);
137 * Main entry point. Prints the rocket drawing and design data.
139 * @param writer a direct byte writer
141 public void writeToDocument(PdfWriter writer) {
142 if (writer == null) {
145 com.itextpdf.text.Rectangle pageSize = document.getPageSize();
146 int pageImageableWidth = (int) pageSize.getWidth() - (int) pageSize.getBorderWidth() * 2;
147 int pageImageableHeight = (int) pageSize.getHeight() / 2 - (int) pageSize.getBorderWidthTop();
149 PrintUtilities.addText(document, PrintUtilities.BIG_BOLD, ROCKET_DESIGN);
151 Rocket rocket = rocketDocument.getRocket();
152 final Configuration configuration = rocket.getDefaultConfiguration().clone();
153 configuration.setAllStages();
154 PdfContentByte canvas = writer.getDirectContent();
156 final PrintFigure figure = new PrintFigure(configuration);
158 FigureElement cp = panel.getExtraCP();
159 FigureElement cg = panel.getExtraCG();
160 RocketInfo text = panel.getExtraText();
162 double scale = paintRocketDiagram(pageImageableWidth, pageImageableHeight, canvas, figure, cp, cg);
166 canvas.setFontAndSize(BaseFont.createFont(PrintUtilities.NORMAL.getFamilyname(), BaseFont.CP1252,
167 BaseFont.EMBEDDED), PrintUtilities.NORMAL_FONT_SIZE);
168 } catch (DocumentException e) {
169 log.error("Could not set font.", e);
170 } catch (IOException e) {
171 log.error("Could not create font.", e);
173 int figHeightPts = (int) (PrintUnit.METERS.toPoints(figure.getFigureHeight()) * 0.4 * (scale / PrintUnit.METERS
175 final int diagramHeight = pageImageableHeight * 2 - 70 - (figHeightPts);
176 canvas.moveText(document.leftMargin() + pageSize.getBorderWidthLeft(), diagramHeight);
177 canvas.moveTextWithLeading(0, -16);
179 float initialY = canvas.getYTLM();
181 canvas.showText(rocketDocument.getRocket().getName());
183 canvas.newlineShowText(STAGES);
184 canvas.showText("" + rocket.getStageCount());
187 if (configuration.hasMotors()) {
188 if (configuration.getStageCount() > 1) {
189 canvas.newlineShowText(MASS_WITH_MOTORS);
191 canvas.newlineShowText(MASS_WITH_MOTOR);
194 canvas.newlineShowText(MASS_EMPTY);
196 canvas.showText(text.getMass(UnitGroup.UNITS_MASS.getDefaultUnit()));
198 canvas.newlineShowText(STABILITY);
199 canvas.showText(text.getStability());
201 canvas.newlineShowText(CG);
202 canvas.showText(text.getCg());
204 canvas.newlineShowText(CP);
205 canvas.showText(text.getCp());
209 //Move the internal pointer of the document below that of what was just written using the direct byte buffer.
210 Paragraph paragraph = new Paragraph();
211 float finalY = canvas.getYTLM();
212 int heightOfDiagramAndText = (int) (pageSize.getHeight() - (finalY - initialY + diagramHeight));
214 paragraph.setSpacingAfter(heightOfDiagramAndText);
215 document.add(paragraph);
217 String[] motorIds = rocket.getMotorConfigurationIDs();
219 for (int j = 0; j < motorIds.length; j++) {
220 String motorId = motorIds[j];
221 if (motorId != null) {
222 PdfPTable parent = new PdfPTable(2);
223 parent.setWidthPercentage(100);
224 parent.setHorizontalAlignment(Element.ALIGN_LEFT);
225 parent.setSpacingBefore(0);
226 parent.setWidths(new int[] { 1, 3 });
228 //The first motor config is always null. Skip it and the top-most motor, then set the leading.
232 addFlightData(rocket, motorId, parent, leading);
233 addMotorData(rocket, motorId, parent);
234 document.add(parent);
237 } catch (DocumentException e) {
238 log.error("Could not modify document.", e);
244 * Paint a diagram of the rocket into the PDF document.
246 * @param thePageImageableWidth the number of points in the width of the page available for drawing
247 * @param thePageImageableHeight the number of points in the height of the page available for drawing
248 * @param theCanvas the direct byte writer
249 * @param theFigure the print figure
250 * @param theCp the center of pressure figure element
251 * @param theCg the center of gravity figure element
253 * @return the scale of the diagram
255 private double paintRocketDiagram(final int thePageImageableWidth, final int thePageImageableHeight,
256 final PdfContentByte theCanvas, final PrintFigure theFigure,
257 final FigureElement theCp, final FigureElement theCg) {
258 theFigure.clearAbsoluteExtra();
259 theFigure.clearRelativeExtra();
260 theFigure.addRelativeExtra(theCp);
261 theFigure.addRelativeExtra(theCg);
262 theFigure.updateFigure();
265 (thePageImageableWidth * 2.2) / theFigure.getFigureWidth();
267 theFigure.setScale(scale);
269 * page dimensions are in points-per-inch, which, in Java2D, are the same as pixels-per-inch; thus we don't need any conversion
271 theFigure.setSize(thePageImageableWidth, thePageImageableHeight);
272 theFigure.updateFigure();
275 final DefaultFontMapper mapper = new DefaultFontMapper();
276 Graphics2D g2d = theCanvas.createGraphics(thePageImageableWidth, thePageImageableHeight * 2, mapper);
277 g2d.translate(20, 120);
279 g2d.scale(0.4d, 0.4d);
280 theFigure.paint(g2d);
286 * Add the motor data for a motor configuration to the table.
288 * @param rocket the rocket
289 * @param motorId the motor ID to output
290 * @param parent the parent to which the motor data will be added
292 private void addMotorData(Rocket rocket, String motorId, final PdfPTable parent) {
294 PdfPTable motorTable = new PdfPTable(8);
295 motorTable.setWidthPercentage(68);
296 motorTable.setHorizontalAlignment(Element.ALIGN_LEFT);
298 final PdfPCell motorCell = ITextHelper.createCell(MOTOR, PdfPCell.BOTTOM);
300 motorCell.setPaddingLeft(mPad);
301 motorTable.addCell(motorCell);
302 motorTable.addCell(ITextHelper.createCell(AVG_THRUST, PdfPCell.BOTTOM));
303 motorTable.addCell(ITextHelper.createCell(BURN_TIME, PdfPCell.BOTTOM));
304 motorTable.addCell(ITextHelper.createCell(MAX_THRUST, PdfPCell.BOTTOM));
305 motorTable.addCell(ITextHelper.createCell(TOTAL_IMPULSE, PdfPCell.BOTTOM));
306 motorTable.addCell(ITextHelper.createCell(THRUST_TO_WT, PdfPCell.BOTTOM));
307 motorTable.addCell(ITextHelper.createCell(PROPELLANT_WT, PdfPCell.BOTTOM));
308 motorTable.addCell(ITextHelper.createCell(SIZE, PdfPCell.BOTTOM));
310 DecimalFormat ttwFormat = new DecimalFormat("0.00");
312 MassCalculator massCalc = new BasicMassCalculator();
314 Configuration config = new Configuration(rocket);
315 config.setMotorConfigurationID(motorId);
317 int totalMotorCount = 0;
318 double totalPropMass = 0;
319 double totalImpulse = 0;
323 double stageMass = 0;
325 boolean topBorder = false;
326 for (RocketComponent c : rocket) {
328 if (c instanceof Stage) {
329 config.setToStage(stage);
331 stageMass = massCalc.getCG(config, MassCalcType.LAUNCH_MASS).weight;
332 // Calculate total thrust-to-weight from only lowest stage motors
337 if (c instanceof MotorMount && ((MotorMount) c).isMotorMount()) {
338 MotorMount mount = (MotorMount) c;
340 if (mount.isMotorMount() && mount.getMotor(motorId) != null) {
341 Motor motor = mount.getMotor(motorId);
342 int motorCount = c.toAbsolute(Coordinate.NUL).length;
345 int border = Rectangle.NO_BORDER;
347 border = Rectangle.TOP;
351 String name = motor.getDesignation();
352 if (motorCount > 1) {
353 name += " (" + Chars.TIMES + motorCount + ")";
356 final PdfPCell motorVCell = ITextHelper.createCell(name, border);
357 motorVCell.setPaddingLeft(mPad);
358 motorTable.addCell(motorVCell);
359 motorTable.addCell(ITextHelper.createCell(
360 UnitGroup.UNITS_FORCE.getDefaultUnit().toStringUnit(motor.getAverageThrustEstimate()), border));
361 motorTable.addCell(ITextHelper.createCell(
362 UnitGroup.UNITS_FLIGHT_TIME.getDefaultUnit().toStringUnit(motor.getBurnTimeEstimate()), border));
363 motorTable.addCell(ITextHelper.createCell(
364 UnitGroup.UNITS_FORCE.getDefaultUnit().toStringUnit(motor.getMaxThrustEstimate()), border));
365 motorTable.addCell(ITextHelper.createCell(
366 UnitGroup.UNITS_IMPULSE.getDefaultUnit().toStringUnit(motor.getTotalImpulseEstimate()), border));
368 double ttw = motor.getAverageThrustEstimate() / (stageMass * GRAVITY_CONSTANT);
369 motorTable.addCell(ITextHelper.createCell(
370 ttwFormat.format(ttw) + ":1", border));
372 double propMass = (motor.getLaunchCG().weight - motor.getEmptyCG().weight);
373 motorTable.addCell(ITextHelper.createCell(
374 UnitGroup.UNITS_MASS.getDefaultUnit().toStringUnit(propMass), border));
376 final Unit motorUnit = UnitGroup.UNITS_MOTOR_DIMENSIONS.getDefaultUnit();
377 motorTable.addCell(ITextHelper.createCell(motorUnit.toString(motor.getDiameter()) +
379 motorUnit.toString(motor.getLength()) + " " +
380 motorUnit.toString(), border));
382 // Sum up total count
383 totalMotorCount += motorCount;
384 totalPropMass += propMass * motorCount;
385 totalImpulse += motor.getTotalImpulseEstimate() * motorCount;
386 totalTTW += ttw * motorCount;
391 if (totalMotorCount > 1) {
392 int border = Rectangle.TOP;
393 final PdfPCell motorVCell = ITextHelper.createCell("Total:", border);
394 motorVCell.setPaddingLeft(mPad);
395 motorTable.addCell(motorVCell);
396 motorTable.addCell(ITextHelper.createCell("", border));
397 motorTable.addCell(ITextHelper.createCell("", border));
398 motorTable.addCell(ITextHelper.createCell("", border));
399 motorTable.addCell(ITextHelper.createCell(
400 UnitGroup.UNITS_IMPULSE.getDefaultUnit().toStringUnit(totalImpulse), border));
401 motorTable.addCell(ITextHelper.createCell(
402 ttwFormat.format(totalTTW) + ":1", border));
403 motorTable.addCell(ITextHelper.createCell(
404 UnitGroup.UNITS_MASS.getDefaultUnit().toStringUnit(totalPropMass), border));
405 motorTable.addCell(ITextHelper.createCell("", border));
409 PdfPCell c = new PdfPCell(motorTable);
410 c.setBorder(PdfPCell.LEFT);
411 c.setBorderWidthTop(0f);
417 * Add the motor data for a motor configuration to the table.
419 * @param theRocket the rocket
420 * @param motorId a motor configuration id
421 * @param parent the parent to which the motor data will be added
422 * @param leading the number of points for the leading
424 private void addFlightData(final Rocket theRocket, final String motorId, final PdfPTable parent, int leading) {
426 // Perform flight simulation
427 Rocket duplicate = theRocket.copyWithOriginalID();
428 FlightData flight = null;
430 Simulation simulation = ((SwingPreferences)Application.getPreferences()).getBackgroundSimulation(duplicate);
431 simulation.getOptions().setMotorConfigurationID(motorId);
432 simulation.simulate();
433 flight = simulation.getSimulatedData();
434 } catch (SimulationException e1) {
438 // Output the flight data
439 if (flight != null) {
441 final Unit distanceUnit = UnitGroup.UNITS_DISTANCE.getDefaultUnit();
442 final Unit velocityUnit = UnitGroup.UNITS_VELOCITY.getDefaultUnit();
443 final Unit flightTimeUnit = UnitGroup.UNITS_FLIGHT_TIME.getDefaultUnit();
445 PdfPTable labelTable = new PdfPTable(2);
446 labelTable.setWidths(new int[] { 3, 2 });
447 final Paragraph chunk = ITextHelper.createParagraph(stripBrackets(
448 theRocket.getMotorConfigurationNameOrDescription(motorId)), PrintUtilities.BOLD);
449 chunk.setLeading(leading);
450 chunk.setSpacingAfter(3f);
454 final PdfPCell cell = ITextHelper.createCell(ALTITUDE, 2, 2);
455 cell.setUseBorderPadding(false);
456 cell.setBorderWidthTop(0f);
457 labelTable.addCell(cell);
458 labelTable.addCell(ITextHelper.createCell(distanceUnit.toStringUnit(flight.getMaxAltitude()), 2, 2));
460 labelTable.addCell(ITextHelper.createCell(FLIGHT_TIME, 2, 2));
461 labelTable.addCell(ITextHelper.createCell(flightTimeUnit.toStringUnit(flight.getFlightTime()), 2, 2));
463 labelTable.addCell(ITextHelper.createCell(TIME_TO_APOGEE, 2, 2));
464 labelTable.addCell(ITextHelper.createCell(flightTimeUnit.toStringUnit(flight.getTimeToApogee()), 2, 2));
466 labelTable.addCell(ITextHelper.createCell(VELOCITY_OFF_PAD, 2, 2));
467 labelTable.addCell(ITextHelper.createCell(velocityUnit.toStringUnit(flight.getLaunchRodVelocity()), 2, 2));
469 labelTable.addCell(ITextHelper.createCell(MAX_VELOCITY, 2, 2));
470 labelTable.addCell(ITextHelper.createCell(velocityUnit.toStringUnit(flight.getMaxVelocity()), 2, 2));
472 labelTable.addCell(ITextHelper.createCell(LANDING_VELOCITY, 2, 2));
473 labelTable.addCell(ITextHelper.createCell(velocityUnit.toStringUnit(flight.getGroundHitVelocity()), 2, 2));
475 //Add the table to the parent; have to wrap it in a cell
476 PdfPCell c = new PdfPCell(labelTable);
477 c.setBorder(PdfPCell.RIGHT);
478 c.setBorderWidthTop(0);
481 } catch (DocumentException e) {
482 log.error("Could not add flight data to document.", e);
488 * Strip [] brackets from a string.
490 * @param target the original string
492 * @return target with [] removed
494 private String stripBrackets(String target) {
495 return stripLeftBracket(stripRightBracket(target));
499 * Strip [ from a string.
501 * @param target the original string
503 * @return target with [ removed
505 private String stripLeftBracket(String target) {
506 return target.replace("[", "");
510 * Strip ] from a string.
512 * @param target the original string
514 * @return target with ] removed
516 private String stripRightBracket(String target) {
517 return target.replace("]", "");