2023-10-11 13:36:54 +11:00
import { PathToNode } from 'lang/wasm'
import { engineCommandManager } from 'lang/std/engineConnection'
import { isReducedMotion } from 'lang/util'
2023-10-16 21:20:05 +11:00
import {
Axis ,
Selection ,
SelectionRangeTypeMap ,
Selections ,
} from 'lib/selections'
2023-10-11 13:36:54 +11:00
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-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-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
}
2023-10-16 21:20:05 +11:00
export type ModelingMachineEvent =
| { 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
segmentId? : string
}
}
| { 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 perpendicular distance' }
| { type : 'Constrain horizontally align' }
| { type : 'Constrain vertically align' }
| { type : 'Constrain length' }
| { type : 'Constrain equal length' }
| { type : 'Constrain parallel' }
| { type : 'Constrain remove constraints' }
| { type : 'extrude intent' }
2023-10-11 13:36:54 +11:00
export const modelingMachine = createMachine (
{
2023-10-16 08:54:38 +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 I a w H l 0 h M i z O l f I 8 x s r q T B H Z D 0 8 4 c F 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 S I l N E o R Q v M F D p R f g a c o h h I y S 0 j O - M 6 q 8 p F E J I W Q s g U A f A n 0 5 n C F S 8 g 6 j 2 A O P S I W O o 9 S Z S V B W B S t h M p i y k L Y p O + D i C O L k W Q n w + B 2 C H m Y R z b R R s M i g 2 h C I 0 M w h L y m y x r Y P I 5 g 5 z Z E K o N c R e D J E P G k b I + R Q x N x M E i n Q 8 g w V M A k C 3 K o h h p F N H p I x j S T I G w e R V 0 L M t R S i B T S X k q N y M W 8 k B z R L b o B V O d S n E K I o c A 0 B J k d D G R w F A X A 7 j t F L G J k o B C 5 g G h q X q B M 0 o 6 k Y S R g 0 E s R C d h L y W E W f Y 2 p 8 S G n 7 2 U c J H Z Q l s D 7 M O R j Z Y 0 E F T A i q I W c w Y h 0 o i B 2 r I N Q y I R B a T h G 8 2 J q y E k K L A M 7 G h S S o A p K B S k N 6 N g D S + i q D k C x a U d T V h g V p Y c f Z r B u V R T U g h n y y F M E 6 S Z a g a S Y r A s R D z V Y Z Z V j q A C S U L U 8 w p x m G R O s L M h Q D B M u W Q 4 m R a y g g b j A N n P O Q R y C s r X M m P p P L C V i 2 z O k N Q 3 o R B I h M V S h S l g E p G 0 R O + c o Y s 3 k A B k 8 A V Q A C q e 0 w G n D O b t 1 X 5 0 9 Q S o Q i g Y G P I U m q d I n l R x K C V F y H q U 5 8 l i w q d + C G K 8 X W 4 H d Z 6 8 Q A A F C U a 4 g g A E E I A Y A g A Q Q t E B x S S m 5 X N E u w J z Y N H S F I N S J q t A 6 k R B U S x U J M p + l t d 5 Z N N N U 2 u q C B 6 r 2 F 1 t 7 q 2 C G W y A p a i 0 V o 1 r V f p s x 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 y J d J Z 1 g 7 h 2 Y F H T v P N k 6 S 2 E I R k w H w 6 4 g j u A 0 f O - V Q h g Q S B n s P N I D Q c i E x v k q X K 9 z l p W D i k e 9 N Q 7 M 0 B H P R O o t U 7 0 4 u z d u m g A 7 u 7 A N V b i 5 C A r v s N 6 Y 8 E J H C O M L N 6 I S b x a V 2 P W p N e F + 3 y z T R m k d V 1 c A c A I I G h A 6 k 1 T - C R Z q X I V c - r 2 E y F c s W R L E S F l A z R 0 9 d G G N U B q u R L R 8 0 p n W r p K a O B S g Q Q t p K J W Y m h Q u R G z 5 r Y M c w n w M j o A H K o C C D m k Y s B p 3 l p Z k x h k 1 r r D q D B C l e T x Z 1 J K g V P k u k W R G J v L Q B q k 9 8 Y u 4 J l 7 m h 7 W T I s q 1 D N A c F Q 7 n N o l A 0 P M U 4 f Z Z 7 u X F u R h O t M f M i f E B l y q 4 n g u W X c 9 S L M B x p 6 6 O h M g v 4 h w Z w K m N K c S 8 3 n c 6 Z e y 3 g c T k n z I v o Q C L c V x p L z m H q N Y Z E d 8 r C S D y I U a E F M Z Z i O X v L b L J 6 s s N Z y + w R j q g 9 X V q E O k D I F z T T m G q 1 5 O + u j K h w i Q q X X Y F p J s p v l v m x D p D g g b K o W A z A t D 6 H q M q s 2 V A B A I D c D A J 4 X A O d U C 7 n E D A d g A B a W 7 W z M B A 7 w K 9 6 z d F + F v k p j f T k M X 9 D Z W t U s d U C F B O m k M G 8 y 7 1 3 y E A M 2 d Q h 7 3 T n u Q 7 e 5 u D c A D x C 3 p I O w V 7 G 4 E Y A 8 C C D g n d 2 h I Q 9 w F D v L G Z E I H F Q Y U U 4 d J q t 1 F 4 e U D 6 1 Q w R Q X F s c H H V 2 O C K I P t g I + x O n u M J e 2 9 j 7 6 b v u - f + 4 D o H 3 y l f s 7 J 9 D + Q B 2 U t V i n B x U x 4 4 Q w W B q y c a E 4 t Z d 4 4 N 8 r x 7 a i 1 d k 4 I B T q n N O 6 c A M Z 3 r 1 3 R v O e o B N z C J F f N k R f p F 1 S u i 2 Z t I R s 1 K p W 2 Z 3 K N 2 N x - L l x P h 1 f v c + 9 r v 7 X 2 9 f Z 7 A B z r n K 3 0 P M a U B t x E x x b A e k O N c q s d R Y R N G W B s e o p x e 0 U e T u I T P e J 8 A 5 6 9 z 7 j c 1 O O n + 4 Z 0 z 4 H J e y 9 h + 5 x B S O + o c h z H V J V k Q 4 8 9 T 5 E y I i P 0 E b 9 H d 7 S y v f v D 7 I p 4 v 3 L n z X X 2 8 A 6 6 L 8 z 3 F K T Z 8 m + J u q E 0 + S L C X g 3 w c e Y t I r D H F U h E 5 3 e X e - c - Y f D c S n U f P 3 e n Q P O - U - B - Y 3 e f G T W y S o O K G + b K G w K s a F Q J C w I 1 b K Y E G + S F L S A a P t X v Y - Z p D c Z p X A V p d p T p d 3 H p C q L 3 S - A v X X Z n M g i g q g j p D c I H E n R h R - B A l I Q 2 G E V a I 2 I w T U N Q D f N z c 2 P q N I Y c N t Q A + 6 J p F p J X a g r p V X S g X P E f M f W n K A q f I H V g x Q t p D g r g 1 Q 0 v e A i v b W A Q h Y J Y X I E e M Q h 8 E Q P W K Q - J M 0 W Q t P Z O I I X A Y z E i W 0 I i D A V s d p e 6 D p d N J j c p a 1 f J Z F a 8 E Z I w Q m K c C 8 U M X G I 2 a e c w A - P S E g O X T g f A L e X E K Y f z T I + 0 I L M w 1 6 Y O a k X R M c H k Y 4 S M K c d K T j a k P s D Y d U V d S 8 U R J e D w A 8 c I b c D o X I n u b g R j P g - Q e 5 f Y T j D k A 4 M 5 K o t S E l V Y c 0 H x G x V w 1 o 8 g d o j I 7 u R I R j V r d m d r e + I M Y 0 C u D Q d U Y E E V P o q Q S o X Y a Q x F D 0 B C J w f k D w j A e A W I P t Z 9 V b D r O k S Q G Q O Q R Q F Q d Q F T R A I H f h a B H k f J I 2 R H b B Z o v y M A O 4 y v Y E I 1 D T K o Q 4 T U e E U O K l Q c C O X R O k N 6 E M L v R Z U E - u Q 4 b M I D V a M F K E d Q F k Z y I 2 W o Q G Y k 8 s e V R W b o D E 1 6 R F f Y J k K E b x W V J H B A Z E J f B S R k k M J I 1 L X B K j Y 9 T 1 a k n n A 4 E w R F B N K Q
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 : {
2023-10-16 21:20:05 +11:00
events : { } as ModelingMachineEvent ,
2023-10-11 13:36:54 +11:00
} ,
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' ,
} ,
2023-10-16 08:54:38 +11:00
'Constrain perpendicular distance' : {
target : 'Await perpendicular distance info' ,
cond : 'Can constrain perpendicular distance' ,
} ,
2023-10-11 13:36:54 +11:00
'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' ] ,
} ,
2023-10-16 08:54:38 +11:00
'Constrain parallel' : {
target : 'SketchIdle' ,
internal : true ,
cond : 'Can canstrain parallel' ,
actions : [ 'Constrain parallel' ] ,
} ,
'Constrain remove constraints' : {
target : 'SketchIdle' ,
internal : true ,
cond : 'Can constrain remove constraints' ,
actions : [ 'Constrain remove constraints' ] ,
} ,
2023-10-11 13:36:54 +11:00
} ,
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' ,
} ,
} ,
2023-10-16 08:54:38 +11:00
'Await perpendicular distance info' : {
invoke : {
src : 'Get perpendicular distance info' ,
id : 'get-perpendicular-distance-info' ,
onDone : {
target : 'SketchIdle' ,
actions : 'Set selection' ,
} ,
onError : 'SketchIdle' ,
} ,
} ,
2023-10-11 13:36:54 +11:00
} ,
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 ,
2023-10-16 08:54:38 +11:00
'Can constrain perpendicular distance' : ( { selectionRanges } ) = >
intersectInfo ( { selectionRanges } ) . enabled ,
2023-10-11 13:36:54 +11:00
'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 ,
2023-10-16 08:54:38 +11:00
'Can canstrain parallel' : ( { selectionRanges } ) = >
equalAngleInfo ( { selectionRanges } ) . enabled ,
'Can constrain remove constraints' : ( { selectionRanges } ) = >
removeConstrainingValuesInfo ( { selectionRanges } ) . enabled ,
2023-10-11 13:36:54 +11:00
'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' ,
} ,
} ) ,
2023-10-16 08:54:38 +11:00
// TODO implement source ranges for all of these constraints
// need to make the async like the modal constraints
2023-10-11 13:36:54 +11:00
'Make selection horizontal' : ( { selectionRanges } ) = > {
const { modifiedAst , pathToNodeMap } = applyConstraintHorzVert (
selectionRanges ,
'horizontal' ,
kclManager . ast ,
kclManager . programMemory
)
2023-10-16 08:54:38 +11:00
kclManager . updateAst ( modifiedAst , true )
2023-10-11 13:36:54 +11:00
} ,
'Make selection vertical' : ( { selectionRanges } ) = > {
const { modifiedAst , pathToNodeMap } = applyConstraintHorzVert (
selectionRanges ,
'vertical' ,
kclManager . ast ,
kclManager . programMemory
)
2023-10-16 08:54:38 +11:00
kclManager . updateAst ( modifiedAst , true )
2023-10-11 13:36:54 +11:00
} ,
'Constrain horizontally align' : ( { selectionRanges } ) = > {
const { modifiedAst , pathToNodeMap } = applyConstraintHorzVertAlign ( {
selectionRanges ,
constraint : 'setVertDistance' ,
} )
2023-10-16 08:54:38 +11:00
kclManager . updateAst ( modifiedAst , true )
2023-10-11 13:36:54 +11:00
} ,
'Constrain vertically align' : ( { selectionRanges } ) = > {
const { modifiedAst , pathToNodeMap } = applyConstraintHorzVertAlign ( {
selectionRanges ,
constraint : 'setHorzDistance' ,
} )
2023-10-16 08:54:38 +11:00
kclManager . updateAst ( modifiedAst , true )
2023-10-11 13:36:54 +11:00
} ,
'Constrain equal length' : ( { selectionRanges } ) = > {
const { modifiedAst , pathToNodeMap } = applyConstraintEqualLength ( {
selectionRanges ,
} )
2023-10-16 08:54:38 +11:00
kclManager . updateAst ( modifiedAst , true )
} ,
'Constrain parallel' : ( { selectionRanges } ) = > {
const { modifiedAst , pathToNodeMap } = applyConstraintEqualAngle ( {
selectionRanges ,
} )
kclManager . updateAst ( modifiedAst , true )
} ,
'Constrain remove constraints' : ( { selectionRanges } ) = > {
const { modifiedAst , pathToNodeMap } = applyRemoveConstrainingValues ( {
selectionRanges ,
2023-10-11 13:36:54 +11:00
} )
2023-10-16 08:54:38 +11:00
kclManager . updateAst ( modifiedAst , true )
2023-10-11 13:36:54 +11:00
} ,
'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 ,
} ) ,
} ,
}
)