2 * ParachuteHandler.java
4 package net.sf.openrocket.file.rocksim;
6 import net.sf.openrocket.aerodynamics.WarningSet;
7 import net.sf.openrocket.file.simplesax.ElementHandler;
8 import net.sf.openrocket.file.simplesax.PlainTextHandler;
9 import net.sf.openrocket.material.Material;
10 import net.sf.openrocket.rocketcomponent.BodyTube;
11 import net.sf.openrocket.rocketcomponent.InnerTube;
12 import net.sf.openrocket.rocketcomponent.Parachute;
13 import net.sf.openrocket.rocketcomponent.RocketComponent;
14 import org.xml.sax.SAXException;
16 import java.util.HashMap;
19 * A SAX handler for Rocksim's Parachute XML type.
21 class ParachuteHandler extends RecoveryDeviceHandler<Parachute> {
23 * The OpenRocket Parachute instance
25 private final Parachute chute;
27 * The shroud line density.
29 private double shroudLineDensity = 0.0d;
34 * @param c the parent component
35 * @throws IllegalArgumentException thrown if <code>c</code> is null
37 public ParachuteHandler(RocketComponent c) throws IllegalArgumentException {
39 throw new IllegalArgumentException("The parent of a parachute may not be null.");
41 chute = new Parachute();
49 public ElementHandler openElement(String element, HashMap<String, String> attributes, WarningSet warnings) {
50 return PlainTextHandler.INSTANCE;
57 public void closeElement(String element, HashMap<String, String> attributes, String content, WarningSet warnings)
59 super.closeElement(element, attributes, content, warnings);
61 if ("Dia".equals(element)) {
62 chute.setDiameter(Double.parseDouble(content) / RocksimHandler.ROCKSIM_TO_OPENROCKET_LENGTH);
63 /* Rocksim doesn't have a packed parachute radius, so we approximate it. */
65 RocketComponent parent = chute.getParent();
66 if (parent instanceof BodyTube) {
67 packed = ((BodyTube) parent).getRadius() * 0.9;
69 else if (parent instanceof InnerTube) {
70 packed = ((InnerTube) parent).getInnerRadius() * 0.9;
73 packed = chute.getDiameter() * 0.025;
75 chute.setRadius(packed);
77 if ("ShroudLineCount".equals(element)) {
78 chute.setLineCount(Math.max(0, Integer.parseInt(content)));
80 if ("ShroudLineLen".equals(element)) {
81 chute.setLineLength(Math.max(0, Double.parseDouble(content) / RocksimHandler.ROCKSIM_TO_OPENROCKET_LENGTH));
83 if ("SpillHoleDia".equals(element)) {
84 //Not supported in OpenRocket
85 double spillHoleRadius = Double.parseDouble(content) / RocksimHandler.ROCKSIM_TO_OPENROCKET_RADIUS;
86 warnings.add("Parachute spill holes are not supported. Ignoring.");
88 if ("ShroudLineMassPerMM".equals(element)) {
89 shroudLineDensity = Double.parseDouble(content) / RocksimHandler.ROCKSIM_TO_OPENROCKET_LINE_DENSITY;
91 if ("ShroudLineMaterial".equals(element)) {
92 chute.setLineMaterial(BaseHandler.createCustomMaterial(Material.Type.LINE, content, shroudLineDensity));
94 if ("DragCoefficient".equals(element)) {
95 chute.setCD(Double.parseDouble(content));
97 if ("Material".equals(element)) {
98 setMaterialName(content);
101 catch (NumberFormatException nfe) {
102 warnings.add("Could not convert " + element + " value of " + content + ". It is expected to be a number.");
107 * Get the component this handler is working upon.
109 * @return a component
111 public Parachute getComponent() {