-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathview.js
128 lines (121 loc) · 3.55 KB
/
view.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
"use strict";
var js_pcb = js_pcb || {};
(function()
{
function view_pcb(pcb_data, scale, margin)
{
//Width and height etc
let width, height, depth;
[width, height, depth] = pcb_data[0];
let path_func = d3.line()
.x(function(d) { return d[0]; })
.y(function(d) { return d[1]; });
//create/replace SVG element
let body = d3.select("body");
let svg = body.select("svg");
if (svg) svg.remove();
svg = body.append("svg")
.attr("width", margin * scale * 2 + width * scale)
.attr("height", margin * scale * 2 + height * scale);
svg.append("rect")
.attr("width", margin * scale * 2 + width * scale)
.attr("height", margin * scale * 2 + height * scale)
.attr("fill", "black");
let pcb = svg.append("g")
.attr("transform", "scale(" + scale + "," + scale + ") translate(" + margin + "," + margin + ")")
.attr("stroke-linecap", "round")
.attr("stroke-linejoin", "round")
.attr("stroke-width", "0")
.attr("fill", "none");
//create layers, last layer is the terminals layer
let layers = [];
let layer_colors = ["red", "green", "blue", "yellow", "cyan", "magenta"];
for (let layer = 0; layer < depth; ++layer)
{
layers.push(pcb.append("g")
.attr("stroke", layer_colors[layer % layer_colors.length])
.attr("stroke-opacity", 0.75));
}
layers.push(pcb.append("g")
.attr("stroke", "white"));
//add tracks
for (let track of pcb_data[1])
{
let track_radius, paths;
[track_radius, , , , paths] = track;
for (let path of paths)
{
let node, start = 0;
let d = path[start][2];
for (node = 1; node < path.length; ++node)
{
if (path[node][2] === d) continue;
if (node - start > 1)
{
layers[d].append("path")
.attr("stroke-width", track_radius * 2)
.attr("d", path_func(path.slice(start, node)));
}
start = node;
d = path[start][2];
}
if (node - start > 1)
{
layers[d].append("path")
.attr("stroke-width", track_radius * 2)
.attr("d", path_func(path.slice(start, node)));
}
}
}
//add terminals and vias
for (let track of pcb_data[1])
{
let track_radius, via_radius, track_gap, terminals, paths;
[track_radius, via_radius, track_gap, terminals, paths] = track;
for (let terminal of terminals)
{
let terminal_radius, terminal_gap, terminal_x, terminal_y, terminal_z, terminal_shape;
[terminal_radius, terminal_gap, [terminal_x, terminal_y, terminal_z], terminal_shape] = terminal;
if (!terminal_shape.length)
{
layers[layers.length-1].append("circle")
.attr("cx", terminal_x)
.attr("cy", terminal_y)
.attr("r", terminal_radius)
.attr("fill", "white");
}
else if (terminal_shape.length === 2)
{
layers[layers.length-1].append("path")
.attr("stroke-width", terminal_radius * 2)
.attr("d", path_func(terminal_shape.map(
function(e){ return [e[0] + terminal_x, e[1] + terminal_y]; })));
}
else
{
layers[layers.length-1].append("path")
.attr("fill", "white")
.attr("d", path_func(terminal_shape.map(
function(e){ return [e[0] + terminal_x, e[1] + terminal_y]; })));
}
}
for (let path of paths)
{
let terminal_z = path[0][2];
for (let node = 1; node < path.length; ++node)
{
if (terminal_z !== path[node][2])
{
layers[layers.length-1].append("circle")
.attr("cx", path[node][0])
.attr("cy", path[node][1])
.attr("r", via_radius)
.attr("fill", "white");
}
terminal_z = path[node][2];
}
}
}
}
js_pcb.view_pcb = view_pcb;
})();