* Add roof surface placement support for items Items (e.g. solar panels) can now be placed on sloped roof surfaces. The placement system computes euler rotation from the roof surface normal so items sit flush on the slope instead of going inside. - Add roofStrategy to placement-strategies with enter/move/click/leave - Wire roof:enter/move/click/leave events in the placement coordinator - Add calculateRoofRotation in placement-math using surface normals - Support full 3D cursor rotation for sloped surfaces - Items on roofs are parented to the level with world-space rotation Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * fixed conflict * feat(duct): ceiling-snap drawing + connected-joint endpoint move Duct draw tool's ceiling mode now hangs each path point just below the ceiling actually covering it (per-room heights tracked), with a translucent surface highlight and a plumb line to the floor so the in-flight point reads clearly from any angle. Dragging a duct corner that sits on a fitting now carries the fitting's other ducts along (port-connectivity second hop), so the joint moves together instead of tearing apart. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com> * feat(mep): detach + vertical modifiers for duct/pipe joint editing Alt detaches a dragged duct/pipe endpoint or fitting from its connected joint (no elbow re-aim, no connectivity follow); Ctrl/Cmd drives vertical riser movement on the fitting move. Behavioral parity across 2D and 3D. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com> * feat(mep): full DWV pipe parity for joint editing Bring pipe-segment endpoint drags and pipe-fitting moves to parity with duct: free-drag endpoints, Alt-detach, Ctrl/Cmd-vertical riser, elbow re-aim, and connectivity follow. Generalizes the shared elbow-reaim and auto-fitting helpers to dispatch by run kind so 2D and 3D share one path. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com> * feat(mep): wall-style arrow handles for duct fittings + segments Add violet directional arrow affordances to duct-fitting selection (height, move cross, rotate arc) mirroring the duct-segment rig: portaled into the parent frame to stay out of the selection outline, rendered via the shared HandleArrow, and carrying mated-run connectivity through the single-undo dance. The move cross engages press-drag-release (placementDragMode) the same way the floating drag does, so the markup hit-areas go inert and the fitting move tool commits on pointer-up. Also re-export the HandleArrow primitives from @pascal-app/editor and extend the duct-segment side-move/floorplan affordances. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com> * feat(mep): click-to-latch cube handles for duct + fitting editing Replace the hover-reveal / multi-handle selection rigs with a single click-to-latch cube that opens a directional cluster, shared between duct segments and fittings via a new selection-handles module (HandleCube / MoveChevron / RotateArc, all sized to the roof pitch cube). - Duct segment: per-vertex + run-center cubes reveal axis-locked move chevrons (down arrow always shown), plus a roll arc at the run center. - Duct fitting: center cube reveals six ±XYZ move arrows and three per-axis rotation arcs (oriented in place), replacing the old height/move/rotate trio with axis-cycling. - Rotation (fitting arcs + duct roll) snaps to 45° steps; Shift = smooth. - thin chevron profile + press-drag-release commit retained. * fix(mep): orient duct roll arc consistently + drop Ctrl-vertical drag Build a fully-determined basis for the duct roll gizmo so the curved arrow always seats at the top-outer 45° corner regardless of run direction, instead of an arbitrary apex from a single setFromUnitVectors. The selection-rig ±Y arrows now own vertical movement, so the redundant Ctrl-modifier riser drag is removed from the fitting move tool. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com> * feat(mep): run-aligned duct handles, swing snapping, elbow flatten Align the duct run-center cube + horizontal arrows to the run axis (matching the per-vertex handles) while keeping whole-run translate. Endpoint side / up-down swing arrows now follow grid snap points and port-snap onto nearby collars (Shift sweeps smoothly). Relax elbow realign + fitting schemas to flatten to a straight 0° coupling. Surface HVAC-specific hints in the select-mode helper panel for duct / fitting. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com> * feat(mep): per-segment linesets/liquid-lines with joint-follow editing Linesets and liquid lines now commit one independent two-point node per drawn segment instead of folding into a single mitered polyline, so each line selects and deletes on its own. Endpoint caps fill shared-coordinate joints so connected segments still read as continuous pipe. Dragging a shared endpoint carries mated segments along via port connectivity (Alt detaches), so a run still edits as one welded piece. Liquid-line follow mode traces the whole connected lineset run, laying a per-segment parallel line down its full assembled length. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com> * feat(mep): vertical-offset auto-routing on duct center-cube ±Y drag Lifting/lowering a connected run with the run-center cube now keeps each connected end welded to its stationary partner instead of dragging the whole network. Run-to-run ends get the classic S/Z offset (two elbows + plumb riser, partner trimmed back one leg); elbow-connected ends form a clean L — the existing elbow stays put and re-aims its collar vertical, with one new top elbow + riser reconnecting to the lifted endpoint. The offset is ghosted live and minted as a single undo step on release. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com> * Add roof accessory placement guides Measure roof accessory placement against the active roof face using visible surface bounds and preview geometry footprints. Add dormer-local guides and special linear handling for ridge vents and gutters. * Improve duct and placement routing * Fix duct vertical movement routing * Fix duct vertical offsets and roof accessory movement * Add DWV movement parity and line endpoint controls * Fix MEP handle review issues * Fix chimney placement and duct offset cleanup * Use snapped targets for roof accessory commits * fix(nodes): repair MEP movement review issues - auto-fitting: tee branch now follows the drawn lateral angle; update the stale square-tee test + doc comment that contradicted the rewrite - duct-segment: re-enable the vertical auto-offset rewind (the disabled stub left mintedIds empty, so re-dragging a tagged duct stranded old elbows/risers and stacked duplicates); remove the dead stub - duct-segment: strip the stale auto-offset tag on manual corner/roll commits so the horizontal-move path no longer trusts an out-of-date base - chimney: resume history before mutating segment children arrays so a cross-segment move reparents in one tracked transaction (undo stays consistent) - dormer: align schema test with the new windowSill=false default * fix: address mep movement review issues * fix: address follow-up mep review comments * fix: address additional mep review comments --------- Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com> Co-authored-by: pascal-open-bot <open@pascal.app>
375 lines
13 KiB
TypeScript
375 lines
13 KiB
TypeScript
'use client'
|
|
|
|
import { emitter, type GridEvent, LinesetNode, useScene } from '@pascal-app/core'
|
|
import {
|
|
CursorSphere,
|
|
DimensionPill,
|
|
EDITOR_LAYER,
|
|
markToolCancelConsumed,
|
|
triggerSFX,
|
|
useEditor,
|
|
} from '@pascal-app/editor'
|
|
import { useViewer } from '@pascal-app/viewer'
|
|
import { Html } from '@react-three/drei'
|
|
import { useEffect, useRef, useState } from 'react'
|
|
import { type Group, Vector3 } from 'three'
|
|
import { alignDrawPoint, clearDrawAlignment } from '../shared/draw-alignment'
|
|
import { LevelOffsetGroup } from '../shared/level-offset-group'
|
|
import { collectScenePorts, findNearestPortXZ, REFRIGERANT_PORT_SYSTEMS } from '../shared/ports'
|
|
import { linesetDefinition } from './definition'
|
|
|
|
/**
|
|
* Continuous placement tool for refrigerant linesets — the refrigerant-loop
|
|
* sibling of the duct-segment tool.
|
|
*
|
|
* Mouse-driven model:
|
|
* - **First click** anchors the run start. Within range of a refrigerant
|
|
* service port (a condenser / coil valve, or another lineset's end) it
|
|
* snaps onto the port so a run mates flush.
|
|
* - **Second click** commits a two-point lineset and keeps its far end
|
|
* anchored, so the next click continues the run like wall / duct drafting.
|
|
* - The in-flight end is angle-locked to the nearest 45° step in XZ from
|
|
* the start; Y stays at the start's height. Hold **Shift** to release.
|
|
* - Hold **Alt** → vertical mode. XZ locks to the start; vertical mouse
|
|
* motion drives Y. Click commits the riser segment.
|
|
* - Esc clears an anchored start point.
|
|
*
|
|
* Snapping is restricted to refrigerant ports, so a lineset never grabs a
|
|
* supply/return duct collar.
|
|
*/
|
|
const PREVIEW_OPACITY = 0.6
|
|
const PREVIEW_COLOR = '#b06b3f'
|
|
/** Snap radius (meters) for joining onto a refrigerant port. */
|
|
const ENDPOINT_SNAP_RADIUS_M = 0.5
|
|
/** Angle step (radians) for the XZ angle lock — 45°. */
|
|
const ANGLE_STEP_RAD = Math.PI / 4
|
|
/** Mouse pixels → meters mapping for Alt-vertical drag. 100 px ≈ 1 m. */
|
|
const ALT_PIXELS_PER_METER = 100
|
|
const ALT_Y_MIN_M = -3
|
|
const ALT_Y_MAX_M = 10
|
|
|
|
function snap(value: number, step: number): number {
|
|
if (step <= 0) return value
|
|
return Math.round(value / step) * step
|
|
}
|
|
|
|
/** Nearest refrigerant port within snap range on the XZ plane, as a
|
|
* position tuple. Y is ignored for the distance check; the snap adopts the
|
|
* port's full 3D position. */
|
|
function findNearbyPort(point: [number, number, number]): [number, number, number] | null {
|
|
const port = findNearestPortXZ(
|
|
point,
|
|
collectScenePorts({ systems: REFRIGERANT_PORT_SYSTEMS }),
|
|
ENDPOINT_SNAP_RADIUS_M,
|
|
)
|
|
return port ? [port.position[0], port.position[1], port.position[2]] : null
|
|
}
|
|
|
|
function projectToAngleLock(
|
|
from: [number, number, number],
|
|
raw: [number, number, number],
|
|
): [number, number, number] {
|
|
const dx = raw[0] - from[0]
|
|
const dz = raw[2] - from[2]
|
|
const len = Math.hypot(dx, dz)
|
|
if (len < 1e-4) return [from[0], from[1], from[2]]
|
|
const theta = Math.atan2(dz, dx)
|
|
const snapped = Math.round(theta / ANGLE_STEP_RAD) * ANGLE_STEP_RAD
|
|
const proj = dx * Math.cos(snapped) + dz * Math.sin(snapped)
|
|
const d = Math.max(0, proj)
|
|
return [from[0] + Math.cos(snapped) * d, from[1], from[2] + Math.sin(snapped) * d]
|
|
}
|
|
|
|
const LinesetTool = () => {
|
|
const activeLevelId = useViewer((s) => s.selection.levelId)
|
|
const unit = useViewer((s) => s.unit)
|
|
const cursorRef = useRef<Group>(null)
|
|
const [draftPoints, setDraftPoints] = useState<Array<[number, number, number]>>([])
|
|
const [cursorPos, setCursorPos] = useState<[number, number, number] | null>(null)
|
|
const [snapTarget, setSnapTarget] = useState<[number, number, number] | null>(null)
|
|
const [altActive, setAltActive] = useState(false)
|
|
const draftRef = useRef(draftPoints)
|
|
draftRef.current = draftPoints
|
|
const altAnchorRef = useRef<{ clientY: number; baseY: number } | null>(null)
|
|
const lastClientYRef = useRef<number | null>(null)
|
|
|
|
useEffect(() => {
|
|
if (!activeLevelId) return
|
|
|
|
const commitSegment = (start: [number, number, number], end: [number, number, number]) => {
|
|
const sameSpot =
|
|
Math.abs(start[0] - end[0]) < 1e-4 &&
|
|
Math.abs(start[1] - end[1]) < 1e-4 &&
|
|
Math.abs(start[2] - end[2]) < 1e-4
|
|
if (sameSpot) return
|
|
|
|
// Each drawn segment is its own standalone two-point lineset node — the
|
|
// refrigerant-loop sibling of duct-segment. Independent nodes mean each
|
|
// segment selects and deletes on its own, rather than folding into one
|
|
// mitered polyline run.
|
|
const lineset = LinesetNode.parse({
|
|
...linesetDefinition.defaults(),
|
|
name: 'Lineset',
|
|
path: [start, end],
|
|
})
|
|
useScene.getState().createNode(lineset, activeLevelId)
|
|
triggerSFX('sfx:item-place')
|
|
setDraftPoints([end])
|
|
setSnapTarget(null)
|
|
altAnchorRef.current = null
|
|
setAltActive(false)
|
|
}
|
|
|
|
const resolveSnappedPoint = (
|
|
event: GridEvent,
|
|
): { point: [number, number, number]; snapped: [number, number, number] | null } => {
|
|
const last = draftRef.current.at(-1)
|
|
if (!last) {
|
|
const raw: [number, number, number] = [event.localPosition[0], 0, event.localPosition[2]]
|
|
if (event.nativeEvent?.altKey !== true) {
|
|
const target = findNearbyPort(raw)
|
|
if (target) return { point: target, snapped: target }
|
|
}
|
|
const step = useEditor.getState().gridSnapStep
|
|
return { point: [snap(raw[0], step), 0, snap(raw[2], step)], snapped: null }
|
|
}
|
|
const rawXZ: [number, number, number] = [
|
|
event.localPosition[0],
|
|
last[1],
|
|
event.localPosition[2],
|
|
]
|
|
const shift = event.nativeEvent?.shiftKey === true
|
|
const angled = shift ? rawXZ : projectToAngleLock(last, rawXZ)
|
|
if (event.nativeEvent?.altKey !== true && !shift) {
|
|
const target = findNearbyPort(rawXZ)
|
|
if (target) return { point: target, snapped: target }
|
|
}
|
|
const step = useEditor.getState().gridSnapStep
|
|
return { point: [snap(angled[0], step), angled[1], snap(angled[2], step)], snapped: null }
|
|
}
|
|
|
|
const resolveAltVerticalPoint = (clientY: number): [number, number, number] | null => {
|
|
const anchor = altAnchorRef.current
|
|
const last = draftRef.current.at(-1)
|
|
if (!anchor || !last) return null
|
|
const step = useEditor.getState().gridSnapStep
|
|
const dy = (anchor.clientY - clientY) / ALT_PIXELS_PER_METER
|
|
const snappedDy = snap(dy, step)
|
|
const y = Math.min(ALT_Y_MAX_M, Math.max(ALT_Y_MIN_M, anchor.baseY + snappedDy))
|
|
return [last[0], y, last[2]]
|
|
}
|
|
|
|
// Resolve the cursor point (port / grid / angle snap) then layer
|
|
// Figma-style alignment so a lineset lines up with other runs, equipment,
|
|
// and items as it's drawn. Free point (first vertex / Shift) snaps; an
|
|
// angle-locked continuation shows the guide passively. Port snap or Alt
|
|
// bypasses alignment.
|
|
const resolveAlignedPoint = (event: GridEvent) => {
|
|
const r = resolveSnappedPoint(event)
|
|
const hasStart = draftRef.current.length > 0
|
|
const shift = event.nativeEvent?.shiftKey === true
|
|
const alt = event.nativeEvent?.altKey === true
|
|
const point = alignDrawPoint(r.point, {
|
|
applySnap: !hasStart || shift,
|
|
bypass: alt || r.snapped !== null,
|
|
})
|
|
return { ...r, point }
|
|
}
|
|
|
|
const onMove = (event: GridEvent) => {
|
|
const clientY = (event.nativeEvent as { clientY?: number } | undefined)?.clientY
|
|
if (typeof clientY === 'number') lastClientYRef.current = clientY
|
|
if (altAnchorRef.current && typeof clientY === 'number') {
|
|
const point = resolveAltVerticalPoint(clientY)
|
|
if (point) {
|
|
clearDrawAlignment()
|
|
setCursorPos(point)
|
|
setSnapTarget(null)
|
|
return
|
|
}
|
|
}
|
|
const { point, snapped } = resolveAlignedPoint(event)
|
|
setCursorPos(point)
|
|
setSnapTarget(snapped)
|
|
}
|
|
|
|
const onClick = (event: GridEvent) => {
|
|
const start = draftRef.current.at(-1)
|
|
if (altAnchorRef.current && start) {
|
|
const clientY =
|
|
(event.nativeEvent as { clientY?: number } | undefined)?.clientY ?? lastClientYRef.current
|
|
if (typeof clientY === 'number') {
|
|
const point = resolveAltVerticalPoint(clientY)
|
|
if (point && Math.abs(point[1] - start[1]) >= 1e-4) {
|
|
commitSegment(start, point)
|
|
}
|
|
}
|
|
return
|
|
}
|
|
const { point } = resolveAlignedPoint(event)
|
|
if (!start) {
|
|
triggerSFX('sfx:grid-snap')
|
|
setDraftPoints([point])
|
|
return
|
|
}
|
|
commitSegment(start, point)
|
|
}
|
|
|
|
const enterAltMode = () => {
|
|
const last = draftRef.current.at(-1)
|
|
if (!last || lastClientYRef.current === null) return
|
|
if (altAnchorRef.current) return
|
|
altAnchorRef.current = { clientY: lastClientYRef.current, baseY: last[1] }
|
|
setAltActive(true)
|
|
}
|
|
|
|
const exitAltMode = () => {
|
|
if (!altAnchorRef.current) return
|
|
altAnchorRef.current = null
|
|
setAltActive(false)
|
|
}
|
|
|
|
const onKeyDown = (e: KeyboardEvent) => {
|
|
const tag = (e.target as HTMLElement | null)?.tagName
|
|
if (tag === 'INPUT' || tag === 'TEXTAREA') return
|
|
if (e.key === 'Alt') {
|
|
e.preventDefault()
|
|
enterAltMode()
|
|
}
|
|
}
|
|
|
|
const onKeyUp = (e: KeyboardEvent) => {
|
|
if (e.key === 'Alt') {
|
|
e.preventDefault()
|
|
exitAltMode()
|
|
}
|
|
}
|
|
|
|
const onCancel = () => {
|
|
clearDrawAlignment()
|
|
if (draftRef.current.length === 0) return
|
|
markToolCancelConsumed()
|
|
setDraftPoints([])
|
|
setCursorPos(null)
|
|
setSnapTarget(null)
|
|
}
|
|
|
|
emitter.on('grid:move', onMove)
|
|
emitter.on('grid:click', onClick)
|
|
emitter.on('tool:cancel', onCancel)
|
|
window.addEventListener('keydown', onKeyDown)
|
|
window.addEventListener('keyup', onKeyUp)
|
|
return () => {
|
|
emitter.off('grid:move', onMove)
|
|
emitter.off('grid:click', onClick)
|
|
emitter.off('tool:cancel', onCancel)
|
|
window.removeEventListener('keydown', onKeyDown)
|
|
window.removeEventListener('keyup', onKeyUp)
|
|
altAnchorRef.current = null
|
|
clearDrawAlignment()
|
|
}
|
|
}, [activeLevelId])
|
|
|
|
if (!activeLevelId) return null
|
|
|
|
const previewSegments: Array<{ a: [number, number, number]; b: [number, number, number] }> = []
|
|
for (let i = 0; i < draftPoints.length - 1; i++) {
|
|
previewSegments.push({ a: draftPoints[i]!, b: draftPoints[i + 1]! })
|
|
}
|
|
const last = draftPoints.at(-1)
|
|
if (last && cursorPos) {
|
|
previewSegments.push({ a: last, b: cursorPos })
|
|
}
|
|
|
|
const pillParts = cursorPos
|
|
? (['x', 'y', 'z'] as const).map((axis, i) => ({
|
|
key: axis,
|
|
prefix: axis.toUpperCase(),
|
|
value: last ? cursorPos[i]! - last[i]! : cursorPos[i]!,
|
|
signed: !!last,
|
|
}))
|
|
: null
|
|
const pillPrimary =
|
|
last && cursorPos
|
|
? altActive
|
|
? 'y'
|
|
: Math.abs(cursorPos[0] - last[0]) >= Math.abs(cursorPos[2] - last[2])
|
|
? 'x'
|
|
: 'z'
|
|
: undefined
|
|
|
|
return (
|
|
<LevelOffsetGroup>
|
|
{/* Cursor marker — the same ground ring + vertical line + tool-icon
|
|
badge the duct draw tool shows in 3D (icon resolved from the active
|
|
`lineset` structure-tools entry). In 2D the floorplan overlay draws
|
|
this for every tool; in 3D each tool renders its own. The dimension
|
|
pill rides just above the cursor. */}
|
|
{cursorPos && (
|
|
<>
|
|
<CursorSphere color={PREVIEW_COLOR} position={cursorPos} ref={cursorRef} />
|
|
{pillParts && (
|
|
<group position={cursorPos}>
|
|
<Html
|
|
center
|
|
position={[0, 0.35, 0]}
|
|
style={{ pointerEvents: 'none', userSelect: 'none' }}
|
|
zIndexRange={[100, 0]}
|
|
>
|
|
<DimensionPill parts={pillParts} primary={pillPrimary} unit={unit} />
|
|
</Html>
|
|
</group>
|
|
)}
|
|
</>
|
|
)}
|
|
{snapTarget && (
|
|
<mesh layers={EDITOR_LAYER} position={snapTarget}>
|
|
<sphereGeometry args={[0.1, 24, 16]} />
|
|
<meshBasicMaterial color={PREVIEW_COLOR} depthTest={false} opacity={0.35} transparent />
|
|
</mesh>
|
|
)}
|
|
{draftPoints.map((p, i) => (
|
|
<mesh key={`pt-${i}`} layers={EDITOR_LAYER} position={p}>
|
|
<sphereGeometry args={[0.06, 16, 12]} />
|
|
<meshBasicMaterial color={PREVIEW_COLOR} depthTest={false} />
|
|
</mesh>
|
|
))}
|
|
{previewSegments.map((seg, i) => (
|
|
<PreviewSegment a={seg.a} b={seg.b} key={`seg-${i}`} />
|
|
))}
|
|
</LevelOffsetGroup>
|
|
)
|
|
}
|
|
|
|
function PreviewSegment({ a, b }: { a: [number, number, number]; b: [number, number, number] }) {
|
|
const start = new Vector3(...a)
|
|
const end = new Vector3(...b)
|
|
const dir = new Vector3().subVectors(end, start)
|
|
const length = dir.length()
|
|
if (length < 1e-4) return null
|
|
dir.normalize()
|
|
const mid = new Vector3().addVectors(start, end).multiplyScalar(0.5)
|
|
// Default suction OD (~7/8") for the ghost.
|
|
const radius = (0.875 * 0.0254) / 2
|
|
return (
|
|
<mesh
|
|
layers={EDITOR_LAYER}
|
|
position={mid.toArray()}
|
|
ref={(m) => {
|
|
if (!m) return
|
|
m.quaternion.setFromUnitVectors(new Vector3(0, 1, 0), dir)
|
|
}}
|
|
>
|
|
<cylinderGeometry args={[radius, radius, length, 16, 1, false]} />
|
|
<meshBasicMaterial
|
|
color={PREVIEW_COLOR}
|
|
depthTest={false}
|
|
opacity={PREVIEW_OPACITY}
|
|
transparent
|
|
/>
|
|
</mesh>
|
|
)
|
|
}
|
|
|
|
export default LinesetTool
|