2 * ParachuteHandler.java
4 package net.sf.openrocket.file.rocksim.importt;
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 * @param warnings the warning set
37 * @throws IllegalArgumentException thrown if <code>c</code> is null
39 public ParachuteHandler(RocketComponent c, WarningSet warnings) throws IllegalArgumentException {
41 throw new IllegalArgumentException("The parent of a parachute may not be null.");
43 chute = new Parachute();
44 if (isCompatible(c, Parachute.class, warnings)) {
53 public ElementHandler openElement(String element, HashMap<String, String> attributes, WarningSet warnings) {
54 return PlainTextHandler.INSTANCE;
61 public void closeElement(String element, HashMap<String, String> attributes, String content, WarningSet warnings)
63 super.closeElement(element, attributes, content, warnings);
65 if ("Dia".equals(element)) {
66 chute.setDiameter(Double.parseDouble(content) / RocksimHandler.ROCKSIM_TO_OPENROCKET_LENGTH);
67 /* Rocksim doesn't have a packed parachute radius, so we approximate it. */
69 RocketComponent parent = chute.getParent();
70 if (parent instanceof BodyTube) {
71 packed = ((BodyTube) parent).getOuterRadius() * 0.9;
73 else if (parent instanceof InnerTube) {
74 packed = ((InnerTube) parent).getInnerRadius() * 0.9;
77 packed = chute.getDiameter() * 0.025;
79 chute.setRadius(packed);
81 if ("ShroudLineCount".equals(element)) {
82 chute.setLineCount(Math.max(0, Integer.parseInt(content)));
84 if ("ShroudLineLen".equals(element)) {
85 chute.setLineLength(Math.max(0, Double.parseDouble(content) / RocksimHandler.ROCKSIM_TO_OPENROCKET_LENGTH));
87 if ("SpillHoleDia".equals(element)) {
88 //Not supported in OpenRocket
89 double spillHoleRadius = Double.parseDouble(content) / RocksimHandler.ROCKSIM_TO_OPENROCKET_RADIUS;
90 warnings.add("Parachute spill holes are not supported. Ignoring.");
92 if ("ShroudLineMassPerMM".equals(element)) {
93 shroudLineDensity = Double.parseDouble(content) / RocksimHandler.ROCKSIM_TO_OPENROCKET_LINE_DENSITY;
95 if ("ShroudLineMaterial".equals(element)) {
96 chute.setLineMaterial(createCustomMaterial(Material.Type.LINE, content, shroudLineDensity));
98 if ("DragCoefficient".equals(element)) {
99 chute.setCD(Double.parseDouble(content));
101 if ("Material".equals(element)) {
102 setMaterialName(content);
105 catch (NumberFormatException nfe) {
106 warnings.add("Could not convert " + element + " value of " + content + ". It is expected to be a number.");
111 * Get the component this handler is working upon.
113 * @return a component
115 public Parachute getComponent() {