cadquery icon indicating copy to clipboard operation
cadquery copied to clipboard

DXF importer cannot load wires

Open sam-vdp opened this issue 1 year ago • 2 comments

The DXF importer at cadquery/occ_impl/importers/dxf.py tries to assemble the loaded file into faces and throws an error if this is not successful. It would be nice to be able to load DXF files that contain wires and edges, but no faces.

for name, layer in layers.items():
    if name.lower() in selected:
        res = _dxf_convert(layers[name], tol)
        wire_sets = sortWiresByBuildOrder(res)
        for wire_set in wire_sets:
            faces.append(Face.makeFromWires(wire_set[0], wire_set[1:]))

Removing the last three lines and returning a list of wires is basically sufficient.

sam-vdp avatar Dec 17 '23 15:12 sam-vdp

That should be doable. Could you provide an example DXF/MRE for testing?

adam-urbanczyk avatar Dec 20 '23 19:12 adam-urbanczyk

This is the file I was trying to load: dxf_with_lines.zip

As a workaround I patched a dxf wires importer in my construction file like this:

def importDXFWires(filename: str, tol: float = 1e-6, exclude = [], include = []):  

    if exclude and include:
        raise ValueError("you may specify either 'include' or 'exclude' but not both")

    dxf = ezdxf.readfile(filename)
    wires = []

    layers = dxf.modelspace().groupby(dxfattrib="layer")

    # normalize layer names to conform the DXF spec
    names = set([name.lower() for name in layers.keys()])

    if include:
        selected = names & set([name.lower() for name in include])
    elif exclude:
        selected = names - set([name.lower() for name in exclude])
    else:
        selected = names

    if not selected:
        raise ValueError("no DXF layers selected")

    for name, layer in layers.items():
        if name.lower() in selected:
            res = dxf_import._dxf_convert(layers[name], tol)
            wires.extend(res)

    return cq.Workplane("XY").newObject(wires)

cq.importers.importDXFWires = importDXFWires

sam-vdp avatar Dec 21 '23 14:12 sam-vdp