LibreCAD icon indicating copy to clipboard operation
LibreCAD copied to clipboard

DXF file fails to open if it includes VIEWPORT section

Open Druco opened this issue 3 years ago • 1 comments

Expected behavior

DXF file with VIEWPORT section should load.

Observed behavior

Trying to load a DXF file with a VIEWPORT section fails with an Error popup that says "Import error: error reading DXF/DWG section".

Steps to reproduce or sample file

  1. Start LibreCAD
  2. Try to open DXF file with VIEWPORT section (see link below for files that fail)

Operating System and LibreCAD version info

Linux openSuse Tumbleweed LibreCAD Version: 2.2.0-rc3-24-g4602ec6a

I tried to open any of the files in https://www.sandiego.gov/sites/default/files/ds-3179_dxf.zip and they failed to open. I'm sorry I don't have a simpler example I could attach but this is the first time I've tried to use LibreCAD and these are the government supplied templates that I am trying to fill in.

Building from source, I was able to track the failure down to loading the VIEWPORT section. It appears to me that libdxfrw.cpp (~ line 2436) contains a logic error:

bool dxfRW::processViewport() {
    DRW_DBG("dxfRW::processViewport");
    int code;
    DRW_Viewport vp;
    while (reader->readRec(&code)) {
        DRW_DBG(code); DRW_DBG("\n");
        if (0 == code) {
            nextentity = reader->getString();
            DRW_DBG(nextentity); DRW_DBG("\n");
            iface->addViewport(vp);
            return true;  //found new entity or ENDSEC, terminate
        }

        if (!vp.parseCode(code, reader)) {
            return setError( DRW::BAD_CODE_PARSED);
        }
        break;   // <====== Possible error
    }

    return setError(DRW::BAD_READ_ENTITIES);
}

where the 'break' statement (I've marked 'Possible error' above) improperly drops out of the loop after only one element of the section is processed. Simply removing the 'break' statement seems to fix the problem for me but I am not familiar enough with the code to tell if this may cause side effects somewhere else and thus need a better solution.

Druco avatar Apr 09 '22 23:04 Druco

DXF spec on viewport entities:

  • https://ezdxf.readthedocs.io/en/stable/dxfentities/viewport.html

Here are related issues and discussions:

  • https://github.com/LibreCAD/LibreCAD/issues/1371
  • https://forum.librecad.org/quot-viewport-quot-is-what-I-m-looking-for-td5707029.html
  • https://forum.librecad.org/Viewports-in-LibreCAD-td5709001.html
  • https://forum.librecad.org/Funcion-ViewPort-The-simple-users-neet-to-this-td5723948.html

Potentially, with LibreCAD would get support for viewports there might be an option for exported PDF to include viewports with interactive bookmarks too, so in PDF viewer it would be easy switch between viewports in interactive mode:

  • https://discuss.logseq.com/t/pdf-custom-viewport-mode-and-adaptive-dpi/2068
  • https://stackoverflow.com/questions/47772334/how-do-i-extract-viewport-from-a-pdf-and-modify-an-annotations-bounding-rectang

PDF viewports export feature might be also related to:

  • https://github.com/LibreCAD/LibreCAD/issues/1823

I tried to open any of the files in https://www.sandiego.gov/sites/default/files/ds-3179_dxf.zip and they failed to open.

Link wrongly pasted, here is correct URL: ds-3179_dxf.zip

I tried to open it in the latest LibreCAD 2.2.2-alpha (AppImage) and it opens without crashes (there are just some errors with polylines and arc in "Cmd" output), but has no clue how does imported viewports are imported as there is no "Viewport" functionality in yet.

dxf-viewport

u2fly avatar Aug 13 '24 09:08 u2fly