Merge pull request #2191 from SiboVG/issue-2083

[#2083] Account for units of measure in material database loader
This commit is contained in:
Sibo Van Gool 2023-04-22 23:11:28 +02:00 committed by GitHub
commit dbd08ea03f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 36 additions and 24 deletions

View File

@ -222,7 +222,7 @@ public class Databases {
* the provided name if unable to do so. * the provided name if unable to do so.
* *
* @param type the material type. * @param type the material type.
* @param baseName the base name of the material. * @param baseName the base name of the material.
* @param density the density of the material. * @param density the density of the material.
* @return the material object from the database or a new material. * @return the material object from the database or a new material.
*/ */

View File

@ -10,6 +10,7 @@ import javax.xml.bind.annotation.XmlRootElement;
import net.sf.openrocket.database.Databases; import net.sf.openrocket.database.Databases;
import net.sf.openrocket.material.Material; import net.sf.openrocket.material.Material;
import net.sf.openrocket.unit.Unit;
import net.sf.openrocket.util.Chars; import net.sf.openrocket.util.Chars;
/** /**
@ -18,7 +19,7 @@ import net.sf.openrocket.util.Chars;
@XmlRootElement(name = "Material") @XmlRootElement(name = "Material")
@XmlAccessorType(XmlAccessType.FIELD) @XmlAccessorType(XmlAccessType.FIELD)
public class MaterialDTO { public class MaterialDTO {
@XmlElement(name = "Name") @XmlElement(name = "Name")
private String name; private String name;
@XmlElement(name = "Density") @XmlElement(name = "Density")
@ -27,65 +28,65 @@ public class MaterialDTO {
private MaterialTypeDTO type; private MaterialTypeDTO type;
@XmlAttribute(name = "UnitsOfMeasure") @XmlAttribute(name = "UnitsOfMeasure")
private String uom; private String uom;
/** /**
* Default constructor. * Default constructor.
*/ */
public MaterialDTO() { public MaterialDTO() {
} }
public MaterialDTO(final Material theMaterial) { public MaterialDTO(final Material theMaterial) {
this(theMaterial.getName(), theMaterial.getDensity(), MaterialTypeDTO.asDTO(theMaterial.getType()), this(theMaterial.getName(), theMaterial.getDensity(), MaterialTypeDTO.asDTO(theMaterial.getType()),
theMaterial.getType().getUnitGroup().getDefaultUnit().toString()); theMaterial.getType().getUnitGroup().getDefaultUnit().toString());
} }
public MaterialDTO(final String theName, final double theDensity, final MaterialTypeDTO theType, final String theUom) { public MaterialDTO(final String theName, final double theDensity, final MaterialTypeDTO theType, final String theUom) {
name = theName; name = theName;
density = theDensity; density = theDensity;
type = theType; type = theType;
uom = theUom; uom = theUom;
} }
public String getName() { public String getName() {
return name; return name;
} }
public void setName(final String theName) { public void setName(final String theName) {
name = theName; name = theName;
} }
public double getDensity() { public double getDensity() {
return density; return density;
} }
public void setDensity(final double theDensity) { public void setDensity(final double theDensity) {
density = theDensity; density = theDensity;
} }
public MaterialTypeDTO getType() { public MaterialTypeDTO getType() {
return type; return type;
} }
public void setType(final MaterialTypeDTO theType) { public void setType(final MaterialTypeDTO theType) {
type = theType; type = theType;
} }
public String getUom() { public String getUom() {
return uom; return uom;
} }
public void setUom(final String theUom) { public void setUom(final String theUom) {
uom = theUom; uom = theUom;
} }
Material asMaterial() { Material asMaterial() {
return Databases.findMaterial(type.getORMaterialType(), name, density); return Databases.findMaterial(type.getORMaterialType(), name, density);
} }
/** /**
* Special directive to the JAXB system. After the object is parsed from xml, * Special directive to the JAXB system. After the object is parsed from xml,
* we replace the '2' with Chars.SQUARED, and '3' with Chars.CUBED. Just the * we replace the '2' with Chars.SQUARED, and '3' with Chars.CUBED. Just the
* opposite transformation as done in beforeMarshal. * opposite transformation as done in beforeMarshal.
* @param unmarshaller * @param unmarshaller
* @param parent * @param parent
@ -95,9 +96,15 @@ public class MaterialDTO {
if (uom != null) { if (uom != null) {
uom = uom.replace('2', Chars.SQUARED); uom = uom.replace('2', Chars.SQUARED);
uom = uom.replace('3', Chars.CUBED); uom = uom.replace('3', Chars.CUBED);
if (type != null) {
// The density value is stored in the XML file in the units of measure, but OR expects the density to be
// in SI units, so we need to convert it to SI units
Unit uomUnit = type.getORMaterialType().getUnitGroup().getUnit(getUom());
density = uomUnit.fromUnit(density);
}
} }
} }
/** /**
* Special directive to the JAXB system. Before the object is serialized into xml, * Special directive to the JAXB system. Before the object is serialized into xml,
* we strip out the special unicode characters for cubed and squared so they appear * we strip out the special unicode characters for cubed and squared so they appear

View File

@ -20,8 +20,7 @@ public enum MaterialTypeDTO {
public static MaterialTypeDTO asDTO(Material.Type targetType) { public static MaterialTypeDTO asDTO(Material.Type targetType) {
MaterialTypeDTO[] values = values(); MaterialTypeDTO[] values = values();
for (int i = 0; i < values.length; i++) { for (MaterialTypeDTO value : values) {
MaterialTypeDTO value = values[i];
if (value.corollary.equals(targetType)) { if (value.corollary.equals(targetType)) {
return value; return value;
} }

View File

@ -212,6 +212,7 @@ public class UnitGroup {
UNITS_DENSITY_BULK = new UnitGroup(); UNITS_DENSITY_BULK = new UnitGroup();
UNITS_DENSITY_BULK.addUnit(new GeneralUnit(1000, "g/cm" + CUBED)); UNITS_DENSITY_BULK.addUnit(new GeneralUnit(1000, "g/cm" + CUBED));
UNITS_DENSITY_BULK.addUnit(new GeneralUnit(1000999, "kg/cm" + CUBED));
UNITS_DENSITY_BULK.addUnit(new GeneralUnit(1000, "kg/dm" + CUBED)); UNITS_DENSITY_BULK.addUnit(new GeneralUnit(1000, "kg/dm" + CUBED));
UNITS_DENSITY_BULK.addUnit(new GeneralUnit(1, "kg/m" + CUBED)); UNITS_DENSITY_BULK.addUnit(new GeneralUnit(1, "kg/m" + CUBED));
UNITS_DENSITY_BULK.addUnit(new GeneralUnit(1729.99404, "oz/in" + CUBED)); UNITS_DENSITY_BULK.addUnit(new GeneralUnit(1729.99404, "oz/in" + CUBED));
@ -220,13 +221,18 @@ public class UnitGroup {
UNITS_DENSITY_SURFACE = new UnitGroup(); UNITS_DENSITY_SURFACE = new UnitGroup();
UNITS_DENSITY_SURFACE.addUnit(new GeneralUnit(10, "g/cm" + SQUARED)); UNITS_DENSITY_SURFACE.addUnit(new GeneralUnit(10, "g/cm" + SQUARED));
UNITS_DENSITY_SURFACE.addUnit(new GeneralUnit(0.001, "g/m" + SQUARED)); UNITS_DENSITY_SURFACE.addUnit(new GeneralUnit(0.001, "g/m" + SQUARED));
UNITS_DENSITY_SURFACE.addUnit(new GeneralUnit(10000, "kg/cm" + SQUARED));
UNITS_DENSITY_SURFACE.addUnit(new GeneralUnit(100, "kg/dm" + SQUARED));
UNITS_DENSITY_SURFACE.addUnit(new GeneralUnit(1, "kg/m" + SQUARED)); UNITS_DENSITY_SURFACE.addUnit(new GeneralUnit(1, "kg/m" + SQUARED));
UNITS_DENSITY_SURFACE.addUnit(new GeneralUnit(43.9418487, "oz/in" + SQUARED)); UNITS_DENSITY_SURFACE.addUnit(new GeneralUnit(43.9418487, "oz/in" + SQUARED));
UNITS_DENSITY_SURFACE.addUnit(new GeneralUnit(0.305151727, "oz/ft" + SQUARED)); UNITS_DENSITY_SURFACE.addUnit(new GeneralUnit(0.305151727, "oz/ft" + SQUARED));
UNITS_DENSITY_SURFACE.addUnit(new GeneralUnit(4.88242764, "lb/ft" + SQUARED)); UNITS_DENSITY_SURFACE.addUnit(new GeneralUnit(4.88242764, "lb/ft" + SQUARED));
UNITS_DENSITY_LINE = new UnitGroup(); UNITS_DENSITY_LINE = new UnitGroup();
UNITS_DENSITY_LINE.addUnit(new GeneralUnit(0.1, "g/cm"));
UNITS_DENSITY_LINE.addUnit(new GeneralUnit(0.001, "g/m")); UNITS_DENSITY_LINE.addUnit(new GeneralUnit(0.001, "g/m"));
UNITS_DENSITY_LINE.addUnit(new GeneralUnit(100, "kg/cm"));
UNITS_DENSITY_LINE.addUnit(new GeneralUnit(10, "kg/dm"));
UNITS_DENSITY_LINE.addUnit(new GeneralUnit(1, "kg/m")); UNITS_DENSITY_LINE.addUnit(new GeneralUnit(1, "kg/m"));
UNITS_DENSITY_LINE.addUnit(new GeneralUnit(0.0930102465, "oz/ft")); UNITS_DENSITY_LINE.addUnit(new GeneralUnit(0.0930102465, "oz/ft"));
@ -428,7 +434,7 @@ public class UnitGroup {
UNITS_ANGLE.setDefaultUnit(0); UNITS_ANGLE.setDefaultUnit(0);
UNITS_DENSITY_BULK.setDefaultUnit(0); UNITS_DENSITY_BULK.setDefaultUnit(0);
UNITS_DENSITY_SURFACE.setDefaultUnit(1); UNITS_DENSITY_SURFACE.setDefaultUnit(1);
UNITS_DENSITY_LINE.setDefaultUnit(0); UNITS_DENSITY_LINE.setDefaultUnit(1);
UNITS_FORCE.setDefaultUnit(0); UNITS_FORCE.setDefaultUnit(0);
UNITS_IMPULSE.setDefaultUnit(0); UNITS_IMPULSE.setDefaultUnit(0);
UNITS_TIME_STEP.setDefaultUnit(1); UNITS_TIME_STEP.setDefaultUnit(1);

@ -1 +1 @@
Subproject commit 1aa03bb4a44a145f459939f487159a202dbf0f9f Subproject commit 9da5f4e25f57e8ec476ea38cd91c78edff06003f

View File

@ -6,13 +6,13 @@
<Materials> <Materials>
<Material UnitsOfMeasure="kg/m"> <Material UnitsOfMeasure="kg/m3">
<Name>Delrin</Name> <Name>Delrin</Name>
<Density>1420</Density> <Density>1420</Density>
<Type>BULK</Type> <Type>BULK</Type>
</Material> </Material>
<Material UnitsOfMeasure="kg/m"> <Material UnitsOfMeasure="kg/m3">
<Name>Nylon</Name> <Name>Nylon</Name>
<Density>1150</Density> <Density>1150</Density>
<Type>BULK</Type> <Type>BULK</Type>