Printable Centering Ring templates.

This commit is contained in:
Doug Pedrick 2012-05-24 23:28:58 +00:00
parent e754dab0ca
commit c89c927e0c
9 changed files with 485 additions and 199 deletions

View File

@ -1356,6 +1356,7 @@ OpenRocketPrintable.Transitiontemplates = Transition templates
OpenRocketPrintable.Noseconetemplates = Nose Cone templates OpenRocketPrintable.Noseconetemplates = Nose Cone templates
OpenRocketPrintable.Finmarkingguide = Fin marking guide OpenRocketPrintable.Finmarkingguide = Fin marking guide
OpenRocketPrintable.DesignReport = Design Report OpenRocketPrintable.DesignReport = Design Report
OpenRocketPrintable.Centeringringtemplates = Centering Ring templates
OpenRocketDocument.Redo = Redo OpenRocketDocument.Redo = Redo
OpenRocketDocument.Undo = Undo OpenRocketDocument.Undo = Undo

View File

@ -1,16 +1,18 @@
package net.sf.openrocket.gui.print; package net.sf.openrocket.gui.print;
import net.sf.openrocket.rocketcomponent.Transition;
import javax.swing.*;
import java.awt.*; import java.awt.*;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
public abstract class AbstractPrintableTransition extends PrintableComponent { public abstract class AbstractPrintable<T> extends PrintableComponent {
/** /**
* The stroke of the transition arc. * A thin stroke.
*/ */
private final static BasicStroke thinStroke = new BasicStroke(1.0f); public final static BasicStroke thinStroke = new BasicStroke(1.0f);
/**
* A thick stroke.
*/
public final static BasicStroke thickStroke = new BasicStroke(4.0f);
/** /**
* The X margin. * The X margin.
@ -28,7 +30,7 @@ public abstract class AbstractPrintableTransition extends PrintableComponent {
* @param isDoubleBuffered a boolean, true for double-buffering * @param isDoubleBuffered a boolean, true for double-buffering
* @param transition the component to be printed * @param transition the component to be printed
*/ */
public AbstractPrintableTransition(boolean isDoubleBuffered, Transition transition) { public AbstractPrintable(boolean isDoubleBuffered, T transition) {
init(transition); init(transition);
} }
@ -39,7 +41,7 @@ public abstract class AbstractPrintableTransition extends PrintableComponent {
* *
* @param component the transition component * @param component the transition component
*/ */
protected abstract void init(Transition component); protected abstract void init(T component);
/** /**
* Draw the component onto the graphics context. * Draw the component onto the graphics context.
@ -52,7 +54,7 @@ public abstract class AbstractPrintableTransition extends PrintableComponent {
* Returns a generated image of the transition. May then be used wherever AWT images can be used, or converted to * Returns a generated image of the transition. May then be used wherever AWT images can be used, or converted to
* another image/picture format and used accordingly. * another image/picture format and used accordingly.
* *
* @return an awt image of the fin set * @return an awt image of the transition
*/ */
public Image createImage() { public Image createImage() {
int width = getWidth() + marginX; int width = getWidth() + marginX;

View File

@ -22,10 +22,12 @@ public enum OpenRocketPrintable {
NOSE_CONE_TEMPLATE("OpenRocketPrintable.Noseconetemplates", false, 3), NOSE_CONE_TEMPLATE("OpenRocketPrintable.Noseconetemplates", false, 3),
// Transition Templates // Transition Templates
TRANSITION_TEMPLATE("OpenRocketPrintable.Transitiontemplates", false, 4), TRANSITION_TEMPLATE("OpenRocketPrintable.Transitiontemplates", false, 4),
// Centering Ring Templates
CENTERING_RING_TEMPLATE("OpenRocketPrintable.Centeringringtemplates", false, 5),
// Finset shape // Finset shape
FIN_TEMPLATE("OpenRocketPrintable.Fintemplates", true, 5), FIN_TEMPLATE("OpenRocketPrintable.Fintemplates", true, 6),
// Fin marking guide. // Fin marking guide.
FIN_MARKING_GUIDE("OpenRocketPrintable.Finmarkingguide", false, 6); FIN_MARKING_GUIDE("OpenRocketPrintable.Finmarkingguide", false, 7);
private static final Translator trans = Application.getTranslator(); private static final Translator trans = Application.getTranslator();

View File

@ -12,9 +12,10 @@ import com.itextpdf.text.pdf.PdfBoolean;
import com.itextpdf.text.pdf.PdfName; import com.itextpdf.text.pdf.PdfName;
import com.itextpdf.text.pdf.PdfWriter; import com.itextpdf.text.pdf.PdfWriter;
import net.sf.openrocket.document.OpenRocketDocument; import net.sf.openrocket.document.OpenRocketDocument;
import net.sf.openrocket.gui.print.visitor.PageFitPrintStrategy; import net.sf.openrocket.gui.print.visitor.CenteringRingStrategy;
import net.sf.openrocket.gui.print.visitor.FinMarkingGuideStrategy; import net.sf.openrocket.gui.print.visitor.FinMarkingGuideStrategy;
import net.sf.openrocket.gui.print.visitor.FinSetPrintStrategy; import net.sf.openrocket.gui.print.visitor.FinSetPrintStrategy;
import net.sf.openrocket.gui.print.visitor.PageFitPrintStrategy;
import net.sf.openrocket.gui.print.visitor.PartsDetailVisitorStrategy; import net.sf.openrocket.gui.print.visitor.PartsDetailVisitorStrategy;
import net.sf.openrocket.gui.print.visitor.TransitionStrategy; import net.sf.openrocket.gui.print.visitor.TransitionStrategy;
@ -52,7 +53,8 @@ public class PrintController {
try { try {
idoc.open(); idoc.open();
Thread.sleep(1000); Thread.sleep(1000);
} catch (InterruptedException e) { }
catch (InterruptedException e) {
} }
// Used to combine multiple components onto fewer sheets of paper // Used to combine multiple components onto fewer sheets of paper
@ -91,6 +93,13 @@ public class PrintController {
idoc.newPage(); idoc.newPage();
break; break;
case CENTERING_RING_TEMPLATE:
final CenteringRingStrategy crWriter = new CenteringRingStrategy(idoc, writer, stages,
pageFitPrint);
crWriter.writeToDocument(doc.getRocket());
idoc.newPage();
break;
case FIN_MARKING_GUIDE: case FIN_MARKING_GUIDE:
final FinMarkingGuideStrategy fmg = new FinMarkingGuideStrategy(idoc, writer); final FinMarkingGuideStrategy fmg = new FinMarkingGuideStrategy(idoc, writer);
fmg.writeToDocument(doc.getRocket()); fmg.writeToDocument(doc.getRocket());
@ -108,13 +117,17 @@ public class PrintController {
} }
writer.close(); writer.close();
idoc.close(); idoc.close();
} catch (DocumentException e) { }
} catch (ExceptionConverter ec) { catch (DocumentException e) {
} finally { }
catch (ExceptionConverter ec) {
}
finally {
if (outputFile != null) { if (outputFile != null) {
try { try {
outputFile.close(); outputFile.close();
} catch (IOException e) { }
catch (IOException e) {
} }
} }
} }
@ -124,6 +137,7 @@ public class PrintController {
* Get the correct paper size from the print settings. * Get the correct paper size from the print settings.
* *
* @param settings the print settings * @param settings the print settings
*
* @return the paper size * @return the paper size
*/ */
private Rectangle getSize(PrintSettings settings) { private Rectangle getSize(PrintSettings settings) {

View File

@ -0,0 +1,104 @@
package net.sf.openrocket.gui.print;
import net.sf.openrocket.rocketcomponent.CenteringRing;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.geom.Ellipse2D;
/**
* This class creates a renderable centering ring. It depends only on AWT/Swing and can be called from other
* actors (like iText handlers) to render the centering ring on different graphics contexts.
*/
public class PrintableCenteringRing extends AbstractPrintable<CenteringRing> {
/**
* If the component to be drawn is a centering ring, save a reference to it.
*/
private CenteringRing target;
/**
* The X margin.
*/
protected int marginX = (int) PrintUnit.INCHES.toPoints(0.25f);
/**
* The Y margin.
*/
protected int marginY = (int) PrintUnit.INCHES.toPoints(0.25f);
/**
* The line length of the cross hairs.
*/
private final int lineLength = 10;
/**
* Construct a printable nose cone.
*
* @param theRing the component to print
*/
public PrintableCenteringRing(CenteringRing theRing) {
super(false, theRing);
}
/**
* @param component the centering ring component
*/
@Override
protected void init(final CenteringRing component) {
target = component;
double radius = target.getOuterRadius();
setSize((int) PrintUnit.METERS.toPoints(2 * radius) + marginX,
(int) PrintUnit.METERS.toPoints(2 * radius) + marginY);
}
/**
* Draw a centering ring.
*
* @param g2 the graphics context
*/
@Override
protected void draw(Graphics2D g2) {
double radius = PrintUnit.METERS.toPoints(target.getOuterRadius());
Color original = g2.getBackground();
double x = marginX;
double y = marginY;
Shape outerCircle = new Ellipse2D.Double(x, y, radius * 2, radius * 2);
g2.setColor(Color.lightGray);
g2.fill(outerCircle);
g2.setColor(Color.black);
g2.draw(outerCircle);
x += radius;
y += radius;
double innerRadius = PrintUnit.METERS.toPoints(target.getInnerRadius());
Shape innerCircle = new Ellipse2D.Double(x - innerRadius, y - innerRadius, innerRadius * 2, innerRadius * 2);
g2.setColor(original);
g2.fill(innerCircle);
g2.setColor(Color.black);
g2.draw(innerCircle);
drawCross(g2, (int) x, (int) y, lineLength, lineLength);
}
/**
* Draw the center cross-hair.
*
* @param g the graphics context
* @param x the x coordinate of the center point
* @param y the y coordinate of the center point
* @param width the width in pixels of the horizontal hair
* @param height the width in pixels of the vertical hair
*/
private void drawCross(Graphics g, int x, int y, int width, int height) {
g.setColor(Color.black);
((Graphics2D) g).setStroke(thinStroke);
g.drawLine(x - width / 2, y, x + width / 2, y);
g.drawLine(x, y - height / 2, x, y + height / 2);
}
}

View File

@ -1,19 +1,14 @@
package net.sf.openrocket.gui.print; package net.sf.openrocket.gui.print;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.Shape;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import net.sf.openrocket.gui.rocketfigure.TransitionShapes; import net.sf.openrocket.gui.rocketfigure.TransitionShapes;
import net.sf.openrocket.rocketcomponent.NoseCone; import net.sf.openrocket.rocketcomponent.NoseCone;
import net.sf.openrocket.rocketcomponent.Transition;
import net.sf.openrocket.util.Transformation; import net.sf.openrocket.util.Transformation;
public class PrintableNoseCone extends AbstractPrintableTransition { import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.Shape;
public class PrintableNoseCone extends AbstractPrintable<NoseCone> {
/** /**
* If the component to be drawn is a nose cone, save a reference to it. * If the component to be drawn is a nose cone, save a reference to it.
@ -25,14 +20,14 @@ public class PrintableNoseCone extends AbstractPrintableTransition {
* *
* @param noseCone the component to print * @param noseCone the component to print
*/ */
public PrintableNoseCone(Transition noseCone) { public PrintableNoseCone(NoseCone noseCone) {
super(false, noseCone); super(false, noseCone);
} }
@Override @Override
protected void init(Transition component) { protected void init(NoseCone component) {
target = (NoseCone) component; target = component;
double radius = target.getForeRadius(); double radius = target.getForeRadius();
if (radius < target.getAftRadius()) { if (radius < target.getAftRadius()) {
radius = target.getAftRadius(); radius = target.getAftRadius();

View File

@ -1,18 +1,14 @@
package net.sf.openrocket.gui.print; package net.sf.openrocket.gui.print;
import net.sf.openrocket.rocketcomponent.Transition;
import java.awt.BasicStroke; import java.awt.BasicStroke;
import java.awt.Graphics;
import java.awt.Graphics2D; import java.awt.Graphics2D;
import java.awt.geom.Arc2D; import java.awt.geom.Arc2D;
import java.awt.geom.GeneralPath; import java.awt.geom.GeneralPath;
import java.awt.geom.Line2D; import java.awt.geom.Line2D;
import java.awt.geom.Path2D; import java.awt.geom.Path2D;
import java.awt.geom.Point2D; import java.awt.geom.Point2D;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import net.sf.openrocket.rocketcomponent.Transition;
/** /**
* This class allows for a Transition to be printable. It does so by decorating an existing transition (which will not be * This class allows for a Transition to be printable. It does so by decorating an existing transition (which will not be
@ -22,7 +18,7 @@ import net.sf.openrocket.rocketcomponent.Transition;
* Note: Currently nose cones are only supported by drawing the 2D projection of the profile. A more useful approach * Note: Currently nose cones are only supported by drawing the 2D projection of the profile. A more useful approach
* may be to draw a myriahedral projection that can be cut out and bent to form the shape. * may be to draw a myriahedral projection that can be cut out and bent to form the shape.
*/ */
public class PrintableTransition extends AbstractPrintableTransition { public class PrintableTransition extends AbstractPrintable<Transition> {
/** /**
* Dashed array value. * Dashed array value.

View File

@ -0,0 +1,188 @@
package net.sf.openrocket.gui.print.visitor;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfWriter;
import net.sf.openrocket.gui.print.AbstractPrintable;
import net.sf.openrocket.gui.print.ITextHelper;
import net.sf.openrocket.gui.print.PrintableCenteringRing;
import net.sf.openrocket.logging.LogHelper;
import net.sf.openrocket.rocketcomponent.CenteringRing;
import net.sf.openrocket.rocketcomponent.RocketComponent;
import net.sf.openrocket.startup.Application;
import java.awt.image.BufferedImage;
import java.util.List;
import java.util.Set;
/**
* A strategy for printing a centering ring to iText.
*/
public class CenteringRingStrategy {
/**
* The logger.
*/
private static final LogHelper log = Application.getLogger();
/**
* The iText document.
*/
protected Document document;
/**
* The direct iText writer.
*/
protected PdfWriter writer;
/**
* The stages selected.
*/
protected Set<Integer> stages;
/**
* Strategy for fitting multiple components onto a page.
*/
protected PageFitPrintStrategy pageFitPrint;
/**
* Constructor.
*
* @param doc The iText document
* @param theWriter The direct iText writer
* @param theStagesToVisit The stages to be visited by this strategy
*/
public CenteringRingStrategy(Document doc, PdfWriter theWriter, Set<Integer> theStagesToVisit, PageFitPrintStrategy pageFit) {
document = doc;
writer = theWriter;
stages = theStagesToVisit;
pageFitPrint = pageFit;
}
/**
* Recurse through the given rocket component.
*
* @param root the root component; all children will be visited recursively
*/
public void writeToDocument(final RocketComponent root) {
List<RocketComponent> rc = root.getChildren();
goDeep(rc);
}
/**
* Recurse through the given rocket component.
*
* @param theRc an array of rocket components; all children will be visited recursively
*/
protected void goDeep(final List<RocketComponent> theRc) {
for (RocketComponent rocketComponent : theRc) {
if (rocketComponent instanceof CenteringRing) {
render((CenteringRing) rocketComponent);
}
else if (rocketComponent.getChildCount() > 0) {
goDeep(rocketComponent.getChildren());
}
}
}
/**
* The core behavior of this visitor.
*
* @param component the object to extract info about; a graphical image of the centering ring shape is drawn to the
* document
*/
private void render(final CenteringRing component) {
try {
AbstractPrintable pfs;
pfs = new PrintableCenteringRing(component);
java.awt.Dimension size = pfs.getSize();
final Dimension pageSize = getPageSize();
if (fitsOnOnePage(pageSize, size.getWidth(), size.getHeight())) {
pageFitPrint.addComponent(pfs);
}
else {
BufferedImage image = (BufferedImage) pfs.createImage();
ITextHelper.renderImageAcrossPages(new Rectangle(pageSize.getWidth(), pageSize.getHeight()),
document, writer, image);
}
}
catch (DocumentException e) {
log.error("Could not render the centering ring.", e);
}
}
/**
* Determine if the image will fit on the given page.
*
* @param pageSize the page size
* @param wImage the width of the thing to be printed
* @param hImage the height of the thing to be printed
*
* @return true if the thing to be printed will fit on a single page
*/
private boolean fitsOnOnePage(Dimension pageSize, double wImage, double hImage) {
double wPage = pageSize.getWidth();
double hPage = pageSize.getHeight();
int wRatio = (int) Math.ceil(wImage / wPage);
int hRatio = (int) Math.ceil(hImage / hPage);
return wRatio <= 1.0d && hRatio <= 1.0d;
}
/**
* Get the dimensions of the paper page.
*
* @return an internal Dimension
*/
protected Dimension getPageSize() {
return new Dimension(document.getPageSize().getWidth(),
document.getPageSize().getHeight());
}
/**
* Convenience class to model a dimension.
*/
class Dimension {
/**
* Width, in points.
*/
public float width;
/**
* Height, in points.
*/
public float height;
/**
* Constructor.
*
* @param w width
* @param h height
*/
public Dimension(float w, float h) {
width = w;
height = h;
}
/**
* Get the width.
*
* @return the width
*/
public float getWidth() {
return width;
}
/**
* Get the height.
*
* @return the height
*/
public float getHeight() {
return height;
}
}
}

View File

@ -3,9 +3,8 @@ package net.sf.openrocket.gui.print.visitor;
import com.itextpdf.text.Document; import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException; import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Rectangle; import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfWriter; import com.itextpdf.text.pdf.PdfWriter;
import net.sf.openrocket.gui.print.AbstractPrintableTransition; import net.sf.openrocket.gui.print.AbstractPrintable;
import net.sf.openrocket.gui.print.ITextHelper; import net.sf.openrocket.gui.print.ITextHelper;
import net.sf.openrocket.gui.print.PrintableNoseCone; import net.sf.openrocket.gui.print.PrintableNoseCone;
import net.sf.openrocket.gui.print.PrintableTransition; import net.sf.openrocket.gui.print.PrintableTransition;
@ -15,7 +14,6 @@ import net.sf.openrocket.rocketcomponent.RocketComponent;
import net.sf.openrocket.rocketcomponent.Transition; import net.sf.openrocket.rocketcomponent.Transition;
import net.sf.openrocket.startup.Application; import net.sf.openrocket.startup.Application;
import java.awt.*;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
@ -103,9 +101,9 @@ public class TransitionStrategy {
*/ */
private void render(final Transition component) { private void render(final Transition component) {
try { try {
AbstractPrintableTransition pfs; AbstractPrintable pfs;
if (component instanceof NoseCone) { if (component instanceof NoseCone) {
pfs = new PrintableNoseCone(component); pfs = new PrintableNoseCone((NoseCone)component);
} else { } else {
pfs = new PrintableTransition(component); pfs = new PrintableTransition(component);
} }
@ -143,20 +141,6 @@ public class TransitionStrategy {
return wRatio <= 1.0d && hRatio <= 1.0d; return wRatio <= 1.0d && hRatio <= 1.0d;
} }
/**
* Print the transition.
*
* @param theTransition the printable transition
*/
/*private void printOnOnePage(final AbstractPrintableTransition theTransition) {
Dimension d = getPageSize();
PdfContentByte cb = writer.getDirectContent();
Graphics2D g2 = cb.createGraphics(d.width, d.height);
theTransition.print(g2);
g2.dispose();
document.newPage();
}*/
/** /**
* Get the dimensions of the paper page. * Get the dimensions of the paper page.
* *