Skip to content

Commit

Permalink
Add units to geometry types
Browse files Browse the repository at this point in the history
Signed-off-by: Nick Cameron <[email protected]>
  • Loading branch information
nrc committed Jan 16, 2025
1 parent a9daa2e commit 5600b7a
Show file tree
Hide file tree
Showing 45 changed files with 7,513 additions and 3,640 deletions.
21 changes: 21 additions & 0 deletions src/lang/artifact.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ const mySketch001 = startSketchOn('XY')
},
],
id: expect.any(String),
units: {
type: 'Mm',
},
__meta: [{ sourceRange: [46, 71, 0] }],
},
})
Expand Down Expand Up @@ -93,6 +96,9 @@ const mySketch001 = startSketchOn('XY')
],
sketch: {
id: expect.any(String),
units: {
type: 'Mm',
},
__meta: expect.any(Array),
on: expect.any(Object),
start: expect.any(Object),
Expand Down Expand Up @@ -123,6 +129,9 @@ const mySketch001 = startSketchOn('XY')
height: 2,
startCapId: expect.any(String),
endCapId: expect.any(String),
units: {
type: 'Mm',
},
__meta: [{ sourceRange: [46, 71, 0] }],
},
})
Expand Down Expand Up @@ -194,6 +203,9 @@ const sk2 = startSketchOn('XY')
on: expect.any(Object),
start: expect.any(Object),
type: 'Sketch',
units: {
type: 'Mm',
},
tags: {
p: {
__meta: [
Expand Down Expand Up @@ -247,6 +259,9 @@ const sk2 = startSketchOn('XY')
height: 2,
startCapId: expect.any(String),
endCapId: expect.any(String),
units: {
type: 'Mm',
},
__meta: [{ sourceRange: [38, 63, 0] }],
},
},
Expand Down Expand Up @@ -285,6 +300,9 @@ const sk2 = startSketchOn('XY')
],
sketch: {
id: expect.any(String),
units: {
type: 'Mm',
},
__meta: expect.any(Array),
on: expect.any(Object),
start: expect.any(Object),
Expand Down Expand Up @@ -343,6 +361,9 @@ const sk2 = startSketchOn('XY')
startCapId: expect.any(String),
endCapId: expect.any(String),
__meta: [{ sourceRange: [342, 367, 0] }],
units: {
type: 'Mm',
},
},
},
])
Expand Down
3 changes: 3 additions & 0 deletions src/lang/executor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,9 @@ const newVar = myVar + 1`
},
],
id: expect.any(String),
units: {
type: 'Mm',
},
__meta: [{ sourceRange: [39, 63, 0] }],
},
})
Expand Down
12 changes: 12 additions & 0 deletions src/wasm-lib/kcl/src/execution/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -746,6 +746,7 @@ pub struct Helix {
pub angle_start: f64,
/// Is the helix rotation counter clockwise?
pub ccw: bool,
pub units: UnitLen,
#[serde(rename = "__meta")]
pub meta: Vec<Metadata>,
}
Expand All @@ -767,6 +768,7 @@ pub struct Plane {
pub y_axis: Point3d,
/// The z-axis (normal).
pub z_axis: Point3d,
pub units: UnitLen,
#[serde(rename = "__meta")]
pub meta: Vec<Metadata>,
}
Expand All @@ -782,6 +784,7 @@ impl Plane {
y_axis: Point3d::new(0.0, 1.0, 0.0),
z_axis: Point3d::new(0.0, 0.0, 1.0),
value: PlaneType::XY,
units: exec_state.length_unit(),
meta: vec![],
},
crate::std::sketch::PlaneData::NegXY => Plane {
Expand All @@ -791,6 +794,7 @@ impl Plane {
y_axis: Point3d::new(0.0, 1.0, 0.0),
z_axis: Point3d::new(0.0, 0.0, -1.0),
value: PlaneType::XY,
units: exec_state.length_unit(),
meta: vec![],
},
crate::std::sketch::PlaneData::XZ => Plane {
Expand All @@ -800,6 +804,7 @@ impl Plane {
y_axis: Point3d::new(0.0, 0.0, 1.0),
z_axis: Point3d::new(0.0, -1.0, 0.0),
value: PlaneType::XZ,
units: exec_state.length_unit(),
meta: vec![],
},
crate::std::sketch::PlaneData::NegXZ => Plane {
Expand All @@ -809,6 +814,7 @@ impl Plane {
y_axis: Point3d::new(0.0, 0.0, 1.0),
z_axis: Point3d::new(0.0, 1.0, 0.0),
value: PlaneType::XZ,
units: exec_state.length_unit(),
meta: vec![],
},
crate::std::sketch::PlaneData::YZ => Plane {
Expand All @@ -818,6 +824,7 @@ impl Plane {
y_axis: Point3d::new(0.0, 0.0, 1.0),
z_axis: Point3d::new(1.0, 0.0, 0.0),
value: PlaneType::YZ,
units: exec_state.length_unit(),
meta: vec![],
},
crate::std::sketch::PlaneData::NegYZ => Plane {
Expand All @@ -827,6 +834,7 @@ impl Plane {
y_axis: Point3d::new(0.0, 0.0, 1.0),
z_axis: Point3d::new(-1.0, 0.0, 0.0),
value: PlaneType::YZ,
units: exec_state.length_unit(),
meta: vec![],
},
crate::std::sketch::PlaneData::Plane {
Expand All @@ -841,6 +849,7 @@ impl Plane {
y_axis: *y_axis,
z_axis: *z_axis,
value: PlaneType::Custom,
units: exec_state.length_unit(),
meta: vec![],
},
}
Expand Down Expand Up @@ -887,6 +896,7 @@ pub struct Face {
pub z_axis: Point3d,
/// The solid the face is on.
pub solid: Box<Solid>,
pub units: UnitLen,
#[serde(rename = "__meta")]
pub meta: Vec<Metadata>,
}
Expand Down Expand Up @@ -1005,6 +1015,7 @@ pub struct Sketch {
/// is sketched on face etc.
#[serde(skip)]
pub original_id: uuid::Uuid,
pub units: UnitLen,
/// Metadata.
#[serde(rename = "__meta")]
pub meta: Vec<Metadata>,
Expand Down Expand Up @@ -1128,6 +1139,7 @@ pub struct Solid {
/// Chamfers or fillets on this solid.
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub edge_cuts: Vec<EdgeCut>,
pub units: UnitLen,
/// Metadata.
#[serde(rename = "__meta")]
pub meta: Vec<Metadata>,
Expand Down
22 changes: 1 addition & 21 deletions src/wasm-lib/kcl/src/std/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -863,27 +863,7 @@ impl<'a> FromKclValue<'a> for crate::std::planes::StandardPlane {

impl<'a> FromKclValue<'a> for crate::execution::Plane {
fn from_kcl_val(arg: &'a KclValue) -> Option<Self> {
if let Some(plane) = arg.as_plane() {
return Some(plane.clone());
}

let obj = arg.as_object()?;
let_field_of!(obj, id);
let_field_of!(obj, value);
let_field_of!(obj, origin);
let_field_of!(obj, x_axis "xAxis");
let_field_of!(obj, y_axis "yAxis");
let_field_of!(obj, z_axis "zAxis");
let_field_of!(obj, meta "__meta");
Some(Self {
id,
value,
origin,
x_axis,
y_axis,
z_axis,
meta,
})
arg.as_plane().cloned()
}
}

Expand Down
1 change: 1 addition & 0 deletions src/wasm-lib/kcl/src/std/extrude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,7 @@ pub(crate) async fn do_post_extrude(
id: sketch.id,
value: new_value,
meta: sketch.meta.clone(),
units: sketch.units,
sketch,
height: length,
start_cap_id,
Expand Down
1 change: 1 addition & 0 deletions src/wasm-lib/kcl/src/std/helix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ async fn inner_helix(data: HelixData, exec_state: &mut ExecState, args: Args) ->
revolutions: data.revolutions,
angle_start: data.angle_start,
ccw: data.ccw,
units: exec_state.length_unit(),
meta: vec![args.source_range.into()],
});

Expand Down
7 changes: 7 additions & 0 deletions src/wasm-lib/kcl/src/std/sketch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1124,6 +1124,7 @@ async fn start_sketch_on_face(
x_axis: solid.sketch.on.x_axis(),
y_axis: solid.sketch.on.y_axis(),
z_axis: solid.sketch.on.z_axis(),
units: solid.units,
solid,
meta: vec![args.source_range.into()],
}))
Expand Down Expand Up @@ -1262,6 +1263,11 @@ pub(crate) async fn inner_start_profile_at(
_ => {}
}

let units = match &sketch_surface {
SketchSurface::Face(face) => face.units,
SketchSurface::Plane(_) => exec_state.length_unit(),
};

// Enter sketch mode on the surface.
// We call this here so you can reuse the sketch surface for multiple sketches.
let id = exec_state.next_uuid();
Expand Down Expand Up @@ -1311,6 +1317,7 @@ pub(crate) async fn inner_start_profile_at(
original_id: path_id,
on: sketch_surface.clone(),
paths: vec![],
units,
meta: vec![args.source_range.into()],
tags: if let Some(tag) = &tag {
let mut tag_identifier: TagIdentifier = tag.into();
Expand Down
9 changes: 9 additions & 0 deletions src/wasm-lib/kcl/tests/angled_line/program_memory.snap
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,9 @@ description: Program memory after executing angled_line.kcl
"y": 0.0,
"z": 1.0
},
"units": {
"type": "Mm"
},
"__meta": []
},
"start": {
Expand Down Expand Up @@ -340,6 +343,9 @@ description: Program memory after executing angled_line.kcl
]
}
},
"units": {
"type": "Mm"
},
"__meta": [
{
"sourceRange": [
Expand All @@ -353,6 +359,9 @@ description: Program memory after executing angled_line.kcl
"height": 4.0,
"startCapId": "[uuid]",
"endCapId": "[uuid]",
"units": {
"type": "Mm"
},
"__meta": [
{
"sourceRange": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,9 @@ description: Program memory after executing basic_fillet_cube_close_opposite.kcl
"y": 0.0,
"z": 1.0
},
"units": {
"type": "Mm"
},
"__meta": []
},
"start": {
Expand Down Expand Up @@ -416,6 +419,9 @@ description: Program memory after executing basic_fillet_cube_close_opposite.kcl
]
}
},
"units": {
"type": "Mm"
},
"__meta": [
{
"sourceRange": [
Expand Down Expand Up @@ -445,6 +451,9 @@ description: Program memory after executing basic_fillet_cube_close_opposite.kcl
"tag": null
}
],
"units": {
"type": "Mm"
},
"__meta": [
{
"sourceRange": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,9 @@ description: Program memory after executing basic_fillet_cube_end.kcl
"y": 0.0,
"z": 1.0
},
"units": {
"type": "Mm"
},
"__meta": []
},
"start": {
Expand Down Expand Up @@ -347,6 +350,9 @@ description: Program memory after executing basic_fillet_cube_end.kcl
]
}
},
"units": {
"type": "Mm"
},
"__meta": [
{
"sourceRange": [
Expand Down Expand Up @@ -376,6 +382,9 @@ description: Program memory after executing basic_fillet_cube_end.kcl
"tag": null
}
],
"units": {
"type": "Mm"
},
"__meta": [
{
"sourceRange": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,9 @@ description: Program memory after executing basic_fillet_cube_next_adjacent.kcl
"y": 0.0,
"z": 1.0
},
"units": {
"type": "Mm"
},
"__meta": []
},
"start": {
Expand Down Expand Up @@ -485,6 +488,9 @@ description: Program memory after executing basic_fillet_cube_next_adjacent.kcl
]
}
},
"units": {
"type": "Mm"
},
"__meta": [
{
"sourceRange": [
Expand All @@ -507,6 +513,9 @@ description: Program memory after executing basic_fillet_cube_next_adjacent.kcl
"tag": null
}
],
"units": {
"type": "Mm"
},
"__meta": [
{
"sourceRange": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,9 @@ description: Program memory after executing basic_fillet_cube_previous_adjacent.
"y": 0.0,
"z": 1.0
},
"units": {
"type": "Mm"
},
"__meta": []
},
"start": {
Expand Down Expand Up @@ -485,6 +488,9 @@ description: Program memory after executing basic_fillet_cube_previous_adjacent.
]
}
},
"units": {
"type": "Mm"
},
"__meta": [
{
"sourceRange": [
Expand All @@ -507,6 +513,9 @@ description: Program memory after executing basic_fillet_cube_previous_adjacent.
"tag": null
}
],
"units": {
"type": "Mm"
},
"__meta": [
{
"sourceRange": [
Expand Down
Loading

0 comments on commit 5600b7a

Please sign in to comment.