d0b39abbc0
- Grid and snap. The grid's squares were powers of ten only, 10 to 100 px wide, and a sketch snapped to a finer lattice nothing showed (1 mm, with lines drawn every 10): a line's end "did not snap to the grid" - it had, to one that was not drawn. The squares are 1, 5 or 10 (times a power of ten) now, the smallest at least 10 px wide, with a stronger line every ten, and the snap goes to exactly those squares. Tried: a line upright from a point, level from it, and free, at two zooms - each end on a crossing of the lines shown.
- Lock grid size, switched on, keeps the squares as they are at this zoom (it kept a size typed earlier). Its row says the size it would take ("now 5 mm"), in the project's unit.
- A grey line under a list's own name at the right edge (Settings, apart from Units & grid, Snapping, Number pad, Appearance; Display the same), as there is over each part's heading. File: a line between the project, the 3D exports and the sketch exports. More: between the view, help and about. All the same grey.
- File > Export SVG (sketch), beside Export DXF: millimetres, the page just round the drawing; lines, circles and arcs exact, ellipses and splines fine polylines; construction curves in a group of their own, dashed. Its share button where the browser shares SVG. tests/svgexport.test.ts: written and read back in, as wide, as tall and as long a line (6 checks).
- A list opened from the line: its buttons in even rows, each as wide as the widest, a short last row's buttons wider, the list just as wide as its rows. (It was 420 wide for any number of buttons: 30 to 200 px empty at the end of each row.) Measured with every list open at 320 x 640, 390 x 800, 430 x 932, 800 x 390, 768 x 1024, 1366 x 1024 and desktop, in the model and in a sketch: no row ends in a gap, no word clipped, none off the screen or over undo / redo.
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
40 lines
2.8 KiB
TypeScript
40 lines
2.8 KiB
TypeScript
// Run: npx tsx tests/svgexport.test.ts
|
||
// A sketch saved as SVG (File › Export SVG) and read back in (Insert › File): the same drawing, the same size.
|
||
import type { SketchEntity, Vec2 } from "../src/doc/types";
|
||
import { parseSVG, sketchToSVG } from "../src/io/svg";
|
||
import { entityPolylines } from "../src/sketch/curves";
|
||
|
||
let failed = 0;
|
||
const check = (name: string, ok: boolean, info = "") => {
|
||
if (!ok) failed++;
|
||
console.log(ok ? "ok " : "FAIL", name, info);
|
||
};
|
||
const box = (es: SketchEntity[]) => {
|
||
const pts = es.flatMap((e) => entityPolylines(e).flat()) as Vec2[];
|
||
return [Math.min(...pts.map((p) => p[0])), Math.min(...pts.map((p) => p[1])), Math.max(...pts.map((p) => p[0])), Math.max(...pts.map((p) => p[1]))];
|
||
};
|
||
const length = (es: SketchEntity[]) => es.reduce((s, e) => s + entityPolylines(e).reduce((t, poly) => t + poly.slice(1).reduce((u, p, i) => u + Math.hypot(p[0] - poly[i][0], p[1] - poly[i][1]), 0), 0), 0);
|
||
|
||
const sketch: SketchEntity[] = [
|
||
{ id: "l", kind: "line", a: [0, 0], b: [40, 0] },
|
||
{ id: "a", kind: "arc", a: [40, 0], m: [50, 10], b: [40, 20] },
|
||
{ id: "big", kind: "arc", a: [-10, 0], m: [-25, 15], b: [-10, 5] },
|
||
{ id: "c", kind: "circle", center: [20, 30], radius: 7.5 },
|
||
{ id: "e", kind: "ellipse", center: [-20, -20], rx: 12, ry: 5, rotation: 0.4 },
|
||
{ id: "r", kind: "rect", center: [60, -20], w: 20, h: 10, rotation: 0.3 },
|
||
{ id: "p", kind: "polygon", center: [0, 50], radius: 8, sides: 6, angle: 0.2 },
|
||
{ id: "s", kind: "spline", points: [[70, 20], [80, 35], [95, 25], [100, 40]], closed: false, mode: "fit" },
|
||
{ id: "k", kind: "line", a: [0, -40], b: [30, -45], construction: true } as SketchEntity,
|
||
];
|
||
const svg = sketchToSVG(sketch, "Bracket <1> & co");
|
||
check("it is an SVG in millimetres, with a title", /^<\?xml/.test(svg) && /width="[\d.]+mm"/.test(svg) && svg.includes("<title>Bracket 1 co</title>"));
|
||
check("a circle is a circle, an arc an arc", svg.includes("<circle ") && (svg.match(/ A/g) ?? []).length === 2);
|
||
check("construction curves are apart, dashed", /<g id="construction"[^>]*stroke-dasharray/.test(svg));
|
||
const back = parseSVG(svg).entities;
|
||
const [b0, b1] = [box(sketch), box(back)];
|
||
// (Read back, y runs the page's way: its height and width are what count, and what is drawn.)
|
||
check("read back: as wide and as tall", Math.abs(b0[2] - b0[0] - (b1[2] - b1[0])) < 0.05 && Math.abs(b0[3] - b0[1] - (b1[3] - b1[1])) < 0.05, `${b0.map((v) => v.toFixed(2))} / ${b1.map((v) => v.toFixed(2))}`);
|
||
check("read back: the same length of line drawn", Math.abs(length(sketch) - length(back)) < length(sketch) * 0.002, `${length(sketch).toFixed(2)} / ${length(back).toFixed(2)}`);
|
||
check("nothing drawn: still a file", /^<\?xml/.test(sketchToSVG([])) && !sketchToSVG([]).includes("NaN") && !sketchToSVG([]).includes("Infinity"));
|
||
process.exit(failed ? 1 : 0);
|