diff --git a/README.md b/README.md index a594e00..5caf4e9 100644 --- a/README.md +++ b/README.md @@ -65,8 +65,10 @@ for an empty workbench or to reload the sample. 1. Drop STL, OBJ, GLB, embedded GLTF, or 3MF files into the editor. Source units and up axis can be overridden. Automatic mode uses meters / Y up for glTF, millimeters / Z up for STL and OBJ, and declared 3MF units / Z up. -2. Select parts in the viewport or the searchable part tree. Shift-click selects - several parts. Move, rotate, and scale using gizmos or numeric fields. The +2. Select parts in the viewport or the searchable part tree. Ctrl/⌘-click selects + several parts, as does Shift-click in the viewport; Shift-click in the part tree + selects a range. **Ctrl/⌘ + G** groups the selection, and clicking a grouped part + in the viewport selects its whole group. Move, rotate, and scale using gizmos or numeric fields. The inspector’s **Move** and **Rotate** controls also offer X/Y/Z −/+ buttons for precise adjustments, including whole selections. Enter a **Step** in millimeters or degrees; **Snap dragging** uses that same increment for the handles. The @@ -692,6 +694,7 @@ During contact painting, undo removes the last brush stroke before scene changes | Deselect all | `Ctrl/⌘ + Shift + A`; `Esc` in Select mode | | Copy / cut / paste components | `Ctrl/⌘ + C` / `X` / `V` | | Duplicate selected | `Ctrl/⌘ + D` | +| Group / ungroup | `Ctrl/⌘ + G` / `Ctrl/⌘ + Shift + G` | | Rename one component | `F2` | | Delete unlocked selection | `Delete` / `Backspace` | | Undo / redo | `Ctrl/⌘ + Z` / `Ctrl/⌘ + Shift + Z`; `Ctrl + Y` | diff --git a/docs/scene-format.md b/docs/scene-format.md index 6805f07..4e01d69 100644 --- a/docs/scene-format.md +++ b/docs/scene-format.md @@ -49,6 +49,8 @@ editor metadata, independent of mesh buffers, supported by all three scene reade ], "joints": [], "constraints": [], + // Only when parts are grouped. + "groups": [{ "id": "4f0c…", "name": "Upper body" }], // Only when the scene has lighting of its own. "lighting": { "exposure": 1, @@ -180,6 +182,15 @@ light. Only its direction counts, so it cannot be zero. A point light's `positio position in millimeters; it does not dim with distance unless it has a `range`. With `camera`, both are in the camera's frame instead: x right, y up, +z toward the viewer. +### Groups + +Groups are optional and only organize the editor. No export contains them, and a file without +them opens with every part ungrouped. + +- `groups` lists each group's `id`, unique within `groups`, and its `name`. It is present only + when at least one part is grouped, and lists only groups that have parts. +- `parts[].groupId` names the group the part belongs to. It is absent when the part has none. + ## Validation A file can be damaged, or crafted, so the reader checks it before trusting it: @@ -204,6 +215,8 @@ A file can be damaged, or crafted, so the reader checks it before trusting it: it is directional with a zero position, and only the first 8 valid lights are kept. Its other fields fall back to the name `Light` or `Point`, `#ffffff`, intensity 1, range 0, enabled, without shadows, and not following the camera. Unknown fields are dropped. +- Groups are repaired rather than refused. A group whose `id` or `name` is not a string, or + whose `id` is empty or repeated, is dropped, and so is a `groupId` that names no group. - A file with a higher version number asks for a newer version of Kitbasher. Blocks carry no checksum. Damage that still unpacks to finite values in range opens as a diff --git a/mcp/session.ts b/mcp/session.ts index 3a9d628..bd53e37 100644 --- a/mcp/session.ts +++ b/mcp/session.ts @@ -12,7 +12,7 @@ import { solveAlignment } from '../src/core/alignment'; import { evaluateBoolean } from '../src/core/boolean'; import type { ClipReport } from '../src/core/clipping'; import type { BooleanOperation } from '../src/core/boolean'; -import { captureComponents, instantiateComponents } from '../src/core/clipboard'; +import { addedGroups, captureComponents, instantiateComponents } from '../src/core/clipboard'; import { anchorError, isJointConnected } from '../src/core/constraints'; import { export3mf, @@ -724,7 +724,10 @@ export class Session { transform = step.clone().multiply(transform); const scene = editor().scene; const clipboard = { ...captureComponents(scene, sources), nextOffset: 0 }; - const copies = instantiateComponents(clipboard, [...scene.parts, ...added]); + const copies = instantiateComponents(clipboard, { + parts: [...scene.parts, ...added], + groups: scene.groups, + }); const ids = new Map(); const parts = copies.parts.map((part, i) => { const name = spec.names?.[added.length + i] ?? part.name; @@ -770,6 +773,7 @@ export class Session { parts: [...scene.parts, ...parts], joints: [...scene.joints, ...joints], constraints: [...(scene.constraints ?? []), ...constraints], + ...addedGroups(scene, copies.groups), }); added.push(...parts); } diff --git a/public/docs/images/reference/view-menu.webp b/public/docs/images/reference/view-menu.webp index ee9975b..af4984f 100644 Binary files a/public/docs/images/reference/view-menu.webp and b/public/docs/images/reference/view-menu.webp differ diff --git a/src/components/Dialogs.tsx b/src/components/Dialogs.tsx index f792c0a..ad59a37 100644 --- a/src/components/Dialogs.tsx +++ b/src/components/Dialogs.tsx @@ -631,7 +631,8 @@ function HelpDialog() { ['Fine move / rotate / scale', 'Hold Shift while dragging · 10× finer'], ['Pan', 'Right drag or Shift + left drag'], ['Zoom', 'Scroll / pinch · tune in Preferences'], - ['Select multiple', 'Shift / Ctrl / ⌘ + click'], + ['Select multiple', 'Ctrl / ⌘ + click · Shift + click in the viewport'], + ['Select a range of parts', 'Shift + click in Parts'], ['Select all components', key('selectAll')], ['Deselect all', keys('deselect', 'cancel')], ['Copy / Cut / Paste components', keys('copy', 'cut', 'paste')], @@ -652,12 +653,13 @@ function HelpDialog() { ['Pick pivot on model', `${key('pivot')} · click a surface`], ['Align / Joint / Measure', keys('align', 'joint', 'measure')], ['Front / Right / Top', keys('viewFront', 'viewRight', 'viewTop')], - ['Toggle projection', key('projection')], + ['Perspective / orthographic view', key('projection')], ['Undo / Redo', keys('undo', 'redo')], ['Save / Open', keys('save', 'open')], ['New assembly / Import', keys('new', 'import')], ['Export', key('export')], ['Duplicate', key('duplicate')], + ['Group / Ungroup', keys('group', 'ungroup')], ['Delete unlocked parts', key('delete')], ['Finish / Cancel tool', keys('apply', 'cancel')], ['Exit camera tool', `${key('cancel')} (keeps picked pairs)`], diff --git a/src/components/EditorContextMenu.tsx b/src/components/EditorContextMenu.tsx index d8172fa..cffd4f5 100644 --- a/src/components/EditorContextMenu.tsx +++ b/src/components/EditorContextMenu.tsx @@ -10,6 +10,7 @@ import { EyeOff, Focus, GitMerge, + Group, Import, Layers2, Link2, @@ -25,6 +26,7 @@ import { ScanLine, Scissors, Trash2, + Ungroup, UnlockKeyhole, } from 'lucide-react'; import { contactBrush, activeBrush } from '../core/editor-brush'; @@ -49,6 +51,8 @@ const icons = { duplicate: Copy, rename: Pencil, delete: Trash2, + group: Group, + ungroup: Ungroup, move: Move3D, rotate: Rotate3D, scale: Scaling, @@ -178,6 +182,7 @@ function OpenContextMenu({ ? [ ['cut', 'copy', 'paste', 'duplicate'], ['rename', 'delete'], + ['group', 'ungroup'], ['focus', 'pivot', 'align', 'paint', 'joint', 'merge', 'inspect'], ['isolate', 'isolateNearby'], ['hide', 'hideOthers', unlocked ? 'lock' : 'unlock', 'showAll'], diff --git a/src/components/PartsPanel.tsx b/src/components/PartsPanel.tsx index 18e2eb8..9cd5d5a 100644 --- a/src/components/PartsPanel.tsx +++ b/src/components/PartsPanel.tsx @@ -1,4 +1,5 @@ -import { useMemo, useState } from 'react'; +import { Fragment, useMemo, useRef, useState } from 'react'; +import type { MouseEvent } from 'react'; import { ArrowDownToLine, Box, @@ -9,6 +10,7 @@ import { Eye, EyeOff, FolderOpen, + Group, Link2, LockKeyhole, Plus, @@ -21,6 +23,9 @@ import { import { useEditor } from '../core/store'; import { referencePlanes } from '../core/references'; import { triangleCount } from '../core/geometry'; +import { partTree, selectionRange, treeOrder } from '../core/part-tree'; +import type { PartTreeEntry } from '../core/part-tree'; +import type { Part } from '../core/types'; import { distinctColors, swatchBackground, swatchColors } from '../core/appearance'; import { IconButton } from './ui'; import { ConstraintList } from './ConstraintList'; @@ -36,9 +41,167 @@ export function PartsPanel({ onClose }: { onClose: () => void }) { const [tab, setTab] = useState<'parts' | 'joints' | 'constraints'>('parts'), [query, setQuery] = useState(''), [expanded, setExpanded] = useState(true); - const filtered = scene.parts.filter((p) => p.name.toLowerCase().includes(query.toLowerCase())); + const tree = useMemo(() => partTree(scene, query), [scene, query]); + const [collapsed, setCollapsed] = useState>(() => new Set()), + [renaming, setRenaming] = useState(null); + const cancelRename = useRef(false); + // Where Shift+click ranges from: the last part clicked here, or else the last one selected. + const anchor = useRef(null); + /** Clicks a part, or a group with these parts. */ + const choose = (event: MouseEvent, ids: string[]) => { + const state = useEditor.getState(); + if (event.shiftKey) { + const from = + anchor.current && state.selected.includes(anchor.current) + ? anchor.current + : state.selected.at(-1); + anchor.current = from ?? ids[0]; + state.selectParts(selectionRange(treeOrder(tree), from, ids)); + return; + } + anchor.current = ids[0]; + if (event.metaKey || event.ctrlKey) state.select(ids[0], true, ids.length > 1); + else state.selectParts(ids); + }; const distinct = useMemo(() => distinctColors(scene.parts), [scene.parts]); const triangles = scene.parts.reduce((sum, p) => sum + triangleCount(p.geometry), 0); + const partRow = (part: Part, inGroup: boolean) => { + // On a row of a larger selection, the eye shows or hides the selection. + const selectedRow = selected.length > 1 && selected.includes(part.id); + return ( +
+ +
+ + +
+
+ ); + }; + const groupRow = ({ group, parts }: Extract) => { + const ids = parts.map((part) => part.id); + const whole = ids.every((id) => selected.includes(id)); + const open = !collapsed.has(group.id); + const visible = parts.some((part) => part.visible); + // Like a part's eye: on a group that is part of a larger selection, it acts on the selection. + const others = whole ? selected.length - ids.length : 0; + const rename = (name: string) => { + if (!cancelRename.current) useEditor.getState().renameGroup(group.id, name); + cancelRename.current = false; + setRenaming(null); + }; + return ( +
isolated.includes(id)) ? 'outside-isolation' : ''}`} + onContextMenu={() => { + if (!whole) useEditor.getState().selectParts(ids); + }} + > + + + {renaming === group.id ? ( + e.target.select()} + onBlur={(e) => rename(e.target.value)} + onKeyDown={(e) => { + if (e.key === 'Escape') cancelRename.current = true; + if (e.key === 'Enter' || e.key === 'Escape') e.currentTarget.blur(); + }} + /> + ) : ( + + )} +
+ +
+
+ ); + }; return (