Skip to main content

brep_kernel/io/parasolid/
mod.rs

1//! Parasolid XT (transmit file) import.
2//!
3//! Written from the published *Parasolid XT Format Reference*; section
4//! numbers in this module are that reference's (July 2022 edition).
5//!
6//! * [`parse_xt_text`] reads a text part (`.x_t`, `.xmt_txt`) into its node
7//!   graph: the header, the token stream, and each node laid out by the base
8//!   schema (`schema`) as edited by the file's embedded schema deltas.
9//!   [`parse_xt`] reads the same from bytes, text or binary (`.x_b`, neutral
10//!   or typed), part or partition (the form SolidWorks embeds bodies in).
11//! * [`import_parasolid`], [`import_parasolid_report`] and
12//!   [`import_parasolid_bytes_report`] read its bodies
13//!   into solids by translating each to Part 21 for the STEP importer
14//!   (`to_step`), with names and colours.
15
16mod check;
17mod geometry;
18mod import;
19mod reader;
20mod schema;
21mod to_step;
22mod to_xt;
23mod writer;
24
25
26pub use import::{import_parasolid, import_parasolid_bodies, import_parasolid_bytes_bodies, import_parasolid_bytes_report, import_parasolid_report, ParasolidImportReport};
27pub use reader::{parse_xt, parse_xt_text, Node, NodeLayout, Value, XtFile};
28pub use check::check_xt_body;
29pub use to_xt::{export_parasolid_binary, export_parasolid_report, export_parasolid_text, XtExportBody, XtExportReport};
30pub use writer::{write_xt_binary_part_data, write_xt_part_data};
31pub use schema::{Count as XtFieldCount, FieldDef as XtFieldDef, Kind as XtFieldKind};
32
33/// Whether `text` is a Parasolid text part: it starts with the transmit
34/// header's preamble (§3.1), or, written without a header, with the part
35/// data's `T<length> : TRANSMIT FILE` banner (§3.2). Only the start counts,
36/// so a STEP file that mentions Parasolid in its own header is not one.
37pub fn is_xt_text(text: &str) -> bool {
38    let start = text.trim_start_matches(['\u{feff}', ' ', '\t', '\r', '\n']);
39    if start.starts_with("**ABCDEFGHIJKLMNOPQRSTUVWXYZ") {
40        return start.lines().nth(1).is_some_and(|line| line.starts_with("**PARASOLID"));
41    }
42    let Some(rest) = start.strip_prefix('T') else { return false };
43    let digits = rest.bytes().take_while(u8::is_ascii_digit).count();
44    digits > 0 && rest[digits..].trim_start().starts_with(": TRANSMIT FILE")
45}
46
47/// Whether `bytes` are a binary Parasolid file: the neutral or typed binary
48/// flag (`PS\0\0`, `PS\0\1`), at the start or after the keyword header
49/// (§3.1, §3.3).
50pub fn is_xt_binary(bytes: &[u8]) -> bool {
51    let body = match bytes.windows(15).position(|w| w == b"**END_OF_HEADER") {
52        Some(end) if bytes.starts_with(b"**ABCDEFGHIJKLMNOPQRSTUVWXYZ") => {
53            match bytes[end..].iter().position(|&b| b == b'\n') {
54                Some(n) => &bytes[end + n + 1..],
55                None => return false,
56            }
57        }
58        _ => bytes,
59    };
60    matches!(body, [b'P', b'S', 0, 0 | 1, ..])
61}