BREP_kernel 0.4.0

A boundary representation (BREP) geometry kernel for building CAD applications.
Documentation
//! ISO-10303-21 (STEP Part 21) importer — the read side of `step.rs`.
//!
//! Strategy is ROUND-TRIP-FIRST: this reads back exactly what `export_step`
//! writes (an all-NURBS ADVANCED_BREP: B_SPLINE_CURVE/SURFACE_WITH_KNOTS and
//! their rational complex forms, VERTEX_POINT / EDGE_CURVE / ORIENTED_EDGE /
//! EDGE_LOOP / FACE_(OUTER_)BOUND / ADVANCED_FACE / CLOSED_SHELL /
//! MANIFOLD_SOLID_BREP), reconstructs a `BrepSolid`, and validates it.
//!
//! This module DERIVES pcurves rather than reading them: every 3D edge curve is
//! projected onto its carrier surface (`build_pcurve_on_surface_range`), seam
//! edges are placed on opposite domain boundaries, and any pole boundary that
//! is not written as a `VERTEX_LOOP` is re-synthesised as a degenerate edge so
//! faces close their parameter domain and integrate to the correct mass
//! properties.
//!
//! That is now a CHOICE, not a necessity: since `step.rs` grew curve-on-surface
//! emission, our own files carry `SURFACE_CURVE`/`SEAM_CURVE` bundles with
//! verified `PCURVE`s, and `VERTEX_LOOP` for collapsed bounds — and this reader
//! still unwraps the bundle to its 3D curve and discards the pcurve list
//! (`parse.rs`). Consuming them instead (a trust-but-verify read path) is a
//! documented follow-up in kernel-plans/step-export-pcurves.md; deriving is
//! correct meanwhile, and is what vendor files without pcurves need anyway.
//!
//! Analytic-surface recognition on import (PLANE/CYLINDRICAL/…) is a documented
//! follow-up: the exporter emits only NURBS, so faithful NURBS round-trip is the
//! milestone. Unsupported entities produce a clear `Err`, never a panic.

use crate::topology::{
    BrepSolid, CoedgeRecord, EdgeRecord, FaceRecord, LoopRecord, ShellRecord, VertexRecord,
};
use crate::{
    build_pcurve_on_surface_range, make_arc, make_extrusion, make_hyperbola, make_line,
    make_parabola, make_plane, make_revolution, offset_surface, transform_brep, AffineTransform,
    NurbsCurve, NurbsSurface, Vec3, Vec4,
};
use rustc_hash::{FxHashMap as HashMap, FxHashSet as HashSet};
use std::collections::VecDeque;

const TAU: f64 = std::f64::consts::TAU;

mod parse;
mod bodies;
mod assembly;
mod builder;
mod geometry;
mod styles;
#[cfg(test)]
mod tests;

use crate::appearance::{BodyAppearance, ImportedColor};
use parse::*;
use bodies::*;
use builder::*;
use geometry::*;
use styles::*;
pub use assembly::{read_step_assembly, StepAssembly, StepOccurrence, StepProduct};


// ---------------------------------------------------------------------------
// Topology assembly
// ---------------------------------------------------------------------------

/// Public entry: parse a STEP Part 21 document and return one `BrepSolid` per
/// MANIFOLD_SOLID_BREP, FACETED_BREP, or certified BREP_WITH_VOIDS. Every
/// returned solid passes `validate()`.
pub fn import_step(text: &str) -> Result<Vec<BrepSolid>, String> {
    Ok(import_step_with_appearance(text)?.0)
}

/// [`import_step`] plus the file's PRESENTATION colours: one
/// [`BodyAppearance`] per returned solid, in the same order.
///
/// This is the lane the IMPORT3D feature uses, because colour is stamped as
/// scene metadata on the names IT chooses — see
/// `feature_pipeline::features::import3d` and `io/appearance.rs` for the record
/// shape. A file with no presentation entities returns default (empty)
/// appearances, never a short vector, so callers can `zip` unconditionally.
pub fn import_step_with_appearance(
    text: &str,
) -> Result<(Vec<BrepSolid>, Vec<BodyAppearance>), String> {
    let imported = collect_step_solids(text)?;
    if imported.solids.is_empty() {
        return Err(imported.first_error.unwrap_or_else(|| {
            "step_import: no MANIFOLD_SOLID_BREP/FACETED_BREP/BREP_WITH_VOIDS body imported (unsupported or invalid representation)"
                .into()
        }));
    }
    Ok((imported.solids, imported.appearances))
}

/// Like [`import_step`] but reports how many bodies failed and the first error,
/// so callers can distinguish a fully- from a partially-imported assembly.
pub fn import_step_report(text: &str) -> Result<(Vec<BrepSolid>, usize, Option<String>), String> {
    let imported = collect_step_solids(text)?;
    Ok((imported.solids, imported.failed, imported.first_error))
}