create changelog entry
[debian/openrocket] / core / src / net / sf / openrocket / file / rocksim / export / BasePartDTO.java
1 package net.sf.openrocket.file.rocksim.export;
2
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;
14
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;
19
20 /**
21  * The base class for all OpenRocket to Rocksim conversions.
22  */
23 @XmlRootElement
24 @XmlAccessorType(XmlAccessType.FIELD)
25 public abstract class BasePartDTO {
26
27     /**
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.
30      */
31     private static int currentSerialNumber = 1;
32
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;
65
66     /**
67      * Default constructor.
68      */
69     protected BasePartDTO() {
70         serialNumber = currentSerialNumber++;
71     }
72
73     /**
74      * Copy constructor of sorts, that performs all common conversions for components.
75      *
76      * @param ec
77      */
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);
84
85         if (! (ec instanceof FinSet)) {
86             setLen(ec.getLength() * RocksimCommonConstants.ROCKSIM_TO_OPENROCKET_LENGTH);
87         }
88         setUseKnownCG(ec.isCGOverridden() || ec.isMassOverridden() ? 1 : 0);
89         setName(ec.getName());
90
91         setXb(ec.getPositionValue() * RocksimCommonConstants.ROCKSIM_TO_OPENROCKET_LENGTH);
92
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);
98         }
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);
102         }
103
104         if (ec instanceof ExternalComponent) {
105             ExternalComponent comp = (ExternalComponent) ec;
106             setLocationMode(RocksimLocationMode.toCode(comp.getRelativePosition()));
107
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());
113             }
114             setMaterial(material);
115
116             setFinishCode(RocksimFinishCode.toCode(comp.getFinish()));
117         }
118         else if (ec instanceof StructuralComponent) {
119             StructuralComponent comp = (StructuralComponent) ec;
120
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());
127             }
128             setMaterial(material);
129         }
130         else if (ec instanceof RecoveryDevice) {
131             RecoveryDevice comp = (RecoveryDevice) ec;
132
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());
139             }
140             setMaterial(material);
141         }
142
143         if (ec instanceof RingComponent) {
144             RingComponent rc = (RingComponent)ec;
145             setRadialAngle(rc.getRadialDirection());
146             setRadialLoc(rc.getRadialPosition() * RocksimCommonConstants.ROCKSIM_TO_OPENROCKET_LENGTH);
147         }
148     }
149
150     public Double getKnownMass() {
151         return knownMass;
152     }
153
154     public void setKnownMass(Double theKnownMass) {
155         knownMass = theKnownMass;
156     }
157
158     public double getDensity() {
159         return density;
160     }
161
162     public void setDensity(double theDensity) {
163         density = theDensity;
164     }
165
166     public String getMaterial() {
167         return material;
168     }
169
170     public void setMaterial(String theMaterial) {
171         material = theMaterial;
172     }
173
174     public String getName() {
175         return name;
176     }
177
178     public void setName(String theName) {
179         name = theName;
180     }
181
182     public Double getKnownCG() {
183         return knownCG;
184     }
185
186     public void setKnownCG(Double theKnownCG) {
187         knownCG = theKnownCG;
188     }
189
190     public int getUseKnownCG() {
191         return useKnownCG;
192     }
193
194     public void setUseKnownCG(int theUseKnownCG) {
195         useKnownCG = theUseKnownCG;
196     }
197
198     public double getXb() {
199         return xb;
200     }
201
202     public void setXb(double theXb) {
203         xb = theXb;
204     }
205
206     public double getCalcMass() {
207         return calcMass;
208     }
209
210     public void setCalcMass(double theCalcMass) {
211         calcMass = theCalcMass;
212     }
213
214     public double getCalcCG() {
215         return calcCG;
216     }
217
218     public void setCalcCG(double theCalcCG) {
219         calcCG = theCalcCG;
220     }
221
222     public int getDensityType() {
223         return densityType;
224     }
225
226     public void setDensityType(int theDensityType) {
227         densityType = theDensityType;
228     }
229
230     public double getRadialLoc() {
231         return radialLoc;
232     }
233
234     public void setRadialLoc(double theRadialLoc) {
235         radialLoc = theRadialLoc;
236     }
237
238     public double getRadialAngle() {
239         return radialAngle;
240     }
241
242     public void setRadialAngle(double theRadialAngle) {
243         radialAngle = theRadialAngle;
244     }
245
246     public int getLocationMode() {
247         return locationMode;
248     }
249
250     public void setLocationMode(int theLocationMode) {
251         locationMode = theLocationMode;
252     }
253
254     public double getLen() {
255         return len;
256     }
257
258     public void setLen(double theLen) {
259         len = theLen;
260     }
261
262     public int getFinishCode() {
263         return finishCode;
264     }
265
266     public void setFinishCode(int theFinishCode) {
267         finishCode = theFinishCode;
268     }
269
270     public static int getCurrentSerialNumber() {
271         return currentSerialNumber - 1;
272     }
273
274     /**
275      * Reset the serial number, which needs to happen after each file save.
276      */
277     public static void resetCurrentSerialNumber() {
278         currentSerialNumber = 0;
279     }
280 }