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

Request: Add variant of process_model that accepts Args rather than parsing them #2307

Open
starthal opened this issue Mar 28, 2024 · 2 comments
Labels
good first issue Good for newcomers type: feature New features and improvements to existing features

Comments

@starthal
Copy link

Fornjot already provides a process_model helper that accepts a "standard" set of arguments for exporting or viewing.

I want to make an app that uses arguments to configure/parameterize a model. But process_model calls parse directly so I can't extend the arguments with my own.

With an API like fn process_model_with_args<M>(fj: &mut fj::Instance, model: &M, args: fj::Args) -> fj::Result this would be straightforward.

For example:

#[derive(clap::Parser)]
pub struct Parameters {
    #[clap(required = true, long, value_parser = parse_vector_3)]
    /// Size of the cuboid, as a comma-separated vector `x,y,z`
    size: [f64; 3],
    #[command(flatten)]
    /// Standard fj args
    fj: fj::Args,
}

fn parse_vector_3(arg: &str) -> anyhow::Result<[f64; 3]> {
    Ok(arg
        .split(',')
        .map(str::parse)
        .collect::<Result<Vec<f64>, _>>()?
        .as_slice()
        .try_into()?)
}

fn main() -> anyhow::Result<()> {
    let mut fj = fj::Instance::new();
    let p = <Parameters as clap::Parser>::parse();
    let model = cuboid::model(p.size, &mut fj.core);
    process_model_with_args(&mut fj, &model, p.fj)?;
    Ok(())
}

The user can invoke this like:

cargo run -- --size 3,5,7 --export cuboid.stl
@hannobraun
Copy link
Owner

Thank you for opening this issue, @starthal! That's a very reasonable request.

Happy to accept a pull request implementing what you propose. Otherwise, I'll get this done whenever I can get to it.

@hannobraun hannobraun added the type: feature New features and improvements to existing features label Mar 30, 2024
@hannobraun
Copy link
Owner

Labeling as good first issue, as this is a pretty small change, and the solution is already laid out in the issue description.

@hannobraun hannobraun added the good first issue Good for newcomers label Apr 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers type: feature New features and improvements to existing features
Projects
None yet
Development

No branches or pull requests

2 participants