dxfscan 0.1.0

Binary DXF parser with typed entity data and lookup indices
Documentation
// SPDX-License-Identifier: ISC
use crate::point::Point3;
use crate::value::GroupValue;

/// A 3DFACE entity — a filled 3- or 4-sided face.
#[derive(Debug, Clone, Copy, Default)]
pub struct Face3d {
    /// First corner.
    pub first_corner: Point3,
    /// Second corner.
    pub second_corner: Point3,
    /// Third corner.
    pub third_corner: Point3,
    /// Fourth corner.
    ///
    /// Copied from the third corner if only three vertices were given.
    pub fourth_corner: Point3,
    /// Invisible edge flags.
    pub invisible_edge_flags: i16,
    /// Whether the fourth corner was explicitly set.
    has_fourth: bool,
}

impl Face3d {
    pub(crate) fn feed(&mut self, code: u16, val: &GroupValue) {
        if code == 70 {
            if let Some(v) = val.as_i16() {
                self.invisible_edge_flags = v;
            }
            return;
        }
        if let Some(v) = val.as_f64() {
            match code {
                10 => self.first_corner.x = v,
                20 => self.first_corner.y = v,
                30 => self.first_corner.z = v,
                11 => self.second_corner.x = v,
                21 => self.second_corner.y = v,
                31 => self.second_corner.z = v,
                12 => self.third_corner.x = v,
                22 => self.third_corner.y = v,
                32 => self.third_corner.z = v,
                13 => {
                    self.fourth_corner.x = v;
                    self.has_fourth = true;
                }
                23 => {
                    self.fourth_corner.y = v;
                    self.has_fourth = true;
                }
                33 => {
                    self.fourth_corner.z = v;
                    self.has_fourth = true;
                }
                _ => {}
            }
        }
    }

    /// Finalizes the face: if only 3 vertices were given, copies the third to the fourth.
    pub(crate) fn finish(&mut self) {
        if !self.has_fourth {
            self.fourth_corner = self.third_corner;
        }
    }
}