-
-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathProgram.cs
More file actions
179 lines (160 loc) · 5.8 KB
/
Program.cs
File metadata and controls
179 lines (160 loc) · 5.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
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)
{
if (args.Length == 0)
{
Console.Error.WriteLine("Usage: SchematicReader <file.SchDoc>");
Console.Error.WriteLine("Reads an Altium SchDoc file and outputs JSON to stdout.");
Environment.Exit(1);
}
string testFile = args[0];
try
{
using var reader = new SchDocReader();
var schDoc = reader.Read(testFile);
var output = new SchematicDump
{
FileName = Path.GetFileName(testFile),
Components = schDoc.Items.OfType<SchComponent>().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<SchPin>().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<SchWire>().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<SchNetLabel>().Select(n => new NetLabelDump
{
Text = n.Text,
X = (int)(n.Location.X.ToMils()),
Y = (int)(n.Location.Y.ToMils())
}).ToList(),
PowerPorts = schDoc.Items.OfType<SchPowerObject>().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 = new StatsDump
{
TotalComponents = schDoc.Items.OfType<SchComponent>().Count(),
TotalWires = schDoc.Items.OfType<SchWire>().Count(),
TotalPins = schDoc.Items.OfType<SchComponent>()
.SelectMany(c => c.GetPrimitivesOfType<SchPin>()).Count(),
TotalNetLabels = schDoc.Items.OfType<SchNetLabel>().Count(),
TotalPowerPorts = schDoc.Items.OfType<SchPowerObject>().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<ComponentDump> Components { get; set; }
public List<WireDump> Wires { get; set; }
public List<NetLabelDump> NetLabels { get; set; }
public List<PowerPortDump> 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 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<string> PrimitiveTypes { get; set; }
public List<PinDump> 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; }
public int ConnY { get; set; }
public int Length { get; set; }
public string Electrical { get; set; }
}
class WireDump
{
public List<PointDump> 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; }
}