Save component ID for component + save flight event source

This is to fix an issue where simulations loaded from the .ork file did not have the correct event source set
This commit is contained in:
SiboVG 2023-06-27 16:35:36 +02:00
parent 16893a74f5
commit bc8f72ca3c
6 changed files with 40 additions and 5 deletions

View File

@ -531,8 +531,13 @@ public class OpenRocketSaver extends RocketSaver {
// 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

View File

@ -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>(

View File

@ -8,6 +8,8 @@ import net.sf.openrocket.file.simplesax.AbstractElementHandler;
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;
@ -126,6 +128,8 @@ class FlightDataBranchHandler extends AbstractElementHandler {
if (element.equals("event")) {
double time;
FlightEvent.Type type;
String sourceID;
RocketComponent source = null;
try {
time = DocumentConfig.stringToDouble(attributes.get("time"));
@ -139,8 +143,20 @@ class FlightDataBranchHandler extends AbstractElementHandler {
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;
}

View File

@ -36,6 +36,7 @@ public class 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) {

View File

@ -1206,8 +1206,16 @@ public abstract class RocketComponent implements ChangeSource, Cloneable, Iterab
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;
}
/**

View File

@ -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.