Next Gen STEP AP242 Native-ish BREP Geometric Modeling Kernel
Classify multi-root STEP as assembly-like for analyze routing
Yuechen Li9bbd50f5cc05
diff --git a/Aetheris.CLI.Tests/CliBaselineTests.cs b/Aetheris.CLI.Tests/CliBaselineTests.csindex a0899854..a32bf8f3 100644--- a/Aetheris.CLI.Tests/CliBaselineTests.cs+++ b/Aetheris.CLI.Tests/CliBaselineTests.cs@@ -550,6 +550,49 @@ public sealed class CliBaselineTestsAssert.True(root.GetProperty("error").GetString()?.Length > 0);}+ [Fact]+ public void Analyze_Command_SingleRootStep_StillReturnsSuccess()+ {+ var stepPath = Path.Combine(RepoRoot, "testdata/firmament/exports/box_basic.step");+ var stdout = new StringWriter();+ var stderr = new StringWriter();++ var exitCode = Aetheris.CLI.CliRunner.Run(["analyze", stepPath, "--json"], stdout, stderr);++ Assert.Equal(0, exitCode);+ Assert.True(string.IsNullOrWhiteSpace(stderr.ToString()));+ using var doc = JsonDocument.Parse(stdout.ToString());+ var root = doc.RootElement;+ Assert.Equal(Path.GetFullPath(stepPath), root.GetProperty("stepPath").GetString());+ Assert.True(root.TryGetProperty("summary", out _));+ }++ [Fact]+ public void Analyze_Command_MultiRootStep_JsonFailure_IsAssemblyLikeAndMachineFriendly()+ {+ var stepPath = Path.Combine(RepoRoot, "testdata/step242/OCCT/as1.step");+ var stdout = new StringWriter();+ var stderr = new StringWriter();++ var exitCode = Aetheris.CLI.CliRunner.Run(["analyze", stepPath, "--json"], stdout, stderr);++ Assert.Equal(1, exitCode);+ Assert.True(string.IsNullOrWhiteSpace(stderr.ToString()));++ using var doc = JsonDocument.Parse(stdout.ToString());+ var root = doc.RootElement;+ Assert.False(root.GetProperty("success").GetBoolean());+ Assert.Equal("assembly-like-step", root.GetProperty("errorKind").GetString());+ Assert.Equal("assembly-like", root.GetProperty("classification").GetString());+ Assert.True(root.GetProperty("rigidRootCount").GetInt32() > 1);+ Assert.Contains("assembly extraction/import", root.GetProperty("routeHint").GetString(), StringComparison.OrdinalIgnoreCase);++ var diagnostics = root.GetProperty("diagnostics");+ Assert.True(diagnostics.GetArrayLength() >= 1);+ var first = diagnostics[0];+ Assert.Equal("Importer.AssemblyLike.StepMultiRoot", first.GetProperty("source").GetString());+ }+[Fact]public void Canon_Command_RoundTrips_Supported_Step_Through_Importer_Exporter_Path(){diff --git a/Aetheris.CLI/CliRunner.cs b/Aetheris.CLI/CliRunner.csindex d41af960..e5eff79e 100644--- a/Aetheris.CLI/CliRunner.cs+++ b/Aetheris.CLI/CliRunner.cs@@ -322,6 +322,80 @@ public static class CliRunnerprivate static string FormatKernelDiagnostics(IReadOnlyList<Aetheris.Kernel.Core.Diagnostics.KernelDiagnostic> diagnostics) =>string.Join(Environment.NewLine, diagnostics.Select(d => $"[{d.Severity}] {d.Source}: {d.Message}"));+ private static void WriteAnalyzeFailureJson(TextWriter stdout, string stepPath, Exception exception)+ {+ var fullPath = Path.GetFullPath(stepPath);+ if (exception is not StepAnalysisImportException importFailure)+ {+ stdout.WriteLine(JsonSerializer.Serialize(new+ {+ success = false,+ stepPath = fullPath,+ errorKind = "analysis-failure",+ error = exception.Message+ }, JsonOptions));+ return;+ }++ var multiRootDiagnostic = importFailure.Diagnostics.FirstOrDefault(d => string.Equals(d.Source, "Importer.AssemblyLike.StepMultiRoot", StringComparison.Ordinal));+ if (multiRootDiagnostic is null)+ {+ stdout.WriteLine(JsonSerializer.Serialize(new+ {+ success = false,+ stepPath = fullPath,+ errorKind = "import-failure",+ error = importFailure.Message,+ diagnostics = importFailure.Diagnostics.Select(d => new+ {+ code = d.Code.ToString(),+ severity = d.Severity.ToString(),+ source = d.Source,+ message = d.Message+ })+ }, JsonOptions));+ return;+ }++ var rigidRootCount = CountManifoldSolidRoots(multiRootDiagnostic.Message);+ stdout.WriteLine(JsonSerializer.Serialize(new+ {+ success = false,+ stepPath = fullPath,+ errorKind = "assembly-like-step",+ classification = "assembly-like",+ rigidRootCount,+ routeHint = "Use the assembly extraction/import path for this STEP input.",+ error = importFailure.Message,+ diagnostics = importFailure.Diagnostics.Select(d => new+ {+ code = d.Code.ToString(),+ severity = d.Severity.ToString(),+ source = d.Source,+ message = d.Message+ })+ }, JsonOptions));+ }++ private static int? CountManifoldSolidRoots(string message)+ {+ const string token = "detected ";+ var detectedIndex = message.IndexOf(token, StringComparison.OrdinalIgnoreCase);+ if (detectedIndex < 0)+ {+ return null;+ }++ var start = detectedIndex + token.Length;+ var end = message.IndexOf(" MANIFOLD_SOLID_BREP", start, StringComparison.OrdinalIgnoreCase);+ if (end <= start)+ {+ return null;+ }++ return int.TryParse(message[start..end], out var value) ? value : null;+ }+private static int RunAnalyze(string[] args, TextWriter stdout, TextWriter stderr){if (args.Length == 0)@@ -415,20 +489,13 @@ public static class CliRunner}catch (Exception ex){- if (json)- {- stdout.WriteLine(JsonSerializer.Serialize(new- {- success = false,- stepPath = Path.GetFullPath(stepPath),- error = ex.Message- }, JsonOptions));- }- else+ if (!json){stderr.WriteLine(ex.Message);+ return 1;}+ WriteAnalyzeFailureJson(stdout, stepPath, ex);return 1;}@@ -552,20 +619,13 @@ public static class CliRunner}catch (Exception ex){- if (json)- {- stdout.WriteLine(JsonSerializer.Serialize(new- {- success = false,- stepPath = Path.GetFullPath(stepPath),- error = ex.Message- }, JsonOptions));- }- else+ if (!json){stderr.WriteLine(ex.Message);+ return 1;}+ WriteAnalyzeFailureJson(stdout, stepPath, ex);return 1;}@@ -669,12 +729,7 @@ public static class CliRunner}catch (Exception ex){- stdout.WriteLine(JsonSerializer.Serialize(new- {- success = false,- stepPath = Path.GetFullPath(stepPath),- error = ex.Message- }, JsonOptions));+ WriteAnalyzeFailureJson(stdout, stepPath, ex);return 1;}diff --git a/Aetheris.CLI/StepAnalysisImportException.cs b/Aetheris.CLI/StepAnalysisImportException.csnew file mode 100644index 00000000..00d46e6a--- /dev/null+++ b/Aetheris.CLI/StepAnalysisImportException.cs@@ -0,0 +1,23 @@+using Aetheris.Kernel.Core.Diagnostics;++namespace Aetheris.CLI;++public sealed class StepAnalysisImportException : InvalidOperationException+{+ public StepAnalysisImportException(string stepPath, IReadOnlyList<KernelDiagnostic> diagnostics)+ : base(FormatMessage(stepPath, diagnostics))+ {+ StepPath = stepPath;+ Diagnostics = diagnostics;+ }++ public string StepPath { get; }++ public IReadOnlyList<KernelDiagnostic> Diagnostics { get; }++ private static string FormatMessage(string stepPath, IReadOnlyList<KernelDiagnostic> diagnostics)+ {+ var lines = diagnostics.Select(d => $"[{d.Severity}] {d.Source}: {d.Message}");+ return $"Failed to analyze STEP '{stepPath}':{Environment.NewLine}{string.Join(Environment.NewLine, lines)}";+ }+}diff --git a/Aetheris.CLI/StepAnalyzer.cs b/Aetheris.CLI/StepAnalyzer.csindex dfc249d5..16ed59e2 100644--- a/Aetheris.CLI/StepAnalyzer.cs+++ b/Aetheris.CLI/StepAnalyzer.cs@@ -14,14 +14,8 @@ public static class StepAnalyzer{public static AnalyzeResult Analyze(string stepPath, int? faceId = null, int? edgeId = null, int? vertexId = null){- var fullPath = Path.GetFullPath(stepPath);- var import = Step242Importer.ImportBody(File.ReadAllText(fullPath));- if (!import.IsSuccess)- {- throw new InvalidOperationException(string.Join(Environment.NewLine, import.Diagnostics.Select(d => d.Message)));- }-- return AnalyzeImportedBody(import.Value, fullPath, faceId, edgeId, vertexId);+ var (fullPath, body) = ImportStepBody(stepPath);+ return AnalyzeImportedBody(body, fullPath, faceId, edgeId, vertexId);}public static AnalyzeResult AnalyzeImportedBody(BrepBody body, string stepPath, int? faceId = null, int? edgeId = null, int? vertexId = null)@@ -38,26 +32,26 @@ public static class StepAnalyzerpublic static OrthographicMapResult AnalyzeMap(string stepPath, OrthographicView view, int rows, int cols){- var fullPath = Path.GetFullPath(stepPath);- var import = Step242Importer.ImportBody(File.ReadAllText(fullPath));- if (!import.IsSuccess)- {- throw new InvalidOperationException(string.Join(Environment.NewLine, import.Diagnostics.Select(d => d.Message)));- }-- return AnalyzeImportedBodyMap(import.Value, fullPath, view, rows, cols);+ var (fullPath, body) = ImportStepBody(stepPath);+ return AnalyzeImportedBodyMap(body, fullPath, view, rows, cols);}public static SectionAnalysisResult AnalyzeSection(string stepPath, SectionPlaneFamily planeFamily, double offset)+ {+ var (fullPath, body) = ImportStepBody(stepPath);+ return AnalyzeImportedBodySection(body, fullPath, planeFamily, offset);+ }++ private static (string FullPath, BrepBody Body) ImportStepBody(string stepPath){var fullPath = Path.GetFullPath(stepPath);var import = Step242Importer.ImportBody(File.ReadAllText(fullPath));if (!import.IsSuccess){- throw new InvalidOperationException(string.Join(Environment.NewLine, import.Diagnostics.Select(d => d.Message)));+ throw new StepAnalysisImportException(fullPath, import.Diagnostics);}- return AnalyzeImportedBodySection(import.Value, fullPath, planeFamily, offset);+ return (fullPath, import.Value);}public static SectionAnalysisResult AnalyzeImportedBodySection(BrepBody body, string stepPath, SectionPlaneFamily planeFamily, double offset)diff --git a/Aetheris.Kernel.Core.Tests/Step242/Step242ImporterTests.cs b/Aetheris.Kernel.Core.Tests/Step242/Step242ImporterTests.csindex 2c9cc86b..d054ecff 100644--- a/Aetheris.Kernel.Core.Tests/Step242/Step242ImporterTests.cs+++ b/Aetheris.Kernel.Core.Tests/Step242/Step242ImporterTests.cs@@ -91,7 +91,7 @@ public sealed class Step242ImporterTests}[Fact]- public void ImportBody_MultipleSolidRoots_ReturnsDeterministicSingleSolidDiagnostic()+ public void ImportBody_MultipleSolidRoots_ClassifiesInputAsAssemblyLike(){const string multiRoot = "ISO-10303-21;\nHEADER;\nENDSEC;\nDATA;\n#1=MANIFOLD_SOLID_BREP('a',#3);\n#2=MANIFOLD_SOLID_BREP('b',#3);\n#3=CLOSED_SHELL($,());\nENDSEC;\nEND-ISO-10303-21;";@@ -100,8 +100,9 @@ public sealed class Step242ImporterTestsAssert.False(import.IsSuccess);var diagnostic = Assert.Single(import.Diagnostics);Assert.Equal(KernelDiagnosticCode.NotImplemented, diagnostic.Code);- Assert.Equal("Importer.SingleSolid", diagnostic.Source);- Assert.StartsWith("Multiple MANIFOLD_SOLID_BREP", diagnostic.Message, StringComparison.Ordinal);+ Assert.Equal("Importer.AssemblyLike.StepMultiRoot", diagnostic.Source);+ Assert.Contains("assembly-like", diagnostic.Message, StringComparison.OrdinalIgnoreCase);+ Assert.Contains("detected 2 MANIFOLD_SOLID_BREP rigid roots", diagnostic.Message, StringComparison.Ordinal);}[Fact]diff --git a/Aetheris.Kernel.Core/Step242/Step242Importer.cs b/Aetheris.Kernel.Core/Step242/Step242Importer.csindex 60cb777c..bc23af03 100644--- a/Aetheris.Kernel.Core/Step242/Step242Importer.cs+++ b/Aetheris.Kernel.Core/Step242/Step242Importer.cs@@ -81,19 +81,22 @@ public static class Step242Importerinternal static KernelResult<BrepBody> ImportExactBrepCore(Step242ParsedDocument document){- var brepEntityResult = Step242ImportSharedUtilities.RequireSingleEntityByName(- document,- entityName: "MANIFOLD_SOLID_BREP",- missingMessage: "Missing MANIFOLD_SOLID_BREP root entity.",- missingSource: "Importer.TopologyRoot",- multipleMessage: "Multiple MANIFOLD_SOLID_BREP roots are unsupported in M23 import subset.",- multipleSource: "Importer.SingleSolid");- if (!brepEntityResult.IsSuccess)+ var rigidRootClassification = Step242RigidRootClassifier.Classify(document);+ if (rigidRootClassification.Kind == Step242RigidRootClassificationKind.MissingRigidRoot)+ {+ return Step242ImportSharedUtilities.NotImplementedFailure<BrepBody>(+ "Missing MANIFOLD_SOLID_BREP root entity.",+ "Importer.TopologyRoot");+ }++ if (rigidRootClassification.Kind == Step242RigidRootClassificationKind.AssemblyLikeMultipleRigidRoots){- return KernelResult<BrepBody>.Failure(brepEntityResult.Diagnostics);+ return Step242ImportSharedUtilities.NotImplementedFailure<BrepBody>(+ $"STEP input is assembly-like: detected {rigidRootClassification.RigidRoots.Count} MANIFOLD_SOLID_BREP rigid roots. Single-part exact BRep import accepts exactly one rigid root; route this input through assembly extraction/import.",+ "Importer.AssemblyLike.StepMultiRoot");}- var brepEntity = brepEntityResult.Value;+ var brepEntity = rigidRootClassification.SingleRigidRoot;var shellRefResult = Step242SubsetDecoder.ReadReference(brepEntity, 1, "MANIFOLD_SOLID_BREP shell");if (!shellRefResult.IsSuccess)diff --git a/Aetheris.Kernel.Core/Step242/Step242RigidRootClassifier.cs b/Aetheris.Kernel.Core/Step242/Step242RigidRootClassifier.csnew file mode 100644index 00000000..b10d6168--- /dev/null+++ b/Aetheris.Kernel.Core/Step242/Step242RigidRootClassifier.cs@@ -0,0 +1,34 @@+namespace Aetheris.Kernel.Core.Step242;++internal enum Step242RigidRootClassificationKind+{+ MissingRigidRoot = 0,+ PartLikeSingleRigidRoot = 1,+ AssemblyLikeMultipleRigidRoots = 2+}++internal sealed record Step242RigidRootClassification(+ Step242RigidRootClassificationKind Kind,+ IReadOnlyList<Step242ParsedEntity> RigidRoots)+{+ public Step242ParsedEntity SingleRigidRoot => RigidRoots[0];+}++internal static class Step242RigidRootClassifier+{+ internal static Step242RigidRootClassification Classify(Step242ParsedDocument document)+ {+ var manifoldSolidRoots = document.Entities+ .Where(e => string.Equals(e.Name, "MANIFOLD_SOLID_BREP", StringComparison.OrdinalIgnoreCase))+ .ToList();++ var kind = manifoldSolidRoots.Count switch+ {+ 0 => Step242RigidRootClassificationKind.MissingRigidRoot,+ 1 => Step242RigidRootClassificationKind.PartLikeSingleRigidRoot,+ _ => Step242RigidRootClassificationKind.AssemblyLikeMultipleRigidRoots+ };++ return new Step242RigidRootClassification(kind, manifoldSolidRoots);+ }+}diff --git a/testdata/step242/manifests/v0.corpus.json b/testdata/step242/manifests/v0.corpus.jsonindex 5683ee90..349617b5 100644--- a/testdata/step242/manifests/v0.corpus.json+++ b/testdata/step242/manifests/v0.corpus.json@@ -44,8 +44,8 @@"group": "expectedFail","expectedFirstDiagnostic": {"code": "NotImplemented",- "source": "Importer.SingleSolid",- "messagePrefix": "Multiple MANIFOLD_SOLID_BREP roots are unsupported"+ "source": "Importer.AssemblyLike.StepMultiRoot",+ "messagePrefix": "STEP input is assembly-like"}},{