dxfscan 0.1.0

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

/// A parsed LAYER table entry.
#[derive(Debug, Clone, Copy, Default)]
pub struct Layer<'a> {
    /// Entity handle.
    pub handle: &'a [u8],
    /// Layer name.
    pub name: &'a [u8],
    /// Layer flags.
    ///
    /// `0b1` = frozen. Frozen layers are not regenerated or plotted.
    /// Use [`is_frozen()`](Self::is_frozen) for convenience.
    pub flags: i16,
    /// Color.
    ///
    /// Negative values indicate the layer is off (entities on the layer
    /// are not displayed but are still regenerated). The absolute value
    /// is the ACI color index (1–255). Use [`is_on()`](Self::is_on) and
    /// [`color_index()`](Self::color_index) for convenience.
    pub color: i16,
    /// Linetype name.
    pub linetype_name: &'a [u8],
    /// Lineweight enum value.
    ///
    /// See [`EntityCommon::lineweight`](crate::EntityCommon::lineweight)
    /// for the interpretation of values.
    pub lineweight: i16,
}

impl<'a> Layer<'a> {
    /// Returns `true` if the layer is on (color is non-negative).
    pub fn is_on(&self) -> bool {
        self.color >= 0
    }

    /// Returns `true` if the layer is frozen (`0b1`).
    pub fn is_frozen(&self) -> bool {
        self.flags & 1 != 0
    }

    /// Returns the ACI color index (absolute value of color).
    pub fn color_index(&self) -> i16 {
        self.color.abs()
    }

    pub(crate) fn feed(&mut self, code: u16, val: &GroupValue<'a>) {
        match code {
            5 => {
                if let Some(s) = val.as_str_bytes() {
                    self.handle = s;
                }
            }
            2 => {
                if let Some(s) = val.as_str_bytes() {
                    self.name = s;
                }
            }
            70 => {
                if let Some(v) = val.as_i16() {
                    self.flags = v;
                }
            }
            6 => {
                if let Some(s) = val.as_str_bytes() {
                    self.linetype_name = s;
                }
            }
            62 => {
                if let Some(v) = val.as_i16() {
                    self.color = v;
                }
            }
            370 => {
                if let Some(v) = val.as_i16() {
                    self.lineweight = v;
                }
            }
            _ => {}
        }
    }
}