use ferrocad_gpui::HostConfig;
pub mod payload;
pub use ferrocad_gpui::Contributions;
pub fn register_core_module() {
use ferrocad_py::ferrocad as ferrocad_module;
pyo3::append_to_inittab!(ferrocad_module);
}
pub fn run() {
run_as("FerroCAD");
}
pub fn run_as(app_name: &str) {
run_edition(app_name, "ferrocad_shell");
}
pub fn run_edition(app_name: &str, entry_module: &str) {
run_edition_with(app_name, entry_module, base_contributions());
}
pub fn base_contributions() -> Contributions {
Contributions::new().with_gizmo(ferrocad_gpui::translate_gizmo)
}
pub fn run_edition_with(
app_name: &str,
entry_module: &str,
contributions: Contributions,
) {
run_edition_with_document(app_name, entry_module, contributions, None);
}
pub fn run_edition_with_document(
app_name: &str,
entry_module: &str,
contributions: Contributions,
open: Option<std::path::PathBuf>,
) {
register_core_module();
let mut config = app_config(app_name, entry_module);
config.contributions = contributions;
config.open_document = open;
ferrocad_gpui::run_with(config);
}
pub fn check_init() -> Result<(), String> {
register_core_module();
ferrocad_gpui::check_init(base_config("FerroCAD"))
}
pub use ferrocad_gpui::RenderReport;
pub fn render_edition_headless(
app_name: &str,
entry_module: &str,
render: Option<&std::path::Path>,
stl: Option<&std::path::Path>,
size: [u32; 2],
script: Option<&std::path::Path>,
open: Option<&std::path::Path>,
) -> Result<RenderReport, String> {
register_core_module();
let mut config = app_config(app_name, entry_module);
config.open_document = open.map(std::path::Path::to_path_buf);
ferrocad_gpui::headless::run(&config, render, stl, size, script)
}
#[derive(Debug, Clone)]
pub struct EditionArgs {
pub render: Option<std::path::PathBuf>,
pub stl: Option<std::path::PathBuf>,
pub size: [u32; 2],
pub script: Option<std::path::PathBuf>,
pub open: Option<std::path::PathBuf>,
}
impl Default for EditionArgs {
fn default() -> Self {
Self {
render: None,
stl: None,
size: [800, 600],
script: None,
open: None,
}
}
}
pub fn edition_args() -> Result<EditionArgs, String> {
let mut parsed = EditionArgs::default();
let mut args = std::env::args().skip(1);
while let Some(arg) = args.next() {
match arg.as_str() {
"--render" => {
let path = args
.next()
.ok_or_else(|| "--render wants a path".to_string())?;
parsed.render = Some(std::path::PathBuf::from(path));
}
"--stl" => {
let path = args
.next()
.ok_or_else(|| "--stl wants a path".to_string())?;
parsed.stl = Some(std::path::PathBuf::from(path));
}
"--script" => {
let path = args
.next()
.ok_or_else(|| "--script wants a path".to_string())?;
parsed.script = Some(std::path::PathBuf::from(path));
}
"--open" => {
let path = args
.next()
.ok_or_else(|| "--open wants a path".to_string())?;
parsed.open = Some(std::path::PathBuf::from(path));
}
"--size" => {
let value = args
.next()
.ok_or_else(|| "--size wants WIDTHxHEIGHT".to_string())?;
parsed.size = parse_size(&value)?;
}
_ => {}
}
}
Ok(parsed)
}
fn parse_size(value: &str) -> Result<[u32; 2], String> {
let (width, height) = value
.split_once('x')
.ok_or_else(|| format!("--size wants WIDTHxHEIGHT, got {value:?}"))?;
let parse = |part: &str| {
part.parse::<u32>()
.ok()
.filter(|n| *n > 0)
.ok_or_else(|| format!("--size wants positive integers, got {value:?}"))
};
Ok([parse(width)?, parse(height)?])
}
fn base_config(app_name: &str) -> HostConfig {
app_config(app_name, "ferrocad_shell")
}
fn app_config(app_name: &str, entry_module: &str) -> HostConfig {
let payload = payload::resolve();
HostConfig {
app_name: app_name.to_string(),
entry_module: entry_module.to_string(),
python_paths: payload.python_paths,
mods_paths: payload.mods_paths,
contributions: Contributions::default(),
open_document: None,
}
}
#[cfg(test)]
mod tests {
use ferrocad_geom::SelectionRef;
use ferrocad_stage::{ShapeItem, Stage};
use super::*;
fn stage_with_a_box() -> Stage {
let mut stage = Stage::new();
stage.add_item(ShapeItem::new(7).with_shape(
vec![[0.0, 0.0, 0.0], [2.0, 2.0, 4.0]],
Vec::new(),
Vec::new(),
Vec::new(),
));
stage
}
#[test]
fn adding_a_manipulator_keeps_the_shared_move_gizmo() {
let stage = stage_with_a_box();
let selection = SelectionRef::whole(7);
let base = base_contributions();
assert_eq!(
base.gizmo_for(&selection, &stage)
.map(|gizmo| gizmo.target_id()),
Some(7),
"the base composition arms the move gizmo for a whole-object selection"
);
let with_manipulator =
base_contributions().with_manipulator(|_: &SelectionRef, _: &Stage| None);
assert!(
with_manipulator.gizmo_for(&selection, &stage).is_some(),
"a workbench manipulator must not replace the gizmo registry"
);
let without_base =
Contributions::default().with_manipulator(|_: &SelectionRef, _: &Stage| None);
assert!(without_base.gizmo_for(&selection, &stage).is_none());
}
#[test]
fn the_shared_move_gizmo_does_not_claim_a_sub_element() {
let stage = stage_with_a_box();
assert!(
base_contributions()
.gizmo_for(&SelectionRef::element(7, "Face1"), &stage)
.is_none()
);
}
}