Pass Services into model functions

This makes those functions more composable. Right now, they basically
can't be combined.
This commit is contained in:
Hanno Braun 2023-06-19 12:47:40 +02:00
parent 1d233e7ce2
commit 395e4ca139
6 changed files with 38 additions and 33 deletions

View File

@ -9,9 +9,7 @@ use fj::{
math::Vector,
};
pub fn model(x: f64, y: f64, z: f64) -> Handle<Solid> {
let mut services = Services::new();
pub fn model(x: f64, y: f64, z: f64, services: &mut Services) -> Handle<Solid> {
let sketch = Sketch::empty()
.add_region(
Region::polygon(
@ -21,13 +19,13 @@ pub fn model(x: f64, y: f64, z: f64) -> Handle<Solid> {
[x / 2., y / 2.],
[-x / 2., y / 2.],
],
&mut services,
services,
)
.insert(&mut services),
.insert(services),
)
.insert(&mut services);
.insert(services);
let surface = services.objects.surfaces.xy_plane();
let path = Vector::from([0., 0., z]);
(sketch, surface).sweep(path, &mut services)
(sketch, surface).sweep(path, services)
}

View File

@ -1,7 +1,7 @@
use fj::handle_model;
use fj::{core::services::Services, handle_model};
fn main() -> fj::Result {
let model = cuboid::model(3., 2., 1.);
let model = cuboid::model(3., 2., 1., &mut Services::new());
handle_model(model)?;
Ok(())
}

View File

@ -12,24 +12,27 @@ use fj::{
math::{Point, Vector},
};
pub fn model(outer: f64, inner: f64, height: f64) -> Handle<Solid> {
let mut services = Services::new();
pub fn model(
outer: f64,
inner: f64,
height: f64,
services: &mut Services,
) -> Handle<Solid> {
let sketch = Sketch::empty()
.add_region(
Region::circle(Point::origin(), outer, &mut services)
Region::circle(Point::origin(), outer, services)
.add_interiors([Cycle::circle(
Point::origin(),
inner,
&mut services,
services,
)
.reverse(&mut services)
.insert(&mut services)])
.insert(&mut services),
.reverse(services)
.insert(services)])
.insert(services),
)
.insert(&mut services);
.insert(services);
let surface = services.objects.surfaces.xy_plane();
let path = Vector::from([0., 0., height]);
(sketch, surface).sweep(path, &mut services)
(sketch, surface).sweep(path, services)
}

View File

@ -1,7 +1,7 @@
use fj::handle_model;
use fj::{core::services::Services, handle_model};
fn main() -> fj::Result {
let model = spacer::model(1., 0.5, 1.);
let model = spacer::model(1., 0.5, 1., &mut Services::new());
handle_model(model)?;
Ok(())
}

View File

@ -14,9 +14,13 @@ use fj::{
math::Vector,
};
pub fn model(num_points: u64, r1: f64, r2: f64, h: f64) -> Handle<Solid> {
let mut services = Services::new();
pub fn model(
num_points: u64,
r1: f64,
r2: f64,
h: f64,
services: &mut Services,
) -> Handle<Solid> {
let num_vertices = num_points * 2;
let vertex_iter = (0..num_vertices).map(|i| {
let angle_rad = 2. * PI / num_vertices as f64 * i as f64;
@ -39,15 +43,15 @@ pub fn model(num_points: u64, r1: f64, r2: f64, h: f64) -> Handle<Solid> {
let sketch = Sketch::empty()
.add_region(
Region::polygon(outer_points, &mut services)
.add_interiors([Cycle::polygon(inner_points, &mut services)
.reverse(&mut services)
.insert(&mut services)])
.insert(&mut services),
Region::polygon(outer_points, services)
.add_interiors([Cycle::polygon(inner_points, services)
.reverse(services)
.insert(services)])
.insert(services),
)
.insert(&mut services);
.insert(services);
let surface = services.objects.surfaces.xy_plane();
let path = Vector::from([0., 0., h]);
(sketch, surface).sweep(path, &mut services)
(sketch, surface).sweep(path, services)
}

View File

@ -1,7 +1,7 @@
use fj::handle_model;
use fj::{core::services::Services, handle_model};
fn main() -> fj::Result {
let model = star::model(5, 1., 2., 1.);
let model = star::model(5, 1., 2., 1., &mut Services::new());
handle_model(model)?;
Ok(())
}