Skip to content

Next Gen STEP AP242 Native-ish BREP Geometric Modeling Kernel

Preserve orphan planar faces during ctc_02 round-trip

Yuechen Lia965b74bfaff
diff --git a/Aetheris.Kernel.Core.Tests/Step242/Step242ExporterTests.cs b/Aetheris.Kernel.Core.Tests/Step242/Step242ExporterTests.cs
index c44ab9b2..886ffabc 100644
--- a/Aetheris.Kernel.Core.Tests/Step242/Step242ExporterTests.cs
+++ b/Aetheris.Kernel.Core.Tests/Step242/Step242ExporterTests.cs
@@ -206,6 +206,28 @@ END-ISO-10303-21;";
}
}
+ [Fact]
+ public void ExportBody_Ctc02RoundTrip_PreservesPlanarFaceWithCircularBoundaryNearExpectedCenter()
+ {
+ var fixturePath = Path.Combine(Step242CorpusManifestRunner.RepoRoot(), "testdata", "step242", "nist", "CTC", "nist_ctc_02_asme1_ap242-e2.stp");
+ var source = File.ReadAllText(fixturePath);
+
+ var import = Step242Importer.ImportBody(source);
+ Assert.True(import.IsSuccess);
+
+ var sourceFace = FindPlanarFaceWithCircle(import.Value, new Aetheris.Kernel.Core.Math.Point3D(0d, 812d, -140d), 42.5d);
+ Assert.True(sourceFace.HasValue);
+
+ var export = Step242Exporter.ExportBody(import.Value);
+ Assert.True(export.IsSuccess);
+
+ var roundTrip = Step242Importer.ImportBody(export.Value);
+ Assert.True(roundTrip.IsSuccess);
+
+ var roundTripFace = FindPlanarFaceWithCircle(roundTrip.Value, new Aetheris.Kernel.Core.Math.Point3D(0d, 812d, -140d), 42.5d);
+ Assert.True(roundTripFace.HasValue);
+ }
+
[Fact]
public void ExportBody_EmitsCanonicalRadianPlaneAngleUnitContext()
@@ -577,6 +599,66 @@ END-ISO-10303-21;";
private static string FormatPoint((double X, double Y, double Z) p) => $"({p.X}, {p.Y}, {p.Z})";
+ private static Aetheris.Kernel.Core.Topology.FaceId? FindPlanarFaceWithCircle(BrepBody body, Aetheris.Kernel.Core.Math.Point3D expectedCenter, double expectedRadius)
+ {
+ foreach (var face in body.Topology.Faces)
+ {
+ if (!body.Bindings.TryGetFaceBinding(face.Id, out var faceBinding))
+ {
+ continue;
+ }
+
+ if (!body.Geometry.TryGetSurface(faceBinding.SurfaceGeometryId, out var surface)
+ || surface is null
+ || surface.Kind != Aetheris.Kernel.Core.Geometry.SurfaceGeometryKind.Plane)
+ {
+ continue;
+ }
+
+ foreach (var loopId in face.LoopIds)
+ {
+ var loop = body.Topology.GetLoop(loopId);
+ foreach (var coedgeId in loop.CoedgeIds)
+ {
+ var coedge = body.Topology.GetCoedge(coedgeId);
+ if (!body.Bindings.TryGetEdgeBinding(coedge.EdgeId, out var edgeBinding))
+ {
+ continue;
+ }
+
+ if (!body.Geometry.TryGetCurve(edgeBinding.CurveGeometryId, out var curve)
+ || curve is null
+ || curve.Kind != Aetheris.Kernel.Core.Geometry.CurveGeometryKind.Circle3
+ || !curve.Circle3.HasValue)
+ {
+ continue;
+ }
+
+ var circle = curve.Circle3.Value;
+ if (double.Abs(circle.Radius - expectedRadius) > 1e-3)
+ {
+ continue;
+ }
+
+ if (Distance(circle.Center, expectedCenter) <= 1e-3)
+ {
+ return face.Id;
+ }
+ }
+ }
+ }
+
+ return null;
+ }
+
+ private static double Distance(Aetheris.Kernel.Core.Math.Point3D a, Aetheris.Kernel.Core.Math.Point3D b)
+ {
+ var dx = a.X - b.X;
+ var dy = a.Y - b.Y;
+ var dz = a.Z - b.Z;
+ return double.Sqrt((dx * dx) + (dy * dy) + (dz * dz));
+ }
+
private sealed record StepEntityRecords(
IReadOnlyDictionary<int, (double X, double Y, double Z)> Points,
IReadOnlyDictionary<int, int> VertexToPoint,
diff --git a/Aetheris.Kernel.Core/Step242/Step242Importer.cs b/Aetheris.Kernel.Core/Step242/Step242Importer.cs
index cb779feb..abd566eb 100644
--- a/Aetheris.Kernel.Core/Step242/Step242Importer.cs
+++ b/Aetheris.Kernel.Core/Step242/Step242Importer.cs
@@ -110,6 +110,9 @@ public static class Step242Importer
return KernelResult<BrepBody>.Failure(faceRefsResult.Diagnostics);
}
+ var faceEntityIds = faceRefsResult.Value.Select(r => r.TargetId).ToList();
+ AppendSupportedOrphanPlanarFaces(document, faceEntityIds);
+
var builder = new TopologyBuilder();
var geometry = new BrepGeometryStore();
var bindings = new BrepBindingModel();
@@ -122,9 +125,9 @@ public static class Step242Importer
var nextSurfaceGeometryId = 1;
var faceIds = new List<FaceId>();
- foreach (var faceRef in faceRefsResult.Value)
+ foreach (var faceEntityId in faceEntityIds)
{
- var faceEntityResult = document.TryGetEntity(faceRef.TargetId, "ADVANCED_FACE");
+ var faceEntityResult = document.TryGetEntity(faceEntityId, "ADVANCED_FACE");
if (!faceEntityResult.IsSuccess)
{
return KernelResult<BrepBody>.Failure(faceEntityResult.Diagnostics);
@@ -430,6 +433,90 @@ public static class Step242Importer
return KernelResult<BrepBody>.Success(body, validation.Diagnostics);
}
+ private static void AppendSupportedOrphanPlanarFaces(Step242ParsedDocument document, IList<int> faceEntityIds)
+ {
+ var known = new HashSet<int>(faceEntityIds);
+
+ foreach (var entity in document.Entities)
+ {
+ if (!string.Equals(entity.Name, "ADVANCED_FACE", StringComparison.OrdinalIgnoreCase) || known.Contains(entity.Id))
+ {
+ continue;
+ }
+
+ var advancedFaceOffset = entity.Arguments.Count >= 4
+ && (entity.Arguments[0] is Step242StringValue || entity.Arguments[0] is Step242OmittedValue)
+ ? 1
+ : 0;
+
+ var surfaceResult = Step242SubsetDecoder.ReadEntityRefOrInlineConstructor(entity, advancedFaceOffset + 1, "ADVANCED_FACE surface");
+ if (!surfaceResult.IsSuccess)
+ {
+ continue;
+ }
+
+ string surfaceName;
+ if (surfaceResult.Value.IsReference)
+ {
+ var surfaceEntityResult = document.TryGetEntity(surfaceResult.Value.ReferenceId!.Value);
+ if (!surfaceEntityResult.IsSuccess)
+ {
+ continue;
+ }
+
+ surfaceName = surfaceEntityResult.Value.Name;
+ }
+ else
+ {
+ surfaceName = surfaceResult.Value.InlineName ?? string.Empty;
+ }
+
+ if (!string.Equals(surfaceName, "PLANE", StringComparison.OrdinalIgnoreCase))
+ {
+ continue;
+ }
+
+ var boundRefsResult = Step242SubsetDecoder.ReadAdvancedFaceBounds(entity, advancedFaceOffset, "ADVANCED_FACE bounds");
+ if (!boundRefsResult.IsSuccess || boundRefsResult.Value.Count == 0)
+ {
+ continue;
+ }
+
+ var allEdgeLoops = true;
+ foreach (var boundRef in boundRefsResult.Value)
+ {
+ var boundEntityResult = document.TryGetEntity(boundRef.TargetId);
+ if (!boundEntityResult.IsSuccess)
+ {
+ allEdgeLoops = false;
+ break;
+ }
+
+ var loopRefResult = Step242SubsetDecoder.ReadReference(boundEntityResult.Value, 1, "FACE_BOUND loop");
+ if (!loopRefResult.IsSuccess)
+ {
+ allEdgeLoops = false;
+ break;
+ }
+
+ var loopEntityResult = document.TryGetEntity(loopRefResult.Value.TargetId);
+ if (!loopEntityResult.IsSuccess || !string.Equals(loopEntityResult.Value.Name, "EDGE_LOOP", StringComparison.OrdinalIgnoreCase))
+ {
+ allEdgeLoops = false;
+ break;
+ }
+ }
+
+ if (!allEdgeLoops)
+ {
+ continue;
+ }
+
+ faceEntityIds.Add(entity.Id);
+ known.Add(entity.Id);
+ }
+ }
+
private static KernelResult<EdgeId> EnsureEdge(
Step242ParsedDocument document,
Step242ParsedEntity edgeCurveEntity,