-
-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathProgram.cs
More file actions
98 lines (85 loc) · 4.04 KB
/
Copy pathProgram.cs
File metadata and controls
98 lines (85 loc) · 4.04 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
// ============================================================================
// Example: Building a multi-part schematic component
// ============================================================================
//
// A multi-part (multi-unit) component is one physical package drawn as several
// independent symbols — e.g. a dual gate placed as part A and part B. The package
// is one SchComponent with PartCount > 1; each child primitive is tagged with an
// OwnerPartId that says which part it belongs to.
//
// WHAT IT SHOWS
// ─────────────
// * SchComponent.PartCount = 2 for a two-part package.
// * Setting OwnerPartId on each pin and body so it renders only on its part.
// * Saving the symbol library and reading it back to confirm the part split.
//
// RUNNING
// ───────
// dotnet run --project examples/BuildMultiPartComponent
//
// ============================================================================
using OriginalCircuit.Altium;
using OriginalCircuit.Altium.Models.Sch;
using OriginalCircuit.Eda.Enums;
using PinElectricalType = OriginalCircuit.Altium.Models.Sch.PinElectricalType;
using OriginalCircuit.Eda.Primitives;
var outDir = Path.Combine(Path.GetTempPath(), "AltiumMultiPartExample");
Directory.CreateDirectory(outDir);
// A dual 2-input gate: two identical parts (A and B) plus shared power pins.
var gate = new SchComponent
{
Name = "DUAL_GATE",
Description = "Dual 2-input gate (2 parts)",
DesignatorPrefix = "U",
PartCount = 2
};
for (var part = 1; part <= 2; part++)
{
// Body rectangle belongs to this part only.
gate.AddRectangle(new SchRectangle
{
Corner1 = new CoordPoint(Coord.FromMm(-5.08), Coord.FromMm(-5.08)),
Corner2 = new CoordPoint(Coord.FromMm(5.08), Coord.FromMm(5.08)),
Color = 128,
OwnerPartId = part
});
// Part 1 uses pins 1/2/3, part 2 uses 4/5/6.
var baseDes = (part - 1) * 3;
AddPin(gate, $"{baseDes + 1}", "A", -7.62, 2.54, PinOrientation.Right, PinElectricalType.Input, part);
AddPin(gate, $"{baseDes + 2}", "B", -7.62, -2.54, PinOrientation.Right, PinElectricalType.Input, part);
AddPin(gate, $"{baseDes + 3}", "Y", 7.62, 0.0, PinOrientation.Left, PinElectricalType.Output, part);
}
// Shared power pins — conventionally drawn on part 1.
AddPin(gate, "14", "VCC", 0.0, 7.62, PinOrientation.Down, PinElectricalType.Power, 1);
AddPin(gate, "7", "GND", 0.0, -7.62, PinOrientation.Up, PinElectricalType.Power, 1);
var lib = new SchLibrary();
lib.Add(gate);
Console.WriteLine($"Built {gate.Name}: PartCount = {gate.PartCount}");
PrintParts("In memory", gate);
// Save and read back to confirm the per-part assignment survives a round-trip.
var path = Path.Combine(outDir, "MultiPart.SchLib");
await lib.SaveAsync(path);
var loaded = (SchLibrary)await AltiumLibrary.OpenSchLibAsync(path);
var reloaded = (SchComponent)loaded.Components.First();
PrintParts($"Reloaded from {Path.GetFileName(path)} (PartCount = {reloaded.PartCount})", reloaded);
// ── Helpers ─────────────────────────────────────────────────────────────────
static void AddPin(SchComponent comp, string designator, string name,
double xMm, double yMm, PinOrientation orient, PinElectricalType type, int part)
{
var pin = SchPin.Create(designator)
.WithName(name)
.At(Coord.FromMm(xMm), Coord.FromMm(yMm))
.Length(Coord.FromMm(2.54))
.Orient(orient)
.Electrical(type)
.Build();
pin.OwnerPartId = part; // tag the pin with its part (the builder doesn't set this)
comp.AddPin(pin);
}
static void PrintParts(string heading, SchComponent comp)
{
Console.WriteLine($"\n{heading}:");
foreach (var group in comp.Pins.GroupBy(p => ((SchPin)p).OwnerPartId).OrderBy(g => g.Key))
Console.WriteLine($" Part {group.Key}: {group.Count()} pins " +
$"({string.Join(", ", group.Select(p => $"{p.Designator}:{p.Name}"))})");
}