Next Gen STEP AP242 Native-ish BREP Geometric Modeling Kernel
Support STEP complex entity instances in AP242 parser/decoder
Yuechen Li87f59e3f116d
diff --git a/Aetheris.Kernel.Core.Tests/Step242/Step242ComplexEntityInstanceTests.cs b/Aetheris.Kernel.Core.Tests/Step242/Step242ComplexEntityInstanceTests.csnew file mode 100644index 00000000..3203dfff--- /dev/null+++ b/Aetheris.Kernel.Core.Tests/Step242/Step242ComplexEntityInstanceTests.cs@@ -0,0 +1,56 @@+using System.Text;+using Aetheris.Kernel.Core.Step242;++namespace Aetheris.Kernel.Core.Tests.Step242;++public sealed class Step242ComplexEntityInstanceTests+{+ [Fact]+ public void StepParser_ParsesComplexEntityInstance_Assignment()+ {+ var text = LoadFixture("testdata/step242/syntax-robustness/complex-context-min.step");++ var parseResult = Step242SubsetParser.Parse(text);++ Assert.True(parseResult.IsSuccess);+ var contextEntity = Assert.Single(parseResult.Value.Entities, e => e.Id == 5);+ var complex = Assert.IsType<Step242ComplexEntityInstance>(contextEntity.Instance);+ Assert.Equal(4, complex.Constructors.Count);+ Assert.Equal("GEOMETRIC_REPRESENTATION_CONTEXT", complex.Constructors[0].Name);+ Assert.Equal("GLOBAL_UNCERTAINTY_ASSIGNED_CONTEXT", complex.Constructors[1].Name);+ Assert.Equal("GLOBAL_UNIT_ASSIGNED_CONTEXT", complex.Constructors[2].Name);+ Assert.Equal("REPRESENTATION_CONTEXT", complex.Constructors[3].Name);+ }++ [Fact]+ public void Step242SubsetDecoder_CanResolveRepresentationContext_FromComplexInstance()+ {+ var text = LoadFixture("testdata/step242/syntax-robustness/complex-context-min.step");++ var parseResult = Step242SubsetParser.Parse(text);+ Assert.True(parseResult.IsSuccess);++ var shapeRepEntity = Assert.Single(parseResult.Value.Entities, e => e.Id == 13);+ var contextRefResult = Step242SubsetDecoder.ReadReference(shapeRepEntity, 2, "ADVANCED_BREP_SHAPE_REPRESENTATION context");+ Assert.True(contextRefResult.IsSuccess);++ var contextEntityResult = parseResult.Value.TryGetEntity(contextRefResult.Value.TargetId, "REPRESENTATION_CONTEXT");+ Assert.True(contextEntityResult.IsSuccess);++ var geometricContext = Step242SubsetDecoder.TryGetConstructor(contextEntityResult.Value.Instance, "GEOMETRIC_REPRESENTATION_CONTEXT");+ Assert.NotNull(geometricContext);++ var representationContext = Step242SubsetDecoder.TryGetConstructor(contextEntityResult.Value.Instance, "REPRESENTATION_CONTEXT");+ Assert.NotNull(representationContext);++ var importResult = Step242Importer.ImportBody(text);+ Assert.False(importResult.IsSuccess);+ Assert.DoesNotContain(importResult.Diagnostics, d => d.Message.Contains("Expected identifier", StringComparison.Ordinal));+ }++ private static string LoadFixture(string relativePath)+ {+ var path = Path.Combine(Step242CorpusManifestRunner.RepoRoot(), relativePath.Replace('/', Path.DirectorySeparatorChar));+ return File.ReadAllText(path, Encoding.UTF8);+ }+}diff --git a/Aetheris.Kernel.Core/Properties/InternalsVisibleTo.cs b/Aetheris.Kernel.Core/Properties/InternalsVisibleTo.csnew file mode 100644index 00000000..9f0f286c--- /dev/null+++ b/Aetheris.Kernel.Core/Properties/InternalsVisibleTo.cs@@ -0,0 +1,3 @@+using System.Runtime.CompilerServices;++[assembly: InternalsVisibleTo("Aetheris.Kernel.Core.Tests")]diff --git a/Aetheris.Kernel.Core/Step242/Step242SubsetDecoder.cs b/Aetheris.Kernel.Core/Step242/Step242SubsetDecoder.csindex f164e97f..404123c1 100644--- a/Aetheris.Kernel.Core/Step242/Step242SubsetDecoder.cs+++ b/Aetheris.Kernel.Core/Step242/Step242SubsetDecoder.cs@@ -8,6 +8,31 @@ namespace Aetheris.Kernel.Core.Step242;internal static class Step242SubsetDecoder{+ public static Step242EntityConstructor? TryGetConstructor(Step242EntityInstance entityValue, string name)+ {+ if (entityValue is Step242SimpleEntityInstance simple)+ {+ return string.Equals(simple.Constructor.Name, name, StringComparison.Ordinal)+ ? simple.Constructor+ : null;+ }++ if (entityValue is not Step242ComplexEntityInstance complex)+ {+ return null;+ }++ for (var i = 0; i < complex.Constructors.Count; i++)+ {+ if (string.Equals(complex.Constructors[i].Name, name, StringComparison.Ordinal))+ {+ return complex.Constructors[i];+ }+ }++ return null;+ }+public static KernelResult<Step242EntityReference> ReadReference(Step242ParsedEntity entity, int argumentIndex, string context){if (argumentIndex < 0 || argumentIndex >= entity.Arguments.Count)diff --git a/Aetheris.Kernel.Core/Step242/Step242SubsetParser.cs b/Aetheris.Kernel.Core/Step242/Step242SubsetParser.csindex f660364c..96482cac 100644--- a/Aetheris.Kernel.Core/Step242/Step242SubsetParser.cs+++ b/Aetheris.Kernel.Core/Step242/Step242SubsetParser.cs@@ -58,10 +58,16 @@ internal static class Step242SubsetParserreturn KernelResult<Step242ParsedDocument>.Success(new Step242ParsedDocument(entities));}- private static KernelResult<Step242ParsedDocument> Failure(string message, string source) =>- KernelResult<Step242ParsedDocument>.Failure([- new KernelDiagnostic(KernelDiagnosticCode.InvalidArgument, KernelDiagnosticSeverity.Error, message, source)+ private static KernelResult<Step242ParsedDocument> Failure(string message, string source)+ {+ var code = string.Equals(source, "Importer.StepSyntax.ComplexInstance", StringComparison.Ordinal)+ ? KernelDiagnosticCode.ValidationFailed+ : KernelDiagnosticCode.InvalidArgument;++ return KernelResult<Step242ParsedDocument>.Failure([+ new KernelDiagnostic(code, KernelDiagnosticSeverity.Error, message, source)]);+ }private sealed class Reader(string text){@@ -129,14 +135,67 @@ internal static class Step242SubsetParserSkipWhitespace();Expect('=');SkipWhitespace();+ var instance = ReadEntityInstance();+ SkipWhitespace();+ Expect(';');+ return new Step242ParsedEntity(id, instance);+ }++ private Step242EntityInstance ReadEntityInstance()+ {+ SkipWhitespace();+ if (End)+ {+ throw Error("Unexpected end of text in entity instance.");+ }++ if (Peek() != '(')+ {+ return new Step242SimpleEntityInstance(ReadEntityConstructor());+ }++ const string complexSource = "Importer.StepSyntax.ComplexInstance";+ Expect('(');++ var constructors = new List<Step242EntityConstructor>();+ while (true)+ {+ SkipWhitespace();+ if (End)+ {+ throw Error("Unexpected end of text in complex instance.", complexSource);+ }++ if (Peek() == ')')+ {+ _index++;+ break;+ }++ if (!IsIdentifierStart(Peek()))+ {+ throw Error("Invalid complex instance element.", complexSource);+ }++ constructors.Add(ReadEntityConstructor());+ }++ if (constructors.Count == 0)+ {+ throw Error("Complex instance must contain at least one entity.", complexSource);+ }++ return new Step242ComplexEntityInstance(constructors);+ }++ private Step242EntityConstructor ReadEntityConstructor()+ {var name = ReadIdentifier();name = name.ToUpperInvariant();SkipWhitespace();Expect('(');var args = ReadArgumentList();- SkipWhitespace();- Expect(';');- return new Step242ParsedEntity(id, name, args);+ return new Step242EntityConstructor(name, args);}public void ReadToStatementTerminator()@@ -416,6 +475,8 @@ internal static class Step242SubsetParserreturn text[start.._index];}+ private static bool IsIdentifierStart(char c) => char.IsLetter(c) || c == '_';+private int ReadInteger(){var start = _index;@@ -489,7 +550,12 @@ internal sealed class Step242ParseException(string message, string sourceTag) :public string SourceTag { get; } = sourceTag;}-internal sealed record Step242ParsedEntity(int Id, string Name, IReadOnlyList<Step242Value> Arguments);+internal sealed record Step242ParsedEntity(int Id, Step242EntityInstance Instance)+{+ public string Name => Instance.PrimaryConstructor.Name;++ public IReadOnlyList<Step242Value> Arguments => Instance.PrimaryConstructor.Arguments;+}internal sealed class Step242ParsedDocument{@@ -510,7 +576,7 @@ internal sealed class Step242ParsedDocumentreturn Failure($"Missing referenced entity #{id}.", $"Entity:{id}");}- if (expectedName is not null && !string.Equals(entity.Name, expectedName, StringComparison.OrdinalIgnoreCase))+ if (expectedName is not null && Step242SubsetDecoder.TryGetConstructor(entity.Instance, expectedName) is null){return Failure($"Entity #{id} expected '{expectedName}' but found '{entity.Name}'.", $"Entity:{id}");}@@ -526,6 +592,23 @@ internal sealed class Step242ParsedDocumentinternal abstract record Step242Value;+internal abstract record Step242EntityInstance+{+ public abstract Step242EntityConstructor PrimaryConstructor { get; }+}++internal sealed record Step242SimpleEntityInstance(Step242EntityConstructor Constructor) : Step242EntityInstance+{+ public override Step242EntityConstructor PrimaryConstructor => Constructor;+}++internal sealed record Step242ComplexEntityInstance(IReadOnlyList<Step242EntityConstructor> Constructors) : Step242EntityInstance+{+ public override Step242EntityConstructor PrimaryConstructor => Constructors[0];+}++internal sealed record Step242EntityConstructor(string Name, IReadOnlyList<Step242Value> Arguments);+internal sealed record Step242EntityReference(int TargetId) : Step242Value;internal sealed record Step242StringValue(string Value) : Step242Value;diff --git a/docs/support-matrix-step242-v0.md b/docs/support-matrix-step242-v0.mdindex 87f2987e..6d5d847e 100644--- a/docs/support-matrix-step242-v0.md+++ b/docs/support-matrix-step242-v0.md@@ -7,6 +7,7 @@ Aetheris STEP242 v0 is intentionally narrow and deterministic.- Single solid only: exactly one `MANIFOLD_SOLID_BREP` root is required.- Backend-authoritative flow: import -> render -> canonical export (+ SHA256).- Deterministic diagnostics: stable `(code, source, messagePrefix)` for expected fail cases.+- STEP Part 21 complex entity-instance assignments are accepted for parser/decoder context use (for example `#5=(GEOMETRIC_REPRESENTATION_CONTEXT(...)...REPRESENTATION_CONTEXT(...));`).## Supported entity families (Tier1)diff --git a/testdata/step242/manifests/nist.v0.report.json b/testdata/step242/manifests/nist.v0.report.jsonindex debf28b3..9c405162 100644--- a/testdata/step242/manifests/nist.v0.report.json+++ b/testdata/step242/manifests/nist.v0.report.json@@ -8,7 +8,7 @@"firstDiagnostic": {"code": "InvalidArgument","source": "Parser.Lexer",- "messagePrefix": "Expected identifier. (position *)."+ "messagePrefix": "Unsupported value token \u0027S\u0027. (position *)."},"diagnosticCount": 1,"exceptionEscaped": false,@@ -32,7 +32,7 @@"firstDiagnostic": {"code": "InvalidArgument","source": "Parser.Lexer",- "messagePrefix": "Expected identifier. (position *)."+ "messagePrefix": "Unsupported value token \u0027L\u0027. (position *)."},"diagnosticCount": 1,"exceptionEscaped": false,@@ -56,7 +56,7 @@"firstDiagnostic": {"code": "InvalidArgument","source": "Parser.Lexer",- "messagePrefix": "Expected identifier. (position *)."+ "messagePrefix": "Unsupported value token \u0027S\u0027. (position *)."},"diagnosticCount": 1,"exceptionEscaped": false,@@ -80,7 +80,7 @@"firstDiagnostic": {"code": "InvalidArgument","source": "Parser.Lexer",- "messagePrefix": "Expected identifier. (position *)."+ "messagePrefix": "Unsupported value token \u0027L\u0027. (position *)."},"diagnosticCount": 1,"exceptionEscaped": false,@@ -128,7 +128,7 @@"firstDiagnostic": {"code": "InvalidArgument","source": "Parser.Lexer",- "messagePrefix": "Expected identifier. (position *)."+ "messagePrefix": "Unsupported value token \u0027P\u0027. (position *)."},"diagnosticCount": 1,"exceptionEscaped": false,@@ -152,7 +152,7 @@"firstDiagnostic": {"code": "InvalidArgument","source": "Parser.Lexer",- "messagePrefix": "Expected identifier. (position *)."+ "messagePrefix": "Unsupported value token \u0027L\u0027. (position *)."},"diagnosticCount": 1,"exceptionEscaped": false,@@ -176,7 +176,7 @@"firstDiagnostic": {"code": "InvalidArgument","source": "Parser.Lexer",- "messagePrefix": "Expected identifier. (position *)."+ "messagePrefix": "Unsupported value token \u0027P\u0027. (position *)."},"diagnosticCount": 1,"exceptionEscaped": false,@@ -200,7 +200,7 @@"firstDiagnostic": {"code": "InvalidArgument","source": "Parser.Lexer",- "messagePrefix": "Expected identifier. (position *)."+ "messagePrefix": "Unsupported value token \u0027S\u0027. (position *)."},"diagnosticCount": 1,"exceptionEscaped": false,@@ -224,7 +224,7 @@"firstDiagnostic": {"code": "InvalidArgument","source": "Parser.Lexer",- "messagePrefix": "Expected identifier. (position *)."+ "messagePrefix": "Unsupported value token \u0027L\u0027. (position *)."},"diagnosticCount": 1,"exceptionEscaped": false,@@ -248,7 +248,7 @@"firstDiagnostic": {"code": "InvalidArgument","source": "Parser.Lexer",- "messagePrefix": "Expected identifier. (position *)."+ "messagePrefix": "Unsupported value token \u0027L\u0027. (position *)."},"diagnosticCount": 1,"exceptionEscaped": false,@@ -272,7 +272,7 @@"firstDiagnostic": {"code": "InvalidArgument","source": "Parser.Lexer",- "messagePrefix": "Expected identifier. (position *)."+ "messagePrefix": "Unsupported value token \u0027L\u0027. (position *)."},"diagnosticCount": 1,"exceptionEscaped": false,@@ -296,7 +296,7 @@"firstDiagnostic": {"code": "InvalidArgument","source": "Parser.Lexer",- "messagePrefix": "Expected identifier. (position *)."+ "messagePrefix": "Unsupported value token \u0027L\u0027. (position *)."},"diagnosticCount": 1,"exceptionEscaped": false,@@ -368,7 +368,7 @@"firstDiagnostic": {"code": "InvalidArgument","source": "Parser.Lexer",- "messagePrefix": "Expected identifier. (position *)."+ "messagePrefix": "Unsupported value token \u0027L\u0027. (position *)."},"diagnosticCount": 1,"exceptionEscaped": false,diff --git a/testdata/step242/syntax-robustness/complex-context-min.step b/testdata/step242/syntax-robustness/complex-context-min.stepnew file mode 100644index 00000000..5294db13--- /dev/null+++ b/testdata/step242/syntax-robustness/complex-context-min.step@@ -0,0 +1,22 @@+ISO-10303-21;+HEADER;+FILE_DESCRIPTION(('complex context parser fixture'),'2;1');+FILE_NAME('complex-context-min.step','2026-03-05T00:00:00',('Aetheris'),('Aetheris'),'','Aetheris','');+FILE_SCHEMA(('AP242_MANAGED_MODEL_BASED_3D_ENGINEERING_MIM_LF'));+ENDSEC;+DATA;+#5=(GEOMETRIC_REPRESENTATION_CONTEXT(3)GLOBAL_UNCERTAINTY_ASSIGNED_CONTEXT((#16))GLOBAL_UNIT_ASSIGNED_CONTEXT((#18,#19,#20))REPRESENTATION_CONTEXT('NONE','WORKSPACE'));+#13=ADVANCED_BREP_SHAPE_REPRESENTATION('0430200200',(#24,#25),#5);+#16=UNCERTAINTY_MEASURE_WITH_UNIT(1.E-6,#18,'DISTANCE_ACCURACY_VALUE','');+#18=(LENGTH_UNIT()NAMED_UNIT(*)SI_UNIT(.MILLI.,.METRE.));+#19=(PLANE_ANGLE_UNIT()NAMED_UNIT(*)SI_UNIT($,.RADIAN.));+#20=(SOLID_ANGLE_UNIT()NAMED_UNIT(*)SI_UNIT($,.STERADIAN.));+#24=ADVANCED_FACE((),#26,.T.);+#25=ADVANCED_FACE((),#26,.T.);+#26=SPHERICAL_SURFACE('',#27,1.0);+#27=AXIS2_PLACEMENT_3D('',#28,#29,#30);+#28=CARTESIAN_POINT('',(0.,0.,0.));+#29=DIRECTION('',(0.,0.,1.));+#30=DIRECTION('',(1.,0.,0.));+ENDSEC;+END-ISO-10303-21;