1 package net.sf.openrocket.preset.loader;
3 import net.sf.openrocket.material.Material;
4 import net.sf.openrocket.preset.TypedKey;
5 import net.sf.openrocket.preset.TypedPropertyMap;
6 import net.sf.openrocket.util.BugException;
10 public class MaterialLoader extends RocksimComponentFileLoader {
12 private MaterialHolder materialMap = new MaterialHolder();
14 private final static TypedKey<String> MATERIALNAME = new TypedKey<String>("MaterialName", String.class);
15 private final static TypedKey<String> UNITS = new TypedKey<String>("Units", String.class);
16 private final static TypedKey<Double> DENSITY = new TypedKey<Double>("Density", Double.class);
18 public MaterialLoader(File theBasePathToLoadFrom) {
19 super(theBasePathToLoadFrom);
20 fileColumns.add( new StringColumnParser("Material Name", MATERIALNAME) );
21 fileColumns.add( new StringColumnParser("Units", UNITS));
22 fileColumns.add( new DoubleColumnParser("Density", DENSITY));
26 protected RocksimComponentFileType getFileType() {
27 return RocksimComponentFileType.MATERIAL;
30 public MaterialHolder getMaterialMap() {
35 protected void postProcess(TypedPropertyMap props) {
36 String name = props.get(MATERIALNAME);
37 String unit = props.get(UNITS);
38 double density = props.get(DENSITY);
40 String cleanedMaterialName = stripAll(name, '"').trim();
42 if ( "g/cm".equals( unit ) ) {
43 materialMap.put( Material.newUserMaterial(Material.Type.LINE,cleanedMaterialName, 0.1d * density));
44 } else if ( "g/cm2".equals(unit) ) {
45 materialMap.put( Material.newUserMaterial(Material.Type.SURFACE,cleanedMaterialName, 10.0d * density));
46 } else if ( "g/cm3".equals(unit) ) {
47 materialMap.put( Material.newUserMaterial(Material.Type.BULK,cleanedMaterialName, 1000.0d * density));
48 } else if ( "kg/m3".equals(unit) ) {
49 materialMap.put( Material.newUserMaterial(Material.Type.BULK,cleanedMaterialName, density));
50 } else if ( "lb/ft3".equals(unit) ) {
51 materialMap.put( Material.newUserMaterial(Material.Type.BULK,cleanedMaterialName, 16.0184634d * density));
52 } else if ( "oz/in".equals(unit) ) {
53 materialMap.put( Material.newUserMaterial(Material.Type.LINE,cleanedMaterialName, 1.11612296d * density));
54 } else if ( "oz/in2".equals(unit ) ) {
55 materialMap.put( Material.newUserMaterial(Material.Type.SURFACE,cleanedMaterialName, 43.94184876d * density));
57 throw new BugException("Unknown unit in Materials file: " + unit);