4 package net.sf.openrocket.file.rocksim.importt;
6 import java.lang.reflect.InvocationTargetException;
7 import java.lang.reflect.Method;
8 import java.util.HashMap;
10 import net.sf.openrocket.aerodynamics.WarningSet;
11 import net.sf.openrocket.database.Databases;
12 import net.sf.openrocket.file.rocksim.RocksimCommonConstants;
13 import net.sf.openrocket.file.rocksim.RocksimDensityType;
14 import net.sf.openrocket.file.simplesax.AbstractElementHandler;
15 import net.sf.openrocket.material.Material;
16 import net.sf.openrocket.rocketcomponent.RocketComponent;
18 import org.xml.sax.SAXException;
21 * An abstract base class that handles common parsing. All Rocksim component handlers are subclassed from here.
23 * @param <C> the specific RocketComponent subtype for which the concrete handler can create
25 public abstract class BaseHandler<C extends RocketComponent> extends AbstractElementHandler {
28 * Prepend rocksim materials.
30 public static final String ROCKSIM_MATERIAL_PREFIX = "RS: ";
32 * The overridden mass.
34 private Double mass = 0d;
38 private Double cg = 0d;
40 * The density of the material in the component.
42 private Double density = 0d;
44 * The internal Rocksim density type.
46 private RocksimDensityType densityType = RocksimDensityType.ROCKSIM_BULK;
51 private String materialName = "";
54 * The SAX method called when the closing element tag is reached.
56 * @param element the element name.
57 * @param attributes attributes of the element.
58 * @param content the textual content of the element.
59 * @param warnings the warning set to store warnings in.
61 * @throws SAXException
65 public void closeElement(String element, HashMap<String, String> attributes, String content, WarningSet warnings)
67 final C component = getComponent();
69 if (RocksimCommonConstants.NAME.equals(element)) {
70 component.setName(content);
72 if (RocksimCommonConstants.KNOWN_MASS.equals(element)) {
73 mass = Math.max(0d, Double.parseDouble(content) / RocksimCommonConstants.ROCKSIM_TO_OPENROCKET_MASS);
75 if (RocksimCommonConstants.DENSITY.equals(element)) {
76 density = Math.max(0d, Double.parseDouble(content));
78 if (RocksimCommonConstants.KNOWN_CG.equals(element)) {
79 cg = Math.max(0d, Double.parseDouble(content) / RocksimCommonConstants.ROCKSIM_TO_OPENROCKET_LENGTH);
81 if (RocksimCommonConstants.USE_KNOWN_CG.equals(element)) { //Rocksim sets UseKnownCG to true to control the override of both cg and mass
82 boolean override = "1".equals(content);
83 setOverride(component, override, mass, cg);
85 if (RocksimCommonConstants.DENSITY_TYPE.equals(element)) {
86 densityType = RocksimDensityType.fromCode(Integer.parseInt(content));
88 } catch (NumberFormatException nfe) {
89 warnings.add("Could not convert " + element + " value of " + content + ". It is expected to be a number.");
97 public void endHandler(String element, HashMap<String, String> attributes, String content, WarningSet warnings)
99 /* Because of the order of XML elements in Rocksim, not all information is known at the time it really needs
100 to be acted upon. So we keep temporary instance variables to be used here at the end of the parsing.
102 density = computeDensity(densityType, density);
103 RocketComponent component = getComponent();
104 updateComponentMaterial(component, materialName, getMaterialType(), density);
108 * Compute the density. Rocksim does strange things with densities. For some streamer material it's in cubic,
109 * rather than square, units. In those cases it needs to be converted to an appropriate SURFACE material density.
110 * Some G10 fiberglass materials are in cubic units, other G10 fiberglass is in square units. And due to a Rocksim
111 * bug, some densities are 0 when they clearly should not be.
113 * This may be overridden for specific component density computations.
115 * @param type the rocksim density
116 * @param rawDensity the density as specified in the Rocksim design file
118 * @return a value in OpenRocket SURFACE density units
120 protected double computeDensity(RocksimDensityType type, double rawDensity) {
121 return rawDensity / type.asOpenRocket();
125 * If the Rocksim component does not override the mass, then create a Material based upon the density defined for
126 * that component. This *should* result in a consistent representation of Cg between Rocksim and OpenRocket.
128 * @param component the component
129 * @param type the type of the material
130 * @param density the density in g/cm^3
131 * @param definedMaterial the material that is currently defined on the component; used only to get the name as it
134 public static void updateComponentMaterial(RocketComponent component, String definedMaterial, Material.Type type,
136 if (definedMaterial != null) {
137 Material custom = createCustomMaterial(type, definedMaterial, density);
138 setMaterial(component, custom);
143 * Override the mass and Cg of the component.
145 * @param component the component
146 * @param override true if any override should happen
147 * @param mass the override mass
148 * @param cg the override cg
150 public static void setOverride(RocketComponent component, boolean override, double mass, double cg) {
152 component.setCGOverridden(override);
153 component.setMassOverridden(override);
154 component.setOverrideSubcomponents(false); //Rocksim does not support this type of override
155 component.setOverrideMass(mass);
156 component.setOverrideCGX(cg);
161 * Get the component this handler is working upon.
163 * @return a component
165 protected abstract C getComponent();
168 * Get the required type of material for this component.
170 * @return the required material type
172 protected abstract Material.Type getMaterialType();
175 * Some CG positions in Rocksim do not correspond to the CG position reference in OpenRocket.
177 * @param theCG the CG value to really use when overriding CG on the OpenRocket component
179 protected void setCG(double theCG) {
184 * Set the material name as specified in the Rocksim design file.
186 * @param content the material name
188 protected void setMaterialName(String content) {
189 materialName = content;
193 * Get the Rocksim enum of the component's density type.
195 * @return a Rocksim density type
197 protected RocksimDensityType getDensityType() {
202 * Add child to parent only if the child is compatible. Otherwise add to warning set.
204 * @param parent the parent component
205 * @param child the child component
206 * @param warnings the warning set
208 * @return true if the child is compatible with parent
210 protected static boolean isCompatible(RocketComponent parent, Class<? extends RocketComponent> child, WarningSet warnings) {
211 return isCompatible(parent, child, warnings, false);
215 * Add child to parent only if the child is compatible. Otherwise add to warning set.
217 * @param parent the parent component
218 * @param child the child component
219 * @param warnings the warning set
220 * @param suppressWarnings suppress warnings, just return the boolean
222 * @return true if the child is compatible with parent
224 protected static boolean isCompatible(RocketComponent parent, Class<? extends RocketComponent> child,
226 boolean suppressWarnings) {
227 if (!parent.isCompatible(child)) {
228 if (!suppressWarnings) {
229 warnings.add(child.getName() + " can not be attached to "
230 + parent.getComponentName() + ", ignoring component.");
240 * Create a custom material based on the density.
242 * @param type the type of the material
243 * @param name the name of the component
244 * @param density the density
246 * @return a Material instance
248 public static Material createCustomMaterial(Material.Type type, String name, double density) {
249 return Databases.findMaterial(type, name, density);
253 * Set the material onto an instance of RocketComponent. This is done because only some subtypes of RocketComponent
254 * have the setMaterial method. Unfortunately the supertype cannot be used.
256 * @param component the component who's material is to be set
257 * @param material the material to be set on the component (defined by getComponent())
259 private static void setMaterial(RocketComponent component, Material material) {
261 final Method method = getMethod(component, "setMaterial", new Class[] { Material.class });
262 if (method != null) {
263 method.invoke(component, material);
265 } catch (IllegalAccessException ignored) {
266 } catch (InvocationTargetException ignored) {
271 * Find a method by name and argument list.
273 * @param component the component who's material is to be set
274 * @param name the method name
275 * @param args the class types of the parameters
277 * @return the Method instance, or null
279 private static Method getMethod(RocketComponent component, String name, Class[] args) {
280 Method method = null;
282 method = component.getClass().getMethod(name, args);
283 } catch (NoSuchMethodException ignored) {