Skip to content

Developers: API to Inject GCODE into CONTROL

openbuilds-engineer edited this page May 29, 2019 · 6 revisions

Purpose

This API allows other CAM/PostProcessor/GCODE Generators to inject GCODE into OpenBuilds CONTROL in the same way that OpenBuilds CAM's "Send to OpenBuilds CONTROL" button does:

Example code

Python 3 (as used in the Fusion360 Add In)

    import requests
    resultFilename = resultFilename + '.nc'   # Filename on disk
    url = "https://mymachine.openbuilds.com:3001/upload"
   
    with open(resultFilename, 'r') as f2:
        data = f2.read() # Data = the GCODE to send
        print(data)
    
    payload = "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"file\"; filename=\".nc\"\r\nContent-Type: false\r\n\r\n"+ data +"\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--"
    headers = {
        'content-type': "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW",
        'cache-control': "no-cache",
        'Postman-Token': "79eeb4b2-5e96-4c84-b04f-8aabda9d47b0"
        }
    
    response = requests.request("POST", url, data=payload, headers=headers)

    if response:
        print(response.text)

(Generated using POSTMAN https://www.getpostman.com/ )

Ruby (as used in the SketchuCAM plugin by @swarfer)

require 'uri'
require 'net/http'

url = URI("http://#{server}:3000/upload")

filename = 'edited-gcode.gcode'   #obviously this must exist
data = File.read(filename)

request = Net::HTTP::Post.new(url)
request["User-Agent"] = 'PostmanRuntime/7.13.0'
request["Accept"] = '*/*'
request["Cache-Control"] = 'no-cache'
request["Postman-Token"] = '9cd91cbf-4d85-4e0f-ad35-6cef45453e71,1c053219-f3ad-4d43-9f3f-46c22253f5bc'
request["Host"] = 'mymachine.openbuilds.com:3001'
request["accept-encoding"] = 'gzip, deflate'
request["content-type"] = 'multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW'
request["Connection"] = 'keep-alive'
request["cache-control"] = 'no-cache'
# important to add the actual file data to the body!
request.body = "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"file\"; filename=\"edited-gcode.gcode\"\r\nContent-Type: false\r\n\r\n#{data}\r\n\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--"
#now update the body length
request["content-length"] = request.body.length

#by doing it this way I can set use_ssl to true, otherwise we must use http not https
# I do get ssl cert errors when sending to a remote machine
response = Net::HTTP.start(url.hostname, url.port, use_ssl: false) do |http|
  res = http.request(request)
  case res
     when Net::HTTPSuccess then
       puts 'success'
     else
       puts 'failed'
   end
end

Javascript/Jquery (As used in OpenBuilds CAM)

function sendGcodeToMyMachine(gcode) {
  var textToWrite = gcode;
  var blob = new Blob([textToWrite], {
    type: "text/plain"
  });
  console.log("Sending ", blob, " to https://mymachine.openbuilds.com:3001/")
  var url = "https://mymachine.openbuilds.com:3001/upload"
  var fd = new FormData();
  var time = new Date();
  var string = "filename.gcode"
  console.log(string)
  fd.append('data', blob, string);
  $.ajax({
    type: 'POST',
    url: url,
    data: fd,
    processData: false,
    contentType: false
  }).done(function(data) {
    // console.log(data);
    console.log('GCODE Successfully sent to OpenBuilds CONTROL! Continue from the OpenBuilds CONTROL window);
    
});
}

GOLang

package main

import (
	"fmt"
	"strings"
	"net/http"
	"io/ioutil"
)

func main() {

	url := "https://mymachine.openbuilds.com:3001/upload"

	payload := strings.NewReader("------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"file\"; filename=\"edited-gcode.gcode\"\r\nContent-Type: false\r\n\r\n\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--")

	req, _ := http.NewRequest("POST", url, payload)

	req.Header.Add("User-Agent", "PostmanRuntime/7.13.0")
	req.Header.Add("Accept", "*/*")
	req.Header.Add("Cache-Control", "no-cache")
	req.Header.Add("Postman-Token", "9cd91cbf-4d85-4e0f-ad35-6cef45453e71,8f15f1e3-3466-4905-aa24-6e9ab465831b")
	req.Header.Add("Host", "mymachine.openbuilds.com:3001")
	req.Header.Add("accept-encoding", "gzip, deflate")
	req.Header.Add("content-type", "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW")
	req.Header.Add("content-length", "359")
	req.Header.Add("Connection", "keep-alive")
	req.Header.Add("cache-control", "no-cache")

	res, _ := http.DefaultClient.Do(req)

	defer res.Body.Close()
	body, _ := ioutil.ReadAll(res.Body)

	fmt.Println(res)
	fmt.Println(string(body))

}