ixmilia / dxf-rs

MIT License
95 stars 29 forks source link

AutoCAD open generated dxf file error: No valid handle for SEQEND #28

Closed MichaelCache closed 1 year ago

MichaelCache commented 1 year ago

how to reproduce error:

extern crate dxf;
use dxf::entities::*;
use dxf::Drawing;
use dxf::Point;
use dxf::*;

fn main() {
    let mut dxf_writer = Drawing::new();

    // use closed ployline to create a rectangle
    let mut poly = Polyline::default();
    poly.add_vertex(&mut dxf_writer, Vertex {
        location: Point::new(-10.0, 0.0, 0.0),
        ..Default::default()
    });
    poly.add_vertex(&mut dxf_writer, Vertex {
        location: Point::new(10.0, 0.0, 0.0),
        ..Default::default()
    });
    poly.add_vertex(&mut dxf_writer, Vertex {
        location: Point::new(10.0, 25.0, 0.0),
        ..Default::default()
    });
    poly.add_vertex(&mut dxf_writer, Vertex {
        location: Point::new(-10.0, 25.0, 0.0),
        ..Default::default()
    });
    poly.add_vertex(&mut dxf_writer, Vertex {
        location: Point::new(-10.0, 0.0, 0.0),
        ..Default::default()
    });

    poly.set_is_closed(true);

    dxf_writer.add_entity(Entity::new(EntityType::Polyline(poly)));

    dxf_writer.save_file("./test.dxf").unwrap_or_else(|err| {
        eprintln!("can not write file : {}", err);
        process::exit(1);
    });
}

then open the generated test.dxf with AutoCAD2022, it complain that: No database handle specified for object on line 1264

which line 1264 of test.dxf is: img

check dxf file format reference, I found that error is from line 1258, SEQEND is setted handle 0. if fix it with a valid handle, for example 16(from HANDSEED value, then HANDSEED need increase by 1), AutoCAD can open this dxf

analyse source code drawing.rs, I found that source process Polyline in this favour img2 not call next_handle() for SEQEND of closed Polyline and then when call drawing::save_file(), will write current HANDSEED in HEAD section then in ENTITIES section, create a temp SEQEND entity with default handle is 0, and write to dxf file directly @brettfo

brettfo commented 1 year ago

Good find and thank you for confirming the fix of assigning the handle and incrementing $HANDSEED. I've pushed commit 60ce7e332bf0da6d9f96b888f214ecd441bc31d7 to main that fixes this.