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.
*
* @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.
* @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.material.Material;
import net.sf.openrocket.unit.Unit;
import net.sf.openrocket.util.Chars;
/**
@ -18,7 +19,7 @@ import net.sf.openrocket.util.Chars;
@XmlRootElement(name = "Material")
@XmlAccessorType(XmlAccessType.FIELD)
public class MaterialDTO {
@XmlElement(name = "Name")
private String name;
@XmlElement(name = "Density")
@ -27,65 +28,65 @@ public class MaterialDTO {
private MaterialTypeDTO type;
@XmlAttribute(name = "UnitsOfMeasure")
private String uom;
/**
* Default constructor.
*/
public MaterialDTO() {
}
public MaterialDTO(final Material theMaterial) {
this(theMaterial.getName(), theMaterial.getDensity(), MaterialTypeDTO.asDTO(theMaterial.getType()),
theMaterial.getType().getUnitGroup().getDefaultUnit().toString());
}
public MaterialDTO(final String theName, final double theDensity, final MaterialTypeDTO theType, final String theUom) {
name = theName;
density = theDensity;
type = theType;
uom = theUom;
}
public String getName() {
return name;
}
public void setName(final String theName) {
name = theName;
}
public double getDensity() {
return density;
}
public void setDensity(final double theDensity) {
density = theDensity;
}
public MaterialTypeDTO getType() {
return type;
}
public void setType(final MaterialTypeDTO theType) {
type = theType;
}
public String getUom() {
return uom;
}
public void setUom(final String theUom) {
uom = theUom;
}
Material asMaterial() {
return Databases.findMaterial(type.getORMaterialType(), name, density);
}
/**
* 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.
* @param unmarshaller
* @param parent
@ -95,9 +96,15 @@ public class MaterialDTO {
if (uom != null) {
uom = uom.replace('2', Chars.SQUARED);
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,
* 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) {
MaterialTypeDTO[] values = values();
for (int i = 0; i < values.length; i++) {
MaterialTypeDTO value = values[i];
for (MaterialTypeDTO value : values) {
if (value.corollary.equals(targetType)) {
return value;
}

View File

@ -212,6 +212,7 @@ public class UnitGroup {
UNITS_DENSITY_BULK = new UnitGroup();
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(1, "kg/m" + 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.addUnit(new GeneralUnit(10, "g/cm" + 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(43.9418487, "oz/in" + 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_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(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(0.0930102465, "oz/ft"));
@ -428,7 +434,7 @@ public class UnitGroup {
UNITS_ANGLE.setDefaultUnit(0);
UNITS_DENSITY_BULK.setDefaultUnit(0);
UNITS_DENSITY_SURFACE.setDefaultUnit(1);
UNITS_DENSITY_LINE.setDefaultUnit(0);
UNITS_DENSITY_LINE.setDefaultUnit(1);
UNITS_FORCE.setDefaultUnit(0);
UNITS_IMPULSE.setDefaultUnit(0);
UNITS_TIME_STEP.setDefaultUnit(1);

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

View File

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