2024-07-05 13:40:16 +10:00
import {
PathToNode ,
VariableDeclaration ,
VariableDeclarator ,
parse ,
recast ,
} from 'lang/wasm'
2024-05-30 13:28:29 +10:00
import { Axis , Selection , Selections , updateSelections } from 'lib/selections'
2024-09-09 19:59:36 +03:00
import { assign , fromPromise , setup } from 'xstate'
2024-07-02 17:16:27 +10:00
import { SidebarType } from 'components/ModelingSidebar/ModelingPanes'
2024-05-24 20:54:42 +10:00
import {
isNodeSafeToReplacePath ,
getNodePathFromSourceRange ,
} from 'lang/queryAst'
2024-03-28 17:22:11 +11:00
import {
kclManager ,
sceneInfra ,
sceneEntitiesManager ,
engineCommandManager ,
2024-04-19 14:24:40 -07:00
editorManager ,
2024-03-28 17:22:11 +11:00
} from 'lib/singletons'
2023-10-11 13:36:54 +11:00
import {
horzVertInfo ,
applyConstraintHorzVert ,
} from 'components/Toolbar/HorzVert'
import {
applyConstraintHorzVertAlign ,
horzVertDistanceInfo ,
} from 'components/Toolbar/SetHorzVertDistance'
import { angleBetweenInfo } from 'components/Toolbar/SetAngleBetween'
2023-12-01 20:18:51 +11:00
import { angleLengthInfo } from 'components/Toolbar/setAngleLength'
2023-10-11 13:36:54 +11:00
import {
applyConstraintEqualLength ,
setEqualLengthInfo ,
} from 'components/Toolbar/EqualLength'
2024-09-17 08:29:52 -05:00
import {
deleteFromSelection ,
extrudeSketch ,
revolveSketch ,
} from 'lang/modifyAst'
2024-08-26 08:07:20 +02:00
import { applyFilletToSelection } from 'lang/modifyAst/addFillet'
2023-10-11 13:36:54 +11:00
import { getNodeFromPath } from '../lang/queryAst'
2023-10-16 08:54:38 +11:00
import {
applyConstraintEqualAngle ,
equalAngleInfo ,
} from 'components/Toolbar/EqualAngle'
import {
applyRemoveConstrainingValues ,
removeConstrainingValuesInfo ,
} from 'components/Toolbar/RemoveConstrainingValues'
import { intersectInfo } from 'components/Toolbar/Intersect'
2023-12-01 20:18:51 +11:00
import {
absDistanceInfo ,
applyConstraintAxisAlign ,
} from 'components/Toolbar/SetAbsDistance'
Command bar: add extrude command, nonlinear editing, etc (#1204)
* Tweak toaster look and feel
* Add icons, tweak plus icon names
* Rename commandBarMeta to commandBarConfig
* Refactor command bar, add support for icons
* Create a tailwind plugin for aria-pressed button state
* Remove overlay from behind command bar
* Clean up toolbar
* Button and other style tweaks
* Icon tweaks follow-up: make old icons work with new sizing
* Delete unused static icons
* More CSS tweaks
* Small CSS tweak to project sidebar
* Add command bar E2E test
* fumpt
* A snapshot a day keeps the bugs away! 📷🐛 (OS: ubuntu)
* fix typo in a comment
* Fix icon padding (built version only)
* Update onboarding and warning banner icons padding
* Misc minor style fixes
* Get Extrude opening and canceling from command bar
* Iconography tweaks
* Get extrude kind of working
* Refactor command bar config types and organization
* Move command bar configs to be co-located with each other
* Start building a state machine for the command bar
* Start converting command bar to state machine
* Add support for multiple args, confirmation step
* Submission behavior, hotkeys, code organization
* Add new test for extruding from command bar
* Polish step back and selection hotkeys, CSS tweaks
* Loading style tweaks
* Validate selection inputs, polish UX of args re-editing
* Prevent submission with multiple selection on singlular arg
* Remove stray console logs
* Tweak test, CSS nit, remove extrude "result" argument
* Fix linting warnings
* Show Ctrl+/ instead of ⌘K on all platforms but Mac
* A snapshot a day keeps the bugs away! 📷🐛 (OS: ubuntu)
* Add "Enter sketch" to command bar
* fix command bar test
* Fix flaky cmd bar extrude test by waiting for engine select response
* Cover both button labels '⌘K' and 'Ctrl+/' in test
---------
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
2023-12-13 12:49:01 -05:00
import { ModelingCommandSchema } from 'lib/commandBarConfigs/modelingCommandConfig'
2024-09-09 18:17:45 -04:00
import { err , reportRejection , trap } from 'lib/trap'
2024-06-29 10:36:04 -07:00
import { DefaultPlaneStr , getFaceDetails } from 'clientSideScene/sceneEntities'
2024-04-03 19:38:16 +11:00
import { uuidv4 } from 'lib/utils'
2024-05-24 20:54:42 +10:00
import { Coords2d } from 'lang/std/sketch'
import { deleteSegment } from 'clientSideScene/ClientSideSceneComp'
2024-07-02 17:16:27 +10:00
import { executeAst } from 'lang/langHelpers'
2024-06-29 10:36:04 -07:00
import toast from 'react-hot-toast'
2024-09-09 19:59:36 +03:00
import { ToolbarModeName } from 'lib/toolbar'
2023-10-11 13:36:54 +11:00
export const MODELING_PERSIST_KEY = 'MODELING_PERSIST_KEY'
export type SetSelections =
| {
selectionType : 'singleCodeCursor'
selection? : Selection
}
| {
selectionType : 'otherSelection'
selection : Axis
}
| {
selectionType : 'completeSelection'
selection : Selections
2024-05-31 11:36:08 +10:00
updatedPathToNode? : PathToNode
2023-10-11 13:36:54 +11:00
}
| {
selectionType : 'mirrorCodeMirrorSelections'
selection : Selections
}
2024-04-04 11:07:51 +11:00
export type MouseState =
| {
type : 'idle'
}
| {
type : 'isHovering'
on : any
}
| {
type : 'isDragging'
on : any
}
2024-05-24 20:54:42 +10:00
| {
type : 'timeoutEnd'
pathToNodeString : string
}
2024-04-04 11:07:51 +11:00
2024-03-22 10:23:04 +11:00
export interface SketchDetails {
sketchPathToNode : PathToNode
zAxis : [ number , number , number ]
yAxis : [ number , number , number ]
origin : [ number , number , number ]
}
2024-05-24 20:54:42 +10:00
export interface SegmentOverlay {
windowCoords : Coords2d
angle : number
group : any
pathToNode : PathToNode
visible : boolean
}
export interface SegmentOverlays {
[ pathToNodeString : string ] : SegmentOverlay
}
2024-09-09 19:59:36 +03:00
export type ExtrudeFacePlane = {
type : 'extrudeFace'
position : [ number , number , number ]
sketchPathToNode : PathToNode
extrudePathToNode : PathToNode
cap : 'start' | 'end' | 'none'
faceId : string
zAxis : [ number , number , number ]
yAxis : [ number , number , number ]
}
export type DefaultPlane = {
type : 'defaultPlane'
plane : DefaultPlaneStr
planeId : string
zAxis : [ number , number , number ]
yAxis : [ number , number , number ]
}
2024-05-24 20:54:42 +10:00
export type SegmentOverlayPayload =
| {
type : 'set-one'
pathToNodeString : string
seg : SegmentOverlay
}
| {
type : 'delete-one'
pathToNodeString : string
}
| { type : 'clear' }
| {
type : 'set-many'
overlays : SegmentOverlays
}
2024-08-14 14:26:44 -04:00
export interface Store {
2024-07-04 01:55:06 -04:00
videoElement? : HTMLVideoElement
2024-07-02 17:16:27 +10:00
openPanes : SidebarType [ ]
}
2024-07-12 16:16:26 -04:00
export type SketchTool = 'line' | 'tangentialArc' | 'rectangle' | 'none'
2023-10-16 21:20:05 +11:00
export type ModelingMachineEvent =
2024-02-19 17:23:03 +11:00
| {
type : 'Enter sketch'
data ? : {
forceNewSketch? : boolean
}
}
2024-03-22 10:23:04 +11:00
| { type : 'Sketch On Face' }
2023-10-16 21:20:05 +11:00
| {
2024-02-11 12:59:00 +11:00
type : 'Select default plane'
2024-09-09 19:59:36 +03:00
data : DefaultPlane | ExtrudeFacePlane
2023-10-16 21:20:05 +11:00
}
2024-05-30 13:28:29 +10:00
| {
type : 'Set selection'
data : SetSelections
}
2024-06-29 10:36:04 -07:00
| {
type : 'Delete selection'
}
2023-10-16 21:20:05 +11:00
| { type : 'Sketch no face' }
| { type : 'Toggle gui mode' }
| { type : 'Cancel' }
| { type : 'CancelSketch' }
2024-02-11 12:59:00 +11:00
| { type : 'Add start point' }
2023-10-16 21:20:05 +11:00
| { type : 'Make segment horizontal' }
| { type : 'Make segment vertical' }
| { type : 'Constrain horizontal distance' }
2023-12-01 20:18:51 +11:00
| { type : 'Constrain ABS X' }
| { type : 'Constrain ABS Y' }
2023-10-16 21:20:05 +11:00
| { type : 'Constrain vertical distance' }
| { type : 'Constrain angle' }
| { type : 'Constrain perpendicular distance' }
| { type : 'Constrain horizontally align' }
| { type : 'Constrain vertically align' }
2023-12-01 20:18:51 +11:00
| { type : 'Constrain snap to X' }
| { type : 'Constrain snap to Y' }
2023-10-16 21:20:05 +11:00
| { type : 'Constrain length' }
| { type : 'Constrain equal length' }
| { type : 'Constrain parallel' }
2024-06-04 16:29:20 +10:00
| { type : 'Constrain remove constraints' ; data? : PathToNode }
2023-10-18 08:03:02 +11:00
| { type : 'Re-execute' }
2024-03-04 16:06:43 -05:00
| { type : 'Export' ; data : ModelingCommandSchema [ 'Export' ] }
2024-08-04 00:51:30 -04:00
| { type : 'Make' ; data : ModelingCommandSchema [ 'Make' ] }
Command bar: add extrude command, nonlinear editing, etc (#1204)
* Tweak toaster look and feel
* Add icons, tweak plus icon names
* Rename commandBarMeta to commandBarConfig
* Refactor command bar, add support for icons
* Create a tailwind plugin for aria-pressed button state
* Remove overlay from behind command bar
* Clean up toolbar
* Button and other style tweaks
* Icon tweaks follow-up: make old icons work with new sizing
* Delete unused static icons
* More CSS tweaks
* Small CSS tweak to project sidebar
* Add command bar E2E test
* fumpt
* A snapshot a day keeps the bugs away! 📷🐛 (OS: ubuntu)
* fix typo in a comment
* Fix icon padding (built version only)
* Update onboarding and warning banner icons padding
* Misc minor style fixes
* Get Extrude opening and canceling from command bar
* Iconography tweaks
* Get extrude kind of working
* Refactor command bar config types and organization
* Move command bar configs to be co-located with each other
* Start building a state machine for the command bar
* Start converting command bar to state machine
* Add support for multiple args, confirmation step
* Submission behavior, hotkeys, code organization
* Add new test for extruding from command bar
* Polish step back and selection hotkeys, CSS tweaks
* Loading style tweaks
* Validate selection inputs, polish UX of args re-editing
* Prevent submission with multiple selection on singlular arg
* Remove stray console logs
* Tweak test, CSS nit, remove extrude "result" argument
* Fix linting warnings
* Show Ctrl+/ instead of ⌘K on all platforms but Mac
* A snapshot a day keeps the bugs away! 📷🐛 (OS: ubuntu)
* Add "Enter sketch" to command bar
* fix command bar test
* Fix flaky cmd bar extrude test by waiting for engine select response
* Cover both button labels '⌘K' and 'Ctrl+/' in test
---------
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
2023-12-13 12:49:01 -05:00
| { type : 'Extrude' ; data? : ModelingCommandSchema [ 'Extrude' ] }
2024-09-17 08:29:52 -05:00
| { type : 'Revolve' ; data? : ModelingCommandSchema [ 'Revolve' ] }
2024-07-15 19:20:32 +10:00
| { type : 'Fillet' ; data? : ModelingCommandSchema [ 'Fillet' ] }
2024-08-14 14:26:44 -04:00
| { type : 'Text-to-CAD' ; data : ModelingCommandSchema [ 'Text-to-CAD' ] }
2024-04-19 11:56:21 -04:00
| {
type : 'Add rectangle origin'
data : [ x : number , y : number ]
}
2024-02-11 12:59:00 +11:00
| {
2024-09-09 19:59:36 +03:00
type : 'xstate.done.actor.animate-to-face'
output : SketchDetails
2024-02-11 12:59:00 +11:00
}
2024-09-09 19:59:36 +03:00
| { type : 'xstate.done.actor.animate-to-sketch' ; output : SketchDetails }
| { type : ` xstate.done.actor.do-constrain ${ string } ` ; output : SetSelections }
2024-04-04 11:07:51 +11:00
| { type : 'Set mouse state' ; data : MouseState }
2024-07-02 17:16:27 +10:00
| { type : 'Set context' ; data : Partial < Store > }
2024-05-24 20:54:42 +10:00
| {
type : 'Set Segment Overlays'
data : SegmentOverlayPayload
}
| {
type : 'Delete segment'
data : PathToNode
}
2024-05-06 19:28:30 +10:00
| {
type : 'code edit during sketch'
}
2024-05-24 20:54:42 +10:00
| {
type : 'Convert to variable'
data : {
pathToNode : PathToNode
variableName : string
}
}
2024-07-05 13:40:16 +10:00
| {
type : 'change tool'
2024-07-12 16:16:26 -04:00
data : {
tool : SketchTool
}
2024-07-05 13:40:16 +10:00
}
| { type : 'Finish rectangle' }
2024-08-03 07:05:35 +10:00
| { type : 'Artifact graph populated' }
| { type : 'Artifact graph emptied' }
2024-02-11 12:59:00 +11:00
export type MoveDesc = { line : number ; snippet : string }
2023-10-16 21:20:05 +11:00
2024-07-02 17:16:27 +10:00
export const PERSIST_MODELING_CONTEXT = 'persistModelingContext'
interface PersistedModelingContext {
openPanes : Store [ 'openPanes' ]
}
type PersistedKeys = keyof PersistedModelingContext
export const PersistedValues : PersistedKeys [ ] = [ 'openPanes' ]
2024-08-02 15:39:05 -04:00
export const getPersistedContext = ( ) : Partial < PersistedModelingContext > = > {
const c = ( typeof window !== 'undefined' &&
JSON . parse ( localStorage . getItem ( PERSIST_MODELING_CONTEXT ) || '{}' ) ) || {
openPanes : [ 'code' ] ,
}
return c
}
2024-09-09 19:59:36 +03:00
export interface ModelingMachineContext {
currentMode : ToolbarModeName
currentTool : SketchTool
selection : string [ ]
selectionRanges : Selections
sketchDetails : SketchDetails | null
sketchPlaneId : string
sketchEnginePathId : string
moveDescs : MoveDesc [ ]
mouseState : MouseState
segmentOverlays : SegmentOverlays
segmentHoverMap : { [ pathToNodeString : string ] : number }
store : Store
}
export const modelingMachineDefaultContext : ModelingMachineContext = {
currentMode : 'modeling' ,
currentTool : 'none' ,
selection : [ ] ,
2024-08-02 15:39:05 -04:00
selectionRanges : {
otherSelections : [ ] ,
codeBasedSelections : [ ] ,
2024-09-09 19:59:36 +03:00
} ,
2024-08-02 15:39:05 -04:00
sketchDetails : {
sketchPathToNode : [ ] ,
zAxis : [ 0 , 0 , 1 ] ,
yAxis : [ 0 , 1 , 0 ] ,
origin : [ 0 , 0 , 0 ] ,
2024-09-09 19:59:36 +03:00
} ,
sketchPlaneId : '' ,
sketchEnginePathId : '' ,
moveDescs : [ ] ,
mouseState : { type : 'idle' } ,
segmentOverlays : { } ,
segmentHoverMap : { } ,
2024-08-02 15:39:05 -04:00
store : {
openPanes : getPersistedContext ( ) . openPanes || [ 'code' ] ,
2024-09-09 19:59:36 +03:00
} ,
2024-07-02 17:16:27 +10:00
}
2024-09-09 19:59:36 +03:00
export const modelingMachine = setup ( {
types : {
context : { } as ModelingMachineContext ,
events : { } as ModelingMachineEvent ,
input : { } as ModelingMachineContext ,
} ,
guards : {
'Selection is on face' : ( ) = > false ,
'has valid extrude selection' : ( ) = > false ,
2024-09-17 08:29:52 -05:00
'has valid revolve selection' : ( ) = > false ,
2024-09-09 19:59:36 +03:00
'has valid fillet selection' : ( ) = > false ,
'Has exportable geometry' : ( ) = > false ,
'has valid selection for deletion' : ( ) = > false ,
'has made first point' : ( { context } ) = > {
if ( ! context . sketchDetails ? . sketchPathToNode ) return false
const variableDeclaration = getNodeFromPath < VariableDeclarator > (
kclManager . ast ,
context . sketchDetails . sketchPathToNode ,
'VariableDeclarator'
)
if ( err ( variableDeclaration ) ) return false
if ( variableDeclaration . node . type !== 'VariableDeclarator' ) return false
const pipeExpression = variableDeclaration . node . init
if ( pipeExpression . type !== 'PipeExpression' ) return false
const hasStartSketchOn = pipeExpression . body . some (
( item ) = >
item . type === 'CallExpression' && item . callee . name === 'startSketchOn'
)
return hasStartSketchOn && pipeExpression . body . length > 1
2023-10-11 13:36:54 +11:00
} ,
2024-09-09 19:59:36 +03:00
'is editing existing sketch' : ( { context : { sketchDetails } } ) = >
isEditingExistingSketch ( { sketchDetails } ) ,
'Can make selection horizontal' : ( { context : { selectionRanges } } ) = > {
const info = horzVertInfo ( selectionRanges , 'horizontal' )
if ( trap ( info ) ) return false
return info . enabled
} ,
'Can make selection vertical' : ( { context : { selectionRanges } } ) = > {
const info = horzVertInfo ( selectionRanges , 'vertical' )
if ( trap ( info ) ) return false
return info . enabled
} ,
'Can constrain horizontal distance' : ( { context : { selectionRanges } } ) = > {
const info = horzVertDistanceInfo ( {
selectionRanges ,
constraint : 'setHorzDistance' ,
} )
if ( trap ( info ) ) return false
return info . enabled
} ,
'Can constrain vertical distance' : ( { context : { selectionRanges } } ) = > {
const info = horzVertDistanceInfo ( {
selectionRanges ,
constraint : 'setVertDistance' ,
} )
if ( trap ( info ) ) return false
return info . enabled
} ,
'Can constrain ABS X' : ( { context : { selectionRanges } } ) = > {
const info = absDistanceInfo ( { selectionRanges , constraint : 'xAbs' } )
if ( trap ( info ) ) return false
return info . enabled
} ,
'Can constrain ABS Y' : ( { context : { selectionRanges } } ) = > {
const info = absDistanceInfo ( { selectionRanges , constraint : 'yAbs' } )
if ( trap ( info ) ) return false
return info . enabled
} ,
'Can constrain angle' : ( { context : { selectionRanges } } ) = > {
const angleBetween = angleBetweenInfo ( { selectionRanges } )
if ( trap ( angleBetween ) ) return false
const angleLength = angleLengthInfo ( {
selectionRanges ,
angleOrLength : 'setAngle' ,
} )
if ( trap ( angleLength ) ) return false
return angleBetween . enabled || angleLength . enabled
} ,
'Can constrain length' : ( { context : { selectionRanges } } ) = > {
const angleLength = angleLengthInfo ( { selectionRanges } )
if ( trap ( angleLength ) ) return false
return angleLength . enabled
} ,
'Can constrain perpendicular distance' : ( {
context : { selectionRanges } ,
} ) = > {
const info = intersectInfo ( { selectionRanges } )
if ( trap ( info ) ) return false
return info . enabled
} ,
'Can constrain horizontally align' : ( { context : { selectionRanges } } ) = > {
const info = horzVertDistanceInfo ( {
selectionRanges ,
constraint : 'setHorzDistance' ,
} )
if ( trap ( info ) ) return false
return info . enabled
} ,
'Can constrain vertically align' : ( { context : { selectionRanges } } ) = > {
const info = horzVertDistanceInfo ( {
selectionRanges ,
constraint : 'setHorzDistance' ,
} )
if ( trap ( info ) ) return false
return info . enabled
} ,
'Can constrain snap to X' : ( { context : { selectionRanges } } ) = > {
const info = absDistanceInfo ( {
selectionRanges ,
constraint : 'snapToXAxis' ,
} )
if ( trap ( info ) ) return false
return info . enabled
} ,
'Can constrain snap to Y' : ( { context : { selectionRanges } } ) = > {
const info = absDistanceInfo ( {
selectionRanges ,
constraint : 'snapToYAxis' ,
} )
if ( trap ( info ) ) return false
return info . enabled
} ,
'Can constrain equal length' : ( { context : { selectionRanges } } ) = > {
const info = setEqualLengthInfo ( { selectionRanges } )
if ( trap ( info ) ) return false
return info . enabled
} ,
'Can canstrain parallel' : ( { context : { selectionRanges } } ) = > {
const info = equalAngleInfo ( { selectionRanges } )
if ( err ( info ) ) return false
return info . enabled
} ,
'Can constrain remove constraints' : ( {
context : { selectionRanges } ,
event ,
} ) = > {
if ( event . type !== 'Constrain remove constraints' ) return false
const info = removeConstrainingValuesInfo ( {
selectionRanges ,
pathToNodes : event.data && [ event . data ] ,
} )
if ( trap ( info ) ) return false
return info . enabled
} ,
'Can convert to variable' : ( { event } ) = > {
if ( event . type !== 'Convert to variable' ) return false
if ( ! event . data ) return false
const ast = parse ( recast ( kclManager . ast ) )
if ( err ( ast ) ) return false
const isSafeRetVal = isNodeSafeToReplacePath ( ast , event . data . pathToNode )
if ( err ( isSafeRetVal ) ) return false
return isSafeRetVal . isSafe
} ,
'next is tangential arc' : ( { context : { sketchDetails , currentTool } } ) = >
currentTool === 'tangentialArc' &&
isEditingExistingSketch ( { sketchDetails } ) ,
'next is rectangle' : ( { context : { sketchDetails , currentTool } } ) = >
currentTool === 'rectangle' && canRectangleTool ( { sketchDetails } ) ,
'next is line' : ( { context } ) = > context . currentTool === 'line' ,
2024-09-11 06:09:39 +10:00
'next is none' : ( { context } ) = > context . currentTool === 'none' ,
2024-09-09 19:59:36 +03:00
} ,
// end guards
actions : {
'assign tool in context' : assign ( {
currentTool : ( { event } ) = >
'data' in event && event . data && 'tool' in event . data
? event . data . tool
: 'none' ,
} ) ,
'enter sketching mode' : assign ( { currentMode : 'sketching' } ) ,
'enter modeling mode' : assign ( { currentMode : 'modeling' } ) ,
'set sketchMetadata from pathToNode' : assign (
( { context : { sketchDetails } } ) = > {
if ( ! sketchDetails ? . sketchPathToNode || ! sketchDetails ) return { }
return {
sketchDetails : {
. . . sketchDetails ,
sketchPathToNode : sketchDetails.sketchPathToNode ,
2024-08-14 14:26:44 -04:00
} ,
2024-09-09 19:59:36 +03:00
}
}
) ,
2024-09-09 18:17:45 -04:00
// eslint-disable-next-line @typescript-eslint/no-misused-promises
2024-09-09 19:59:36 +03:00
'hide default planes' : ( ) = > kclManager . hidePlanes ( ) ,
'reset sketch metadata' : assign ( {
sketchDetails : null ,
sketchEnginePathId : '' ,
sketchPlaneId : '' ,
} ) ,
2024-09-09 18:17:45 -04:00
'reset camera position' : ( ) = > {
// eslint-disable-next-line @typescript-eslint/no-floating-promises
2024-09-09 19:59:36 +03:00
engineCommandManager . sendSceneCommand ( {
type : 'modeling_cmd_req' ,
cmd_id : uuidv4 ( ) ,
cmd : {
type : 'default_camera_look_at' ,
center : { x : 0 , y : 0 , z : 0 } ,
vantage : { x : 0 , y : - 1250 , z : 580 } ,
up : { x : 0 , y : 0 , z : 1 } ,
2023-10-11 13:36:54 +11:00
} ,
2024-09-09 18:17:45 -04:00
} )
} ,
2024-09-09 19:59:36 +03:00
'set new sketch metadata' : assign ( ( { event } ) = > {
if (
event . type !== 'xstate.done.actor.animate-to-sketch' &&
event . type !== 'xstate.done.actor.animate-to-face'
)
return { }
return {
sketchDetails : event.output ,
}
} ) ,
2024-09-09 18:17:45 -04:00
'AST extrude' : ( { context : { store } , event } ) = > {
2024-09-09 19:59:36 +03:00
if ( event . type !== 'Extrude' ) return
2024-09-09 18:17:45 -04:00
; ( async ( ) = > {
if ( ! event . data ) return
const { selection , distance } = event . data
let ast = kclManager . ast
if (
'variableName' in distance &&
distance . variableName &&
distance . insertIndex !== undefined
) {
const newBody = [ . . . ast . body ]
newBody . splice (
distance . insertIndex ,
0 ,
distance . variableDeclarationAst
)
ast . body = newBody
}
const pathToNode = getNodePathFromSourceRange (
ast ,
selection . codeBasedSelections [ 0 ] . range
)
const extrudeSketchRes = extrudeSketch (
ast ,
pathToNode ,
false ,
'variableName' in distance
? distance . variableIdentifierAst
: distance . valueAst
)
if ( trap ( extrudeSketchRes ) ) return
const { modifiedAst , pathToExtrudeArg } = extrudeSketchRes
2024-09-09 19:59:36 +03:00
2024-09-09 18:17:45 -04:00
store . videoElement ? . pause ( )
const updatedAst = await kclManager . updateAst ( modifiedAst , true , {
focusPath : pathToExtrudeArg ,
zoomToFit : true ,
zoomOnRangeAndType : {
range : selection.codeBasedSelections [ 0 ] . range ,
type : 'path' ,
} ,
2024-09-09 19:59:36 +03:00
} )
2024-09-09 18:17:45 -04:00
if ( ! engineCommandManager . engineConnection ? . idleMode ) {
store . videoElement ? . play ( ) . catch ( ( e ) = > {
console . warn ( 'Video playing was prevented' , e )
} )
}
if ( updatedAst ? . selections ) {
editorManager . selectRange ( updatedAst ? . selections )
}
} ) ( ) . catch ( reportRejection )
2024-09-09 19:59:36 +03:00
} ,
2024-09-17 08:29:52 -05:00
'AST revolve' : ( { context : { store } , event } ) = > {
if ( event . type !== 'Revolve' ) return
; ( async ( ) = > {
if ( ! event . data ) return
const { selection , angle } = event . data
let ast = kclManager . ast
if (
'variableName' in angle &&
angle . variableName &&
angle . insertIndex !== undefined
) {
const newBody = [ . . . ast . body ]
newBody . splice ( angle . insertIndex , 0 , angle . variableDeclarationAst )
ast . body = newBody
}
const pathToNode = getNodePathFromSourceRange (
ast ,
selection . codeBasedSelections [ 0 ] . range
)
const revolveSketchRes = revolveSketch (
ast ,
pathToNode ,
false ,
'variableName' in angle ? angle.variableIdentifierAst : angle.valueAst
)
if ( trap ( revolveSketchRes ) ) return
const { modifiedAst , pathToRevolveArg } = revolveSketchRes
store . videoElement ? . pause ( )
const updatedAst = await kclManager . updateAst ( modifiedAst , true , {
focusPath : pathToRevolveArg ,
zoomToFit : true ,
zoomOnRangeAndType : {
range : selection.codeBasedSelections [ 0 ] . range ,
type : 'path' ,
} ,
} )
if ( ! engineCommandManager . engineConnection ? . idleMode ) {
store . videoElement ? . play ( ) . catch ( ( e ) = > {
console . warn ( 'Video playing was prevented' , e )
} )
}
if ( updatedAst ? . selections ) {
editorManager . selectRange ( updatedAst ? . selections )
}
} ) ( ) . catch ( reportRejection )
} ,
2024-09-09 18:17:45 -04:00
'AST delete selection' : ( { context : { selectionRanges } } ) = > {
; ( async ( ) = > {
let ast = kclManager . ast
2024-09-09 19:59:36 +03:00
2024-09-09 18:17:45 -04:00
const modifiedAst = await deleteFromSelection (
ast ,
selectionRanges . codeBasedSelections [ 0 ] ,
kclManager . programMemory ,
getFaceDetails
)
if ( err ( modifiedAst ) ) return
2024-09-09 19:59:36 +03:00
2024-09-09 18:17:45 -04:00
const testExecute = await executeAst ( {
ast : modifiedAst ,
useFakeExecutor : true ,
engineCommandManager ,
} )
if ( testExecute . errors . length ) {
toast . error ( 'Unable to delete part' )
return
}
2024-02-19 12:41:36 +11:00
2024-09-09 18:17:45 -04:00
await kclManager . updateAst ( modifiedAst , true )
} ) ( ) . catch ( reportRejection )
2024-09-09 19:59:36 +03:00
} ,
2024-09-09 18:17:45 -04:00
'AST fillet' : ( { event } ) = > {
2024-09-09 19:59:36 +03:00
if ( event . type !== 'Fillet' ) return
if ( ! event . data ) return
2024-08-03 07:05:35 +10:00
2024-09-09 19:59:36 +03:00
// Extract inputs
2024-09-12 21:45:26 +02:00
const ast = kclManager . ast
2024-09-09 19:59:36 +03:00
const { selection , radius } = event . data
2024-08-03 07:05:35 +10:00
2024-09-09 19:59:36 +03:00
// Apply fillet to selection
const applyFilletToSelectionResult = applyFilletToSelection (
2024-09-12 21:45:26 +02:00
ast ,
2024-09-09 19:59:36 +03:00
selection ,
radius
)
if ( err ( applyFilletToSelectionResult ) ) return applyFilletToSelectionResult
} ,
2024-09-12 22:06:50 -04:00
'set selection filter to curves only' : ( ) = > {
; ( async ( ) = > {
await engineCommandManager . sendSceneCommand ( {
type : 'modeling_cmd_req' ,
cmd_id : uuidv4 ( ) ,
cmd : {
type : 'set_selection_filter' ,
filter : [ 'curve' ] ,
} ,
} )
} ) ( ) . catch ( reportRejection )
} ,
2024-09-09 19:59:36 +03:00
'conditionally equip line tool' : ( { event : { type } } ) = > {
if ( type === 'xstate.done.actor.animate-to-face' ) {
sceneInfra . modelingSend ( {
type : 'change tool' ,
data : { tool : 'line' } ,
} )
}
} ,
'setup client side sketch segments' : ( {
context : { sketchDetails , selectionRanges } ,
} ) = > {
if ( ! sketchDetails ) return
; ( async ( ) = > {
if ( Object . keys ( sceneEntitiesManager . activeSegments ) . length > 0 ) {
await sceneEntitiesManager . tearDownSketch ( { removeAxis : false } )
}
sceneInfra . resetMouseListeners ( )
await sceneEntitiesManager . setupSketch ( {
sketchPathToNode : sketchDetails?.sketchPathToNode || [ ] ,
forward : sketchDetails.zAxis ,
up : sketchDetails.yAxis ,
position : sketchDetails.origin ,
maybeModdedAst : kclManager.ast ,
selectionRanges ,
} )
sceneInfra . resetMouseListeners ( )
sceneEntitiesManager . setupSketchIdleCallbacks ( {
pathToNode : sketchDetails?.sketchPathToNode || [ ] ,
forward : sketchDetails.zAxis ,
up : sketchDetails.yAxis ,
position : sketchDetails.origin ,
} )
2024-09-09 18:17:45 -04:00
} ) ( ) . catch ( reportRejection )
2024-09-09 19:59:36 +03:00
} ,
'tear down client sketch' : ( ) = > {
if ( sceneEntitiesManager . activeSegments ) {
2024-09-09 18:17:45 -04:00
// eslint-disable-next-line @typescript-eslint/no-floating-promises
2024-09-09 19:59:36 +03:00
sceneEntitiesManager . tearDownSketch ( { removeAxis : false } )
}
} ,
'remove sketch grid' : ( ) = > sceneEntitiesManager . removeSketchGrid ( ) ,
'set up draft line' : ( { context : { sketchDetails } } ) = > {
if ( ! sketchDetails ) return
2024-09-09 18:17:45 -04:00
// eslint-disable-next-line @typescript-eslint/no-floating-promises
2024-09-09 19:59:36 +03:00
sceneEntitiesManager . setUpDraftSegment (
sketchDetails . sketchPathToNode ,
sketchDetails . zAxis ,
sketchDetails . yAxis ,
sketchDetails . origin ,
'line'
)
} ,
'set up draft arc' : ( { context : { sketchDetails } } ) = > {
if ( ! sketchDetails ) return
2024-09-09 18:17:45 -04:00
// eslint-disable-next-line @typescript-eslint/no-floating-promises
2024-09-09 19:59:36 +03:00
sceneEntitiesManager . setUpDraftSegment (
sketchDetails . sketchPathToNode ,
sketchDetails . zAxis ,
sketchDetails . yAxis ,
sketchDetails . origin ,
'tangentialArcTo'
)
} ,
'listen for rectangle origin' : ( { context : { sketchDetails } } ) = > {
if ( ! sketchDetails ) return
sceneEntitiesManager . setupNoPointsListener ( {
sketchDetails ,
afterClick : ( args ) = > {
const twoD = args . intersectionPoint ? . twoD
if ( twoD ) {
sceneInfra . modelingSend ( {
type : 'Add rectangle origin' ,
data : [ twoD . x , twoD . y ] ,
} )
} else {
console . error ( 'No intersection point found' )
}
} ,
} )
} ,
'set up draft rectangle' : ( { context : { sketchDetails } , event } ) = > {
if ( event . type !== 'Add rectangle origin' ) return
if ( ! sketchDetails || ! event . data ) return
2024-09-09 18:17:45 -04:00
// eslint-disable-next-line @typescript-eslint/no-floating-promises
2024-09-09 19:59:36 +03:00
sceneEntitiesManager . setupDraftRectangle (
sketchDetails . sketchPathToNode ,
sketchDetails . zAxis ,
sketchDetails . yAxis ,
sketchDetails . origin ,
event . data
)
} ,
'set up draft line without teardown' : ( { context : { sketchDetails } } ) = > {
if ( ! sketchDetails ) return
2024-09-09 18:17:45 -04:00
// eslint-disable-next-line @typescript-eslint/no-floating-promises
2024-09-09 19:59:36 +03:00
sceneEntitiesManager . setUpDraftSegment (
sketchDetails . sketchPathToNode ,
sketchDetails . zAxis ,
sketchDetails . yAxis ,
sketchDetails . origin ,
'line' ,
false
)
} ,
2024-09-09 18:17:45 -04:00
'show default planes' : ( ) = > {
// eslint-disable-next-line @typescript-eslint/no-floating-promises
kclManager . showPlanes ( )
} ,
2024-09-09 19:59:36 +03:00
'setup noPoints onClick listener' : ( { context : { sketchDetails } } ) = > {
if ( ! sketchDetails ) return
2024-08-03 07:05:35 +10:00
2024-09-09 19:59:36 +03:00
sceneEntitiesManager . setupNoPointsListener ( {
sketchDetails ,
afterClick : ( ) = > sceneInfra . modelingSend ( { type : 'Add start point' } ) ,
} )
} ,
'add axis n grid' : ( { context : { sketchDetails } } ) = > {
if ( ! sketchDetails ) return
if ( localStorage . getItem ( 'disableAxis' ) ) return
sceneEntitiesManager . createSketchAxis (
sketchDetails . sketchPathToNode || [ ] ,
sketchDetails . zAxis ,
sketchDetails . yAxis ,
sketchDetails . origin
)
} ,
'reset client scene mouse handlers' : ( ) = > {
// when not in sketch mode we don't need any mouse listeners
// (note the orbit controls are always active though)
sceneInfra . resetMouseListeners ( )
} ,
'clientToEngine cam sync direction' : ( ) = > {
sceneInfra . camControls . syncDirection = 'clientToEngine'
} ,
'engineToClient cam sync direction' : ( ) = > {
sceneInfra . camControls . syncDirection = 'engineToClient'
} ,
2024-09-09 18:17:45 -04:00
'set selection filter to faces only' : ( ) = > {
// eslint-disable-next-line @typescript-eslint/no-floating-promises
2024-09-09 19:59:36 +03:00
engineCommandManager . sendSceneCommand ( {
type : 'modeling_cmd_req' ,
cmd_id : uuidv4 ( ) ,
cmd : {
type : 'set_selection_filter' ,
filter : [ 'face' , 'object' ] ,
2024-08-03 07:05:35 +10:00
} ,
2024-09-09 18:17:45 -04:00
} )
} ,
2024-09-09 19:59:36 +03:00
'set selection filter to defaults' : ( ) = >
kclManager . defaultSelectionFilter ( ) ,
'Delete segment' : ( { context : { sketchDetails } , event } ) = > {
if ( event . type !== 'Delete segment' ) return
if ( ! sketchDetails || ! event . data ) return
2024-09-09 18:17:45 -04:00
// eslint-disable-next-line @typescript-eslint/no-floating-promises
deleteSegment ( {
2024-09-09 19:59:36 +03:00
pathToNode : event.data ,
sketchDetails ,
} )
} ,
'Reset Segment Overlays' : ( ) = > sceneEntitiesManager . resetOverlays ( ) ,
'Set context' : assign ( {
store : ( { context : { store } , event } ) = > {
if ( event . type !== 'Set context' ) return store
if ( ! event . data ) return store
2024-08-03 07:05:35 +10:00
2024-09-09 19:59:36 +03:00
const result = {
. . . store ,
. . . event . data ,
}
const persistedContext : Partial < PersistedModelingContext > = { }
for ( const key of PersistedValues ) {
persistedContext [ key ] = result [ key ]
}
if ( typeof window !== 'undefined' ) {
window . localStorage . setItem (
PERSIST_MODELING_CONTEXT ,
JSON . stringify ( persistedContext )
)
}
return result
2023-10-11 13:36:54 +11:00
} ,
2024-09-09 19:59:36 +03:00
} ) ,
Make : ( ) = > { } ,
'enable copilot' : ( ) = > { } ,
'disable copilot' : ( ) = > { } ,
'Set selection' : ( ) = > { } ,
'Set mouse state' : ( ) = > { } ,
'Set Segment Overlays' : ( ) = > { } ,
'Engine export' : ( ) = > { } ,
'Submit to Text-to-CAD API' : ( ) = > { } ,
'Set sketchDetails' : ( ) = > { } ,
'sketch exit execute' : ( ) = > { } ,
2023-10-11 13:36:54 +11:00
} ,
2024-09-09 19:59:36 +03:00
// end actions
actors : {
'do-constrain-remove-constraint' : fromPromise (
async ( {
input : { selectionRanges , sketchDetails , data } ,
} : {
input : Pick <
ModelingMachineContext ,
'selectionRanges' | 'sketchDetails'
> & { data? : PathToNode }
2024-06-22 04:49:31 -04:00
} ) = > {
2024-06-24 11:45:40 -04:00
const constraint = applyRemoveConstrainingValues ( {
2024-06-03 22:40:59 +10:00
selectionRanges ,
2024-06-04 16:29:20 +10:00
pathToNodes : data && [ data ] ,
2024-06-03 22:40:59 +10:00
} )
2024-06-24 11:45:40 -04:00
if ( trap ( constraint ) ) return
const { pathToNodeMap } = constraint
2024-06-03 22:40:59 +10:00
if ( ! sketchDetails ) return
2024-06-24 11:45:40 -04:00
let updatedAst = await sceneEntitiesManager . updateAstAndRejigSketch (
pathToNodeMap [ 0 ] ,
constraint . modifiedAst ,
2024-06-03 22:40:59 +10:00
sketchDetails . zAxis ,
sketchDetails . yAxis ,
sketchDetails . origin
)
2024-06-24 11:45:40 -04:00
if ( trap ( updatedAst , { suppress : true } ) ) return
if ( ! updatedAst ) return
2024-06-03 22:40:59 +10:00
return {
selectionType : 'completeSelection' ,
selection : updateSelections (
pathToNodeMap ,
selectionRanges ,
2024-06-24 11:45:40 -04:00
updatedAst . newAst
2024-06-03 22:40:59 +10:00
) ,
}
2024-09-09 19:59:36 +03:00
}
) ,
'do-constrain-horizontally' : fromPromise (
async ( {
input : { selectionRanges , sketchDetails } ,
} : {
input : Pick < ModelingMachineContext , 'selectionRanges' | 'sketchDetails' >
2024-05-30 19:43:35 +10:00
} ) = > {
2024-06-24 11:45:40 -04:00
const constraint = applyConstraintHorzVert (
2024-05-30 19:43:35 +10:00
selectionRanges ,
'horizontal' ,
kclManager . ast ,
kclManager . programMemory
)
2024-06-24 11:45:40 -04:00
if ( trap ( constraint ) ) return false
const { modifiedAst , pathToNodeMap } = constraint
2024-05-30 19:43:35 +10:00
if ( ! sketchDetails ) return
2024-06-24 11:45:40 -04:00
const updatedAst = await sceneEntitiesManager . updateAstAndRejigSketch (
2024-05-30 19:43:35 +10:00
sketchDetails . sketchPathToNode ,
modifiedAst ,
sketchDetails . zAxis ,
sketchDetails . yAxis ,
sketchDetails . origin
)
2024-06-24 11:45:40 -04:00
if ( trap ( updatedAst , { suppress : true } ) ) return
if ( ! updatedAst ) return
2024-05-30 19:43:35 +10:00
return {
selectionType : 'completeSelection' ,
selection : updateSelections (
pathToNodeMap ,
selectionRanges ,
2024-06-24 11:45:40 -04:00
updatedAst . newAst
2024-05-30 19:43:35 +10:00
) ,
}
2024-09-09 19:59:36 +03:00
}
) ,
'do-constrain-vertically' : fromPromise (
async ( {
input : { selectionRanges , sketchDetails } ,
} : {
input : Pick < ModelingMachineContext , 'selectionRanges' | 'sketchDetails' >
} ) = > {
2024-06-24 11:45:40 -04:00
const constraint = applyConstraintHorzVert (
2024-05-30 19:43:35 +10:00
selectionRanges ,
'vertical' ,
kclManager . ast ,
kclManager . programMemory
)
2024-06-24 11:45:40 -04:00
if ( trap ( constraint ) ) return false
const { modifiedAst , pathToNodeMap } = constraint
2024-05-30 19:43:35 +10:00
if ( ! sketchDetails ) return
2024-06-24 11:45:40 -04:00
const updatedAst = await sceneEntitiesManager . updateAstAndRejigSketch (
2024-05-30 19:43:35 +10:00
sketchDetails . sketchPathToNode || [ ] ,
modifiedAst ,
sketchDetails . zAxis ,
sketchDetails . yAxis ,
sketchDetails . origin
)
2024-06-24 11:45:40 -04:00
if ( trap ( updatedAst , { suppress : true } ) ) return
if ( ! updatedAst ) return
2024-05-30 19:43:35 +10:00
return {
selectionType : 'completeSelection' ,
selection : updateSelections (
pathToNodeMap ,
selectionRanges ,
2024-06-24 11:45:40 -04:00
updatedAst . newAst
2024-05-30 19:43:35 +10:00
) ,
}
2024-09-09 19:59:36 +03:00
}
) ,
'do-constrain-horizontally-align' : fromPromise (
async ( {
input : { selectionRanges , sketchDetails } ,
} : {
input : Pick < ModelingMachineContext , 'selectionRanges' | 'sketchDetails' >
2024-05-30 13:28:29 +10:00
} ) = > {
2024-06-24 11:45:40 -04:00
const constraint = applyConstraintHorzVertAlign ( {
2024-05-30 13:28:29 +10:00
selectionRanges ,
constraint : 'setVertDistance' ,
} )
2024-06-24 11:45:40 -04:00
if ( trap ( constraint ) ) return
const { modifiedAst , pathToNodeMap } = constraint
2024-05-30 13:28:29 +10:00
if ( ! sketchDetails ) return
2024-06-24 11:45:40 -04:00
const updatedAst = await sceneEntitiesManager . updateAstAndRejigSketch (
2024-05-30 13:28:29 +10:00
sketchDetails ? . sketchPathToNode || [ ] ,
modifiedAst ,
sketchDetails . zAxis ,
sketchDetails . yAxis ,
sketchDetails . origin
)
2024-06-24 11:45:40 -04:00
if ( trap ( updatedAst , { suppress : true } ) ) return
if ( ! updatedAst ) return
2024-05-30 13:28:29 +10:00
const updatedSelectionRanges = updateSelections (
pathToNodeMap ,
selectionRanges ,
2024-06-24 11:45:40 -04:00
updatedAst . newAst
2024-05-30 13:28:29 +10:00
)
return {
selectionType : 'completeSelection' ,
selection : updatedSelectionRanges ,
}
2024-09-09 19:59:36 +03:00
}
) ,
'do-constrain-vertically-align' : fromPromise (
async ( {
input : { selectionRanges , sketchDetails } ,
} : {
input : Pick < ModelingMachineContext , 'selectionRanges' | 'sketchDetails' >
2024-05-30 13:28:29 +10:00
} ) = > {
2024-06-24 11:45:40 -04:00
const constraint = applyConstraintHorzVertAlign ( {
2024-05-30 13:28:29 +10:00
selectionRanges ,
constraint : 'setHorzDistance' ,
} )
2024-06-24 11:45:40 -04:00
if ( trap ( constraint ) ) return
const { modifiedAst , pathToNodeMap } = constraint
2024-05-30 13:28:29 +10:00
if ( ! sketchDetails ) return
2024-06-24 11:45:40 -04:00
const updatedAst = await sceneEntitiesManager . updateAstAndRejigSketch (
2024-05-30 13:28:29 +10:00
sketchDetails ? . sketchPathToNode || [ ] ,
modifiedAst ,
sketchDetails . zAxis ,
sketchDetails . yAxis ,
sketchDetails . origin
)
2024-06-24 11:45:40 -04:00
if ( trap ( updatedAst , { suppress : true } ) ) return
if ( ! updatedAst ) return
2024-05-30 13:28:29 +10:00
const updatedSelectionRanges = updateSelections (
pathToNodeMap ,
selectionRanges ,
2024-06-24 11:45:40 -04:00
updatedAst . newAst
2024-05-30 13:28:29 +10:00
)
return {
selectionType : 'completeSelection' ,
selection : updatedSelectionRanges ,
}
2024-09-09 19:59:36 +03:00
}
) ,
'do-constrain-snap-to-x' : fromPromise (
async ( {
input : { selectionRanges , sketchDetails } ,
} : {
input : Pick < ModelingMachineContext , 'selectionRanges' | 'sketchDetails' >
} ) = > {
2024-06-24 11:45:40 -04:00
const constraint = applyConstraintAxisAlign ( {
2024-05-30 13:28:29 +10:00
selectionRanges ,
constraint : 'snapToXAxis' ,
} )
2024-06-24 11:45:40 -04:00
if ( err ( constraint ) ) return false
const { modifiedAst , pathToNodeMap } = constraint
2024-05-30 13:28:29 +10:00
if ( ! sketchDetails ) return
2024-06-24 11:45:40 -04:00
const updatedAst = await sceneEntitiesManager . updateAstAndRejigSketch (
2024-05-30 13:28:29 +10:00
sketchDetails ? . sketchPathToNode || [ ] ,
modifiedAst ,
sketchDetails . zAxis ,
sketchDetails . yAxis ,
sketchDetails . origin
)
2024-06-24 11:45:40 -04:00
if ( trap ( updatedAst , { suppress : true } ) ) return
if ( ! updatedAst ) return
2024-05-30 13:28:29 +10:00
const updatedSelectionRanges = updateSelections (
pathToNodeMap ,
selectionRanges ,
2024-06-24 11:45:40 -04:00
updatedAst . newAst
2024-05-30 13:28:29 +10:00
)
return {
selectionType : 'completeSelection' ,
selection : updatedSelectionRanges ,
}
2024-09-09 19:59:36 +03:00
}
) ,
'do-constrain-snap-to-y' : fromPromise (
async ( {
input : { selectionRanges , sketchDetails } ,
} : {
input : Pick < ModelingMachineContext , 'selectionRanges' | 'sketchDetails' >
} ) = > {
2024-06-24 11:45:40 -04:00
const constraint = applyConstraintAxisAlign ( {
2024-05-30 13:28:29 +10:00
selectionRanges ,
constraint : 'snapToYAxis' ,
} )
2024-06-24 11:45:40 -04:00
if ( trap ( constraint ) ) return false
const { modifiedAst , pathToNodeMap } = constraint
2024-05-30 13:28:29 +10:00
if ( ! sketchDetails ) return
2024-06-24 11:45:40 -04:00
const updatedAst = await sceneEntitiesManager . updateAstAndRejigSketch (
2024-05-30 13:28:29 +10:00
sketchDetails ? . sketchPathToNode || [ ] ,
modifiedAst ,
sketchDetails . zAxis ,
sketchDetails . yAxis ,
sketchDetails . origin
)
2024-06-24 11:45:40 -04:00
if ( trap ( updatedAst , { suppress : true } ) ) return
if ( ! updatedAst ) return
2024-05-30 13:28:29 +10:00
const updatedSelectionRanges = updateSelections (
pathToNodeMap ,
selectionRanges ,
2024-06-24 11:45:40 -04:00
updatedAst . newAst
2024-05-30 13:28:29 +10:00
)
return {
selectionType : 'completeSelection' ,
selection : updatedSelectionRanges ,
}
2024-09-09 19:59:36 +03:00
}
) ,
'do-constrain-parallel' : fromPromise (
async ( {
input : { selectionRanges , sketchDetails } ,
} : {
input : Pick < ModelingMachineContext , 'selectionRanges' | 'sketchDetails' >
} ) = > {
2024-06-24 11:45:40 -04:00
const constraint = applyConstraintEqualAngle ( {
2024-05-30 13:28:29 +10:00
selectionRanges ,
} )
2024-06-24 11:45:40 -04:00
if ( trap ( constraint ) ) return false
const { modifiedAst , pathToNodeMap } = constraint
if ( ! sketchDetails ) {
trap ( new Error ( 'No sketch details' ) )
return
}
const updatedAst = await sceneEntitiesManager . updateAstAndRejigSketch (
2024-05-30 13:28:29 +10:00
sketchDetails ? . sketchPathToNode || [ ] ,
parse ( recast ( modifiedAst ) ) ,
sketchDetails . zAxis ,
sketchDetails . yAxis ,
sketchDetails . origin
)
2024-06-24 11:45:40 -04:00
if ( trap ( updatedAst , { suppress : true } ) ) return
if ( ! updatedAst ) return
2024-05-30 13:28:29 +10:00
const updatedSelectionRanges = updateSelections (
pathToNodeMap ,
selectionRanges ,
2024-06-24 11:45:40 -04:00
updatedAst . newAst
2024-05-30 13:28:29 +10:00
)
return {
selectionType : 'completeSelection' ,
selection : updatedSelectionRanges ,
}
2024-09-09 19:59:36 +03:00
}
) ,
'do-constrain-equal-length' : fromPromise (
async ( {
input : { selectionRanges , sketchDetails } ,
} : {
input : Pick < ModelingMachineContext , 'selectionRanges' | 'sketchDetails' >
2024-05-30 13:28:29 +10:00
} ) = > {
2024-06-24 11:45:40 -04:00
const constraint = applyConstraintEqualLength ( {
2024-05-30 13:28:29 +10:00
selectionRanges ,
} )
2024-06-24 11:45:40 -04:00
if ( trap ( constraint ) ) return false
const { modifiedAst , pathToNodeMap } = constraint
2024-05-30 13:28:29 +10:00
if ( ! sketchDetails ) return
2024-06-24 11:45:40 -04:00
const updatedAst = await sceneEntitiesManager . updateAstAndRejigSketch (
2024-05-30 13:28:29 +10:00
sketchDetails ? . sketchPathToNode || [ ] ,
modifiedAst ,
sketchDetails . zAxis ,
sketchDetails . yAxis ,
sketchDetails . origin
)
2024-06-24 11:45:40 -04:00
if ( trap ( updatedAst , { suppress : true } ) ) return
if ( ! updatedAst ) return
2024-05-30 13:28:29 +10:00
const updatedSelectionRanges = updateSelections (
pathToNodeMap ,
selectionRanges ,
2024-06-24 11:45:40 -04:00
updatedAst . newAst
2024-05-30 13:28:29 +10:00
)
return {
selectionType : 'completeSelection' ,
selection : updatedSelectionRanges ,
}
2024-09-09 19:59:36 +03:00
}
) ,
'Get vertical info' : fromPromise (
async ( _ : {
input : Pick < ModelingMachineContext , 'selectionRanges' | 'sketchDetails' >
} ) = > {
return { } as SetSelections
}
) ,
'Get ABS X info' : fromPromise (
async ( _ : {
input : Pick < ModelingMachineContext , 'selectionRanges' | 'sketchDetails' >
} ) = > {
return { } as SetSelections
}
) ,
'Get ABS Y info' : fromPromise (
async ( _ : {
input : Pick < ModelingMachineContext , 'selectionRanges' | 'sketchDetails' >
} ) = > {
return { } as SetSelections
}
) ,
'Get angle info' : fromPromise (
async ( _ : {
input : Pick < ModelingMachineContext , 'selectionRanges' | 'sketchDetails' >
} ) = > {
return { } as SetSelections
}
) ,
'Get perpendicular distance info' : fromPromise (
async ( _ : {
input : Pick < ModelingMachineContext , 'selectionRanges' | 'sketchDetails' >
} ) = > {
return { } as SetSelections
}
) ,
'AST-undo-startSketchOn' : fromPromise (
async ( _ : { input : Pick < ModelingMachineContext , 'sketchDetails' > } ) = > {
return undefined
}
) ,
'animate-to-face' : fromPromise (
async ( _ : { input? : ExtrudeFacePlane | DefaultPlane } ) = > {
return { } as
| undefined
| {
sketchPathToNode : PathToNode
zAxis : [ number , number , number ]
yAxis : [ number , number , number ]
origin : [ number , number , number ]
}
}
) ,
'animate-to-sketch' : fromPromise (
async ( _ : { input : Pick < ModelingMachineContext , 'selectionRanges' > } ) = > {
return { } as {
sketchPathToNode : PathToNode
zAxis : [ number , number , number ]
yAxis : [ number , number , number ]
origin : [ number , number , number ]
}
}
) ,
'Get horizontal info' : fromPromise (
async ( _ : {
input : Pick < ModelingMachineContext , 'sketchDetails' | 'selectionRanges' >
} ) = > {
return { } as SetSelections
}
) ,
'Get length info' : fromPromise (
async ( _ : {
input : Pick < ModelingMachineContext , 'sketchDetails' | 'selectionRanges' >
} ) = > {
return { } as SetSelections
}
) ,
'Get convert to variable info' : fromPromise (
async ( _ : {
input : Pick <
ModelingMachineContext ,
'sketchDetails' | 'selectionRanges'
> & {
data ? : {
variableName : string
pathToNode : PathToNode
}
}
} ) = > {
return { } as SetSelections
}
) ,
} ,
// end services
} ) . createMachine ( {
2024-09-12 22:06:50 -04:00
/ * * @ x s t a t e - l a y o u t N 4 I g p g J g 5 m D O I C 5 Q F k D 2 E w B s C W A 7 K A x A M I C G u A x l g N o A M A u o q A A 6 q z Y A u 2 q u j I A H o g C 0 A N h o B W A H Q A O A M w B 2 K Q E Y 5 A F g C c G q W q k A a E A E 9 E w 0 R L E q a 6 4 T I B M K m T U X C A v k 7 1 o M O f A Q D K Y d g A J Y L D B y T m 5 a B i Q Q F j Y w n i i B B E E p Y S k J O U U a O W s x e y l r W z k 9 Q w Q C l Q k r V O s Z N W E x F I t n V x B 3 L D x C X w C A W 1 Q A V y D A 9 h J 2 M A j e G I 4 u e N B E 5 K k a C W s p Z f M M + X E 1 Y s Q Z M Q W x N Q t x a o 0 Z e R c 3 d D a v T v 9 y b h G + d j G o i b j e R J o t h B p z l s v P D p + f y + K B d M C 4 A I A e Q A b m A A E 6 Y E j 6 W D P Z i s S b c d 6 I T 4 G b G - V o A i T Y C C Y M A E A C i E P h g Q A 1 n 5 y A A L V H R d F v B J C J Y L H Z K a w H c z V B x f e y S R T m O p q M R y N S Z a p N C 4 e d p E k l k y k j O G 0 + l M x S R N G x K Z Y p K 2 B b m W r q c Q 0 U R i J Z C k x y G g y Y T K f Z S K 2 8 s T 4 - 6 K 4 m k i m P O G 9 D D M 1 7 6 9 l J S U y C Q S l T K G Q q X K K F 3 C L 6 K a o R j K p O R i e M 1 R Q e h X 4 J U + g B i 2 E w p K e 9 H G r J D M y E W Y j d h d Y k c d s l 1 i K u I Q i l j w g k M a k c h s M m U D j z V y g h Z V f B Y c I r O p Z e s x o c E Y j q 6 X b o h o K V F U s U y Z U j o k N U z S m E 1 g T 5 7 H h O 9 Z O Q J D p Q e r S 9 r C B U 1 g q W j f a k H 0 a y + y + w g K O k 3 I q C o m Z m L G U h X l 6 y o E A A I s E I y B M E o R T I + i 7 T P w Q i O u U 2 h m C O C b R o o Y j J p K 5 Q 1 G e v K K I 4 1 F S t B B Y 3 g Q A A q Y C P I I 7 C o I I R A A I J w e h G K Y b M z b v m Y K R Z h K 4 Y H s m q i 9 l Y 2 Q 5 N o 1 j C N G c j 0 R O N 4 S A y x J g A A C o i u B w A Q 3 G z t g A B m J C h P 4 U B w i Q T A M v 4 L B M L 0 i I j B A - F s i + g g H m o E j i D G 3 5 a I F D p f N + E Y O K J A 7 d t R U g q G p k 4 S L A D K o A A 7 n p Z C G c Z n D m Z Z 1 m 2 f Z Y B d E w n C Q O 5 N Z Y U k Y F y O k L q Z k p g E x W B J G d t R B R 9 l o 1 H 2 B a O b W H F 3 h 0 u w j L E G Q l C Y H 1 m q l c + 5 U p q c i x 2 G + d S O k p t j J o o B y + c p V h Z n I U r 2 r 1 - W D Q h 5 Z g M h o L g n O V Y Y Q a 2 2 S G o b p L O o d r L D I X x x l V G j m P W u z K R a e 2 a g Q d w Y P 4 k A c P 4 E C 9 H C 7 Q a g N T K V i 8 T 6 C Y g 3 a 3 a Y Y E B V o y l m B 2 J R q D U v m r s s t R Z N R a i x c 0 B K K m N 0 M S J T j I A J K w X e d I n W C E L + L C J n k C Q m A T Q j X a n G k w 7 L E p j g q O j + 6 t b Y 9 T q P M Q u y L 9 V M 0 w y 9 M + o z x 1 B K d r N J R D A B e 9 x c z z B r d u e 6 S g f U O w 4 5 U K j J j Y x r S l a + x E z Q o 5 k 5 6 B a K 9 T + 1 K 7 B R D c L A 7 A 2 X g - h a 9 g u s Q l z o P Y H 7 w 2 j L D u o C Y b 9 i K H 2 C h L L I x E K L o n Z G p I T t G 2 B O z p 3 K f z 5 h O 7 u K 8 r Z I + 7 g f s B 7 g b P w p w n O Y B H U c U D H 8 7 B p N i R R b 2 W 5 P Q 4 5 4 S r G X w F P k E h g U 7 N A H L k w 7 y 4 y H u a h X x C + - 7 J C B 9 x A B C 3 j + A A G g b o b d j Y p g - j j 0 b n o 6 T U l L y V i R j Q S k W P Y 1 g O F B L s l w v C u e 0 v V c 1 2 v d e b 9 v A B N f e L 5 D 7 v n k E o E 0 t R 7 Z W 0 7 P Y b 8 F Q 1 g n h i h n I u 5 M 3 a e 3 f n T b 2 K 9 a 7 + D I F A U k w C p o 1 D 2 K t e Y r Y + T 5 B H s 2 X s T 9 Q J 3 3 t P d U C c 8 G S Y K 9 j 6 b + q 9 A 6 k n w O w G G H d 4 Y J x i q Y c K A 5 H S C z s C P M 8 V U M h W j f A U P u x E W F s K - j g 3 + D l 4 R M H B B A b A 5 B n I k H V D o 1 u l A i H d 3 b J I T a v I + 7 t l 5 J n K + + Q I z G D b E - J 2 6 g e q v 3 H G w l R 2 D q 5 c L r s H U O Q w y z 6 D w T g K A u B T G I z g Z G E m B 4 Z D 5 G k V m U i O R Z o t h c Q 6 L c M h l H l x 8 T - Q O 7 N G 5 c 0 w C E r m 2 B w m R L 5 k p U w q Q a o a G y M p Z 6 z U r D l F y K u A o E o D h D m y Z - X J f j A i 4 F s v 4 D i u 8 K n d h M D U A K q h 9 i Z C o V n P y y d R Q H G W K t Z Q 3 T F 6 9 N w b A A Z T A h m o H 8 E A 2 O C 5 4 4 H w y Y s F M 1 9 z x S k H I 0 q + x F e z q D j L s V a O w p 5 r O h q o 3 x u C w A A E d e j h x 4 V A P h o z Y w L D q H + J Q 2 5 2 n S V x v I R a d g b A 4 T e V g j h a j A 5 M A M U U 6 g h z O 6 8 2 7 K o P s Z 5 9 i A U y K K b s y Y J S 9 i z F L B w y l u y k 3 l J 4 s u P S U W f P U X C A q q B Y S 3 F R R C F E 2 L B E H z d C b G K s Y 5 B g v M c m W Q P l a j t l A n a a U w 5 c w e M J E y 9 Z P o A B K Y B B C s R C L 0 E Y o z H R p G 0 N G c U h N v z J n N O P W Q o E K X C B O O 4 h l K q M E 5 J 9 A D Y 6 w M A h g w h v g K G j J g X I 0 d N K Z Q A 4 H 6 r Q t f a X y Y r N q r h x l m J V j q K b O u Z Z X b g B T d l s w M d g E g A A j Q h - L L o n I 0 N I L I C S S a r S s N Y I U 5 8 j y r h w k - f Y 9 L i 6 M q T W q s k j J 8 H H Q 4 q g b m + b j k v h y H f a Q V j b o O 1 C p W 5 q x F 3 x Z E 3 O 0 2 Q V g X 4 J v Q Z q C Q 3 F k p r w C A E v W z c j F D D b v 4 P A p l U A E A g N w M A R J c D Q l Q H S C Q M B 2 C C E 3 W H T A g g D 2 o A q d U Z s p g H F C y f j F O o I 8 3 x p C l C k M C p t E 6 N r Q a X D B q 7 1 1 B 1 Q D r L d L d d 2 U H 3 b g Q 9 B B 4 R w j g x I J g L l D 1 w i 6 D e v w 9 6 4 M h y 3 c + 1 D r 7 e 0 e X K u + 9 8 A 7 4 m K Q v p s L O J D I z A d s G e Z Y 4 h V L K s T c u 6 D I M C m 6 P D j u 6 O K G 0 M n o M u e y 9 1 7 b 2 C C E 0 3 c j h 6 3 3 m 0 W I O D O M V 5 h n h H r y d 8 + R M h i p N J c 5 R A m A g K Z E 5 H J D x 0 X 3 o b h J h u E 2 H c N w Y I 3 J 8 z T 6 X 0 q f 2 J G F S s L V z y p g V f H I S d q K i j q g 0 0 4 J m 1 0 g 3 - r v c T R 7 J N n r w D J s 9 c m c 2 w E E H w J T l G B E F v 7 Q 6 C M W g g N Z F j D a k e 3 4 k 5 b j P K s S r 2 R I s w Z i z v O L t n 7 O O e G H h l z R G 0 s Z a y y p 5 S 1 T t B n i J i p M r K T 7 B y s H O Q n G P H F 2 Q f 4 1 F g I M W A F N Y S 9 J q 9 K X O v Z v S - o H r V G y q J F l A 2 a U P 5 f y 2 H k E K a d 4 9 x u i 1 H Q O O r 0 W t 7 7 K a x h r D O G 2 v O c I 3 e r r 2 2 P O 7 a 7 o g W U 5 R H A O n T m B c 8 W 4 h Q x l 8 s R U W d R Z U t j u w E f B p J l u n t W 7 J z r + B S Q 7 Z y 3 2 m j D p y h 2 A y H i 8 w s y r 6 r k k G I m w E E V m 2 A R 3 g z H 1 m K P N Z e 0 5 - D H 3 B B I 6 1 T 9 n H 1 H 9 s O g p + Y T I x g H B i r 3 F n K 0 v d 9 g 1 D j E p S U o o 6 c A r 4 S j q T S W 1 v s 8 V w y b H F 1 c d 8 7 P N U 2 Q 9 S s x Z H H V f O 1 5 Q n b S h N f I T M 0 2 m 1 O r m z B j X T 2 7 M s 7 e 2 z u T G u t d w 1 y 3 j m + B 2 H T K V P q K K R H 7 R B p 3 k M 2 f I u 1 e N L q p q Z j R c I t G 4 B 0 X o x E h j L N i Z s y t 1 X 6 O 7 1 a M T 9 o 3 R + i 4 S C F E 2 3 L 3 c d e f - e H A s K e d h 7 S Z B 2 M 2 L G - 3 r V H m U F Y g c W 0 6 f 5 6 T y n 4 v i H M 9 M + e w 5 1 7 7 B 2 v s 9 7 4 X 1 P B j S 8 Z - L 9 z 7 X V f S g 1 9 8 i T e w N e m - K B H g o X s w 5 s h L P A r b i D X i A A y e B O 2 o G 7 f 9 B k H b d n d r f a I S l c Y B w Z B S D Y E K S x T C y 6 n s L 9 s z s Z s z 8 L 9 7 9 M A J B a Z c A O A C A 3 0 l g I x v w b U 6 h M w U x Z A A I 1 p C I x V T h u w D c H U 7 c + M q Z z 8 D I Q C w C I D 2 A o D t Q l 8 9 t - s q J I w M h 7 p l J C c l I v g p R e x v w 7 U g d z x 4 k F B l F 8 D L 9 u 0 J A A A 5 P Z H S V A P A d g W A I y C A C A Q Y A x A I F g E Q x - N a R + G o I c b Q C U A C b I c e C + S U b a B S O i G P W b P A 4 A r t U A g Q - w I Q k Q s Q 0 g N u H t H n C g r s d G A l O d b j N s O x R A D I Y 0 N 8 E X e o L Q L c Z R c A y A 0 Z L q C o c + K P B o X f Z M W w d 8 a M W M X Y U 4 K w V 5 P Q r x P w k g q g M g 7 3 H X R G K e H y O + G p R V J Y O 0 J M Z q T q Y C W i Q 4 H I e J d 0 B I 9 2 J i D t C E L N Z u A x c g X Z G - O - I w i p d Y P f O M c Q O 1 J + M V L I R g + o C o C e d A r I J Z C o w A 9 2 X o Z P P Z K O W c R W S E X A Y 9 V H H P M 9 b i b w J i Q Q S Y k 9 Q Q G Y 9 g O Y i J X 7 X F Y H T 9 b Q 0 U e o f Y E m f o 4 L D G a 7 B Q V G c D V 2 f Q + e T V U I T n E A 5 o - A H g 6 w 8 g v 7 B A G v B Y a H f I V Q F M X f F v L s O + H y Q 1 Q E 3 Y J s V B B 4 r x Z 4 3 d A h T 4 l d e b S G I I O 4 Z P b l O E A y O E A g E s C A x K f w d l F 4 h n N o + R C o e M R o P y Z s A L R A A c M B W w E m Y W T I E D Z R B E 1 4 o w l E 9 d S G E j K A P A c Q y Q 4 k x E 5 H X k v A M k h
2024-09-09 19:59:36 +03:00
id : 'Modeling' ,
context : ( { input } ) = > ( {
. . . modelingMachineDefaultContext ,
. . . input ,
} ) ,
states : {
idle : {
on : {
'Enter sketch' : [
{
target : 'animating to existing sketch' ,
guard : 'Selection is on face' ,
} ,
'Sketch no face' ,
] ,
Extrude : {
target : 'idle' ,
guard : 'has valid extrude selection' ,
actions : [ 'AST extrude' ] ,
reenter : false ,
} ,
2024-09-17 08:29:52 -05:00
Revolve : {
target : 'idle' ,
guard : 'has valid revolve selection' ,
actions : [ 'AST revolve' ] ,
reenter : false ,
} ,
2024-09-09 19:59:36 +03:00
Fillet : {
target : 'idle' ,
guard : 'has valid fillet selection' , // TODO: fix selections
actions : [ 'AST fillet' ] ,
reenter : false ,
} ,
Export : {
target : 'idle' ,
reenter : false ,
guard : 'Has exportable geometry' ,
actions : 'Engine export' ,
} ,
Make : {
target : 'idle' ,
reenter : false ,
guard : 'Has exportable geometry' ,
actions : 'Make' ,
} ,
'Delete selection' : {
target : 'idle' ,
guard : 'has valid selection for deletion' ,
actions : [ 'AST delete selection' ] ,
reenter : false ,
} ,
'Text-to-CAD' : {
target : 'idle' ,
reenter : false ,
actions : [ 'Submit to Text-to-CAD API' ] ,
} ,
} ,
entry : 'reset client scene mouse handlers' ,
states : {
hidePlanes : {
on : {
'Artifact graph populated' : 'showPlanes' ,
} ,
entry : 'hide default planes' ,
} ,
showPlanes : {
on : {
'Artifact graph emptied' : 'hidePlanes' ,
} ,
2024-09-12 22:06:50 -04:00
entry : [
'show default planes' ,
'reset camera position' ,
'set selection filter to curves only' ,
] ,
description : ` We want to disable selections and hover highlights here, because users can't do anything with that information until they actually add something to the scene. The planes are just for orientation here. ` ,
exit : 'set selection filter to defaults' ,
2024-09-09 19:59:36 +03:00
} ,
} ,
initial : 'hidePlanes' ,
} ,
Sketch : {
states : {
SketchIdle : {
on : {
'Make segment vertical' : {
guard : 'Can make selection vertical' ,
target : 'Await constrain vertically' ,
} ,
'Make segment horizontal' : {
guard : 'Can make selection horizontal' ,
target : 'Await constrain horizontally' ,
} ,
'Constrain horizontal distance' : {
target : 'Await horizontal distance info' ,
guard : 'Can constrain horizontal distance' ,
} ,
'Constrain vertical distance' : {
target : 'Await vertical distance info' ,
guard : 'Can constrain vertical distance' ,
} ,
'Constrain ABS X' : {
target : 'Await ABS X info' ,
guard : 'Can constrain ABS X' ,
} ,
'Constrain ABS Y' : {
target : 'Await ABS Y info' ,
guard : 'Can constrain ABS Y' ,
} ,
'Constrain angle' : {
target : 'Await angle info' ,
guard : 'Can constrain angle' ,
} ,
'Constrain length' : {
target : 'Await length info' ,
guard : 'Can constrain length' ,
} ,
'Constrain perpendicular distance' : {
target : 'Await perpendicular distance info' ,
guard : 'Can constrain perpendicular distance' ,
} ,
'Constrain horizontally align' : {
guard : 'Can constrain horizontally align' ,
target : 'Await constrain horizontally align' ,
} ,
'Constrain vertically align' : {
guard : 'Can constrain vertically align' ,
target : 'Await constrain vertically align' ,
} ,
'Constrain snap to X' : {
guard : 'Can constrain snap to X' ,
target : 'Await constrain snap to X' ,
} ,
'Constrain snap to Y' : {
guard : 'Can constrain snap to Y' ,
target : 'Await constrain snap to Y' ,
} ,
'Constrain equal length' : {
guard : 'Can constrain equal length' ,
target : 'Await constrain equal length' ,
} ,
'Constrain parallel' : {
target : 'Await constrain parallel' ,
guard : 'Can canstrain parallel' ,
} ,
'Constrain remove constraints' : {
guard : 'Can constrain remove constraints' ,
target : 'Await constrain remove constraints' ,
} ,
'Re-execute' : {
target : 'SketchIdle' ,
reenter : false ,
actions : [ 'set sketchMetadata from pathToNode' ] ,
} ,
'code edit during sketch' : 'clean slate' ,
'Convert to variable' : {
target : 'Await convert to variable' ,
guard : 'Can convert to variable' ,
} ,
'change tool' : {
target : 'Change Tool' ,
} ,
} ,
entry : 'setup client side sketch segments' ,
} ,
'Await horizontal distance info' : {
invoke : {
src : 'Get horizontal info' ,
id : 'get-horizontal-info' ,
input : ( { context : { selectionRanges , sketchDetails } } ) = > ( {
selectionRanges ,
sketchDetails ,
} ) ,
onDone : {
target : 'SketchIdle' ,
actions : 'Set selection' ,
} ,
onError : 'SketchIdle' ,
} ,
} ,
'Await vertical distance info' : {
invoke : {
src : 'Get vertical info' ,
id : 'get-vertical-info' ,
input : ( { context : { selectionRanges , sketchDetails } } ) = > ( {
selectionRanges ,
sketchDetails ,
} ) ,
onDone : {
target : 'SketchIdle' ,
actions : 'Set selection' ,
} ,
onError : 'SketchIdle' ,
} ,
} ,
'Await ABS X info' : {
invoke : {
src : 'Get ABS X info' ,
id : 'get-abs-x-info' ,
input : ( { context : { selectionRanges , sketchDetails } } ) = > ( {
selectionRanges ,
sketchDetails ,
} ) ,
onDone : {
target : 'SketchIdle' ,
actions : 'Set selection' ,
} ,
onError : 'SketchIdle' ,
} ,
} ,
'Await ABS Y info' : {
invoke : {
src : 'Get ABS Y info' ,
id : 'get-abs-y-info' ,
input : ( { context : { selectionRanges , sketchDetails } } ) = > ( {
selectionRanges ,
sketchDetails ,
} ) ,
onDone : {
target : 'SketchIdle' ,
actions : 'Set selection' ,
} ,
onError : 'SketchIdle' ,
} ,
} ,
'Await angle info' : {
invoke : {
src : 'Get angle info' ,
id : 'get-angle-info' ,
input : ( { context : { selectionRanges , sketchDetails } } ) = > ( {
selectionRanges ,
sketchDetails ,
} ) ,
onDone : {
target : 'SketchIdle' ,
actions : 'Set selection' ,
} ,
onError : 'SketchIdle' ,
} ,
} ,
'Await length info' : {
invoke : {
src : 'Get length info' ,
id : 'get-length-info' ,
input : ( { context : { selectionRanges , sketchDetails } } ) = > ( {
selectionRanges ,
sketchDetails ,
} ) ,
onDone : {
target : 'SketchIdle' ,
actions : 'Set selection' ,
} ,
onError : 'SketchIdle' ,
} ,
} ,
'Await perpendicular distance info' : {
invoke : {
src : 'Get perpendicular distance info' ,
id : 'get-perpendicular-distance-info' ,
input : ( { context : { selectionRanges , sketchDetails } } ) = > ( {
selectionRanges ,
sketchDetails ,
} ) ,
onDone : {
target : 'SketchIdle' ,
actions : 'Set selection' ,
} ,
onError : 'SketchIdle' ,
} ,
} ,
'Line tool' : {
exit : [ ] ,
states : {
Init : {
always : [
{
target : 'normal' ,
guard : 'has made first point' ,
actions : 'set up draft line' ,
} ,
'No Points' ,
] ,
} ,
normal : { } ,
'No Points' : {
entry : 'setup noPoints onClick listener' ,
on : {
'Add start point' : {
target : 'normal' ,
actions : 'set up draft line without teardown' ,
} ,
Cancel : '#Modeling.Sketch.undo startSketchOn' ,
} ,
} ,
} ,
initial : 'Init' ,
on : {
'change tool' : {
target : 'Change Tool' ,
} ,
} ,
} ,
Init : {
always : [
{
target : 'SketchIdle' ,
guard : 'is editing existing sketch' ,
} ,
'Line tool' ,
] ,
} ,
'Tangential arc to' : {
entry : 'set up draft arc' ,
on : {
'change tool' : {
target : 'Change Tool' ,
} ,
} ,
} ,
'undo startSketchOn' : {
invoke : {
src : 'AST-undo-startSketchOn' ,
id : 'AST-undo-startSketchOn' ,
input : ( { context : { sketchDetails } } ) = > ( { sketchDetails } ) ,
onDone : {
target : '#Modeling.idle' ,
actions : 'enter modeling mode' ,
} ,
} ,
} ,
'Rectangle tool' : {
entry : [ 'listen for rectangle origin' ] ,
states : {
'Awaiting second corner' : {
on : {
'Finish rectangle' : 'Finished Rectangle' ,
} ,
} ,
'Awaiting origin' : {
on : {
'Add rectangle origin' : {
target : 'Awaiting second corner' ,
actions : 'set up draft rectangle' ,
} ,
} ,
} ,
'Finished Rectangle' : {
always : '#Modeling.Sketch.SketchIdle' ,
} ,
} ,
initial : 'Awaiting origin' ,
on : {
'change tool' : {
target : 'Change Tool' ,
} ,
} ,
} ,
'clean slate' : {
always : 'SketchIdle' ,
} ,
'Await convert to variable' : {
invoke : {
src : 'Get convert to variable info' ,
id : 'get-convert-to-variable-info' ,
input : ( { context : { selectionRanges , sketchDetails } , event } ) = > {
if ( event . type !== 'Convert to variable' ) {
return {
selectionRanges ,
sketchDetails ,
data : undefined ,
}
}
return {
selectionRanges ,
sketchDetails ,
data : event.data ,
}
} ,
onError : 'SketchIdle' ,
onDone : {
target : 'SketchIdle' ,
actions : [ 'Set selection' ] ,
} ,
} ,
} ,
'Await constrain remove constraints' : {
invoke : {
src : 'do-constrain-remove-constraint' ,
id : 'do-constrain-remove-constraint' ,
input : ( { context : { selectionRanges , sketchDetails } , event } ) = > {
return {
selectionRanges ,
sketchDetails ,
data :
event . type === 'Constrain remove constraints'
? event . data
: undefined ,
}
} ,
onDone : {
target : 'SketchIdle' ,
actions : 'Set selection' ,
} ,
} ,
} ,
'Await constrain horizontally' : {
invoke : {
src : 'do-constrain-horizontally' ,
id : 'do-constrain-horizontally' ,
input : ( { context : { selectionRanges , sketchDetails } } ) = > ( {
selectionRanges ,
sketchDetails ,
} ) ,
onDone : {
target : 'SketchIdle' ,
actions : 'Set selection' ,
} ,
} ,
} ,
'Await constrain vertically' : {
invoke : {
src : 'do-constrain-vertically' ,
id : 'do-constrain-vertically' ,
input : ( { context : { selectionRanges , sketchDetails } } ) = > ( {
selectionRanges ,
sketchDetails ,
} ) ,
onDone : {
target : 'SketchIdle' ,
actions : 'Set selection' ,
} ,
} ,
} ,
'Await constrain horizontally align' : {
invoke : {
src : 'do-constrain-horizontally-align' ,
id : 'do-constrain-horizontally-align' ,
input : ( { context } ) = > ( {
selectionRanges : context.selectionRanges ,
sketchDetails : context.sketchDetails ,
} ) ,
onDone : {
target : 'SketchIdle' ,
actions : 'Set selection' ,
} ,
} ,
} ,
'Await constrain vertically align' : {
invoke : {
src : 'do-constrain-vertically-align' ,
id : 'do-constrain-vertically-align' ,
input : ( { context } ) = > ( {
selectionRanges : context.selectionRanges ,
sketchDetails : context.sketchDetails ,
} ) ,
onDone : {
target : 'SketchIdle' ,
actions : 'Set selection' ,
} ,
} ,
} ,
'Await constrain snap to X' : {
invoke : {
src : 'do-constrain-snap-to-x' ,
id : 'do-constrain-snap-to-x' ,
input : ( { context } ) = > ( {
selectionRanges : context.selectionRanges ,
sketchDetails : context.sketchDetails ,
} ) ,
onDone : {
target : 'SketchIdle' ,
actions : 'Set selection' ,
} ,
} ,
} ,
'Await constrain snap to Y' : {
invoke : {
src : 'do-constrain-snap-to-y' ,
id : 'do-constrain-snap-to-y' ,
input : ( { context } ) = > ( {
selectionRanges : context.selectionRanges ,
sketchDetails : context.sketchDetails ,
} ) ,
onDone : {
target : 'SketchIdle' ,
actions : 'Set selection' ,
} ,
} ,
} ,
'Await constrain equal length' : {
invoke : {
src : 'do-constrain-equal-length' ,
id : 'do-constrain-equal-length' ,
input : ( { context } ) = > ( {
selectionRanges : context.selectionRanges ,
sketchDetails : context.sketchDetails ,
} ) ,
onDone : {
target : 'SketchIdle' ,
actions : 'Set selection' ,
} ,
} ,
} ,
'Await constrain parallel' : {
invoke : {
src : 'do-constrain-parallel' ,
id : 'do-constrain-parallel' ,
input : ( { context } ) = > ( {
selectionRanges : context.selectionRanges ,
sketchDetails : context.sketchDetails ,
} ) ,
onDone : {
target : 'SketchIdle' ,
actions : 'Set selection' ,
} ,
} ,
} ,
'Change Tool' : {
always : [
{
target : 'SketchIdle' ,
guard : 'next is none' ,
} ,
{
target : 'Line tool' ,
guard : 'next is line' ,
} ,
{
target : 'Rectangle tool' ,
guard : 'next is rectangle' ,
} ,
{
target : 'Tangential arc to' ,
guard : 'next is tangential arc' ,
} ,
] ,
entry : 'assign tool in context' ,
} ,
} ,
initial : 'Init' ,
on : {
CancelSketch : '.SketchIdle' ,
'Delete segment' : {
reenter : false ,
actions : [ 'Delete segment' , 'Set sketchDetails' ] ,
} ,
'code edit during sketch' : '.clean slate' ,
} ,
exit : [
'sketch exit execute' ,
'tear down client sketch' ,
'remove sketch grid' ,
'engineToClient cam sync direction' ,
'Reset Segment Overlays' ,
'enable copilot' ,
] ,
entry : [
'add axis n grid' ,
'conditionally equip line tool' ,
'clientToEngine cam sync direction' ,
] ,
} ,
'Sketch no face' : {
entry : [
'disable copilot' ,
'show default planes' ,
'set selection filter to faces only' ,
'enter sketching mode' ,
] ,
exit : [ 'hide default planes' , 'set selection filter to defaults' ] ,
on : {
'Select default plane' : {
target : 'animating to plane' ,
actions : [ 'reset sketch metadata' ] ,
} ,
} ,
} ,
'animating to plane' : {
invoke : {
src : 'animate-to-face' ,
id : 'animate-to-face' ,
input : ( { event } ) = > {
if ( event . type !== 'Select default plane' ) return undefined
return event . data
} ,
onDone : {
target : 'Sketch' ,
actions : 'set new sketch metadata' ,
} ,
} ,
} ,
'animating to existing sketch' : {
invoke : {
src : 'animate-to-sketch' ,
id : 'animate-to-sketch' ,
input : ( { context } ) = > ( {
selectionRanges : context.selectionRanges ,
sketchDetails : context.sketchDetails ,
} ) ,
onDone : {
target : 'Sketch' ,
actions : [
'disable copilot' ,
'set new sketch metadata' ,
'enter sketching mode' ,
] ,
} ,
} ,
} ,
} ,
initial : 'idle' ,
on : {
Cancel : {
target : '.idle' ,
// TODO what if we're existing extrude equipped, should these actions still be fired?
// maybe cancel needs to have a guard for if else logic?
actions : [
'reset sketch metadata' ,
'enable copilot' ,
'enter modeling mode' ,
] ,
} ,
'Set selection' : {
reenter : false ,
actions : 'Set selection' ,
} ,
'Set mouse state' : {
reenter : false ,
actions : 'Set mouse state' ,
} ,
'Set context' : {
reenter : false ,
actions : 'Set context' ,
} ,
'Set Segment Overlays' : {
reenter : false ,
actions : 'Set Segment Overlays' ,
} ,
} ,
} )
2024-07-05 13:40:16 +10:00
export function isEditingExistingSketch ( {
sketchDetails ,
} : {
sketchDetails : SketchDetails | null
} ) : boolean {
// should check that the variable declaration is a pipeExpression
// and that the pipeExpression contains a "startProfileAt" callExpression
if ( ! sketchDetails ? . sketchPathToNode ) return false
const variableDeclaration = getNodeFromPath < VariableDeclarator > (
kclManager . ast ,
sketchDetails . sketchPathToNode ,
'VariableDeclarator'
)
if ( err ( variableDeclaration ) ) return false
if ( variableDeclaration . node . type !== 'VariableDeclarator' ) return false
const pipeExpression = variableDeclaration . node . init
if ( pipeExpression . type !== 'PipeExpression' ) return false
const hasStartProfileAt = pipeExpression . body . some (
( item ) = >
item . type === 'CallExpression' && item . callee . name === 'startProfileAt'
)
return hasStartProfileAt && pipeExpression . body . length > 2
}
export function canRectangleTool ( {
sketchDetails ,
} : {
sketchDetails : SketchDetails | null
} ) : boolean {
const node = getNodeFromPath < VariableDeclaration > (
kclManager . ast ,
sketchDetails ? . sketchPathToNode || [ ] ,
'VariableDeclaration'
)
// This should not be returning false, and it should be caught
// but we need to simulate old behavior to move on.
if ( err ( node ) ) return false
return node . node ? . declarations ? . [ 0 ] ? . init . type !== 'PipeExpression'
}