2

I am looking for a python module that will merge dxf files. I have found dxfgrabber and ezdxf, however they seem to be used for different applications then what I am after.

I am using ExpressPCB, which outputs each layer of the PCB, the holes, and the silkscreen separately. For my application I want to combine all of these individual DXF into a single. See photo

enter image description here

As far as I know, the origins, etc are the same so it should all line up as it would in real life.

Currently, neither of these modules have any tutorials for this type of application. Some psudo code to get the idea across in a pythonic way:

dxf_file1 = read(file1)
dxf_file2 = read(file2)    
dxf_file3 = read(file3)

out_file.append(dxf_file1)
out_file.append(dxf_file2)
out_file.append(dxf_file3)

outfile.save()

In my application, the files will all have the same origin point and will never overlap, so you should be able to easily merge the files somehow. Thank you for the help in advance!

paperstsoap
  • 335
  • 4
  • 13

2 Answers2

4

You can use the rewritten Importer add-on in ezdxf v0.10:

import ezdxf
from ezdxf.addons import Importer


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()


base_dxf = ezdxf.readfile('file1.dxf')

for filename in ('file2.dxf', 'file3.dxf'):
    merge_dxf = ezdxf.readfile(filename)
    merge(merge_dxf, base_dxf)

# base_dxf.save()  # to save as file1.dxf
base_dxf.saveas('merged.dxf')

This importer supports just basic graphics like LINE, CIRCLE, ARC and DIMENSION (without dimension style override) and so on.

All XDATA and 3rd party data will be ignored, but your files seems to be simple enough to work.

Documentation of the Importer add-on can be found here.

mozman
  • 2,001
  • 8
  • 23
  • 2
    Using the above code is merging dxf files, but files are getting overlapped, is there a way to create multiple dxf in one file with an offset? – Manjula Devi Jan 06 '21 at 04:17
  • 1
    That is beyond the capabilities of ezdxf -- you need a CAD application. – mozman Jan 06 '21 at 05:02
  • @mozman I have 10 DXF files with lines and points and I would like to create one DXF file with 10 layers. Is that possible? When I use your code, I have 2 layers (one with circles and one with lines) in the output file. – Taras Melon Jul 03 '23 at 14:59
0
import sys
import ezdxf
from ezdxf.addons import geo
from shapely.geometry import shape
from shapely.ops import unary_union

def dxf2shapley(filename):
    doc = ezdxf.readfile(filename)
    msp = doc.modelspace()
    entities = msp.query('LINE')
    proxy = geo.proxy(entities)
    shapley_polygon = shape(proxy)
    if !shapley_polygon.is_valid:
        raise Exception('polygon is not valid')
    return shapley_polygon

h0 = dxf2shapley('test-0.dxf')
h1 = dxf2shapley('test-1.dxf')

polygons = [h0, h1]
polyout = unary_union(polygons)

result = ezdxf.addons.geo.dxf_entities(polyout, polygon=2)
doc = ezdxf.new('R2010')
msp = doc.modelspace()
for entity in result:
    msp.add_entity(entity)

doc.saveas('test_merged.dxf')
Ray Hulha
  • 10,701
  • 5
  • 53
  • 53