Skip to content

Commit

Permalink
Add VirtualFile, sldprt import (#3)
Browse files Browse the repository at this point in the history
* Add VirtualFile

* Add sldprt

* Move .github to repo root
  • Loading branch information
adamchalmers authored Dec 7, 2023
1 parent 82273da commit cfc1a73
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 5 deletions.
File renamed without changes.
File renamed without changes.
44 changes: 39 additions & 5 deletions modeling-cmds/src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@ pub mod step;
/// **ST**ereo**L**ithography format.
pub mod stl;

/// SolidWorks part (SLDPRT) format.
pub mod sldprt;

/// Output format specifier.
#[derive(Clone, Debug, Eq, Hash, PartialEq, Serialize, Deserialize, JsonSchema, Display, FromStr)]
#[serde(tag = "type", rename_all = "snake_case")]
#[display(style = "snake_case")]
pub enum OutputFormat {
// TODO: Uncomment all these variants and support their options.
/// Autodesk Filmbox (FBX) format.
#[display("{}: {0}")]
Fbx(fbx::export::Options),
Expand Down Expand Up @@ -52,7 +54,6 @@ pub enum OutputFormat {
#[serde(tag = "type", rename_all = "snake_case")]
#[display(style = "snake_case")]
pub enum InputFormat {
// TODO: Uncomment all these variants and support their options.
/// Autodesk Filmbox (FBX) format.
#[display("{}: {0}")]
Fbx(fbx::import::Options),
Expand All @@ -67,9 +68,9 @@ pub enum InputFormat {
/// The PLY Polygon File Format.
#[display("{}: {0}")]
Ply(ply::import::Options),
// /// SolidWorks part (SLDPRT) format.
// #[display("{}: {0}")]
// Sldprt(sldprt::import::Options),
/// SolidWorks part (SLDPRT) format.
#[display("{}: {0}")]
Sldprt(sldprt::import::Options),
/// ISO 10303-21 (STEP) format.
#[display("{}: {0}")]
Step(step::ImportOptions),
Expand Down Expand Up @@ -115,3 +116,36 @@ pub enum Selection {
name: String,
},
}

/// Represents an in-memory file with an associated potentially foreign file path.
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub struct VirtualFile {
/// Original file path.
pub path: std::path::PathBuf,
/// File payload.
pub data: Vec<u8>,
}

impl VirtualFile {
/// Returns true if the file name has the given extension.
pub fn has_extension(&self, required_extension: &str) -> bool {
self.path
.extension()
.and_then(std::ffi::OsStr::to_str)
.map(|extension| extension.eq_ignore_ascii_case(required_extension))
.unwrap_or(false)
}

fn read_fs_impl(path: std::path::PathBuf) -> std::io::Result<Self> {
let data = std::fs::read(&path)?;
Ok(Self { path, data })
}

/// Read from file system.
pub fn read_fs<P>(path: P) -> std::io::Result<Self>
where
P: Into<std::path::PathBuf>,
{
Self::read_fs_impl(path.into())
}
}
11 changes: 11 additions & 0 deletions modeling-cmds/src/format/sldprt.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/// Import functionality.
pub mod import {
use parse_display::{Display, FromStr};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

/// Options for importing SolidWorks parts.
#[derive(Clone, Debug, Default, Eq, Hash, PartialEq, Serialize, Deserialize, JsonSchema, Display, FromStr)]
#[display("")]
pub struct Options {}
}

0 comments on commit cfc1a73

Please sign in to comment.