Replies: 1 comment 1 reply
-
You always have to think like an engineer and combine the tools you have. Fortunately, the toolbox has grown since the This is the content of the This solution is limited by the features of the Importer add-on and will only work with simple structured DXF files. import ezdxf
from ezdxf import colors, bbox
from ezdxf.math import Matrix44
from ezdxf.addons import Importer
LAYER_RED = ("LAYER_RED", colors.RED)
LAYER_GREEN = ("LAYER_GREEN", colors.GREEN)
LAYER_BLUE = ("LAYER_BLUE", colors.BLUE)
GAP = 20
def merge(source, target):
importer = Importer(source, target)
# import all entities from source modelspace into target modelspace
importer.import_modelspace()
# import all required resources and dependencies
importer.finalize()
def assign_layer(doc, layer_props):
layer_name, layer_color = layer_props
try:
new_layer = doc.layers.new(layer_name)
except ezdxf.DXFTableEntryError:
print(f"layer '{layer_name}' already exist")
return
new_layer.dxf.color = layer_color
for entity in doc.modelspace():
entity.dxf.layer = layer_name
def shift_entities(msp, dx):
m = Matrix44.translate(dx, 0, 0)
for entity in msp:
try:
entity.transform(m)
except NotImplementedError:
entity.destroy()
target_dxf = ezdxf.readfile("base.dxf")
assign_layer(target_dxf, LAYER_RED)
cache = bbox.Cache()
for filename, layer_props in [
# could be different DXF files of course
("base.dxf", LAYER_GREEN),
("base.dxf", LAYER_BLUE),
]:
append_dxf = ezdxf.readfile(filename)
# get extents of the merged modelspace of the target DXF,
# here is a bounding box cache useful:
extents = bbox.extents(target_dxf.modelspace(), cache=cache)
# shift imported entities to the right:
shift_entities(append_dxf.modelspace(), extents.size.x + GAP)
# assign new layers to the imported entities
assign_layer(append_dxf, layer_props)
# import entities into the target DXF
merge(append_dxf, target_dxf)
target_dxf.saveas("merged.dxf") |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Hi @mozman
is it possible to merge different dxf files like in this query https://stackoverflow.com/questions/58722235/python-module-to-merge-dxf-files but assigning the entities of each file to a different layer?
i.e. file1 to edge layer, file2 to silks layer etc., everything in the same dxf merged file
thanks in advance
Maurice
Beta Was this translation helpful? Give feedback.
All reactions