fix errors for bad angles x/y constrained (#3152)

* fix errors for bad angles x/y constrained

Signed-off-by: Jess Frazelle <github@jessfraz.com>

* updates

Signed-off-by: Jess Frazelle <github@jessfraz.com>

---------

Signed-off-by: Jess Frazelle <github@jessfraz.com>
This commit is contained in:
Jess Frazelle
2024-07-28 20:36:18 -07:00
committed by GitHub
parent 9fd4fd0dd8
commit 2375f900b9
2 changed files with 242 additions and 0 deletions

View File

@ -545,6 +545,20 @@ async fn inner_angled_line_of_x_length(
AngledLineData::AngleAndLengthPair(pair) => (pair[0], pair[1]), AngledLineData::AngleAndLengthPair(pair) => (pair[0], pair[1]),
}; };
if angle.abs() == 270.0 {
return Err(KclError::Type(KclErrorDetails {
message: "Cannot have an x constrained angle of 270 degrees".to_string(),
source_ranges: vec![args.source_range],
}));
}
if angle.abs() == 90.0 {
return Err(KclError::Type(KclErrorDetails {
message: "Cannot have an x constrained angle of 90 degrees".to_string(),
source_ranges: vec![args.source_range],
}));
}
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(to.into(), sketch_group, tag, args).await?; let new_sketch_group = inner_line(to.into(), sketch_group, tag, args).await?;
@ -596,6 +610,20 @@ async fn inner_angled_line_to_x(
let from = sketch_group.current_pen_position()?; let from = sketch_group.current_pen_position()?;
let AngledLineToData { angle, to: x_to } = data; let AngledLineToData { angle, to: x_to } = data;
if angle.abs() == 270.0 {
return Err(KclError::Type(KclErrorDetails {
message: "Cannot have an x constrained angle of 270 degrees".to_string(),
source_ranges: vec![args.source_range],
}));
}
if angle.abs() == 90.0 {
return Err(KclError::Type(KclErrorDetails {
message: "Cannot have an x constrained angle of 90 degrees".to_string(),
source_ranges: vec![args.source_range],
}));
}
let x_component = x_to - from.x; let x_component = x_to - from.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;
@ -642,6 +670,20 @@ async fn inner_angled_line_of_y_length(
AngledLineData::AngleAndLengthPair(pair) => (pair[0], pair[1]), AngledLineData::AngleAndLengthPair(pair) => (pair[0], pair[1]),
}; };
if angle.abs() == 0.0 {
return Err(KclError::Type(KclErrorDetails {
message: "Cannot have a y constrained angle of 0 degrees".to_string(),
source_ranges: vec![args.source_range],
}));
}
if angle.abs() == 180.0 {
return Err(KclError::Type(KclErrorDetails {
message: "Cannot have a y constrained angle of 180 degrees".to_string(),
source_ranges: vec![args.source_range],
}));
}
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(to.into(), sketch_group, tag, args).await?; let new_sketch_group = inner_line(to.into(), sketch_group, tag, args).await?;
@ -682,6 +724,20 @@ async fn inner_angled_line_to_y(
let from = sketch_group.current_pen_position()?; let from = sketch_group.current_pen_position()?;
let AngledLineToData { angle, to: y_to } = data; let AngledLineToData { angle, to: y_to } = data;
if angle.abs() == 0.0 {
return Err(KclError::Type(KclErrorDetails {
message: "Cannot have a y constrained angle of 0 degrees".to_string(),
source_ranges: vec![args.source_range],
}));
}
if angle.abs() == 180.0 {
return Err(KclError::Type(KclErrorDetails {
message: "Cannot have a y constrained angle of 180 degrees".to_string(),
source_ranges: vec![args.source_range],
}));
}
let y_component = y_to - from.y; let y_component = y_to - from.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;

View File

@ -2627,3 +2627,189 @@ async fn serial_test_arc_error_same_start_end() {
r#"type: KclErrorDetails { source_ranges: [SourceRange([57, 140])], message: "Arc start and end angles must be different" }"# r#"type: KclErrorDetails { source_ranges: [SourceRange([57, 140])], message: "Arc start and end angles must be different" }"#
); );
} }
#[tokio::test(flavor = "multi_thread")]
async fn serial_test_angled_line_to_x_90() {
let code = r#"const exampleSketch = startSketchOn('XZ')
|> startProfileAt([0, 0], %)
|> angledLineToX({ angle: 90, to: 10 }, %)
|> line([0, 10], %)
|> line([-10, 0], %)
|> close(%)
const example = extrude(10, exampleSketch)
"#;
let result = execute_and_snapshot(code, UnitLength::Mm).await;
assert!(result.is_err());
assert_eq!(
result.err().unwrap().to_string(),
r#"type: KclErrorDetails { source_ranges: [SourceRange([78, 117])], message: "Cannot have an x constrained angle of 90 degrees" }"#
);
}
#[tokio::test(flavor = "multi_thread")]
async fn serial_test_angled_line_to_x_270() {
let code = r#"const exampleSketch = startSketchOn('XZ')
|> startProfileAt([0, 0], %)
|> angledLineToX({ angle: 270, to: 10 }, %)
|> line([0, 10], %)
|> line([-10, 0], %)
|> close(%)
const example = extrude(10, exampleSketch)
"#;
let result = execute_and_snapshot(code, UnitLength::Mm).await;
assert!(result.is_err());
assert_eq!(
result.err().unwrap().to_string(),
r#"type: KclErrorDetails { source_ranges: [SourceRange([78, 118])], message: "Cannot have an x constrained angle of 270 degrees" }"#
);
}
#[tokio::test(flavor = "multi_thread")]
async fn serial_test_angled_line_to_y_0() {
let code = r#"const exampleSketch = startSketchOn('XZ')
|> startProfileAt([0, 0], %)
|> angledLineToY({ angle: 0, to: 20 }, %)
|> line([-20, 0], %)
|> angledLineToY({ angle: 70, to: 10 }, %)
|> close(%)
const example = extrude(10, exampleSketch)
"#;
let result = execute_and_snapshot(code, UnitLength::Mm).await;
assert!(result.is_err());
assert_eq!(
result.err().unwrap().to_string(),
r#"type: KclErrorDetails { source_ranges: [SourceRange([78, 116])], message: "Cannot have a y constrained angle of 0 degrees" }"#
);
}
#[tokio::test(flavor = "multi_thread")]
async fn serial_test_angled_line_to_y_180() {
let code = r#"const exampleSketch = startSketchOn('XZ')
|> startProfileAt([0, 0], %)
|> angledLineToY({ angle: 180, to: 20 }, %)
|> line([-20, 0], %)
|> angledLineToY({ angle: 70, to: 10 }, %)
|> close(%)
const example = extrude(10, exampleSketch)
"#;
let result = execute_and_snapshot(code, UnitLength::Mm).await;
assert!(result.is_err());
assert_eq!(
result.err().unwrap().to_string(),
r#"type: KclErrorDetails { source_ranges: [SourceRange([78, 118])], message: "Cannot have a y constrained angle of 180 degrees" }"#
);
}
#[tokio::test(flavor = "multi_thread")]
async fn serial_test_angled_line_of_x_length_90() {
let code = r#"const sketch001 = startSketchOn('XZ')
|> startProfileAt([0, 0], %)
|> angledLineOfXLength({ angle: 90, length: 10 }, %, $edge1)
|> angledLineOfXLength({ angle: -15, length: 20 }, %, $edge2)
|> line([0, -5], %)
|> close(%, $edge3)
const extrusion = extrude(10, sketch001)
"#;
let result = execute_and_snapshot(code, UnitLength::Mm).await;
assert!(result.is_err());
assert_eq!(
result.err().unwrap().to_string(),
r#"type: KclErrorDetails { source_ranges: [SourceRange([74, 131])], message: "Cannot have an x constrained angle of 90 degrees" }"#
);
}
#[tokio::test(flavor = "multi_thread")]
async fn serial_test_angled_line_of_x_length_270() {
let code = r#"const sketch001 = startSketchOn('XZ')
|> startProfileAt([0, 0], %)
|> angledLineOfXLength({ angle: 90, length: 10 }, %, $edge1)
|> angledLineOfXLength({ angle: -15, length: 20 }, %, $edge2)
|> line([0, -5], %)
|> close(%, $edge3)
const extrusion = extrude(10, sketch001)
"#;
let result = execute_and_snapshot(code, UnitLength::Mm).await;
assert!(result.is_err());
assert_eq!(
result.err().unwrap().to_string(),
r#"type: KclErrorDetails { source_ranges: [SourceRange([74, 131])], message: "Cannot have an x constrained angle of 90 degrees" }"#
);
}
#[tokio::test(flavor = "multi_thread")]
async fn serial_test_angled_line_of_y_length_0() {
let code = r#"const exampleSketch = startSketchOn('XZ')
|> startProfileAt([0, 0], %)
|> line([10, 0], %)
|> angledLineOfYLength({ angle: 0, length: 10 }, %)
|> line([0, 10], %)
|> angledLineOfYLength({ angle: 135, length: 10 }, %)
|> line([-10, 0], %)
|> line([0, -30], %)
const example = extrude(10, exampleSketch)
"#;
let result = execute_and_snapshot(code, UnitLength::Mm).await;
assert!(result.is_err());
assert_eq!(
result.err().unwrap().to_string(),
r#"type: KclErrorDetails { source_ranges: [SourceRange([100, 148])], message: "Cannot have a y constrained angle of 0 degrees" }"#
);
}
#[tokio::test(flavor = "multi_thread")]
async fn serial_test_angled_line_of_y_length_180() {
let code = r#"const exampleSketch = startSketchOn('XZ')
|> startProfileAt([0, 0], %)
|> line([10, 0], %)
|> angledLineOfYLength({ angle: 180, length: 10 }, %)
|> line([0, 10], %)
|> angledLineOfYLength({ angle: 135, length: 10 }, %)
|> line([-10, 0], %)
|> line([0, -30], %)
const example = extrude(10, exampleSketch)
"#;
let result = execute_and_snapshot(code, UnitLength::Mm).await;
assert!(result.is_err());
assert_eq!(
result.err().unwrap().to_string(),
r#"type: KclErrorDetails { source_ranges: [SourceRange([100, 150])], message: "Cannot have a y constrained angle of 180 degrees" }"#
);
}
#[tokio::test(flavor = "multi_thread")]
async fn serial_test_angled_line_of_y_length_negative_180() {
let code = r#"const exampleSketch = startSketchOn('XZ')
|> startProfileAt([0, 0], %)
|> line([10, 0], %)
|> angledLineOfYLength({ angle: -180, length: 10 }, %)
|> line([0, 10], %)
|> angledLineOfYLength({ angle: 135, length: 10 }, %)
|> line([-10, 0], %)
|> line([0, -30], %)
const example = extrude(10, exampleSketch)
"#;
let result = execute_and_snapshot(code, UnitLength::Mm).await;
assert!(result.is_err());
assert_eq!(
result.err().unwrap().to_string(),
r#"type: KclErrorDetails { source_ranges: [SourceRange([100, 151])], message: "Cannot have a y constrained angle of 180 degrees" }"#
);
}