ezdwg-core 0.22.0

Pure Rust DWG reader and native AC1015 writer
Documentation
use crate::bit::{BitWriter, Endian};
use crate::core::error::{DwgError, ErrorKind};
use crate::core::result::Result;

use super::common::{encode_entity_payload, CommonEntityEncodeInput};

#[derive(Debug, Clone)]
pub struct LwPolylineEncodeInput {
    pub common: CommonEntityEncodeInput,
    pub flags: u16,
    pub vertices: Vec<(f64, f64)>,
    pub const_width: Option<f64>,
    pub bulges: Vec<f64>,
    pub widths: Vec<(f64, f64)>,
}

pub fn encode_lwpolyline_entity_payload(input: LwPolylineEncodeInput) -> Result<Vec<u8>> {
    if input.vertices.is_empty() {
        return Err(DwgError::new(
            ErrorKind::Format,
            "LWPOLYLINE requires at least one vertex",
        ));
    }
    let common = input.common;
    encode_entity_payload(0x4D, common, |writer| write_lwpolyline_body(writer, &input))
}

fn write_lwpolyline_body(writer: &mut BitWriter, input: &LwPolylineEncodeInput) -> Result<()> {
    let vertex_count = input.vertices.len();
    let bulge_count = input.bulges.len().min(vertex_count);
    let width_count = input.widths.len().min(vertex_count);

    // Native DWG closes a polyline with bit 9; bit 0 means an explicit normal.
    // Also accept the legacy writer API's DXF-style closed bit.
    let mut flags = (input.flags & 0x80)
        | if input.flags & (0x0200 | 0x0001) != 0 {
            0x0200
        } else {
            0
        };
    if input.const_width.is_some() {
        flags |= 0x0004;
    }
    if bulge_count > 0 {
        flags |= 0x0010;
    }
    if width_count > 0 {
        flags |= 0x0020;
    }

    let f = input.common.formatting;
    if f.extrusion != (0., 0., 1.) {
        flags |= 1;
    }
    if f.thickness != 0. {
        flags |= 2;
    }
    if f.elevation != 0. {
        flags |= 8;
    }
    writer.write_bs(flags)?;

    if let Some(const_width) = input.const_width {
        writer.write_bd(const_width)?;
    }

    if flags & 8 != 0 {
        writer.write_bd(f.elevation)?;
    }
    if flags & 2 != 0 {
        writer.write_bd(f.thickness)?;
    }
    if flags & 1 != 0 {
        writer.write_3bd(f.extrusion.0, f.extrusion.1, f.extrusion.2)?;
    }
    writer.write_bl(vertex_count as u32)?;
    if bulge_count > 0 {
        writer.write_bl(bulge_count as u32)?;
    }
    if width_count > 0 {
        writer.write_bl(width_count as u32)?;
    }

    let (x0, y0) = input.vertices[0];
    writer.write_rd(Endian::Little, x0)?;
    writer.write_rd(Endian::Little, y0)?;
    let mut prev_x = x0;
    let mut prev_y = y0;
    for (x, y) in input.vertices.iter().skip(1).copied() {
        writer.write_dd(prev_x, x)?;
        writer.write_dd(prev_y, y)?;
        prev_x = x;
        prev_y = y;
    }

    for bulge in input.bulges.iter().take(bulge_count) {
        writer.write_bd(*bulge)?;
    }
    for (start_width, end_width) in input.widths.iter().take(width_count) {
        writer.write_bd(*start_width)?;
        writer.write_bd(*end_width)?;
    }
    Ok(())
}