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

add unit functions #3604

Merged
merged 7 commits into from
Aug 21, 2024
Merged
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
4 changes: 2 additions & 2 deletions docs/kcl/assertEqual.md

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/kcl/circle.md

Large diffs are not rendered by default.

38 changes: 38 additions & 0 deletions docs/kcl/cm.md

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/kcl/extrude.md

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/kcl/fillet.md

Large diffs are not rendered by default.

38 changes: 38 additions & 0 deletions docs/kcl/ft.md

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/kcl/getNextAdjacentEdge.md

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/kcl/getPreviousAdjacentEdge.md

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/kcl/helix.md

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/kcl/hole.md

Large diffs are not rendered by default.

38 changes: 38 additions & 0 deletions docs/kcl/inch.md

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions docs/kcl/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,20 @@ layout: manual
* [`chamfer`](kcl/chamfer)
* [`circle`](kcl/circle)
* [`close`](kcl/close)
* [`cm`](kcl/cm)
* [`cos`](kcl/cos)
* [`e`](kcl/e)
* [`extrude`](kcl/extrude)
* [`fillet`](kcl/fillet)
* [`floor`](kcl/floor)
* [`ft`](kcl/ft)
* [`getNextAdjacentEdge`](kcl/getNextAdjacentEdge)
* [`getOppositeEdge`](kcl/getOppositeEdge)
* [`getPreviousAdjacentEdge`](kcl/getPreviousAdjacentEdge)
* [`helix`](kcl/helix)
* [`hole`](kcl/hole)
* [`import`](kcl/import)
* [`inch`](kcl/inch)
* [`int`](kcl/int)
* [`lastSegX`](kcl/lastSegX)
* [`lastSegY`](kcl/lastSegY)
Expand All @@ -55,8 +58,10 @@ layout: manual
* [`log`](kcl/log)
* [`log10`](kcl/log10)
* [`log2`](kcl/log2)
* [`m`](kcl/m)
* [`max`](kcl/max)
* [`min`](kcl/min)
* [`mm`](kcl/mm)
* [`patternCircular2d`](kcl/patternCircular2d)
* [`patternCircular3d`](kcl/patternCircular3d)
* [`patternLinear2d`](kcl/patternLinear2d)
Expand Down Expand Up @@ -89,3 +94,4 @@ layout: manual
* [`xLineTo`](kcl/xLineTo)
* [`yLine`](kcl/yLine)
* [`yLineTo`](kcl/yLineTo)
* [`yd`](kcl/yd)
2 changes: 1 addition & 1 deletion docs/kcl/int.md

Large diffs are not rendered by default.

38 changes: 38 additions & 0 deletions docs/kcl/m.md

Large diffs are not rendered by default.

38 changes: 38 additions & 0 deletions docs/kcl/mm.md

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/kcl/patternCircular3d.md

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/kcl/patternLinear2d.md

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/kcl/pi.md

Large diffs are not rendered by default.

16 changes: 8 additions & 8 deletions docs/kcl/revolve.md

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/kcl/segLen.md

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/kcl/startSketchOn.md

Large diffs are not rendered by default.

140 changes: 139 additions & 1 deletion docs/kcl/std.json
Original file line number Diff line number Diff line change
Expand Up @@ -55356,7 +55356,7 @@
"unpublished": false,
"deprecated": false,
"examples": [
"let n = 1.0285\nlet m = 1.0286\nassertEqual(n, m, 0.01, \"n is within the given tolerance for m\")"
"let n = 1.0285\nlet o = 1.0286\nassertEqual(n, o, 0.01, \"n is within the given tolerance for o\")"
]
},
{
Expand Down Expand Up @@ -82255,6 +82255,29 @@
"const exampleSketch = startSketchOn('-XZ')\n |> startProfileAt([0, 0], %)\n |> line([10, 0], %)\n |> line([0, 10], %)\n |> close(%)\n\nconst example = extrude(10, exampleSketch)"
]
},
{
"name": "cm",
"summary": "Centimeters conversion factor for current projects units.",
"description": "No matter what units the current project uses, this function will always return the conversion factor to centimeters.\nFor example, if the current project uses inches, this function will return `0.393701`. If the current project uses millimeters, this function will return `10`. If the current project uses centimeters, this function will return `1`.\n**Caution**: This function is only intended to be used when you absolutely MUST have different units in your code than the project settings. Otherwise, it is a bad pattern to use this function.\nWe merely provide these functions for convenience and readability, as `10 * cm()` is more readable that your intent is \"I want 10 centimeters\" than `10 * 10`, if the project settings are in millimeters.",
"tags": [
"units"
],
"args": [],
"returnValue": {
"name": "",
"type": "number",
"schema": {
"type": "number",
"format": "double"
},
"required": true
},
"unpublished": false,
"deprecated": false,
"examples": [
"const totalWidth = 10 * cm()"
]
},
{
"name": "cos",
"summary": "Compute the cosine of a number (in radians).",
Expand Down Expand Up @@ -98138,6 +98161,29 @@
"const sketch001 = startSketchOn('XZ')\n |> startProfileAt([0, 0], %)\n |> lineTo([12, 10], %)\n |> line([floor(7.02986), 0], %)\n |> yLineTo(0, %)\n |> close(%)\n\nconst extrude001 = extrude(5, sketch001)"
]
},
{
"name": "ft",
"summary": "Feet conversion factor for current projects units.",
"description": "No matter what units the current project uses, this function will always return the conversion factor to feet.\nFor example, if the current project uses inches, this function will return `12`. If the current project uses millimeters, this function will return `304.8`. If the current project uses feet, this function will return `1`.\n**Caution**: This function is only intended to be used when you absolutely MUST have different units in your code than the project settings. Otherwise, it is a bad pattern to use this function.\nWe merely provide these functions for convenience and readability, as `10 * ft()` is more readable that your intent is \"I want 10 feet\" than `10 * 304.8`, if the project settings are in millimeters.",
"tags": [
"units"
],
"args": [],
"returnValue": {
"name": "",
"type": "number",
"schema": {
"type": "number",
"format": "double"
},
"required": true
},
"unpublished": false,
"deprecated": false,
"examples": [
"const totalWidth = 10 * ft()"
]
},
{
"name": "getNextAdjacentEdge",
"summary": "Get the next adjacent edge to the edge given.",
Expand Down Expand Up @@ -117499,6 +117545,29 @@
"const model = import(\"tests/inputs/cube.step\")"
]
},
{
"name": "inch",
"summary": "Inches conversion factor for current projects units.",
"description": "No matter what units the current project uses, this function will always return the conversion factor to inches.\nFor example, if the current project uses inches, this function will return `1`. If the current project uses millimeters, this function will return `25.4`.\n**Caution**: This function is only intended to be used when you absolutely MUST have different units in your code than the project settings. Otherwise, it is a bad pattern to use this function.\nWe merely provide these functions for convenience and readability, as `10 * inch()` is more readable that your intent is \"I want 10 inches\" than `10 * 25.4`, if the project settings are in millimeters.",
"tags": [
"units"
],
"args": [],
"returnValue": {
"name": "",
"type": "number",
"schema": {
"type": "number",
"format": "double"
},
"required": true
},
"unpublished": false,
"deprecated": false,
"examples": [
"const totalWidth = 10 * inch()"
]
},
{
"name": "int",
"summary": "Convert a number to an integer.",
Expand Down Expand Up @@ -137886,6 +137955,29 @@
"const exampleSketch = startSketchOn(\"XZ\")\n |> startProfileAt([0, 0], %)\n |> line([log2(100), 0], %)\n |> line([5, 8], %)\n |> line([-10, 0], %)\n |> close(%)\n\nconst example = extrude(5, exampleSketch)"
]
},
{
"name": "m",
"summary": "Meters conversion factor for current projects units.",
"description": "No matter what units the current project uses, this function will always return the conversion factor to meters.\nFor example, if the current project uses inches, this function will return `39.3701`. If the current project uses millimeters, this function will return `1000`. If the current project uses meters, this function will return `1`.\n**Caution**: This function is only intended to be used when you absolutely MUST have different units in your code than the project settings. Otherwise, it is a bad pattern to use this function.\nWe merely provide these functions for convenience and readability, as `10 * m()` is more readable that your intent is \"I want 10 meters\" than `10 * 1000`, if the project settings are in millimeters.",
"tags": [
"units"
],
"args": [],
"returnValue": {
"name": "",
"type": "number",
"schema": {
"type": "number",
"format": "double"
},
"required": true
},
"unpublished": false,
"deprecated": false,
"examples": [
"const totalWidth = 10 * m()"
]
},
{
"name": "max",
"summary": "Compute the maximum of the given arguments.",
Expand Down Expand Up @@ -137958,6 +138050,29 @@
"const exampleSketch = startSketchOn(\"XZ\")\n |> startProfileAt([0, 0], %)\n |> angledLine({\n angle: 70,\n length: min(15, 31, 4, 13, 22)\n }, %)\n |> line([20, 0], %)\n |> close(%)\n\nconst example = extrude(5, exampleSketch)"
]
},
{
"name": "mm",
"summary": "Millimeters conversion factor for current projects units.",
"description": "No matter what units the current project uses, this function will always return the conversion factor to millimeters.\nFor example, if the current project uses inches, this function will return `(1/25.4)`. If the current project uses millimeters, this function will return `1`.\n**Caution**: This function is only intended to be used when you absolutely MUST have different units in your code than the project settings. Otherwise, it is a bad pattern to use this function.\nWe merely provide these functions for convenience and readability, as `10 * mm()` is more readable that your intent is \"I want 10 millimeters\" than `10 * (1/25.4)`, if the project settings are in inches.",
"tags": [
"units"
],
"args": [],
"returnValue": {
"name": "",
"type": "number",
"schema": {
"type": "number",
"format": "double"
},
"required": true
},
"unpublished": false,
"deprecated": false,
"examples": [
"const totalWidth = 10 * mm()"
]
},
{
"name": "patternCircular2d",
"summary": "Repeat a 2-dimensional sketch some number of times along a partial or",
Expand Down Expand Up @@ -252170,5 +252285,28 @@
"examples": [
"const exampleSketch = startSketchOn(\"XZ\")\n |> startProfileAt([0, 0], %)\n |> angledLine({ angle: 50, length: 45 }, %)\n |> yLineTo(0, %)\n |> close(%)\n\nconst example = extrude(5, exampleSketch)"
]
},
{
"name": "yd",
"summary": "Yards conversion factor for current projects units.",
"description": "No matter what units the current project uses, this function will always return the conversion factor to yards.\nFor example, if the current project uses inches, this function will return `36`. If the current project uses millimeters, this function will return `914.4`. If the current project uses yards, this function will return `1`.\n**Caution**: This function is only intended to be used when you absolutely MUST have different units in your code than the project settings. Otherwise, it is a bad pattern to use this function.\nWe merely provide these functions for convenience and readability, as `10 * yd()` is more readable that your intent is \"I want 10 yards\" than `10 * 914.4`, if the project settings are in millimeters.",
"tags": [
"units"
],
"args": [],
"returnValue": {
"name": "",
"type": "number",
"schema": {
"type": "number",
"format": "double"
},
"required": true
},
"unpublished": false,
"deprecated": false,
"examples": [
"const totalWidth = 10 * yd()"
]
}
]
2 changes: 1 addition & 1 deletion docs/kcl/tangentialArc.md

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/kcl/tangentialArcTo.md

Large diffs are not rendered by default.

38 changes: 38 additions & 0 deletions docs/kcl/yd.md

Large diffs are not rendered by default.

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 10 additions & 0 deletions src/wasm-lib/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/wasm-lib/kcl/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ gltf-json = "1.4.1"
image = { version = "0.25.1", default-features = false, features = ["png"] }
kittycad = { workspace = true, features = ["clap"] }
lazy_static = "1.5.0"
measurements = "0.11.0"
mime_guess = "2.0.5"
parse-display = "0.9.1"
pyo3 = { version = "0.22.2", optional = true }
Expand Down
33 changes: 0 additions & 33 deletions src/wasm-lib/kcl/src/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2114,39 +2114,6 @@ mod tests {
.unwrap()
}

#[test]
fn how_does_sketchgroup_serialize() {
let sg = SketchGroup {
id: uuid::Uuid::new_v4(),
value: vec![],
on: SketchSurface::Plane(Box::new(Plane {
id: uuid::Uuid::new_v4(),
value: PlaneType::XY,
origin: Point3d::default(),
x_axis: Point3d::default(),
y_axis: Point3d::default(),
z_axis: Point3d::default(),
meta: Vec::new(),
})),
start: BasePath {
from: [0.0, 0.0],
to: [0.0, 0.0],
tag: None,
geo_meta: GeoMeta {
id: uuid::Uuid::new_v4(),
metadata: Metadata {
source_range: SourceRange([0, 0]),
},
},
},
tags: HashMap::new(),
original_id: uuid::Uuid::new_v4(),
meta: Vec::new(),
};
let jstr = serde_json::to_string_pretty(&sg).unwrap();
println!("{jstr}");
}

#[tokio::test(flavor = "multi_thread")]
async fn test_execute_assign_two_variables() {
let ast = r#"const myVar = 5
Expand Down
22 changes: 21 additions & 1 deletion src/wasm-lib/kcl/src/std/args.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
use std::any::type_name;

use anyhow::Result;
use kittycad::types::OkWebSocketResponseData;
use serde::de::DeserializeOwned;

use super::{shapes::SketchSurfaceOrGroup, sketch::FaceTag, FnAsArg};
use crate::{
ast::types::{parse_json_number_as_f64, TagDeclarator},
errors::{KclError, KclErrorDetails},
executor::{
DynamicState, ExecutorContext, ExtrudeGroup, ExtrudeGroupSet, ExtrudeSurface, KclValue, Metadata,
ProgramMemory, SketchGroup, SketchGroupSet, SketchSurface, SourceRange, TagIdentifier,
},
std::{shapes::SketchSurfaceOrGroup, sketch::FaceTag, FnAsArg},
};

#[derive(Debug, Clone)]
Expand Down Expand Up @@ -39,6 +40,25 @@ impl Args {
}
}

#[cfg(test)]
pub(crate) async fn new_test_args() -> Result<Self> {
use std::sync::Arc;

Ok(Self {
args: Vec::new(),
source_range: SourceRange::default(),
ctx: ExecutorContext {
engine: Arc::new(Box::new(crate::engine::conn_mock::EngineConnection::new().await?)),
fs: Arc::new(crate::fs::FileManager::new()),
stdlib: Arc::new(crate::std::StdLib::new()),
settings: Default::default(),
is_mock: true,
},
current_program_memory: ProgramMemory::default(),
dynamic_state: DynamicState::default(),
})
}

// Add a modeling command to the batch but don't fire it right away.
pub(crate) async fn batch_modeling_cmd(
&self,
Expand Down
4 changes: 2 additions & 2 deletions src/wasm-lib/kcl/src/std/assert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ pub async fn assert_gt(args: Args) -> Result<KclValue, KclError> {
///
/// ```no_run
/// let n = 1.0285
/// let m = 1.0286
/// assertEqual(n, m, 0.01, "n is within the given tolerance for m")
/// let o = 1.0286
/// assertEqual(n, o, 0.01, "n is within the given tolerance for o")
/// ```
#[stdlib {
name = "assertEqual",
Expand Down
7 changes: 7 additions & 0 deletions src/wasm-lib/kcl/src/std/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ pub mod shapes;
pub mod shell;
pub mod sketch;
pub mod types;
pub mod units;
pub mod utils;

use std::collections::HashMap;
Expand Down Expand Up @@ -118,6 +119,12 @@ lazy_static! {
Box::new(crate::std::math::Ln),
Box::new(crate::std::math::ToDegrees),
Box::new(crate::std::math::ToRadians),
Box::new(crate::std::units::Mm),
Box::new(crate::std::units::Inch),
Box::new(crate::std::units::Ft),
Box::new(crate::std::units::M),
Box::new(crate::std::units::Cm),
Box::new(crate::std::units::Yd),
Box::new(crate::std::polar::Polar),
Box::new(crate::std::assert::Assert),
Box::new(crate::std::assert::AssertEqual),
Expand Down
Loading