Skip to content

Commit

Permalink
Save component ID for component + save flight event source
Browse files Browse the repository at this point in the history
This is to fix an issue where simulations loaded from the .ork file did not have the correct event source set
  • Loading branch information
SiboVG committed Jun 27, 2023
1 parent 16893a7 commit bc8f72c
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -531,8 +531,13 @@ private void saveFlightDataBranch(FlightDataBranch branch)

// Write events
for (FlightEvent event : branch.getEvents()) {
writeln("<event time=\"" + TextUtil.doubleToString(event.getTime())
+ "\" type=\"" + enumToXMLName(event.getType()) + "\"/>");
String eventStr = "<event time=\"" + TextUtil.doubleToString(event.getTime())
+ "\" type=\"" + enumToXMLName(event.getType());
if (event.getSource() != null) {
eventStr += "\" source=\"" + TextUtil.escapeXML(event.getSource().getID());
}
eventStr += "\"/>";
writeln(eventStr);
}

// Write the data
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ class DocumentConfig {
// RocketComponent
setters.put("RocketComponent:name", new StringSetter(
Reflection.findMethod(RocketComponent.class, "setName", String.class)));
setters.put("RocketComponent:id", new StringSetter(
Reflection.findMethod(RocketComponent.class, "setID", String.class)));
setters.put("RocketComponent:color", new ColorSetter(
Reflection.findMethod(RocketComponent.class, "setColor", Color.class)));
setters.put("RocketComponent:linestyle", new EnumSetter<LineStyle>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import net.sf.openrocket.file.simplesax.ElementHandler;
import net.sf.openrocket.file.simplesax.PlainTextHandler;
import net.sf.openrocket.l10n.Translator;
import net.sf.openrocket.rocketcomponent.Rocket;
import net.sf.openrocket.rocketcomponent.RocketComponent;
import net.sf.openrocket.simulation.FlightDataBranch;
import net.sf.openrocket.simulation.FlightDataType;
import net.sf.openrocket.simulation.FlightEvent;
Expand Down Expand Up @@ -126,6 +128,8 @@ public void closeElement(String element, HashMap<String, String> attributes,
if (element.equals("event")) {
double time;
FlightEvent.Type type;
String sourceID;
RocketComponent source = null;

try {
time = DocumentConfig.stringToDouble(attributes.get("time"));
Expand All @@ -139,8 +143,20 @@ public void closeElement(String element, HashMap<String, String> attributes,
warnings.add("Illegal event specification, ignoring.");
return;
}

// Get the event source
Rocket rocket = context.getOpenRocketDocument().getRocket();
sourceID = attributes.get("source");
if (sourceID != null) {
for (RocketComponent child : rocket.getAllChildren()) {
if (child.getID().equals(sourceID)) {
source = child;
break;
}
}
}

branch.addEvent(new FlightEvent(type, time));
branch.addEvent(new FlightEvent(type, time, source));
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ protected RocketComponentSaver() {

protected void addParams(net.sf.openrocket.rocketcomponent.RocketComponent c, List<String> elements) {
elements.add("<name>" + TextUtil.escapeXML(c.getName()) + "</name>");
elements.add("<id>" + TextUtil.escapeXML(c.getID()) + "</id>");

ComponentPreset preset = c.getPresetComponent();
if (preset != null) {
Expand Down
12 changes: 10 additions & 2 deletions core/src/net/sf/openrocket/rocketcomponent/RocketComponent.java
Original file line number Diff line number Diff line change
Expand Up @@ -1206,8 +1206,16 @@ private final void newID() {
mutex.verify();
this.id = UniqueID.uuid();
}



/**
* Set the ID for this component.
* Generally not recommended to directly set the ID, this is done automatically. Only use this in case you have to.
* @param newID new ID
*/
public void setID(String newID) {
mutex.verify();
this.id = newID;
}


/**
Expand Down
3 changes: 3 additions & 0 deletions fileformat.txt
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,6 @@ The following file format versions exist:
Rename <fincount> to <instancecount> (<fincount> remains for backward compatibility)
Rename <position> to <axialoffset> (<position> remains for backward compatibility)
Rename <rotation> to <angleoffset> (<rotation> remains for backward compatibility)

1.9: Introduced with OpenRocket 23.xx.
Added ID for each rocket component, to in turn add this ID as a source for flight events.

0 comments on commit bc8f72c

Please sign in to comment.