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
39 * @param warnings the warning set
41 * @throws IllegalArgumentException thrown if <code>c</code> is null
43 public MassObjectHandler(RocketComponent c, WarningSet warnings) throws IllegalArgumentException {
45 throw new IllegalArgumentException("The parent component of a mass component may not be null.");
47 mass = new MassComponent();
48 if (isCompatible(c, MassComponent.class, warnings)) {
54 public ElementHandler openElement(String element, HashMap<String, String> attributes, WarningSet warnings) {
55 return PlainTextHandler.INSTANCE;
59 public void closeElement(String element, HashMap<String, String> attributes, String content, WarningSet warnings)
61 super.closeElement(element, attributes, content, warnings);
63 if ("Len".equals(element)) {
64 mass.setLength(Double.parseDouble(content) / (RocksimHandler.ROCKSIM_TO_OPENROCKET_LENGTH * MASS_LEN_FUDGE_FACTOR));
66 if ("KnownMass".equals(element)) {
67 mass.setComponentMass(Double.parseDouble(content) / RocksimHandler.ROCKSIM_TO_OPENROCKET_MASS);
69 if ("KnownCG".equals(element)) {
70 //Setting the CG of the Mass Object to 0 is important because of the different ways that Rocksim and
71 //OpenRocket treat mass objects. Rocksim treats them as points (even though the data file contains a
72 //length) and because Rocksim sets the CG of the mass object to really be relative to the front of
73 //the parent. But that value is already assumed in the position and position value for the component.
74 //Thus it needs to be set to 0 to say that the mass object's CG is at the point of the mass object.
78 catch (NumberFormatException nfe) {
79 warnings.add("Could not convert " + element + " value of " + content + ". It is expected to be a number.");
84 * Get the component this handler is working upon.
89 public MassComponent getComponent() {
94 * Set the relative position onto the component. This cannot be done directly because setRelativePosition is not
95 * public in all components.
97 * @param position the OpenRocket position
99 public void setRelativePosition(RocketComponent.Position position) {
100 mass.setRelativePosition(position);
104 * Get the required type of material for this component. Does not apply to MassComponents.
109 public Material.Type getMaterialType() {
110 return Material.Type.BULK;