@rulecad/viewer
The model page: opens model links, checks dropped STL files, writes print files. Plain static files you can host anywhere.
npm install @rulecad/viewer # then serve node_modules/@rulecad/viewer/site
See it
The model page itself, live on this site. Open the soap tray from create-rulecad's starter project, or drop an STL file on the page to check it for printing.
Reference
A model page you can host anywhere, as plain static files: no server code, no build, no accounts. Try it at jonibach.github.io/rulecad: drop an STL on it.
- Open a model link (
#m=…, made bycad linkor@rulecad/model-link): the model turns slowly, and you can drag to turn it yourself. A parts panel shows, hides and restyles each part and ticks the ones a download holds. Downloads are STL, or 3MF in Bambu Studio's layout with each part on its filament slot. A circuit board's link also carries its fab zip (Gerbers, drill files, parts list, placement): a Download for JLCPCB button saves it as it is. - On a phone: the model and an info pane share the screen, with the model on top in portrait and on the left in landscape, so nothing covers it. The Parts button swaps the parts panel into the pane, and the rules sheet opens there too. Drag the grip between the two to change how the screen is shared (it's remembered), or double tap it to go back to half and half.
- Things that move: a link with joints (a
cadassembly with joints and poses) gets a Motion panel. It has a slider for each joint, a button for each pose, and, when you let a slider go, a quick look for parts pushed into each other. - Machines: a link for an assembly with a
task(a pen, a sheet) adds, in the Motion panel:- Draw with the pen: the sheet and the region the arm reaches are drawn on the model (worked out off the page's thread). Drag on the sheet and the joints follow, by inverse kinematics in the page with
@rulecad/robots; out of reach, the pen stops and says why. - Playback: a step file from
cad plot, carried in the link, or dropped on the page, or chosen with the button, plays on the model with Play, a scrubber and a speed. The drawn line appears on the sheet in its predicted-error colour: green well inside the task's accuracy, red past it. - The margins strip over the model, for the pose on show: torque, reach, accuracy and heat, coloured by margin (red under, amber close). Torque and accuracy are worked out at each pose from what the link carries; heat is the same in every pose.
- Build: a button that opens the rest of the project beside the model, or under it on a phone: the shopping list, the beams' cut list (each mark, the bars to buy and the cuts on each) with each beam's cut-and-drill sheet named, the plates, the wiring sheet, the assembly steps (each shown on the model) and the machine's settings. Nothing in it runs; it's the link's data, shown as text. Wires are drawn faint along their routes and move with their joints.
- Draw with the pen: the sheet and the region the arm reaches are drawn on the model (worked out off the page's thread). Drag on the sheet and the joints follow, by inverse kinematics in the page with
- Frames: a link for an assembly of beams lists each beam by its mark in the parts panel (
mark A: profile-2020-misumi-hfs5 x 300 mm, with a count where several beams are the same), and carries each mark's cut-and-drill sheet, to scale, in a Beams tab over the model, one view a mark. Pick a beam in the model or the list and a Sheet A › button opens its own sheet; drag to pan it, wheel or pinch to zoom, double-click to fit. - Sliced as the printer builds it: the Slice panel has a layer slider. Everything above the layer is cut away and its outline drawn, so you can step up through the part from the bed. Off the page's thread, every layer is checked against the one below (
islands()in@rulecad/printcheck). An island, a region with nothing under it that the printer would lay on air, shows in red over the model, with a button that jumps the slider to it. The layer height, overhang limit and line width come from the link's rules when it has them, and otherwise 0.2 mm, 45° and 0.42 mm. - Or drop an STL file on it: the page draws it, checks it for printing (bed, flat base, overhangs, bridges, closed solid;
@rulecad/printcheck), and turns the address into a link that holds the model, ready to share. - Nothing is uploaded. The model lives in the link's
#fragment, which browsers never send to the web host, so a public page can open private designs. Links are read as data, never run as code. (Short links,#s=<id>, fetch their data from a GitHub Gist.) The page sets a content policy (aContent-Security-Policyinindex.html): it loads only its own files, pictures it makes itself, and a short link's Gist fromapi.github.comandgist.githubusercontent.com, and runs no script or style written into the page, so a slip in showing a link's text can run nothing. Serveview.cssastext/css, as any web host does.
Host your own
The page is the site/ folder. Copy it anywhere that serves files:
npm pack @rulecad/viewer && tar xzf rulecad-viewer-*.tgz # package/site/ is the page
- GitHub Pages: put the files of
site/in a repository and turn on Pages (Settings → Pages → deploy from branch).site/already has the.nojekyllfile that makes Pages serve the files as they are. - Anything else: Netlify, Cloudflare Pages, a Raspberry Pi running
python3 -m http.server. It needs no headers or rewrites, just a.mjsfile served as JavaScript. - Then point
cadat it:"viewUrl": "https://you.github.io/models/"in yourcad.config.json.
Working on it
site/view.mjs is the page, site/index.html its layout (and its content policy) and site/view.css its styles; site/boot.js says so when the page's script fails to load. node scripts/build.mjs copies the link format, the print check and @rulecad/robots (into site/robots/) in beside them (--three rebuilds the trimmed three.js after a name is added to its list). node --test test/viewer.browser.mjs from the repository root drops an STL into the page in headless Chrome and saves screenshots.
three.js (MIT, site/three.LICENSE) draws the model.