create changelog entry
[debian/openrocket] / core / src / net / sf / openrocket / file / rocksim / export / StageDTO.java
1 package net.sf.openrocket.file.rocksim.export;
2
3 import net.sf.openrocket.file.rocksim.RocksimCommonConstants;
4 import net.sf.openrocket.rocketcomponent.BodyTube;
5 import net.sf.openrocket.rocketcomponent.NoseCone;
6 import net.sf.openrocket.rocketcomponent.RocketComponent;
7 import net.sf.openrocket.rocketcomponent.Stage;
8 import net.sf.openrocket.rocketcomponent.Transition;
9 import net.sf.openrocket.util.ArrayList;
10
11 import javax.xml.bind.annotation.XmlAccessType;
12 import javax.xml.bind.annotation.XmlAccessorType;
13 import javax.xml.bind.annotation.XmlElementRef;
14 import javax.xml.bind.annotation.XmlElementRefs;
15 import java.util.List;
16
17 /**
18  * Placeholder for a Rocksim Stage.
19  */
20 @XmlAccessorType(XmlAccessType.FIELD)
21 public class StageDTO {
22
23     @XmlElementRefs({
24             @XmlElementRef(name = RocksimCommonConstants.BODY_TUBE, type = BodyTubeDTO.class),
25             @XmlElementRef(name = RocksimCommonConstants.NOSE_CONE, type = NoseConeDTO.class),
26             @XmlElementRef(name = RocksimCommonConstants.TRANSITION, type = TransitionDTO.class)
27     })
28     private List<BasePartDTO> externalPart = new ArrayList<BasePartDTO>();
29
30     /**
31      * Default constructor.
32      */
33     public StageDTO() {
34     }
35
36     /**
37      * Copy constructor.
38      *
39      * @param theORStage  the OR stage
40      * @param design      the encompassing container DTO
41      * @param stageNumber the stage number (3 is always at the top, even if it's the only one)
42      */
43     public StageDTO(Stage theORStage, RocketDesignDTO design, int stageNumber) {
44
45         if (stageNumber == 3) {
46             if (theORStage.isMassOverridden()) {
47                 design.setStage3Mass(theORStage.getMass() * RocksimCommonConstants.ROCKSIM_TO_OPENROCKET_MASS);
48                 design.setUseKnownMass(1);
49             }
50             if (theORStage.isCGOverridden()) {
51                 design.setStage3CG(theORStage.getOverrideCGX() * RocksimCommonConstants.ROCKSIM_TO_OPENROCKET_LENGTH);
52             }
53         }
54         
55         if (stageNumber == 2) {
56             if (theORStage.isMassOverridden()) {
57                 design.setStage2Mass(theORStage.getMass() * RocksimCommonConstants.ROCKSIM_TO_OPENROCKET_MASS);
58                 design.setUseKnownMass(1);
59             }
60             if (theORStage.isCGOverridden()) {
61                 design.setStage2CGAlone(theORStage.getOverrideCGX() * RocksimCommonConstants.ROCKSIM_TO_OPENROCKET_LENGTH);
62             }
63         }
64
65         if (stageNumber == 1) {
66             if (theORStage.isMassOverridden()) {
67                 design.setStage1Mass(theORStage.getMass() * RocksimCommonConstants.ROCKSIM_TO_OPENROCKET_MASS);
68                 design.setUseKnownMass(1);
69             }
70             if (theORStage.isCGOverridden()) {
71                 design.setStage1CGAlone(theORStage.getOverrideCGX() * RocksimCommonConstants.ROCKSIM_TO_OPENROCKET_LENGTH);
72             }
73         }
74
75         List<RocketComponent> children = theORStage.getChildren();
76         for (int i = 0; i < children.size(); i++) {
77             RocketComponent rocketComponents = children.get(i);
78             if (rocketComponents instanceof NoseCone) {
79                 addExternalPart(toNoseConeDTO((NoseCone) rocketComponents));
80             } else if (rocketComponents instanceof BodyTube) {
81                 addExternalPart(toBodyTubeDTO((BodyTube) rocketComponents));
82             } else if (rocketComponents instanceof Transition) {
83                 addExternalPart(toTransitionDTO((Transition) rocketComponents));
84             }
85         }
86     }
87
88     public List<BasePartDTO> getExternalPart() {
89         return externalPart;
90     }
91
92     public void addExternalPart(BasePartDTO theExternalPartDTO) {
93         externalPart.add(theExternalPartDTO);
94     }
95
96     private NoseConeDTO toNoseConeDTO(NoseCone nc) {
97         return new NoseConeDTO(nc);
98     }
99
100     private BodyTubeDTO toBodyTubeDTO(BodyTube bt) {
101         return new BodyTubeDTO(bt);
102     }
103
104     private TransitionDTO toTransitionDTO(Transition tran) {
105         return new TransitionDTO(tran);
106     }
107 }