univert 0.2.0

Universal file converter (library and CLI)
Documentation
use std::fs;
use std::io::{BufWriter, Write};

use crate::{Error, Job, Result};

pub fn stl_to_obj(job: &Job) -> Result<()> {
  let mut file = fs::File::open(job.input)?;
  let stl = stl_io::read_stl(&mut file)
    .map_err(|e| Error::other(format!("stl parse: {e}")))?;

  let out = fs::File::create(job.output)?;
  let mut w = BufWriter::new(out);

  writeln!(w, "o Solid Object")?;
  writeln!(w)?;

  for v in &stl.vertices {
    writeln!(w, "v {} {} {}", v[0], v[1], v[2])?;
  }
  writeln!(w)?;

  for t in &stl.faces {
    // OBJ indices are 1-based.
    writeln!(
      w,
      "f {} {} {}",
      t.vertices[0] + 1,
      t.vertices[1] + 1,
      t.vertices[2] + 1,
    )?;
  }

  w.flush()?;
  Ok(())
}

pub fn stl_to_png(job: &Job) -> Result<()> {
  // Renders via openscad with a generated wrapper that imports the STL.
  crate::converters::scad::stl_to_png(job)
}