diff --git a/core/src/net/sf/openrocket/file/wavefrontobj/export/OBJExporterFactory.java b/core/src/net/sf/openrocket/file/wavefrontobj/export/OBJExporterFactory.java index db93b8233..0080fa11c 100644 --- a/core/src/net/sf/openrocket/file/wavefrontobj/export/OBJExporterFactory.java +++ b/core/src/net/sf/openrocket/file/wavefrontobj/export/OBJExporterFactory.java @@ -38,6 +38,7 @@ import java.io.OutputStream; import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; +import java.util.LinkedHashSet; import java.util.List; import java.util.Map; import java.util.Set; @@ -114,8 +115,11 @@ public class OBJExporterFactory { } } + // Sort the components according to how they are ordered in the rocket (component tree) + Set sortedComponents = sortComponents(componentsToExport); + int idx = 1; - for (RocketComponent component : componentsToExport) { + for (RocketComponent component : sortedComponents) { if (component instanceof ComponentAssembly) { continue; } @@ -179,7 +183,7 @@ public class OBJExporterFactory { if (options.isExportAppearance()) { String mtlFilePath = FileUtils.removeExtension(filePath) + ".mtl"; List mtls = materials.get(obj); - try (OutputStream mtlOutputStream = new FileOutputStream(mtlFilePath)) { + try (OutputStream mtlOutputStream = new FileOutputStream(mtlFilePath, false)) { DefaultMtlWriter.write(mtls, mtlOutputStream); } catch (IOException e) { throw new RuntimeException(e); @@ -234,6 +238,27 @@ public class OBJExporterFactory { } } + /** + * Sort a set of components according to how they are ordered in the rocket (component tree). + * @param components components to sort + * @return sorted components + */ + private Set sortComponents(Set components) { + Set sortedComponents = new LinkedHashSet<>(); + addChildComponentToList(this.configuration.getRocket(), components, sortedComponents); + + return sortedComponents; + } + + private void addChildComponentToList(RocketComponent parent, Set components, Set sortedComponents) { + for (RocketComponent child : parent.getChildren()) { + if (components.contains(child)) { + sortedComponents.add(child); + } + addChildComponentToList(child, components, sortedComponents); + } + } + interface ExporterFactory { RocketComponentExporter create(DefaultObj obj, FlightConfiguration config, CoordTransform transformer, T component, String groupName, ObjUtils.LevelOfDetail LOD);