DRY the code (#921)
This commit is contained in:
@ -3812,7 +3812,7 @@
|
|||||||
"args": [
|
"args": [
|
||||||
{
|
{
|
||||||
"name": "data",
|
"name": "data",
|
||||||
"type": "AngeledLineThatIntersectsData",
|
"type": "AngledLineThatIntersectsData",
|
||||||
"schema": {
|
"schema": {
|
||||||
"description": "Data for drawing an angled line that intersects with a given line.",
|
"description": "Data for drawing an angled line that intersects with a given line.",
|
||||||
"type": "object",
|
"type": "object",
|
||||||
|
@ -763,12 +763,12 @@ Draw an angled line that intersects with a given line.
|
|||||||
|
|
||||||
|
|
||||||
```
|
```
|
||||||
angledLineThatIntersects(data: AngeledLineThatIntersectsData, sketch_group: SketchGroup) -> SketchGroup
|
angledLineThatIntersects(data: AngledLineThatIntersectsData, sketch_group: SketchGroup) -> SketchGroup
|
||||||
```
|
```
|
||||||
|
|
||||||
#### Arguments
|
#### Arguments
|
||||||
|
|
||||||
* `data`: `AngeledLineThatIntersectsData` - Data for drawing an angled line that intersects with a given line.
|
* `data`: `AngledLineThatIntersectsData` - Data for drawing an angled line that intersects with a given line.
|
||||||
```
|
```
|
||||||
{
|
{
|
||||||
// The angle of the line.
|
// The angle of the line.
|
||||||
|
4
src/wasm-lib/Cargo.lock
generated
4
src/wasm-lib/Cargo.lock
generated
@ -1432,9 +1432,9 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "kittycad"
|
name = "kittycad"
|
||||||
version = "0.2.38"
|
version = "0.2.39"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "633a728fb7209b398b7fa5b67460cb7f3cdb268c6b2a9e81967dda464cfbb5c1"
|
checksum = "881c0e85e8051b0b68ea05fbb93780cf76da57921ba017963f01e9613f9c5bb7"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"anyhow",
|
"anyhow",
|
||||||
"async-trait",
|
"async-trait",
|
||||||
|
@ -334,6 +334,16 @@ pub enum AngledLineData {
|
|||||||
AngleAndLength([f64; 2]),
|
AngleAndLength([f64; 2]),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl AngledLineData {
|
||||||
|
pub fn into_inner_line(self, to: [f64; 2]) -> LineData {
|
||||||
|
if let AngledLineData::AngleWithTag { tag, .. } = self {
|
||||||
|
LineData::PointWithTag { to, tag }
|
||||||
|
} else {
|
||||||
|
LineData::Point(to)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Draw an angled line.
|
/// Draw an angled line.
|
||||||
pub async fn angled_line(args: Args) -> Result<MemoryItem, KclError> {
|
pub async fn angled_line(args: Args) -> Result<MemoryItem, KclError> {
|
||||||
let (data, sketch_group): (AngledLineData, Box<SketchGroup>) = args.get_data_and_sketch_group()?;
|
let (data, sketch_group): (AngledLineData, Box<SketchGroup>) = args.get_data_and_sketch_group()?;
|
||||||
@ -429,16 +439,7 @@ async fn inner_angled_line_of_x_length(
|
|||||||
|
|
||||||
let to = get_y_component(Angle::from_degrees(angle), length);
|
let to = get_y_component(Angle::from_degrees(angle), length);
|
||||||
|
|
||||||
let new_sketch_group = inner_line(
|
let new_sketch_group = inner_line(data.into_inner_line(to.into()), sketch_group, args).await?;
|
||||||
if let AngledLineData::AngleWithTag { tag, .. } = data {
|
|
||||||
LineData::PointWithTag { to: to.into(), tag }
|
|
||||||
} else {
|
|
||||||
LineData::Point(to.into())
|
|
||||||
},
|
|
||||||
sketch_group,
|
|
||||||
args,
|
|
||||||
)
|
|
||||||
.await?;
|
|
||||||
|
|
||||||
Ok(new_sketch_group)
|
Ok(new_sketch_group)
|
||||||
}
|
}
|
||||||
@ -461,6 +462,16 @@ pub enum AngledLineToData {
|
|||||||
AngleAndPoint([f64; 2]),
|
AngleAndPoint([f64; 2]),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl AngledLineToData {
|
||||||
|
pub fn into_inner_line(self, x_to: f64, y_to: f64) -> LineToData {
|
||||||
|
if let AngledLineToData::AngleWithTag { tag, .. } = self {
|
||||||
|
LineToData::PointWithTag { to: [x_to, y_to], tag }
|
||||||
|
} else {
|
||||||
|
LineToData::Point([x_to, y_to])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Draw an angled line to a given x coordinate.
|
/// Draw an angled line to a given x coordinate.
|
||||||
pub async fn angled_line_to_x(args: Args) -> Result<MemoryItem, KclError> {
|
pub async fn angled_line_to_x(args: Args) -> Result<MemoryItem, KclError> {
|
||||||
let (data, sketch_group): (AngledLineToData, Box<SketchGroup>) = args.get_data_and_sketch_group()?;
|
let (data, sketch_group): (AngledLineToData, Box<SketchGroup>) = args.get_data_and_sketch_group()?;
|
||||||
@ -488,16 +499,7 @@ async fn inner_angled_line_to_x(
|
|||||||
let y_component = x_component * f64::tan(angle.to_radians());
|
let y_component = x_component * f64::tan(angle.to_radians());
|
||||||
let y_to = from.y + y_component;
|
let y_to = from.y + y_component;
|
||||||
|
|
||||||
let new_sketch_group = inner_line_to(
|
let new_sketch_group = inner_line_to(data.into_inner_line(x_to, y_to), sketch_group, args).await?;
|
||||||
if let AngledLineToData::AngleWithTag { tag, .. } = data {
|
|
||||||
LineToData::PointWithTag { to: [x_to, y_to], tag }
|
|
||||||
} else {
|
|
||||||
LineToData::Point([x_to, y_to])
|
|
||||||
},
|
|
||||||
sketch_group,
|
|
||||||
args,
|
|
||||||
)
|
|
||||||
.await?;
|
|
||||||
Ok(new_sketch_group)
|
Ok(new_sketch_group)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -526,16 +528,7 @@ async fn inner_angled_line_of_y_length(
|
|||||||
|
|
||||||
let to = get_x_component(Angle::from_degrees(angle), length);
|
let to = get_x_component(Angle::from_degrees(angle), length);
|
||||||
|
|
||||||
let new_sketch_group = inner_line(
|
let new_sketch_group = inner_line(data.into_inner_line(to.into()), sketch_group, args).await?;
|
||||||
if let AngledLineData::AngleWithTag { tag, .. } = data {
|
|
||||||
LineData::PointWithTag { to: to.into(), tag }
|
|
||||||
} else {
|
|
||||||
LineData::Point(to.into())
|
|
||||||
},
|
|
||||||
sketch_group,
|
|
||||||
args,
|
|
||||||
)
|
|
||||||
.await?;
|
|
||||||
|
|
||||||
Ok(new_sketch_group)
|
Ok(new_sketch_group)
|
||||||
}
|
}
|
||||||
@ -567,16 +560,7 @@ async fn inner_angled_line_to_y(
|
|||||||
let x_component = y_component / f64::tan(angle.to_radians());
|
let x_component = y_component / f64::tan(angle.to_radians());
|
||||||
let x_to = from.x + x_component;
|
let x_to = from.x + x_component;
|
||||||
|
|
||||||
let new_sketch_group = inner_line_to(
|
let new_sketch_group = inner_line_to(data.into_inner_line(x_to, y_to), sketch_group, args).await?;
|
||||||
if let AngledLineToData::AngleWithTag { tag, .. } = data {
|
|
||||||
LineToData::PointWithTag { to: [x_to, y_to], tag }
|
|
||||||
} else {
|
|
||||||
LineToData::Point([x_to, y_to])
|
|
||||||
},
|
|
||||||
sketch_group,
|
|
||||||
args,
|
|
||||||
)
|
|
||||||
.await?;
|
|
||||||
Ok(new_sketch_group)
|
Ok(new_sketch_group)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -585,7 +569,7 @@ async fn inner_angled_line_to_y(
|
|||||||
#[ts(export)]
|
#[ts(export)]
|
||||||
#[serde(rename_all = "camelCase")]
|
#[serde(rename_all = "camelCase")]
|
||||||
// TODO: make sure the docs on the args below are correct.
|
// TODO: make sure the docs on the args below are correct.
|
||||||
pub struct AngeledLineThatIntersectsData {
|
pub struct AngledLineThatIntersectsData {
|
||||||
/// The angle of the line.
|
/// The angle of the line.
|
||||||
pub angle: f64,
|
pub angle: f64,
|
||||||
/// The tag of the line to intersect with.
|
/// The tag of the line to intersect with.
|
||||||
@ -598,7 +582,7 @@ pub struct AngeledLineThatIntersectsData {
|
|||||||
|
|
||||||
/// Draw an angled line that intersects with a given line.
|
/// Draw an angled line that intersects with a given line.
|
||||||
pub async fn angled_line_that_intersects(args: Args) -> Result<MemoryItem, KclError> {
|
pub async fn angled_line_that_intersects(args: Args) -> Result<MemoryItem, KclError> {
|
||||||
let (data, sketch_group): (AngeledLineThatIntersectsData, Box<SketchGroup>) = args.get_data_and_sketch_group()?;
|
let (data, sketch_group): (AngledLineThatIntersectsData, Box<SketchGroup>) = args.get_data_and_sketch_group()?;
|
||||||
let new_sketch_group = inner_angled_line_that_intersects(data, sketch_group, args).await?;
|
let new_sketch_group = inner_angled_line_that_intersects(data, sketch_group, args).await?;
|
||||||
Ok(MemoryItem::SketchGroup(new_sketch_group))
|
Ok(MemoryItem::SketchGroup(new_sketch_group))
|
||||||
}
|
}
|
||||||
@ -608,7 +592,7 @@ pub async fn angled_line_that_intersects(args: Args) -> Result<MemoryItem, KclEr
|
|||||||
name = "angledLineThatIntersects",
|
name = "angledLineThatIntersects",
|
||||||
}]
|
}]
|
||||||
async fn inner_angled_line_that_intersects(
|
async fn inner_angled_line_that_intersects(
|
||||||
data: AngeledLineThatIntersectsData,
|
data: AngledLineThatIntersectsData,
|
||||||
sketch_group: Box<SketchGroup>,
|
sketch_group: Box<SketchGroup>,
|
||||||
args: Args,
|
args: Args,
|
||||||
) -> Result<Box<SketchGroup>, KclError> {
|
) -> Result<Box<SketchGroup>, KclError> {
|
||||||
@ -1150,40 +1134,12 @@ async fn inner_tangential_arc(
|
|||||||
to.into()
|
to.into()
|
||||||
}
|
}
|
||||||
TangentialArcData::PointWithTag { to, .. } => {
|
TangentialArcData::PointWithTag { to, .. } => {
|
||||||
args.send_modeling_cmd(
|
args.send_modeling_cmd(id, tan_arc_to(&sketch_group, to)).await?;
|
||||||
id,
|
|
||||||
ModelingCmd::ExtendPath {
|
|
||||||
path: sketch_group.id,
|
|
||||||
segment: kittycad::types::PathSegment::TangentialArcTo {
|
|
||||||
angle_snap_increment: None,
|
|
||||||
to: kittycad::types::Point3D {
|
|
||||||
x: to[0],
|
|
||||||
y: to[1],
|
|
||||||
z: 0.0,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
)
|
|
||||||
.await?;
|
|
||||||
|
|
||||||
*to
|
*to
|
||||||
}
|
}
|
||||||
TangentialArcData::Point(to) => {
|
TangentialArcData::Point(to) => {
|
||||||
args.send_modeling_cmd(
|
args.send_modeling_cmd(id, tan_arc_to(&sketch_group, to)).await?;
|
||||||
id,
|
|
||||||
ModelingCmd::ExtendPath {
|
|
||||||
path: sketch_group.id,
|
|
||||||
segment: kittycad::types::PathSegment::TangentialArcTo {
|
|
||||||
angle_snap_increment: None,
|
|
||||||
to: kittycad::types::Point3D {
|
|
||||||
x: to[0],
|
|
||||||
y: to[1],
|
|
||||||
z: 0.0,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
)
|
|
||||||
.await?;
|
|
||||||
|
|
||||||
*to
|
*to
|
||||||
}
|
}
|
||||||
@ -1209,6 +1165,20 @@ async fn inner_tangential_arc(
|
|||||||
Ok(new_sketch_group)
|
Ok(new_sketch_group)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn tan_arc_to(sketch_group: &SketchGroup, to: &[f64; 2]) -> ModelingCmd {
|
||||||
|
ModelingCmd::ExtendPath {
|
||||||
|
path: sketch_group.id,
|
||||||
|
segment: kittycad::types::PathSegment::TangentialArcTo {
|
||||||
|
angle_snap_increment: None,
|
||||||
|
to: Point3D {
|
||||||
|
x: to[0],
|
||||||
|
y: to[1],
|
||||||
|
z: 0.0,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Data to draw a tangential arc to a specific point.
|
/// Data to draw a tangential arc to a specific point.
|
||||||
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, JsonSchema, ts_rs::TS)]
|
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, JsonSchema, ts_rs::TS)]
|
||||||
#[ts(export)]
|
#[ts(export)]
|
||||||
@ -1249,24 +1219,8 @@ async fn inner_tangential_arc_to(
|
|||||||
};
|
};
|
||||||
|
|
||||||
let delta = [to[0] - from.x, to[1] - from.y];
|
let delta = [to[0] - from.x, to[1] - from.y];
|
||||||
|
|
||||||
let id = uuid::Uuid::new_v4();
|
let id = uuid::Uuid::new_v4();
|
||||||
|
args.send_modeling_cmd(id, tan_arc_to(&sketch_group, &delta)).await?;
|
||||||
args.send_modeling_cmd(
|
|
||||||
id,
|
|
||||||
ModelingCmd::ExtendPath {
|
|
||||||
path: sketch_group.id,
|
|
||||||
segment: kittycad::types::PathSegment::TangentialArcTo {
|
|
||||||
angle_snap_increment: None,
|
|
||||||
to: kittycad::types::Point3D {
|
|
||||||
x: delta[0],
|
|
||||||
y: delta[1],
|
|
||||||
z: 0.0,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
)
|
|
||||||
.await?;
|
|
||||||
|
|
||||||
let current_path = Path::ToPoint {
|
let current_path = Path::ToPoint {
|
||||||
base: BasePath {
|
base: BasePath {
|
||||||
|
Reference in New Issue
Block a user