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 15, 2025
1 parent bd55850 commit 6c8c585
Show file tree
Hide file tree
Showing 41 changed files with 7,477 additions and 3,641 deletions.
19 changes: 18 additions & 1 deletion src/wasm-lib/kcl/src/execution/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type Point2D = kcmc::shared::Point2d<f64>;
type Point3D = kcmc::shared::Point3d<f64>;

pub use function_param::FunctionParam;
pub use kcl_value::{KclObjectFields, KclValue};
pub use kcl_value::{KclObjectFields, KclValue, UnitLen};
use uuid::Uuid;

mod annotations;
Expand Down Expand Up @@ -158,6 +158,11 @@ impl ExecState {
}
}

pub fn length_unit(&self) -> UnitLen {
// TODO
UnitLen::Mm
}

pub fn memory(&self) -> &ProgramMemory {
&self.mod_local.memory
}
Expand Down Expand Up @@ -736,6 +741,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 @@ -757,6 +763,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 @@ -772,6 +779,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 @@ -781,6 +789,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 @@ -790,6 +799,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 @@ -799,6 +809,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 @@ -808,6 +819,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 @@ -817,6 +829,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 @@ -831,6 +844,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 @@ -877,6 +891,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 @@ -995,6 +1010,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 @@ -1118,6 +1134,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 @@ -878,27 +878,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 @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,9 @@ description: Program memory after executing basic_fillet_cube_start.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_start.kcl
]
}
},
"units": {
"type": "Mm"
},
"__meta": [
{
"sourceRange": [
Expand Down Expand Up @@ -376,6 +382,9 @@ description: Program memory after executing basic_fillet_cube_start.kcl
"tag": null
}
],
"units": {
"type": "Mm"
},
"__meta": [
{
"sourceRange": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,9 @@ description: Program memory after executing big_number_angle_to_match_length_x.k
"y": 0.0,
"z": 1.0
},
"units": {
"type": "Mm"
},
"__meta": []
},
"start": {
Expand Down Expand Up @@ -247,6 +250,9 @@ description: Program memory after executing big_number_angle_to_match_length_x.k
]
}
},
"units": {
"type": "Mm"
},
"__meta": [
{
"sourceRange": [
Expand All @@ -260,6 +266,9 @@ description: Program memory after executing big_number_angle_to_match_length_x.k
"height": 10.0,
"startCapId": "[uuid]",
"endCapId": "[uuid]",
"units": {
"type": "Mm"
},
"__meta": [
{
"sourceRange": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,9 @@ description: Program memory after executing big_number_angle_to_match_length_y.k
"y": 0.0,
"z": 1.0
},
"units": {
"type": "Mm"
},
"__meta": []
},
"start": {
Expand Down Expand Up @@ -247,6 +250,9 @@ description: Program memory after executing big_number_angle_to_match_length_y.k
]
}
},
"units": {
"type": "Mm"
},
"__meta": [
{
"sourceRange": [
Expand All @@ -260,6 +266,9 @@ description: Program memory after executing big_number_angle_to_match_length_y.k
"height": 10.0,
"startCapId": "[uuid]",
"endCapId": "[uuid]",
"units": {
"type": "Mm"
},
"__meta": [
{
"sourceRange": [
Expand Down
Loading

0 comments on commit 6c8c585

Please sign in to comment.