ZSLICEDOCS

Embedding

Zig API

Add zslice to your project

zig fetch --save git+<zslice repo url>

In build.zig:

const zslice = b.dependency("zslice", .{ .target = target, .optimize = optimize });
exe.root_module.addImport("zslice", zslice.module("zslice"));

Slice to a writer

const std = @import("std");
const zslice = @import("zslice");

pub fn sliceFile(gpa: std.mem.Allocator, stl: []const u8, out: *std.Io.Writer) !zslice.Stats {
    var mesh = try zslice.Mesh.fromStl(gpa, stl);
    defer mesh.deinit(gpa);

    var settings: zslice.Settings = .{};
    // Set fields directly...
    settings.project.wall_count = 3;
    settings.filament.nozzle_temp = 220;
    // ...or by name, for example from a config file or a UI.
    try settings.set("project.infill_pattern", "lines");

    const stats = try zslice.slice(gpa, &mesh, settings, out);
    try out.flush();
    return stats;
}

zslice.slice does not flush the writer. Flush it when the slice returns.

String settings (machine.start_gcode, machine.name, and others) are slices. Settings.set does not copy the value. Keep the text alive until the slice returns.

Get the paths of one layer

Use a Job to get the paths of a layer without G-code, for example for a preview.

var job = try zslice.Job.init(gpa, &mesh, settings);
defer job.deinit();

std.debug.print("{d} layers\n", .{job.layerCount()});

var plan = try job.planLayer(20);
defer plan.deinit();

for (plan.paths.items, 0..) |path, i| {
    const points = plan.points.get(i); // []zslice.geom.Point, in micrometers
    // path.feature: .outer_wall, .inner_wall, .skin, .infill
    // path.closed: true for a loop. The last point then connects to the first.
    _ = points;
}

A LayerPlan has:

FieldMeaning
indexThe layer number, from 0.
zThe nozzle height for this layer, in mm.
heightThe layer height, in mm.
widthThe line width, in mm.
outlineThe outline of the layer: counterclockwise outer boundaries and clockwise holes.
paths, pointsThe paths in print order. points.get(i) gives the points of paths.items[i].
point_zFor a path that is not planar (slope smoothing): paths.items[i].z_start is not Path.planar, and point_z.items[z_start + k] has the nozzle z (mm) and the bead area (mm²) of point k. All other paths are at z.
point_wFor a path whose line width changes along it (Arachne walls, project.wall_generator = arachne): paths.items[i].w_start is not Path.planar, and point_w.items[w_start + k] has the line width (mm) of point k. A segment has the mean width of its two ends. All other paths have the width paths.items[i].width, or width if that is 0.

Coordinates are integers in micrometers (zslice.geom.Point). Use zslice.geom.toMm to change them to millimeters.

You can also write the G-code from the same job: try job.writeGcode(out). A Job must not move in memory after init.

Job.init does the work that needs all layers: the support areas and, with project.infill_pattern = lightning, the lightning branches (job.lightning, one Paths for each layer). planLayer then plans one layer on its own.

Modifier volumes

A modifier volume is a mesh that does not print. Inside it, the part gets the settings of the volume (see layer.Modifier). Give the volumes to the job after init and before the plans. The meshes are in the coordinates of the mesh of the job:

var job = try zslice.Job.init(gpa, &mesh, settings);
defer job.deinit();
const changes = [_]zslice.settings.Change{
    .{ .name = "project.wall_count", .value = "0" },
    .{ .name = "project.top_thickness", .value = "0" },
    .{ .name = "project.bottom_thickness", .value = "0" },
    .{ .name = "project.infill_density", .value = "0.3" },
};
try job.setModifiers(&.{.{ .mesh = &window, .changes = &changes, .order = 1, .name = "window" }});

A path of a plan in a volume has path.region > 0; LayerPlan.pathResolved(path) gives its settings. For a Cura project 3MF, zslice.threemf.read keeps the metadata of each object, and zslice.part_settings.plan sorts the parts: the settings for the whole plate (applyBase), and for each part that prints its own changes and its modifier volumes.

A plate of many parts slices with a zslice.PlateJob: one job for each part (or for each group of copies), with the layers of all parts merged by z. It has layerCount, planLayer, and writeGcode, as a Job:

var plate = try zslice.PlateJob.init(gpa, settings, &.{
    .{ .mesh = &a, .part = 0 },
    .{ .mesh = &b, .part = 1, .changes = &.{.{ .name = "project.wall_count", .value = "4" }} },
}, .{});
defer plate.deinit();
_ = try plate.writeGcode(out);

Make a mesh from triangles

// 9 floats for each triangle: x, y, z of 3 vertices, in mm.
// The vertices are counterclockwise as seen from outside the part.
var mesh = try zslice.Mesh.fromTriangles(gpa, xyz);

Or build one triangle at a time:

var b: zslice.mesh.Builder = .init(gpa);
errdefer b.deinit();
try b.addTriangle(v0, v1, v2);
var mesh = try b.finish();

Allocators

The lower-level modules

The root module also exports the stages, for tools that need only a part of the pipeline:

ModuleUse
zslice.clipPolygon union, intersection, difference, and xor.
zslice.offsetPolygon offset.
zslice.slicerCut a mesh at a height.
zslice.infillFill a polygon set with parallel lines.
zslice.svgWrite an SVG picture of a layer plan.

These APIs can change more often than slice, Job, Mesh, and Settings.