Skip to content

Next Gen STEP AP242 Native-ish BREP Geometric Modeling Kernel

12.9 KB
Raw Blame
using System.Globalization;
using System.Security.Cryptography;
using System.Text;
using System.Text.Json;
using System.Text.RegularExpressions;
using Aetheris.Kernel.Core.Math;
using Aetheris.Kernel.Core.Results;
using Aetheris.Kernel.Core.Step242;

namespace Aetheris.Kernel.Firmament.Assembly;

public sealed record FirmasmComponentPackageItem(string DefinitionStableId, string RelativePath, string Sha256);
public sealed record FirmasmImportPackageResult(string FirmasmPath, string ManifestPath, Step242ProductStructure ProductStructure, IReadOnlyList<FirmasmComponentPackageItem> Components, string FirmasmSha256);

public static class Step242FirmasmPackageImporter
{
    public static KernelResult<FirmasmImportPackageResult> Import(string sourceStepPath, string outputDirectory)
    {
        var structure = Step242AssemblyImporter.Import(File.ReadAllText(sourceStepPath));
        if (!structure.IsSuccess) return KernelResult<FirmasmImportPackageResult>.Failure(structure.Diagnostics);
        var output = Path.GetFullPath(outputDirectory); var componentsDirectory = Path.Combine(output, "components");
        Directory.CreateDirectory(componentsDirectory);
        var componentItems = new List<FirmasmComponentPackageItem>();
        var componentByDefinition = new Dictionary<string, string>(StringComparer.Ordinal);
        foreach (var (definition, index) in structure.Value.Definitions.Where(item => item.Geometry is not null).OrderBy(item => item.StableId, StringComparer.Ordinal).Select((item, index) => (item, index)))
        {
            var relative = $"components/{index + 1:D4}-{Identifier(definition.Name).ToLowerInvariant()}.step";
            var export = Step242Exporter.ExportBody(definition.Geometry!, options: new Step242ExportOptions { ProductId = definition.StableId, ProductName = definition.Name });
            if (!export.IsSuccess) return KernelResult<FirmasmImportPackageResult>.Failure(export.Diagnostics);
            var path = Path.Combine(output, relative.Replace('/', Path.DirectorySeparatorChar));
            File.WriteAllText(path, export.Value, new UTF8Encoding(false));
            var hash = Sha(export.Value); componentItems.Add(new(definition.StableId, relative, hash)); componentByDefinition[definition.StableId] = relative;
        }
        var source = GenerateSource(structure.Value, componentByDefinition, Path.GetFileName(sourceStepPath));
        var firmasmPath = Path.Combine(output, Identifier(Path.GetFileNameWithoutExtension(sourceStepPath)) + ".firmasm");
        File.WriteAllText(firmasmPath, source, new UTF8Encoding(false));
        var manifestPath = Path.Combine(output, "component-package.json");
        var manifest = new
        {
            schema = "aetheris/firmasm-component-package/m2",
            sourceStep = Path.GetFileName(sourceStepPath),
            firmasm = Path.GetFileName(firmasmPath),
            normalization = structure.Diagnostics.Any(diagnostic => diagnostic.Source == "Importer.Assembly.MultiplicityNormalization") ? "flat-assembly-from-ambiguous-multiplicity" : "explicit-ap242-occurrence-hierarchy",
            definitionCount = componentItems.Count,
            occurrenceCount = structure.Value.Occurrences.Count,
            components = componentItems
        };
        File.WriteAllText(manifestPath, JsonSerializer.Serialize(manifest, new JsonSerializerOptions { WriteIndented = true }), new UTF8Encoding(false));
        return KernelResult<FirmasmImportPackageResult>.Success(new(firmasmPath, manifestPath, structure.Value, componentItems, Sha(source)), structure.Diagnostics);
    }

    public static string GenerateSource(Step242ProductStructure structure, IReadOnlyDictionary<string, string> componentByDefinition, string provenance, bool componentReferences = false)
    {
        var definitions = structure.Definitions.ToDictionary(item => item.StableId, StringComparer.Ordinal);
        var occurrences = structure.Occurrences.ToDictionary(item => item.StableId, StringComparer.Ordinal);
        var children = structure.Occurrences.GroupBy(item => item.ParentStableId).ToDictionary(group => group.Key ?? string.Empty, group => group.OrderBy(item => item.StableId, StringComparer.Ordinal).ToArray(), StringComparer.Ordinal);
        var rootName = Identifier(definitions.GetValueOrDefault(structure.RootDefinitionStableId)?.Name ?? "ImportedAssembly");
        var builder = new StringBuilder();
        builder.AppendLine($"// Imported from {provenance}; occurrence transforms are interchange evidence, not semantic Mates.");
        builder.AppendLine("// Multiplicity without trustworthy foreign hierarchy is intentionally normalized to a flat Assembly.");
        builder.AppendLine($"Assembly {rootName} {{");
        builder.AppendLine($"  <Assembly {rootName}>");
        foreach (var occurrence in children.GetValueOrDefault(string.Empty, [])) Emit(occurrence, 4);
        builder.AppendLine("  </Assembly>");
        builder.AppendLine($"  Anchor: {rootName};");
        builder.AppendLine("}");
        return builder.ToString();

        void Emit(Step242ImportedProductOccurrence occurrence, int indent)
        {
            var padding = new string(' ', indent); var id = Identifier(occurrence.StableId);
            var definition = definitions[occurrence.DefinitionStableId];
            var nested = definition.Geometry is null;
            if (nested)
            {
                builder.AppendLine($"{padding}<Assembly {id}>");
                builder.AppendLine($"{padding}  // STEP occurrence #{occurrence.StepEntityId}; Placement authority: ImportedOccurrence.");
                builder.AppendLine($"{padding}  Placement ImportedOccurrence = [{string.Join(", ", occurrence.LocalTransform.Select(Number))}];");
                foreach (var child in children.GetValueOrDefault(occurrence.StableId, [])) Emit(child, indent + 2);
                builder.AppendLine($"{padding}</Assembly>");
                return;
            }
            var component = componentByDefinition[occurrence.DefinitionStableId];
            builder.AppendLine(componentReferences ? $"{padding}<Part {id} = {component}>" : $"{padding}<Part {id} = ExternalStep<\"{component}\">>");
            builder.AppendLine($"{padding}  // STEP occurrence #{occurrence.StepEntityId}; Placement authority: ImportedOccurrence.");
            builder.AppendLine($"{padding}  Placement ImportedOccurrence = [{string.Join(", ", occurrence.LocalTransform.Select(Number))}];");
            builder.AppendLine($"{padding}</Part>");
        }
    }

    private static string Identifier(string value)
    {
        var result = Regex.Replace(value, "[^A-Za-z0-9_]", "_");
        if (result.Length == 0) result = "Imported";
        return char.IsLetter(result[0]) || result[0] == '_' ? result : "_" + result;
    }
    private static string Number(double value) => value.ToString("R", CultureInfo.InvariantCulture);
    private static string Sha(string value) => Convert.ToHexString(SHA256.HashData(Encoding.UTF8.GetBytes(value)));
}

public static class AssemblyIrAp242Exporter
{
    public static KernelResult<string> Export(AssemblyM1CompilationResult compilation)
    {
        var prepared = PrepareExport(compilation);
        return prepared.IsSuccess ? prepared.Value() : KernelResult<string>.Failure(prepared.Diagnostics);
    }

    /// <summary>
    /// Every structural check of <see cref="Export"/> (solved roots and child transforms,
    /// definition-owned usages), returning the AP242 writer to run when STEP is requested.
    /// The writer produces exactly the text <see cref="Export"/> would.
    /// </summary>
    public static KernelResult<Func<KernelResult<string>>> PrepareExport(AssemblyM1CompilationResult compilation)
    {
        static KernelResult<Func<KernelResult<string>>> Failure(string message) =>
            KernelResult<Func<KernelResult<string>>>.Failure(AssemblyIrAp242Exporter.Failure(message).Diagnostics);
        if (!compilation.IsSuccess || compilation.Ir is null || compilation.Geometry is null)
            return Failure("AssemblyIR must be successfully materialized before AP242 product-structure export.");
        var ir = compilation.Ir;
        var artifactByIdentity = compilation.Geometry.Artifact.Definitions.ToDictionary(item => item.DefinitionIdentity, item => item.StableId, StringComparer.Ordinal);
        var definitions = compilation.Geometry.DefinitionBodies.OrderBy(pair => pair.Key, StringComparer.Ordinal)
            .Select(pair => new Step242AssemblyDefinition(artifactByIdentity[pair.Key], pair.Key, pair.Value))
            .Concat((ir.AssemblyDefinitions ?? []).Select(definition => new Step242AssemblyDefinition(definition.StableId, definition.DefinitionIdentity, null)))
            .OrderBy(definition => definition.StableId, StringComparer.Ordinal).ToArray();
        var byId = ir.Instances.ToDictionary(item => item.StableId, StringComparer.Ordinal);
        var assemblyDefinitionByIdentity = (ir.AssemblyDefinitions ?? []).ToDictionary(item => item.DefinitionIdentity, item => item.StableId, StringComparer.Ordinal);
        // A reusable product owns its child usages. Recovering these from each
        // posed world occurrence introduces roundoff-dependent duplicate STEP
        // usages. Read the already-solved local definition once instead.
        var definitionChildTransforms = new Dictionary<(string Definition, string Child), IReadOnlyList<double>>();
        foreach (var definition in ir.AssemblyDefinitions ?? [])
        {
            var root = definition.LocalInstances.Single(item => item.ParentStableId is null);
            if (root.ResolvedTransform is null) return Failure($"Assembly definition '{definition.DefinitionIdentity}' has no solved local root.");
            var rootInverse = Transform3D.FromRowMajor(root.ResolvedTransform.Matrix).Inverse();
            foreach (var child in definition.LocalInstances.Where(item => item.ParentStableId == root.StableId))
            {
                if (child.ResolvedTransform is null) return Failure($"Assembly definition '{definition.DefinitionIdentity}' has an unsolved child '{child.Path}'.");
                definitionChildTransforms.Add((definition.DefinitionIdentity, child.Path.Segments.Last()),
                    (Transform3D.FromRowMajor(child.ResolvedTransform.Matrix) * rootInverse).ToRowMajor());
            }
        }
        foreach (var instance in ir.Instances.Where(item => item.ParentStableId is not null))
            if (byId[instance.ParentStableId!].IsEncapsulatedDefinition &&
                !definitionChildTransforms.ContainsKey((byId[instance.ParentStableId!].DefinitionIdentity, instance.Path.Segments.Last())))
                return Failure($"Occurrence '{instance.Path}' has no definition-owned local child transform.");
        var occurrences = ir.Instances.OrderBy(item => item.Path.Segments.Count).ThenBy(item => item.StableId, StringComparer.Ordinal).Select(instance =>
        {
            var world = instance.ResolvedTransform ?? AssemblyTransform.Identity;
            IReadOnlyList<double> local = world.Matrix;
            if (instance.ParentStableId is not null && byId[instance.ParentStableId].IsEncapsulatedDefinition)
                local = definitionChildTransforms[(byId[instance.ParentStableId].DefinitionIdentity, instance.Path.Segments.Last())];
            else if (instance.ParentStableId is not null && byId[instance.ParentStableId].ResolvedTransform is { } parentWorld)
            {
                var parent = Transform3D.FromRowMajor(parentWorld.Matrix);
                local = (Transform3D.FromRowMajor(world.Matrix) * parent.Inverse()).ToRowMajor();
            }
            return new Step242AssemblyOccurrence(instance.StableId, instance.Path.Segments.Last(), instance.ParentStableId,
                instance.Kind == AssemblyInstanceKind.Part ? artifactByIdentity[instance.DefinitionIdentity]
                    : instance.IsEncapsulatedDefinition ? assemblyDefinitionByIdentity[instance.DefinitionIdentity] : null, local);
        }).ToArray();
        var notes = (ir.Annotations?.Notes ?? []).Select(note => new Step242AssemblyNote(
            ir.Instances.Single(i => i.Path.ToString() == note.Target).StableId,
            new(note.Name, note.Target, note.Text))).ToList();
        if (ir.Annotations?.Release is { } release)
            foreach (var field in release.Fields.OrderBy(p => p.Key, StringComparer.Ordinal))
                notes.Add(new(ir.RootInstanceStableId, new($"{release.Name}.{field.Key}", ir.Name, field.Value)));
        var request = new Step242AssemblyExportModel(ir.Name, ir.RootInstanceStableId, definitions, occurrences,
            AssemblyPmiAuthoring.Header(ir.Annotations?.Release, ir.Name), notes);
        return KernelResult<Func<KernelResult<string>>>.Success(() => Step242AssemblyExporter.Export(request));
    }

    private static KernelResult<string> Failure(string message) => KernelResult<string>.Failure([
        new(Aetheris.Kernel.Core.Diagnostics.KernelDiagnosticCode.ValidationFailed, Aetheris.Kernel.Core.Diagnostics.KernelDiagnosticSeverity.Error, message, "Exporter.AssemblyIR")]);
}