1 package net.sf.openrocket.file.rocksim.export;
3 import net.sf.openrocket.file.rocksim.RocksimCommonConstants;
4 import net.sf.openrocket.file.rocksim.RocksimDensityType;
5 import net.sf.openrocket.file.rocksim.RocksimFinishCode;
6 import net.sf.openrocket.file.rocksim.RocksimLocationMode;
7 import net.sf.openrocket.file.rocksim.importt.BaseHandler;
8 import net.sf.openrocket.rocketcomponent.ExternalComponent;
9 import net.sf.openrocket.rocketcomponent.FinSet;
10 import net.sf.openrocket.rocketcomponent.RecoveryDevice;
11 import net.sf.openrocket.rocketcomponent.RingComponent;
12 import net.sf.openrocket.rocketcomponent.RocketComponent;
13 import net.sf.openrocket.rocketcomponent.StructuralComponent;
15 import javax.xml.bind.annotation.XmlAccessType;
16 import javax.xml.bind.annotation.XmlAccessorType;
17 import javax.xml.bind.annotation.XmlElement;
18 import javax.xml.bind.annotation.XmlRootElement;
21 * The base class for all OpenRocket to Rocksim conversions.
24 @XmlAccessorType(XmlAccessType.FIELD)
25 public abstract class BasePartDTO {
28 * The very important Rocksim serial number. Each component needs one. This is not multi-thread safe. Trying
29 * to save multiple files at the same time will have unpredictable results with respect to the serial numbering.
31 private static int currentSerialNumber = 1;
33 @XmlElement(name = RocksimCommonConstants.KNOWN_MASS)
34 private double knownMass = 0d;
35 @XmlElement(name = RocksimCommonConstants.DENSITY)
36 private double density = 0d;
37 @XmlElement(name = RocksimCommonConstants.MATERIAL)
38 private String material = "";
39 @XmlElement(name = RocksimCommonConstants.NAME)
40 private String name = "";
41 @XmlElement(name = RocksimCommonConstants.KNOWN_CG)
42 private double knownCG = 0;
43 @XmlElement(name = RocksimCommonConstants.USE_KNOWN_CG)
44 private int useKnownCG = 1;
45 @XmlElement(name = RocksimCommonConstants.XB)
46 private double xb = 0;
47 @XmlElement(name = RocksimCommonConstants.CALC_MASS)
48 private double calcMass = 0d;
49 @XmlElement(name = RocksimCommonConstants.CALC_CG)
50 private double calcCG = 0d;
51 @XmlElement(name = RocksimCommonConstants.DENSITY_TYPE)
52 private int densityType = 0;
53 @XmlElement(name = RocksimCommonConstants.RADIAL_LOC)
54 private double radialLoc = 0;
55 @XmlElement(name = RocksimCommonConstants.RADIAL_ANGLE)
56 private double radialAngle = 0;
57 @XmlElement(name = RocksimCommonConstants.LOCATION_MODE)
58 private int locationMode = 0;
59 @XmlElement(name = RocksimCommonConstants.LEN, required = false, nillable = false)
60 private double len = 0d;
61 @XmlElement(name = RocksimCommonConstants.FINISH_CODE)
62 private int finishCode = 0;
63 @XmlElement(name = RocksimCommonConstants.SERIAL_NUMBER)
64 private int serialNumber = -1;
67 * Default constructor.
69 protected BasePartDTO() {
70 serialNumber = currentSerialNumber++;
74 * Copy constructor of sorts, that performs all common conversions for components.
78 protected BasePartDTO(RocketComponent ec) {
79 serialNumber = currentSerialNumber++;
80 setCalcCG(ec.getCG().x * RocksimCommonConstants.ROCKSIM_TO_OPENROCKET_LENGTH);
81 setCalcMass(ec.getComponentMass() * RocksimCommonConstants.ROCKSIM_TO_OPENROCKET_MASS);
82 setKnownCG(ec.getOverrideCGX() * RocksimCommonConstants.ROCKSIM_TO_OPENROCKET_LENGTH);
83 setKnownMass(ec.getMass() * RocksimCommonConstants.ROCKSIM_TO_OPENROCKET_MASS);
85 if (! (ec instanceof FinSet)) {
86 setLen(ec.getLength() * RocksimCommonConstants.ROCKSIM_TO_OPENROCKET_LENGTH);
88 setUseKnownCG(ec.isCGOverridden() || ec.isMassOverridden() ? 1 : 0);
89 setName(ec.getName());
91 setXb(ec.getPositionValue() * RocksimCommonConstants.ROCKSIM_TO_OPENROCKET_LENGTH);
93 //When the relative position is BOTTOM, the position location of the bottom edge of the component is +
94 //to the right of the bottom of the parent, and - to the left.
95 //But in Rocksim, it's + to the left and - to the right
96 if (ec.getRelativePosition().equals(RocketComponent.Position.BOTTOM)) {
97 setXb((-1 * ec.getPositionValue()) * RocksimCommonConstants.ROCKSIM_TO_OPENROCKET_LENGTH);
99 else if (ec.getRelativePosition().equals(RocketComponent.Position.MIDDLE)) {
100 //Mapped to TOP, so adjust accordingly
101 setXb((ec.getPositionValue() + (ec.getParent().getLength() - ec.getLength()) /2)* RocksimCommonConstants.ROCKSIM_TO_OPENROCKET_LENGTH);
104 if (ec instanceof ExternalComponent) {
105 ExternalComponent comp = (ExternalComponent) ec;
106 setLocationMode(RocksimLocationMode.toCode(comp.getRelativePosition()));
108 setDensity(comp.getMaterial().getDensity() * RocksimCommonConstants.ROCKSIM_TO_OPENROCKET_BULK_DENSITY);
109 setDensityType(RocksimDensityType.toCode(comp.getMaterial().getType()));
110 String material = comp.getMaterial().getName();
111 if (material.startsWith(BaseHandler.ROCKSIM_MATERIAL_PREFIX)) {
112 material = material.substring(BaseHandler.ROCKSIM_MATERIAL_PREFIX.length());
114 setMaterial(material);
116 setFinishCode(RocksimFinishCode.toCode(comp.getFinish()));
118 else if (ec instanceof StructuralComponent) {
119 StructuralComponent comp = (StructuralComponent) ec;
121 setLocationMode(RocksimLocationMode.toCode(comp.getRelativePosition()));
122 setDensity(comp.getMaterial().getDensity() * RocksimCommonConstants.ROCKSIM_TO_OPENROCKET_BULK_DENSITY);
123 setDensityType(RocksimDensityType.toCode(comp.getMaterial().getType()));
124 String material = comp.getMaterial().getName();
125 if (material.startsWith(BaseHandler.ROCKSIM_MATERIAL_PREFIX)) {
126 material = material.substring(BaseHandler.ROCKSIM_MATERIAL_PREFIX.length());
128 setMaterial(material);
130 else if (ec instanceof RecoveryDevice) {
131 RecoveryDevice comp = (RecoveryDevice) ec;
133 setLocationMode(RocksimLocationMode.toCode(comp.getRelativePosition()));
134 setDensity(comp.getMaterial().getDensity() * RocksimCommonConstants.ROCKSIM_TO_OPENROCKET_SURFACE_DENSITY);
135 setDensityType(RocksimDensityType.toCode(comp.getMaterial().getType()));
136 String material = comp.getMaterial().getName();
137 if (material.startsWith(BaseHandler.ROCKSIM_MATERIAL_PREFIX)) {
138 material = material.substring(BaseHandler.ROCKSIM_MATERIAL_PREFIX.length());
140 setMaterial(material);
143 if (ec instanceof RingComponent) {
144 RingComponent rc = (RingComponent)ec;
145 setRadialAngle(rc.getRadialDirection());
146 setRadialLoc(rc.getRadialPosition() * RocksimCommonConstants.ROCKSIM_TO_OPENROCKET_LENGTH);
150 public Double getKnownMass() {
154 public void setKnownMass(Double theKnownMass) {
155 knownMass = theKnownMass;
158 public double getDensity() {
162 public void setDensity(double theDensity) {
163 density = theDensity;
166 public String getMaterial() {
170 public void setMaterial(String theMaterial) {
171 material = theMaterial;
174 public String getName() {
178 public void setName(String theName) {
182 public Double getKnownCG() {
186 public void setKnownCG(Double theKnownCG) {
187 knownCG = theKnownCG;
190 public int getUseKnownCG() {
194 public void setUseKnownCG(int theUseKnownCG) {
195 useKnownCG = theUseKnownCG;
198 public double getXb() {
202 public void setXb(double theXb) {
206 public double getCalcMass() {
210 public void setCalcMass(double theCalcMass) {
211 calcMass = theCalcMass;
214 public double getCalcCG() {
218 public void setCalcCG(double theCalcCG) {
222 public int getDensityType() {
226 public void setDensityType(int theDensityType) {
227 densityType = theDensityType;
230 public double getRadialLoc() {
234 public void setRadialLoc(double theRadialLoc) {
235 radialLoc = theRadialLoc;
238 public double getRadialAngle() {
242 public void setRadialAngle(double theRadialAngle) {
243 radialAngle = theRadialAngle;
246 public int getLocationMode() {
250 public void setLocationMode(int theLocationMode) {
251 locationMode = theLocationMode;
254 public double getLen() {
258 public void setLen(double theLen) {
262 public int getFinishCode() {
266 public void setFinishCode(int theFinishCode) {
267 finishCode = theFinishCode;
270 public static int getCurrentSerialNumber() {
271 return currentSerialNumber - 1;
275 * Reset the serial number, which needs to happen after each file save.
277 public static void resetCurrentSerialNumber() {
278 currentSerialNumber = 0;