2023-10-11 13:36:54 +11:00
import { PathToNode } from 'lang/wasm'
import { engineCommandManager } from 'lang/std/engineConnection'
import { isReducedMotion } from 'lang/util'
import { Axis , Selection , SelectionRangeTypeMap , Selections } from 'useStore'
import { assign , createMachine } from 'xstate'
import { v4 as uuidv4 } from 'uuid'
2023-10-11 15:12:29 +11:00
import { isCursorInSketchCommandRange } from 'lang/util'
2023-10-11 13:36:54 +11:00
import {
doesPipeHaveCallExp ,
getNodePathFromSourceRange ,
hasExtrudeSketchGroup ,
} from 'lang/queryAst'
import { kclManager } from 'lang/KclSinglton'
import {
horzVertInfo ,
applyConstraintHorzVert ,
} from 'components/Toolbar/HorzVert'
import {
applyConstraintHorzVertAlign ,
horzVertDistanceInfo ,
} from 'components/Toolbar/SetHorzVertDistance'
import { angleBetweenInfo } from 'components/Toolbar/SetAngleBetween'
import { setAngleLengthInfo } from 'components/Toolbar/setAngleLength'
import {
applyConstraintEqualLength ,
setEqualLengthInfo ,
} from 'components/Toolbar/EqualLength'
import { extrudeSketch } from 'lang/modifyAst'
import { getNodeFromPath } from '../lang/queryAst'
import { CallExpression , PipeExpression } from '../lang/wasm'
2023-10-14 03:47:46 +11:00
import { getConstraintLevelFromSourceRange } from 'lang/std/sketchcombos'
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
}
| {
selectionType : 'mirrorCodeMirrorSelections'
selection : Selections
}
export const modelingMachine = createMachine (
{
2023-10-14 03:47:46 +11: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 D M A V g D s A O m E i A j L M G C A H I I A s A J l H K A N C A C e i a Q D Z B E s c t G r V A T m m i a N Q Q F 8 H 2 t B h z 5 x 2 C J j A E A y m D s A A S w W G D k n N y 0 D E g g L G y R P L E C C M r C C u L 2 0 q r C R p Y a g v n a e g j Z B t L i B q L p l o I G C t Z V y k 4 u 6 F h 4 U J 7 e v g A i c G E R Q S x 4 7 N G 8 8 R x c S a A p d c K S N K I K i 8 p p w s r L w s X 6 0 o r i q g a W B g b K I m p W q i 0 g r u 0 e X j 4 E f a E + g 5 A w Y 7 E T i b y z x 5 l L u 1 s y g M 6 1 E R V 0 + h o 0 m U 4 g U R l O w i h l i s 2 S u N 3 c n X u v X 6 L 2 C J D 4 2 F g H 2 Y r E m 3 B + Q n M 4 m q d h o y P U I L q B m 2 C A a F T S 5 h o u Q U C M c z m u b Q x X Q e A V x Q 1 Q I 2 J c V J 3 2 S i G U X P E h j B t W k l g a o k M z I h q V U N H E G 0 W g j V q k E U N 2 a M F H W F v l F 4 W C b z A U q + U w p q R o G Q 2 p w a c P W 4 J K J x h g Y U 0 k W u 3 W y m k l r c 1 q x - g G e I J R P o 4 x l r r l q U E q k q d M K O S R N E O L P W G T V h k s w l U 0 O 5 f N a M b u 3 X j Y o A Z i R K M 6 0 + S M w q M g Y 7 G o 6 m b a l C W a I j L C j t J 0 n 3 6 t U F N H b p j G w B R X D s M A A J x C A G t A u Q A B Y d h L p m Y 7 S P i S z A y O X w G F U Q s 0 1 B t X V E E V 9 V S Z r 8 9 G x l d r z c 7 v d D 2 k G I S W P L t T w Q e p B G p E N 8 l D G h V B D L Y d T U f V Z z v Y F Q 3 M S 4 v y t B s H i e B M g l b d s U 0 + T t p n 4 S k 0 K z B R 1 E K d I N G 1 E o z Q q Y x a j y N Q 6 M O B c h T j O 1 B h I T B M C P M l K J S S N o I s E E l l Q r l h G Q k o 9 R h Z E D F U L 1 v W E U M c L r R c b U e H F 7 S C I S R L I 0 C x L d R R 2 R O J Q w Q Q 2 R m M Q E N J D 1 O x j R 5 a E + 1 r A V 6 y X B 4 w D 4 d g N w A V w w I I R j A N d R N l C D l G R J U 6 k f a p K 0 v d Y W S n B Q J E s f J M t O D Y r M - X S + M b A K g t C s B w r - K L g N T M D x P l a w J 0 D K t X N k L l 0 o 2 N j N U L P M P Q 2 H S f L 0 v x d 3 Y A 9 i D I S h M G G w D o p P K j S g 0 f U a D U R F F o V b J 7 x 1 S 9 o O S m Q c l 2 C t R F 4 6 1 p t G - d x F O g 8 A E l G w E 4 J h i i s z p T q t 1 p B 9 C 8 N A 8 p l y n V F l y n 1 E M z V f d T r A U 4 6 P E u 8 7 I Z u k U i N C K A A F t I t G J 6 X X A + a 3 v V D 6 v V 2 Y 4 1 I Q l k z A M a k L C n D 0 K 0 j K 9 w c 6 S G L p G 6 7 G 0 I s U H p R k D n o s j M 3 o U i 9 1 S s R Y j m U y w W R U D J R F E Y 0 P X z d Q r G p u n A L l s 6 Y e x Z 4 j P h p H H r Z t H 6 t K H k J H k L J 1 A U G p s o f Q x x B E W p x a y C X F F l 2 n o Z X A B H Y L s C Y I J 2 F Q V B T M 1 i j L K 5 N j p 2 0 w t P M E U c + y V R C 4 v g p Z l q j X D f I V g 9 E - 3 J W C G X Z 3 X a C B H U A A N w q j 2 v d m 9 H Z g 9 N j p P U Z b I y k F k K Y N T D 1 n s G y c j t + m o d b 1 O A m C F W I i m I v t b V M w c z F 4 w t X q R y E D H f V s i N w t a h y T U 4 6 K k 7 W + T 1 P k B I X c Q j A R H k a C P O N 0 4 c g h L 7 1 6 k K V d Y j E r E W F E y 9 L o X Z K s Q T 7 I 3 j W E F v 5 Y d h 5 1 8 3 t X d - 3 V A N z Y A A F 7 c H Y M f V G v t O Z g g y O k e w q k j B q Q r O l O B l Q q j y C r E o a c R 1 4 5 D R X p - X w R B u C w C C i Q P A Q R - 6 A J A W u I S Q Q I C E j A R Q J 0 E C X p Q O q J U L m + Q V A r F v j q c s M I D g 2 U w R s d I h V B p C n t u 3 R s h D c D E I 3 K Q 3 A e 9 N y H x o X Q 4 h E 0 m E + x Y R B G w l h 9 Q p Q O p 5 a w d R 0 p i A q O L L S 5 g 0 H m l E d + C G e D J E P G k b I + R x l 8 A + B P p z O E K k + w O T 6 n Y H k 6 V N g G k O L k U J O Q 1 j v z O q v K R R C S F k J 8 P g d g h 5 m E c 2 0 U b D I N 4 G j H A K o p f Q j Q L z L A V C I r k d E 3 4 4 P E f Y w C q c n F x I U R Q 4 B o C T I 6 G M j g K A u A P H a K W M T J Q C F z B Z I O E b d K 6 k Y S R g 0 E s R C d h L y W E i U n f B x B Y l y L I f v Z R w l m l C W w G 0 j p G N l j Q Q V M C K o h Z z B i H S i I H a s g 1 D I h E F p O E M y 2 5 V J i T I 2 p Q Q w D O x o Q k q A S S t k p D e j Y A 0 v o q g 5 H K F O L Q v C 0 g w K 0 s O P s 1 g 3 K 3 P E A A G T w B V A A K p 7 T A a c M 5 u 2 z n n d 2 q K v l C E U D A 8 Z C k 1 T p E 8 q O J Q S o u Q 9 S n M I f I G h Y U I t w M i 1 F 4 g A A K E o 1 x B A A I I Q A w B A A g P K I D i k l C k m K 8 0 z T A n N g 0 d I U g 1 J q F W M W K E S o T h Q k y n 6 I 2 U g G W I q C C i r 2 F 1 t 7 q 2 C I K y A A r e X C o 1 r V V J 4 r E I V E O J l L y Z p C z 1 E J q a A 0 B x 1 J P x + d J L V T K d U s o C D v T l x r + W E I R k w H w 6 4 g j u A 0 Z a s V s x g Q S B n s P M F y J c m p B v k q X K o z l p W D i j 6 5 l e q A 2 G u 5 b y k 1 6 c X Z u y Z Q A d x x Y X U V c 1 Z g V 3 2 G 9 M e C E j h H G F m 9 J U 2 U q 5 y G l T Y v C N M V 6 M o L Z g c Q V 1 c A c A I H i h A 6 k 1 T - C u Z q X I V c - r 2 E y P U M Q m o k J 2 A M P m v 1 e q J 1 T q o D V c i W j 5 q m m y p k S 5 c C l A g h B S U S s x N C g l J S o s R Y u 7 y n L 3 l i O - d Y 6 A B y q A g j s p G L A U 1 Q q W Y z o Z J Y C 8 G 0 w S v u R M W d S S o i m 6 N 0 a - T K s K 0 D Y t 1 W i z u W 9 c S 9 3 r c X I Q x w s q 1 D N A c F Q t L F i j j S A a O o O 7 V S Z R U L C r l V b S H B H q V Q s B m B a H 0 P U Z V Z s q A C A Q G 4 G A T w u A c 6 o F 3 O I G A 7 A A C 0 X H G m Y H k 3 g I T k G l j z E Q s i T U x p g X H H S k u 2 E Z p j B u X 7 N M z 9 d j 5 Z s Y 4 + Q g B D T q G 8 d U Q w y g g n h O b g 3 A A 8 Q 4 a S D s C E x u B G s n A i K f s 9 x o S q n c D q a I 9 r Q 2 8 x D r S 0 a E b S 8 6 U q g Z F V d C c w 4 Z 1 i s f Y x w R R B 9 s B H y c - x x h b m R N i Y k 1 J m T c n 5 P L M K + F t T q A N O 6 M k O Y e V t E 4 T G B M Y 1 R C z H T O I m f D l 2 z d W i t 8 b U a V x r B A P N e Z 8 3 5 g B g W a s j Y a 5 F p r 0 W 3 T q B a 2 I d S 6 k O t G E 2 i U Y F O 0 b 7 i 2 M H C O k t Q h t 5 b I F A H w Z X R N M s q 9 J 8 T N X r s + A i 1 F z R V q U g b d L K c b K U E p x V k C Z W C 8 H p l r L F H h 6 S 7 e I 3 E V U m 9 N j c 3 n M C + f 8 w t 4 L r 2 w D v d W 5 9 u N i A N v Q Y v u q d t H q p y B N 2 F e 2 Q N h K f G i O N D q N k U P n 7 j u x V v A V X n v B f e U k r H z X i b q E r A p D Q m 7 V D p X V B U Z a H o T g W H z B s O n n O m f w 4 3 J 5 x H s 3 U d B Y U 3 L 7 n a 2 M w b e J t t K s + Q F S x z T W q P Y K r G 7 y C f k o W 5 Q R c C A Z I r a I i G B W z B U w P d Z H T K Z 3 Z D i p I C s b 1 r w 8 m B K H F C U 4 Y N Q j q E b a e 5 h v K 2 M 6 C Q X L n B 8 D 4 f t F M e M X c E y E Z x w 2 y E 5 R q S 6 L H D y Y 4 k Z S e 8 O X d S P s G x 1 T 2 s v P O S z n Q D z h G 3 B 0 J P P d u D T q 1 9 o 0 Z + x l 0 c g O L 0 k x a k - m r H N A x y M s t 6 - k E b 4 n 7 u i R p 0 n v M r j 0 o l N K j G g r h o d U w I h a g q k O w q j 2 V T R g 4 G j H 5 O 4 g s O j p P 7 n O H k 7 2 B t 8 z 8 R h A F t 9 T i 2 k t P X R 0 J k F - E O D O B U x o - s D o T r T U - X 9 c - b F P A I 9 O f d m B f E W e Y X Y A e S x N k B D X h b I A n P I Q o a E C m G W W v Y - Q A n D Y A y - I 9 V Q W - b W E Q D N f p Y w d S R u e w O + F r M e b d E M G A v k f k W 3 D A e A W I G P W N L P e - O k S Q G Q O Q R Q F Q d Q e 9 R A e T O L F U O o G w T U b - E M W W L E d g u - Y E b M K Q X R K o Q 4 T U e E I P A 7 Q c C O X R O k N 6 E M U 4 a P Q d Z O W Q - u Q 4 b M H N V a P Z K E d Q F k Z y J L U 7 W Q M E I w e Q W F f B Y w 1 6 S 5 f Y J k M P e Q Q o K o G u P U L v K Q H w k M K P P - X B b 9 b V H D N w 7 X A 4 E w S 5 W l a o L M C w Q Q 2 d M 0 S o H t f a I 4 Z Q m + P d H A n o M T a I i C Y w N I 3 I J - E M V V B k R D a C Z 9 f 6 W l K w K 3 T A 2 m H 9 H A 4 D I N U t C A Q o 8 9 N D c 2 T h X K H w k 4 f b R A S s I M F D G + E E d Y C w X I - 1 A 1 X e Y N T o x t c O O D C m f Z I c R D E Y r k S v c Y 8 4 K Y g 9 K - e Y v H V + H 3 U 0 B I q Q K X F I 6 5 f Y C 9 F Q F Q U 4 O k D 9 J e K z K J Z o l l A D I D D l d g F g + f D g g 4 S V U 4 H 6 U 0 a e O E N Y y o F D O k L I R i T D C - X 9 f Y + - Y w L K Q F M w B N M Q m u N I y 8 H x O K c 7 Q s O n J T R z M b F z S - I T G E i 9 J a G w A R D B W l T f A 7 Q 4 P R X I X r U J K E E E O n J b Y r c b V z R r I k 7 - a k Q c K E H N b I Z a E x D Y T w r S Q 4 R K K w
2023-10-11 13:36:54 +11:00
id : 'Modeling' ,
tsTypes : { } as import ( './modelingMachine.typegen' ) . Typegen0 ,
predictableActionArguments : true ,
preserveActionOrder : true ,
context : {
guiMode : 'default' ,
selection : [ ] as string [ ] ,
selectionRanges : {
otherSelections : [ ] ,
codeBasedSelections : [ ] ,
} as Selections ,
selectionRangeTypeMap : { } as SelectionRangeTypeMap ,
sketchPathToNode : null as PathToNode | null , // maybe too specific, and we should have a generic pathToNode, but being specific seems less risky when I'm not sure
sketchEnginePathId : '' as string ,
sketchPlaneId : '' as string ,
} ,
schema : {
events : { } as
| { type : 'Deselect all' }
| { type : 'Deselect edge' ; data : Selection & { type : 'edge' } }
| { type : 'Deselect axis' ; data : Axis }
| {
type : 'Deselect segment'
data : Selection & { type : 'line' | 'arc' }
}
| { type : 'Deselect face' ; data : Selection & { type : 'face' } }
| {
type : 'Deselect point'
data : Selection & { type : 'point' | 'line-end' | 'line-mid' }
}
| { type : 'Enter sketch' }
| { type : 'Select all' ; data : Selection & { type : 'all ' } }
| { type : 'Select edge' ; data : Selection & { type : 'edge' } }
| { type : 'Select axis' ; data : Axis }
| { type : 'Select segment' ; data : Selection & { type : 'line' | 'arc' } }
| { type : 'Select face' ; data : Selection & { type : 'face' } }
| { type : 'Select default plane' ; data : { planeId : string } }
| { type : 'Set selection' ; data : SetSelections }
| {
type : 'Select point'
data : Selection & { type : 'point' | 'line-end' | 'line-mid' }
}
| { type : 'Sketch no face' }
| { type : 'Toggle gui mode' }
| { type : 'Cancel' }
| { type : 'CancelSketch' }
| {
type : 'Add point'
data : {
coords : { x : number ; y : number } [ ]
axis : 'xy' | 'xz' | 'yz' | '-xy' | '-xz' | '-yz' | null
2023-10-14 03:47:46 +11:00
segmentId? : string
2023-10-11 13:36:54 +11:00
}
}
| { type : 'Equip tool' }
| { type : 'Equip move tool' }
| { type : 'Set radius' }
| { type : 'Complete line' }
| { type : 'Set distance' }
| { type : 'Equip new tool' }
| { type : 'update_code' ; data : string }
| { type : 'Make segment horizontal' }
| { type : 'Make segment vertical' }
| { type : 'Constrain horizontal distance' }
| { type : 'Constrain vertical distance' }
| { type : 'Constrain angle' }
| { type : 'Constrain horizontally align' }
| { type : 'Constrain vertically align' }
| { type : 'Constrain length' }
| { type : 'Constrain equal length' }
| { type : 'extrude intent' } ,
// ,
} ,
states : {
idle : {
on : {
'Set selection' : {
target : 'idle' ,
internal : true ,
actions : 'Set selection' ,
} ,
'Deselect point' : {
target : 'idle' ,
internal : true ,
actions : [
'Remove from code-based selection' ,
'Update code selection cursors' ,
// 'Engine: remove highlight',
] ,
cond : 'Selection contains point' ,
} ,
'Deselect edge' : {
target : 'idle' ,
internal : true ,
actions : [
'Remove from code-based selection' ,
'Update code selection cursors' ,
// 'Engine: remove highlight',
] ,
cond : 'Selection contains edge' ,
} ,
'Deselect axis' : {
target : 'idle' ,
internal : true ,
actions : [
'Remove from other selection' ,
'Update code selection cursors' ,
// 'Engine: remove highlight',
] ,
cond : 'Selection contains axis' ,
} ,
'Select point' : {
target : 'idle' ,
internal : true ,
actions : [
'Add to code-based selection' ,
'Update code selection cursors' ,
// 'Engine: add highlight',
] ,
} ,
'Select edge' : {
target : 'idle' ,
internal : true ,
actions : [
'Add to code-based selection' ,
'Update code selection cursors' ,
// 'Engine: add highlight',
] ,
} ,
'Select axis' : {
target : 'idle' ,
internal : true ,
actions : [
'Add to other selection' ,
// 'Engine: add highlight',
] ,
} ,
'Select face' : {
target : 'idle' ,
internal : true ,
actions : [
'Add to code-based selection' ,
'Update code selection cursors' ,
// 'Engine: add highlight',
] ,
} ,
'Enter sketch' : [
{
target : 'Sketch' ,
cond : 'Selection is one face' ,
actions : [
'set sketch metadata' ,
'sketch mode enabled' ,
'edit mode enter' ,
] ,
} ,
'Sketch no face' ,
] ,
'Deselect face' : {
target : 'idle' ,
internal : true ,
actions : [
'Remove from code-based selection' ,
'Update code selection cursors' ,
// 'Engine: remove highlight',
] ,
cond : 'Selection contains face' ,
} ,
'Select all' : {
target : 'idle' ,
internal : true ,
actions : 'Add to code-based selection' ,
} ,
'Deselect all' : {
target : 'idle' ,
internal : true ,
actions : [
'Clear selection' ,
'Update code selection cursors' ,
// 'Engine: remove highlight',
] ,
cond : 'Selection is not empty' ,
} ,
'extrude intent' : [
{
target : 'awaiting selection' ,
cond : 'has no selection' ,
} ,
{
target : 'idle' ,
cond : 'has valid extrude selection' ,
internal : true ,
actions : 'AST extrude' ,
} ,
] ,
} ,
} ,
Sketch : {
states : {
SketchIdle : {
on : {
'Select point' : {
target : 'SketchIdle' ,
internal : true ,
actions : [
'Update code selection cursors' ,
'Add to code-based selection' ,
] ,
} ,
'Select segment' : {
target : 'SketchIdle' ,
internal : true ,
actions : [
'Update code selection cursors' ,
'Add to code-based selection' ,
] ,
} ,
'Deselect point' : {
target : 'SketchIdle' ,
internal : true ,
cond : 'Selection contains point' ,
actions : [
'Update code selection cursors' ,
'Add to code-based selection' ,
] ,
} ,
'Deselect segment' : {
target : 'SketchIdle' ,
internal : true ,
cond : 'Selection contains line' ,
actions : [
'Update code selection cursors' ,
'Add to code-based selection' ,
] ,
} ,
'Equip tool' : {
target : 'Line Tool' ,
actions : 'set tool line' ,
} ,
'Equip move tool' : 'Move Tool' ,
'Set selection' : {
target : 'SketchIdle' ,
internal : true ,
actions : 'Set selection' ,
} ,
'Make segment vertical' : {
cond : 'Can make selection vertical' ,
target : 'SketchIdle' ,
internal : true ,
actions : [ 'Make selection vertical' ] ,
} ,
'Make segment horizontal' : {
target : 'SketchIdle' ,
internal : true ,
cond : 'Can make selection horizontal' ,
actions : [ 'Make selection horizontal' ] ,
} ,
'Constrain horizontal distance' : {
target : 'Await horizontal distance info' ,
cond : 'Can constrain horizontal distance' ,
} ,
'Constrain vertical distance' : {
target : 'Await vertical distance info' ,
cond : 'Can constrain vertical distance' ,
} ,
'Constrain angle' : {
target : 'Await angle info' ,
cond : 'Can constrain angle' ,
} ,
'Constrain length' : {
target : 'Await length info' ,
cond : 'Can constrain length' ,
} ,
'Constrain horizontally align' : {
cond : 'Can constrain horizontally align' ,
target : 'SketchIdle' ,
internal : true ,
actions : [ 'Constrain horizontally align' ] ,
} ,
'Constrain vertically align' : {
cond : 'Can constrain vertically align' ,
target : 'SketchIdle' ,
internal : true ,
actions : [ 'Constrain vertically align' ] ,
} ,
'Constrain equal length' : {
cond : 'Can constrain equal length' ,
target : 'SketchIdle' ,
internal : true ,
actions : [ 'Constrain equal length' ] ,
} ,
} ,
entry : 'equip select' ,
} ,
'Line Tool' : {
states : {
Done : {
type : 'final' ,
} ,
'Point Added' : {
on : {
'Add point' : {
target : 'Segment Added' ,
actions : [ 'AST start new sketch' ] ,
} ,
} ,
} ,
'Segment Added' : {
on : {
'Add point' : {
target : 'Segment Added' ,
internal : true ,
actions : [ 'AST add line segment' ] ,
} ,
'Complete line' : {
target : 'Done' ,
actions : [ 'Modify AST' , 'Update code selection cursors' ] ,
} ,
'Equip new tool' : {
target : 'Segment Added' ,
internal : true ,
actions : 'set tool' ,
} ,
} ,
} ,
Init : {
always : [
{
target : 'Segment Added' ,
cond : 'is editing existing sketch' ,
} ,
'No Points' ,
] ,
} ,
'No Points' : {
on : {
'Add point' : 'Point Added' ,
} ,
} ,
} ,
// invoke: [
// {
// src: 'createLine',
// id: 'Create line',
// onDone: 'SketchIdle',
// },
// ],
initial : 'Init' ,
on : {
'Equip move tool' : 'Move Tool' ,
} ,
} ,
'Move Tool' : {
entry : 'set tool move' ,
2023-10-14 03:47:46 +11:00
on : {
'Set selection' : {
target : 'Move Tool' ,
internal : true ,
actions : 'Set selection' ,
} ,
} ,
states : {
'Move init' : {
always : [
{
target : 'Move without re-execute' ,
cond : 'can move' ,
} ,
{
target : 'Move with execute' ,
cond : 'can move with execute' ,
} ,
'No move' ,
] ,
} ,
'Move without re-execute' : { } ,
'Move with execute' : { } ,
'No move' : { } ,
} ,
initial : 'Move init' ,
2023-10-11 13:36:54 +11:00
} ,
'Await horizontal distance info' : {
invoke : {
src : 'Get horizontal info' ,
id : 'get-horizontal-info' ,
onDone : {
target : 'SketchIdle' ,
actions : 'Set selection' ,
} ,
onError : 'SketchIdle' ,
} ,
} ,
'Await vertical distance info' : {
invoke : {
src : 'Get vertical info' ,
id : 'get-vertical-info' ,
onDone : {
target : 'SketchIdle' ,
actions : 'Set selection' ,
} ,
onError : 'SketchIdle' ,
} ,
} ,
'Await angle info' : {
invoke : {
src : 'Get angle info' ,
id : 'get-angle-info' ,
onDone : {
target : 'SketchIdle' ,
actions : 'Set selection' ,
} ,
onError : 'SketchIdle' ,
} ,
} ,
'Await length info' : {
invoke : {
src : 'Get length info' ,
id : 'get-length-info' ,
onDone : {
target : 'SketchIdle' ,
actions : 'Set selection' ,
} ,
onError : 'SketchIdle' ,
} ,
} ,
} ,
initial : 'SketchIdle' ,
on : {
CancelSketch : '.SketchIdle' ,
} ,
exit : 'sketch exit execute' ,
} ,
'Sketch no face' : {
entry : 'show default planes' ,
exit : 'hide default planes' ,
on : {
'Select default plane' : {
target : 'Sketch' ,
actions : [
'reset sketch metadata' ,
'set default plane id' ,
'sketch mode enabled' ,
'create path' ,
] ,
} ,
} ,
} ,
'awaiting selection' : {
on : {
'Set selection' : {
target : 'checking selection' ,
actions : 'Set selection' ,
} ,
} ,
} ,
'checking selection' : {
always : [
{
target : 'idle' ,
cond : 'has valid extrude selection' ,
actions : 'AST extrude' ,
} ,
{
target : 'idle' ,
actions : 'toast extrude failed' ,
} ,
] ,
} ,
} ,
initial : 'idle' ,
on : {
Cancel : {
target : 'idle' ,
// TODO what if we're existing extrude equiped, should these actions still be fired?
// mabye cancel needs to have a guard for if else logic?
actions : [
'edit_mode_exit' ,
'default_camera_disable_sketch_mode' ,
'reset sketch metadata' ,
] ,
} ,
} ,
} ,
{
guards : {
'is editing existing sketch' : ( { sketchPathToNode } ) = >
! ! sketchPathToNode ,
'Can make selection horizontal' : ( { selectionRanges } ) = >
horzVertInfo ( selectionRanges , 'horizontal' ) . enabled ,
'Can make selection vertical' : ( { selectionRanges } ) = >
horzVertInfo ( selectionRanges , 'vertical' ) . enabled ,
'Can constrain horizontal distance' : ( { selectionRanges } ) = >
horzVertDistanceInfo ( { selectionRanges , constraint : 'setHorzDistance' } )
. enabled ,
'Can constrain vertical distance' : ( { selectionRanges } ) = >
horzVertDistanceInfo ( { selectionRanges , constraint : 'setVertDistance' } )
. enabled ,
'Can constrain angle' : ( { selectionRanges } ) = >
angleBetweenInfo ( { selectionRanges } ) . enabled ,
'Can constrain length' : ( { selectionRanges } ) = >
setAngleLengthInfo ( { selectionRanges } ) . enabled ,
'Can constrain horizontally align' : ( { selectionRanges } ) = >
horzVertDistanceInfo ( { selectionRanges , constraint : 'setHorzDistance' } )
. enabled ,
'Can constrain vertically align' : ( { selectionRanges } ) = >
horzVertDistanceInfo ( { selectionRanges , constraint : 'setHorzDistance' } )
. enabled ,
'Can constrain equal length' : ( { selectionRanges } ) = >
setEqualLengthInfo ( { selectionRanges } ) . enabled ,
'has no selection' : ( { selectionRanges } ) = > {
if ( selectionRanges ? . codeBasedSelections ? . length < 1 ) return true
const selection = selectionRanges ? . codeBasedSelections ? . [ 0 ] || { }
return (
selectionRanges . codeBasedSelections . length === 1 &&
! hasExtrudeSketchGroup ( {
ast : kclManager.ast ,
programMemory : kclManager.programMemory ,
selection ,
} )
)
} ,
'has valid extrude selection' : ( { selectionRanges } ) = > {
if ( selectionRanges . codeBasedSelections . length !== 1 ) return false
const isSketchPipe = isCursorInSketchCommandRange (
engineCommandManager . artifactMap ,
selectionRanges
)
const common = {
selection : selectionRanges.codeBasedSelections [ 0 ] ,
ast : kclManager.ast ,
}
const hasClose = doesPipeHaveCallExp ( { calleeName : 'close' , . . . common } )
const hasExtrude = doesPipeHaveCallExp ( {
calleeName : 'extrude' ,
. . . common ,
} )
return ! ! isSketchPipe && hasClose && ! hasExtrude
} ,
2023-10-14 03:47:46 +11:00
'can move' : ( { selectionRanges } ) = >
// todo check all cursors are also in the right sketch
selectionRanges . codeBasedSelections . every (
( selection ) = >
getConstraintLevelFromSourceRange (
selection . range ,
kclManager . ast
) === 'free'
) ,
'can move with execute' : ( { selectionRanges } ) = >
// todo check all cursors are also in the right sketch
selectionRanges . codeBasedSelections . every ( ( selection ) = >
[ 'partial' , 'free' ] . includes (
getConstraintLevelFromSourceRange ( selection . range , kclManager . ast )
)
) ,
2023-10-11 13:36:54 +11:00
} ,
actions : {
'Add to code-based selection' : assign ( {
selectionRanges : ( { selectionRanges } , event ) = > ( {
. . . selectionRanges ,
codeBasedSelections : [
. . . selectionRanges . codeBasedSelections ,
event . data ,
] ,
} ) ,
} ) ,
'Add to other selection' : assign ( {
selectionRanges : ( { selectionRanges } , event ) = > ( {
. . . selectionRanges ,
otherSelections : [ . . . selectionRanges . otherSelections , event . data ] ,
} ) ,
} ) ,
'Remove from code-based selection' : assign ( {
selectionRanges : ( { selectionRanges } , event ) = > ( {
. . . selectionRanges ,
codeBasedSelections : [
. . . selectionRanges . codeBasedSelections ,
event . data ,
] ,
} ) ,
} ) ,
'Remove from other selection' : assign ( {
selectionRanges : ( { selectionRanges } , event ) = > ( {
. . . selectionRanges ,
otherSelections : [ . . . selectionRanges . otherSelections , event . data ] ,
} ) ,
} ) ,
'Clear selection' : assign ( {
selectionRanges : ( ) = > ( {
otherSelections : [ ] ,
codeBasedSelections : [ ] ,
} ) ,
} ) ,
'sketch mode enabled' : ( { sketchPlaneId } ) = > {
engineCommandManager . sendSceneCommand ( {
type : 'modeling_cmd_req' ,
cmd_id : uuidv4 ( ) ,
cmd : {
type : 'sketch_mode_enable' ,
plane_id : sketchPlaneId ,
ortho : true ,
animated : ! isReducedMotion ( ) ,
} ,
} )
} ,
'edit mode enter' : ( { selectionRanges } ) = > {
const pathId = isCursorInSketchCommandRange (
engineCommandManager . artifactMap ,
selectionRanges
)
pathId &&
engineCommandManager . sendSceneCommand ( {
type : 'modeling_cmd_req' ,
cmd_id : uuidv4 ( ) ,
cmd : {
type : 'edit_mode_enter' ,
target : pathId ,
} ,
} )
} ,
'hide default planes' : ( { } ) = > {
kclManager . hidePlanes ( )
} ,
edit_mode_exit : ( ) = >
engineCommandManager . sendSceneCommand ( {
type : 'modeling_cmd_req' ,
cmd_id : uuidv4 ( ) ,
cmd : { type : 'edit_mode_exit' } ,
} ) ,
default_camera_disable_sketch_mode : ( ) = >
engineCommandManager . sendSceneCommand ( {
type : 'modeling_cmd_req' ,
cmd_id : uuidv4 ( ) ,
cmd : { type : 'default_camera_disable_sketch_mode' } ,
} ) ,
'reset sketch metadata' : assign ( {
sketchPathToNode : null ,
sketchEnginePathId : '' ,
sketchPlaneId : '' ,
} ) ,
'set sketch metadata' : assign ( ( { selectionRanges } ) = > {
const sourceRange = selectionRanges . codeBasedSelections [ 0 ] . range
const sketchPathToNode = getNodePathFromSourceRange (
kclManager . ast ,
sourceRange
)
const pipeExpression = getNodeFromPath < PipeExpression > (
kclManager . ast ,
sketchPathToNode ,
'PipeExpression'
) . node
if ( pipeExpression . type !== 'PipeExpression' ) return { }
const sketchCallExpression = pipeExpression . body . find (
( e ) = >
e . type === 'CallExpression' && e . callee . name === 'startSketchOn'
) as CallExpression
if ( ! sketchCallExpression ) return { }
const firstArg = sketchCallExpression . arguments [ 0 ]
let planeId = ''
if ( firstArg . type === 'Literal' && firstArg . value ) {
const planeStrCleaned = firstArg . value
. toString ( )
. toLowerCase ( )
. replace ( '-' , '' )
if (
planeStrCleaned === 'xy' ||
planeStrCleaned === 'xz' ||
planeStrCleaned === 'yz'
) {
planeId = kclManager . getPlaneId ( planeStrCleaned )
}
}
console . log ( 'planeId' , planeId )
const sketchEnginePathId =
isCursorInSketchCommandRange (
engineCommandManager . artifactMap ,
selectionRanges
) || ''
return {
sketchPathToNode ,
sketchEnginePathId ,
sketchPlaneId : planeId ,
}
} ) ,
'set tool line' : ( ) = >
engineCommandManager . sendSceneCommand ( {
type : 'modeling_cmd_req' ,
cmd_id : uuidv4 ( ) ,
cmd : {
type : 'set_tool' ,
tool : 'sketch_line' ,
} ,
} ) ,
'equip select' : ( ) = >
engineCommandManager . sendSceneCommand ( {
type : 'modeling_cmd_req' ,
cmd_id : uuidv4 ( ) ,
cmd : {
type : 'set_tool' ,
tool : 'select' ,
} ,
} ) ,
'set tool move' : ( ) = >
engineCommandManager . sendSceneCommand ( {
type : 'modeling_cmd_req' ,
cmd_id : uuidv4 ( ) ,
cmd : {
type : 'set_tool' ,
tool : 'move' ,
} ,
} ) ,
'Make selection horizontal' : ( { selectionRanges } ) = > {
const { modifiedAst , pathToNodeMap } = applyConstraintHorzVert (
selectionRanges ,
'horizontal' ,
kclManager . ast ,
kclManager . programMemory
)
kclManager . updateAst ( modifiedAst , true , {
// TODO re implement cursor shit
// callBack: updateCursors(setCursor, selectionRanges, pathToNodeMap),
} )
} ,
'Make selection vertical' : ( { selectionRanges } ) = > {
const { modifiedAst , pathToNodeMap } = applyConstraintHorzVert (
selectionRanges ,
'vertical' ,
kclManager . ast ,
kclManager . programMemory
)
kclManager . updateAst ( modifiedAst , true , {
// TODO re implement cursor shit
// callBack: updateCursors(setCursor, selectionRanges, pathToNodeMap),
} )
} ,
'Constrain horizontally align' : ( { selectionRanges } ) = > {
const { modifiedAst , pathToNodeMap } = applyConstraintHorzVertAlign ( {
selectionRanges ,
constraint : 'setVertDistance' ,
} )
kclManager . updateAst ( modifiedAst , true , {
// TODO re implement cursor shit
// callBack: updateCursors(setCursor, selectionRanges, pathToNodeMap),
} )
} ,
'Constrain vertically align' : ( { selectionRanges } ) = > {
const { modifiedAst , pathToNodeMap } = applyConstraintHorzVertAlign ( {
selectionRanges ,
constraint : 'setHorzDistance' ,
} )
kclManager . updateAst ( modifiedAst , true , {
// TODO re implement cursor shit
// callBack: updateCursors(setCursor, selectionRanges, pathToNodeMap),
} )
} ,
'Constrain equal length' : ( { selectionRanges } ) = > {
const { modifiedAst , pathToNodeMap } = applyConstraintEqualLength ( {
selectionRanges ,
} )
kclManager . updateAst ( modifiedAst , true , {
// TODO re implement cursor shit
// callBack: updateCursors(setCursor, selectionRanges, pathToNodeMap),
} )
} ,
'AST extrude' : ( { selectionRanges } ) = > {
const pathToNode = getNodePathFromSourceRange (
kclManager . ast ,
selectionRanges . codeBasedSelections [ 0 ] . range
)
const { modifiedAst , pathToExtrudeArg } = extrudeSketch (
kclManager . ast ,
pathToNode
)
// TODO not handling focusPath correctly I think
kclManager . updateAst ( modifiedAst , true , {
focusPath : pathToExtrudeArg ,
} )
} ,
'set default plane id' : assign ( {
sketchPlaneId : ( _ , { data } ) = > data . planeId ,
} ) ,
} ,
}
)