| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640 |
- /*
- 3D Modeler - drawing tools
- What the pointer does on the sheet while a drawing command is active
- (editor.js hands it over). Each tool is
- { name, prompt, move(p), down(p, event), key(event) -> handled,
- overlay() -> { items, edges, marks, frames }, cancel(rightClick) }
- with p a sheet point (mm).
- The Dimension tool works like a desktop CAD general dimension: what is
- clicked decides what is measured.
- a circle its diameter (an arc: its radius); R / D switch
- a straight edge its length, or click a second straight edge:
- the angle between them, or the distance when
- they are parallel
- a point (an end, a middle, a centre) and a second point
- or edge: the distance between them
- then a last click places the text. A linear dimension is horizontal,
- vertical or aligned by where the pointer is; Space cycles through them.
- The Radius, Diameter, Angle, Hole Note and Center Mark buttons start the
- same tool limited to what they measure.
- ctx (from drawing.js):
- editor, data(), projections(), newId(prefix), commit(label, fn),
- setStatus(text), setPrompt(text), estimate(view) (a local box),
- editText(draft) (opens the text panel for a new note or text),
- done() (the tool ended by itself)
- */
- import { annotationItems, scaleOf, toSheet, toLocal, frameOf } from './layout.js';
- import { projectedFrame, sideFor, orientationKey, ORIENTATIONS } from './sheet.js';
- import * as G2 from './geometry2d.js';
- const NAMES = {
- linear: 'Dimension', radius: 'Radius', diameter: 'Diameter', angle: 'Angle', hole: 'Hole Note',
- center: 'Center Mark', note: 'Leader Note', text: 'Text'
- };
- function viewById(data, id) {
- return data.views.filter(function (v) {
- return v.id === id;
- })[0] || null;
- }
- function sheetPoint(data, viewId, local) {
- const v = viewById(data, viewId);
- return v ? toSheet(v, scaleOf(data, v), local) : local;
- }
- function edgeOnSheet(data, viewId, edge) {
- const v = viewById(data, viewId);
- if (!v) {
- return [];
- }
- const s = scaleOf(data, v);
- if (edge.type === 'circle' && edge.closed) {
- const pts = [];
- for (let i = 0; i <= 64; i++) {
- const t = i / 64 * 2 * Math.PI;
- pts.push(toSheet(v, s, [edge.c[0] + edge.r * Math.cos(t), edge.c[1] + edge.r * Math.sin(t)]));
- }
- return pts;
- }
- const pts = [];
- for (let i = 0; i < edge.pts.length; i += 2) {
- pts.push(toSheet(v, s, [edge.pts[i], edge.pts[i + 1]]));
- }
- return pts;
- }
- // What a pointer hit is, as something to dimension
- function pickOf(hit) {
- if (!hit) {
- return null;
- }
- if (hit.snap) {
- return { kind: 'point', view: hit.view, p: hit.snap.p, snap: hit.snap.kind };
- }
- if (hit.edge) {
- return { kind: hit.edge.type === 'circle' ? 'circle' : hit.edge.type === 'line' ? 'line' : 'curve', view: hit.view, edge: hit.edge, local: hit.local };
- }
- return { kind: 'free', view: hit.view, p: hit.local };
- }
- function isHorizontal(e) {
- return Math.abs(e.a[1] - e.b[1]) < 1e-6;
- }
- function isVertical(e) {
- return Math.abs(e.a[0] - e.b[0]) < 1e-6;
- }
- // The orientation measuring square to a line: across a horizontal line is vertical
- function across(e) {
- return isHorizontal(e) ? 'v' : isVertical(e) ? 'h' : 'a';
- }
- function along(e) {
- return isHorizontal(e) ? 'h' : isVertical(e) ? 'v' : 'a';
- }
- // Horizontal, vertical or aligned, by where the pointer is around two points
- function orientFor(p1, p2, q) {
- if (Math.abs(p1[1] - p2[1]) < 1e-9) {
- return 'h';
- }
- if (Math.abs(p1[0] - p2[0]) < 1e-9) {
- return 'v';
- }
- const insideX = q[0] > Math.min(p1[0], p2[0]) && q[0] < Math.max(p1[0], p2[0]);
- const insideY = q[1] > Math.min(p1[1], p2[1]) && q[1] < Math.max(p1[1], p2[1]);
- if (insideX && !insideY) {
- return 'h';
- }
- if (insideY && !insideX) {
- return 'v';
- }
- return 'a';
- }
- // The foot of p on the (endless) line through edge e
- function foot(p, e) {
- const d = [e.b[0] - e.a[0], e.b[1] - e.a[1]];
- const l2 = d[0] * d[0] + d[1] * d[1];
- const t = l2 ? ((p[0] - e.a[0]) * d[0] + (p[1] - e.a[1]) * d[1]) / l2 : 0;
- return [e.a[0] + t * d[0], e.a[1] + t * d[1]];
- }
- function parallel(e1, e2) {
- const d1 = [e1.b[0] - e1.a[0], e1.b[1] - e1.a[1]];
- const d2 = [e2.b[0] - e2.a[0], e2.b[1] - e2.a[1]];
- const l = Math.hypot(d1[0], d1[1]) * Math.hypot(d2[0], d2[1]);
- return !l || Math.abs(d1[0] * d2[1] - d1[1] * d2[0]) / l < 1e-6;
- }
- // The point of an edge nearest to p (view coordinates)
- function nearestOnEdge(edge, p) {
- if (edge.type === 'circle' && edge.closed) {
- const d = [p[0] - edge.c[0], p[1] - edge.c[1]];
- const l = Math.hypot(d[0], d[1]) || 1;
- return [edge.c[0] + d[0] / l * edge.r, edge.c[1] + d[1] / l * edge.r];
- }
- let best = null;
- for (let i = 2; i < edge.pts.length; i += 2) {
- const n = G2.nearestOnSegment(p, [edge.pts[i - 2], edge.pts[i - 1]], [edge.pts[i], edge.pts[i + 1]]);
- if (!best || n.d < best.d) {
- best = n;
- }
- }
- return best ? best.point : p;
- }
- export function createDrawingTools(ctx) {
- const editor = ctx.editor;
- function hoverMarks(pick) {
- const data = ctx.data();
- const out = { marks: [], edges: [] };
- if (!pick) {
- return out;
- }
- if (pick.kind === 'point') {
- out.marks.push({ p: sheetPoint(data, pick.view, pick.p), kind: pick.snap || 'end' });
- } else if (pick.edge) {
- out.edges.push(edgeOnSheet(data, pick.view, pick.edge));
- if (pick.kind === 'circle') {
- out.marks.push({ p: sheetPoint(data, pick.view, pick.edge.c), kind: 'center' });
- }
- }
- return out;
- }
- /* ================= dimensions ================= */
- function dimension(mode) {
- let picks = [];
- let hovered = null;
- let draft = null;
- let forced = null; // a linear orientation chosen with Space
- let circleKind = null; // radius or diameter chosen with R / D
- const accepts = function (pick) {
- if (!pick) {
- return false;
- }
- if (mode === 'angle') {
- return pick.kind === 'line';
- }
- if (mode === 'radius' || mode === 'diameter' || mode === 'hole' || mode === 'center') {
- return pick.kind === 'circle';
- }
- return pick.kind === 'point' || pick.kind === 'line' || pick.kind === 'circle';
- };
- const firstPrompt = {
- auto: 'Dimension: click an edge, a circle or a point of a view',
- radius: 'Radius: click a circle or an arc',
- diameter: 'Diameter: click a circle or an arc',
- hole: 'Hole Note: click a hole (a circle)',
- angle: 'Angle: click two straight edges',
- center: 'Center Mark: click circles to mark their centres'
- }[mode];
- function prompt() {
- if (!picks.length) {
- ctx.setPrompt(firstPrompt);
- return;
- }
- const first = picks[0];
- if (first.kind === 'circle' || picks.length === 2) {
- ctx.setPrompt('Click to place the text' + (first.kind === 'circle' && mode === 'auto' ? ' (R: radius, D: diameter)' : '') + '. Esc starts again.');
- } else if (first.kind === 'line') {
- ctx.setPrompt(mode === 'angle' ? 'Angle: click the second straight edge' : 'Click to place the length, or a second edge (angle or distance), or a point');
- } else {
- ctx.setPrompt('Click a second point, edge or circle');
- }
- }
- function linearDraft(view, p1, p2, at, natural) {
- return { type: 'linear', view: view, p1: p1.slice(), p2: p2.slice(), orient: forced || natural || orientFor(p1, p2, at), at: at };
- }
- function twoLines(view, e1, e2, at) {
- if (parallel(e1, e2)) {
- const mid = [(e1.a[0] + e1.b[0]) / 2, (e1.a[1] + e1.b[1]) / 2];
- return linearDraft(view, mid, foot(mid, e2), at, across(e1));
- }
- return { type: 'angle', view: view, l1: [e1.a.slice(), e1.b.slice()], l2: [e2.a.slice(), e2.b.slice()], at: at };
- }
- function makeDraft(p) {
- if (!picks.length) {
- return null;
- }
- const data = ctx.data();
- const first = picks[0];
- const v = viewById(data, first.view);
- if (!v) {
- return null;
- }
- const at = toLocal(v, scaleOf(data, v), p);
- if (first.kind === 'circle') {
- const type = mode === 'auto' ? (circleKind || (first.edge.closed ? 'diameter' : 'radius')) : mode;
- const out = { type: type, view: v.id, c: first.edge.c.slice(), r: first.edge.r, at: at };
- if (type === 'hole') {
- const proj = ctx.projections()[v.id];
- out.count = proj ? Math.max(1, G2.countCircles(proj.edges, first.edge.r)) : 1;
- }
- return out;
- }
- const second = picks[1] || (hovered && hovered.view === first.view ? hovered : null);
- if (first.kind === 'line') {
- if (second && second.kind === 'line' && second.edge !== first.edge) {
- return twoLines(v.id, first.edge, second.edge, at);
- }
- if (second && second.kind === 'point' && picks[1]) {
- return linearDraft(v.id, second.p, foot(second.p, first.edge), at, across(first.edge));
- }
- return mode === 'angle' ? null : linearDraft(v.id, first.edge.a, first.edge.b, at, along(first.edge));
- }
- if (first.kind === 'point' && second) {
- if (second.kind === 'point' && G2.dist(second.p, first.p) > 1e-9) {
- return linearDraft(v.id, first.p, second.p, at, null);
- }
- if (second.kind === 'line' && picks[1]) {
- return linearDraft(v.id, first.p, foot(first.p, second.edge), at, across(second.edge));
- }
- }
- return null;
- }
- function reset() {
- picks = [];
- draft = null;
- forced = null;
- circleKind = null;
- prompt();
- }
- function commit(record) {
- if (!record) {
- return;
- }
- const out = Object.assign({ id: ctx.newId('d') }, record);
- ctx.commit('Add ' + NAMES[out.type === 'linear' ? 'linear' : out.type], function (d) {
- d.annotations.push(out);
- });
- reset();
- }
- const tool = {
- name: mode === 'auto' ? 'dimension' : mode,
- move: function (p) {
- const first = picks[0];
- const hit = editor.geometryAt(p, first ? function (v) {
- return v.id === first.view;
- } : null);
- hovered = pickOf(hit);
- draft = makeDraft(p);
- },
- down: function (p) {
- tool.move(p);
- const pick = hovered;
- if (!picks.length) {
- if (!accepts(pick)) {
- ctx.setStatus(mode === 'auto' ? 'Click a straight edge, a circle or a snap point of a view' : firstPrompt);
- return;
- }
- if (mode === 'center') {
- commit({ type: 'center', view: pick.view, c: pick.edge.c.slice(), r: pick.edge.r });
- return;
- }
- picks = [pick];
- draft = makeDraft(p);
- prompt();
- return;
- }
- const first = picks[0];
- const same = pick && pick.view === first.view;
- if (picks.length === 1 && first.kind === 'line') {
- if (same && pick.kind === 'line' && pick.edge !== first.edge) {
- picks.push(pick);
- draft = makeDraft(p);
- prompt();
- return;
- }
- if (same && pick.kind === 'point' && mode === 'auto') {
- picks.push(pick);
- draft = makeDraft(p);
- prompt();
- return;
- }
- if (mode === 'angle') {
- ctx.setStatus('Click a second straight edge of the same view');
- return;
- }
- }
- if (picks.length === 1 && first.kind === 'point') {
- if (same && pick.kind === 'point' && G2.dist(pick.p, first.p) > 1e-9) {
- picks.push(pick);
- } else if (same && pick.kind === 'circle') {
- picks.push({ kind: 'point', view: pick.view, p: pick.edge.c, snap: 'center' });
- } else if (same && pick.kind === 'line') {
- picks.push(pick);
- } else {
- ctx.setStatus('Click a second point, a straight edge or a circle of the same view');
- return;
- }
- draft = makeDraft(p);
- prompt();
- return;
- }
- commit(draft);
- },
- key: function (event) {
- const k = event.key.toLowerCase();
- if (k === 'escape' && picks.length) {
- reset();
- return true;
- }
- if (k === ' ' && draft && draft.type === 'linear') {
- const order = [null, 'h', 'v', 'a'];
- forced = order[(order.indexOf(forced) + 1) % order.length];
- ctx.setStatus(forced ? 'Dimension: ' + { h: 'horizontal', v: 'vertical', a: 'aligned' }[forced] : 'Dimension: by the pointer');
- const pointer = editor.pointer();
- if (pointer) {
- draft = makeDraft(pointer);
- }
- return true;
- }
- if ((k === 'r' || k === 'd') && mode === 'auto' && picks.length && picks[0].kind === 'circle') {
- circleKind = k === 'r' ? 'radius' : 'diameter';
- const pointer = editor.pointer();
- if (pointer) {
- draft = makeDraft(pointer);
- }
- return true;
- }
- return false;
- },
- overlay: function () {
- const data = ctx.data();
- const out = hoverMarks(hovered);
- picks.forEach(function (pick) {
- const m = hoverMarks(pick);
- out.marks = out.marks.concat(m.marks);
- out.edges = out.edges.concat(m.edges);
- });
- out.items = draft ? annotationItems(draft, data) : [];
- // A rubber band from a first point while nothing can be measured yet
- if (!draft && picks.length === 1 && picks[0].kind === 'point' && editor.pointer()) {
- out.items.push({ t: 'line', a: sheetPoint(data, picks[0].view, picks[0].p), b: editor.pointer(), layer: 'thin' });
- }
- return out;
- },
- cancel: function () {
- if (picks.length) {
- reset();
- } else {
- ctx.done();
- }
- },
- start: prompt
- };
- return tool;
- }
- /* ================= leader notes and text ================= */
- function note() {
- let target = null;
- let hovered = null;
- let draft = null;
- const tool = {
- name: 'note',
- move: function (p) {
- const data = ctx.data();
- if (!target) {
- hovered = pickOf(editor.geometryAt(p));
- draft = null;
- return;
- }
- const v = target.view ? viewById(data, target.view) : null;
- draft = { type: 'note', view: target.view, p: target.p, at: v ? toLocal(v, scaleOf(data, v), p) : p, text: 'Note', end: target.end };
- },
- down: function (p) {
- if (!target) {
- const pick = pickOf(editor.geometryAt(p));
- if (pick && pick.kind === 'point') {
- target = { view: pick.view, p: pick.p.slice(), end: 'arrow' };
- } else if (pick && pick.edge) {
- target = { view: pick.view, p: nearestOnEdge(pick.edge, pick.local), end: 'arrow' };
- } else if (pick) {
- // Inside a view but on no edge: a face; ISO marks surfaces with a dot
- target = { view: pick.view, p: pick.p.slice(), end: 'dot' };
- } else {
- target = { view: null, p: p, end: 'arrow' };
- }
- ctx.setPrompt('Leader Note: click where the text goes');
- return;
- }
- tool.move(p);
- const made = draft;
- target = null;
- draft = null;
- ctx.setPrompt('Leader Note: click what the note points at (an edge, a point, or a face inside a view)');
- ctx.editText(made);
- },
- key: function (event) {
- if (event.key === 'Escape' && target) {
- target = null;
- draft = null;
- ctx.setPrompt('Leader Note: click what the note points at (an edge, a point, or a face inside a view)');
- return true;
- }
- return false;
- },
- overlay: function () {
- const out = target ? { marks: [], edges: [] } : hoverMarks(hovered);
- out.items = draft ? annotationItems(draft, ctx.data()) : [];
- return out;
- },
- cancel: function () {
- if (target) {
- target = null;
- draft = null;
- } else {
- ctx.done();
- }
- },
- start: function () {
- ctx.setPrompt('Leader Note: click what the note points at (an edge, a point, or a face inside a view)');
- }
- };
- return tool;
- }
- function freeText() {
- return {
- name: 'text',
- move: function () {},
- down: function (p) {
- ctx.editText({ type: 'text', view: null, at: p, text: '' });
- },
- key: function () {
- return false;
- },
- overlay: function () {
- const p = editor.pointer();
- return p ? { marks: [{ p: p, kind: 'end' }] } : null;
- },
- cancel: function () {
- ctx.done();
- },
- start: function () {
- ctx.setPrompt('Text: click where the text starts');
- }
- };
- }
- /* ================= projected views ================= */
- // Where a projected view would go for a pointer at p: { rel, x, y, box, label }
- function projectedGhost(parentId, p) {
- const data = ctx.data();
- const parent = viewById(data, parentId);
- const pbox = parent && editor.viewBox(parentId);
- if (!pbox) {
- return null;
- }
- const s = scaleOf(data, parent);
- const rel = sideFor(p[0] - (pbox[0] + pbox[2]) / 2, p[1] - (pbox[1] + pbox[3]) / 2);
- if (!rel) {
- return null;
- }
- const frame = projectedFrame(frameOf(data, parent), rel, data.sheet.projection);
- const local = ctx.estimate({ source: parent.source, dir: frame.dir, xdir: frame.xdir });
- if (!local) {
- return null;
- }
- const mx = (local[0] + local[2]) / 2;
- const my = (local[1] + local[3]) / 2;
- let x = p[0] - s * mx;
- let y = p[1] - s * my;
- if (rel === 'right' || rel === 'left') {
- y = parent.y;
- } else if (rel === 'top' || rel === 'bottom') {
- x = parent.x;
- }
- const key = orientationKey(frame);
- const label = rel.length === 2 ? 'Isometric view' : key ? ORIENTATIONS[key].label + ' view' : 'Projected view';
- return { rel: rel, x: x, y: y, box: [x + s * local[0], y + s * local[1], x + s * local[2], y + s * local[3]], label: label };
- }
- function projected(parentId) {
- let parent = parentId || null;
- let ghost = null;
- let hoverView = null;
- let made = 0;
- const tool = {
- name: 'projected',
- move: function (p) {
- if (!parent) {
- hoverView = editor.viewAt(p);
- return;
- }
- ghost = projectedGhost(parent, p);
- },
- down: function (p) {
- if (!parent) {
- parent = editor.viewAt(p);
- if (parent) {
- tool.start();
- } else {
- ctx.setStatus('Click a view to project from');
- }
- return;
- }
- ghost = projectedGhost(parent, p);
- if (!ghost) {
- return;
- }
- const data = ctx.data();
- const pv = viewById(data, parent);
- const id = ctx.newId('v');
- const iso = ghost.rel.length === 2;
- const record = {
- id: id, name: ctx.newViewName(), source: pv.source, parent: parent, rel: ghost.rel,
- x: ghost.x, y: ghost.y, scale: null, hidden: iso ? false : !!pv.hidden, smooth: pv.smooth !== false,
- centers: pv.centers !== false, label: false, faces: pv.faces || null
- };
- ctx.commit('Add projected view', function (d) {
- d.views.push(record);
- });
- made++;
- ctx.setStatus(ghost.label + ' added. Move round the view for more; right click or Esc when done.');
- },
- key: function () {
- return false;
- },
- overlay: function () {
- if (!parent) {
- const box = hoverView && editor.viewBox(hoverView);
- return box ? { frames: [{ box: box, cls: 'ov-hot' }] } : null;
- }
- return ghost ? { frames: [{ box: ghost.box, cls: 'ov-ghost', label: ghost.label }] } : null;
- },
- cancel: function () {
- ctx.done();
- },
- start: function () {
- ctx.setPrompt(parent ? 'Projected View: move beside the view and click to place (right click or Esc when done)' : 'Projected View: click the view to project from');
- },
- count: function () {
- return made;
- }
- };
- return tool;
- }
- /* ================= placing a new view ================= */
- // spec: the view record without x, y; local: its estimated local box
- function place(spec, local, onPlaced, onCancel) {
- let ghost = null;
- const tool = {
- name: 'place',
- move: function (p) {
- const s = scaleOf(ctx.data(), spec);
- const mx = (local[0] + local[2]) / 2;
- const my = (local[1] + local[3]) / 2;
- const x = p[0] - s * mx;
- const y = p[1] - s * my;
- ghost = { x: x, y: y, box: [x + s * local[0], y + s * local[1], x + s * local[2], y + s * local[3]] };
- },
- down: function (p) {
- tool.move(p);
- onPlaced(ghost.x, ghost.y);
- },
- key: function () {
- return false;
- },
- overlay: function () {
- return ghost ? { frames: [{ box: ghost.box, cls: 'ov-ghost', label: spec.name }] } : null;
- },
- cancel: function () {
- if (onCancel) {
- onCancel();
- }
- ctx.done();
- },
- start: function () {
- ctx.setPrompt('Click on the sheet to place ' + spec.name + ' (Esc to cancel)');
- }
- };
- return tool;
- }
- return {
- dimension: dimension,
- note: note,
- text: freeText,
- projected: projected,
- place: place
- };
- }
|