Plugins
Modlumi comes with twelve plugins. Most are ports of well-known open-source SketchUp extensions, rewritten for Modlumi’s plugin API. Plugins run in the desktop app; the browser preview doesn’t run them yet.
Each plugin’s source code is under its description and in the modlumi-plugins repository on GitHub. They are complete, working plugins, so they’re a good place to see how to build your own; see Learning from the built-in plugins.
Use and manage plugins
Section titled “Use and manage plugins”Each plugin has a submenu in the Extensions menu with its commands. Some commands also appear in the viewport’s right-click menu while something is selected, importers and exporters add File → Import and File → Export items, and plugin panels are listed in the Window menu. A plugin’s edit to the model is one undo step.
Extensions → Manage Extensions… lists every plugin. The built-in ones are
marked Built in; turn any plugin off or on with its Enabled switch.
Built-in plugins can be turned off but not removed. To add someone else’s plugin,
choose Install plugin… and pick its zip archive or the plugin.json in its
folder. To make your own, see Writing a plugin.
Install a plugin from GitHub
Section titled “Install a plugin from GitHub”Most plugins are shared as a GitHub repository. To install one:
-
On the repository’s page, click Code, then Download ZIP.
-
In Modlumi, choose Extensions → Manage Extensions…, click Install plugin… and pick the zip you downloaded. You don’t need to unzip it.
-
Check the plugin’s name, author and the permissions it asks for, then click Install. A plugin that isn’t in your plugin catalog comes with a warning: install only plugins from authors you trust. The plugin starts right away, without a restart, and gets a submenu in Extensions.
If the repository holds several plugins, its zip has no plugin.json at the top
and Modlumi says so. Unzip it, click Install plugin… again, and pick the
plugin.json inside the folder of the plugin you want, such as
plugins/face-area/plugin.json.
Don’t download plugin.json on its own. Choosing a plugin.json installs every
file in the folder it is in, so one saved alone to your Downloads folder doesn’t
install: the plugin’s other files aren’t there, and your downloads are.
To update the plugin later, download the zip again and install it the same way. The dialog then says which version it replaces, and the button reads Replace.
Clean Up
Section titled “Clean Up”Tidies a model in one step: purges unused components and materials, merges coplanar faces, repairs edges split by an unneeded vertex, erases stray edges and, if you ask, hidden geometry. It can also merge identical materials and smooth edges between faces that meet at a shallow angle.
Choose Extensions → Clean Up → Clean Up…, pick what to clean and whether to clean the whole model or only the group you’re editing, and click Clean Up. Clean Up with Last Settings repeats it without the dialog, and the submenu has each step as a command of its own.
Port of CleanUp by Thomas Thomassen (MIT).
Source code
{ "id": "cleanup", "name": "Clean Up", "version": "1.0.0", "description": "Purges unused components and materials, merges coplanar faces and identical materials, repairs split edges, erases stray edges and hidden geometry, and smooths edges.", "author": "Modlumi, after CleanUp by Thomas Thomassen", "license": "MIT", "apiVersion": 1}// Cleans up models, especially imported ones: purges unused components and materials, merges// coplanar faces and identical materials, repairs split edges, erases stray edges and hidden// geometry, and smooths edges by angle, all in one undo step.//// A port of CleanUp (https://github.com/thomthom/cleanup)://// Copyright (c) 2009 Thomas Thomassen//// Permission is hereby granted, free of charge, to any person obtaining a copy of this software// and associated documentation files (the "Software"), to deal in the Software without// restriction, including without limitation the rights to use, copy, modify, merge, publish,// distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the// Software is furnished to do so, subject to the following conditions://// The above copyright notice and this permission notice shall be included in all copies or// substantial portions of the Software.//// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING// BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
/** * @typedef {"Whole model" | "Context being edited"} Scope * @typedef {{ * scope: Scope, * purge: boolean, * eraseHidden: boolean, * mergeFaces: boolean, * ignoreMaterials: boolean, * mergeMaterials: boolean, * repairEdges: boolean, * strayEdges: boolean, * smoothAngle: number, * }} Options * @typedef {Record<string, number>} Statistics */
const model = modlumi.activeModel;/** @type {Scope[]} */const kScopes = ["Whole model", "Context being edited"];
/** @type {Options} */const kDefaults = { scope: "Whole model", purge: true, eraseHidden: false, mergeFaces: true, ignoreMaterials: false, mergeMaterials: false, repairEdges: true, strayEdges: true, smoothAngle: 0,};
/** @type {Record<string, modlumi.FormField>} */const kSchema = { scope: { type: "radio", label: "Scope", choices: kScopes }, purge: { type: "checkbox", label: "Purge unused components and materials" }, eraseHidden: { type: "checkbox", label: "Erase hidden geometry" }, mergeFaces: { type: "checkbox", label: "Merge coplanar faces" }, ignoreMaterials: { type: "checkbox", label: "Merge faces with different materials", tip: "Coplanar faces merge even when their materials differ", }, mergeMaterials: { type: "checkbox", label: "Merge identical materials", tip: "Materials with the same color and image become one, across the whole model", }, repairEdges: { type: "checkbox", label: "Repair split edges" }, strayEdges: { type: "checkbox", label: "Erase stray edges", tip: "Edges that bound no face" }, smoothAngle: { type: "angle", label: "Smooth edges below", min: 0, max: Math.PI / 2, tip: "Softens and smooths edges between faces that meet at less than this angle; 0 smooths none", },};
/** * The drawing contexts in scope: the context being edited, or the model root and every * component definition. * @param {Scope} scope * @returns {modlumi.Entities[]} */function getContexts(scope) { if (scope === "Context being edited") { return [model.activeEntities]; } return [model.entities, ...model.definitions.map((definition) => definition.entities)];}
/** * Erases hidden faces, containers and guides, then hidden or soft edges that no visible face * needs. Returns how many elements went. * @param {modlumi.Entities} entities */function eraseHidden(entities) { let erased = 0; for (const element of entities.toArray()) { if (element.type !== "Edge" && element.isValid && element.hidden) { entities.erase(element); erased += 1; } } const edges = entities.ofType("Edge").filter((edge) => (edge.hidden || edge.soft) && edge.faces.every((face) => face.hidden)); return erased + eraseEdges(entities, edges);}
/** * Erases edges, skipping curves of which only some edges were chosen, because erasing one * edge of a curve erases all of it. Returns how many edges went. * @param {modlumi.Entities} entities * @param {modlumi.Edge[]} edges */function eraseEdges(entities, edges) { const chosen = new Set(edges); const erasable = edges.filter((edge) => edge.curve === null || edge.curve.edges.every((member) => chosen.has(member))); if (erasable.length > 0) { entities.eraseEdges(erasable); } return erasable.length;}
/** * Softens and smooths edges between two faces that meet at less than angle. Returns how many * edges changed. * @param {modlumi.Entities} entities * @param {number} angle */function smoothEdges(entities, angle) { let smoothed = 0; for (const edge of entities.ofType("Edge")) { const [first, second, ...rest] = edge.faces; if (!first || !second || rest.length > 0 || !first.normal || !second.normal || (edge.soft && edge.smooth)) { continue; } if (first.normal.angleBetween(second.normal) <= angle) { edge.soft = true; edge.smooth = true; smoothed += 1; } } return smoothed;}
/** * Whether two materials look the same: the same color and the same image at the same size. * @param {modlumi.Material} a * @param {modlumi.Material} b */function isIdentical(a, b) { const [ca, cb] = [a.color, b.color]; if (ca.r !== cb.r || ca.g !== cb.g || ca.b !== cb.b || ca.a !== cb.a) { return false; } const [ta, tb] = [a.texture, b.texture]; if (ta === null || tb === null) { return ta === tb; } return ta.width === tb.width && ta.height === tb.height && ta.image.length === tb.image.length && ta.image.every((byte, index) => byte === tb.image[index]);}
/** * Makes faces, edges and containers in every context use one of each set of identical materials, * and removes the others. Returns how many materials went. */function mergeMaterials() { /** @type {Map<modlumi.Material, modlumi.Material>} */ const replacements = new Map(); const materials = [...model.materials]; for (let index = 0; index < materials.length; ++index) { const kept = materials[index]; if (replacements.has(kept)) { continue; } for (const other of materials.slice(index + 1)) { if (!replacements.has(other) && isIdentical(kept, other)) { replacements.set(other, kept); } } } if (replacements.size === 0) { return 0; } /** @param {modlumi.Material | null} material */ const replace = (material) => (material === null ? null : replacements.get(material) ?? material); for (const entities of getContexts("Whole model")) { for (const face of entities.ofType("Face")) { face.material = replace(face.material); } for (const edge of entities.ofType("Edge")) { edge.material = replace(edge.material); } for (const container of [...entities.ofType("Group"), ...entities.ofType("ComponentInstance")]) { container.material = replace(container.material); } } for (const material of replacements.keys()) { model.removeMaterial(material); } return replacements.size;}
/** * Adds amount to a statistic. * @param {Statistics} statistics * @param {string} name * @param {number} amount */function count(statistics, name, amount) { statistics[name] = (statistics[name] ?? 0) + amount;}
/** * Cleans up with the options as one undo step and returns what it did. * @param {Options} options * @returns {Statistics} */function cleanUp(options) { /** @type {Statistics} */ const statistics = {}; model.operation("Clean Up", () => { const purge = () => { const purged = model.purgeUnused(); count(statistics, "Components purged", purged.definitions); count(statistics, "Materials purged", purged.materials); }; if (options.eraseHidden) { for (const entities of getContexts(options.scope)) { count(statistics, "Hidden elements erased", eraseHidden(entities)); } } // Purging first leaves fewer contexts to clean; purging again removes what cleaning orphaned. if (options.purge) { purge(); } if (options.mergeMaterials) { count(statistics, "Materials merged", mergeMaterials()); } for (const entities of getContexts(options.scope)) { if (options.mergeFaces) { count(statistics, "Edges between coplanar faces erased", entities.mergeCoplanarFaces({ sameMaterials: !options.ignoreMaterials })); } if (options.strayEdges) { const stray = entities.ofType("Edge").filter((edge) => edge.faces.length === 0); count(statistics, "Stray edges erased", eraseEdges(entities, stray)); } if (options.repairEdges) { count(statistics, "Split edges repaired", entities.mergeCollinearEdges()); } if (options.smoothAngle > 0) { count(statistics, "Edges smoothed", smoothEdges(entities, options.smoothAngle)); } } if (options.purge) { purge(); } }); return statistics;}
/** * Describes the statistics in one line per change, or says nothing needed cleaning. * @param {Statistics} statistics */function describe(statistics) { const lines = Object.entries(statistics).filter(([, value]) => value > 0).map(([name, value]) => `${name}: ${value}`); return lines.length > 0 ? lines.join("\n") : "The model was already clean.";}
/** @param {modlumi.PluginContext} context */export function activate(context) { /** The last options used, which the dialog starts from. @returns {Options} */ const lastOptions = () => ({ ...kDefaults, ...context.settings.get("options", {}) });
/** @param {Options} options */ function run(options) { try { context.settings.set("options", options); } catch (error) { console.warn(`Could not remember the cleanup options: ${error}`); } context.alert(describe(cleanUp(options)), { title: "Clean Up" }); }
context.registerCommand({ id: "clean", title: "Clean Up...", tip: "Chooses what to clean, then cleans the model in one undo step.", run() { let options = lastOptions(); context.showDialog({ id: "options", title: "Clean Up", modal: true, draw(ui) { options = /** @type {Options} */ (ui.form(options, kSchema)); ui.spacing(); ui.row(() => { if (ui.button("Clean Up", { primary: true })) { ui.close(); run(options); } if (ui.button("Cancel")) { ui.close(); } }); }, }); }, }); context.registerCommand({ id: "clean-again", title: "Clean Up with Last Settings", tip: "Cleans the model with the options used last time.", run: () => run(lastOptions()), });
/** Commands that run one cleanup step on the context being edited, or the model. */ const steps = /** @type {const} */ ([ ["purge", "Purge Unused", "Removes components and materials nothing uses.", { purge: true }], ["merge-faces", "Merge Coplanar Faces", "Erases edges between coplanar faces with the same materials.", { mergeFaces: true }], ["merge-materials", "Merge Identical Materials", "Makes identical materials one.", { mergeMaterials: true }], ["repair-edges", "Repair Split Edges", "Joins collinear edges split by an unneeded vertex.", { repairEdges: true }], ["stray-edges", "Erase Stray Edges", "Erases edges that bound no face.", { strayEdges: true }], ["erase-hidden", "Erase Hidden Geometry", "Erases hidden faces, edges, groups and components.", { eraseHidden: true }], ]); /** @type {Options} */ const nothing = { scope: "Context being edited", purge: false, eraseHidden: false, mergeFaces: false, ignoreMaterials: false, mergeMaterials: false, repairEdges: false, strayEdges: false, smoothAngle: 0, }; for (const [id, title, tip, enabled] of steps) { context.registerCommand({ id, title, tip, run: () => context.showMessage(describe(cleanUp({ ...nothing, ...enabled })).replaceAll("\n", "; ")), }); }}Cut List
Section titled “Cut List”Lists the parts of a woodworking model with their sizes, quantities and materials, works out how many sheets and boards to buy, and shows how to cut them.
Make each board a group or a component, then choose Extensions → Cut List → Cut List. Every visible group or component with faces of its own is a part, measured along its own axes, so boards turned at an angle measure true. Its thickness is its smallest size and its length the one the grain runs along. Identical parts share a row and a letter. With groups or components selected, the list shows only those; select nothing to list the whole model.
- Parts lists them. Choose one to outline its copies in the model, set which way its grain runs or leave it out of the list, such as a drawer pull. Double-click it to zoom to it.
- Materials sets how each material is bought: as sheet goods laid out on sheets of a size you set, as lumber cut to length from boards of one cross section, as solid wood totaled by volume (board feet in imperial models), or not cut, such as glass. Common names such as plywood, MDF and pine are recognized. Turn off Has grain for sheets such as MDF so parts can turn. Saw kerf and edge trim are set here too.
- Diagrams shows the cutting layout of each sheet and board, with each part’s letter and size. The layouts can be cut with straight saw cuts across the whole piece, as on a table or panel saw.
Export CSV writes the parts to a spreadsheet. Save Report writes a page with a shopping list, the parts and every cutting diagram; open it in a web browser to print it or save it as a PDF. Both are under File → Export too. The settings are saved with the model.
Source code
{ "id": "cut-list", "name": "Cut List", "version": "1.0.0", "description": "Lists the parts of a woodworking model with their sizes, quantities, materials and grain, lays them out on sheets and boards, and writes a CSV file or a cutting report to print.", "author": "Modlumi", "license": "MIT", "apiVersion": 1, "permissions": { "files.dialog": true }}// Cut List: lists the parts of a woodworking model, the groups and components with faces of// their own, with their sizes, quantities and materials, works out how many sheets and boards// to buy and how to cut them, and writes it all to a CSV file or a report to print. It lists// the selected objects, or the whole model when nothing is selected. Choosing a part// highlights its copies in the model; double-clicking it zooms to them.
import { collectParts, readPartData } from "./parts.js";import { describeGrain, getPartColor, writeCsv, writeReport } from "./report.js";import { describeNeed, getTooLarge, kKindNames, planStock, readCutSettings, readMaterialSettings, writeMaterialSettings,} from "./stock.js";
/** @typedef {import("./parts.js").Part} Part *//** @typedef {import("./parts.js").Grain} Grain *//** @typedef {import("./parts.js").Container} Container *//** @typedef {import("./stock.js").StockGroup} StockGroup *//** @typedef {import("./stock.js").StockKind} StockKind */
const model = modlumi.activeModel;
const kHighlight = { r: 0.95, g: 0.45, b: 0.05, a: 1 };const kGrainChoices = /** @type {const} */ (["Along length", "Along width", "Either way"]);/** @type {Record<typeof kGrainChoices[number], Grain>} */const kGrainByChoice = { "Along length": "length", "Along width": "width", "Either way": "any" };const kKinds = /** @type {StockKind[]} */ (Object.keys(kKindNames));// Diagrams are drawn this many pixels wide, a little narrower than the panel opens at first.const kDiagramWidth = 500;// How long one panel draw spends measuring parts before showing its progress, in milliseconds,// so a large model keeps the window responsive while it's measured over several frames.const kMeasureSlice = 30;
/** * Converts a color written as `#rrggbb` to a {@link modlumi.Color}. * @param {string} hex */function toColor(hex) { const value = parseInt(hex.slice(1), 16); return { r: ((value >> 16) & 255) / 255, g: ((value >> 8) & 255) / 255, b: (value & 255) / 255, a: 1 };}
/** @param {unknown} element @returns {element is Container} */function isContainer(element) { return element instanceof modlumi.Group || element instanceof modlumi.ComponentInstance;}
/** * The containers to list and where they are: the selected groups and instances, or every one at * the model root when none is selected. */function getScope() { const selected = model.selection.toArray().filter(isContainer); if (selected.length > 0) { return { selected: true, roots: selected, above: [...model.activePath], transformation: model.activeTransformation }; } const roots = model.entities.toArray().filter(isContainer); return { selected: false, roots, above: [], transformation: new modlumi.Transformation() };}
/** * Lays parts out on their stock. * @param {modlumi.PluginContext} context * @param {Part[]} parts */function plan(context, parts) { const cut = readCutSettings(context, model); /** @type {Map<modlumi.Material | null, import("./stock.js").MaterialSettings>} */ const settings = new Map(); /** @param {modlumi.Material | null} material */ const settingsFor = (material) => { let found = settings.get(material); if (found === undefined) { found = readMaterialSettings(context, model, material); settings.set(material, found); } return found; }; const groups = planStock(parts, settingsFor, cut, (meters) => model.formatLength(meters)); return { parts, groups, cut, settingsFor };}
/** * Returns a camera looking the same way as `camera` with `bounds` filling the view. * @param {modlumi.Camera} camera * @param {modlumi.BoundingBox} bounds */function frame(camera, bounds) { const direction = camera.target.vectorTo(camera.eye).normalize(); const radius = Math.max(bounds.diagonal / 2, 0.05); const distance = radius / Math.tan((camera.fieldOfView * Math.PI) / 360) * 1.3; return new modlumi.Camera(bounds.center.offset(direction.scale(distance)), bounds.center, { up: camera.up, perspective: camera.perspective, fieldOfView: camera.fieldOfView, });}
/** * The world-space box around every copy of a part. * @param {Part} part */function getBounds(part) { let [minX, minY, minZ] = [Infinity, Infinity, Infinity]; let [maxX, maxY, maxZ] = [-Infinity, -Infinity, -Infinity]; for (const instance of part.instances) { for (const corner of [0, 1, 2, 3, 4, 5, 6, 7]) { const local = instance.min.offset([ corner & 1 ? instance.size.x : 0, corner & 2 ? instance.size.y : 0, corner & 4 ? instance.size.z : 0, ]); const { x, y, z } = local.transform(instance.transformation); [minX, minY, minZ] = [Math.min(minX, x), Math.min(minY, y), Math.min(minZ, z)]; [maxX, maxY, maxZ] = [Math.max(maxX, x), Math.max(maxY, y), Math.max(maxZ, z)]; } } return new modlumi.BoundingBox([minX, minY, minZ], [maxX, maxY, maxZ]);}
/** @param {modlumi.PluginContext} context */export function activate(context) { /** @type {ReturnType<typeof plan> | null} */ let planned = null; // The measuring in progress, while `planned` is null. /** @type {{ parts: ReturnType<typeof collectParts>, total: number, done: number } | null} */ let measuring = null; let scopeIsSelection = false; // The chosen part's row, and one of its containers, which finds the row again after a rename // changed its id. /** @type {string | null} */ let chosenId = null; /** @type {Container | null} */ let chosen = null; /** @type {string | null} */ let chosenGroup = null;
const invalidate = () => { planned = null; measuring = null; }; context.onModelChanged(invalidate); context.onSelectionChanged(invalidate); context.onActiveContextChanged(invalidate); context.onDocumentOpened(() => { invalidate(); chosenId = null; chosen = null; });
/** * Measures the parts for up to `milliseconds`, or to the end without a limit, and returns the * plan once it's complete, or null while measuring goes on. * @param {number} [milliseconds] */ function advance(milliseconds = Infinity) { if (planned !== null) { return planned; } if (measuring === null) { const scope = getScope(); scopeIsSelection = scope.selected; const parts = collectParts(model, context, scope.roots, scope.above, scope.transformation); measuring = { parts, total: Math.max(1, model.definitions.length), done: 0 }; } const end = Date.now() + milliseconds; for (;;) { /** @type {IteratorResult<number | void, Part[]>} */ let step; try { step = measuring.parts.next(); } catch (error) { // Under a busy frame budget a draw can run before the change listener that would have // restarted measuring, and meet an entity the change deleted; start over instead. if (error instanceof ReferenceError) { measuring = null; return null; } throw error; } if (step.done) { planned = plan(context, step.value); measuring = null; return planned; } measuring.done = step.value ?? measuring.done; if (Date.now() >= end) { return null; } } }
/** The complete plan, measuring whatever is left at once. */ function getPlan() { // A measuring that met a deleted entity restarts, and then runs on the current model. return advance() ?? /** @type {ReturnType<typeof plan>} */ (advance()); }
/** * A plan measured afresh, for exports: commands and exporters run before the change listeners * in a frame, so the panel's plan may not show a change made earlier in it yet. */ function getFreshPlan() { invalidate(); return getPlan(); }
/** @returns {Part | null} */ function getChosenPart() { if (planned === null) { return null; } return planned.parts.find((part) => part.id === chosenId) ?? planned.parts.find((part) => part.instances.some((instance) => instance.container === chosen)) ?? null; }
/** @param {Part | undefined | null} part */ function choose(part) { chosenId = part?.id ?? null; chosen = part?.instances[0]?.container ?? null; }
/** @param {Part} part */ function zoomTo(part) { context.viewport.setCamera(frame(context.viewport.camera, getBounds(part)), { duration: 0.5 }); }
/** * Stores part data on every definition of a part, in one undo step. * @param {string} name * @param {Part} part * @param {Partial<{ grain: Grain, exclude: boolean }>} change */ function changePart(name, part, change) { model.operation(name, () => { for (const definition of part.definitions) { context.entityData.set(definition, { version: 1, ...readPartData(context, definition), ...change }); } }); }
/** The definitions the user left out of the cut list. */ function getExcluded() { return model.definitions.filter((definition) => readPartData(context, definition).exclude); }
/** @param {modlumi.Ui} ui */ function drawParts(ui) { const { parts } = getPlan(); let part = getChosenPart(); const rows = parts.map((row) => ({ id: row.id, cells: [ row.letter, row.name, row.instances.length, model.formatLength(row.length), model.formatLength(row.width), model.formatLength(row.thickness), row.materialName, ], })); const columns = [ "Part", "Name", { title: "Qty", numeric: true }, { title: "Length", numeric: true }, { title: "Width", numeric: true }, { title: "Thickness", numeric: true }, "Material", ]; const picked = ui.table("Parts", columns, rows, part?.id ?? null, { visibleRows: 12, onActivate(id) { const activated = parts.find((row) => row.id === id); if (activated !== undefined) { zoomTo(activated); } }, }); if (picked !== (part?.id ?? null)) { choose(parts.find((row) => row.id === picked)); part = getChosenPart(); } if (part === null) { ui.text("Choose a part to see it in the model and set its grain. Double-click it to zoom to it.", { tone: "muted" }); } else { ui.separator(`Part ${part.letter}`); const name = ui.textField("Name", part.name, { id: "name" }); if (name !== part.name && name.trim() !== "") { model.operation("Rename Part", () => { for (const instance of part.instances) { if (instance.container instanceof modlumi.ComponentInstance) { instance.container.definition.name = name; } else { instance.container.name = name; } } }); } const grain = ui.dropdown("Grain", describeGrain(part.grain), kGrainChoices, { tip: "Which way the grain runs: the part's length is measured along it, and \"Either way\" lets the layout turn the part", }); if (kGrainByChoice[grain] !== part.grain) { changePart("Change Grain", part, { grain: kGrainByChoice[grain] }); } ui.row(() => { if (ui.button("Zoom to Part")) { zoomTo(part); } if (ui.button("Leave Out", { tip: "Takes this part off the cut list, such as a drawer pull or a jig" })) { changePart("Leave Out Part", part, { exclude: true }); choose(null); } }, { id: "part-actions" }); } const excluded = getExcluded(); if (excluded.length > 0) { ui.row(() => { ui.text(`${excluded.length} left out`, { tone: "muted" }); if (ui.button("Put Back", { tip: "Lists the parts you left out again" })) { model.operation("Put Back Parts", () => { for (const definition of excluded) { context.entityData.set(definition, { version: 1, ...readPartData(context, definition), exclude: false }); } }); } }, { id: "excluded" }); } }
/** @param {modlumi.Ui} ui */ function drawMaterials(ui) { const { parts, groups, cut, settingsFor } = getPlan(); /** @type {Map<modlumi.Material | null, string>} */ const materials = new Map(); for (const part of parts) { materials.set(part.material, part.materialName); } for (const [material, name] of materials) { ui.section(name, () => { const settings = settingsFor(material); /** @param {Partial<import("./stock.js").MaterialSettings>} change */ const store = (change) => { model.operation("Change Stock", () => writeMaterialSettings(context, model, material, { ...settings, ...change })); }; const kind = ui.dropdown("Bought as", kKindNames[settings.kind], Object.values(kKindNames), { tip: "Sheet goods are laid out on sheets, lumber is cut to length from boards, solid wood is totaled by volume", }); const chosenKind = kKinds.find((candidate) => kKindNames[candidate] === kind) ?? settings.kind; if (chosenKind !== settings.kind) { store({ kind: chosenKind }); } if (settings.kind === "sheet") { const length = ui.length("Sheet length", settings.sheetLength, { min: 0.01 }); const width = ui.length("Sheet width", settings.sheetWidth, { min: 0.01 }); const grain = ui.checkbox("Has grain", settings.grain, { tip: "Keeps each part's length along the sheet's length; turn it off for MDF and other sheets without grain", }); if (length !== settings.sheetLength || width !== settings.sheetWidth || grain !== settings.grain) { store({ sheetLength: length, sheetWidth: width, grain }); } } else if (settings.kind === "lumber") { const length = ui.length("Board length", settings.boardLength, { min: 0.01 }); if (length !== settings.boardLength) { store({ boardLength: length }); } } for (const group of groups.filter((candidate) => candidate.materialName === name)) { const tooLarge = getTooLarge(group); ui.text(`${group.title}: ${describeNeed(group, model)}`, { tone: "muted" }); if (tooLarge.length > 0) { ui.text(`Too large for the stock: ${tooLarge.join(", ")}`, { tone: "warning" }); } } }, { id: `material-${material?.entityId ?? "none"}` }); } ui.separator("Saw"); const kerf = ui.length("Saw kerf", cut.kerf, { min: 0, tip: "The width of wood each cut turns to dust" }); const trim = ui.length("Edge trim", cut.trim, { min: 0, tip: "Comes off every sheet edge and board end first" }); if (kerf !== cut.kerf || trim !== cut.trim) { model.operation("Change Saw", () => context.documentData.set({ ...cut, kerf, trim })); } }
/** @param {modlumi.Ui} ui */ function drawDiagrams(ui) { const { parts, groups } = getPlan(); const laidOut = groups.filter((group) => group.sheets !== null || group.bars !== null); if (laidOut.length === 0) { ui.text("Nothing to lay out. Diagrams show the parts of sheet goods and lumber; set how each material is bought " + "under Materials.", { tone: "muted" }); return; } let group = laidOut.find((candidate) => candidate.id === chosenGroup) ?? laidOut[0]; const titles = laidOut.map((candidate) => candidate.title); const title = ui.dropdown("Stock", group.title, titles); if (title !== group.title) { group = laidOut[titles.indexOf(title)]; } chosenGroup = group.id; ui.text(describeNeed(group, model), { tone: "muted" }); const tooLarge = getTooLarge(group); if (tooLarge.length > 0) { ui.text(`Too large for the stock: ${tooLarge.join(", ")}`, { tone: "warning" }); } const chosenLetter = getChosenPart()?.letter ?? null; const shown = group; const height = shown.sheets !== null ? shown.sheets.sheets.length * (shown.settings.sheetWidth * (kDiagramWidth / shown.settings.sheetLength) + 30) : (shown.bars?.bars.length ?? 0) * 52; const event = ui.canvas("Diagram", Math.max(40, height), (canvas) => { const stroke = { r: 0.25, g: 0.25, b: 0.25, a: 1 }; const ink = { r: 0.1, g: 0.1, b: 0.1, a: 1 }; /** * Draws a piece with its letter, and its size where it fits, outlined in the highlight color * when it's the chosen part. * @param {string} letter @param {number} x @param {number} y @param {number} w @param {number} h * @param {string} size */ const piece = (letter, x, y, w, h, size) => { const isChosen = letter === chosenLetter; canvas.rect(x, y, w, h, { id: letter, fill: toColor(getPartColor(letter)), stroke: isChosen ? kHighlight : stroke, width: isChosen ? 2.5 : 1, }); // Text is about 7 pixels a character and 14 high. const sized = h >= 40 && w >= 7 * size.length + 8; if (w > 12 && h > 14) { canvas.text([x + w / 2 - 3.5 * letter.length, y + h / 2 - (sized ? 15 : 7)], letter, { stroke: ink }); } if (sized) { canvas.text([x + w / 2 - 3.5 * size.length, y + h / 2 + 1], size, { stroke: ink }); } }; /** @param {string} letter */ const sizeOf = (letter) => { const part = parts.find((candidate) => candidate.letter === letter); return part === undefined ? "" : `${model.formatLength(part.length)} × ${model.formatLength(part.width)}`; }; let top = 0; if (shown.sheets !== null) { const scale = kDiagramWidth / shown.settings.sheetLength; shown.sheets.sheets.forEach((sheet, index, sheets) => { canvas.text([0, top], `Sheet ${index + 1} of ${sheets.length}`); top += 20; canvas.rect(0, top, kDiagramWidth, shown.settings.sheetWidth * scale, { fill: { r: 0.93, g: 0.93, b: 0.93, a: 1 }, stroke }); for (const placed of sheet.placements) { piece(placed.id, placed.x * scale, top + placed.y * scale, placed.length * scale, placed.width * scale, sizeOf(placed.id)); } top += shown.settings.sheetWidth * scale + 10; }); } else if (shown.bars !== null) { const scale = kDiagramWidth / shown.settings.boardLength; shown.bars.bars.forEach((bar, index, bars) => { canvas.text([0, top], `Board ${index + 1} of ${bars.length}`); top += 20; canvas.rect(0, top, kDiagramWidth, 26, { fill: { r: 0.93, g: 0.93, b: 0.93, a: 1 }, stroke }); for (const cut of bar.cuts) { piece(cut.id, cut.x * scale, top, cut.length * scale, 26, ""); } top += 32; }); } }); if (event?.type === "click" && event.item !== null) { choose(parts.find((part) => part.letter === event.item)); } else if (event?.type === "doubleClick" && event.item !== null) { const part = parts.find((candidate) => candidate.letter === event.item); choose(part); if (part !== undefined) { zoomTo(part); } } }
async function exportCsv() { const { parts } = getFreshPlan(); const file = await context.files.save({ title: "Export Cut List", name: "cut-list.csv", filters: [{ name: "CSV spreadsheet", extensions: ["csv"] }], }); if (file !== null) { file.writeText(writeCsv(parts, model, context)); context.showMessage(`Wrote ${parts.length} parts to ${file.name}`); } }
function getReport() { const { parts, groups, cut } = getFreshPlan(); const date = new Date().toISOString().slice(0, 10); return writeReport(parts, groups, model, { title: "Cut List", date, kerf: cut.kerf, trim: cut.trim }); }
async function exportReport() { const file = await context.files.save({ title: "Save Cut List Report", name: "cut-list.html", filters: [{ name: "Web page", extensions: ["html"] }], }); if (file !== null) { file.writeText(getReport()); context.showMessage(`Wrote ${file.name}. Open it in a web browser to print it or save it as a PDF.`); } }
const panel = context.addPanel({ id: "cut-list", title: "Cut List", width: 560, height: 600, draw(ui) { if (advance(kMeasureSlice) === null) { const progress = measuring === null ? 0 : Math.min(1, measuring.done / measuring.total); ui.text("Measuring the parts...", { tone: "muted" }); ui.progress(progress); return; } const { parts } = getPlan(); const count = parts.reduce((sum, part) => sum + part.instances.length, 0); if (parts.length === 0) { ui.text(scopeIsSelection ? "The selection has no parts. Parts are groups and components with faces of their own." : "No parts yet. Make each board a group or component, and it shows up here with its size.", { tone: "muted" }); if (getExcluded().length === 0) { return; } } else { ui.text(`${count} ${count === 1 ? "part" : "parts"} from ${scopeIsSelection ? "the selection" : "the whole model"}` + (scopeIsSelection ? ". Select nothing to list the whole model." : ""), { tone: "muted" }); } ui.tabs({ Parts: () => drawParts(ui), Materials: () => drawMaterials(ui), Diagrams: () => drawDiagrams(ui), }); ui.separator(); ui.row(() => { if (ui.button("Export CSV...", { tip: "Writes the parts to a spreadsheet file" })) { void exportCsv(); } if (ui.button("Save Report...", { primary: true, tip: "Writes the parts, shopping list and cutting diagrams to a page to print" })) { void exportReport(); } }, { id: "export" }); }, });
// The chosen part's copies, outlined in the model while the panel is open. if (context.features.includes("previewBoxes")) { context.addOverlay((view) => { // It only shows parts already measured, since overlays draw every frame. const part = panel.isOpen ? getChosenPart() : null; for (const instance of part?.instances ?? []) { view.box(instance.min, instance.size, { transformation: instance.transformation, color: kHighlight, width: 3 }); } }); }
context.registerCommand({ id: "open", title: "Cut List", tip: "Lists the parts to cut, with their sizes, and lays them out on sheets and boards.", icon: "icons/cut-list.svg", toolbar: true, run: () => panel.show(), });
context.registerExporter({ id: "csv", name: "Cut List (CSV)", extensions: ["csv"], export(file) { file.writeText(writeCsv(getFreshPlan().parts, model, context)); }, });
context.registerExporter({ id: "report", name: "Cut List Report (HTML)", extensions: ["html"], export(file) { file.writeText(getReport()); }, });}// Finds the parts of a model: every visible group or component instance with faces of its own.// A part's size is its faces' extent along its own axes, scaled as it's placed, so parts that// are turned at an angle measure true. Its thickness is the smallest of the three sizes and its// length the one the grain runs along, the largest unless the part says otherwise.
/** @typedef {modlumi.Group | modlumi.ComponentInstance} Container */
/** * How a part's grain runs: along its longest side, along its middle one, or either way, for * parts the layout may turn. * @typedef {"length" | "width" | "any"} Grain */
/** * What the plugin keeps on a part's definition. * @typedef {{ version: 1, grain?: Grain, exclude?: boolean }} PartData */
/** * One placed copy of a part: the container, the groups and instances above it from the model * root down, its transformation to world coordinates, and the box around its faces in its own * coordinates. * @typedef {{ * container: Container, * path: Container[], * transformation: modlumi.Transformation, * min: modlumi.Point3d, * size: modlumi.Vector3d, * }} PartInstance */
/** * A row of the cut list: identical parts of one material, with their sizes in meters. * @typedef {{ * id: string, * letter: string, * name: string, * material: modlumi.Material | null, * materialName: string, * length: number, * width: number, * thickness: number, * grain: Grain, * definitions: modlumi.ComponentDefinition[], * instances: PartInstance[], * }} Part */
/** The name of parts without a material. */export const kNoMaterial = "No material";
// Sizes are compared after rounding to this, in meters, a hundredth of a millimeter.const kSizeStep = 1e-5;
/** * A definition's own faces: their extent, the area of each material on them, and the faces * themselves, for parts scaled unevenly. * @typedef {{ * min: modlumi.Point3d, * size: modlumi.Vector3d, * areas: Map<modlumi.Material | null, number>, * faces: modlumi.Face[], * scaled: Map<string, Map<modlumi.Material | null, number>>, * }} DefinitionInfo */
// Normals closer than this to parallel make faces one plane's, as sines.const kParallel = 1e-6;
// Elements read between yields of a large definition.const kChunk = 500;
/** * A definition's elements sorted for measuring: its faces, its groups and instances, and * whether it holds anything else beside its faces, such as a stray edge, a guide or a label. * @typedef {{ faces: modlumi.Face[], children: Container[], extras: boolean }} SortedElements */
/** * Sorts a definition's elements, yielding after each chunk of them. The iterator wraps them * all at once natively, which no yield can split. * @param {modlumi.ComponentDefinition} definition * @returns {Generator<void, SortedElements, void>} */function* sortElements(definition) { /** @type {modlumi.Face[]} */ const faces = []; /** @type {Container[]} */ const children = []; let extras = false; let read = 0; // Iterating is much quicker than ofType for large definitions, and can pause between elements. for (const element of definition.entities) { if (element instanceof modlumi.Face) { faces.push(element); } else if (element instanceof modlumi.Group || element instanceof modlumi.ComponentInstance) { children.push(element); } else if (!(element instanceof modlumi.Edge) || element.faces.length === 0) { extras = true; } read += 1; if (read % kChunk === 0) { yield; } } return { faces, children, extras };}
/** * Measures a definition's own faces, leaving out nested groups and instances, yielding between * chunks of faces. Returns null when it has none or they all lie in parallel planes, as a * drawing does. * @param {modlumi.ComponentDefinition} definition * @param {SortedElements} sorted * @returns {Generator<void, DefinitionInfo | null, void>} */function* measure(definition, { faces, children, extras }) { /** @type {Map<modlumi.Material | null, number>} */ const areas = new Map(); /** @type {modlumi.Vector3d | null} */ let first = null; let flat = true; for (let index = 0; index < faces.length; index += 1) { const face = faces[index]; const normal = face.normal; if (normal !== null) { first ??= normal; flat &&= normal.cross(first).length < kParallel; } areas.set(face.material, (areas.get(face.material) ?? 0) + face.area); if ((index + 1) % kChunk === 0) { yield; } } if (flat) { return null; } // The context's bounds are measured natively, but take in everything in it, so definitions // with more than faces and their edges measure the corners of their faces instead. let min; let max; const bounds = children.length > 0 || extras ? null : definition.entities.bounds; if (bounds !== null) { ({ min, max } = bounds); } else { let [minX, minY, minZ] = [Infinity, Infinity, Infinity]; let [maxX, maxY, maxZ] = [-Infinity, -Infinity, -Infinity]; for (let index = 0; index < faces.length; index += 1) { // Holes lie inside the outer loop, and its edges run either way, so both ends count. for (const edge of faces[index].outerLoop.edges) { for (const vertex of [edge.start, edge.end]) { const { x, y, z } = vertex.position; [minX, minY, minZ] = [Math.min(minX, x), Math.min(minY, y), Math.min(minZ, z)]; [maxX, maxY, maxZ] = [Math.max(maxX, x), Math.max(maxY, y), Math.max(maxZ, z)]; } } if ((index + 1) % kChunk === 0) { yield; } } min = new modlumi.Point3d(minX, minY, minZ); max = new modlumi.Point3d(maxX, maxY, maxZ); } return { min, size: min.vectorTo(max), areas, faces, scaled: new Map() };}
/** * The area of each material on a definition's faces as placed: as measured, unless the part is * stretched or sheared, which changes which faces show the most. A face's placed area depends * only on the products of the placement's axes with each other, so stretched parts' areas are * kept on `info` by those, and measured yielding between chunks of faces. * @param {DefinitionInfo} info * @param {modlumi.Transformation} placed * @returns {Generator<void, Map<modlumi.Material | null, number>, void>} */function* getPlacedAreas(info, placed) { const { xAxis, yAxis, zAxis } = placed; const metric = [xAxis.dot(xAxis), yAxis.dot(yAxis), zAxis.dot(zAxis), xAxis.dot(yAxis), yAxis.dot(zAxis), zAxis.dot(xAxis)]; const size = Math.max(metric[0], metric[1], metric[2]); const even = Math.max(...metric.slice(0, 3)) - Math.min(...metric.slice(0, 3)) <= kParallel * size && metric.slice(3).every((product) => Math.abs(product) <= kParallel * size); if (even) { return info.areas; } const key = metric.map((product) => product.toPrecision(9)).join("|"); let areas = info.scaled.get(key); if (areas === undefined) { areas = new Map(); for (let index = 0; index < info.faces.length; index += 1) { const face = info.faces[index]; areas.set(face.material, (areas.get(face.material) ?? 0) + face.transformedArea(placed)); if ((index + 1) % kChunk === 0) { yield; } } info.scaled.set(key, areas); } return areas;}
/** * The material most of a part's area shows: its faces' own, or for faces without one, the * material the part or a container above it applies. * @param {Map<modlumi.Material | null, number>} areas * @param {modlumi.Material | null} applied */function getDominantMaterial(areas, applied) { /** @type {Map<modlumi.Material | null, number>} */ const shown = new Map(); for (const [material, area] of areas) { const showing = material ?? applied; shown.set(showing, (shown.get(showing) ?? 0) + area); } let best = null; let bestArea = -1; for (const [material, area] of shown) { if (area > bestArea) { best = material; bestArea = area; } } return best;}
/** @param {number} meters */function round(meters) { return Math.round(meters / kSizeStep) * kSizeStep;}
/** * Returns the letters that label the `index`th row: A to Z, then AA, AB and so on. * @param {number} index */export function getLetter(index) { let letters = ""; for (let rest = index + 1; rest > 0; rest = Math.floor((rest - 1) / 26)) { letters = String.fromCharCode(65 + ((rest - 1) % 26)) + letters; } return letters;}
/** * Reads a definition's part data, with defaults for what it lacks. * @param {modlumi.PluginContext} context * @param {modlumi.ComponentDefinition} definition * @returns {{ grain: Grain, exclude: boolean }} */export function readPartData(context, definition) { const data = /** @type {Partial<PartData> | undefined} */ (context.entityData.get(definition)); const grain = data?.grain === "width" || data?.grain === "any" ? data.grain : "length"; return { grain, exclude: data?.exclude === true };}
/** * The name a container goes by: a component's definition name, or a group's own name, or an * empty string. * @param {Container} container */function getName(container) { if (container instanceof modlumi.ComponentInstance) { return container.definition.name || container.name; } return container.name;}
/** * Collects the parts under `roots`, the containers to start from with the groups and instances * above them, merged into rows of identical parts and lettered in the order of the list: by * material, then thickest, longest and widest first. * * It yields the number of definitions measured so far as it goes, so a caller can spread the * work of a large model over several frames, and returns the parts. * @param {modlumi.Model} model * @param {modlumi.PluginContext} context * @param {readonly Container[]} roots * @param {readonly Container[]} above The groups and instances the roots are in, from the model root down. * @param {modlumi.Transformation} transformation Maps the roots' context to world coordinates. * @returns {Generator<number | void, Part[], void>} */export function* collectParts(model, context, roots, above, transformation) { /** @type {Map<modlumi.ComponentDefinition, { info: DefinitionInfo | null, children: Container[] }>} */ const measured = new Map(); /** @type {Map<string, Part>} */ const rows = new Map(); let visits = 0;
/** * Visits a container and everything in it, depth first, yielding as it measures. * @param {Container} container * @param {Container[]} path * @param {modlumi.Transformation} parent * @param {modlumi.Material | null} inherited * @param {string} wrapper The name of the container around this one when it holds nothing else. * @returns {Generator<number | void, void, void>} */ function* visit(container, path, parent, inherited, wrapper) { // Thousands of copies of measured parts take a while too. visits += 1; if (visits % kChunk === 0) { yield measured.size; } const full = [...path, container]; if (!model.isDrawingElementVisible(full)) { return; } const placed = parent.multiply(container.transformation); const applied = container.material ?? inherited; const definition = container.definition; let contents = measured.get(definition); if (contents === undefined) { const sorted = yield* sortElements(definition); contents = { info: yield* measure(definition, sorted), children: sorted.children }; measured.set(definition, contents); yield measured.size; } const { info, children } = contents; const data = readPartData(context, definition); const name = getName(container) || wrapper; if (info !== null && !data.exclude) { const material = getDominantMaterial(yield* getPlacedAreas(info, placed), applied); add(container, full, placed, info, material, data.grain, name || "Part"); } // A named component around a single unnamed group, as when a board was made a group and // then a component, lends the board its name. const lent = info === null && children.length === 1 ? name : ""; for (const child of children) { yield* visit(child, full, placed, applied, lent); } }
/** * @param {Container} container * @param {Container[]} path * @param {modlumi.Transformation} placed * @param {DefinitionInfo} info * @param {modlumi.Material | null} material * @param {Grain} grain * @param {string} name */ function add(container, path, placed, info, material, grain, name) { const sizes = [ info.size.x * placed.xAxis.length, info.size.y * placed.yAxis.length, info.size.z * placed.zAxis.length, ].map(round).sort((a, b) => b - a); // A part too thin to measure, such as one scaled flat, is a drawing, not something to cut. if (sizes[2] <= kSizeStep) { return; } const [longest, middle, thickness] = sizes; const [length, width] = grain === "width" ? [middle, longest] : [longest, middle]; const key = [name, material?.entityId ?? "", length, width, thickness, grain].join("|"); let row = rows.get(key); if (row === undefined) { row = { id: key, letter: "", name, material, materialName: material?.name || kNoMaterial, length, width, thickness, grain, definitions: [], instances: [], }; rows.set(key, row); } if (!row.definitions.includes(container.definition)) { row.definitions.push(container.definition); } row.instances.push({ container, path, transformation: placed, min: info.min, size: info.size }); }
// Roots inside an open group or component show the material applied above them. const inherited = above.reduce((material, container) => container.material ?? material, /** @type {modlumi.Material | null} */ (null)); for (const root of roots) { yield* visit(root, [...above], transformation, inherited, ""); } const parts = [...rows.values()].sort((a, b) => a.materialName.localeCompare(b.materialName) || b.thickness - a.thickness || b.length - a.length || b.width - a.width || a.name.localeCompare(b.name)); parts.forEach((part, index) => { part.letter = getLetter(index); }); return parts;}// What parts are cut from: each material's kind of stock and its sizes, kept on the material so// they travel with the model, and the layouts of parts on that stock.
import { nestBars, nestSheets } from "./nesting.js";import { kNoMaterial } from "./parts.js";
/** @typedef {import("./parts.js").Part} Part *//** @typedef {import("./nesting.js").SheetResult} SheetResult *//** @typedef {import("./nesting.js").BarLayout} BarLayout */
/** * How a material is bought: sheet goods laid out on sheets, lumber cut to length from boards of * one cross section, solid wood only totaled by volume, or left out of the cut list, such as * glass and hardware. * @typedef {"sheet" | "lumber" | "solid" | "ignore"} StockKind */
/** * A material's stock, in meters. `grain` says whether a sheet's parts must keep their length * along the sheet's length. * @typedef {{ * version: 1, * kind: StockKind, * sheetLength: number, * sheetWidth: number, * grain: boolean, * boardLength: number, * }} MaterialSettings */
/** * The model's cut settings: the saw's kerf, what comes off each sheet edge and board end, and * the stock of parts without a material, which has no material to keep it on. * @typedef {{ version: 1, kerf: number, trim: number, unassigned: MaterialSettings }} CutSettings */
/** * The parts cut from one stock: a material in one thickness, and for lumber, one width too. * @typedef {{ * id: string, * title: string, * materialName: string, * kind: StockKind, * thickness: number, * width: number, * settings: MaterialSettings, * parts: Part[], * count: number, * volume: number, * area: number, * sheets: SheetResult | null, * bars: { bars: BarLayout[], tooLarge: string[] } | null, * }} StockGroup */
/** The kinds as the panel lists them. */export const kKindNames = /** @type {const} */ ({ sheet: "Sheet goods", lumber: "Lumber", solid: "Solid wood", ignore: "Not cut",});
/** @param {modlumi.Model} model */export function isImperial(model) { return ["in", "ft", "yd"].includes(model.units.lengthUnit);}
/** * Guesses how a material is bought from its name, so the usual names need no setup. * @param {string} name * @returns {StockKind} */export function guessKind(name) { if (/glass|metal|steel|alumin|chrome|brass|rubber|fabric|leather|plastic|acrylic|mirror/i.test(name)) { return "ignore"; } if (/ply|mdf|hdf|osb|melamin|particle|chip|hardboard|board|veneer|sheet|panel|multiplex|spaanplaat|triplex/i.test(name)) { return "sheet"; } if (/lumber|timber|stud|2x|1x|4x|beam|joist|rafter|batten|cls|pine|spruce|fir/i.test(name)) { return "lumber"; } return "solid";}
/** * The stock a material starts with: a full sheet and an 8 foot board in imperial models, and * their metric counterparts otherwise. * @param {modlumi.Model} model * @param {string} name * @returns {MaterialSettings} */export function getDefaultSettings(model, name) { const imperial = isImperial(model); return { version: 1, kind: guessKind(name), sheetLength: imperial ? modlumi.inches(96) : 2.44, sheetWidth: imperial ? modlumi.inches(48) : 1.22, grain: !/mdf|hdf|osb|particle|chip|hardboard|melamin/i.test(name), boardLength: imperial ? modlumi.inches(96) : 2.4, };}
/** * Reads stored settings over defaults, keeping only values of the right type and range. * @param {unknown} stored * @param {MaterialSettings} defaults * @returns {MaterialSettings} */function readSettings(stored, defaults) { const value = /** @type {Partial<Record<keyof MaterialSettings, unknown>>} */ (stored ?? {}); /** @param {unknown} length @param {number} fallback */ const positive = (length, fallback) => (typeof length === "number" && length > 0 ? length : fallback); return { version: 1, kind: typeof value.kind === "string" && value.kind in kKindNames ? /** @type {StockKind} */ (value.kind) : defaults.kind, sheetLength: positive(value.sheetLength, defaults.sheetLength), sheetWidth: positive(value.sheetWidth, defaults.sheetWidth), grain: typeof value.grain === "boolean" ? value.grain : defaults.grain, boardLength: positive(value.boardLength, defaults.boardLength), };}
/** * Reads the model's cut settings. * @param {modlumi.PluginContext} context * @param {modlumi.Model} model * @returns {CutSettings} */export function readCutSettings(context, model) { const stored = /** @type {Partial<Record<keyof CutSettings, unknown>>} */ (context.documentData.get() ?? {}); /** @param {unknown} length @param {number} fallback */ const nonNegative = (length, fallback) => (typeof length === "number" && length >= 0 ? length : fallback); return { version: 1, kerf: nonNegative(stored.kerf, isImperial(model) ? modlumi.inches(1 / 8) : 0.003), trim: nonNegative(stored.trim, 0), unassigned: readSettings(stored.unassigned, getDefaultSettings(model, kNoMaterial)), };}
/** * Reads a material's stock, or that of parts without a material. * @param {modlumi.PluginContext} context * @param {modlumi.Model} model * @param {modlumi.Material | null} material */export function readMaterialSettings(context, model, material) { if (material === null) { return readCutSettings(context, model).unassigned; } return readSettings(context.entityData.get(material), getDefaultSettings(model, material.name));}
/** * Stores a material's stock, or that of parts without a material. Call it inside an operation. * @param {modlumi.PluginContext} context * @param {modlumi.Model} model * @param {modlumi.Material | null} material * @param {MaterialSettings} settings */export function writeMaterialSettings(context, model, material, settings) { if (material === null) { context.documentData.set({ ...readCutSettings(context, model), unassigned: settings }); } else { context.entityData.set(material, settings); }}
/** * Groups parts by the stock they're cut from and lays them out on it. Each sheet piece and * board cut has the id `"<letter>"`, the part's letter, so diagrams can label it. * @param {readonly Part[]} parts * @param {(material: modlumi.Material | null) => MaterialSettings} settingsFor * @param {CutSettings} cut * @param {(meters: number) => string} formatLength * @returns {StockGroup[]} */export function planStock(parts, settingsFor, cut, formatLength) { /** @type {Map<string, StockGroup>} */ const groups = new Map(); for (const part of parts) { const settings = settingsFor(part.material); const width = settings.kind === "lumber" ? part.width : 0; const id = [part.material?.entityId ?? "", part.thickness, width].join("|"); let group = groups.get(id); if (group === undefined) { const size = settings.kind === "lumber" ? `${formatLength(part.thickness)} × ${formatLength(part.width)}` : formatLength(part.thickness); group = { id, title: `${part.materialName}, ${size}`, materialName: part.materialName, kind: settings.kind, thickness: part.thickness, width, settings, parts: [], count: 0, volume: 0, area: 0, sheets: null, bars: null, }; groups.set(id, group); } const count = part.instances.length; group.parts.push(part); group.count += count; group.area += count * part.length * part.width; group.volume += count * part.length * part.width * part.thickness; } for (const group of groups.values()) { if (group.kind === "sheet") { const stock = { length: group.settings.sheetLength, width: group.settings.sheetWidth, kerf: cut.kerf, trim: cut.trim }; const pieces = group.parts.flatMap((part) => part.instances.map(() => ({ id: part.letter, length: part.length, width: part.width, canRotate: !group.settings.grain || part.grain === "any", }))); group.sheets = nestSheets(pieces, stock); } else if (group.kind === "lumber") { const stock = { length: group.settings.boardLength, kerf: cut.kerf, trim: cut.trim }; const pieces = group.parts.flatMap((part) => part.instances.map(() => ({ id: part.letter, length: part.length }))); group.bars = nestBars(pieces, stock); } } return [...groups.values()].filter((group) => group.kind !== "ignore");}
/** * The share of the stock's area or length that becomes parts, from 0 to 1, or null for stock * that isn't laid out. * @param {StockGroup} group */export function getYield(group) { if (group.sheets !== null && group.sheets.sheets.length > 0) { const used = group.sheets.sheets.reduce((sum, sheet) => sum + sheet.usedArea, 0); return used / (group.sheets.sheets.length * group.settings.sheetLength * group.settings.sheetWidth); } if (group.bars !== null && group.bars.bars.length > 0) { const used = group.bars.bars.reduce((sum, bar) => sum + bar.usedLength, 0); return used / (group.bars.bars.length * group.settings.boardLength); } return null;}
/** * Describes what a stock group needs, such as "3 sheets, 78% used" or "2.4 board feet". * @param {StockGroup} group * @param {modlumi.Model} model */export function describeNeed(group, model) { const percent = Math.round(100 * (getYield(group) ?? 0)); if (group.sheets !== null) { const count = group.sheets.sheets.length; return `${count} ${count === 1 ? "sheet" : "sheets"} of ${model.formatLength(group.settings.sheetLength)} × ` + `${model.formatLength(group.settings.sheetWidth)}, ${percent}% used`; } if (group.bars !== null) { const count = group.bars.bars.length; return `${count} ${count === 1 ? "board" : "boards"} of ${model.formatLength(group.settings.boardLength)}, ` + `${percent}% used`; } return formatVolume(group.volume, model);}
/** * Writes a volume of wood as woodworkers buy it: board feet in imperial models, cubic meters * otherwise. * @param {number} cubicMeters * @param {modlumi.Model} model */export function formatVolume(cubicMeters, model) { if (isImperial(model)) { const boardFeet = modlumi.toInches(modlumi.toInches(modlumi.toInches(cubicMeters))) / 144; return `${boardFeet.toFixed(2)} board feet`; } return `${cubicMeters.toFixed(4)} m³`;}
/** * The parts too large for their stock, by letter, or an empty list. * @param {StockGroup} group */export function getTooLarge(group) { return [...new Set(group.sheets?.tooLarge ?? group.bars?.tooLarge ?? [])];}// Lays parts out on stock: rectangles on sheets, cut with straight saw cuts from edge to edge// as on a table or panel saw, and lengths on boards. It only does arithmetic on plain numbers,// so it knows nothing of the model.
/** * A rectangle to cut from a sheet. `length` runs along the sheet's length, the grain, unless * the piece may turn. * @typedef {{ id: string, length: number, width: number, canRotate: boolean }} Piece */
/** * Where a piece sits on a sheet: its corner nearest the sheet's corner, and its size as placed. * @typedef {{ id: string, x: number, y: number, length: number, width: number, rotated: boolean }} Placement */
/** * One sheet of a layout, with the rectangles still free after the last cut, which are its * offcuts and waste. * @typedef {{ placements: Placement[], offcuts: Rect[], usedArea: number }} SheetLayout */
/** @typedef {{ x: number, y: number, length: number, width: number }} Rect */
/** * The stock sheets and how they're cut: `trim` comes off every edge first, and every saw cut * removes `kerf`. * @typedef {{ length: number, width: number, kerf: number, trim: number }} SheetStock */
/** * The sheets a set of pieces needs, and the pieces too large for any sheet. * @typedef {{ sheets: SheetLayout[], tooLarge: string[] }} SheetResult */
// Sizes closer than this are equal, in meters, so parts measured from a model fit exactly.const kTolerance = 1e-6;
/** * How a placement's free rectangle splits into the two that stay free: "short" cuts along * the leftover's shorter side, "long" along its longer side, "horizontal" across the sheet's * length first and "vertical" across its width first. * @typedef {"short" | "long" | "horizontal" | "vertical"} SplitRule *//** @type {readonly SplitRule[]} */const kSplitRules = ["short", "long", "horizontal", "vertical"];
/** @type {readonly ((a: Piece, b: Piece) => number)[]} */const kOrders = [ (a, b) => b.length * b.width - a.length * a.width, (a, b) => Math.max(b.length, b.width) - Math.max(a.length, a.width), (a, b) => b.length - a.length || b.width - a.width, (a, b) => b.width - a.width || b.length - a.length, (a, b) => b.length + b.width - (a.length + a.width),];
/** * Splits `free` around a piece placed in its corner with the size `length` × `width`, kerf * included, and returns the leftovers that aren't empty. * @param {Rect} free * @param {number} length * @param {number} width * @param {SplitRule} rule * @returns {Rect[]} */function split(free, length, width, rule) { const restLength = free.length - length; const restWidth = free.width - width; let lengthwise; switch (rule) { case "short": lengthwise = restLength < restWidth; break; case "long": lengthwise = restLength >= restWidth; break; case "horizontal": lengthwise = true; break; default: lengthwise = false; } // A lengthwise split makes a cut along the sheet's length first, so the strip beside the // piece runs the whole length of the free rectangle. const pieces = lengthwise ? [ { x: free.x + length, y: free.y, length: restLength, width }, { x: free.x, y: free.y + width, length: free.length, width: restWidth }, ] : [ { x: free.x + length, y: free.y, length: restLength, width: free.width }, { x: free.x, y: free.y + width, length, width: restWidth }, ]; return pieces.filter((rect) => rect.length > kTolerance && rect.width > kTolerance);}
/** * Finds the free rectangle a piece of `length` × `width` fits best, the one it leaves the * shortest side over in, or null. * @param {Rect[]} free * @param {number} length * @param {number} width */function findBestFit(free, length, width) { let best = -1; let bestShort = Infinity; let bestLong = Infinity; free.forEach((rect, index) => { const overLength = rect.length - length; const overWidth = rect.width - width; if (overLength < -kTolerance || overWidth < -kTolerance) { return; } const short = Math.min(overLength, overWidth); const long = Math.max(overLength, overWidth); if (short < bestShort - kTolerance || (Math.abs(short - bestShort) <= kTolerance && long < bestLong)) { best = index; bestShort = short; bestLong = long; } }); return best < 0 ? null : { index: best, short: bestShort };}
/** * Packs pieces in the given order, filling each sheet before starting the next, with one rule * for splitting free rectangles. * @param {readonly Piece[]} pieces * @param {SheetStock} stock * @param {SplitRule} rule * @returns {SheetLayout[]} */function pack(pieces, stock, rule) { // Every piece takes its size plus a kerf, so the usable sheet gets one kerf back for the // last piece of each row, which needs no cut beyond it. const usable = { x: stock.trim, y: stock.trim, length: stock.length - 2 * stock.trim + stock.kerf, width: stock.width - 2 * stock.trim + stock.kerf, }; /** @type {{ layout: SheetLayout, free: Rect[] }[]} */ const sheets = []; for (const piece of pieces) { /** @type {{ sheet: typeof sheets[number], index: number, rotated: boolean } | null} */ let chosen = null; for (const sheet of sheets) { const upright = findBestFit(sheet.free, piece.length + stock.kerf, piece.width + stock.kerf); const turned = piece.canRotate ? findBestFit(sheet.free, piece.width + stock.kerf, piece.length + stock.kerf) : null; if (upright !== null && (turned === null || upright.short <= turned.short)) { chosen = { sheet, index: upright.index, rotated: false }; } else if (turned !== null) { chosen = { sheet, index: turned.index, rotated: true }; } if (chosen !== null) { break; } } if (chosen === null) { const sheet = { layout: { placements: [], offcuts: [], usedArea: 0 }, free: [{ ...usable }] }; sheets.push(sheet); const fits = findBestFit(sheet.free, piece.length + stock.kerf, piece.width + stock.kerf) !== null; chosen = { sheet, index: 0, rotated: !fits }; } const { sheet, index, rotated } = chosen; const free = sheet.free[index]; const length = rotated ? piece.width : piece.length; const width = rotated ? piece.length : piece.width; sheet.layout.placements.push({ id: piece.id, x: free.x, y: free.y, length, width, rotated }); sheet.layout.usedArea += length * width; sheet.free.splice(index, 1, ...split(free, length + stock.kerf, width + stock.kerf, rule)); } for (const sheet of sheets) { // Free rectangles end in the kerf of the cut beyond them, or a kerf past the usable area. sheet.layout.offcuts = sheet.free .map((rect) => ({ x: rect.x, y: rect.y, length: rect.length - stock.kerf, width: rect.width - stock.kerf })) .filter((rect) => rect.length > kTolerance && rect.width > kTolerance); } return sheets.map((sheet) => sheet.layout);}
/** * Whether layout `a` is better than `b`: fewer sheets, then the least used on the last sheet, * which leaves the largest offcut, then the fewest pieces turned against the grain. * @param {SheetLayout[]} a * @param {SheetLayout[]} b */function isBetter(a, b) { if (a.length !== b.length) { return a.length < b.length; } const lastA = a.at(-1)?.usedArea ?? 0; const lastB = b.at(-1)?.usedArea ?? 0; if (Math.abs(lastA - lastB) > kTolerance) { return lastA < lastB; } /** @param {SheetLayout[]} layout */ const turned = (layout) => layout.reduce((sum, sheet) => sum + sheet.placements.filter((p) => p.rotated).length, 0); return turned(a) < turned(b);}
/** * Lays pieces out on as few sheets as it can, trying several orders and ways of splitting the * free space and keeping the best. Each sheet's layout can be cut with guillotine cuts. * @param {readonly Piece[]} pieces * @param {SheetStock} stock * @returns {SheetResult} */export function nestSheets(pieces, stock) { const usableLength = stock.length - 2 * stock.trim + kTolerance; const usableWidth = stock.width - 2 * stock.trim + kTolerance; /** @param {Piece} piece */ const fits = (piece) => (piece.length <= usableLength && piece.width <= usableWidth) || (piece.canRotate && piece.width <= usableLength && piece.length <= usableWidth); const fitting = pieces.filter(fits); const tooLarge = pieces.filter((piece) => !fits(piece)).map((piece) => piece.id); /** @type {SheetLayout[] | null} */ let best = null; for (const order of kOrders) { const sorted = [...fitting].sort((a, b) => order(a, b) || a.id.localeCompare(b.id)); for (const rule of kSplitRules) { const layout = pack(sorted, stock, rule); if (best === null || isBetter(layout, best)) { best = layout; } } } return { sheets: best ?? [], tooLarge };}
/** * A length to cut from a board. * @typedef {{ id: string, length: number }} BarPiece */
/** * One board of a layout: where each piece starts along it, and the length the pieces take. * @typedef {{ cuts: { id: string, x: number, length: number }[], usedLength: number }} BarLayout */
/** * Cuts lengths from boards of one stock length, longest pieces first, each into the board it * leaves the least over on. `trim` comes off both of the board's ends first, as for checked or * rough ends. * @param {readonly BarPiece[]} pieces * @param {{ length: number, kerf: number, trim: number }} stock * @returns {{ bars: BarLayout[], tooLarge: string[] }} */export function nestBars(pieces, stock) { const usable = stock.length - 2 * stock.trim + stock.kerf; /** @type {{ layout: BarLayout, left: number }[]} */ const bars = []; /** @type {string[]} */ const tooLarge = []; const sorted = [...pieces].sort((a, b) => b.length - a.length || a.id.localeCompare(b.id)); for (const piece of sorted) { const needed = piece.length + stock.kerf; if (needed > usable + kTolerance) { tooLarge.push(piece.id); continue; } let bar = null; for (const candidate of bars) { if (candidate.left + kTolerance >= needed && (bar === null || candidate.left < bar.left)) { bar = candidate; } } if (bar === null) { bar = { layout: { cuts: [], usedLength: 0 }, left: usable }; bars.push(bar); } bar.layout.cuts.push({ id: piece.id, x: stock.trim + (usable - bar.left), length: piece.length }); bar.layout.usedLength += piece.length; bar.left -= needed; } return { bars: bars.map((bar) => bar.layout), tooLarge };}// Writes the cut list out: a CSV file for spreadsheets, and a report to print, an HTML page with// the parts, what to buy and a cutting diagram of every sheet and board.
import { describeNeed, getTooLarge, kKindNames } from "./stock.js";
/** @typedef {import("./parts.js").Part} Part *//** @typedef {import("./stock.js").StockGroup} StockGroup */
/** Fill colors of parts in diagrams, picked by letter so a part looks the same everywhere. */const kPartColors = [ "#e8c48f", "#b9d4a3", "#a8c8e6", "#e6b3b3", "#d1bde3", "#f0dc96", "#a9ded6", "#e5c1a1",];
/** @param {string} letter */export function getPartColor(letter) { let hash = 0; for (const character of letter) { hash = hash * 31 + character.charCodeAt(0); } return kPartColors[hash % kPartColors.length];}
/** @param {Part["grain"]} grain */export function describeGrain(grain) { return grain === "any" ? "Either way" : grain === "width" ? "Along width" : "Along length";}
/** * Writes the parts as CSV rows, one per row of the cut list. * @param {readonly Part[]} parts * @param {modlumi.Model} model * @param {modlumi.PluginContext} context */export function writeCsv(parts, model, context) { const rows = [["Part", "Name", "Quantity", "Length", "Width", "Thickness", "Material", "Grain"]]; for (const part of parts) { rows.push([ part.letter, part.name, String(part.instances.length), model.formatLength(part.length), model.formatLength(part.width), model.formatLength(part.thickness), part.materialName, describeGrain(part.grain), ]); } // Excel reads accents and the × sign right only after a byte order mark. return "" + context.formatCsv(rows);}
/** @param {unknown} text */function escape(text) { return String(text).replace(/[&<>"]/g, (c) => ({ "&": "&", "<": "<", ">": ">", '"': """ })[c] ?? c);}
// Diagrams are drawn this many SVG units wide; the page scales them to fit.const kDiagramWidth = 760;
/** * Draws one sheet's layout as SVG, the sheet's length across the page. * @param {import("./nesting.js").SheetLayout} sheet * @param {StockGroup} group * @param {modlumi.Model} model * @param {ReadonlyMap<string, Part>} byLetter */function drawSheet(sheet, group, model, byLetter) { const { sheetLength, sheetWidth } = group.settings; const scale = kDiagramWidth / sheetLength; const height = sheetWidth * scale; const shapes = [`<rect class="stock" x="0" y="0" width="${kDiagramWidth}" height="${height}"/>`]; for (const offcut of sheet.offcuts) { shapes.push(`<rect class="offcut" x="${offcut.x * scale}" y="${offcut.y * scale}" width="${offcut.length * scale}" ` + `height="${offcut.width * scale}"/>`); } for (const placement of sheet.placements) { const [x, y, w, h] = [placement.x * scale, placement.y * scale, placement.length * scale, placement.width * scale]; const part = byLetter.get(placement.id); const size = part === undefined ? "" : `${model.formatLength(part.length)} × ${model.formatLength(part.width)}`; shapes.push(`<rect class="part" x="${x}" y="${y}" width="${w}" height="${h}" fill="${getPartColor(placement.id)}"/>`); const fontSize = Math.max(7, Math.min(16, h * 0.35, w * 0.3)); shapes.push(`<text x="${x + w / 2}" y="${y + h / 2}" font-size="${fontSize}">${escape(placement.id)}` + (placement.rotated ? " ↻" : "") + "</text>"); if (h > fontSize * 2.6 && w > size.length * fontSize * 0.42) { shapes.push(`<text class="size" x="${x + w / 2}" y="${y + h / 2 + fontSize * 1.1}" ` + `font-size="${fontSize * 0.6}">${escape(size)}</text>`); } } return `<svg viewBox="-1 -1 ${kDiagramWidth + 2} ${height + 2}" xmlns="http://www.w3.org/2000/svg">${shapes.join("")}</svg>`;}
/** * Draws one board's cuts as SVG. * @param {import("./nesting.js").BarLayout} bar * @param {StockGroup} group */function drawBar(bar, group) { const scale = kDiagramWidth / group.settings.boardLength; const height = 34; const shapes = [`<rect class="stock" x="0" y="0" width="${kDiagramWidth}" height="${height}"/>`]; for (const cut of bar.cuts) { const [x, w] = [cut.x * scale, cut.length * scale]; shapes.push(`<rect class="part" x="${x}" y="0" width="${w}" height="${height}" fill="${getPartColor(cut.id)}"/>`); shapes.push(`<text x="${x + w / 2}" y="${height / 2}" font-size="14">${escape(cut.id)}</text>`); } return `<svg viewBox="-1 -1 ${kDiagramWidth + 2} ${height + 2}" xmlns="http://www.w3.org/2000/svg">${shapes.join("")}</svg>`;}
const kStyle = ` body { font: 13px/1.4 system-ui, sans-serif; color: #222; margin: 24px; } h1 { font-size: 22px; margin: 0 0 4px; } h2 { font-size: 17px; margin: 28px 0 8px; border-bottom: 2px solid #333; padding-bottom: 2px; } h3 { font-size: 14px; margin: 16px 0 6px; } .muted { color: #666; } table { border-collapse: collapse; width: 100%; margin-bottom: 8px; } th, td { border-bottom: 1px solid #ddd; padding: 3px 8px; text-align: left; } th { background: #f2f2f2; } td.number, th.number { text-align: right; font-variant-numeric: tabular-nums; } .letter { display: inline-block; min-width: 22px; padding: 0 4px; border-radius: 3px; font-weight: 600; text-align: center; } .warning { color: #a33; font-weight: 600; } .diagram { break-inside: avoid; margin-bottom: 14px; } svg { width: 100%; height: auto; display: block; } svg .stock { fill: #fafafa; stroke: #555; stroke-width: 1; } svg .offcut { fill: url(#hatch); stroke: none; } svg .part { stroke: #333; stroke-width: 0.8; } svg text { text-anchor: middle; dominant-baseline: middle; font-family: system-ui, sans-serif; font-weight: 600; fill: #222; } svg text.size { font-weight: 400; } @media print { body { margin: 0; } h2 { break-after: avoid; } }`;
/** * Writes the report: what to buy, the parts of each stock and their cutting diagrams. * @param {readonly Part[]} parts * @param {readonly StockGroup[]} groups * @param {modlumi.Model} model * @param {{ title: string, date: string, kerf: number, trim: number }} details */export function writeReport(parts, groups, model, details) { const byLetter = new Map(parts.map((part) => [part.letter, part])); const count = parts.reduce((sum, part) => sum + part.instances.length, 0); const out = []; out.push(`<!doctype html><html><head><meta charset="utf-8"><title>${escape(details.title)}</title>`); out.push(`<style>${kStyle}</style></head><body>`); // One hatch pattern for every diagram's offcuts. out.push(`<svg width="0" height="0" style="position:absolute"><defs><pattern id="hatch" width="6" height="6" ` + `patternUnits="userSpaceOnUse" patternTransform="rotate(45)"><rect width="6" height="6" fill="#f4f4f4"/>` + `<line x1="0" y1="0" x2="0" y2="6" stroke="#ccc" stroke-width="2"/></pattern></defs></svg>`); out.push(`<h1>${escape(details.title)}</h1>`); out.push(`<div class="muted">${escape(details.date)} · ${count} parts · saw kerf ${escape(model.formatLength(details.kerf))}` + (details.trim > 0 ? ` · edge trim ${escape(model.formatLength(details.trim))}` : "") + "</div>");
out.push("<h2>Shopping list</h2><table><tr><th>Stock</th><th>Kind</th><th class=\"number\">Parts</th><th>Needed</th></tr>"); for (const group of groups) { out.push(`<tr><td>${escape(group.title)}</td><td>${kKindNames[group.kind]}</td><td class="number">${group.count}</td>` + `<td>${escape(describeNeed(group, model))}</td></tr>`); } out.push("</table>");
out.push("<h2>Parts</h2><table><tr><th>Part</th><th>Name</th><th class=\"number\">Qty</th><th class=\"number\">Length</th>" + "<th class=\"number\">Width</th><th class=\"number\">Thickness</th><th>Material</th><th>Grain</th></tr>"); for (const part of parts) { out.push(`<tr><td><span class="letter" style="background:${getPartColor(part.letter)}">${escape(part.letter)}</span></td>` + `<td>${escape(part.name)}</td><td class="number">${part.instances.length}</td>` + `<td class="number">${escape(model.formatLength(part.length))}</td>` + `<td class="number">${escape(model.formatLength(part.width))}</td>` + `<td class="number">${escape(model.formatLength(part.thickness))}</td>` + `<td>${escape(part.materialName)}</td><td>${describeGrain(part.grain)}</td></tr>`); } out.push("</table>");
for (const group of groups) { if (group.sheets === null && group.bars === null) { continue; } out.push(`<h2>${escape(group.title)}</h2><div class="muted">${escape(describeNeed(group, model))}</div>`); const tooLarge = getTooLarge(group); if (tooLarge.length > 0) { out.push(`<p class="warning">Too large for the stock: ${tooLarge.map(escape).join(", ")}</p>`); } (group.sheets?.sheets ?? []).forEach((sheet, index, sheets) => { out.push(`<div class="diagram"><h3>Sheet ${index + 1} of ${sheets.length}</h3>${drawSheet(sheet, group, model, byLetter)}</div>`); }); (group.bars?.bars ?? []).forEach((bar, index, bars) => { const cuts = bar.cuts.map((cut) => `${cut.id} ${model.formatLength(cut.length)}`).join(" · "); out.push(`<div class="diagram"><h3>Board ${index + 1} of ${bars.length}</h3>${drawBar(bar, group)}` + `<div class="muted">${escape(cuts)}</div></div>`); }); } out.push("</body></html>\n"); return out.join("\n");}Face Area
Section titled “Face Area”Totals the area of the selected faces, including faces inside selected groups and components, in the model’s units.
Select faces, then choose Total Face Area from the right-click menu or Extensions → Face Area. It changes nothing in the model.
Port of Eneroth Face Area Counter by Julia Christina Eneroth (MIT).
Source code
{ "id": "face-area", "name": "Face Area", "version": "1.0.0", "description": "Totals the area of the selected faces in the model's units.", "author": "Modlumi, after Eneroth Face Area Counter by Julia Christina Eneroth", "license": "MIT", "apiVersion": 1}// Totals the area of the selected faces, including faces inside selected groups and components.// A port of Eneroth Face Area Counter (MIT) by Julia Christina Eneroth:// https://github.com/Eneroth3/eneroth-face-area-counter//// Copyright (c) 2017 Julia Christina Eneroth//// Permission is hereby granted, free of charge, to any person obtaining a copy of this software// and associated documentation files (the "Software"), to deal in the Software without// restriction, including without limitation the rights to use, copy, modify, merge, publish,// distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the// Software is furnished to do so, subject to the following conditions://// The above copyright notice and this permission notice shall be included in all copies or// substantial portions of the Software.//// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING// BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// Adds the area of every face in elements, descending into containers. Areas are measured in// each face's own context, so scaled instances report their definition's area./** * @param {Iterable<modlumi.DrawingElement>} elements * @param {{ area: number, faces: number }} totals */function countFaces(elements, totals) { for (const element of elements) { if (element instanceof modlumi.Face) { totals.area += element.area; totals.faces += 1; } else if (element instanceof modlumi.Group) { countFaces(element.entities, totals); } else if (element instanceof modlumi.ComponentInstance) { countFaces(element.definition.entities, totals); } } return totals;}
/** @param {modlumi.PluginContext} context */export function activate(context) { context.registerCommand({ id: "total-area", title: "Total Face Area", tip: "Shows the total area of the selected faces, including those inside groups and components.", when: "selection", contextMenu: true, run() { const model = modlumi.activeModel; const { area, faces } = countFaces(model.selection, { area: 0, faces: 0 }); if (faces === 0) { context.showMessage("The selection has no faces."); return; } context.showMessage(`${model.formatArea(area)} in ${faces} ${faces === 1 ? "face" : "faces"}`); }, });}Levels
Section titled “Levels”Keeps a building’s floor levels in the model. In front, back and side views each level shows as a dashed line with its name and height, and points snap to level heights while you draw.
Choose Extensions → Levels → Levels to open the panel. Add Level adds the ground floor first, then a level 3 m above the highest one; pick a level to rename it or change its elevation. Front Elevation glides to a parallel front view. Select a level line and press Delete to remove it. Levels are saved with the model.
Source code
{ "id": "levels", "name": "Levels", "version": "1.0.0", "description": "Keeps a building's levels in the model, draws them as dashed lines in elevations and sections, and snaps the cursor to their heights.", "author": "Modlumi", "license": "MIT", "apiVersion": 1}// Levels: the floors of a building, each a name and an elevation, kept in the model so they// travel with it and come back with Undo. A panel edits them. In elevations and sections,// where the camera looks sideways, each level draws as a dashed line across the view with its// name and height, and the Select tool picks a line so Delete removes its level. The cursor// snaps to level heights in every tool that snaps, with the level's name in the tooltip.
/** @typedef {{ id: string, name: string, elevation: number }} Level */
const model = modlumi.activeModel;// The height of a new level above the highest one.const kStoreyHeight = 3;const kLineColor = { r: 0.15, g: 0.42, b: 0.82, a: 0.95 };// Level lines show while the view direction is within this of horizontal, about 30°.const kSidewaysSine = 0.5;// How far from the viewport's left edge a level's label sits, in pixels.const kLabelInset = 12;
/** * Formats an elevation with its sign, such as "+3.00 m" or "-0.50 m". * @param {number} elevation */function formatElevation(elevation) { return `${elevation < 0 ? "-" : "+"}${model.formatLength(Math.abs(elevation))}`;}
/** * Reads the levels kept in the model, lowest first; anything malformed is left out. * @param {unknown} data * @returns {Level[]} */function readLevels(data) { const stored = /** @type {{ levels?: unknown }} */ (data ?? {}).levels; if (!Array.isArray(stored)) { return []; } return stored .filter((level) => typeof level?.id === "string" && typeof level.name === "string" && Number.isFinite(level.elevation)) .map((level) => ({ id: level.id, name: level.name, elevation: level.elevation })) .sort((a, b) => a.elevation - b.elevation);}
/** * Returns the horizontal direction across the view, or null when the camera looks too far up * or down for levels to show as lines. * @param {modlumi.Camera} camera */function getAcross(camera) { const forward = camera.eye.vectorTo(camera.target).normalize(); if (Math.abs(forward.z) > kSidewaysSine) { return null; } return new modlumi.Vector3d(forward.y, -forward.x, 0).normalize();}
/** @param {modlumi.PluginContext} context */export function activate(context) { /** @type {Level[]} */ let levels = readLevels(context.documentData.get()); let showLines = Boolean(context.settings.get("showLines", true)); let snapToLevels = Boolean(context.settings.get("snap", true));
// Undo, redo and other documents change the levels too. const reread = () => { levels = readLevels(context.documentData.get()); }; context.onModelChanged(reread); context.onDocumentOpened(reread);
/** * Stores levels in the model in one undo step. * @param {string} name * @param {Level[]} next */ function save(name, next) { model.operation(name, () => context.documentData.set({ levels: next })); reread(); }
/** * Returns the ends of a level's line across the view, through the camera's target at the * level's height, long enough to cross the whole view, or null when the view isn't sideways. * @param {Level} level */ function getLine(level) { const camera = context.viewport.camera; const across = getAcross(camera); if (across === null) { return null; } const reach = Math.max(100, 20 * camera.eye.distance(camera.target)); const middle = new modlumi.Point3d(camera.target.x, camera.target.y, level.elevation); return /** @type {[modlumi.Point3d, modlumi.Point3d]} */ ([ middle.offset(across.scale(-reach)), middle.offset(across.scale(reach)), ]); }
// Level lines are annotations, so they draw over the model, across the whole viewport. context.addOverlay((view) => { if (!showLines) { return; } const { width, height } = context.viewport; for (const level of levels) { const line = getLine(level); if (line === null) { return; } const [start, end] = line.map((point) => context.viewport.project(point)); if (start === null || end === null || start[0] === end[0]) { continue; } /** @param {number} x */ const at = (x) => start[1] + ((x - start[0]) / (end[0] - start[0])) * (end[1] - start[1]); if (Math.max(at(0), at(width)) < 0 || Math.min(at(0), at(width)) > height) { continue; } view.lines([[0, at(0)], [width, at(width)]], { screen: true, color: kLineColor, width: 1.5, stipple: "dashed" }); view.text([kLabelInset, at(kLabelInset) - 18], `${level.name} ${formatElevation(level.elevation)}`, { screen: true, color: kLineColor }); } });
context.addSnapProvider(() => { if (!snapToLevels) { return []; } return levels.map((level) => ({ plane: { point: [0, 0, level.elevation], normal: [0, 0, 1] }, label: `${level.name} (${formatElevation(level.elevation)})`, })); });
context.addPickableItems({ items() { if (!showLines) { return []; } return levels.flatMap((level) => { const line = getLine(level); return line === null ? [] : [{ id: level.id, lines: [line] }]; }); }, // Runs inside the Delete operation, so the levels come back with Undo. onDelete(ids) { context.documentData.set({ levels: levels.filter((level) => !ids.includes(level.id)) }); }, });
/** Adds a level a storey above the highest one, or at the ground for the first, and returns its id. */ function addLevel() { const top = levels.at(-1); const used = new Set(levels.map((level) => level.id)); let number = levels.length + 1; while (used.has(`level-${number}`)) { number += 1; } const id = `level-${number}`; save("Add Level", [...levels, { id, name: top === undefined ? "Ground Floor" : `Level ${levels.length}`, elevation: top === undefined ? 0 : top.elevation + kStoreyHeight, }]); return id; }
/** Glides to a front elevation that fits the model and its levels, in a parallel projection. */ function lookFromTheFront() { const bounds = model.entities.bounds; const top = Math.max(bounds?.max.z ?? 0, levels.at(-1)?.elevation ?? 0); const bottom = Math.min(bounds?.min.z ?? 0, levels[0]?.elevation ?? 0); const center = new modlumi.Point3d(bounds?.center.x ?? 0, bounds?.center.y ?? 0, (top + bottom) / 2); // A parallel view shows as much as a perspective one does at the camera's distance. const fieldOfView = 45; const aspect = context.viewport.width / Math.max(1, context.viewport.height); // Room around the model, and for panels docked over the viewport's sides. const half = 0.9 * Math.max(top - bottom, (bounds?.width ?? 0) / aspect, 1); const distance = half / Math.tan((fieldOfView * Math.PI) / 360) + (bounds?.depth ?? 0); const front = new modlumi.Camera(center.offset([0, -distance, 0]), center, { perspective: false, fieldOfView }); context.viewport.setCamera(front, { duration: 0.6 }); }
// The level the panel edits, which follows the level lines the user selects. /** @type {string | null} */ let chosen = null; let lastSelected = "";
const panel = context.addPanel({ id: "levels", title: "Levels", draw(ui) { const selected = context.selectedItems; if (selected.join("\n") !== lastSelected) { lastSelected = selected.join("\n"); chosen = selected.at(-1) ?? chosen; } if (levels.length === 0) { ui.text("No levels yet. Add the ground floor to start.", { tone: "muted" }); } else { // Highest first, as on a section drawing. const rows = [...levels].reverse().map((level) => ({ id: level.id, cells: [level.name, formatElevation(level.elevation)], })); chosen = ui.table("Levels", ["Name", { title: "Elevation", numeric: true }], rows, chosen, { visibleRows: 6 }); } const level = levels.find((candidate) => candidate.id === chosen); if (level !== undefined) { const name = ui.textField("Name", level.name, { id: "name" }); const elevation = ui.length("Elevation", level.elevation, { id: "elevation" }); if (name !== level.name || elevation !== level.elevation) { save("Change Level", levels.map((other) => (other.id === level.id ? { ...other, name, elevation } : other))); } } ui.row(() => { if (ui.button("Add Level", { primary: true })) { chosen = addLevel(); } if (ui.button("Remove", { disabled: level === undefined, tip: "Removes the chosen level" })) { save("Remove Level", levels.filter((other) => other.id !== chosen)); chosen = null; } if (ui.button("Front Elevation", { tip: "Glides to a parallel view of the model from the front" })) { lookFromTheFront(); } }, { id: "actions" }); ui.separator("Display"); const show = ui.checkbox("Show level lines", showLines, { tip: "Dashed lines across elevations and sections, which the Select tool picks" }); if (show !== showLines) { showLines = show; context.settings.set("showLines", show); } const snap = ui.checkbox("Snap to levels", snapToLevels, { tip: "Points snap to level heights, seen from the side" }); if (snap !== snapToLevels) { snapToLevels = snap; context.settings.set("snap", snap); } if (getAcross(context.viewport.camera) === null) { ui.text("Level lines show in front, back and side views.", { tone: "muted" }); } }, });
context.registerCommand({ id: "open", title: "Levels", tip: "Opens the building levels.", run: () => panel.show(), });}Modify Solids
Section titled “Modify Solids”Unites, subtracts, trims and intersects solid groups and components in place, so the changed solid keeps its name, paint and position, and every copy of a component changes along.
Select the solids to cut or add with and choose Extensions → Modify Solids → Mark Modifiers. Then select the solids to change and choose Union, Subtract, Trim or Intersect. Subtract erases the modifiers unless Keep Modifiers is on; Trim always keeps them. With two or more solids selected and none marked, Union and Intersect combine them into the largest one.
Port of Eneroth Solid Tools by Julia Christina Eneroth (MIT).
Source code
{ "id": "modify-solids", "name": "Modify Solids", "version": "1.0.0", "description": "Unites, subtracts, trims and intersects solids in place, keeping their names, paint and placement.", "author": "Modlumi, after Eneroth Solid Tools by Julia Christina Eneroth", "license": "MIT", "apiVersion": 1}// Changes solid groups and components in place with other solids, so they keep their names,// paint, placement and plugin data, and every instance of a component changes along.// A port of Eneroth Solid Tools (MIT) by Julia Christina Eneroth:// https://github.com/Eneroth3/eneroth-solid-tools//// Copyright (c) 2017 Julia Christina Eneroth//// Permission is hereby granted, free of charge, to any person obtaining a copy of this software// and associated documentation files (the "Software"), to deal in the Software without// restriction, including without limitation the rights to use, copy, modify, merge, publish,// distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the// Software is furnished to do so, subject to the following conditions://// The above copyright notice and this permission notice shall be included in all copies or// substantial portions of the Software.//// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING// BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.//// Eneroth's tools ask for a target and then each modifier with the mouse. Here the user marks// modifiers once, then selects targets and runs a command. Without marked modifiers, Union and// Intersect combine the selection into its largest solid, like Eneroth's instant mode.
/** @typedef {modlumi.Group | modlumi.ComponentInstance} Solid */
/** @type {Solid[]} */let modifiers = [];
/** * @param {unknown} element * @returns {element is Solid} */function isSolid(element) { if (!(element instanceof modlumi.Group || element instanceof modlumi.ComponentInstance)) { return false; } if (solidity === undefined) { return element.isSolid; } let solid = solidity.get(element); if (solid === undefined) { solid = element.isSolid; solidity.set(element, solid); } return solid;}
/** * Whether entities are solid, kept while the commands' isEnabled checks run: they only read, * and each looks at the whole selection, which can hold thousands of entities. * @type {WeakMap<Solid, boolean> | undefined} */let solidity;
/** @type {Solid[] | undefined} */let selectedSolids;
/** * Runs a read-only check with solidity kept until the current run's code finishes. * @param {() => boolean} check */function withSolidity(check) { if (solidity === undefined) { solidity = new WeakMap(); Promise.resolve().then(() => { solidity = undefined; selectedSolids = undefined; }); } return check();}
// Returns the marked modifiers that still exist and belong to the context being edited.function liveModifiers() { if (modifiers.length === 0) { return modifiers; } const entities = modlumi.activeModel.activeEntities.toArray(); modifiers = modifiers.filter((modifier) => modifier.isValid && entities.includes(modifier)); return modifiers;}
// Reuse the selection snapshot across the commands' read-only checks in this run.function selectedSolidEntities() { if (solidity === undefined) { return modlumi.activeModel.selection.toArray().filter(isSolid); } return selectedSolids ??= modlumi.activeModel.selection.toArray().filter(isSolid);}
// Returns the selected solids that aren't marked as modifiers.function selectedTargets() { const marked = liveModifiers(); return selectedSolidEntities().filter((solid) => !marked.includes(solid));}
/** * @param {number} count * @param {string} noun */function plural(count, noun) { return `${count} ${noun}${count === 1 ? "" : "s"}`;}
/** * Changes each target in place with each modifier in turn, as one undo step. * @param {"union" | "subtract" | "intersect"} operation * @param {Solid[]} targets * @param {Solid[]} cutters */function modify(operation, targets, cutters) { const entities = modlumi.activeModel.activeEntities; for (const target of targets) { for (const cutter of cutters) { entities.modifySolid(target, cutter, operation); } }}
/** @param {modlumi.PluginContext} context */export function activate(context) { const keepModifiers = () => context.settings.get("keepModifiers", false);
context.registerCommand({ id: "mark-modifiers", title: "Mark Modifiers", tip: "Remembers the selected solids as the modifiers the other commands change targets with.", when: "selection", contextMenu: true, isEnabled: () => withSolidity(() => selectedSolidEntities().length > 0), run() { modifiers = modlumi.activeModel.selection.toArray().filter(isSolid); context.showMessage(`Marked ${plural(modifiers.length, "modifier")}. Now select the targets.`); }, });
context.registerCommand({ id: "keep-modifiers", title: "Keep Modifiers", tip: "Keeps the modifiers after Subtract instead of erasing them.", isChecked: keepModifiers, run() { context.settings.set("keepModifiers", !keepModifiers()); }, });
/** * Registers a command that changes the selected targets with the marked modifiers. * @param {string} id * @param {string} title * @param {string} tip * @param {"union" | "subtract" | "intersect"} operation * @param {{ erase: () => boolean, instant: boolean }} options */ const registerTool = (id, title, tip, operation, { erase, instant }) => context.registerCommand({ id, title, tip, when: "selection", contextMenu: true, isEnabled() { return withSolidity(() => { const targets = selectedTargets(); return targets.length > 0 && (liveModifiers().length > 0 || (instant && targets.length > 1)); }); }, run() { const model = modlumi.activeModel; let targets = selectedTargets(); let cutters = liveModifiers(); if (cutters.length === 0 && instant) { // The largest solid takes in the others. targets.sort((a, b) => (b.volume ?? 0) - (a.volume ?? 0)); cutters = targets.slice(1); targets = targets.slice(0, 1); } if (targets.length === 0 || cutters.length === 0) { context.showMessage("Mark modifiers first, then select the solids to change."); return; } const erased = erase(); model.operation(title, () => { modify(operation, targets, cutters); if (erased) { for (const cutter of cutters) { model.activeEntities.erase(cutter); } } }); if (erased) { modifiers = []; } model.selection.clear(); for (const target of targets) { model.selection.add(target); } context.showMessage(`${title}: changed ${plural(targets.length, "solid")} with ${plural(cutters.length, "modifier")}.`); }, });
registerTool("union", "Union", "Adds the modifiers to the selected solids, which keep their names and paint.", "union", { erase: () => false, instant: true, }); registerTool( "subtract", "Subtract", "Cuts the modifiers out of the selected solids and erases them, unless Keep Modifiers is on.", "subtract", { erase: () => !keepModifiers(), instant: false }, ); registerTool("trim", "Trim", "Cuts the modifiers out of the selected solids and keeps them.", "subtract", { erase: () => false, instant: false, }); registerTool( "intersect", "Intersect", "Keeps only the parts of the selected solids that lie inside the modifiers.", "intersect", { erase: () => false, instant: true }, );}Exports Wavefront OBJ models. Modlumi imports OBJ files itself, with File → Import….
Use File → Export OBJ Model…. OBJ files have no units, so Extensions → OBJ → OBJ Options… sets the units to write and whether Y is up, as most other tools expect. Export writes the faces of each group and component as an object of its own, and leaves out hidden items and materials.
Source code
{ "id": "obj", "name": "OBJ", "version": "1.1.0", "description": "Exports Wavefront OBJ models, with each group and component as an object.", "author": "Modlumi", "license": "MIT", "apiVersion": 1}// Wavefront OBJ export. OBJ files list vertices ("v"), and faces ("f") through them, grouped// into objects ("o"); they have no units, and most tools read them with Y up. Export writes the// faces of each group and component as an object of its own. Modlumi imports OBJ files itself,// through File > Import.
/** * @typedef {"Millimeters" | "Centimeters" | "Meters" | "Inches" | "Feet"} Unit * @typedef {{ units: Unit, yUp: boolean }} Options */
const model = modlumi.activeModel;/** Meters per unit. @type {Record<Unit, number>} */const kMeters = { Millimeters: 0.001, Centimeters: 0.01, Meters: 1, Inches: 0.0254, Feet: 0.3048 };/** @type {Unit[]} */const kUnits = ["Meters", "Centimeters", "Millimeters", "Inches", "Feet"];/** @type {Options} */const kDefaults = { units: "Meters", yUp: true };
/** * Returns a face's outer boundary as positions, counterclockwise around its normal. * @param {modlumi.Face} face */function getOutline(face) { const points = face.outerLoop.edgeUses.map((use) => use.start.position); let normal = new modlumi.Vector3d(0, 0, 0); for (let index = 0; index < points.length; ++index) { const a = points[index]; const b = points[(index + 1) % points.length]; normal = normal.add(new modlumi.Vector3d((a.y - b.y) * (a.z + b.z), (a.z - b.z) * (a.x + b.x), (a.x - b.x) * (a.y + b.y))); } return face.normal !== null && normal.dot(face.normal) < 0 ? points.reverse() : points;}
/** * Writes the faces of elements and the groups and components in them as OBJ text, in world * coordinates, with an object per group and component. Faces with holes are written as their * triangles. * @param {Iterable<modlumi.DrawingElement>} elements * @param {Options} options */export function writeObj(elements, options) { const scale = 1 / (kMeters[options.units] ?? 1); const lines = ["# Exported by Modlumi"]; /** @type {Map<string, number>} */ const indices = new Map(); /** @param {modlumi.Point3d} point */ const vertex = (point) => { const [x, y, z] = [point.x * scale, point.y * scale, point.z * scale]; const written = (options.yUp ? [x, z, -y] : [x, y, z]).map((value) => (Object.is(value, -0) ? 0 : value)); const key = written.join(" "); let index = indices.get(key); if (index === undefined) { index = indices.size + 1; indices.set(key, index); lines.push(`v ${key}`); } return index; }; let faces = 0; /** * @param {Iterable<modlumi.DrawingElement>} list * @param {modlumi.Transformation} transformation * @param {string} name */ const visit = (list, transformation, name) => { const [x, y, z] = [[1, 0, 0], [0, 1, 0], [0, 0, 1]].map((axis) => new modlumi.Vector3d(...axis).transform(transformation)); const mirrored = x.dot(y.cross(z)) < 0; const shown = Array.from(list).filter((element) => !element.hidden); // All of this object's faces first, so none follow a child's "o" record. const own = shown.filter((element) => element instanceof modlumi.Face); if (own.length > 0) { lines.push(`o ${name}`); } for (const face of /** @type {modlumi.Face[]} */ (own)) { const polygons = face.loops.length === 1 ? [getOutline(face)] : face.triangles; for (const polygon of polygons) { const placed = polygon.map((point) => vertex(point.transform(transformation))); lines.push(`f ${(mirrored ? placed.reverse() : placed).join(" ")}`); faces += 1; } } for (const element of shown) { if (element instanceof modlumi.Group) { visit(element.entities, transformation.multiply(element.transformation), element.name || "Group"); } else if (element instanceof modlumi.ComponentInstance) { visit(element.definition.entities, transformation.multiply(element.transformation), element.name || element.definition.name); } } }; visit(elements, model.activeTransformation, "Model"); return { text: lines.join("\n") + "\n", faces };}
/** @param {modlumi.PluginContext} context */export function activate(context) { /** @returns {Options} */ const getOptions = () => ({ ...kDefaults, ...context.settings.get("options", {}) });
context.registerExporter({ id: "obj", name: "OBJ Model", extensions: ["obj"], export(file) { const { text, faces } = writeObj(model.activeEntities, getOptions()); file.writeText(text); context.showMessage(`Exported ${faces} faces to ${file.name}`); }, });
context.registerCommand({ id: "options", title: "OBJ Options...", tip: "Sets the units and up axis OBJ export uses.", run() { const options = getOptions(); context.showDialog({ id: "options", title: "OBJ Options", modal: true, draw(ui) { options.units = /** @type {Unit} */ (ui.dropdown("Units", options.units, kUnits, { tip: "OBJ files have no units; this says which to write" })); options.yUp = ui.checkbox("Y is up", options.yUp, { tip: "As most other tools write OBJ files" }); ui.spacing(); ui.row(() => { if (ui.button("OK", { primary: true })) { context.settings.set("options", options); ui.close(); } if (ui.button("Cancel")) { ui.close(); } }); }, }); }, });}Rail Sweep
Section titled “Rail Sweep”Sweeps a surface between a start and an end profile along two rails. The profile blends from one shape to the other, and the rails set where each section sits, how it turns and how wide it is, so a square can become a circle along two curves.
Choose Extensions → Rail Sweep → Rail Sweep, then click the start profile, the end profile and the two rails. A profile is a face, a curve such as a circle or arc, or a chain of edges; a rail is a curve or chain of edges that runs from one profile to the other. Click the start profile again, or press Enter, to sweep a single profile. A preview shows the sections; press Enter, double-click, or click Sweep to add the surface as a group.
Each rail must start on the start profile and end on the end profile, at any
point of them. The profiles can have different numbers of points: both are resampled at each other’s corners, so the rails stay
on the surface. A sweep of closed profiles is capped at its ends and becomes a
solid; turn off Cap ends to leave it open. By default there’s a section at
each point of the rails; type a count such as 24s for evenly spaced sections
instead, or 0 to go back. Scale height with width scales each section
evenly as the rails move apart; turn it off to change only its width. A face’s
holes aren’t swept. To use edges that connect to other edges as a profile,
select them first, then click one of them.
Source code
{ "id": "rail-sweep", "name": "Rail Sweep", "version": "1.0.0", "description": "Sweeps a surface between a start and an end profile along two rails, which set where each section sits, how it turns and how wide it is, with a live preview.", "author": "Modlumi", "license": "MIT", "apiVersion": 1}// Sweeps a surface between a start and an end profile along two rails. Each rail runs from a// point on the start profile to a point on the end profile; at every section along the way the// profile blends from the start shape to the end shape, and the two rail points set where the// section sits, how it turns and how wide it is. Closed profiles get end caps, so the result// can be a solid. A preview shows the surface before it is added, as a group, in one undo step.//// Profiles are faces, curves or chains of edges, and may have different numbers of points:// both are resampled at each other's corners, in parts split where the rails touch them, so// the rails stay on the surface. Pick the start profile twice to sweep a single profile.
/** * @typedef {{ face: modlumi.Face } | { edges: modlumi.Edge[] }} Pick * A profile or rail: a face's outer loop, or a chain of edges. * @typedef {{ points: modlumi.Point3d[], closed: boolean }} Polyline * Points along a profile or rail, the first not repeated at the end of a closed one. * @typedef {{ origin: modlumi.Point3d, xAxis: modlumi.Vector3d, up: modlumi.Vector3d, * along: modlumi.Vector3d, width: number }} Frame * A section's placement: from rail 1's point `origin` toward rail 2's point along `xAxis`, * `width` away, with `along` the rails' direction square to `xAxis`, and `up` the third axis. * @typedef {{ sections: modlumi.Point3d[][], closed: boolean }} Sweep * The swept surface: corresponding points of each section, from the start profile to the end. */
const model = modlumi.activeModel;const { Vector3d } = modlumi;const kEpsilon = 1e-9;/** Edges between faces that meet at less than this many radians are softened. */const kSmoothAngle = (30 * Math.PI) / 180;/** @type {modlumi.Color[]} The colors of the start profile, the end profile and the rails. */const kPickColors = [ { r: 0.1, g: 0.35, b: 0.9, a: 1 }, { r: 0.6, g: 0.1, b: 0.8, a: 1 }, { r: 0, g: 0.6, b: 0, a: 1 }, { r: 0, g: 0.6, b: 0, a: 1 },];/** @type {modlumi.Color} */const kHoverColor = { r: 0.95, g: 0.6, b: 0, a: 1 };/** @type {modlumi.Color} */const kPreviewColor = { r: 0.5, g: 0, b: 0, a: 1 };const kSlotNames = ["the start profile", "the end profile", "rail 1", "rail 2"];const kSlotLabels = ["Start", "End", "Rail 1", "Rail 2"];
/** @param {Pick} pick */function getEdges(pick) { return "face" in pick ? [...pick.face.outerLoop.edges] : pick.edges;}
/** @param {Pick} pick */function isValidPick(pick) { return "face" in pick ? pick.face.isValid : pick.edges.every((edge) => edge.isValid);}
/** * The chain of edges through an edge: the selected edges around it when it is selected with * others, its curve when it has one, or else the edges that continue it one to one at each * vertex. Edges in `excluded`, such as those of profiles already picked, end the chain at * the vertices they touch, so a rail stops at a profile even where another edge continues it. * @param {modlumi.Edge} edge * @param {Set<modlumi.Edge>} excluded * @returns {modlumi.Edge[]} */function getChain(edge, excluded) { const selected = model.selection.has(edge) ? new Set(/** @type {modlumi.Edge[]} */ (model.selection.toArray().filter((entity) => entity.type === "Edge"))) : null; /** @type {Set<modlumi.Edge> | null} */ const allowed = selected !== null && selected.size > 1 ? selected : edge.curve === null ? null : new Set(edge.curve.edges); /** @param {modlumi.Vertex} vertex @param {modlumi.Edge} from */ const next = (vertex, from) => { if (vertex.edges.some((other) => excluded.has(other))) { return null; } const others = vertex.edges.filter((other) => other !== from && !excluded.has(other) && (allowed === null ? other.curve === null : allowed.has(other))); return others.length === 1 && (allowed !== null || vertex.edges.length === 2) ? others[0] : null; }; const chain = [edge]; const visited = new Set(chain); /** @param {modlumi.Vertex} vertex @param {boolean} forward */ const extend = (vertex, forward) => { for (let from = edge, at = vertex; ;) { const other = next(at, from); if (other === null || visited.has(other)) { return; } visited.add(other); if (forward) { chain.push(other); } else { chain.unshift(other); } at = other.start === at ? other.end : other.start; from = other; } }; extend(edge.end, true); extend(edge.start, false); return chain;}
/** * The points along a pick, in order. * @param {Pick} pick * @returns {Polyline} */function getPolyline(pick) { if ("face" in pick) { return { points: pick.face.outerLoop.edgeUses.map((use) => use.start.position), closed: true }; } const { edges } = pick; if (edges.length === 1) { return { points: [edges[0].start.position, edges[0].end.position], closed: false }; } // The first vertex is the one the first edge doesn't share with the second. const second = edges[1]; let at = edges[0].start === second.start || edges[0].start === second.end ? edges[0].end : edges[0].start; const vertices = [at]; for (const edge of edges) { at = edge.start === at ? edge.end : edge.start; vertices.push(at); } const closed = vertices.length > 3 && vertices[0] === vertices[vertices.length - 1]; if (closed) { vertices.pop(); } return { points: vertices.map((vertex) => vertex.position), closed };}
/** * The distances along a polyline to each point, and to the end of a closed one's last segment. * @param {Polyline} line */function getStations(line) { const { points, closed } = line; const stations = [0]; const count = closed ? points.length : points.length - 1; for (let index = 0; index < count; ++index) { stations.push(stations[index] + points[index].distance(points[(index + 1) % points.length])); } return stations;}
/** * The point a distance along a polyline, wrapping around a closed one. * @param {Polyline} line * @param {number[]} stations * @param {number} at */function getPointAt(line, stations, at) { const { points, closed } = line; const length = stations[stations.length - 1]; let s = closed && length > 0 ? ((at % length) + length) % length : Math.min(length, Math.max(0, at)); let index = 0; while (index + 2 < stations.length && stations[index + 1] < s) { ++index; } const span = stations[index + 1] - stations[index]; const t = span > 0 ? (s - stations[index]) / span : 0; const a = points[index]; const b = points[(index + 1) % points.length]; return a.offset(a.vectorTo(b).scale(t));}
/** * The point of a polyline closest to a point, and how far along the polyline it lies. * @param {Polyline} line * @param {number[]} stations * @param {modlumi.Point3d} point */function getNearest(line, stations, point) { const { points } = line; let best = { at: 0, distance: points[0].distance(point) }; for (let index = 0; index + 1 < stations.length; ++index) { const a = points[index]; const segment = a.vectorTo(points[(index + 1) % points.length]); const lengthSquared = segment.dot(segment); const t = lengthSquared > 0 ? Math.min(1, Math.max(0, a.vectorTo(point).dot(segment) / lengthSquared)) : 0; const distance = a.offset(segment.scale(t)).distance(point); if (distance < best.distance) { best = { at: stations[index] + t * (stations[index + 1] - stations[index]), distance }; } } return best;}
/** @param {Polyline} line */function reverse(line) { const points = [...line.points].reverse(); return { points, closed: line.closed };}
/** @param {modlumi.Vector3d} vector */function getAnyPerpendicular(vector) { const other = Math.abs(vector.x) < 0.9 ? new Vector3d(1, 0, 0) : new Vector3d(0, 1, 0); return vector.cross(other).normalize();}
/** * A rail's direction at a fraction of its length: its segment's, or the average of the two * segments that meet there. * @param {Polyline} rail * @param {number[]} stations * @param {number} t */function getRailDirection(rail, stations, t) { const { points } = rail; const length = stations[stations.length - 1]; const s = t * length; /** @param {number} index */ const direction = (index) => points[index].vectorTo(points[index + 1]); let index = 0; while (index + 2 < stations.length && stations[index + 1] < s - kEpsilon * length) { ++index; } let result = direction(index); if (result.length > 0) { result = result.normalize(); } /** @param {number} other */ const average = (other) => { const vector = direction(other); return vector.length > 0 ? result.add(vector.normalize()) : result; }; if (Math.abs(s - stations[index]) <= kEpsilon * length && index > 0) { result = average(index - 1); } else if (Math.abs(s - stations[index + 1]) <= kEpsilon * length && index + 2 < stations.length) { result = average(index + 1); } return result;}
/** * The sorted fractions in [0, 1] from several lists, with near repeats removed. * @param {number[][]} lists */function mergeFractions(lists) { const sorted = lists.flat().sort((a, b) => a - b); /** @type {number[]} */ const merged = []; for (const value of sorted) { if (merged.length === 0 || value - merged[merged.length - 1] > 1e-7) { merged.push(value); } } return merged;}
/** * Sweeps the picked profiles along the picked rails. * @param {Pick} startPick * @param {Pick} endPick * @param {Pick} rail1Pick * @param {Pick} rail2Pick * @param {{ sections: number, scaleHeight: boolean }} options * `sections`: how many sections to divide the rails into, or 0 for one at each rail point. * `scaleHeight`: whether a section's height scales with its width, or stays as it is. * @returns {Sweep} */function sweep(startPick, endPick, rail1Pick, rail2Pick, options) { const single = startPick === endPick; const profiles = [getPolyline(startPick), getPolyline(endPick)]; if (profiles[0].closed !== profiles[1].closed) { throw new Error("Both profiles must be closed, or both open."); } const profileStations = profiles.map(getStations); const rails = [getPolyline(rail1Pick), getPolyline(rail2Pick)].map((rail) => { if (rail.closed) { throw new Error("A rail must be open, from one profile to the other."); } // Each rail starts at the start profile. const stations = getStations(rail); const first = rail.points[0]; const last = rail.points[rail.points.length - 1]; const toStart = (/** @type {modlumi.Point3d} */ point) => getNearest(profiles[0], profileStations[0], point).distance; const toEnd = (/** @type {modlumi.Point3d} */ point) => getNearest(profiles[1], profileStations[1], point).distance; const flip = single ? toStart(last) < toStart(first) : toStart(first) + toEnd(last) > toStart(last) + toEnd(first); const oriented = flip ? reverse(rail) : rail; return { line: oriented, stations: flip ? getStations(oriented) : stations }; }); if (rails.some(({ stations }) => stations[stations.length - 1] <= kEpsilon)) { throw new Error("A rail has no length."); }
/** @param {number} t @param {Frame | null} previous @returns {Frame} */ const getFrame = (t, previous) => { const [a, b] = rails.map(({ line, stations }) => getPointAt(line, stations, t * stations[stations.length - 1])); const across = a.vectorTo(b); const width = across.length; const xAxis = width > kEpsilon ? across.normalize() : previous?.xAxis ?? new Vector3d(1, 0, 0); const direction = getRailDirection(rails[0].line, rails[0].stations, t) .add(getRailDirection(rails[1].line, rails[1].stations, t)); const square = direction.subtract(xAxis.scale(direction.dot(xAxis))); let along = square.length > kEpsilon ? square : previous?.along ?? getAnyPerpendicular(xAxis); along = along.subtract(xAxis.scale(along.dot(xAxis))); along = along.length > kEpsilon ? along.normalize() : getAnyPerpendicular(xAxis); return { origin: a, xAxis, up: along.cross(xAxis), along, width }; };
// The section fractions along the rails. const fractions = options.sections > 0 ? Array.from({ length: options.sections + 1 }, (_, index) => index / options.sections) : mergeFractions(rails.map(({ stations }) => stations.map((s) => s / stations[stations.length - 1]))); /** @type {Frame[]} */ const frames = []; for (const t of fractions) { frames.push(getFrame(t, frames.length === 0 ? null : frames[frames.length - 1])); } const ends = [frames[0], frames[frames.length - 1]]; /** @param {Frame} frame */ const heightScale = (frame) => options.scaleHeight ? frame.width : 1; /** * A point in a frame's coordinates: across in widths, up and along in widths or meters. * @param {Frame} frame @param {modlumi.Point3d} point */ const toLocal = (frame, point) => { const offset = frame.origin.vectorTo(point); const height = heightScale(frame); return [offset.dot(frame.xAxis) / Math.max(frame.width, kEpsilon), offset.dot(frame.up) / Math.max(height, kEpsilon), offset.dot(frame.along) / Math.max(height, kEpsilon)]; }; if (ends.some((frame) => frame.width <= kEpsilon)) { throw new Error("The rails must touch each profile at two different points."); }
// Each profile runs the same way around the rails, and is cut into parts where they touch it. const parts = (single ? [profiles[0]] : profiles).map((original, index) => { const frame = ends[index]; let line = original; if (line.closed) { let area = 0; const local = line.points.map((point) => toLocal(frame, point)); local.forEach(([u, v], corner) => { const [nextU, nextV] = local[(corner + 1) % local.length]; area += u * nextV - nextU * v; }); if (area < 0) { line = reverse(line); } } let stations = getStations(line); const length = stations[stations.length - 1]; // Each rail must touch the profile, to within a hundredth of the section's width. const anchor = (/** @type {number} */ rail) => { const { points } = rails[rail].line; const nearest = getNearest(line, stations, points[index === 0 ? 0 : points.length - 1]); if (nearest.distance > Math.max(1e-6, frame.width / 100)) { throw new Error(`Rail ${rail + 1} doesn't touch the ${index === 0 ? "start" : "end"} profile.`); } return nearest.at; }; let first = anchor(0); let second = anchor(1); if (!line.closed && first > second) { line = reverse(line); stations = getStations(line); first = length - first; second = length - second; } // The parts' starts and ends, as distances along the profile, and the distances to each // point of the profile within them. const bounds = line.closed ? [[first, first + ((second - first + length) % length)], [first + ((second - first + length) % length), first + length]] : [[0, first], [first, second], [second, length]]; return bounds.map(([start, end]) => { const inner = []; for (const station of stations) { for (const shifted of line.closed ? [station, station + length] : [station]) { if (shifted > start + kEpsilon && shifted < end - kEpsilon) { inner.push((shifted - start) / (end - start)); } } } return { line, stations, start, end, inner }; }); });
if (single) { parts.push(parts[0]); }
// Both profiles are sampled at the same fractions of each part: their own points and the other's. /** @type {modlumi.Point3d[][]} */ const samples = [[], []]; const partCount = parts[0].length; for (let part = 0; part < partCount; ++part) { const both = [parts[0][part], parts[1][part]]; const empty = both.every(({ start, end }) => end - start <= kEpsilon); const at = empty ? [0] : mergeFractions([[0, 1], ...both.map(({ inner }) => inner)]); // A part starts where the one before it ends, and a closed profile's last part ends at its start. const from = part === 0 ? 0 : 1; const to = parts[0][0].line.closed && part === partCount - 1 ? at.length - 1 : at.length; both.forEach(({ line, stations, start, end }, index) => { for (let sample = from; sample < to; ++sample) { samples[index].push(getPointAt(line, stations, start + at[sample] * (end - start))); } }); } // A single profile keeps its shape all the way, carried along by the rails. const local = samples.map((points, index) => points.map((point) => toLocal(ends[single ? 0 : index], point))); const sections = frames.map((frame, step) => { const t = fractions[step]; const height = heightScale(frame); return local[0].map((start, index) => { const end = local[1][index]; const [u, v, w] = start.map((value, axis) => value + (end[axis] - value) * t); return frame.origin.offset(frame.xAxis.scale(u * frame.width).add(frame.up.scale(v * height)) .add(frame.along.scale(w * height))); }); }); return { sections, closed: profiles[0].closed };}
/** * The faces of a swept surface, as polygons of points, capped at its ends when `cap` is set * and the profiles are closed. A capped surface's faces face outward. * @param {Sweep} swept * @param {boolean} cap */function getPolygons(swept, cap) { const { sections, closed } = swept; /** @type {modlumi.Point3d[][]} */ const polygons = []; /** @param {modlumi.Point3d[]} corners */ const add = (corners) => { /** @type {modlumi.Point3d[]} */ const distinct = []; for (const corner of corners) { if (distinct.every((other) => other.distance(corner) > kEpsilon)) { distinct.push(corner); } } if (distinct.length >= 3) { polygons.push(distinct); } }; const count = sections[0].length; for (let step = 0; step + 1 < sections.length; ++step) { const [a, b] = [sections[step], sections[step + 1]]; for (let index = 0; index < (closed ? count : count - 1); ++index) { const next = (index + 1) % count; add([a[index], a[next], b[next], b[index]]); } } if (closed && cap) { add([...sections[0]].reverse()); add(sections[sections.length - 1]); // Six times the signed volume, from fans of triangles; faces turn outward when it's positive. let volume = 0; for (const polygon of polygons) { const [origin] = polygon; for (let index = 1; index + 1 < polygon.length; ++index) { const a = new Vector3d(...origin); volume += a.dot(new Vector3d(...polygon[index]).cross(new Vector3d(...polygon[index + 1]))); } } if (volume < 0) { polygons.forEach((polygon) => polygon.reverse()); } } return polygons;}
/** @param {modlumi.PluginContext} context */export function activate(context) { /** @type {(Pick | null)[]} The start profile, the end profile and the two rails. */ let picks = [null, null, null, null]; /** @type {Pick | null} What a click would pick. */ let hover = null; /** @type {Sweep | string | null} The swept surface, or why it can't be swept. */ let cached = null; /** Whether the next release is the end of a double-click that applied the sweep. */ let releaseApplied = false; let sections = Number(context.settings.get("sections", 0)); let scaleHeight = Boolean(context.settings.get("scaleHeight", true)); let cap = Boolean(context.settings.get("cap", true)); let smooth = Boolean(context.settings.get("smooth", true));
function getSlot() { return picks.indexOf(null); }
function getSweep() { if (getSlot() !== -1) { return null; } if (cached === null) { const [start, end, rail1, rail2] = /** @type {Pick[]} */ (picks); try { cached = sweep(start, end, rail1, rail2, { sections, scaleHeight }); } catch (error) { cached = error instanceof Error ? error.message : String(error); } } return cached; }
function showState() { const slot = getSlot(); const swept = getSweep(); tool.status = slot === 1 ? "Click the end profile, or click the start profile again or press Enter to sweep one profile." : slot !== -1 ? `Click ${kSlotNames[slot]}: ${slot === 0 ? "a face, curve or chain of edges" : "a curve or chain of edges"}.` : typeof swept === "string" ? `${swept} Press Escape to start over.` : "Press Enter or double-click to add the sweep. Type a number of sections like 24s."; tool.measurement = { label: "Sections", value: swept === null || typeof swept === "string" ? "" : String(swept.sections.length - 1), }; }
function reset() { picks = [null, null, null, null]; hover = null; cached = null; showState(); }
/** * What clicking the entity under the pointer would pick for the next slot, or null. * @param {modlumi.ToolEvent} event * @returns {Pick | null} */ function getCandidate(event) { const slot = getSlot(); const picked = event.picked; if (slot === -1 || picked === null || picked.path.length !== model.activePath.length) { return null; } const { entity } = picked; const profile = slot < 2; if (entity.type === "Face") { if (!profile) { return null; } return slot === 1 && picks[0] !== null && "face" in picks[0] && picks[0].face === entity ? picks[0] : { face: /** @type {modlumi.Face} */ (entity) }; } if (entity.type !== "Edge") { return null; } const edge = /** @type {modlumi.Edge} */ (entity); const owner = picks.find((pick) => pick !== null && getEdges(pick).includes(edge)); if (owner !== undefined) { // Picking the start profile again sweeps it alone. return slot === 1 && owner === picks[0] ? owner : null; } /** @type {Set<modlumi.Edge>} */ const excluded = new Set(picks.flatMap((pick) => pick === null ? [] : getEdges(pick))); return { edges: getChain(edge, excluded) }; }
function commit() { const swept = getSweep(); if (swept === null || typeof swept === "string") { return; } const mesh = new modlumi.MeshBuilder(); for (const polygon of getPolygons(swept, cap)) { mesh.addPolygon(polygon); } let group; try { group = model.operation("Rail Sweep", () => { const added = model.activeEntities.addGroup(); added.entities.addMesh(mesh, { smoothAngle: smooth ? kSmoothAngle : 0 }); return added; }); } catch (error) { context.showMessage(`Can't sweep: ${error instanceof Error ? error.message : error}`); return; } reset(); model.selection.clear(); model.selection.add(group); }
/** @param {string} text */ function applyValue(text) { const match = /^(\d+)\s*s?$/i.exec(text.trim()); if (match === null) { return false; } sections = Math.min(1000, Number(match[1])); context.settings.set("sections", sections); cached = null; return true; }
const tool = context.registerTool({ id: "rail-sweep", title: "Rail Sweep", tip: "Sweeps between two profiles along two rails.", snap: false, activate: reset, deactivate() { reset(); releaseApplied = false; }, onMouseMove(event) { hover = getCandidate(event); }, onMouseUp(event) { if (releaseApplied) { releaseApplied = false; return; } const candidate = getCandidate(event); if (candidate !== null) { picks[getSlot()] = candidate; hover = null; cached = null; } showState(); }, onDoubleClick() { // The double-click's own release follows; it ends the gesture rather than picking again. releaseApplied = getSlot() === -1; commit(); }, onKeyDown(event) { if (event.key !== "enter") { return; } if (getSlot() === 1) { picks[1] = picks[0]; cached = null; showState(); } else { commit(); } }, onValue(text) { if (!applyValue(text)) { context.showMessage(`Can't use "${text}" as a number of sections`); } showState(); }, onCancel(reason) { if (reason === "escape") { reset(); } }, options(ui) { const chosen = ui.integer("Sections", sections, { min: 0, max: 1000, tip: "0 places a section at each rail point." }); if (chosen !== sections) { sections = chosen; context.settings.set("sections", sections); cached = null; } const scale = ui.checkbox("Scale height with width", scaleHeight, { tip: "Scale each section evenly, or only across the rails." }); if (scale !== scaleHeight) { scaleHeight = scale; context.settings.set("scaleHeight", scaleHeight); cached = null; } const capped = ui.checkbox("Cap ends", cap, { tip: "Close the ends of a sweep of closed profiles." }); if (capped !== cap) { cap = capped; context.settings.set("cap", cap); } const smoothed = ui.checkbox("Smooth", smooth, { tip: "Soften the edges between sections." }); if (smoothed !== smooth) { smooth = smoothed; context.settings.set("smooth", smooth); } const slot = getSlot(); kSlotLabels.forEach((label, index) => ui.readOnly(label, index === 1 && picks[1] !== null && picks[1] === picks[0] ? "Same as start" : picks[index] !== null ? "Picked" : index === slot ? "Click to pick" : "")); const swept = getSweep(); if (typeof swept === "string") { ui.text(swept, { tone: "error" }); } ui.row(() => { if (ui.button("Start Over", { disabled: picks[0] === null })) { reset(); } if (ui.button("Sweep", { primary: true, disabled: swept === null || typeof swept === "string" })) { commit(); } }); }, draw(view) { showState(); const shown = /** @type {[Pick, modlumi.Color, string][]} */ (picks.flatMap((pick, index) => pick === null || (index === 1 && pick === picks[0]) ? [] : [[pick, kPickColors[index], kSlotLabels[index]]])); if (hover !== null) { shown.push([hover, kHoverColor, ""]); } for (const [pick, color, label] of shown) { const { points, closed } = getPolyline(pick); view.polyline(closed ? [...points, points[0]] : points, { color, width: 3 }); if (label !== "") { view.text(points[Math.floor((points.length - 1) / 2)], label, { color }); } } const swept = getSweep(); if (swept === null || typeof swept === "string") { return; } const { sections: rows, closed } = swept; for (const row of rows) { view.polyline(closed ? [...row, row[0]] : row, { color: kPreviewColor }); } for (let index = 0; index < rows[0].length; ++index) { view.polyline(rows.map((row) => row[index]), { color: kPreviewColor, stipple: "dotted" }); } }, });
// Undo, redo and edits elsewhere may change or remove what was picked. const refresh = () => { if (picks.some((pick) => pick !== null && !isValidPick(pick))) { reset(); } // What was under the pointer may be gone; the next move finds it again. hover = null; cached = null; }; context.onModelChanged(refresh); context.onActiveContextChanged(reset); context.onDocumentOpened(reset);}Select Similar
Section titled “Select Similar”Grows the selection to faces like the selected ones, or filters it by type.
Select a face, then choose a command from Extensions → Select Similar: coplanar faces, parallel faces, faces facing the same way, faces with the same material, or connected faces on the same plane. Keep Only Faces, Keep Only Edges and Keep Only Groups and Components filter the selection. The most used commands are also in the right-click menu.
Inspired by the selection tools in ThomThom’s SketchUp extensions.
Source code
{ "id": "select-similar", "name": "Select Similar", "version": "1.0.0", "description": "Grows or filters the selection by plane, direction, material or type.", "author": "Modlumi", "license": "MIT", "apiVersion": 1}// Grows the selection to faces that match the selected ones, or filters it by type.// Inspired by the selection commands in ThomThom's Selection Tools.
// Faces closer than this, in meters, or whose normals differ less than this, in radians, match.const kDistanceTolerance = 1e-6;const kAngleTolerance = 1e-6;
/** @typedef {{ normal: modlumi.Vector3d, distance: number }} Plane */
/** * @param {modlumi.Entity} element * @returns {element is modlumi.Face} */function isFace(element) { return element instanceof modlumi.Face;}
/** * @param {modlumi.Model} model * @returns {modlumi.Face[]} */function selectedFaces(model) { return model.selection.toArray().filter(isFace);}
/** * @param {modlumi.Model} model * @returns {modlumi.Face[]} */function contextFaces(model) { return model.activeEntities.toArray().filter(isFace);}
// Returns a face's unit normal and its plane's signed distance from the origin, or null for a// face without a normal./** * @param {modlumi.Face} face * @returns {Plane | null} */function planeOf(face) { const normal = face.normal; if (normal === null) { return null; } const point = face.outerLoop.edges[0].start.position; return { normal, distance: normal.dot(point.toArray()) };}
/** * @param {modlumi.Vector3d} a * @param {modlumi.Vector3d} b */function isParallel(a, b) { return Math.abs(Math.abs(a.dot(b)) - 1) <= kAngleTolerance;}
/** * @param {modlumi.Vector3d} a * @param {modlumi.Vector3d} b */function isSameDirection(a, b) { return Math.abs(a.dot(b) - 1) <= kAngleTolerance;}
// Whether two planes coincide, whichever way their normals point./** * @param {Plane} a * @param {Plane} b */function isCoplanar(a, b) { if (!isParallel(a.normal, b.normal)) { return false; } const distance = a.normal.dot(b.normal) > 0 ? b.distance : -b.distance; return Math.abs(a.distance - distance) <= kDistanceTolerance;}
// Adds the faces of the active context that match any selected face, and reports how many./** * @param {modlumi.PluginContext} context * @param {string} describe * @param {(seed: modlumi.Face, face: modlumi.Face) => boolean} matches */function growSelection(context, describe, matches) { const model = modlumi.activeModel; const seeds = selectedFaces(model); if (seeds.length === 0) { context.showMessage("Select at least one face first."); return; } let added = 0; for (const face of contextFaces(model)) { const matching = seeds.some((seed) => matches(seed, face)); if (matching && !model.selection.has(face) && model.selection.add(face)) { added += 1; } } const faces = added === 1 ? "face" : "faces"; context.showMessage(added === 0 ? `No more ${describe} faces.` : `Added ${added} ${describe} ${faces}.`);}
// Adds faces reachable from the selected ones through shared edges while staying on their plane./** @param {modlumi.PluginContext} context */function selectConnectedPlanar(context) { const model = modlumi.activeModel; const seeds = selectedFaces(model); if (seeds.length === 0) { context.showMessage("Select at least one face first."); return; } /** @type {Map<modlumi.Edge, modlumi.Face[]>} */ const facesByEdge = new Map(); for (const face of contextFaces(model)) { for (const loop of face.loops) { for (const edge of loop.edges) { const faces = facesByEdge.get(edge) ?? []; faces.push(face); facesByEdge.set(edge, faces); } } } const reached = new Set(seeds); const pending = [...seeds]; let added = 0; while (pending.length > 0) { const face = /** @type {modlumi.Face} */ (pending.pop()); const plane = planeOf(face); if (plane === null) { continue; } for (const loop of face.loops) { for (const edge of loop.edges) { for (const neighbor of facesByEdge.get(edge) ?? []) { const other = planeOf(neighbor); if (reached.has(neighbor) || other === null || !isCoplanar(plane, other)) { continue; } reached.add(neighbor); pending.push(neighbor); if (model.selection.add(neighbor)) { added += 1; } } } } } const faces = added === 1 ? "face" : "faces"; context.showMessage(added === 0 ? "No more connected faces on the same plane." : `Added ${added} connected ${faces}.`);}
// Keeps only the selected elements that pass keep, and reports what was removed./** * @param {modlumi.PluginContext} context * @param {string} describe * @param {(element: modlumi.DrawingElement) => boolean} keep */function filterSelection(context, describe, keep) { const selection = modlumi.activeModel.selection; let removed = 0; for (const element of selection.toArray()) { if (!keep(element) && selection.remove(element)) { removed += 1; } } const items = removed === 1 ? "item" : "items"; context.showMessage(`Kept ${selection.length} ${describe}; removed ${removed} other ${items}.`);}
/** @param {modlumi.PluginContext} context */export function activate(context) { /** * @param {string} id * @param {string} title * @param {string} tip * @param {string} describe * @param {(seed: modlumi.Face, face: modlumi.Face) => boolean} matches */ const grow = (id, title, tip, describe, matches, contextMenu = false) => context.registerCommand({ id, title, tip, when: "selection", contextMenu, run: () => growSelection(context, describe, matches), }); grow( "coplanar-faces", "Select Coplanar Faces", "Adds every face in the current context that lies on the plane of a selected face.", "coplanar", (seed, face) => { const a = planeOf(seed); const b = planeOf(face); return a !== null && b !== null && isCoplanar(a, b); }, true, ); grow( "parallel-faces", "Select Parallel Faces", "Adds every face in the current context that is parallel to a selected face, facing either way.", "parallel", (seed, face) => seed.normal !== null && face.normal !== null && isParallel(seed.normal, face.normal), ); grow( "same-direction-faces", "Select Faces Facing the Same Way", "Adds every face in the current context whose front faces the same direction as a selected face.", "same-direction", (seed, face) => seed.normal !== null && face.normal !== null && isSameDirection(seed.normal, face.normal), ); grow( "same-material-faces", "Select Faces with the Same Material", "Adds every face in the current context whose material matches a selected face.", "same-material", (seed, face) => seed.material === face.material, true, ); context.registerCommand({ id: "connected-planar-faces", title: "Select Connected Faces on the Same Plane", tip: "Adds the faces that connect to a selected face through shared edges without leaving its plane.", when: "selection", contextMenu: true, run: () => selectConnectedPlanar(context), }); /** @type {{ id: string, title: string, describe: string, keep: (element: modlumi.DrawingElement) => boolean }[]} */ const filters = [ { id: "keep-faces", title: "Keep Only Faces", describe: "faces", keep: isFace }, { id: "keep-edges", title: "Keep Only Edges", describe: "edges", keep: (element) => element instanceof modlumi.Edge, }, { id: "keep-containers", title: "Keep Only Groups and Components", describe: "groups and components", keep: (element) => element instanceof modlumi.Group || element instanceof modlumi.ComponentInstance, }, ]; for (const { id, title, describe, keep } of filters) { context.registerCommand({ id, title, tip: `Removes everything but ${describe} from the selection.`, when: "selection", run: () => filterSelection(context, describe, keep), }); }}Shapes
Section titled “Shapes”Adds boxes, cylinders, cones, tori, tubes, prisms, pyramids, domes, spheres, helices and helical ramps from a dialog of their dimensions. A shape stays editable: select it and change its dimensions in Properties.
Choose a shape from Extensions → Shapes, set its dimensions and click Add. The shape is added as a group at the origin of the context you’re editing, and the dialog remembers the last values you used.
Port of Shapes by Trimble (MIT).
Source code
{ "id": "shapes", "name": "Shapes", "version": "1.0.0", "description": "Adds boxes, cylinders, cones, tori, tubes, prisms, pyramids, domes, spheres, helices and helical ramps from parameter dialogs, editable later in Properties.", "author": "Modlumi, after the Shapes extension by Trimble", "license": "MIT", "apiVersion": 1}// Adds parametric shapes from a dialog of their parameters. Each shape is a group that keeps// its parameters, so selecting it shows them in Properties, where changing one rebuilds it.//// A port of the Shapes extension (https://github.com/SketchUp/sketchup-shapes)://// Copyright (c) 2014 Trimble Navigation Ltd.//// Permission is hereby granted, free of charge, to any person obtaining a copy of this software// and associated documentation files (the "Software"), to deal in the Software without// restriction, including without limitation the rights to use, copy, modify, merge, publish,// distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the// Software is furnished to do so, subject to the following conditions://// The above copyright notice and this permission notice shall be included in all copies or// substantial portions of the Software.//// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING// BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
/** * @typedef {Record<string, number>} Parameters * @typedef {{ * id: string, * title: string, * tip: string, * defaults: Parameters, * schema: Record<string, modlumi.FormField>, * problem?: (parameters: Parameters) => string | undefined, * draw: (entities: modlumi.Entities, parameters: Parameters) => void, * }} Shape * @typedef {{ shape: string, parameters: Parameters }} ShapeData */
const model = modlumi.activeModel;// The shortest length a dimension may have.const kMinLength = 0.001;// Edges between faces that meet at a smaller angle are softened, so curved surfaces read as one.const kSmoothAngle = (46 * Math.PI) / 180;// Coordinates closer than this to a plane or axis lie on it.const kTolerance = 1e-9;
/** @param {string} label @returns {modlumi.FormField} */const length = (label) => ({ type: "length", label, min: kMinLength });/** @param {string} label @param {number} min @param {number} max @param {string} [tip] @returns {modlumi.FormField} */const count = (label, min, max, tip) => ({ type: "integer", label, min, max, tip });
/** * `segments` points around a circle of `radius` at height `z`, counterclockwise from `start` * radians off the X axis. * @param {number} radius * @param {number} segments * @returns {[number, number, number][]} */function ring(radius, segments, z = 0, start = 0) { return Array.from({ length: segments }, (_, index) => { const angle = start + (2 * Math.PI * index) / segments; return [radius * Math.cos(angle), radius * Math.sin(angle), z]; });}
/** * Points along a helix that turns `turns` times, counterclockwise when positive, rising * `pitch` per turn while its radius changes linearly from `startRadius` to `endRadius`. * @param {Parameters} helix * @param {number} startRadius * @param {number} endRadius * @returns {[number, number, number][]} */function helixPoints(helix, startRadius, endRadius) { const steps = Math.max(1, Math.round(Math.abs(helix.turns) * helix.segments)); const turnsPerStep = helix.turns / steps; return Array.from({ length: steps + 1 }, (_, step) => { const radius = startRadius + ((endRadius - startRadius) * step) / steps; const angle = helix.startAngle + 2 * Math.PI * turnsPerStep * step; return [radius * Math.cos(angle), radius * Math.sin(angle), helix.pitch * turnsPerStep * step]; });}
/** * Explains why a helix, or a ramp `width` wide, can't be drawn, or returns `undefined`. Without * a pitch, turns overlap unless the radius grows by more than the width each turn. * @param {Parameters} helix * @param {number} [width] */function helixProblem(helix, width = 0) { if (Math.abs(helix.turns) * helix.segments < 1) { return "Too few turns to draw one segment."; } const growth = Math.abs(helix.endRadius - helix.startRadius) / Math.abs(helix.turns); if (helix.pitch === 0 && Math.abs(helix.turns) > 1 && growth <= width) { return "Without a pitch, the turns would overlap."; } return undefined;}
/** * Softens and smooths the edges between faces that meet at a shallow angle, so curved * surfaces read as one. * @param {modlumi.Entities} entities */function smooth(entities) { /** @type {Map<modlumi.Edge, modlumi.Vector3d[]>} */ const normals = new Map(); for (const face of entities.ofType("Face")) { for (const loop of face.loops) { for (const edge of loop.edges) { const list = normals.get(edge) ?? []; if (face.normal) { list.push(face.normal); } normals.set(edge, list); } } } for (const [edge, [first, second, ...rest]] of normals) { if (first && second && rest.length === 0 && first.angleBetween(second) < kSmoothAngle) { edge.soft = true; edge.smooth = true; } }}
/** * The normal of the side `points` run counterclockwise around, by Newell's method. * @param {modlumi.Point3dLike[]} points */function windingNormal(points) { let normal = new modlumi.Vector3d(); points.forEach((point, index) => { const [x1, y1, z1] = point; const [x2, y2, z2] = points[(index + 1) % points.length]; normal = normal.add([(y1 - y2) * (z1 + z2), (z1 - z2) * (x1 + x2), (x1 - x2) * (y1 + y2)]); }); return normal;}
/** * Draws a face through each polygon, its front on the side the points run counterclockwise * around. Closing planar loops also fills them, like drawing them by hand would, so faces other * than these and `keep` are erased again. * @param {modlumi.Entities} entities * @param {modlumi.Point3dLike[][]} polygons * @param {modlumi.Face[]} [keep] */function addFaces(entities, polygons, keep = []) { const wanted = new Set(keep); for (const polygon of polygons) { const face = entities.addFace(polygon); // Drawing earlier faces may have filled this one already, facing either way. if (face.normal && face.normal.dot(windingNormal(polygon)) < 0) { face.reverse(); } wanted.add(face); } for (const face of entities.ofType("Face")) { if (!wanted.has(face)) { entities.erase(face); } }}
/** * Revolves a profile in the XZ plane around the Z axis, along a circle of `radius`. * @param {modlumi.Entities} entities * @param {modlumi.Point3dLike[]} profile * @param {number} radius * @param {number} segments */function revolve(entities, profile, radius, segments) { const path = ring(radius, segments); entities.addFace(profile).sweep([...path, path[0]]);}
/** * Revolving a profile edge along the X axis leaves a fan of triangles around the origin; erases * its spokes so they merge into one face. * @param {modlumi.Entities} entities */function mergeFan(entities) { const isHub = (/** @type {modlumi.Vertex} */ vertex) => vertex.position.distance([0, 0, 0]) < kTolerance; const spokes = entities.ofType("Edge").filter((edge) => Math.abs(edge.start.position.z) < kTolerance && Math.abs(edge.end.position.z) < kTolerance && (isHub(edge.start) || isHub(edge.end))); // Erasing the spokes also removes the hub vertex they leave behind. entities.eraseEdges(spokes);}
/** * Draws triangles from each edge of `base`'s outer loop up to `apex`, then turns the base to * face down, closing a solid. * @param {modlumi.Entities} entities * @param {modlumi.Face} base a face in the XY plane whose front faces up * @param {modlumi.Point3dLike} apex */function addApex(entities, base, apex) { // Taking the corners from the base keeps the triangles on its exact vertices. const corners = base.outerLoop.edgeUses.map((use) => use.start.position); base.reverse(); const sides = corners.map((corner, index) => [corner, corners[(index + 1) % corners.length], apex]); addFaces(entities, sides, [base]);}
/** @type {Shape[]} */const kShapes = [ { id: "box", title: "Box", tip: "Adds a box from its width, depth and height.", defaults: { width: 1, depth: 1, height: 1 }, schema: { width: length("Width"), depth: length("Depth"), height: length("Height") }, draw(entities, box) { entities.addRectangle([0, 0, 0], box.width, box.depth).extrude(box.height); }, }, { id: "cylinder", title: "Cylinder", tip: "Adds a cylinder standing on its base circle.", defaults: { radius: 0.5, height: 1, segments: 24 }, schema: { radius: length("Radius"), height: length("Height"), segments: count("Segments", 3, 360) }, draw(entities, cylinder) { entities.addCircle([0, 0, 0], cylinder.radius, { segments: cylinder.segments }).extrude(cylinder.height); // Extruding leaves the top's edges without a curve: retrace them as a circle. entities.addCircle([0, 0, cylinder.height], cylinder.radius, { segments: cylinder.segments }); smooth(entities); }, }, { id: "cone", title: "Cone", tip: "Adds a cone standing on its base circle.", defaults: { radius: 0.5, height: 1, segments: 24 }, schema: { radius: length("Radius"), height: length("Height"), segments: count("Segments", 3, 360) }, draw(entities, cone) { const base = entities.addCircle([0, 0, 0], cone.radius, { segments: cone.segments }); addApex(entities, base, [0, 0, cone.height]); smooth(entities); }, }, { id: "torus", title: "Torus", tip: "Adds a ring with a round profile, lying flat.", defaults: { outerRadius: 1, profileRadius: 0.25, profileSegments: 12, segments: 24 }, schema: { outerRadius: { ...length("Outer radius"), tip: "From the center to the outside of the ring" }, profileRadius: { ...length("Profile radius"), tip: "Radius of the ring's round cross-section" }, profileSegments: count("Profile segments", 3, 96), segments: count("Ring segments", 3, 96), }, problem: (torus) => torus.profileRadius * 2 >= torus.outerRadius ? "The profile radius must be less than half the outer radius." : undefined, draw(entities, torus) { const middle = torus.outerRadius - torus.profileRadius; const profile = ring(torus.profileRadius, torus.profileSegments).map(([x, y]) => [middle + x, 0, y]); revolve(entities, /** @type {[number, number, number][]} */ (profile), middle, torus.segments); smooth(entities); }, }, { id: "tube", title: "Tube", tip: "Adds a hollow cylinder from its outer radius and wall thickness.", defaults: { radius: 0.5, thickness: 0.05, height: 1, segments: 24 }, schema: { radius: { ...length("Outer radius") }, thickness: length("Wall thickness"), height: length("Height"), segments: count("Segments", 3, 360), }, problem: (tube) => (tube.thickness >= tube.radius ? "The wall must be thinner than the radius." : undefined), draw(entities, tube) { entities.addCircle([0, 0, 0], tube.radius, { segments: tube.segments }); // The inner circle cuts its own face out of the outer one, leaving a ring. entities.erase(entities.addCircle([0, 0, 0], tube.radius - tube.thickness, { segments: tube.segments })); entities.ofType("Face")[0].extrude(tube.height); smooth(entities); }, }, { id: "prism", title: "Prism", tip: "Adds a prism on a regular polygon.", defaults: { radius: 0.5, height: 1, sides: 6 }, schema: { radius: { ...length("Radius"), tip: "From the center to a corner" }, height: length("Height"), sides: count("Sides", 3, 360), }, draw(entities, prism) { entities.addPolygon([0, 0, 0], prism.radius, prism.sides).extrude(prism.height); }, }, { id: "pyramid", title: "Pyramid", tip: "Adds a pyramid on a regular polygon.", defaults: { radius: 0.5, height: 1, sides: 4 }, schema: { radius: { ...length("Radius"), tip: "From the center to a corner" }, height: length("Height"), sides: count("Sides", 3, 360), }, draw(entities, pyramid) { addApex(entities, entities.addPolygon([0, 0, 0], pyramid.radius, pyramid.sides), [0, 0, pyramid.height]); }, }, { id: "dome", title: "Dome", tip: "Adds a half sphere standing on its flat side.", defaults: { radius: 0.5, segments: 6 }, schema: { radius: length("Radius"), segments: count("Segments per 90°", 1, 24) }, draw(entities, dome) { /** @type {[number, number, number][]} */ const profile = [[0, 0, 0]]; for (let index = 0; index <= dome.segments; ++index) { const angle = (Math.PI / 2) * (index / dome.segments); profile.push([index === dome.segments ? 0 : dome.radius * Math.cos(angle), 0, dome.radius * Math.sin(angle)]); } revolve(entities, profile, dome.radius, dome.segments * 4); mergeFan(entities); smooth(entities); }, }, { id: "sphere", title: "Sphere", tip: "Adds a sphere around the origin.", defaults: { radius: 0.5, segments: 6 }, schema: { radius: length("Radius"), segments: count("Segments per 90°", 1, 24) }, draw(entities, sphere) { const rings = sphere.segments * 2; const profile = Array.from({ length: rings + 1 }, (_, index) => { const angle = (Math.PI * index) / rings - Math.PI / 2; const onAxis = index === 0 || index === rings; return /** @type {[number, number, number]} */ ([onAxis ? 0 : sphere.radius * Math.cos(angle), 0, sphere.radius * Math.sin(angle)]); }); revolve(entities, profile, sphere.radius, sphere.segments * 4); smooth(entities); }, }, { id: "helix", title: "Helix", tip: "Adds a helix or spiral line around the Z axis.", defaults: { startRadius: 0.5, endRadius: 0.5, pitch: 0.5, turns: 2, segments: 24, startAngle: 0 }, schema: { startRadius: length("Start radius"), endRadius: length("End radius"), pitch: { type: "length", label: "Pitch", tip: "Rise per turn; negative pitches go down" }, turns: { type: "number", label: "Turns", decimals: 2, tip: "Negative turns run clockwise" }, segments: count("Segments per turn", 3, 360), startAngle: { type: "angle", label: "Start angle", tip: "Where the helix starts, from the X axis" }, }, problem: (helix) => helixProblem(helix), draw(entities, helix) { // Edges never fill the turn a flat helix closes. entities.addEdges(helixPoints(helix, helix.startRadius, helix.endRadius)); }, }, { id: "helical-ramp", title: "Helical Ramp", tip: "Adds a spiral ramp surface around the Z axis.", defaults: { startRadius: 0.5, endRadius: 0.5, startWidth: 1, endWidth: 1, pitch: 2, turns: 1, segments: 24, startAngle: 0, }, schema: { startRadius: { ...length("Start radius"), tip: "Inner radius where the ramp starts" }, endRadius: { ...length("End radius"), tip: "Inner radius where the ramp ends" }, startWidth: length("Start width"), endWidth: length("End width"), pitch: { type: "length", label: "Pitch", tip: "Rise per turn; negative pitches go down" }, turns: { type: "number", label: "Turns", decimals: 2, tip: "Negative turns run clockwise" }, segments: count("Segments per turn", 3, 360), startAngle: { type: "angle", label: "Start angle", tip: "Where the ramp starts, from the X axis" }, }, problem: (ramp) => helixProblem(ramp, Math.max(ramp.startWidth, ramp.endWidth)), draw(entities, ramp) { const inner = helixPoints(ramp, ramp.startRadius, ramp.endRadius); const outer = helixPoints(ramp, ramp.startRadius + ramp.startWidth, ramp.endRadius + ramp.endWidth); /** @type {modlumi.Point3dLike[][]} */ const triangles = []; for (let step = 0; step + 1 < inner.length; ++step) { // Ordered so the ramp's front faces up while it turns counterclockwise. const first = [inner[step], outer[step], inner[step + 1]]; const second = [outer[step], outer[step + 1], inner[step + 1]]; triangles.push(...(ramp.turns > 0 ? [first, second] : [first.reverse(), second.reverse()])); } addFaces(entities, triangles); smooth(entities); }, },];
const kShapesById = new Map(kShapes.map((shape) => [shape.id, shape]));
/** * Draws the shape's parameters and returns them, changed where the user edited them, and why * they can't be drawn, if they can't. * @param {modlumi.Ui} ui * @param {Shape} shape * @param {Parameters} parameters * @returns {[Parameters, string | undefined]} */function drawParameters(ui, shape, parameters) { const changed = ui.form(parameters, shape.schema); const problem = shape.problem?.(changed); if (problem) { ui.text(problem, { tone: "warning" }); } return [changed, problem];}
/** * Replaces everything in entities with the shape. * @param {modlumi.Entities} entities * @param {Shape} shape * @param {Parameters} parameters */function redraw(entities, shape, parameters) { for (const element of entities) { if (!(element instanceof modlumi.Edge) && !(element instanceof modlumi.Face) && element.isValid) { entities.erase(element); } } entities.eraseEdges(entities.ofType("Edge")); shape.draw(entities, parameters);}
/** @param {modlumi.PluginContext} context */export function activate(context) { /** * Parameters shown in Properties that can't be drawn yet, by the shape they belong to, with * the stored parameters they were edited from. An undo or redo that changes those drops them. * @type {Map<modlumi.Group | modlumi.ComponentInstance, { from: string, parameters: Parameters }>} */ const drafts = new Map();
/** * Adds the shape as a new group in the active context and selects it. One undo step. * @param {Shape} shape * @param {Parameters} parameters */ function create(shape, parameters) { const group = model.operation(`Add ${shape.title}`, () => { const added = model.activeEntities.addGroup(); added.name = shape.title; shape.draw(added.entities, parameters); context.entityData.set(added, /** @type {ShapeData} */ ({ shape: shape.id, parameters })); return added; }); model.selection.clear(); model.selection.add(group); }
/** * Rebuilds the shape in group from new parameters. One undo step. * @param {modlumi.Group} group * @param {Shape} shape * @param {Parameters} parameters */ function rebuild(group, shape, parameters) { model.operation(`Change ${shape.title}`, () => { // Copies of the shape share its entities until one of them changes. group.makeUnique(); redraw(group.entities, shape, parameters); context.entityData.set(group, /** @type {ShapeData} */ ({ shape: shape.id, parameters })); }); }
/** @param {Shape} shape */ function showDialog(shape) { // The dialog starts from the shape's last parameters. /** @type {Parameters} */ let parameters = { ...shape.defaults, ...context.settings.get(shape.id, {}) }; context.showDialog({ id: "add", title: shape.title, draw(ui) { const [changed, problem] = drawParameters(ui, shape, parameters); parameters = changed; ui.spacing(); ui.row(() => { if (ui.button("Add", { primary: true, disabled: problem !== undefined })) { ui.close(); create(shape, parameters); try { context.settings.set(shape.id, parameters); } catch (error) { // Only the next dialog's starting values are lost. console.warn(`Could not remember the ${shape.title.toLowerCase()} parameters: ${error}`); } } if (ui.button("Cancel")) { ui.close(); } }); }, }); }
for (const shape of kShapes) { context.registerCommand({ id: shape.id, title: `${shape.title}...`, tip: shape.tip, run: () => showDialog(shape) }); }
context.addPropertiesSection({ id: "shape", title: "Shape", draw(ui, container) { /** @type {ShapeData | undefined} */ const data = context.entityData.get(container); const shape = kShapesById.get(data?.shape ?? ""); if (!data || !shape || !(container instanceof modlumi.Group)) { return; } ui.readOnly("Shape", shape.title); const stored = JSON.stringify(data.parameters); const draft = drafts.get(container); const [changed, problem] = drawParameters(ui, shape, draft?.from === stored ? draft.parameters : data.parameters); if (problem) { drafts.set(container, { from: stored, parameters: changed }); return; } drafts.delete(container); if (JSON.stringify(changed) !== stored) { rebuild(container, shape, changed); } }, });}Solid Inspector
Section titled “Solid Inspector”Finds what keeps a group or component from being a solid, shows each problem in red, and fixes what can be fixed. It finds reversed, internal and external faces, stray edges, holes, hidden faces, nested groups and, if you ask, short edges.
Select a group or component and choose Extensions → Solid Inspector → Solid Inspector. The panel lists the problems by kind; the arrow keys step through them and glide the camera to each one. Fix All, or the fix for one kind, makes the repairs in one undo step. Some problems, such as a missing face, can’t be fixed automatically.
Port of Solid Inspector² by Thomas Thomassen (MIT).
Source code
{ "id": "solid-inspector", "name": "Solid Inspector", "version": "1.0.0", "description": "Finds what keeps a group or component from being a solid, such as holes, stray edges, internal faces and reversed faces, shows each in the model and fixes what can be fixed in one undo step.", "author": "Modlumi, after Solid Inspector² by Thomas Thomassen", "license": "MIT", "apiVersion": 1}// Finds what keeps a group or component from being a solid: holes, stray edges, internal and// external faces, reversed faces, hidden faces and nested instances. The tool shows each in// the model, lists them in its options and fixes what it can in one undo step.//// A port of Solid Inspector² (https://github.com/thomthom/solid-inspector), whose shell// resolution is based on Shellify by Anders Lyhagen://// Copyright (c) 2010-2014 Thomas Thomassen//// Permission is hereby granted, free of charge, to any person obtaining a copy of this software// and associated documentation files (the "Software"), to deal in the Software without// restriction, including without limitation the rights to use, copy, modify, merge, publish,// distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the// Software is furnished to do so, subject to the following conditions://// The above copyright notice and this permission notice shall be included in all copies or// substantial portions of the Software.//// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING// BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
/** * @typedef {"reversedFace" | "internalFace" | "externalFace" | "strayEdge" | "faceHole" | "hiddenFace" * | "surfaceBorder" | "internalFaceEdge" | "sharedEdge" | "nestedInstance" | "shortEdge"} ErrorKind * @typedef {"erase" | "reverse" | "show" | null} Fix * @typedef {{ * kind: ErrorKind, * entities: modlumi.DrawingElement[], * triangles: modlumi.Point3d[][], * lines: modlumi.Point3d[], * }} SolidError * @typedef {{ * entities: modlumi.Entities, * transformation: modlumi.Transformation, * name: string, * }} Target */
const model = modlumi.activeModel;const kTwoPi = Math.PI * 2;/** @type {modlumi.Color} */const kEdgeColor = { r: 1, g: 0, b: 0, a: 1 };/** @type {modlumi.Color} */const kFaceColor = { r: 1, g: 0, b: 0, a: 0.5 };/** @type {modlumi.Color} */const kCurrentEdgeColor = { r: 1, g: 0.58, b: 0.03, a: 1 };/** @type {modlumi.Color} */const kCurrentFaceColor = { r: 1, g: 0.58, b: 0.03, a: 0.6 };
/** @type {Record<ErrorKind, { name: string, fix: Fix, description: string }>} */const kErrorKinds = { reversedFace: { name: "Reversed Faces", fix: "reverse", description: "The front of every face of a solid must face outward, or many applications won't treat it as " + "a solid. Fixed by reversing the faces.", }, internalFace: { name: "Internal Faces", fix: "erase", description: "Faces inside a mesh that should be a solid. Fixed by erasing them.", }, externalFace: { name: "External Faces", fix: "erase", description: "Faces sticking out of a mesh that should be a solid. Fixed by erasing them.", }, strayEdge: { name: "Stray Edges", fix: "erase", description: "Edges that bound no face, so they aren't part of any solid. Fixed by erasing them.", }, faceHole: { name: "Face Holes", fix: "erase", description: "Edges that cut a hole in a face. Fixed by erasing the hole's edges.", }, hiddenFace: { name: "Hidden Faces", fix: "show", description: "Hidden faces aren't exported to STL files and may leave holes in the mesh. Fixed by showing them.", }, surfaceBorder: { name: "Surface Borders", fix: null, description: "Edges that bound only one face: the border of a surface or a hole in the mesh. These can't be " + "fixed automatically: close the mesh by hand and inspect it again.", }, internalFaceEdge: { name: "Internal Face Edges", fix: null, description: "Edges of internal faces. While the mesh has holes, it isn't possible to tell which faces are " + "internal: fix the holes and inspect it again.", }, sharedEdge: { name: "Shared Edges", fix: null, description: "Edges where closed volumes meet, so they don't form one solid. These can't be fixed " + "automatically: move the volumes apart or join them into one.", }, nestedInstance: { name: "Nested Instances", fix: null, description: "Solid tools don't count groups and components inside a solid as part of it. Explode them to " + "make their geometry part of the solid.", }, shortEdge: { name: "Short Edges", fix: null, description: "Very small geometry can give unpredictable results through precision errors. These can't be " + "fixed automatically; scaling the model up by a factor of 10 can work around them.", },};
// The order errors are listed and cycled through.const kKindOrder = /** @type {ErrorKind[]} */ (Object.keys(kErrorKinds));
/** * Returns the vector from the start of the edge to its end. * @param {modlumi.Edge} edge */function edgeDirection(edge) { return edge.start.position.vectorTo(edge.end.position);}
// The faces, edges and vertices of one drawing context, with what the shell resolution looks up// often, by entity id.class Mesh { /** * @param {modlumi.Face[]} faces * @param {modlumi.Edge[]} edges */ constructor(faces, edges) { this.faces = faces; this.edges = edges; /** @type {Map<number, modlumi.Face[]>} */ this.edgeFaces = new Map(); /** @type {Map<number, modlumi.Edge[]>} The edges of each face's loops. */ this.faceEdges = new Map(); /** @type {Map<number, Map<number, boolean>>} Whether each face's loop runs along an edge backward. */ this.reversedUses = new Map(); /** @type {Map<number, modlumi.Vector3d>} */ this.normals = new Map(); /** @type {Map<number, modlumi.Edge[]>} */ this.vertexEdges = new Map(); /** @type {Map<number, modlumi.Vertex>} */ this.vertices = new Map(); for (const face of faces) { const uses = new Map(); const loopEdges = []; for (const loop of face.loops) { for (const use of loop.edgeUses) { uses.set(use.edge.entityId, use.isReversed); loopEdges.push(use.edge); } } this.reversedUses.set(face.entityId, uses); this.faceEdges.set(face.entityId, loopEdges); this.normals.set(face.entityId, face.normal ?? new modlumi.Vector3d(0, 0, 0)); } for (const edge of edges) { this.edgeFaces.set(edge.entityId, [...edge.faces]); for (const vertex of [edge.start, edge.end]) { this.vertices.set(vertex.entityId, vertex); const at = this.vertexEdges.get(vertex.entityId); if (at === undefined) { this.vertexEdges.set(vertex.entityId, [edge]); } else { at.push(edge); } } } }
/** @param {modlumi.Edge} edge */ getEdgeFaces(edge) { return this.edgeFaces.get(edge.entityId) ?? []; }
/** @param {modlumi.Vertex} vertex */ getVertexEdges(vertex) { return this.vertexEdges.get(vertex.entityId) ?? []; }}
// Resolves the outer shell of a mesh: the faces that would form a closed solid, the internal and// external faces that don't belong to it, and the shell faces that face inward.class Shell { /** @param {Mesh} mesh */ constructor(mesh) { this.mesh = mesh; /** @type {Set<number>} */ this.shellFaces = new Set(); /** @type {Set<number>} */ this.internalFaces = new Set(); /** @type {Set<number>} */ this.externalFaces = new Set(); /** @type {Set<number>} */ this.reversedFaces = new Set(); }
resolve() { const groups = this.findGeometryGroups(); if (groups.length < 2) { this.resolveGroups(groups, [0]); return; } // A part inside another, such as a cavity's boundary, faces the other way. Which faces // form each part's shell doesn't depend on that, so find them first, and count how many // other shells enclose each part without partitions and flaps in the way. const probe = new Shell(this.mesh); probe.resolveGroups(groups, groups.map(() => 0)); this.resolveGroups(groups, getNestingDepths(this.mesh, groups, probe.shellFaces)); }
/** * Resolves the shell of each part of the mesh, facing out at an even nesting depth. * @param {modlumi.Edge[][]} groups * @param {number[]} depths */ resolveGroups(groups, depths) { const allFaces = this.mesh.faces.map((face) => face.entityId); // First a shell of the faces oriented outward. const front = new Set(); for (const [index, group] of groups.entries()) { const start = this.findStartFace(group, depths[index] % 2 === 0); if (start !== null) { for (const face of this.findShell(start)) { front.add(face); } } } // The faces it doesn't reach are internal; it may still hold external "flaps". this.internalFaces = new Set(allFaces.filter((face) => !front.has(face))); // findShell changes the reversed faces, so keep the first pass's to restore them. const firstReversed = new Set(this.reversedFaces); // A second shell, looking for normals the other way, tells external faces apart. const back = new Set(); for (const [index, group] of groups.entries()) { const start = this.findStartFace(group, depths[index] % 2 === 1); if (start !== null) { for (const face of this.findShell(start)) { back.add(face); } } } this.shellFaces = new Set([...front].filter((face) => back.has(face))); this.externalFaces = new Set( allFaces.filter((face) => !this.internalFaces.has(face) && !this.shellFaces.has(face))); this.reversedFaces = new Set([...this.shellFaces].filter((face) => firstReversed.has(face))); }
// Whether the shell forms a manifold: every edge of its faces bounds two or more of them. isValid() { if (this.shellFaces.size === 0) { return false; } for (const face of this.shellFaces) { for (const edge of this.mesh.faceEdges.get(face) ?? []) { if (this.mesh.getEdgeFaces(edge).filter((other) => this.shellFaces.has(other.entityId)).length < 2) { return false; } } } return true; }
// Returns the edges of each part of the mesh whose faces connect through shared edges. Parts // that only touch at a vertex or through stray edges are separate, so each gets its own shell. findGeometryGroups() { /** @type {modlumi.Edge[][]} */ const groups = []; const visited = new Set(); for (const seed of this.mesh.faces) { if (visited.has(seed.entityId)) { continue; } visited.add(seed.entityId); /** @type {Map<number, modlumi.Edge>} */ const edges = new Map(); const stack = [seed]; while (stack.length > 0) { const face = /** @type {modlumi.Face} */ (stack.pop()); for (const edge of this.mesh.faceEdges.get(face.entityId) ?? []) { edges.set(edge.entityId, edge); for (const next of this.mesh.getEdgeFaces(edge)) { if (!visited.has(next.entityId)) { visited.add(next.entityId); stack.push(next); } } } } groups.push([...edges.values()]); } return groups; }
/** * The faces at an edge that aren't known to be internal. * @param {modlumi.Edge} edge */ getFaces(edge) { return this.mesh.getEdgeFaces(edge).filter((face) => !this.internalFaces.has(face.entityId)); }
/** @param {modlumi.Face} face */ getNormal(face) { const normal = /** @type {modlumi.Vector3d} */ (this.mesh.normals.get(face.entityId)); return this.reversedFaces.has(face.entityId) ? normal.negate() : normal; }
/** * @param {modlumi.Edge} edge * @param {modlumi.Face} face */ isEdgeReversedIn(edge, face) { const reversed = this.mesh.reversedUses.get(face.entityId)?.get(edge.entityId) ?? false; return this.reversedFaces.has(face.entityId) ? !reversed : reversed; }
/** @param {modlumi.Face} face */ reverseFace(face) { if (!this.reversedFaces.delete(face.entityId)) { this.reversedFaces.add(face.entityId); } }
/** * Finds a face on the shell: at the highest vertex, along its edge least aligned with the z * axis, the face whose normal is closest to it, turned up for the outside or down for the * inside. It can't pick the right side of a face at a hole whose normal is horizontal. * @param {modlumi.Edge[]} edges * @param {boolean} outside * @returns {modlumi.Face | null} */ findStartFace(edges, outside) { // Only the group's own edges count: another part may touch it at a vertex. const own = new Set(edges.map((edge) => edge.entityId)); /** @type {modlumi.Vertex | null} */ let highest = null; for (const edge of edges) { for (const vertex of [edge.start, edge.end]) { if ((highest === null || vertex.position.z > highest.position.z) && this.getFaces(edge).length > 0) { highest = vertex; } } } if (highest === null) { return null; } /** @type {modlumi.Edge | null} */ let edge = null; let flattest = Infinity; for (const candidate of this.mesh.getVertexEdges(highest)) { const z = Math.abs(edgeDirection(candidate).normalize().z); if (own.has(candidate.entityId) && this.getFaces(candidate).length > 0 && z < flattest) { flattest = z; edge = candidate; } } if (edge === null) { return null; } /** @type {modlumi.Face | null} */ let face = null; let steepest = -Infinity; for (const candidate of this.getFaces(edge)) { const z = Math.abs(this.getNormal(candidate).z); if (z > steepest) { steepest = z; face = candidate; } } if (face === null) { return null; } const z = this.getNormal(face).z; if (outside ? z < 0 : z > 0) { this.reverseFace(face); } return face; }
/** * A vector along the edge in the direction the face's loop runs. * @param {modlumi.Edge} edge * @param {modlumi.Face} face */ getEdgeVector(edge, face) { return this.isEdgeReversedIn(edge, face) ? edgeDirection(edge).negate() : edgeDirection(edge); }
/** * Given a shell face and one of its edges, returns the other shell face at the edge, reversed * to match the face, or null. * @param {modlumi.Edge} edge * @param {modlumi.Face} face * @returns {modlumi.Face | null} */ findOtherShellFace(edge, face) { const faces = this.getFaces(edge); if (faces.length < 2) { return null; } if (faces.length === 2) { const other = faces.find((candidate) => candidate.entityId !== face.entityId); if (other === undefined) { return null; } if (this.isEdgeReversedIn(edge, face) === this.isEdgeReversedIn(edge, other)) { this.reverseFace(other); } return other; } // With more faces at the edge, the shell continues into the one at the smallest angle // around the edge, turning from the face's back. const edgeVector = this.getEdgeVector(edge, face); const normal = this.getNormal(face); const product = normal.cross(edgeVector); const reversed = this.isEdgeReversedIn(edge, face); let smallest = kTwoPi; /** @type {modlumi.Face | null} */ let shellFace = null; for (const other of faces) { if (other.entityId === face.entityId) { continue; } let otherNormal = this.getNormal(other); if (this.isEdgeReversedIn(edge, other) === reversed) { otherNormal = otherNormal.negate(); } const otherProduct = edgeVector.cross(otherNormal); let angle = product.angleBetween(otherProduct); if (otherProduct.dot(normal) < 0) { angle = kTwoPi - angle; } if (angle < smallest) { smallest = angle; shellFace = other; } } if (shellFace === null) { return null; } if (this.isEdgeReversedIn(edge, shellFace) === reversed) { this.reverseFace(shellFace); } return shellFace; }
/** * Walks the mesh from a shell face and returns the ids of the faces of its outer shell. * @param {modlumi.Face} start */ findShell(start) { const stack = [start]; const processed = new Set([`f${start.entityId}`]); /** @type {number[]} */ const shell = []; while (stack.length > 0) { const face = /** @type {modlumi.Face} */ (stack.pop()); shell.push(face.entityId); for (const edge of this.mesh.faceEdges.get(face.entityId) ?? []) { if (processed.has(`e${edge.entityId}`) || this.getFaces(edge).length < 2) { continue; } processed.add(`e${edge.entityId}`); const other = this.findOtherShellFace(edge, face); if (other === null || processed.has(`f${other.entityId}`)) { continue; } stack.push(other); processed.add(`f${other.entityId}`); } } return shell; }}
// A ray direction unlikely to run along an edge or within a face of modeled geometry.const kRay = new modlumi.Vector3d(0.5773, 0.5774, 0.5776).normalize();
/** * Whether a ray from origin along kRay crosses a triangle, by the Möller-Trumbore test. * @param {modlumi.Point3d} origin * @param {readonly modlumi.Point3d[]} triangle */function isRayCrossing(origin, [a, b, c]) { const ab = a.vectorTo(b); const ac = a.vectorTo(c); const p = kRay.cross(ac); const determinant = ab.dot(p); if (Math.abs(determinant) < 1e-12) { return false; } const t = a.vectorTo(origin); const u = t.dot(p) / determinant; if (u < 0 || u > 1) { return false; } const q = t.cross(ab); const v = kRay.dot(q) / determinant; return v >= 0 && u + v <= 1 && ac.dot(q) / determinant > 1e-9;}
/** * Returns, for each part of the mesh, how many shells of other parts enclose it. * @param {Mesh} mesh * @param {modlumi.Edge[][]} groups * @param {Set<number>} shellFaces */function getNestingDepths(mesh, groups, shellFaces) { const triangles = groups.map((edges) => { /** @type {Map<number, modlumi.Face>} */ const faces = new Map(); for (const edge of edges) { for (const face of mesh.getEdgeFaces(edge)) { if (shellFaces.has(face.entityId)) { faces.set(face.entityId, face); } } } return [...faces.values()].flatMap((face) => { try { return face.triangles; } catch { return []; } }); }); return groups.map((_, index) => { // The middle of one of the part's own triangles, which no other part can merely touch. const triangle = triangles[index][0]; if (triangle === undefined) { return 0; } const [a, b, c] = triangle; const origin = new modlumi.Point3d((a.x + b.x + c.x) / 3, (a.y + b.y + c.y) / 3, (a.z + b.z + c.z) / 3); let depth = 0; for (const [other, others] of triangles.entries()) { if (other !== index && others.filter((triangle) => isRayCrossing(origin, triangle)).length % 2 === 1) { ++depth; } } return depth; });}
/** * Splits edges into groups that touch at their vertices. * @param {modlumi.Edge[]} edges */function groupConnectedEdges(edges) { const remaining = new Map(edges.map((edge) => [edge.entityId, edge])); /** @type {modlumi.Edge[][]} */ const groups = []; for (const seed of edges) { if (!remaining.delete(seed.entityId)) { continue; } const group = [seed]; const stack = [seed]; while (stack.length > 0) { const edge = /** @type {modlumi.Edge} */ (stack.pop()); for (const vertex of [edge.start, edge.end]) { for (const next of vertex.edges) { if (remaining.delete(next.entityId)) { group.push(next); stack.push(next); } } } } groups.push(group); } return groups;}
/** * Returns the ids of the faces that close volumes of their own: what is left after peeling * away, again and again, every face with an edge that fewer than two remaining faces bound. * Flaps and partitions peel away, while a box with a flap keeps its six faces. * @param {Mesh} mesh * @param {modlumi.Face[]} faces */function findClosedVolumes(mesh, faces) { const remaining = new Set(faces.map((face) => face.entityId)); let peeled = true; while (peeled) { peeled = false; for (const face of faces) { if (!remaining.has(face.entityId)) { continue; } const open = (mesh.faceEdges.get(face.entityId) ?? []).some((edge) => mesh.getEdgeFaces(edge).filter((other) => remaining.has(other.entityId)).length < 2); if (open) { remaining.delete(face.entityId); peeled = true; } } } return remaining;}
/** * Finds what keeps the entities from forming a solid. * @param {modlumi.Entities} entities * @param {number} shortEdge Edges shorter than this are reported; 0 reports none. * @returns {{ kind: ErrorKind, entities: modlumi.DrawingElement[] }[]} */function findErrors(entities, shortEdge) { /** @type {{ kind: ErrorKind, entities: modlumi.DrawingElement[] }[]} */ const errors = []; const faces = /** @type {modlumi.Face[]} */ (entities.ofType("Face")); const edges = /** @type {modlumi.Edge[]} */ (entities.ofType("Edge")); for (const face of faces) { if (face.hidden) { errors.push({ kind: "hiddenFace", entities: [face] }); } } const mesh = new Mesh(faces, edges); /** @type {modlumi.Edge[]} */ const borderEdges = []; /** @type {modlumi.Edge[]} */ const holeEdges = []; /** @type {modlumi.Edge[]} */ const internalEdges = []; for (const edge of edges) { const edgeFaces = mesh.getEdgeFaces(edge); if (edgeFaces.length === 0) { errors.push({ kind: "strayEdge", entities: [edge] }); } else if (edgeFaces.length === 1) { const outer = edgeFaces[0].outerLoop.edges.some((other) => other.entityId === edge.entityId); (outer ? borderEdges : holeEdges).push(edge); } else if (edgeFaces.length > 2) { internalEdges.push(edge); } } const shell = new Shell(mesh); shell.resolve(); if (shell.isValid()) { // Faces off the shell that close a volume of their own, such as a second box sharing an // edge with the first, are another solid rather than faces to erase. const volumes = findClosedVolumes(mesh, faces.filter((face) => shell.internalFaces.has(face.entityId) || shell.externalFaces.has(face.entityId))); for (const face of faces) { const kind = volumes.has(face.entityId) ? null : shell.internalFaces.has(face.entityId) ? "internalFace" : shell.externalFaces.has(face.entityId) ? "externalFace" : shell.reversedFaces.has(face.entityId) ? "reversedFace" : null; if (kind !== null) { errors.push({ kind, entities: [face] }); } } // Fixing erases the other faces; more than two that stay at an edge keep it non-manifold. for (const edge of internalEdges) { const staying = mesh.getEdgeFaces(edge).filter((face) => shell.shellFaces.has(face.entityId) || volumes.has(face.entityId)); if (staying.length > 2) { errors.push({ kind: "sharedEdge", entities: [edge] }); } } } else { for (const group of groupConnectedEdges(borderEdges)) { errors.push({ kind: "surfaceBorder", entities: group }); } for (const group of groupConnectedEdges(holeEdges)) { errors.push({ kind: "faceHole", entities: group }); } for (const edge of internalEdges) { errors.push({ kind: "internalFaceEdge", entities: [edge] }); } } for (const instance of [...entities.ofType("Group"), ...entities.ofType("ComponentInstance")]) { errors.push({ kind: "nestedInstance", entities: [instance] }); } if (shortEdge > 0) { for (const edge of edges) { if (edge.length < shortEdge) { errors.push({ kind: "shortEdge", entities: [edge] }); } } } return errors;}
/** * Adds what draws an error, in the coordinates of the context being edited. * @param {{ kind: ErrorKind, entities: modlumi.DrawingElement[] }} error * @param {modlumi.Transformation} transformation * @returns {SolidError} */function prepareError(error, transformation) { /** @type {modlumi.Point3d[][]} */ const triangles = []; /** @type {modlumi.Point3d[]} */ const lines = []; for (const entity of error.entities) { if (entity.type === "Edge") { const edge = /** @type {modlumi.Edge} */ (entity); lines.push(edge.start.position.transform(transformation), edge.end.position.transform(transformation)); } else if (entity.type === "Face") { const face = /** @type {modlumi.Face} */ (entity); try { for (const triangle of face.triangles) { triangles.push(triangle.map((corner) => corner.transform(transformation))); } } catch { // A degenerate face has no triangles; its edges still show it. } if (error.kind !== "internalFace" && error.kind !== "externalFace") { for (const edge of face.loops.flatMap((loop) => loop.edges)) { lines.push(edge.start.position.transform(transformation), edge.end.position.transform(transformation)); } } } else { const bounds = entity.bounds; if (bounds !== null) { // The instance's bounding box. const { min, max } = bounds; /** @param {number} corner */ const at = (corner) => new modlumi.Point3d(corner & 1 ? max.x : min.x, corner & 2 ? max.y : min.y, corner & 4 ? max.z : min.z).transform(transformation); for (const [a, b] of [[0, 1], [2, 3], [4, 5], [6, 7], [0, 2], [1, 3], [4, 6], [5, 7], [0, 4], [1, 5], [2, 6], [3, 7]]) { lines.push(at(a), at(b)); } } } } return { ...error, triangles, lines };}
/** * Returns the selected group or component instance to inspect, or the context being edited. * @returns {Target} */function findTarget() { for (const entity of model.selection.toArray()) { if (entity.type === "Group") { const group = /** @type {modlumi.Group} */ (entity); return { entities: group.entities, transformation: group.transformation, name: group.name || "Group" }; } if (entity.type === "ComponentInstance") { const instance = /** @type {modlumi.ComponentInstance} */ (entity); return { entities: instance.definition.entities, transformation: instance.transformation, name: instance.name || instance.definition.name, }; } } return { entities: model.activeEntities, transformation: new modlumi.Transformation(), name: "the context being edited" };}
/** * Fixes the errors that can be fixed in one undo step, and returns how many couldn't be. * @param {SolidError[]} errors * @param {modlumi.Entities} entities */function fixErrors(errors, entities) { /** @type {Map<number, modlumi.Face>} */ const eraseFaces = new Map(); /** @type {Map<number, modlumi.Edge>} */ const eraseEdges = new Map(); let unfixed = 0; for (const error of errors) { if (kErrorKinds[error.kind].fix === null) { ++unfixed; } else if (kErrorKinds[error.kind].fix === "erase") { for (const entity of error.entities) { if (entity.type === "Face") { eraseFaces.set(entity.entityId, /** @type {modlumi.Face} */ (entity)); } else if (entity.type === "Edge") { eraseEdges.set(entity.entityId, /** @type {modlumi.Edge} */ (entity)); } } } } // Edges between erased faces would be left stray. for (const face of eraseFaces.values()) { for (const edge of face.loops.flatMap((loop) => loop.edges)) { if (edge.faces.every((other) => eraseFaces.has(other.entityId))) { eraseEdges.set(edge.entityId, edge); } } } // Erasing an edge of a curve erases the whole curve, so keep curves that would lose more. const edges = [...eraseEdges.values()].filter((edge) => edge.curve === null || edge.curve.edges.every((other) => eraseEdges.has(other.entityId))); unfixed += eraseEdges.size - edges.length > 0 ? 1 : 0; model.operation("Repair Solid", () => { for (const face of eraseFaces.values()) { if (face.isValid) { entities.erase(face); } } const valid = edges.filter((edge) => edge.isValid); if (valid.length > 0) { entities.eraseEdges(valid); } for (const error of errors) { const fix = kErrorKinds[error.kind].fix; for (const entity of error.entities) { if (!entity.isValid) { continue; } if (fix === "reverse") { /** @type {modlumi.Face} */ (entity).reverse(); } else if (fix === "show") { entity.hidden = false; } } } }); return unfixed;}
/** @param {modlumi.PluginContext} context */export function activate(context) { let active = false; /** @type {Target | null} */ let target = null; /** @type {SolidError[]} */ let errors = []; /** @type {ErrorKind | null} The kind of error shown, or null for every kind. */ let shownKind = null; /** @type {number | null} The error cycled to with the arrow keys, in the shown errors. */ let current = null; /** Edges shorter than this many meters are reported; 0 reports none. */ let shortEdge = 0; /** Whether cycling to an error glides the camera to it. */ let zoomToErrors = Boolean(context.settings.get("zoomToErrors", true));
function getShownErrors() { return shownKind === null ? errors : errors.filter((error) => error.kind === shownKind); }
function showStatus() { const shown = getShownErrors(); tool.status = current === null || current >= shown.length ? "Click a group or component to inspect it. The arrow keys cycle through the errors." : `Error ${current + 1} of ${shown.length}: ${kErrorKinds[shown[current].kind].name}. The arrow keys ` + "cycle through the errors."; }
/** * Glides the camera to frame an error, looking in the same direction as before. * @param {SolidError} error */ function zoomTo(error) { // The error's points are in the coordinates of the context being edited. const toWorld = model.activeTransformation; const points = [...error.triangles.flat(), ...error.lines].map((point) => point.transform(toWorld)); if (points.length === 0) { return; } const low = points.reduce((a, b) => new modlumi.Point3d(Math.min(a.x, b.x), Math.min(a.y, b.y), Math.min(a.z, b.z))); const high = points.reduce((a, b) => new modlumi.Point3d(Math.max(a.x, b.x), Math.max(a.y, b.y), Math.max(a.z, b.z))); const box = new modlumi.BoundingBox(low, high); const camera = context.viewport.camera; const direction = camera.eye.vectorTo(camera.target).normalize(); // Far enough that the error fills about a third of the view across its narrower side, but // never inside it. The field of view is vertical; a narrow view sees less sideways. const radius = Math.max(box.diagonal / 2, 0.05); const aspect = context.viewport.width / Math.max(1, context.viewport.height); const reach = Math.tan((camera.fieldOfView * Math.PI) / 360) * Math.min(1, aspect || 1); const distance = (1.5 * radius) / reach + radius; const framed = new modlumi.Camera(box.center.offset(direction.scale(-distance)), box.center, { up: camera.up, perspective: camera.perspective, fieldOfView: camera.fieldOfView }); context.viewport.setCamera(framed, { duration: 0.4 }); }
// Inspects the selected group or component, or the context being edited. function analyze() { target = findTarget(); const { transformation } = target; errors = findErrors(target.entities, shortEdge) .sort((a, b) => kKindOrder.indexOf(a.kind) - kKindOrder.indexOf(b.kind)) .map((error) => prepareError(error, transformation)); if (shownKind !== null && !errors.some((error) => error.kind === shownKind)) { shownKind = null; } current = null; showStatus(); }
/** @param {SolidError[]} fixing */ function fix(fixing) { if (target === null) { return; } const unfixed = fixErrors(fixing, target.entities); analyze(); if (unfixed > 0) { context.showMessage("Some errors can't be fixed automatically and remain."); } else { const solid = errors.length === 0 && target.entities.ofType("Face").length > 0; context.showMessage(solid ? "All errors fixed: it's a solid." : "Errors fixed."); } }
const tool = context.registerTool({ id: "inspect", title: "Solid Inspector", tip: "Finds and fixes what keeps a group or component from being a solid.", snap: false, activate() { active = true; shownKind = null; analyze(); }, deactivate() { active = false; target = null; errors = []; }, onMouseUp(event) { // Clicking a group or component inspects it; clicking anything else, the context. model.selection.clear(); // What Select would pick: the container in the context being edited. const picked = event.picked?.path[model.activePath.length]; if (picked !== undefined) { model.selection.add(picked); } shownKind = null; analyze(); }, onKeyDown(event) { const shown = getShownErrors(); if (shown.length === 0) { return; } const step = event.key === "up" || event.key === "right" ? 1 : event.key === "down" || event.key === "left" ? -1 : 0; if (step !== 0) { current = current === null ? 0 : (current + step + shown.length) % shown.length; showStatus(); if (zoomToErrors) { zoomTo(shown[current]); } } }, onCancel() { current = null; showStatus(); }, options(ui) { if (target === null) { return; } ui.text(`Inspecting ${target.name}`, { tone: "muted" }); if (errors.length === 0) { const solid = target.entities.ofType("Face").length > 0; ui.text(solid ? "No errors: it's a solid." : "Nothing to inspect.", { tone: solid ? "success" : "normal" }); } else { const rows = kKindOrder .map((kind) => ({ kind, count: errors.filter((error) => error.kind === kind).length })) .filter(({ count }) => count > 0) .map(({ kind, count }) => ({ id: kind, cells: [kErrorKinds[kind].name, count] })); const chosen = /** @type {ErrorKind | null} */ (ui.table("Errors", ["Error", { title: "Count", numeric: true }], rows, shownKind, { visibleRows: Math.min(rows.length, 6) })); if (chosen !== shownKind) { shownKind = chosen; current = null; showStatus(); } if (shownKind !== null) { const kind = kErrorKinds[shownKind]; ui.text(kind.description, { tone: "muted" }); if (kind.fix !== null && ui.button(`Fix ${kind.name}`)) { fix(getShownErrors()); } } if (errors.some((error) => kErrorKinds[error.kind].fix !== null) && ui.button("Fix All", { primary: true })) { fix(errors); } } ui.separator(); const zoom = ui.checkbox("Zoom to errors", zoomToErrors, { tip: "The arrow keys glide the camera to each error" }); if (zoom !== zoomToErrors) { zoomToErrors = zoom; context.settings.set("zoomToErrors", zoom); } const findShort = ui.checkbox("Find short edges", shortEdge > 0); if (findShort !== shortEdge > 0) { shortEdge = findShort ? 0.001 : 0; analyze(); } if (shortEdge > 0) { const threshold = ui.length("Shorter than", shortEdge, { min: 0.000001 }); if (threshold !== shortEdge) { shortEdge = threshold; analyze(); } } }, draw(view) { if (target === null) { return; } const shown = getShownErrors(); const lines = []; for (const [index, error] of shown.entries()) { if (index === current) { continue; } for (const triangle of error.triangles) { view.polygon(triangle, { color: kFaceColor }); } lines.push(...error.lines); } if (lines.length > 0) { view.lines(lines, { color: kEdgeColor, width: 2 }); } const highlighted = current === null ? undefined : shown[current]; if (highlighted !== undefined) { for (const triangle of highlighted.triangles) { view.polygon(triangle, { color: kCurrentFaceColor }); } if (highlighted.lines.length > 0) { view.lines(highlighted.lines, { color: kCurrentEdgeColor, width: 3 }); } } }, });
// Undo, redo and edits elsewhere change what the inspected geometry gets wrong, and entering // or leaving a group, selecting another or opening a document changes what is inspected. const reanalyze = () => { if (active) { analyze(); } }; context.onModelChanged(reanalyze); context.onActiveContextChanged(reanalyze); context.onSelectionChanged(reanalyze); context.onDocumentOpened(reanalyze);}Exports STL models, binary and ASCII, for 3D printing and other tools. Modlumi imports STL files itself, with File → Import….
Use File → Export STL Model…. STL files have no units: Extensions → STL → STL Options… sets the units files are written in, binary or ASCII, and whether to export only the selection.
Port of sketchup-stl by Trimble and its contributors (MIT).
Source code
{ "id": "stl", "name": "STL", "version": "1.1.0", "description": "Exports STL models, binary or ASCII, for 3D printing and other tools.", "author": "Modlumi, after sketchup-stl by Trimble Navigation and its contributors", "license": "MIT", "apiVersion": 1}// STL export, ported from sketchup-stl. Modlumi imports STL files itself, through File > Import.//// Copyright (c) 2014 Trimble Navigation Ltd.//// Permission is hereby granted, free of charge, to any person obtaining a copy of this software// and associated documentation files (the "Software"), to deal in the Software without// restriction, including without limitation the rights to use, copy, modify, merge, publish,// distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the// Software is furnished to do so, subject to the following conditions://// The above copyright notice and this permission notice shall be included in all copies or// substantial portions of the Software.//// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING// BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
/** * @typedef {"Millimeters" | "Centimeters" | "Meters" | "Inches" | "Feet"} Unit * @typedef {{ exportUnits: Unit | "Model Units", format: "Binary" | "ASCII", selectionOnly: boolean }} Options * @typedef {[modlumi.Point3d, modlumi.Point3d, modlumi.Point3d]} Triangle */
const model = modlumi.activeModel;/** Meters per unit. @type {Record<Unit, number>} */const kMeters = { Millimeters: 0.001, Centimeters: 0.01, Meters: 1, Inches: 0.0254, Feet: 0.3048 };/** @type {Unit[]} */const kUnits = ["Millimeters", "Centimeters", "Meters", "Inches", "Feet"];/** The STL unit of each model unit; yards export as feet. */const kModelUnits = /** @type {Record<string, Unit>} */ ({ mm: "Millimeters", cm: "Centimeters", m: "Meters", in: "Inches", ft: "Feet", yd: "Feet",});/** @type {Options} */const kDefaults = { exportUnits: "Model Units", format: "Binary", selectionOnly: false,};// A binary STL file is an 80 byte header, a facet count, and 50 bytes per facet.const kHeaderBytes = 80;const kFacetBytes = 50;
/** * Collects the triangles of the visible faces in entities and the groups and components in * them, in the coordinates transformation maps to, each counterclockwise around its outside. * @param {Iterable<modlumi.DrawingElement>} elements * @param {modlumi.Transformation} transformation * @param {Triangle[]} triangles */function collectTriangles(elements, transformation, triangles) { // A mirroring transformation turns counterclockwise triangles clockwise. const [x, y, z] = [[1, 0, 0], [0, 1, 0], [0, 0, 1]].map((axis) => new modlumi.Vector3d(...axis).transform(transformation)); const mirrored = x.dot(y.cross(z)) < 0; for (const element of elements) { if (element.hidden) { continue; } if (element instanceof modlumi.Face) { for (const triangle of element.triangles) { const [a, b, c] = triangle.map((point) => point.transform(transformation)); triangles.push(mirrored ? [a, c, b] : [a, b, c]); } } else if (element instanceof modlumi.Group) { collectTriangles(element.entities, transformation.multiply(element.transformation), triangles); } else if (element instanceof modlumi.ComponentInstance) { collectTriangles(element.definition.entities, transformation.multiply(element.transformation), triangles); } }}
/** * Returns a triangle's unit normal by the right-hand rule, or zero for a degenerate one. * @param {Triangle} triangle */function getNormal([a, b, c]) { const normal = a.vectorTo(b).cross(a.vectorTo(c)); const length = Math.hypot(normal.x, normal.y, normal.z); return length > 0 ? normal.scale(1 / length) : normal;}
/** * Writes triangles as a binary STL file, in units of scale per meter. * @param {Triangle[]} triangles * @param {number} scale * @param {string} name */export function writeBinary(triangles, scale, name) { const bytes = new Uint8Array(kHeaderBytes + 4 + triangles.length * kFacetBytes); // The header is ASCII; other characters become question marks. const header = `Modlumi STL ${name}`.slice(0, kHeaderBytes); for (let index = 0; index < header.length; ++index) { const code = header.charCodeAt(index); bytes[index] = code < 128 ? code : 63; } const view = new DataView(bytes.buffer); view.setUint32(kHeaderBytes, triangles.length, true); triangles.forEach((triangle, facet) => { let at = kHeaderBytes + 4 + facet * kFacetBytes; const normal = getNormal(triangle); for (const value of [normal.x, normal.y, normal.z]) { view.setFloat32(at, value, true); at += 4; } for (const point of triangle) { for (const value of [point.x, point.y, point.z]) { view.setFloat32(at, value * scale, true); at += 4; } } }); return bytes;}
/** * Writes triangles as an ASCII STL file, in units of scale per meter. * @param {Triangle[]} triangles * @param {number} scale * @param {string} name */export function writeAscii(triangles, scale, name) { const lines = [`solid ${name}`]; for (const triangle of triangles) { const normal = getNormal(triangle); lines.push(`facet normal ${normal.x} ${normal.y} ${normal.z}`, " outer loop"); for (const point of triangle) { lines.push(` vertex ${point.x * scale} ${point.y * scale} ${point.z * scale}`); } lines.push(" endloop", "endfacet"); } lines.push(`endsolid ${name}`); return lines.join("\n") + "\n";}
/** * Returns a file's name without its extension. * @param {string} name */function getStem(name) { return name.replace(/\.[^.]*$/, "") || "Untitled";}
/** @param {modlumi.PluginContext} context */export function activate(context) { /** @returns {Options} */ const getOptions = () => ({ ...kDefaults, ...context.settings.get("options", {}) });
context.registerExporter({ id: "stl", name: "STL Model", extensions: ["stl"], export(file) { const options = getOptions(); const elements = options.selectionOnly && model.selection.length > 0 ? model.selection.toArray() : model.activeEntities; /** @type {Triangle[]} */ const triangles = []; collectTriangles(elements, model.activeTransformation, triangles); const unit = options.exportUnits === "Model Units" ? kModelUnits[model.units.lengthUnit] ?? "Millimeters" : options.exportUnits; const scale = 1 / kMeters[unit]; const name = getStem(file.name); if (options.format === "ASCII") { file.writeText(writeAscii(triangles, scale, name)); } else { file.writeBytes(writeBinary(triangles, scale, name)); } context.showMessage(`Exported ${triangles.length} triangles to ${file.name}, in ${unit.toLowerCase()}`); }, });
context.registerCommand({ id: "options", title: "STL Options...", tip: "Sets the units, format and scope STL export uses.", run() { /** @type {Options} */ const options = getOptions(); context.showDialog({ id: "options", title: "STL Options", modal: true, draw(ui) { options.exportUnits = /** @type {Unit | "Model Units"} */ (ui.dropdown("Units", options.exportUnits, ["Model Units", ...kUnits])); options.format = /** @type {"Binary" | "ASCII"} */ (ui.dropdown("Format", options.format, ["Binary", "ASCII"])); options.selectionOnly = ui.checkbox("Export only the selection", options.selectionOnly); ui.spacing(); ui.row(() => { if (ui.button("OK", { primary: true })) { context.settings.set("options", options); ui.close(); } if (ui.button("Cancel")) { ui.close(); } }); }, }); }, });}True Bend
Section titled “True Bend”Bends a group or component along a curve, to a typed angle or a dragged amount, with a live preview, in smooth or visible segments.
Select a group or component and choose Extensions → True Bend → True Bend.
Click and move to set the bend, then click again, or type an angle such as 90.
Press Enter, double-click, or click Bend to apply it. Type a count such as
24s to set the number of segments, and choose in the panel whether they show as
facets or are softened.
Port of True Bend by Thomas Thomassen (MIT).
Source code
{ "id": "true-bend", "name": "True Bend", "version": "1.0.0", "description": "Bends a group or component to a typed angle or a dragged amount, along a true or segmented curve, with a live preview.", "author": "Modlumi, after True Bend by Thomas Thomassen", "license": "MIT", "apiVersion": 1}// Bends a group or component along its red axis, as if around a cylinder: drag to set the// amount, or type an angle in degrees, a bend distance, or a number of segments such as 24s.// The bend follows a true curve, or one segment per slice; a preview shows the result before// it is applied in one undo step.//// A port of True Bend (https://github.com/thomthom/true-bend)://// Copyright (c) 2016 Thomas Thomassen//// Permission is hereby granted, free of charge, to any person obtaining a copy of this software// and associated documentation files (the "Software"), to deal in the Software without// restriction, including without limitation the rights to use, copy, modify, merge, publish,// distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the// Software is furnished to do so, subject to the following conditions://// The above copyright notice and this permission notice shall be included in all copies or// substantial portions of the Software.//// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING// BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
/** * @typedef {modlumi.Group | modlumi.ComponentInstance} Container * @typedef {{ * instance: Container, * middle: modlumi.Point3d, * xAxis: modlumi.Vector3d, * normal: modlumi.Vector3d, * zAxis: modlumi.Vector3d, * length: number, * handle: modlumi.Point3d, * size: number, * edges: modlumi.Point3d[], * }} Setup * What a bend works from, in the coordinates of the context being edited: the reference line * through `middle` along the instance's red axis, `length` long; the `normal` a positive bend * curls toward; the handle on the instance's front; and the instance's edges, two points each. */
const model = modlumi.activeModel;const { Point3d, Vector3d } = modlumi;const kTau = Math.PI * 2;/** @type {modlumi.Color} */const kReferenceColor = { r: 0, g: 0.6, b: 0, a: 1 };/** @type {modlumi.Color} */const kHandleColor = { r: 0.9, g: 0, b: 0, a: 1 };/** @type {modlumi.Color} */const kPreviewColor = { r: 0.5, g: 0, b: 0, a: 1 };
/** @param {Container} container */function getContents(container) { return container.type === "Group" ? /** @type {modlumi.Group} */ (container).entities : /** @type {modlumi.ComponentInstance} */ (container).definition.entities;}
/** @param {modlumi.Entities} entities */function getContainers(entities) { return /** @type {Container[]} */ ([...entities.ofType("Group"), ...entities.ofType("ComponentInstance")]);}
/** * Adds the edges of a container's contents, and of the containers inside it, to points as pairs. * @param {Container} container * @param {modlumi.Transformation} transformation * @param {modlumi.Point3d[]} points */function collectEdges(container, transformation, points) { const entities = getContents(container); for (const edge of /** @type {modlumi.Edge[]} */ (entities.ofType("Edge"))) { if (!edge.hidden) { points.push(edge.start.position.transform(transformation), edge.end.position.transform(transformation)); } } for (const child of getContainers(entities)) { collectEdges(child, transformation.multiply(child.transformation), points); }}
/** * Prepares to bend a container, or returns null when it can't be bent. * @param {Container} instance * @returns {Setup | null} */function prepare(instance) { const bounds = getContents(instance).bounds; const transformation = instance.transformation; if (bounds === null) { return null; } const { min, max } = bounds; /** @param {number} value @param {number} low @param {number} high */ const clamp = (value, low, high) => Math.min(high, Math.max(low, value)); // The reference line runs along the red axis through the origin, or the nearest point of // the bounds when the origin lies outside them. const y = clamp(0, min.y, max.y); const z = clamp(0, min.z, max.z); const start = new Point3d(min.x, y, z).transform(transformation); const end = new Point3d(max.x, y, z).transform(transformation); const length = start.distance(end); if (length <= 1e-9) { return null; } const xAxis = start.vectorTo(end).normalize(); // The front of the instance faces down its green axis. const front = new Vector3d(0, -1, 0).transform(transformation); const across = front.subtract(xAxis.scale(front.dot(xAxis))); if (across.length <= 1e-9) { return null; } const normal = across.normalize(); const corners = [0, 1, 2, 3, 4, 5, 6, 7].map((corner) => new Point3d(corner & 1 ? max.x : min.x, corner & 2 ? max.y : min.y, corner & 4 ? max.z : min.z).transform(transformation)); /** @type {modlumi.Point3d[]} */ const edges = []; collectEdges(instance, transformation, edges); return { instance, middle: start.offset(xAxis.scale(length / 2)), xAxis, normal, zAxis: xAxis.cross(normal), length, handle: new Point3d((min.x + max.x) / 2, min.y, (min.z + max.z) / 2).transform(transformation), size: corners[0].distance(corners[7]), edges, };}
/** @param {modlumi.PluginContext} context */export function activate(context) { /** @type {Setup | null} */ let setup = null; /** The handle's offset along the normal, in meters; positive curls toward the front. */ let amount = 0; /** @type {{ start: modlumi.Point3d, base: number } | null} Where a drag started. */ let dragging = null; /** Whether the next release is the end of a double-click that applied the bend. */ let releaseApplied = false; let subdivisions = Number(context.settings.get("subdivisions", 24)); let segmented = Boolean(context.settings.get("segmented", true)); let softSmooth = Boolean(context.settings.get("softSmooth", true));
// The largest bend, a full circle, moves the handle by the circle's diameter. function getMaxAmount() { return setup === null ? 0 : (2 * setup.length) / kTau; }
function getAngle() { const max = getMaxAmount(); return max <= 0 ? 0 : (kTau * Math.min(Math.abs(amount), max)) / max; }
function isBending() { return getAngle() > 1e-9; }
/** * Where a point goes when the reference line bends by angle around a circle that keeps its * length, as one chord per subdivision when segmented. * @param {modlumi.Point3d} point */ function bendPoint(point) { const { middle, xAxis, normal, zAxis, length } = /** @type {Setup} */ (setup); const angle = getAngle(); const side = amount > 0 ? 1 : -1; const radius = length / angle; const offset = middle.vectorTo(point); const along = offset.dot(xAxis) / radius; let distance = radius - side * offset.dot(normal); if (segmented) { const step = angle / subdivisions; const index = Math.min(subdivisions - 1, Math.max(0, Math.floor((along + angle / 2) / step))); const chordMiddle = -angle / 2 + (index + 0.5) * step; distance = (distance * Math.cos(step / 2)) / Math.cos(along - chordMiddle); } const center = middle.offset(normal.scale(side * radius)); return center.offset(normal.scale(-side * distance * Math.cos(along)) .add(xAxis.scale(distance * Math.sin(along))) .add(zAxis.scale(offset.dot(zAxis)))); }
// Offsets of the slicing planes along the reference line, from its middle. function getPlaneOffsets() { const { length } = /** @type {Setup} */ (setup); return Array.from({ length: subdivisions - 1 }, (_, index) => -length / 2 + ((index + 1) * length) / subdivisions); }
/** * Splits the pairs of points at the slicing planes, so straight edges show the curve. * @param {modlumi.Point3d[]} pairs */ function slice(pairs) { const { middle, xAxis } = /** @type {Setup} */ (setup); const planes = getPlaneOffsets(); /** @type {modlumi.Point3d[]} */ const result = []; for (let index = 0; index + 1 < pairs.length; index += 2) { const a = pairs[index]; const b = pairs[index + 1]; const ua = middle.vectorTo(a).dot(xAxis); const ub = middle.vectorTo(b).dot(xAxis); const crossings = planes.filter((u) => (u - ua) * (u - ub) < 0) .map((u) => (u - ua) / (ub - ua)) .sort((s, t) => s - t); let previous = a; for (const t of crossings) { const point = a.offset(a.vectorTo(b).scale(t)); result.push(previous, point); previous = point; } result.push(previous, b); } return result; }
function showState() { tool.status = setup === null ? "Click a group or component to bend it." : dragging !== null ? "Move to adjust the bend, then click. Type an angle, a distance, or segments like 24s." : "Click and move to bend, or type an angle. Press Enter or double-click to apply."; tool.measurement = { label: "Angle", value: setup === null ? "" : `${((getAngle() * 180) / Math.PI).toFixed(1)}°`, }; tool.reference = dragging === null || setup === null ? null : setup.handle.offset(setup.normal.scale(amount)); }
/** @param {Container | null} instance */ function choose(instance) { setup = instance === null ? null : prepare(instance); amount = 0; dragging = null; showState(); }
function chooseFromSelection() { const selected = model.selection.toArray().find((entity) => entity.type === "Group" || entity.type === "ComponentInstance"); choose(/** @type {Container | undefined} */ (selected) ?? null); }
// Bends the instance in one undo step: slices it at the planes, then moves every vertex. function commit() { if (setup === null || !isBending()) { return; } const { instance, middle, xAxis, normal, zAxis, size } = setup; const planes = getPlaneOffsets(); try { model.operation("Bend", () => { instance.makeUnique(); /** @type {{ entities: modlumi.Entities, transformation: modlumi.Transformation }[]} */ const contexts = []; /** @param {Container} container @param {modlumi.Transformation} transformation */ const collect = (container, transformation) => { const entities = getContents(container); contexts.push({ entities, transformation }); for (const child of getContainers(entities)) { child.makeUnique(); collect(child, transformation.multiply(child.transformation)); } }; collect(instance, instance.transformation);
// Large squares across the instance, in a group beside it so they don't cut themselves. const cutter = model.activeEntities.addGroup(); const squares = planes.map((u) => { const center = middle.offset(xAxis.scale(u)); const a = normal.scale(size); const b = zAxis.scale(size); return cutter.entities.addFace([center.offset(a.add(b)), center.offset(a.subtract(b)), center.offset(a.negate().subtract(b)), center.offset(a.negate().add(b))]); }); for (const { entities, transformation } of contexts) { const toLocal = transformation.inverse(); const before = new Set(entities.ofType("Face").map((face) => face.entityId)); const cut = entities.intersectFaces(entities.ofType("Face"), squares.map((face) => ({ face, transformation: toLocal }))); const cutIds = new Set(cut.map((edge) => edge.entityId)); // Cut edges that close a loop across the inside fill it; such faces aren't wanted. for (const face of /** @type {modlumi.Face[]} */ (entities.ofType("Face"))) { if (!before.has(face.entityId) && face.loops.every((loop) => loop.edges.every((edge) => cutIds.has(edge.entityId)))) { entities.erase(face); } } if (!segmented || softSmooth) { for (const edge of cut) { if (edge.isValid) { edge.soft = true; edge.smooth = true; } } } // Loose edges cross no face, so they are split at the planes by hand, last crossing // first so the remaining part keeps starting where the edge did. for (const edge of /** @type {modlumi.Edge[]} */ (entities.ofType("Edge"))) { if (edge.faces.length > 0) { continue; } const ua = middle.vectorTo(edge.start.position.transform(transformation)).dot(xAxis); const ub = middle.vectorTo(edge.end.position.transform(transformation)).dot(xAxis); const crossings = planes.filter((u) => (u - ua) * (u - ub) < 0) .map((u) => (u - ua) / (ub - ua)) .sort((s, t) => t - s); let rest = edge; let length = 1; for (const t of crossings) { // The remaining part runs from the edge's start to the previous crossing. rest = entities.splitEdge(rest, t / length)[0]; length = t; } } } model.activeEntities.erase(cutter);
for (const { entities, transformation } of contexts) { const toLocal = transformation.inverse(); /** @type {Map<number, modlumi.Vertex>} */ const vertices = new Map(); for (const edge of /** @type {modlumi.Edge[]} */ (entities.ofType("Edge"))) { vertices.set(edge.start.entityId, edge.start); vertices.set(edge.end.entityId, edge.end); } const moving = [...vertices.values()]; const offsets = moving.map((vertex) => vertex.position.vectorTo(bendPoint(vertex.position.transform(transformation)).transform(toLocal))); if (moving.length > 0) { entities.moveVertices(moving, offsets); } } }); } catch (error) { context.showMessage(`Can't bend it: ${error instanceof Error ? error.message : error}`); return; } choose(instance.isValid ? instance : null); }
/** @param {string} text */ function applyValue(text) { const trimmed = text.trim(); const segments = /^(\d+)\s*s$/i.exec(trimmed); if (segments !== null) { subdivisions = Math.min(360, Math.max(2, Number(segments[1]))); context.settings.set("subdivisions", subdivisions); return true; } if (setup === null) { return false; } const side = amount < 0 ? -1 : 1; const degrees = /^(-?[\d.,]+)\s*(deg|°)?$/i.exec(trimmed); if (degrees !== null) { const angle = (Number(degrees[1].replace(",", ".")) * Math.PI) / 180; if (!Number.isFinite(angle) || angle <= 0 || angle > kTau) { return false; } amount = (side * getMaxAmount() * angle) / kTau; return true; } const distance = model.parseLength(trimmed); if (distance === null || distance <= 0) { return false; } amount = side * Math.min(distance, getMaxAmount()); return true; }
const tool = context.registerTool({ id: "bend", title: "True Bend", tip: "Bends a group or component to an angle, along a true or segmented curve.", activate: chooseFromSelection, deactivate() { setup = null; dragging = null; releaseApplied = false; }, onMouseMove(event) { if (setup !== null && dragging !== null && event.point !== null) { const moved = dragging.start.vectorTo(event.point.position).dot(setup.normal); amount = Math.max(-getMaxAmount(), Math.min(getMaxAmount(), dragging.base + moved)); showState(); } }, onMouseUp(event) { if (releaseApplied) { releaseApplied = false; return; } if (dragging !== null) { dragging = null; showState(); return; } // Before bending, a click chooses what to bend. const picked = event.picked?.path[model.activePath.length] ?? null; if (setup === null || (amount === 0 && picked !== null && picked.entityId !== setup.instance.entityId)) { model.selection.clear(); if (picked !== null) { model.selection.add(picked); } choose(picked); return; } if (event.point !== null) { dragging = { start: event.point.position, base: amount }; showState(); } }, onDoubleClick() { // The double-click's own release follows; it ends the gesture rather than starting another. releaseApplied = true; commit(); }, onKeyDown(event) { if (event.key === "enter") { commit(); } }, onValue(text) { if (!applyValue(text)) { context.showMessage(`Can't bend by "${text}"`); } showState(); }, onCancel(reason) { if (dragging !== null) { amount = dragging.base; dragging = null; } else if (reason === "escape") { amount = 0; } showState(); }, options(ui) { if (setup === null) { ui.text("Click a group or component to bend it.", { tone: "muted" }); return; } const chosen = ui.integer("Segments", subdivisions, { min: 2, max: 360 }); if (chosen !== subdivisions) { subdivisions = chosen; context.settings.set("subdivisions", subdivisions); } const chord = ui.checkbox("Segmented", segmented, { tip: "Bend along one straight segment per slice." }); if (chord !== segmented) { segmented = chord; context.settings.set("segmented", segmented); } const soften = ui.checkbox("Soften segment edges", segmented ? softSmooth : true, { disabled: !segmented }); if (segmented && soften !== softSmooth) { softSmooth = soften; context.settings.set("softSmooth", softSmooth); } const angle = getAngle(); ui.readOnly("Angle", `${((angle * 180) / Math.PI).toFixed(1)}°`); ui.readOnly("Radius", isBending() ? model.formatLength(setup.length / angle) : "Straight"); ui.row(() => { if (ui.button("Reset", { disabled: !isBending() })) { amount = 0; dragging = null; showState(); } if (ui.button("Bend", { primary: true, disabled: !isBending() })) { commit(); } }); }, draw(view) { if (setup === null) { return; } showState(); const { middle, xAxis, normal, length, handle, size } = setup; const reference = Array.from({ length: subdivisions + 1 }, (_, index) => middle.offset(xAxis.scale(-length / 2 + (index * length) / subdivisions))); const tip = handle.offset(normal.scale(amount)); view.lines([handle.offset(normal.scale(-size / 2)), handle.offset(normal.scale(size / 2))], { color: kHandleColor, stipple: "dashed" }); view.lines([handle, tip], { color: kHandleColor, width: 2 }); view.points([tip], { color: kHandleColor, width: 10 }); if (!isBending()) { view.polyline(reference, { color: kReferenceColor, width: 2 }); view.points(reference, { color: kReferenceColor, width: 5 }); return; } const bent = reference.map(bendPoint); view.polyline(bent, { color: kReferenceColor, width: 2 }); view.points(bent, { color: kReferenceColor, width: 5 }); view.lines(slice(setup.edges).map(bendPoint), { color: kPreviewColor, width: 1 }); }, });
// Undo, redo and edits elsewhere may change or remove what is being bent. const refresh = () => { if (setup !== null && dragging === null) { choose(setup.instance.isValid ? setup.instance : null); } }; context.onModelChanged(refresh); // The target and its frame belong to the context being edited, like the selection. context.onActiveContextChanged(chooseFromSelection); context.onDocumentOpened(() => choose(null));}