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;
use alloc::vec::Vec;

/// A LEADER entity — a sequence of vertices forming an annotation leader line.
#[derive(Debug, Clone, Default)]
pub struct Leader<'a> {
    /// Dimension style name.
    pub style_name: &'a [u8],
    /// Arrowhead flag.
    ///
    /// 0 = disabled, 1 = enabled.
    pub arrowhead_flag: i16,
    /// Leader path type.
    ///
    /// | Value | Meaning        |
    /// |------:|----------------|
    /// |     0 | Straight lines |
    /// |     1 | Spline         |
    pub path_type: i16,
    /// Hook line direction flag.
    ///
    /// 0 = hook line is the opposite direction from the horizontal
    /// vector, 1 = same direction.
    pub hook_line_direction: i16,
    /// Leader creation flag.
    ///
    /// | Value | Meaning   |
    /// |------:|-----------|
    /// |     0 | Text      |
    /// |     1 | Tolerance |
    /// |     2 | Block     |
    /// |     3 | None      |
    pub creation_flag: i16,
    /// Hook line flag.
    ///
    /// 0 = no hook line, 1 = has hook line.
    pub hook_line_flag: i16,
    /// Text annotation height.
    pub annotation_height: f64,
    /// Text annotation width.
    pub annotation_width: f64,
    /// Horizontal direction vector.
    pub horizontal_direction: Point3,
    /// Offset of last leader vertex from block reference insertion point.
    pub block_offset: Point3,
    /// Offset of last leader vertex from annotation placement point.
    pub annotation_offset: Point3,
    /// Vertex list.
    pub vertices: Vec<Point3>,
    /// In-progress vertex being accumulated.
    current_pt: Option<Point3>,
}

impl<'a> Leader<'a> {
    pub(crate) fn feed(&mut self, code: u16, val: &GroupValue<'a>) {
        match code {
            3 => {
                if let Some(s) = val.as_str_bytes() {
                    self.style_name = s;
                }
            }
            10 => {
                if let Some(pt) = self.current_pt.take() {
                    self.vertices.push(pt);
                }
                self.current_pt = Some(Point3 {
                    x: val.as_f64().unwrap_or(0.0),
                    ..Default::default()
                });
            }
            20 => {
                if let Some(ref mut pt) = self.current_pt {
                    pt.y = val.as_f64().unwrap_or(0.0);
                }
            }
            30 => {
                if let Some(ref mut pt) = self.current_pt {
                    pt.z = val.as_f64().unwrap_or(0.0);
                }
            }
            71 => {
                if let Some(v) = val.as_i16() {
                    self.arrowhead_flag = v;
                }
            }
            72 => {
                if let Some(v) = val.as_i16() {
                    self.path_type = v;
                }
            }
            73 => {
                if let Some(v) = val.as_i16() {
                    self.creation_flag = v;
                }
            }
            74 => {
                if let Some(v) = val.as_i16() {
                    self.hook_line_direction = v;
                }
            }
            75 => {
                if let Some(v) = val.as_i16() {
                    self.hook_line_flag = v;
                }
            }
            _ => {
                if let Some(v) = val.as_f64() {
                    match code {
                        40 => self.annotation_height = v,
                        41 => self.annotation_width = v,
                        211 => self.horizontal_direction.x = v,
                        221 => self.horizontal_direction.y = v,
                        231 => self.horizontal_direction.z = v,
                        212 => self.block_offset.x = v,
                        222 => self.block_offset.y = v,
                        232 => self.block_offset.z = v,
                        213 => self.annotation_offset.x = v,
                        223 => self.annotation_offset.y = v,
                        233 => self.annotation_offset.z = v,
                        _ => {}
                    }
                }
            }
        }
    }

    pub(crate) fn finish(&mut self) {
        if let Some(pt) = self.current_pt.take() {
            self.vertices.push(pt);
        }
    }
}