-
Notifications
You must be signed in to change notification settings - Fork 7
/
board.js
206 lines (193 loc) · 7.76 KB
/
board.js
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
extensionId = api('createDialog',{title:'getId'})[0].id.split('-')[2]; // really bad hack to get the extension id ;)
extension = easyeda.extension.instances[extensionId];
const { Point } = extension.paper;
const { Line, assignIslands, makePolyLines, cleanupColinearTrackPair, splitIntersectingLines } = extension.line;
function floatToString(inputValue) {
return inputValue;
//return ('%f' % inputValue).rstrip('0').rstrip('.')
}
function pointToStr(pt) {
return floatToString(pt.x) + " " + floatToString(pt.y);
//return "${floatToString(round(pt.x + 4000, 5))} ${floatToString(round(pt.y + 3000, 5))}";
}
class Via {
constructor(pos, diameter, holeRadius, shapeId) {
this.pos = pos;
this.diameter = diameter;
this.holeRadius = holeRadius;
this.id = shapeId;
}
}
class Board {
constructor(json) {
console.log("Loading board");
this.nextTrackId = 0;
this.board = JSON.parse(json);
this.loadTracks();
console.log("Cleaning up overlapping tracks");
this.cleanupColinearTracks();
console.log("Splitting intersecting tracks");
this.splitIntersectingLines();
console.log("Identifying connected track sections");
this.assignIslands();
}
cleanupColinearTracks() {
for(const tracks of Object.values(this.tracksByNetAndLayer)) {
//var trackLookup = kdboxtree(list((t.bounds(), t) for t in tracks))
for(const t of tracks) {
for(const t2 of tracks) { // kdboxinside(trackLookup, t.bounds()):
if (1) //id(t) < id(t2): doens't work???
if (cleanupColinearTrackPair(t, t2, tracks))
break;
}
}
}
}
splitIntersectingLines() {
for(const tracks of Object.values(this.tracksByNetAndLayer)) {
splitIntersectingLines(tracks);
}
}
assignIslands() {
for(const tracks of Object.values(this.tracksByNetAndLayer)) {
assignIslands(tracks);
}
}
allocateShapeId() {
this.highestShapeId += 1;
return `gge${this.highestShapeId}`;
}
addShape(shape) {
this.board.shape.push(shape);
}
addSignalLayers(layers) {
for(const layer of layers) {
const fields = layer.split("~");
// Add layer id only if it's a custom signal layer
if (fields.pop() == "Signal") {
this.signalLayers.push(fields[0])
}
}
}
loadTracks() {
this.highestShapeId = 0;
this.tracksByNetAndLayer = {};
this.viasByNet = {};
this.teardrops = [];
this.teardropIndices = [];
this.signalLayers = ["1", "2"]; // TopLayer & BottomLayer
this.addSignalLayers(this.board.layers);
this.loadShapes(this.board.shape);
}
loadShapes(shapes) {
for(const [shapeIndex, shapeStr] of shapes.entries()) {
const shape = shapeStr.split("~");
var shapeId;
if (shape[0] == "LIB") {
const [, x, y, attributes, rotation, importFlag, _shapeId, , , , locked] = shape;
shapeId = _shapeId;
const shapeList = shape.join("~").split("#@$").slice(1);
this.loadShapes(shapeList);
} else if (shape[0] == "TRACK") {
const [, widthStr, layer, net, coordsStr, _shapeId, locked] = shape;
shapeId = _shapeId;
if (this.signalLayers.includes(layer)) {
const key = net+'~'+layer;
if (!(key in this.tracksByNetAndLayer))
this.tracksByNetAndLayer[key] = [];
const tracks = this.tracksByNetAndLayer[key];
const coords = coordsStr.split(' ').map(Number);
const width = Number(widthStr);
var start = new Point(coords[0], coords[1]);
for(var i = 2; i < coords.length; i += 2) {
const end = new Point(coords[i], coords[i + 1]);
var t = new Line(start, end);
t.shapeIndex = shapeIndex;
t.id = shapeId;
t.width = width;
t.layer = layer;
t.net = net;
t.locked = locked;
start = end;
if (t.length > 0)
tracks.push(t);
}
}
} else if (shape[0] == 'VIA') {
const [, x, y, diameter, net, drill, _shapeId, locked] = shape;
shapeId = _shapeId;
const pos = new Point(Number(x), Number(y));
if (!(net in this.viasByNet))
this.viasByNet[net] = [];
this.viasByNet[net].push(new Via(pos, Number(diameter), Number(drill), shapeId));
} else if (shape[0] == 'PAD') {
const [, shapeType, x, y, w, h, layer, net, num, holeRadius, points,
rotation, _shapeId, holeLength, holePoints, plated, locked,
pasteExpansion, solderMaskExpansion] = shape;
shapeId = _shapeId;
const pos = new Point(Number(x), Number(y));
if (Number(holeRadius)) {
if (!(net in this.viasByNet))
this.viasByNet[net] = [];
this.viasByNet[net].push(new Via(pos, Math.min(Number(w), Number(h)), Number(holeRadius), shapeId));
}
} else if (shape[0] == 'SOLIDREGION') {
const [, layer, net, path, type, shapeId, teardrop, targetWire, targetPad] = shape;
if (teardrop) {
this.teardrops.push(shapeId);
this.teardropIndices.push(shapeIndex);
}
} else {
for(const field of shape) {
if (field.startsWith('gge')) {
shapeId = field;
break;
}
}
}
if (shapeId && shapeId.startsWith('gge') && shape[0] != 'LIB') {
const numericId = Number(shapeId.substr(3));
if (numericId > this.highestShapeId)
this.highestShapeId = numericId;
}
}
}
removeTeardrops() {
while(this.teardropIndices.length) {
this.board.shape.splice(this.teardropIndices.pop(), 1);
}
}
saveTracks() {
var tracksByShape = {};
for(const tracks of Object.values(this.tracksByNetAndLayer)) {
for(const track of tracks) {
if (!(track.shapeIndex in tracksByShape))
tracksByShape[track.shapeIndex] = [];
tracksByShape[track.shapeIndex].push(track);
}
}
for(var [shapeIndex, tracks] of Object.entries(tracksByShape)) {
const track = tracks[0];
var trackId = track.id;
for(const coords of makePolyLines(tracks)) {
if (shapeIndex === null)
trackId = this.allocateShapeId();
const shape = [
'TRACK',
floatToString(track.width),
track.layer,
track.net,
coords.map(pointToStr).join(' '),
trackId,
track.locked].join('~');
if (shapeIndex != null) {
this.board['shape'][shapeIndex] = shape;
shapeIndex = null;
} else {
this.addShape(shape);
}
}
}
}
}
extension.board = {Board, pointToStr, floatToString};