Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix sketch-on-face sketches to be in the artifact graph #5489

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
66 changes: 43 additions & 23 deletions src/wasm-lib/kcl/src/execution/artifact.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,15 @@
pub path_to_node: DummyPathToNode,
}

impl CodeRef {
pub fn placeholder(range: SourceRange) -> Self {
Self {
range,
path_to_node: Vec::new(),
}
}
}

#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS)]
#[ts(export_to = "Artifact.ts")]
#[serde(rename_all = "camelCase")]
Expand Down Expand Up @@ -177,6 +186,24 @@
pub path_id: ArtifactId,
}

#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS)]
#[ts(export_to = "Artifact.ts")]
#[serde(rename_all = "camelCase")]
pub struct StartSketchOnFace {
pub id: ArtifactId,
pub face_id: ArtifactId,
pub code_ref: CodeRef,
}

#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS)]
#[ts(export_to = "Artifact.ts")]
#[serde(rename_all = "camelCase")]
pub struct StartSketchOnPlane {
pub id: ArtifactId,
pub plane_id: ArtifactId,
pub code_ref: CodeRef,
}

#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS)]
#[ts(export_to = "Artifact.ts")]
#[serde(rename_all = "camelCase")]
Expand Down Expand Up @@ -294,18 +321,8 @@
Path(Path),
Segment(Segment),
Solid2d(Solid2d),
#[serde(rename_all = "camelCase")]
StartSketchOnFace {
id: ArtifactId,
face_id: Uuid,
source_range: SourceRange,
},
#[serde(rename_all = "camelCase")]
StartSketchOnPlane {
id: ArtifactId,
plane_id: Uuid,
source_range: SourceRange,
},
StartSketchOnFace(StartSketchOnFace),
StartSketchOnPlane(StartSketchOnPlane),
Sweep(Sweep),
Wall(Wall),
Cap(Cap),
Expand All @@ -322,8 +339,8 @@
Artifact::Path(a) => a.id,
Artifact::Segment(a) => a.id,
Artifact::Solid2d(a) => a.id,
Artifact::StartSketchOnFace { id, .. } => *id,
Artifact::StartSketchOnPlane { id, .. } => *id,
Artifact::StartSketchOnFace(a) => a.id,
Artifact::StartSketchOnPlane(a) => a.id,
Artifact::Sweep(a) => a.id,
Artifact::Wall(a) => a.id,
Artifact::Cap(a) => a.id,
Expand All @@ -341,9 +358,8 @@
Artifact::Path(a) => Some(&a.code_ref),
Artifact::Segment(a) => Some(&a.code_ref),
Artifact::Solid2d(_) => None,
// TODO: We should add code refs for these.
Artifact::StartSketchOnFace { .. } => None,
Artifact::StartSketchOnPlane { .. } => None,
Artifact::StartSketchOnFace(a) => Some(&a.code_ref),
Artifact::StartSketchOnPlane(a) => Some(&a.code_ref),

Check warning on line 362 in src/wasm-lib/kcl/src/execution/artifact.rs

View check run for this annotation

Codecov / codecov/patch

src/wasm-lib/kcl/src/execution/artifact.rs#L361-L362

Added lines #L361 - L362 were not covered by tests
Artifact::Sweep(a) => Some(&a.code_ref),
Artifact::Wall(_) => None,
Artifact::Cap(_) => None,
Expand Down Expand Up @@ -506,6 +522,10 @@
}
}

for exec_artifact in exec_artifacts.values() {
merge_artifact_into_map(&mut map, exec_artifact.clone());
}
Comment on lines +525 to +527
Copy link
Collaborator Author

@jtran jtran Feb 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the core of the change.

It's unfortunate that there are two loops, this one and the one above. I think this might cause some problems when there are multiple sketches-on-face/place on the same face/plane.


Ok(ArtifactGraph { map })
}

Expand Down Expand Up @@ -838,15 +858,15 @@
})
})?;
let extra_artifact = exec_artifacts.values().find(|a| {
if let Artifact::StartSketchOnFace { face_id: id, .. } = a {
*id == face_id.0
if let Artifact::StartSketchOnFace(s) = a {
s.face_id == face_id
} else {
false
}
});
let sketch_on_face_source_range = extra_artifact
.and_then(|a| match a {
Artifact::StartSketchOnFace { source_range, .. } => Some(*source_range),
Artifact::StartSketchOnFace(s) => Some(s.code_ref.range),
// TODO: If we didn't find it, it's probably a bug.
_ => None,
})
Expand Down Expand Up @@ -891,15 +911,15 @@
})
})?;
let extra_artifact = exec_artifacts.values().find(|a| {
if let Artifact::StartSketchOnFace { face_id: id, .. } = a {
*id == face_id.0
if let Artifact::StartSketchOnFace(s) = a {
s.face_id == face_id
} else {
false
}
});
let sketch_on_face_source_range = extra_artifact
.and_then(|a| match a {
Artifact::StartSketchOnFace { source_range, .. } => Some(*source_range),
Artifact::StartSketchOnFace(s) => Some(s.code_ref.range),
_ => None,
})
.unwrap_or_default();
Expand Down
26 changes: 15 additions & 11 deletions src/wasm-lib/kcl/src/execution/artifact/mermaid_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ impl Artifact {
Artifact::Path(a) => vec![a.plane_id],
Artifact::Segment(a) => vec![a.path_id],
Artifact::Solid2d(a) => vec![a.path_id],
Artifact::StartSketchOnFace { face_id, .. } => vec![face_id.into()],
Artifact::StartSketchOnPlane { plane_id, .. } => vec![plane_id.into()],
Artifact::StartSketchOnFace(a) => vec![a.face_id],
Artifact::StartSketchOnPlane(a) => vec![a.plane_id],
Artifact::Sweep(a) => vec![a.path_id],
Artifact::Wall(a) => vec![a.seg_id, a.sweep_id],
Artifact::Cap(a) => vec![a.sweep_id],
Expand Down Expand Up @@ -115,8 +115,14 @@ impl Artifact {
// Note: Don't include these since they're parents: path_id.
Vec::new()
}
Artifact::StartSketchOnFace { .. } => Vec::new(),
Artifact::StartSketchOnPlane { .. } => Vec::new(),
Artifact::StartSketchOnFace { .. } => {
// Note: Don't include these since they're parents: face_id.
Vec::new()
}
Artifact::StartSketchOnPlane { .. } => {
// Note: Don't include these since they're parents: plane_id.
Vec::new()
}
Artifact::Sweep(a) => {
// Note: Don't include these since they're parents: path_id.
let mut ids = Vec::new();
Expand Down Expand Up @@ -267,9 +273,7 @@ impl ArtifactGraph {
) -> std::fmt::Result {
// For now, only showing the source range.
fn code_ref_display(code_ref: &CodeRef) -> [usize; 3] {
range_display(code_ref.range)
}
fn range_display(range: SourceRange) -> [usize; 3] {
let range = code_ref.range;
[range.start(), range.end(), range.module_id().as_usize()]
}

Expand Down Expand Up @@ -301,20 +305,20 @@ impl ArtifactGraph {
Artifact::Solid2d(_solid2d) => {
writeln!(output, "{prefix}{}[Solid2d]", id)?;
}
Artifact::StartSketchOnFace { source_range, .. } => {
Artifact::StartSketchOnFace(StartSketchOnFace { code_ref, .. }) => {
writeln!(
output,
"{prefix}{}[\"StartSketchOnFace<br>{:?}\"]",
id,
range_display(*source_range)
code_ref_display(code_ref)
)?;
}
Artifact::StartSketchOnPlane { source_range, .. } => {
Artifact::StartSketchOnPlane(StartSketchOnPlane { code_ref, .. }) => {
writeln!(
output,
"{prefix}{}[\"StartSketchOnPlane<br>{:?}\"]",
id,
range_display(*source_range)
code_ref_display(code_ref)
)?;
}
Artifact::Sweep(sweep) => {
Expand Down
4 changes: 3 additions & 1 deletion src/wasm-lib/kcl/src/execution/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ use crate::{
CompilationError, ExecError, ExecutionKind, KclErrorWithOutputs,
};

pub use artifact::{Artifact, ArtifactCommand, ArtifactGraph, ArtifactId};
pub use artifact::{
Artifact, ArtifactCommand, ArtifactGraph, ArtifactId, CodeRef, StartSketchOnFace, StartSketchOnPlane,
};
pub use cache::{bust_cache, clear_mem_cache};
pub use cad_op::Operation;
pub use geometry::*;
Expand Down
17 changes: 9 additions & 8 deletions src/wasm-lib/kcl/src/std/sketch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use parse_display::{Display, FromStr};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

use crate::execution::{CodeRef, StartSketchOnFace, StartSketchOnPlane};
use crate::{
errors::{KclError, KclErrorDetails},
execution::{
Expand Down Expand Up @@ -1144,11 +1145,11 @@ async fn inner_start_sketch_on(
SketchData::Plane(plane) => {
// Create artifact used only by the UI, not the engine.
let id = exec_state.next_uuid();
exec_state.add_artifact(Artifact::StartSketchOnPlane {
exec_state.add_artifact(Artifact::StartSketchOnPlane(StartSketchOnPlane {
id: ArtifactId::from(id),
plane_id: plane.id,
source_range: args.source_range,
});
plane_id: plane.artifact_id,
code_ref: CodeRef::placeholder(args.source_range),
}));

Ok(SketchSurface::Plane(plane))
}
Expand All @@ -1163,11 +1164,11 @@ async fn inner_start_sketch_on(

// Create artifact used only by the UI, not the engine.
let id = exec_state.next_uuid();
exec_state.add_artifact(Artifact::StartSketchOnFace {
exec_state.add_artifact(Artifact::StartSketchOnFace(StartSketchOnFace {
id: ArtifactId::from(id),
face_id: face.id,
source_range: args.source_range,
});
face_id: face.artifact_id,
code_ref: CodeRef::placeholder(args.source_range),
}));

Ok(SketchSurface::Face(face))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ flowchart LR
39["SweepEdge Adjacent"]
40["SweepEdge Opposite"]
41["SweepEdge Adjacent"]
42["StartSketchOnFace<br>[345, 377, 0]"]
1 --- 2
2 --- 3
2 --- 4
Expand Down Expand Up @@ -106,4 +107,5 @@ flowchart LR
31 --- 39
31 --- 40
31 --- 41
11 <--x 42
```
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ flowchart LR
1["Plane<br>[17, 47, 0]"]
2["Plane<br>[65, 96, 0]"]
3["Plane<br>[114, 144, 0]"]
6["StartSketchOnPlane<br>[158, 187, 0]"]
1 --- 4
4 --- 5
1 <--x 6
```
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ flowchart LR
68["SweepEdge Adjacent"]
69["SweepEdge Opposite"]
70["SweepEdge Adjacent"]
71["StartSketchOnFace<br>[257, 289, 0]"]
72["StartSketchOnFace<br>[506, 538, 0]"]
73["StartSketchOnFace<br>[768, 800, 0]"]
1 --- 2
2 --- 3
2 --- 4
Expand Down Expand Up @@ -183,4 +186,7 @@ flowchart LR
60 --- 68
60 --- 69
60 --- 70
10 <--x 71
30 <--x 72
46 <--x 73
```
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ flowchart LR
37["SweepEdge Adjacent"]
38["EdgeCut Fillet<br>[846, 973, 0]"]
39["EdgeCut Fillet<br>[846, 973, 0]"]
40["StartSketchOnFace<br>[442, 464, 0]"]
41["StartSketchOnFace<br>[442, 464, 0]"]
1 --- 2
2 --- 3
2 --- 4
Expand Down Expand Up @@ -98,4 +100,6 @@ flowchart LR
33 --- 36
33 --- 37
36 <--x 39
9 <--x 40
7 <--x 41
```
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ flowchart LR
52["SweepEdge Adjacent"]
53["SweepEdge Opposite"]
54["SweepEdge Adjacent"]
55["StartSketchOnFace<br>[670, 702, 0]"]
56["StartSketchOnFace<br>[1118, 1150, 0]"]
1 --- 2
2 --- 3
2 --- 4
Expand Down Expand Up @@ -136,4 +138,6 @@ flowchart LR
41 --- 52
41 --- 53
41 --- 54
26 <--x 55
25 <--x 56
```
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ flowchart LR
52["SweepEdge Adjacent"]
53["SweepEdge Opposite"]
54["SweepEdge Adjacent"]
55["StartSketchOnFace<br>[670, 702, 0]"]
56["StartSketchOnFace<br>[1118, 1150, 0]"]
1 --- 2
2 --- 3
2 --- 4
Expand Down Expand Up @@ -136,4 +138,6 @@ flowchart LR
41 --- 52
41 --- 53
41 --- 54
25 <--x 55
26 <--x 56
```
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ flowchart LR
41["SweepEdge Adjacent"]
42["SweepEdge Opposite"]
43["SweepEdge Adjacent"]
44["StartSketchOnFace<br>[231, 259, 0]"]
1 --- 2
2 --- 3
2 --- 4
Expand Down Expand Up @@ -113,4 +114,5 @@ flowchart LR
29 --- 41
29 --- 42
29 --- 43
12 <--x 44
```
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ flowchart LR
51["SweepEdge Adjacent"]
52["SweepEdge Opposite"]
53["SweepEdge Adjacent"]
54["StartSketchOnFace<br>[1496, 1525, 0]"]
1 --- 2
2 --- 3
2 --- 4
Expand Down Expand Up @@ -139,4 +140,5 @@ flowchart LR
40 --- 51
40 --- 52
40 --- 53
12 <--x 54
```
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ flowchart LR
29["Cap End"]
30["SweepEdge Opposite"]
31["SweepEdge Adjacent"]
32["StartSketchOnFace<br>[263, 292, 0]"]
1 --- 2
2 --- 3
2 --- 4
Expand Down Expand Up @@ -80,4 +81,5 @@ flowchart LR
26 --- 29
26 --- 30
26 --- 31
14 <--x 32
```
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ flowchart LR
41["SweepEdge Adjacent"]
42["SweepEdge Opposite"]
43["SweepEdge Adjacent"]
44["StartSketchOnFace<br>[263, 292, 0]"]
1 --- 2
2 --- 3
2 --- 4
Expand Down Expand Up @@ -113,4 +114,5 @@ flowchart LR
29 --- 41
29 --- 42
29 --- 43
14 <--x 44
```
Loading
Loading