Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow specification of dxf layer names to import. for issue #1051 #1061

Merged
merged 7 commits into from
Jul 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions cadquery/occ_impl/importers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,19 @@ def importStep(fileName):
return cq.Workplane("XY").newObject(solids)


def importDXF(filename, tol=1e-6, exclude=[]):
def importDXF(filename, tol=1e-6, exclude=[], include=[]):
"""
Loads a DXF file into a cadquery Workplane.
Loads a DXF file into a Workplane.

All layers are imported by default. Provide a layer include or exclude list
to select layers. Layer names are handled as case-insensitive.

:param fileName: The path and name of the DXF file to be imported
:param tol: The tolerance used for merging edges into wires (default: 1e-6)
:param exclude: a list of layer names not to import (default: [])
:param tol: The tolerance used for merging edges into wires
:param exclude: a list of layer names not to import
:param include: a list of layer names to import
"""

faces = _importDXF(filename, tol, exclude)
faces = _importDXF(filename, tol, exclude, include)

return cq.Workplane("XY").newObject(faces)
34 changes: 26 additions & 8 deletions cadquery/occ_impl/importers/dxf.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,24 +157,42 @@ def _dxf_convert(elements, tol):
return rv


def _importDXF(filename: str, tol: float = 1e-6, exclude: List[str] = []) -> List[Face]:
def _importDXF(
filename: str, tol: float = 1e-6, exclude: List[str] = [], include: List[str] = [],
) -> List[Face]:
"""
Loads a DXF file into a list of faces.

:param fileName: The path and name of the DXF file to be imported
:param tol: The tolerance used for merging edges into wires (default: 1e-6)
:param exclude: a list of layer names not to import (default: [])
:param tol: The tolerance used for merging edges into wires
:param exclude: a list of layer names not to import
:param include: a list of layer names to import
"""

# normalize layer names to conform the DXF spec
exclude_lwr = [ex.lower() for ex in exclude]
if exclude and include:
raise ValueError("you may specify either 'include' or 'exclude' but not both")

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

for name, layer in dxf.modelspace().groupby(dxfattrib="layer").items():
res = _dxf_convert(layer, tol) if name.lower() not in exclude_lwr else None
if res:
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_convert(layers[name], tol)
wire_sets = sortWiresByBuildOrder(res)
for wire_set in wire_sets:
faces.append(Face.makeFromWires(wire_set[0], wire_set[1:]))
Expand Down
3 changes: 2 additions & 1 deletion cadquery/sketch.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ def importDXF(
filename: str,
tol: float = 1e-6,
exclude: List[str] = [],
include: List[str] = [],
angle: Real = 0,
mode: Modes = "a",
tag: Optional[str] = None,
Expand All @@ -178,7 +179,7 @@ def importDXF(
Import a DXF file and construct face(s)
"""

res = Compound.makeCompound(_importDXF(filename, tol, exclude))
res = Compound.makeCompound(_importDXF(filename, tol, exclude, include))

return self.face(res, angle, mode, tag)

Expand Down
21 changes: 21 additions & 0 deletions tests/test_importers.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

from cadquery import importers, Workplane
from tests import BaseTest
from pytest import approx, raises

# where unit test output will be saved
OUTDIR = tempfile.gettempdir()
Expand Down Expand Up @@ -138,6 +139,26 @@ def testImportDXF(self):
self.assertEqual(obj.faces().size(), 2)
self.assertEqual(obj.wires().size(), 2)

obj = importers.importDXF(filename, include=["Layer2"])
assert obj.vertices("<XY").val().toTuple() == approx(
(104.2871791623584, 0.0038725018551133, 0.0)
)

obj = importers.importDXF(filename, include=["Layer2", "Layer3"])
assert obj.vertices("<XY").val().toTuple() == approx(
(104.2871791623584, 0.0038725018551133, 0.0)
)
assert obj.vertices(">XY").val().toTuple() == approx(
(257.6544359816229, 93.62447646419444, 0.0)
)

with raises(ValueError):
importers.importDXF(filename, include=["Layer1"], exclude=["Layer3"])

with raises(ValueError):
# Layer4 does not exist
importers.importDXF(filename, include=["Layer4"])

# test dxf extrusion into the third dimension
extrusion_value = 15.0
tmp = obj.wires()
Expand Down
8 changes: 8 additions & 0 deletions tests/test_sketch.py
Original file line number Diff line number Diff line change
Expand Up @@ -717,3 +717,11 @@ def test_dxf_import():
s3 = Sketch().circle(20).importDXF(filename, tol=1e-3, mode="s")

assert s3._faces.isValid()

s4 = Sketch().importDXF(filename, tol=1e-3, include=["0"])

assert s4._faces.isValid()

s5 = Sketch().importDXF(filename, tol=1e-3, exclude=["1"])

assert s5._faces.isValid()