2 * MassObjectHandler.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.MassComponent;
11 import net.sf.openrocket.rocketcomponent.RocketComponent;
12 import org.xml.sax.SAXException;
14 import java.util.HashMap;
17 * A SAX handler for Rocksim's MassObject XML type.
19 class MassObjectHandler extends PositionDependentHandler<MassComponent> {
22 * The Rocksim Mass length fudge factor. Rocksim completely exaggerates the length of a mass object to the point
23 * that it looks ridiculous in OpenRocket. This fudge factor is here merely to get the typical mass object to
24 * render in the OpenRocket UI with it's bounds mostly inside it's parent. The odd thing about it is that
25 * Rocksim does not expose the length of a mass object in the UI and actually treats mass objects as point objects -
26 * not 3 or even 2 dimensional.
28 public static final int MASS_LEN_FUDGE_FACTOR = 100;
31 * The OpenRocket MassComponent - counterpart to the RS MassObject.
33 private final MassComponent mass;
38 * @param c the parent component
40 * @throws IllegalArgumentException thrown if <code>c</code> is null
42 public MassObjectHandler(RocketComponent c) throws IllegalArgumentException {
44 throw new IllegalArgumentException("The parent component of a mass component may not be null.");
46 mass = new MassComponent();
51 public ElementHandler openElement(String element, HashMap<String, String> attributes, WarningSet warnings) {
52 return PlainTextHandler.INSTANCE;
56 public void closeElement(String element, HashMap<String, String> attributes, String content, WarningSet warnings)
58 super.closeElement(element, attributes, content, warnings);
60 if ("Len".equals(element)) {
61 mass.setLength(Double.parseDouble(content) / (RocksimHandler.ROCKSIM_TO_OPENROCKET_LENGTH * MASS_LEN_FUDGE_FACTOR));
63 if ("KnownMass".equals(element)) {
64 mass.setComponentMass(Double.parseDouble(content) / RocksimHandler.ROCKSIM_TO_OPENROCKET_MASS);
66 if ("KnownCG".equals(element)) {
67 //Setting the CG of the Mass Object to 0 is important because of the different ways that Rocksim and
68 //OpenRocket treat mass objects. Rocksim treats them as points (even though the data file contains a
69 //length) and because Rocksim sets the CG of the mass object to really be relative to the front of
70 //the parent. But that value is already assumed in the position and position value for the component.
71 //Thus it needs to be set to 0 to say that the mass object's CG is at the point of the mass object.
75 catch (NumberFormatException nfe) {
76 warnings.add("Could not convert " + element + " value of " + content + ". It is expected to be a number.");
81 * Get the component this handler is working upon.
86 public MassComponent getComponent() {
91 * Set the relative position onto the component. This cannot be done directly because setRelativePosition is not
92 * public in all components.
94 * @param position the OpenRocket position
96 public void setRelativePosition(RocketComponent.Position position) {
97 mass.setRelativePosition(position);
101 * Get the required type of material for this component. Does not apply to MassComponents.
106 public Material.Type getMaterialType() {
107 return Material.Type.BULK;