DGP - bug fix for MassObject/Shock Cord
authorrodinia814 <rodinia814@180e2498-e6e9-4542-8430-84ac67f01cd8>
Mon, 23 Jan 2012 03:39:47 +0000 (03:39 +0000)
committerrodinia814 <rodinia814@180e2498-e6e9-4542-8430-84ac67f01cd8>
Mon, 23 Jan 2012 03:39:47 +0000 (03:39 +0000)
git-svn-id: https://openrocket.svn.sourceforge.net/svnroot/openrocket/trunk@366 180e2498-e6e9-4542-8430-84ac67f01cd8

core/src/net/sf/openrocket/file/rocksim/importt/MassObjectHandler.java
core/test/net/sf/openrocket/file/rocksim/importt/MassObjectHandlerTest.java

index 178d96dc14b8824a795cb67f0ae2a52c00a4897d..ee8cd22db4aeb7977234aa6632c1c9cbd12b6a0f 100644 (file)
@@ -9,7 +9,9 @@ import net.sf.openrocket.file.simplesax.ElementHandler;
 import net.sf.openrocket.file.simplesax.PlainTextHandler;
 import net.sf.openrocket.material.Material;
 import net.sf.openrocket.rocketcomponent.MassComponent;
+import net.sf.openrocket.rocketcomponent.MassObject;
 import net.sf.openrocket.rocketcomponent.RocketComponent;
+import net.sf.openrocket.rocketcomponent.ShockCord;
 import org.xml.sax.SAXException;
 
 import java.util.HashMap;
@@ -17,7 +19,7 @@ import java.util.HashMap;
 /**
  * A SAX handler for Rocksim's MassObject XML type.
  */
-class MassObjectHandler extends PositionDependentHandler<MassComponent> {
+class MassObjectHandler extends PositionDependentHandler<MassObject> {
 
     /** 
      * The Rocksim Mass length fudge factor.  Rocksim completely exaggerates the length of a mass object to the point
@@ -33,6 +35,21 @@ class MassObjectHandler extends PositionDependentHandler<MassComponent> {
      */
     private final MassComponent mass;
 
+    /**
+     * Reference to answer for getComponent().
+     */
+    private MassObject current;
+
+    /**
+     * Parent.
+     */
+    private RocketComponent parent;
+
+    /**
+     * 0 == General, 1 == Shock Cord
+     */
+    private int typeCode = 0;
+
     /**
      * Constructor.
      *l
@@ -46,9 +63,8 @@ class MassObjectHandler extends PositionDependentHandler<MassComponent> {
             throw new IllegalArgumentException("The parent component of a mass component may not be null.");
         }
         mass = new MassComponent();
-        if (isCompatible(c, MassComponent.class, warnings)) {
-            c.addChild(mass);
-        }
+        current = mass;
+        parent = c;
     }
 
     @Override
@@ -62,7 +78,7 @@ class MassObjectHandler extends PositionDependentHandler<MassComponent> {
         super.closeElement(element, attributes, content, warnings);
         try {
             if (RocksimCommonConstants.LEN.equals(element)) {
-                mass.setLength(Double.parseDouble(content) / (RocksimCommonConstants.ROCKSIM_TO_OPENROCKET_LENGTH * MASS_LEN_FUDGE_FACTOR));
+                mass.setLength(Double.parseDouble(content) / (RocksimCommonConstants.ROCKSIM_TO_OPENROCKET_LENGTH));
             }
             if (RocksimCommonConstants.KNOWN_MASS.equals(element)) {
                 mass.setComponentMass(Double.parseDouble(content) / RocksimCommonConstants.ROCKSIM_TO_OPENROCKET_MASS);
@@ -75,20 +91,56 @@ class MassObjectHandler extends PositionDependentHandler<MassComponent> {
                 //Thus it needs to be set to 0 to say that the mass object's CG is at the point of the mass object.
                 super.setCG(0); 
             }
+            if (RocksimCommonConstants.TYPE_CODE.equals(element)) {
+                typeCode = Integer.parseInt(content);
+            }
+            if (RocksimCommonConstants.MATERIAL.equals(element)) {
+                setMaterialName(content);
+            }
         }
         catch (NumberFormatException nfe) {
             warnings.add("Could not convert " + element + " value of " + content + ".  It is expected to be a number.");
         }
     }
 
+    @Override
+    public void endHandler(String element, HashMap<String, String> attributes, String content, WarningSet warnings) throws SAXException {
+        if (typeCode == 0) { //General Mass Object
+            if (isCompatible(parent, MassComponent.class, warnings)) {
+                parent.addChild(mass);
+            }
+            super.endHandler(element, attributes, content, warnings);
+        }
+        else if (typeCode == 1) { //Shock Cord
+            ShockCord cord = new ShockCord();
+            current = cord;
+            if (isCompatible(parent, ShockCord.class, warnings)) {
+                parent.addChild(cord);
+            }
+            super.endHandler(element, attributes, content, warnings);
+            cord.setName(mass.getName());
+
+            setOverride(cord, mass.isMassOverridden(), mass.getOverrideMass(), mass.getOverrideCGX());
+
+            cord.setRadialDirection(mass.getRadialDirection());
+            cord.setRadialPosition(mass.getRadialPosition());
+            cord.setRadius(mass.getRadius());
+
+            //Rocksim does not distinguish between total length of the cord and the packed length.  Fudge the
+            //packed length and set the real length.
+            cord.setCordLength(mass.getLength());
+            cord.setLength(cord.getCordLength()/MASS_LEN_FUDGE_FACTOR);
+        }
+    }
+
     /**
-     * Get the component this handler is working upon.
+     * Get the component this handler is working upon.  This changes depending upon the type of mass object.
      *
      * @return a component
      */
     @Override
-    public MassComponent getComponent() {
-        return mass;
+    public MassObject getComponent() {
+        return current;
     }
 
     /**
@@ -98,17 +150,17 @@ class MassObjectHandler extends PositionDependentHandler<MassComponent> {
      * @param position the OpenRocket position
      */
     public void setRelativePosition(RocketComponent.Position position) {
-        mass.setRelativePosition(position);
+        current.setRelativePosition(position);
     }
 
     /**
-     * Get the required type of material for this component.  Does not apply to MassComponents.
+     * Get the required type of material for this component.  Does not apply to MassComponents, but does apply to Shock Cords.
      *
-     * @return BULK
+     * @return LINE
      */
     @Override
     public Material.Type getMaterialType() {
-        return Material.Type.BULK;
+        return Material.Type.LINE;
     }
 
 }
index fa435229f4bd4c110cd538b7b77c1423546e0c9e..89cf327abf7105913a04c29d6bb0f5f538d6b9ab 100644 (file)
@@ -38,8 +38,9 @@ public class MassObjectHandlerTest extends RocksimTestBase {
 
         BodyTube tube = new BodyTube();
         MassObjectHandler handler = new MassObjectHandler(tube, new WarningSet());
-        MassComponent component = (MassComponent) getField(handler, "mass");
-        assertContains(component, tube.getChildren());
+        MassComponent mass = (MassComponent) getField(handler, "mass");
+        MassComponent current = (MassComponent) getField(handler, "current");
+        Assert.assertEquals(mass, current);
     }
 
     /**
@@ -70,10 +71,10 @@ public class MassObjectHandlerTest extends RocksimTestBase {
         handler.closeElement("Len", attributes, "-1", warnings);
         Assert.assertEquals(0d, component.getLength(), 0.001);
         handler.closeElement("Len", attributes, "10", warnings);
-        Assert.assertEquals(10d / (MassObjectHandler.MASS_LEN_FUDGE_FACTOR * RocksimCommonConstants.ROCKSIM_TO_OPENROCKET_LENGTH)
+        Assert.assertEquals(0.01
                 , component.getLength(), 0.001);
         handler.closeElement("Len", attributes, "10.0", warnings);
-        Assert.assertEquals(10d / (MassObjectHandler.MASS_LEN_FUDGE_FACTOR * RocksimCommonConstants.ROCKSIM_TO_OPENROCKET_LENGTH)
+        Assert.assertEquals(0.01
                 , component.getLength(), 0.001);
         handler.closeElement("Len", attributes, "foo", warnings);
         Assert.assertEquals(1, warnings.size());
@@ -120,6 +121,6 @@ public class MassObjectHandlerTest extends RocksimTestBase {
      */
     @org.junit.Test
     public void testGetMaterialType() throws Exception {
-        Assert.assertEquals(Material.Type.BULK, new MassObjectHandler(new BodyTube(), new WarningSet()).getMaterialType());
+        Assert.assertEquals(Material.Type.LINE, new MassObjectHandler(new BodyTube(), new WarningSet()).getMaterialType());
     }
 }