Choose and mix part backends

A leaf node is one part, built by one modelling technology. Every kind below produces the same box with a hole, and every kind takes part in an assembly, a fusion, a test and an export the same way. Choose by what you already know and by what the part needs; a project may mix them freely.

Class

Imported from

You write

B-rep

Needs

CadQueryNode

machinome.node.cadquery

render() returning a Workplane

yes

machinome[cadquery]

Build123dNode

machinome.node.build123d

render() returning a Part, Solid, Compound or BuildPart

yes

machinome[build123d]

Build123dSheetNode

machinome.node.build123d

profile() plus thickness

yes

machinome[build123d] (Cut parts from sheet)

StepNode

machinome.node.step

step_source and a product name

yes

machinome[step] (Reuse a vendor STEP or an STL)

Solid2Node

machinome.node.solid2

render() returning a SolidPython object

no

OpenSCAD and machinome[solid2]

OpenScadNode

machinome.node.openscad

scad_source naming a module

no

OpenSCAD and machinome[openscad]

JScadNode

machinome.node.jscad

jscad_source exporting main

no

the jscad command and machinome[jscad]

StlNode

machinome.node.stl

stl_source

no

machinome[stl] (Reuse a vendor STEP or an STL)

MolejoNode

machinome.node.molejo

render() returning a molejo Shape

yes

machinome[molejo] (Model springs, belts and cables)

FreeCADAssemblyNode

machinome.node.freecad

freecad_source naming a native assembly, and a FreeCADJoint per driven revolute

yes

FreeCAD and machinome[freecad] (Simulate a native FreeCAD assembly)

Each class is imported from the module beside it, the module of its node type: nine node types; machinome.node.build123d defines two classes, and machinome.node.freecad reads a whole assembly rather than one part. The root of machinome.node exports nothing, and the extra is the module’s last name (Install).

B-rep parts keep their boundary representation: a .brep beside the STL, a B-rep fusion with other B-rep parts, and geometric tests decided by the B-rep engine with no tolerance. Mesh parts are meshes from the moment they are built, and a fusion holding one becomes a mesh fusion, unioned by the mesh engine.

CadQuery

import cadquery as cq
from machinome.node.cadquery import CadQueryNode

class Box(CadQueryNode):

    def render(self):
        wp = cq.Workplane("XY")
        cube = wp.box(50, 50, 50)
        hole = wp.workplane(offset=-50).circle(10).extrude(100)
        return cube.cut(hole)

To keep using CQ-editor on the same file, add a guard that does not conflict with Machinome:

if __name__ == '__cq_main__':
    show_object(Box().render())

build123d

Either of build123d’s two styles works. Builder mode:

from build123d import BuildPart, Box, Cylinder, Mode
from machinome.node.build123d import Build123dNode

class Box(Build123dNode):

    def render(self):
        with BuildPart() as part:
            Box(50, 50, 50)
            Cylinder(radius=10, height=100, mode=Mode.SUBTRACT)
        return part.part

Algebra mode:

from build123d import Box, Cylinder
from machinome.node.build123d import Build123dNode

class Box(Build123dNode):

    def render(self):
        return Box(50, 50, 50) - Cylinder(radius=10, height=100)

Returning the builder itself instead of its .part also works. A leaf is one part, so render() must produce a solid; a sketch or a curve is refused naming the node, rather than failing later in the STL export.

CadQuery and build123d are both OCCT front ends, so a fusion may take children from either and fuse them exactly.

SolidPython and OpenSCAD

Solid2Node wraps SolidPython 2, a Python front end for OpenSCAD. Both classes are the OpenSCAD node family, the package machinome.node.openscad with Solid2Node at machinome.node.solid2 over it: install machinome[solid2] for Solid2Node and machinome[openscad] for OpenScadNode, or machinome[all]. Without SolidPython, importing either is refused with the line that installs it. A family part writes its own .scad, which the build keeps, and OpenSCAD renders its STL from it:

from machinome.node.solid2 import Solid2Node
from solid2 import cube, cylinder, translate

class Box(Solid2Node):

    def render(self):
        return translate(-25, -25, 0)(
            cube(50, 50, 50)
        ) - cylinder(r=10, h=100)

translate here is a SolidPython primitive applied inside the part. Nodes also have a translate() method of their own, which positions a part within an assembly; Rest and motion keeps the two apart.

OpenScadNode wraps one module of an existing .scad file:

from machinome.node.openscad import OpenScadNode

class Box(OpenScadNode):

    scad_source = 'demo.scad'
module demo() {
  difference() {
    translate([-25, -25, 0]) cube([50, 50, 50]);
    cylinder(r=10, h=100);
  }
}

The module is expected to have the file’s name unless module_name says otherwise, and the node’s constructor arguments are forwarded to it, so one .scad module can back several parametrized nodes:

class Box(OpenScadNode):

    scad_source = 'shapes.scad'
    module_name = 'box_with_hole'

box = Box(50, hole_radius=10)

A scad_source that does not resolve to a file, or that resolves outside the project, is refused when the node is constructed, naming the class, the attribute and the resolved path, before OpenSCAD runs. Simulate an existing open-source design shows a whole printer read this way.

Resolution

OpenSCAD approximates circles by polygons, and its default is coarse: a small hole comes out as a hexagon, and a hexagonal hole is tighter than the circle it stands for. Set fn on the node to raise the segment count:

class Pointer(Solid2Node):

    fn = 256

fn affects only Solid2Node and OpenScadNode. The OCCT-backed kinds tessellate their B-rep geometry themselves, and Reuse a vendor STEP or an STL shows how to set that tessellation’s precision.

JSCAD

JScadNode wraps a JavaScript module whose main returns the geometry. It needs the jscad command on the PATH and its node dependencies installed in the directory you run machinome from.

from machinome.node.jscad import JScadNode

class Box(JScadNode):

    jscad_source = 'demo.js'
const { square, circle } = require('@jscad/modeling').primitives
const { subtract } = require('@jscad/modeling').booleans
const { extrudeLinear } = require('@jscad/modeling').extrusions

function main() {
  const shape = subtract(square({size: 50}), circle({radius: 10}));
  return extrudeLinear({height: 50}, shape);
}

module.exports = { main }

As with every source file, a jscad_source outside the project is refused when the node is constructed.

Colour

Any node may set color, a hex RGB string the viewer and the exports honour. Anything else is refused.

class Pointer(CadQueryNode):

    color = '#cc4444'

A StepNode that declares no colour takes the product’s colour from the document.