From 9bb1151dba1a0c10963c5ecb6a7f4561706b613b Mon Sep 17 00:00:00 2001 From: SiboVG Date: Fri, 18 Aug 2023 04:01:03 +0200 Subject: [PATCH] Sort export components based on order in rocket This is useful if you import the OBJ with "split by groups". In that case, all the components are in nice chronological order. --- .../export/OBJExporterFactory.java | 29 +++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) 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);