javascriptfilebinaryslicers3d-printing

Binary handling for 3d printing js-slicer (SLAcer.js)


Introduction

Hey folks, i am currently working on a JavaScript-based 3d slicing tool (SLAcer.js - awesome work btw.), that can generate convenient print files for my cheap dlp-printer (SparkMaker). The printing files consist of some G-Code and simple binary layers/images (see here).

I am already able to generate the G-Code and a Uint8Array that contains the relevant information of the binary layer. All G-Code and binary layers currently get concatenated as a String (there may be the first Problem) and in the end can be downloaded as print file (.wow) as part of a zip File. (projects default JSZip Framework) (see here)

The point where i am struggling is:

I am not able to get the right TextEncoding for the binary layer to match the original binary layer.

var array = new Uint8Array(width*height/8);
var binary_layer;

Already tried several things, including:

binary_layer=(new TextDecoder("utf-8")).decode(array) /*with different text encodes*/

Also tried:

binary_layer=bin2string(array)
function bin2string(array){
var result = "";
for(var i = 0; i < array.length; ++i){
result+= (String.fromCharCode(array[i]));
}
console.log(result);
return result;
}

What amazes me is that, when exporting each Uint8Array as a separate binary txt file they nearly perfectly match the wanted/original pattern

Concatenation:

file_contents = "[some gecode]";
file_contents += binary_layer;

Summary

var staring = "hello";
var array = new Uint8Array(2);
array[0]=255;
array[1]=0;

Wanted file contents (both text and raw binary - utf8):

hello(xFF)(NUL)

This may sound easy, but in terms of right encoding it's not:


Solution

  • Issue could be fixed (still further testing is needed)

    Problem could be solved by saving all data with {binary: true} option and using simple hex to string decoder method:

    function bin2string(array){
    var result = "";
    for(var i = 0; i < array.length; ++i){
    result+= (String.fromCharCode(array[i]));
    }
    return result;
    }
    

    Annoyingly (in my case):

    var array = new Uint8Array(2);
    array[1]=255;
    (new TextDecoder("utf-8")).decode(array)
    

    Did output: (NUL)(xFD)

    But should have output: (NUL)(xFF)

    This need to be discussed further more! (see also here)