-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimeline.html
152 lines (134 loc) · 5.29 KB
/
timeline.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Toolpath Visualization</title>
<script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
<style>
html,
body {
margin: 0;
padding: 0;
height: 100%;
}
</style>
</head>
<body>
<div id="chart"></div>
<script>
let toolpathData = [];
// Load data
function loadData(event) {
const message = event.data;
if (message.type === 'updateToolpath') {
const uri = message.uri;
const gcodeContent = message.gcodeContent;
// Parse the G-code content and update the toolpath
toolpathData = parseGcode(gcodeContent);
updateChart();
// Add click event listener to the Plotly chart
const chart = document.getElementById('chart');
chart.on('plotly_click', function (data) {
const pointIndex = data.points[0].pointNumber;
const lineNumber = pointIndex + 1;
// Send message to extension with line number
window.parent.postMessage({
command: 'jumpToLine',
lineNumber: lineNumber
}, '*');
});
}
}
function updateChart() {
const traceX = {
x: Array.from({ length: toolpathData.length }, (_, i) => i),
y: toolpathData.map(d => d.x),
mode: 'lines',
name: 'X',
line: { color: 'red' }
};
const traceY = {
x: Array.from({ length: toolpathData.length }, (_, i) => i),
y: toolpathData.map(d => d.y),
mode: 'lines',
name: 'Y',
line: { color: 'green' }
};
const traceZ = {
x: Array.from({ length: toolpathData.length }, (_, i) => i),
y: toolpathData.map(d => d.z),
mode: 'lines',
name: 'Z',
line: { color: 'blue' }
};
const traceE = {
x: Array.from({ length: toolpathData.length }, (_, i) => i),
y: toolpathData.map(d => d.e),
mode: 'lines',
name: 'E',
line: { color: 'purple' }
};
const traceF = {
x: Array.from({ length: toolpathData.length }, (_, i) => i),
y: toolpathData.map(d => d.f),
mode: 'lines',
name: 'F',
line: { color: 'orange' }
};
const traceS = {
x: Array.from({ length: toolpathData.length }, (_, i) => i),
y: toolpathData.map(d => d.s),
mode: 'lines',
name: 'S',
line: { color: 'pink' }
};
const layout = {
title: 'Toolpath Data',
xaxis: { title: 'Step' },
yaxis: { title: 'Value' },
autosize: true,
margin: { l: 50, r: 50, b: 50, t: 50 }
};
Plotly.newPlot('chart', [traceX, traceY, traceZ, traceE, traceF, traceS], layout);
}
function parseGcode(gcodeContent) {
const lines = gcodeContent.split("\n");
const coordinates = [];
var current = { x: 0, y: 0, z: 0, e: 0, f: 0, s: 0 };
let lastCommand = "";
for (var line of lines) {
const command = line.split(" ")[0].toUpperCase();
if (command === "G1" || command === "G0") {
const matchX = line.match(/X(-?\d+(\.\d+)?)/);
const matchY = line.match(/Y(-?\d+(\.\d+)?)/);
const matchZ = line.match(/Z(-?\d+(\.\d+)?)/);
const matchE = line.match(/E(-?\d+(\.\d+)?)/);
const matchF = line.match(/F(-?\d+(\.\d+)?)/);
const matchS = line.match(/S(-?\d+(\.\d+)?)/);
const newX = matchX ? parseFloat(matchX[1]) : current.x;
const newY = matchY ? parseFloat(matchY[1]) : current.y;
const newZ = matchZ ? parseFloat(matchZ[1]) : current.z;
const newE = matchE ? parseFloat(matchE[1]) : current.e;
const newF = matchF ? parseFloat(matchF[1]) : current.f;
const newS = matchS ? parseFloat(matchS[1]) : current.s;
const next = { x: newX, y: newY, z: newZ, e: newE, f: newF, s: newS };
coordinates.push(next);
current = next;
lastCommand = command;
}
}
return coordinates;
}
window.addEventListener('message', loadData);
window.addEventListener('panelOpened', loadData);
window.addEventListener('resize', () => {
const update = {
width: window.innerWidth,
height: window.innerHeight
};
Plotly.relayout('chart', update);
});
</script>
</body>
</html>