Skip to content

Commit

Permalink
Add x,y,z properties to center
Browse files Browse the repository at this point in the history
  • Loading branch information
DSchroer committed Mar 3, 2023
1 parent db302d6 commit d8a5ee2
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 21 deletions.
10 changes: 5 additions & 5 deletions crates/dslcad/src/library.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,10 @@ macro_rules! invoke {
}};
($map: ident, $name: ident=option_bool) => {{
match $map.get(stringify!($name)) {
Some(value) => value
Some(value) => Some(value
.to_bool()
.unwrap_or_else(||false),
None => false,
.ok_or(RuntimeError::UnexpectedType(value.get_type()))?),
None => None,
}
}};
($map: ident, $name: ident=text) => {{
Expand Down Expand Up @@ -319,7 +319,7 @@ impl Library {
bind!(scale, faces::scale[shape=edge, scale=number], Category::TwoD, "scale an edge"),
bind!(
center,
faces::center[shape = edge],
faces::center[shape = edge, x=option_bool, y=option_bool, z=option_bool],
Category::TwoD,
"center an edge"
),
Expand All @@ -344,7 +344,7 @@ impl Library {
bind!(scale, shapes::scale[shape=shape, scale=number], Category::ThreeD, "scale a shape"),
bind!(
center,
shapes::center[shape = shape],
shapes::center[shape = shape, x=option_bool, y=option_bool, z=option_bool],
Category::ThreeD,
"center a shape"
),
Expand Down
17 changes: 10 additions & 7 deletions crates/dslcad/src/library/faces.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,12 +215,15 @@ pub fn scale(shape: Rc<RefCell<Wire>>, size: f64) -> Result<Value, RuntimeError>
)?))))
}

pub fn center(shape: Rc<RefCell<Wire>>) -> Result<Value, RuntimeError> {
pub fn center(
shape: Rc<RefCell<Wire>>,
x: Option<bool>,
y: Option<bool>,
z: Option<bool>,
) -> Result<Value, RuntimeError> {
let center = shape.borrow().center_of_mass();
translate(
shape,
Some(-center.x()),
Some(-center.y()),
Some(-center.z()),
)
let x = if x.unwrap_or(true) { -center.x() } else { 0.0 };
let y = if y.unwrap_or(true) { -center.y() } else { 0.0 };
let z = if z.unwrap_or(true) { -center.z() } else { 0.0 };
translate(shape, Some(x), Some(y), Some(z))
}
17 changes: 10 additions & 7 deletions crates/dslcad/src/library/shapes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,15 @@ pub fn scale(shape: Rc<RefCell<Shape>>, size: f64) -> Result<Value, RuntimeError
Ok(Shape::scale(&shape, size)?.into())
}

pub fn center(shape: Rc<RefCell<Shape>>) -> Result<Value, RuntimeError> {
pub fn center(
shape: Rc<RefCell<Shape>>,
x: Option<bool>,
y: Option<bool>,
z: Option<bool>,
) -> Result<Value, RuntimeError> {
let center = shape.borrow().center_of_mass();
translate(
shape,
Some(-center.x()),
Some(-center.y()),
Some(-center.z()),
)
let x = if x.unwrap_or(true) { -center.x() } else { 0.0 };
let y = if y.unwrap_or(true) { -center.y() } else { 0.0 };
let z = if z.unwrap_or(true) { -center.z() } else { 0.0 };
translate(shape, Some(x), Some(y), Some(z))
}
4 changes: 2 additions & 2 deletions docs/reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ to build parts.
- `rotate(shape=edge, angle=[number])` rotate an edge
- `rotate(shape=edge, x=[number], y=[number], z=[number])` rotate an edge
- `scale(shape=edge, scale=number)` scale an edge
- `center(shape=edge)` center an edge
- `center(shape=edge, x=[bool], y=[bool], z=[bool])` center an edge

## 3D
- `extrude(shape=edge, x=[number], y=[number], z=[number])` extrude a face into a 3D shape
Expand All @@ -83,7 +83,7 @@ to build parts.
- `translate(shape=shape, x=[number], y=[number], z=[number])` move a shape
- `rotate(shape=shape, x=[number], y=[number], z=[number])` rotate a shape
- `scale(shape=shape, scale=number)` scale a shape
- `center(shape=shape)` center a shape
- `center(shape=shape, x=[bool], y=[bool], z=[bool])` center a shape

## Lists
- `length(list=list)` get the length of a list
Expand Down

0 comments on commit d8a5ee2

Please sign in to comment.