import cadquery as cq
from cadquery.vis import show
import ezdxf
def preprocess_dxf(input_dxf_path, layer_name):
"""
Removes bend and center lines from the DXF.
"""
# Read the original DXF file in
dxf = ezdxf.readfile("sheet-with-bend-line.dxf")
# Remove all entities from the layer, turning the layer off
# and/or deleting the layer definition is not enough
with dxf.entitydb.trashcan() as trash:
for entity in dxf.entitydb.values():
if not entity.dxf.hasattr("layer"):
continue
if layer_name == entity.dxf.layer:
# safe destruction while iterating
trash.add(entity.dxf.handle)
# Output the modified DXF as a copy
output_filename = input_dxf_path.replace(".dxf", "-copy.dxf")
dxf.saveas(output_filename)
return output_filename
# Work with the cleaned-up DXF
dxf_output_name = preprocess_dxf("sheet-with-bend-line.dxf", "DOWN_CENTERLINES")
bar = cq.importers.importDXF(dxf_output_name)
# The following line works to convert the wires into something that can be extruded.
# In the example, it ends up being a bar with pairs of holes down the x-axis.
# bar = bar.wires().toPending().extrude(10.0)
show(bar)