58ce95a443
1. The "Next" suggestions after a step are gone: in the way, and at odds with the guide text. 2. A new sketch starts with Line on (it did, except a new project's, which started on Rectangle). 3. Drag arrows are slimmer and shorter (64 on screen, from 84; a 3 px shaft with a dark edge under it so it reads over a lit face and the grid; a head the same size whatever the arrow's length). A double arrow - Offset in a sketch, Offset Edge on the model - is two short ones back to back: 68 across, from 168. 4. Move: the dot in the middle of the gimbal is dragged to move the point everything turns about - onto a corner, an edge or a face (it snaps, and says which), or anywhere. The body stays where it is. (It moved the body across the screen; the body itself is dragged for that now.) 5. In a sketch seen at a slant, a double tap on something drawn turns the view to face the sketch. 6. A box drawn in a sketch picks every curve it touches, not only those wholly inside it. 7. A pattern with no direction yet: pointing at an axis, an edge or a face shows the row of copies along it in dashed yellow; a circular one, the ring round the axis pointed at. 8. No line from the pick to its name: the name, with its +, says what is picked. 9. Looks ahead: the pointer over a tool draws what it would do to the pick, in dashed yellow on the model, before anything starts - a face's outline lifted (Extrude, Offset Face) or sunk (Shell), a sketch's shape lifted, the lines a Fillet or Chamfer runs between on a straight edge, a row or a ring of boxes for a pattern. Drawn from what the view already has (tools/ghost.ts), so it is there at once. Tools with no telling picture show none; a finger has no hover. 10. With something picked, a group's list holds the pick's own tools and, under them in a quieter part, the group's other tools (each asks for its own pick as it starts) - every tool is there, and a group is dimmed only when it has none at all. Tried in the app: each of the ten (new project on Line; a box crossing a rectangle's side and a circle picks both; the view from three-quarter to straight on; the centre dot onto a corner with the body unmoved; dashed copies along X then Y; hover on Pattern - Linear in the open list; Modify with a face picked: six of its own, four others). Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
203 lines
9.3 KiB
TypeScript
203 lines
9.3 KiB
TypeScript
// A look ahead, drawn on the model in dashed yellow: what a tool would do to
|
|
// what is picked, shown while the pointer is over the tool (before anything
|
|
// is started) - a face's outline lifted for Extrude, the lines a round would
|
|
// run between for Fillet, a row of boxes for a pattern. Nothing is computed
|
|
// by the kernel for it: it is a sketch of the change, from what the view
|
|
// already has, so it comes up at once.
|
|
|
|
import { BufferGeometry, Float32BufferAttribute, LineDashedMaterial, LineSegments } from "three";
|
|
import type { Vec3 } from "../doc/types";
|
|
import { add, cross, dot, len, norm, scale, sub, toWorld } from "../geom";
|
|
import { entityPolylines } from "../sketch/curves";
|
|
import type { Viewport } from "../view/viewport";
|
|
import { edgesOfFace, sourceFrame, sourcesOf, type AppContext, type Sel } from "./tool";
|
|
|
|
const YELLOW = 0xffd23a;
|
|
|
|
/** Line segments (pairs of points, flat) in dashed yellow, over everything. Dashes keep their size on screen. */
|
|
export class Ghost {
|
|
private mat = new LineDashedMaterial({ color: YELLOW, depthTest: false, transparent: true, opacity: 0.95, dashSize: 1, gapSize: 1 });
|
|
private line = new LineSegments(new BufferGeometry(), this.mat);
|
|
private wpp = 0;
|
|
private onFrame = () => this.dashes();
|
|
|
|
constructor(private view: Viewport, segs: number[]) {
|
|
this.line.renderOrder = 48;
|
|
this.line.frustumCulled = false;
|
|
this.line.geometry.setAttribute("position", new Float32BufferAttribute(segs, 3));
|
|
this.line.computeLineDistances();
|
|
this.dashes();
|
|
view.overlay.add(this.line);
|
|
view.frameHooks.add(this.onFrame);
|
|
view.helpers.add(this);
|
|
view.requestRender();
|
|
}
|
|
|
|
private dashes(): void {
|
|
const w = this.view.cam.worldPerPixel(this.view.height);
|
|
if (Math.abs(w - this.wpp) < this.wpp * 0.02) return;
|
|
this.wpp = w;
|
|
this.mat.dashSize = w * 7;
|
|
this.mat.gapSize = w * 5;
|
|
}
|
|
|
|
dispose(): void {
|
|
this.view.helpers.delete(this);
|
|
this.view.frameHooks.delete(this.onFrame);
|
|
this.view.overlay.remove(this.line);
|
|
this.line.geometry.dispose();
|
|
this.mat.dispose();
|
|
this.view.requestRender();
|
|
}
|
|
}
|
|
|
|
type Segs = number[];
|
|
const seg = (out: Segs, a: Vec3, b: Vec3) => void out.push(a[0], a[1], a[2], b[0], b[1], b[2]);
|
|
const moved = (segs: Segs, v: Vec3): Segs => segs.map((x, i) => x + v[i % 3]);
|
|
|
|
/** The outline of a body's face, as the view drew its edges. */
|
|
function faceOutline(app: AppContext, body: string, face: number): Segs {
|
|
const m = app.view.body(body);
|
|
if (!m) return [];
|
|
const out: Segs = [];
|
|
const of = new Set(edgesOfFace(app, body, face));
|
|
for (const g of m.edgeGroups) if (of.has(g.edge)) for (let j = g.start * 3; j < (g.start + g.count) * 3; j++) out.push(m.edgePositions[j]);
|
|
return out;
|
|
}
|
|
|
|
/** The box round some bodies: its twelve edges, and its middle. */
|
|
export function bodiesBox(app: AppContext, ids: string[]): { lo: Vec3; hi: Vec3; mid: Vec3; segs: Segs } | null {
|
|
const lo: Vec3 = [Infinity, Infinity, Infinity];
|
|
const hi: Vec3 = [-Infinity, -Infinity, -Infinity];
|
|
for (const id of ids) {
|
|
const p = app.view.body(id)?.positions;
|
|
if (!p) continue;
|
|
for (let i = 0; i < p.length; i++) {
|
|
const k = i % 3;
|
|
if (p[i] < lo[k]) lo[k] = p[i];
|
|
if (p[i] > hi[k]) hi[k] = p[i];
|
|
}
|
|
}
|
|
if (!Number.isFinite(lo[0])) return null;
|
|
const c = (i: number): Vec3 => [i & 1 ? hi[0] : lo[0], i & 2 ? hi[1] : lo[1], i & 4 ? hi[2] : lo[2]];
|
|
const segs: Segs = [];
|
|
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]]) seg(segs, c(a), c(b));
|
|
return { lo, hi, mid: scale(add(lo, hi), 0.5), segs };
|
|
}
|
|
|
|
/** A row of copies: the bodies' box again and again along `dir`, and the line they go along. */
|
|
export function rowGhost(app: AppContext, ids: string[], dir: Vec3, spacing: number, count: number): Segs {
|
|
const box = bodiesBox(app, ids);
|
|
if (!box) return [];
|
|
const d = norm(dir);
|
|
const out: Segs = [];
|
|
for (let k = 1; k < count; k++) out.push(...moved(box.segs, scale(d, spacing * k)));
|
|
seg(out, box.mid, add(box.mid, scale(d, spacing * (count - 1))));
|
|
return out;
|
|
}
|
|
|
|
/** A ring of copies: the bodies' box turned about the axis, and the circle its middle goes round. */
|
|
export function ringGhost(app: AppContext, ids: string[], axis: { origin: Vec3; dir: Vec3 }, angle: number, count: number): Segs {
|
|
const box = bodiesBox(app, ids);
|
|
if (!box) return [];
|
|
const d = norm(axis.dir);
|
|
const turn = (p: Vec3, deg: number): Vec3 => {
|
|
const a = (deg * Math.PI) / 180;
|
|
const v = sub(p, axis.origin);
|
|
const along = scale(d, dot(v, d));
|
|
const flat = sub(v, along);
|
|
return add(axis.origin, add(along, add(scale(flat, Math.cos(a)), scale(cross(d, flat), Math.sin(a)))));
|
|
};
|
|
const full = Math.abs(Math.abs(angle) - 360) < 1e-6;
|
|
const step = angle / (full ? count : Math.max(1, count - 1));
|
|
const out: Segs = [];
|
|
for (let k = 1; k < count; k++) for (let i = 0; i < box.segs.length; i += 3) out.push(...turn([box.segs[i], box.segs[i + 1], box.segs[i + 2]], step * k));
|
|
// (The circle: only where the bodies are off the axis.)
|
|
if (len(sub(box.mid, turn(box.mid, 180))) > 1e-6) for (let k = 0; k < 48; k++) seg(out, turn(box.mid, (angle * k) / 48), turn(box.mid, (angle * (k + 1)) / 48));
|
|
return out;
|
|
}
|
|
|
|
/**
|
|
* What the tool `id` would do to what is picked, as dashed lines - or nothing,
|
|
* where there is no telling picture of it (most tools: those say what they do
|
|
* by name).
|
|
*/
|
|
export function peekSegs(app: AppContext, id: string, sel: Sel[]): Segs {
|
|
const s = sel[0];
|
|
if (!s) return [];
|
|
const out: Segs = [];
|
|
if (s.kind === "face" && (id === "extrude" || id === "offsetFace" || id === "offset" || id === "shell" || id === "thicken")) {
|
|
const f = app.view.body(s.body)?.faces[s.face];
|
|
if (!f) return [];
|
|
const outline = faceOutline(app, s.body, s.face);
|
|
// (Out by about a third of the face's size; Shell: in, by a wall.)
|
|
const d = Math.sqrt(f.area) * (id === "shell" ? -0.12 : 0.35);
|
|
const v = scale(f.normal, d);
|
|
out.push(...moved(outline, v));
|
|
// (Up from the outline's corners - every few points of a round one.)
|
|
const stride = Math.max(1, Math.floor(outline.length / 6 / 12));
|
|
for (let i = 0; i < outline.length; i += 6 * stride) seg(out, [outline[i], outline[i + 1], outline[i + 2]], add([outline[i], outline[i + 1], outline[i + 2]], v));
|
|
return out;
|
|
}
|
|
if (s.kind === "profile" && (id === "extrude" || id === "revolve")) {
|
|
if (id !== "extrude") return [];
|
|
const src = sourcesOf(app, sel)[0];
|
|
const plane = app.sketchPlane(s.sketch);
|
|
if (!src || !plane) return [];
|
|
const fr = sourceFrame(app, src);
|
|
const flat: Segs = [];
|
|
for (const e of app.sketchCurves(s.sketch)) {
|
|
if (e.construction) continue;
|
|
for (const poly of entityPolylines(e)) for (let i = 0; i + 1 < poly.length; i++) seg(flat, toWorld(plane, poly[i]), toWorld(plane, poly[i + 1]));
|
|
}
|
|
let size = 0;
|
|
for (let i = 0; i < flat.length; i += 3) size = Math.max(size, len(sub([flat[i], flat[i + 1], flat[i + 2]], fr.base)));
|
|
const v = scale(fr.normal, Math.max(size * 0.6, 1e-3));
|
|
out.push(...moved(flat, v));
|
|
const stride = Math.max(1, Math.floor(flat.length / 6 / 12));
|
|
for (let i = 0; i < flat.length; i += 6 * stride) seg(out, [flat[i], flat[i + 1], flat[i + 2]], add([flat[i], flat[i + 1], flat[i + 2]], v));
|
|
return out;
|
|
}
|
|
if (s.kind === "edge" && (id === "fillet" || id === "chamfer")) {
|
|
const m = app.view.body(s.body);
|
|
if (!m || m.faces.length > 400) return [];
|
|
for (const one of sel) {
|
|
if (one.kind !== "edge" || one.body !== s.body) continue;
|
|
const e = m.edges[one.edge];
|
|
if (!e || e.type !== "LINE") continue;
|
|
// The two flat faces the edge is between, and the way into each from the edge.
|
|
const into: Vec3[] = [];
|
|
for (let f = 0; f < m.faces.length && into.length < 2; f++) {
|
|
const face = m.faces[f];
|
|
if (!face.plane || Math.abs(dot(face.normal, e.dir)) > 1e-3 || Math.abs(dot(sub(e.mid, face.center), face.normal)) > 1e-4) continue;
|
|
if (!edgesOfFace(app, s.body, f).includes(one.edge)) continue;
|
|
const side = norm(cross(face.normal, e.dir));
|
|
into.push(dot(side, sub(face.center, e.mid)) < 0 ? scale(side, -1) : side);
|
|
}
|
|
if (into.length < 2) continue;
|
|
const r = Math.min(e.length * 0.25, Math.sqrt(Math.min(...m.faces.map((x) => x.area))) * 0.5);
|
|
const a = (p: Vec3) => add(p, scale(into[0], r));
|
|
const b = (p: Vec3) => add(p, scale(into[1], r));
|
|
seg(out, a(e.start), a(e.end));
|
|
seg(out, b(e.start), b(e.end));
|
|
for (const p of [e.start, e.end]) {
|
|
if (id === "chamfer") seg(out, a(p), b(p));
|
|
else {
|
|
// (The round's end: from one line to the other, bowed towards the edge.)
|
|
const q = (t: number): Vec3 => add(add(scale(a(p), (1 - t) * (1 - t)), scale(p, 2 * t * (1 - t) * 0.83 + 0)), add(scale(add(a(p), b(p)), t * (1 - t) * 0.17), scale(b(p), t * t)));
|
|
for (let k = 0; k < 8; k++) seg(out, q(k / 8), q((k + 1) / 8));
|
|
}
|
|
}
|
|
}
|
|
return out;
|
|
}
|
|
if (s.kind === "body" && (id === "patl" || id === "patc")) {
|
|
const ids = sel.flatMap((x) => (x.kind === "body" ? [x.body] : []));
|
|
const box = bodiesBox(app, ids);
|
|
if (!box) return [];
|
|
if (id === "patl") return rowGhost(app, ids, [1, 0, 0], (box.hi[0] - box.lo[0]) * 1.5 || 10, 3);
|
|
return ringGhost(app, ids, { origin: [0, 0, 0], dir: [0, 0, 1] }, 360, 6);
|
|
}
|
|
return out;
|
|
}
|