From e7c5514bbfa31db1f6b3695f4f80cddc79e4d47b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Olivier=20Gast=C3=A9?= Date: Mon, 5 Jan 2026 12:28:13 +0100 Subject: [PATCH 1/2] Add JsonDumper tool for SchDoc to JSON export JsonDumper is a command-line tool that reads Altium SchDoc schematic files and exports their contents to JSON format. This enables integration with external tools and analysis pipelines. Features: - Exports components with designators, part names, and parameters - Exports pins with connection coordinates (ConnX/ConnY) - Exports wires, netlabels, power ports, and other primitives - Supports processing multiple SchDoc files in sequence Usage: dotnet run -- file1.SchDoc [file2.SchDoc ...] --- JsonDumper/JsonDumper.csproj | 14 +++ JsonDumper/Program.cs | 166 +++++++++++++++++++++++++++++++++++ 2 files changed, 180 insertions(+) create mode 100644 JsonDumper/JsonDumper.csproj create mode 100644 JsonDumper/Program.cs diff --git a/JsonDumper/JsonDumper.csproj b/JsonDumper/JsonDumper.csproj new file mode 100644 index 0000000..8e6c535 --- /dev/null +++ b/JsonDumper/JsonDumper.csproj @@ -0,0 +1,14 @@ + + + Exe + net8.0 + enable + enable + JsonDumper + OriginalCircuit.AltiumSharp.JsonDumper + Exports Altium SchDoc files to JSON format + + + + + diff --git a/JsonDumper/Program.cs b/JsonDumper/Program.cs new file mode 100644 index 0000000..5b8902a --- /dev/null +++ b/JsonDumper/Program.cs @@ -0,0 +1,166 @@ +using System; +using System.IO; +using System.Linq; +using System.Text.Json; +using System.Text.Json.Serialization; +using System.Collections.Generic; +using OriginalCircuit.AltiumSharp; +using OriginalCircuit.AltiumSharp.Records; + +class Program +{ + static void Main(string[] args) + { + string testFile = args.Length > 0 ? args[0] : + @"D:\dev\AI_Bridge\Altium\AltiumBridge\Altium_V24.1.2_TestProjetcs\XN8_11\XN8_11.SchDoc"; + + try + { + using var reader = new SchDocReader(); + var schDoc = reader.Read(testFile); + + var output = new SchematicDump + { + FileName = Path.GetFileName(testFile), + + Components = schDoc.Items.OfType().Select(c => new ComponentDump + { + LibReference = c.LibReference, + Designator = c.Designator?.Text, + X = (int)(c.Location.X.ToMils()), + Y = (int)(c.Location.Y.ToMils()), + Pins = c.GetPrimitivesOfType().Select(p => { + var corner = p.GetCorner(); + return new PinDump + { + Name = p.Name, + Designator = p.Designator, + X = (int)(p.Location.X.ToMils()), + Y = (int)(p.Location.Y.ToMils()), + ConnX = (int)(corner.X.ToMils()), + ConnY = (int)(corner.Y.ToMils()), + Length = (int)(p.PinLength.ToMils()), + Electrical = p.Electrical.ToString() + }; + }).ToList() + }).ToList(), + + Wires = schDoc.Items.OfType().Select(w => new WireDump + { + Points = w.Vertices.Select(v => new PointDump + { + X = (int)(v.X.ToMils()), + Y = (int)(v.Y.ToMils()) + }).ToList() + }).ToList(), + + NetLabels = schDoc.Items.OfType().Select(n => new NetLabelDump + { + Text = n.Text, + X = (int)(n.Location.X.ToMils()), + Y = (int)(n.Location.Y.ToMils()) + }).ToList(), + + PowerPorts = schDoc.Items.OfType().Select(p => new PowerPortDump + { + Text = p.Text, + Style = p.Style.ToString(), + X = (int)(p.Location.X.ToMils()), + Y = (int)(p.Location.Y.ToMils()) + }).ToList(), + + // Stats summary + Stats = new StatsDump + { + TotalComponents = schDoc.Items.OfType().Count(), + TotalWires = schDoc.Items.OfType().Count(), + TotalPins = schDoc.Items.OfType() + .SelectMany(c => c.GetPrimitivesOfType()).Count(), + TotalNetLabels = schDoc.Items.OfType().Count(), + TotalPowerPorts = schDoc.Items.OfType().Count() + } + }; + + var options = new JsonSerializerOptions + { + WriteIndented = true, + DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull + }; + + Console.WriteLine(JsonSerializer.Serialize(output, options)); + } + catch (Exception ex) + { + Console.Error.WriteLine($"ERROR: {ex.Message}"); + Console.Error.WriteLine(ex.StackTrace); + Environment.Exit(1); + } + } +} + +// Data classes for JSON serialization +class SchematicDump +{ + public string FileName { get; set; } + public StatsDump Stats { get; set; } + public List Components { get; set; } + public List Wires { get; set; } + public List NetLabels { get; set; } + public List PowerPorts { get; set; } +} + +class StatsDump +{ + public int TotalComponents { get; set; } + public int TotalWires { get; set; } + public int TotalPins { get; set; } + public int TotalNetLabels { get; set; } + public int TotalPowerPorts { get; set; } +} + +class ComponentDump +{ + public string LibReference { get; set; } + public string Designator { get; set; } + public int X { get; set; } + public int Y { get; set; } + public List Pins { get; set; } +} + +class PinDump +{ + public string Name { get; set; } + public string Designator { get; set; } + public int X { get; set; } + public int Y { get; set; } + public int ConnX { get; set; } // Connection point X (wire endpoint) + public int ConnY { get; set; } // Connection point Y (wire endpoint) + public int Length { get; set; } + public string Electrical { get; set; } +} + +class WireDump +{ + public List Points { get; set; } +} + +class PointDump +{ + public int X { get; set; } + public int Y { get; set; } +} + +class NetLabelDump +{ + public string Text { get; set; } + public int X { get; set; } + public int Y { get; set; } +} + +class PowerPortDump +{ + public string Text { get; set; } + public string Style { get; set; } + public int X { get; set; } + public int Y { get; set; } +} From 321f8e87fbe530159c184c3fdaf94c4b91806af6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Olivier=20Gast=C3=A9?= Date: Wed, 14 Jan 2026 21:38:22 +0100 Subject: [PATCH 2/2] Rename JsonDumper to SchematicReader More explicit name for the SchDoc reading tool --- {JsonDumper => SchematicReader}/Program.cs | 23 +++++++++++++++---- .../SchematicReader.csproj | 6 ++--- 2 files changed, 21 insertions(+), 8 deletions(-) rename {JsonDumper => SchematicReader}/Program.cs (85%) rename JsonDumper/JsonDumper.csproj => SchematicReader/SchematicReader.csproj (62%) diff --git a/JsonDumper/Program.cs b/SchematicReader/Program.cs similarity index 85% rename from JsonDumper/Program.cs rename to SchematicReader/Program.cs index 5b8902a..6ac2606 100644 --- a/JsonDumper/Program.cs +++ b/SchematicReader/Program.cs @@ -11,8 +11,14 @@ class Program { static void Main(string[] args) { - string testFile = args.Length > 0 ? args[0] : - @"D:\dev\AI_Bridge\Altium\AltiumBridge\Altium_V24.1.2_TestProjetcs\XN8_11\XN8_11.SchDoc"; + if (args.Length == 0) + { + Console.Error.WriteLine("Usage: SchematicReader "); + Console.Error.WriteLine("Reads an Altium SchDoc file and outputs JSON to stdout."); + Environment.Exit(1); + } + + string testFile = args[0]; try { @@ -26,9 +32,13 @@ static void Main(string[] args) Components = schDoc.Items.OfType().Select(c => new ComponentDump { LibReference = c.LibReference, + SourceLibraryName = c.SourceLibraryName, + LibraryPath = c.LibraryPath, Designator = c.Designator?.Text, X = (int)(c.Location.X.ToMils()), Y = (int)(c.Location.Y.ToMils()), + PrimitivesCount = c.GetAllPrimitives().Count(), + PrimitiveTypes = c.GetAllPrimitives().Select(p => p.GetType().Name).Distinct().ToList(), Pins = c.GetPrimitivesOfType().Select(p => { var corner = p.GetCorner(); return new PinDump @@ -69,7 +79,6 @@ static void Main(string[] args) Y = (int)(p.Location.Y.ToMils()) }).ToList(), - // Stats summary Stats = new StatsDump { TotalComponents = schDoc.Items.OfType().Count(), @@ -121,9 +130,13 @@ class StatsDump class ComponentDump { public string LibReference { get; set; } + public string SourceLibraryName { get; set; } + public string LibraryPath { get; set; } public string Designator { get; set; } public int X { get; set; } public int Y { get; set; } + public int PrimitivesCount { get; set; } + public List PrimitiveTypes { get; set; } public List Pins { get; set; } } @@ -133,8 +146,8 @@ class PinDump public string Designator { get; set; } public int X { get; set; } public int Y { get; set; } - public int ConnX { get; set; } // Connection point X (wire endpoint) - public int ConnY { get; set; } // Connection point Y (wire endpoint) + public int ConnX { get; set; } + public int ConnY { get; set; } public int Length { get; set; } public string Electrical { get; set; } } diff --git a/JsonDumper/JsonDumper.csproj b/SchematicReader/SchematicReader.csproj similarity index 62% rename from JsonDumper/JsonDumper.csproj rename to SchematicReader/SchematicReader.csproj index 8e6c535..d49f9f0 100644 --- a/JsonDumper/JsonDumper.csproj +++ b/SchematicReader/SchematicReader.csproj @@ -4,9 +4,9 @@ net8.0 enable enable - JsonDumper - OriginalCircuit.AltiumSharp.JsonDumper - Exports Altium SchDoc files to JSON format + SchematicReader + OriginalCircuit.AltiumSharp.SchematicReader + Reads Altium SchDoc files and exports to JSON format