-
-
Notifications
You must be signed in to change notification settings - Fork 119
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1860 from hannobraun/fj
Add standardized CLI for models
- Loading branch information
Showing
8 changed files
with
87 additions
and
46 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,4 +13,4 @@ | |
mod run; | ||
mod window; | ||
|
||
pub use self::run::run; | ||
pub use self::run::{run, Error}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
use std::path::PathBuf; | ||
|
||
/// Standardized CLI for Fornjot models | ||
#[derive(clap::Parser)] | ||
pub struct Args { | ||
/// Export model to this path | ||
#[arg(short, long, value_name = "PATH")] | ||
pub export: Option<PathBuf>, | ||
} | ||
|
||
impl Args { | ||
/// Parse the command-line arguments | ||
/// | ||
/// Convenience method that saves the caller from having to import the | ||
/// `clap::Parser` trait. | ||
pub fn parse() -> Self { | ||
<Self as clap::Parser>::parse() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
use std::ops::Deref; | ||
|
||
use fj_core::algorithms::{approx::Tolerance, triangulate::Triangulate}; | ||
|
||
use crate::Args; | ||
|
||
/// Export or display a model, according to CLI arguments | ||
/// | ||
/// This function is intended to be called by applications that define a model | ||
/// and want to provide a standardized CLI interface for dealing with that | ||
/// model. | ||
/// | ||
/// This function is used by Fornjot's own testing infrastructure, but is useful | ||
/// beyond that, when using Fornjot directly to define a model. | ||
pub fn handle_model<Model>( | ||
model: impl Deref<Target = Model>, | ||
tolerance: impl Into<Tolerance>, | ||
) -> Result | ||
where | ||
for<'r> (&'r Model, Tolerance): Triangulate, | ||
{ | ||
let mesh = (model.deref(), tolerance.into()).triangulate(); | ||
|
||
let args = Args::parse(); | ||
if let Some(path) = args.export { | ||
crate::export::export(&mesh, &path)?; | ||
} else { | ||
crate::window::run(mesh, false)?; | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
/// Return value of [`handle_model`] | ||
pub type Result = std::result::Result<(), Error>; | ||
|
||
/// Error returned by [`handle_model`] | ||
#[derive(Debug, thiserror::Error)] | ||
pub enum Error { | ||
/// Error displaying model | ||
#[error("Error displaying model")] | ||
Display(#[from] crate::window::Error), | ||
|
||
/// Error exporting model | ||
#[error("Error exporting model")] | ||
Export(#[from] crate::export::Error), | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,12 @@ | ||
use std::{ops::Deref, path::PathBuf}; | ||
|
||
use fj::core::algorithms::{approx::Tolerance, triangulate::Triangulate}; | ||
|
||
fn main() -> anyhow::Result<()> { | ||
let args = Args::parse(); | ||
use fj::handle_model; | ||
|
||
fn main() -> fj::Result { | ||
let cuboid = cuboid::cuboid(3., 2., 1.); | ||
|
||
// The tolerance makes no difference for this model, as there aren't any | ||
// curves. | ||
let tolerance = Tolerance::from_scalar(1.)?; | ||
|
||
let mesh = (cuboid.deref(), tolerance).triangulate(); | ||
|
||
if let Some(path) = args.export { | ||
fj::export::export(&mesh, &path)?; | ||
} else { | ||
fj::window::run(mesh, false)?; | ||
} | ||
let tolerance = 1.; | ||
handle_model(cuboid, tolerance)?; | ||
|
||
Ok(()) | ||
} | ||
|
||
/// Standardized CLI for Fornjot models | ||
#[derive(clap::Parser)] | ||
pub struct Args { | ||
/// Export model to this path | ||
#[arg(short, long, value_name = "PATH")] | ||
pub export: Option<PathBuf>, | ||
} | ||
|
||
impl Args { | ||
/// Parse the command-line arguments | ||
/// | ||
/// Convenience method that saves the caller from having to import the | ||
/// `clap::Parser` trait. | ||
pub fn parse() -> Self { | ||
<Self as clap::Parser>::parse() | ||
} | ||
} |