Skip to content

Beam Studio Easy API

Dean Sung edited this page Jul 5, 2021 · 4 revisions

Dev Notes


  1. Users can use a global class module called EasyManipulator, extending EventTarget
  2. The script's extension file name is .js, which runs as javascript. The script file can be executed by importing to the beam-studio.

Example Code


let main = async () => {
    // BVG file, which can be exported by Beam Studio
    // you can use js FileReader to read string from .bvg file too.
    const bvg = `
        <svg id="svgcontent" width="3000" height="2100" xmlns="http://www.w3.org/2000/svg" xmlns:svg="http://www.w3.org/2000/svg" data-top="-1300" data-left="-512" data-zoom="0.115" data-rotary_mode="false" data-engrave_dpi="medium">
            <g data-repeat="1" data-strength="1" data-speed="20" clip-path="url(#scene_mask)" data-color="#333333" class="layer">
                <title>Default</title>
                <rect fill="black" vector-effect="non-scaling-stroke" fill-opacity="0" id="svg_1" stroke="#333333" height="913.57312" width="713.4571" y="196.98372" x="201.85626"/>
            </g>
        </svg>`;

    let beamAPI = new EasyManipulator();

    const onLoad = (event) => {
        console.log('BVG loaded');
    };
    const onError = (event) => {
        console.log('Error', event.detail);
    };
    const oldOnDone = (event) => {
        console.log('Task Done', event.detail);
    };
    //Use addEventListener to register event 
    beamAPI.addEventListener('LOAD', onLoad);
    beamAPI.addEventListener('DONE', oldOnDone);
    beamAPI.addEventListener('ERROR', onError);
    
    let res;
    // Machine Name Here
    res = await beamAPI.selectMachine('Mark');
    // Click Beam Studio -> Menubar -> Help -> Debug Tool to open the console
    console.log('Select result:', res);
    res = await beamAPI.loadBVG(bvg);
    console.log('Load BVG result:', res);
    res = await beamAPI.calculate();
    console.log('Calculate result:', res);

    // Use removeEventListener to remove event function
    beamAPI.removeEventListener('DONE', oldOnDone);

    res = await beamAPI.start();
    console.log('Start result:', res);

    let interval = window.setInterval(() => {
        console.log(beamAPI.getStatus());
    }, 2000);

    const newOnDone = (event) => {
        console.log('(New) Task done', event.detail);
        window.clearInterval(interval);
    };
    beamAPI.addEventListener('DONE', newOnDone);
}
main();

Member Functions


EasyManipulator.selectMachine(machineName)

Hooks the instance to specific machine

Returns: Promise (Success or Failed)

EasyManipulator.loadBVG(string)

Load BVG String (Similar to SVG, but with laser parameters)

Returns: Promise (Success or Failed)

EasyManipulator.calculate()

Run slicing, and returns time cost.

Returns: Promise<Object {success: Boolean, timeCost: Integer}>

EasyManipulator.start()

Upload the code to machine and start.

Returns: Promise<Boolean (Succeeded or Failed)>

EasyManipulator.resume()

Resume the machine

Returns: Boolean (Succeeded or Failed)

EasyManipulator.pause()

Pause the machine

Returns: Boolean (Succeeded or Failed)

EasyManipulator.abort()

Abort the machine

Returns: Boolean (Succeeded or Failed)

EasyManipulator.getStatus()

Returns the machine state and progress

Status includes EasyManipulator.IDLE, EasyManipulator.READY, EasyManipulator.BUSY, EasyManipulator.WORKING, EasyManipulator.COMPLETED

Returns: {state: String, progress: Float}

Member Events


EasyManipulator.LOAD

EasyManipulator.CALCULATED

EasyManipulator.UPLOADING

EasyManipulator.UPLOADED

EasyManipulator.DONE

EasyManipulator.ERROR

Clone this wiki locally