Bagworm - topology optimization for 3D printing in Grasshopper
==============================================================

Bagworm finds the stiffest shape for a given amount of material (SIMP) and keeps every
overhang within the angle your printer can build (Langelaar's overhang filter), so the
result prints without supports. It runs on the GPU (NVIDIA, Intel Arc and other GPUs with
OpenCL 2.0) or on the CPU when there is no GPU. No Python is needed.

Requirements
- Windows 10 or 11, Rhino 7 or Rhino 8
- Optional: a GPU with an up-to-date driver

Installation
1. Unzip everything.
2. Close Rhino and double-click install.bat.
   (Or copy the "Bagworm" folder to %APPDATA%\Grasshopper\Libraries\ yourself, then
   right-click every .gha and .dll file > Properties > tick Unblock.)
3. Start Rhino and Grasshopper. The components are on the "Bagworm" tab.
4. Open Bagworm_cantilever_sample.gh to try the example: press Run, wait until the Status says
   done, then press Save STL.

Uninstall: delete the folder %APPDATA%\Grasshopper\Libraries\Bagworm

Quick start
1. Domain: draw a box, or a closed Brep or mesh of any shape, that the part must fit in.
2. Support: connect the geometry where the part is held (points, curves, surfaces or
   solids) and set Fix, e.g. xyz (clamped) or z (only vertical). To print the supported
   face on the bed, put it on the bottom face of the domain.
3. Load: connect the geometry where the force acts and a Force vector (its length is the
   force; each piece of geometry carries the full force).
4. Solver: connect Domain, Supports and Loads, and set
   - Voxel: the voxel size in model units. Start coarse (about 20,000 to 100,000 voxels;
     the Solver shows the grid and the count before a run), then refine.
   - VolFrac: the fraction of the domain filled with material, e.g. 0.15.
   - MinSize: the thinnest member in model units, e.g. 3 to 5 voxels.
   - Build: the print direction, e.g. +z (printed upward), or none for no overhang rule.
   - Angle: the smallest angle from the bed your printer can print without supports,
     e.g. 45.
   - Preset: preview (about 100 iterations) while you explore, final (300) at the end.
   Press Run. Rhino stays responsive; the voxel design and the Status update while it runs.
   Stop ends the run early, Reset clears the result.
5. Smooth to Tri Mesh or Smooth to Quad Mesh: connect Density from the Solver to get a
   smooth closed mesh (Quad Mesh can also make a SubD).
6. Export STL: connect the mesh and press Save.

Components
- Support (1 Setup): Geometry, Fix, Skin
- Load (1 Setup): Geometry, Force, Skin
- Solver (2 Solve): Domain, Voxel, Supports, Loads, Solid, Void, VolFrac, MinSize,
  Build (+z -z +x -x +y -y none), Angle, Iterations / Preset, Solver (auto/gpu/cpu),
  Run / Stop / Reset buttons. Outputs: voxel Mesh, Density, Status, Compliance, History.
- Smooth to Tri Mesh (3 Output): a smooth closed triangle mesh from the Density.
  Iso, Smooth (passes), Live (update while running), Keep Support, Keep Load.
- Smooth to Quad Mesh (3 Output): a smooth closed all-quad mesh from the Density, as
  above plus Detail (quads per voxel edge) and SubD.
- Export STL (3 Output): press Save to write the STL file (empty Folder = the folder of
  the .gh file).

Tips
- Solid and Void: regions that must stay filled (e.g. a mounting plate) or empty (e.g. a
  hole or a clearance). A Solid must itself be printable from the bed: chamfer it at no
  more than the Angle, or let it rest on the bed.
- Skin: a solid layer of the given thickness (rounded to whole voxels, at least one)
  under a support or load, so that face is printed whole. The Status tells how much of
  each skin was printed; a line starting with "warning:" means the overhang rule removed
  part of it. Then try another Build, a smaller Angle or a larger VolFrac.
- The Status also reports "overhang cells": voxels left without support below them.
  It should be 0, or close to it.
- Compliance: lower is stiffer. Compare designs by it.
- Iso: the density level of the surface (0.5 by default); a lower level gives slightly
  thicker members.
- Keep Support / Keep Load: keep the voxels at supports and loads unsmoothed, so those
  faces stay flat and sharp (good for feet on the bed). Turn it off for a tilted face,
  which would otherwise keep its voxel steps.
- MinSize = 0 means 3 voxels, so the shape changes with the Voxel size. Set a value to
  keep the same member size when you change Voxel.

Notes
- The first run after starting Rhino is a few seconds slower while the GPU code is
  prepared.
- Solver auto uses the GPU if one is found. With no GPU, or a very thin domain (one voxel
  thick), it uses the CPU. The Status says which one ran.
- STL files are in the units of the Rhino model.

Terms of use
- Free for teaching, academic research and personal non-commercial projects. Commercial
  use needs permission from the author. Full terms: TERMS.txt (installing or using
  Bagworm means you accept them).
- The third-party libraries that come with the plug-in (ILGPU, System.*.dll) are under
  their own licenses: see THIRD-PARTY-NOTICES.txt.
- Contact: chavanont.k@chula.ac.th

References
- SIMP: Bendsoe & Sigmund, Topology Optimization: Theory, Methods and Applications (2003)
- AM filter: Langelaar (2016) Additive Manufacturing 12:60-70; Langelaar (2017) Struct Multidisc Optim 55:871-883
- MMA: Svanberg (1987, 2007)
