Skip to content
Snippets Groups Projects
Commit 79d4c993 authored by Jake Read's avatar Jake Read
Browse files

nest to nest

parent 7fcc9a12
Branches
No related tags found
No related merge requests found
/*
hunks/comm/swExtractFaces.js
pipe from solidworks, delivers array of svgs, from
slice-it add-in
Jake Read at the Center for Bits and Atoms
Shawn Liu at Solidworks
(c) Massachusetts Institute of Technology 2019
This work may be reproduced, modified, distributed, performed, and
displayed for any purpose, but must acknowledge the squidworks and cuttlefish projects.
Copyright is retained and must be preserved. The work is provided as is;
no warranty is provided, and users accept all liability.
*/
import {
Hunkify,
Input,
Output,
State
} from '../hunks.js'
export default function SWExtractFaces() {
Hunkify(this)
let dtout = this.output('reference', 'svg array')
// lots!
let connectionStatus = this.state('boolean', 'connected', false)
let statusMessage = this.state('string', 'status', '...')
// conn, and sw conn,
let addressState = this.state('string', 'address', '127.0.0.1')
let portState = this.state('number', 'port', 8787)
let swId = this.state('string', 'swiD', '7772')
// config ...
let thickness = this.state('number', 'thickness (mm)', 3.125)
// this ws is a client,
let ws = {}
let url = `ws://127.0.0.1:${portState.value}`
// we keep an outgoing set,
let outbuffer = new Array()
// in case saved when 'open'
this.init = () => {
connectionStatus.set(false)
}
connectionStatus.onChange = (value) => {
if (value) {
startWs()
} else {
ws.close()
}
}
let startWs = () => {
// manager calls this once
// it is loaded and state is updated (from program)
url = 'ws://' + addressState.value + ':' + portState.value
this.log(`attempt start ws at ${url}`)
ws = new WebSocket(url)
ws.binaryType = "arraybuffer"
ws.onopen = (evt) => {
this.log('ws opened')
statusMessage.set('open')
connectionStatus.set(true)
// ack to shawn,
ws.send(JSON.stringify({
modCmd: 'connect',
owner: 'mods',
id: swId.value
}))
}
ws.onerror = (evt) => {
console.log('ws error:', evt)
statusMessage.set('error')
connectionStatus.set(false)
}
ws.onclose = (evt) => {
statusMessage.set('closed')
connectionStatus.set(false)
}
ws.onmessage = (message) => {
let mobj = JSON.parse(message.data)
if (true) console.log('sw-ws-faces receives', mobj)
try {
if (mobj.swType == 'FaceSVGArray') {
outbuffer.push(mobj.data[0])
}
} catch (err) {
console.error('bad form on SW connect', err)
}
}
statusMessage.set('ws startup...')
}
let extractSVGs = () => {
let mc = {
modCmd: "AutoExtractFaces",
thickness: thickness.value
}
if(ws.readyState === 1){
ws.send(JSON.stringify(mc))
}
}
let extractor = this.state('boolean', 'extract', false)
extractor.onChange = (value) => {
extractSVGs()
extractor.set(false)
}
this.loop = () => {
if(outbuffer.length > 0 && !dtout.io()){
dtout.put(outbuffer.shift())
}
}
}
......@@ -23,7 +23,7 @@ import {
export default function SWSVGClient() {
Hunkify(this)
let dtout = this.output('string', 'svg', this)
let dtout = this.output('string', 'svg')
// lots!
let connectionStatus = this.state('boolean', 'connected', false)
......@@ -107,7 +107,8 @@ export default function SWSVGClient() {
if (true) console.log('sw-ws receives', mobj)
try {
if (mobj.swType == 'FaceSVG') {
outbuffer.push(mobj.data)
// some odd formatting,
outbuffer.push(mobj.data[0])
}
} catch (err) {
console.error('bad form on SW connect', err)
......
......@@ -89,6 +89,7 @@ export default function svgToImageData() {
let width = size.width * dpi.value / size.units
let height = size.height * dpi.value / size.units
img.onload = () => {
console.log('img2 loads', img)
// new vcanvas always,
vcanvas = document.createElement('canvas')
vcanvas.width = width
......@@ -103,6 +104,7 @@ export default function svgToImageData() {
let test = this.state('boolean', 'test', false)
test.onChange = (value) => {
let size = getSize(svgString)
console.log('img2 test size', size)
loadImage(svgString, size)
test.set(false)
}
......@@ -111,6 +113,9 @@ export default function svgToImageData() {
if(strin.io() && !haveImageUpdate){
let str = strin.get()
let size = getSize(str)
size.width = parseFloat(size.width).toString()
size.height = parseFloat(size.height).toString()
console.log('img2 size', size)
loadImage(str, size)
}
if (haveImageUpdate && !imgOut.io() && !width.io()) {
......
This diff is collapsed.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment