Newer
Older
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
/*
gCodePanel.js
input gcodes
Jake Read at the Center for Bits and Atoms
(c) Massachusetts Institute of Technology 2020
This work may be reproduced, modified, distributed, performed, and
displayed for any purpose, but must acknowledge the open systems assembly protocol (OSAP) project.
Copyright is retained and must be preserved. The work is provided as is;
no warranty is provided, and users accept all liability.
*/
/*
notes on this thing
this is pretty straightforward at the moment, it'll read small gcodes
i.e. those used to mill circuits. for larger files, like 3DP files,
reading times / manipulating large streams of texts needs to be handled
more intelligently, i.e. not rendering the whole file into the 'incoming' body.
*/
'use strict'
import dt from './drawing/domtools.js'
// reference:
// spy from https://github.com/cncjs/gcode-parser/blob/master/src/index.js thx
/*
G00: move at rapids speed
G01: move at last G01 F<num>
G04 P<num>: dwell for P milliseconds or X seconds
G20: set coordinates to inches
G21: set coordinates to mm
G28: do homing routine
G90: positions are absolute w/r/t zero
G91: positions are incremenetal w/r/t last moves
G94: feedrates are per minute
*/
/*
F<num>: set feedrate for modal G
M03 S<num>: set clockwise rotation
M04 S<num>: set counter-clockwise rotation
M05: stop spindle
M83: use extruder relative motion
*/
function Plane() {
// setup drawing env
let plane = $('<div>').addClass('plane').get(0)
let wrapper = $('#wrapper').get(0)
// odd, but w/o this, scaling the plane & the background together introduces some numerical errs,
// probably because the DOM is scaling a zero-size plane, or somesuch.
$(plane).css('background', 'url("/client/drawing/bg.png")').css('width', '100px').css('height', '100px')
let cs = 1 // current scale,
let dft = { s: cs, x: 0, y: 0, ox: 0, oy: 0 } // default transform
// init w/ defaults,
dt.writeTransform(plane, dft)
dt.writeBackgroundTransform(wrapper, dft)
$(wrapper).append(plane)
}
function Button(xPlace, yPlace, width, height, text) {
let btn = $('<div>').addClass('button')
.text(text)
.get(0)
placeField(btn, width, height, xPlace, yPlace)
return btn
}
function TextInput(xPlace, yPlace, width, height, text) {
let input = $('<input>').addClass('inputwrap').get(0)
input.value = text
placeField(input, width, height, xPlace, yPlace)
return input
}
let BTN_RED = 'rgb(242, 201, 201)'
let BTN_GRN = 'rgb(201, 242, 201)'
let BTN_YLW = 'rgb(240, 240, 180)'
let BTN_GREY = 'rgb(242, 242, 242)'
let BTN_HANGTIME = 1000
let BTN_ERRTIME = 2000
function AutoPlot(xPlace, yPlace, xSize, ySize) {
let chart = $('<div>').get(0)
$(chart).css('background-color', BTN_GREY)
let uid = `lineChart_${Math.round(Math.random() * 1000)}_uid`
$(chart).attr('id', uid)
placeField(chart, xSize, ySize, xPlace, yPlace)
// the data
var datas = [[0, 0]]
var numToHold = 100
// our vars,
var margin = {
top: 20,
right: 20,
bottom: 30,
left: 50
}
var width = xSize - margin.left - margin.right
var height = ySize - margin.top - margin.bottom
var x = d3.scaleLinear().range([0, width])
var y = d3.scaleLinear().range([height, 0])
var thesvg = null
// redraw
this.redraw = () => {
var valueline = d3.line()
.x(function (d) {
return x(d[0])
})
.y(function (d) {
return y(d[1])
})
// scale
x.domain([d3.min(datas, function (d) {
return d[0]
}), d3.max(datas, function (d) {
return d[0];
})])
y.domain([d3.min(datas, function (d) {
return d[1]
}), d3.max(datas, function (d) {
return d[1];
})])
if (thesvg) {
d3.select(`#${uid}`).selectAll("*").remove()
}
thesvg = d3.select(`#${uid}`).append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
// write it?
thesvg.append("path")
.data([datas])
.attr("fill", "none")
.attr("stroke", "black")
.attr("stroke-width", "4px")
.attr("d", valueline)
// write the x axis
thesvg.append("g")
.attr("transform", "translate(0," + height + ")")
.call(d3.axisBottom(x))
// the y axis
thesvg.append("g")
.call(d3.axisLeft(y))
}
// startup
this.redraw()
// add new pts
this.pushPt = (pt) => {
datas.push(pt)
if (datas.length > numToHold) {
datas.shift()
}
}
// set draw length
this.setDrawLength = (len) => {
numToHold = len
if (datas.length > numToHold) {
datas = datas.slice(datas.length - numToHold)
}
}
}
let placeField = (field, width, height, xpos, ypos) => {
$(field).css('position', 'absolute')
.css('border', 'none')
.css('width', `${width}px`)
.css('height', `${height}px`)
$($('.plane').get(0)).append(field)
let dft = { s: 1, x: xpos, y: ypos, ox: 0, oy: 0 }
dt.writeTransform(field, dft)
}
export { Plane, Button, TextInput, AutoPlot }