From 17aec7af08a10a94fd7c243085d867b98ebfdd82 Mon Sep 17 00:00:00 2001 From: Jonathan Tran Date: Wed, 18 Dec 2024 17:33:07 -0500 Subject: [PATCH] Add Rust side artifacts for startSketchOn face or plane --- src/lang/KclSingleton.ts | 2 + src/lang/wasm.ts | 7 ++++ src/wasm-lib/kcl/src/execution/artifact.rs | 47 ++++++++++++++++++++++ src/wasm-lib/kcl/src/execution/mod.rs | 10 +++++ src/wasm-lib/kcl/src/std/sketch.rs | 22 +++++++++- 5 files changed, 87 insertions(+), 1 deletion(-) create mode 100644 src/wasm-lib/kcl/src/execution/artifact.rs diff --git a/src/lang/KclSingleton.ts b/src/lang/KclSingleton.ts index e1222ccae..025002558 100644 --- a/src/lang/KclSingleton.ts +++ b/src/lang/KclSingleton.ts @@ -313,6 +313,8 @@ export class KclManager { this.addDiagnostics(await lintAst({ ast: ast })) setSelectionFilterToDefault(execState.memory, this.engineCommandManager) + console.log('executeAst artifacts', execState.artifacts) + if (args.zoomToFit) { let zoomObjectId: string | undefined = '' if (args.zoomOnRangeAndType) { diff --git a/src/lang/wasm.ts b/src/lang/wasm.ts index 908030d07..6eb5f87a1 100644 --- a/src/lang/wasm.ts +++ b/src/lang/wasm.ts @@ -44,7 +44,11 @@ import { CompilationError } from 'wasm-lib/kcl/bindings/CompilationError' import { SourceRange as RustSourceRange } from 'wasm-lib/kcl/bindings/SourceRange' import { getAllCurrentSettings } from 'lib/settings/settingsUtils' import { KclErrorWithOutputs } from 'wasm-lib/kcl/bindings/KclErrorWithOutputs' +import { Artifact } from 'wasm-lib/kcl/bindings/Artifact' +import { ArtifactId } from 'wasm-lib/kcl/bindings/ArtifactId' +export type { Artifact } from 'wasm-lib/kcl/bindings/Artifact' +export type { ArtifactId } from 'wasm-lib/kcl/bindings/ArtifactId' export type { Configuration } from 'wasm-lib/kcl/bindings/Configuration' export type { Program } from '../wasm-lib/kcl/bindings/Program' export type { Expr } from '../wasm-lib/kcl/bindings/Expr' @@ -245,6 +249,7 @@ export const isPathToNodeNumber = ( export interface ExecState { memory: ProgramMemory + artifacts: { [key in ArtifactId]?: Artifact } } /** @@ -254,12 +259,14 @@ export interface ExecState { export function emptyExecState(): ExecState { return { memory: ProgramMemory.empty(), + artifacts: {}, } } function execStateFromRaw(raw: RawExecState): ExecState { return { memory: ProgramMemory.fromRaw(raw.modLocal.memory), + artifacts: raw.global.artifacts, } } diff --git a/src/wasm-lib/kcl/src/execution/artifact.rs b/src/wasm-lib/kcl/src/execution/artifact.rs new file mode 100644 index 000000000..a7f8330bb --- /dev/null +++ b/src/wasm-lib/kcl/src/execution/artifact.rs @@ -0,0 +1,47 @@ +use schemars::JsonSchema; +use serde::{Deserialize, Serialize}; +use uuid::Uuid; + +use crate::SourceRange; + +#[derive(Debug, Clone, Copy, Deserialize, Serialize, PartialEq, Eq, Hash, ts_rs::TS, JsonSchema)] +#[ts(export)] +pub struct ArtifactId(Uuid); + +impl ArtifactId { + pub fn new(uuid: Uuid) -> Self { + Self(uuid) + } +} + +impl From for ArtifactId { + fn from(uuid: Uuid) -> Self { + Self::new(uuid) + } +} + +impl From<&Uuid> for ArtifactId { + fn from(uuid: &Uuid) -> Self { + Self::new(*uuid) + } +} + +#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema)] +#[ts(export)] +#[serde(rename_all = "camelCase")] +pub struct Artifact { + pub id: ArtifactId, + #[serde(flatten)] + pub inner: ArtifactInner, + pub source_range: SourceRange, +} + +#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema)] +#[ts(export)] +#[serde(tag = "type")] +pub enum ArtifactInner { + #[serde(rename_all = "camelCase")] + StartSketchOnFace { face_id: Uuid }, + #[serde(rename_all = "camelCase")] + StartSketchOnPlane { plane_id: Uuid }, +} diff --git a/src/wasm-lib/kcl/src/execution/mod.rs b/src/wasm-lib/kcl/src/execution/mod.rs index c48a71bbe..5536b103c 100644 --- a/src/wasm-lib/kcl/src/execution/mod.rs +++ b/src/wasm-lib/kcl/src/execution/mod.rs @@ -25,6 +25,7 @@ pub use kcl_value::{KclObjectFields, KclValue}; use uuid::Uuid; mod annotations; +mod artifact; pub(crate) mod cache; mod cad_op; mod exec_ast; @@ -47,6 +48,7 @@ use crate::{ }; // Re-exports. +pub use artifact::{Artifact, ArtifactId, ArtifactInner}; pub use cad_op::Operation; /// State for executing a program. @@ -68,6 +70,8 @@ pub struct GlobalState { pub path_to_source_id: IndexMap, /// Map from module ID to module info. pub module_infos: IndexMap, + /// Output map of UUIDs to artifacts. + pub artifacts: IndexMap, } #[derive(Debug, Default, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema)] @@ -134,6 +138,11 @@ impl ExecState { self.global.id_generator.next_uuid() } + pub fn add_artifact(&mut self, artifact: Artifact) { + let id = artifact.id; + self.global.artifacts.insert(id, artifact); + } + async fn add_module( &mut self, path: std::path::PathBuf, @@ -171,6 +180,7 @@ impl GlobalState { id_generator: Default::default(), path_to_source_id: Default::default(), module_infos: Default::default(), + artifacts: Default::default(), }; // TODO(#4434): Use the top-level file's path. diff --git a/src/wasm-lib/kcl/src/std/sketch.rs b/src/wasm-lib/kcl/src/std/sketch.rs index 6d9fc3e63..6eb82861a 100644 --- a/src/wasm-lib/kcl/src/std/sketch.rs +++ b/src/wasm-lib/kcl/src/std/sketch.rs @@ -11,6 +11,7 @@ use parse_display::{Display, FromStr}; use schemars::JsonSchema; use serde::{Deserialize, Serialize}; +use crate::execution::{Artifact, ArtifactId, ArtifactInner}; use crate::{ errors::{KclError, KclErrorDetails}, execution::{ @@ -1075,7 +1076,17 @@ async fn inner_start_sketch_on( let plane = make_sketch_plane_from_orientation(plane_data, exec_state, args).await?; Ok(SketchSurface::Plane(plane)) } - SketchData::Plane(plane) => Ok(SketchSurface::Plane(plane)), + SketchData::Plane(plane) => { + // Create artifact used only by the UI, not the engine. + let id = exec_state.next_uuid(); + exec_state.add_artifact(Artifact { + id: ArtifactId::from(id), + inner: ArtifactInner::StartSketchOnPlane { plane_id: plane.id }, + source_range: args.source_range, + }); + + Ok(SketchSurface::Plane(plane)) + } SketchData::Solid(solid) => { let Some(tag) = tag else { return Err(KclError::Type(KclErrorDetails { @@ -1084,6 +1095,15 @@ async fn inner_start_sketch_on( })); }; let face = start_sketch_on_face(solid, tag, exec_state, args).await?; + + // Create artifact used only by the UI, not the engine. + let id = exec_state.next_uuid(); + exec_state.add_artifact(Artifact { + id: ArtifactId::from(id), + inner: ArtifactInner::StartSketchOnFace { face_id: face.id }, + source_range: args.source_range, + }); + Ok(SketchSurface::Face(face)) } }