Created
January 7, 2018 22:32
-
-
Save techninja/a8938bf3ccd140d655d7a9a188cb5c1c to your computer and use it in GitHub Desktop.
RoboPaint Remote mode file send example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>RoboPaint Remote Example App</title> | |
<meta charset="UTF-8"> | |
<script type="text/javascript" src="http://code.jquery.com/jquery-2.1.0.min.js"></script> | |
</head> | |
<body> | |
<script type="text/javascript"> | |
// RoboPaint Print simplified example API wrapper | |
var api = { | |
path: '/robopaint/v1/print/', | |
prefix: function() { | |
return 'http://' + $('#ipaddr').val() + ':' + $('#port').val(); | |
}, | |
status: function(cb) { | |
$.get(api.prefix() + api.path, function(data){ | |
cb(data.status); | |
}); | |
}, | |
setReady: function(toggle, cb) { | |
$.post(api.prefix() + api.path, {ready: toggle}, function(data){ | |
cb(data); | |
}); | |
}, | |
printSVG: function(svg, options, cb) { | |
var postData = { | |
options: options, | |
svg: svg | |
}; | |
console.log(postData); | |
$.post(api.prefix() + api.path, postData, function(data){ | |
cb(data); | |
}); | |
}, | |
} | |
</script> | |
<h2>RoboPaint Transmit Script</h2> | |
For interfacing with a remote computer running RoboPaint in Remote Print Mode. | |
<p><hr><p> | |
<b>Location of remote print server:</b><br> | |
IP Address: <input type="text" id="ipaddr" value="127.0.0.1" size="15">: | |
<input type="text" id="port" value="4242" size="5"> | |
<p><hr><p> | |
<button id="forceready">Force Ready State</button> | |
<p><hr><p> | |
<div> | |
Select an SVG file: | |
<input type="file" id="svgfile"> | |
</div> | |
<input type="hidden" id="svgtext" name="svg" value=""> | |
<input type="button" id="plotstart" disabled="disabled" value=" Plot Selected File " style="background-color:pink"> <span> </span> | |
<script type="text/javascript"> | |
// jQuery Bind to file field change. Load the text of the file directly. | |
$('#svgfile').change(function(e) { | |
// If the user selected a file, enable the plot button. | |
$('#plotstart').prop('disabled', this.files.length == 0); | |
loadAsText(this.files[0], function(fileContents){ | |
$('#svgtext').val(fileContents); | |
}); | |
}); | |
// jQuery bind to force ready state button click. | |
$('#forceready').click(function(e) { | |
api.setReady(true, function(data) { | |
console.log(data); | |
}); | |
}); | |
// jQuery bind to plot button click, start the plotting! Muwahahahah!!! | |
$('#plotstart').click(function(e) { | |
var file = $('#svgfile')[0].files[0]; // The DOM API file object. | |
// Pass the details to the API wrapper function... | |
api.printSVG( | |
$('#svgtext').val(), // The full text of the SVG file (loaded above) | |
{ // The "options" key object for the post request. | |
name: file.name, | |
settingsOverrides: { | |
fillType: 'zigsmooth' | |
} | |
}, | |
function (data) { // This function will be called when the request finishes. | |
console.log(data); | |
} | |
); | |
}); | |
// Helper: Load a file object as text directly, content passed to callback. | |
function loadAsText(file, cb) { | |
var reader = new FileReader(); | |
reader.onload = function(loadedEvent) { | |
// Result contains loaded file, passed to callback. | |
cb(loadedEvent.target.result); | |
} | |
// Reader is Async, onload will be called above when done reading. | |
reader.readAsText(file); | |
} | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment