我正在寻找一个python模块,将合并dxf文件。我找到了dxfgrabber和ezdxf,但是它们似乎用于不同的应用程序,而不是我想要的应用程序。
我使用的是ExpressPCB,它分别输出印刷电路板、孔和丝网的每一层。对于我的应用程序,我想将所有这些单独的DXF组合成一个。请看照片

据我所知,它的起源等都是一样的,所以它应该和现实生活中的一样。
目前,这两个模块都没有针对这类应用程序的任何教程。一些psudo代码以一种pythonic的方式让人理解这个想法:
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()在我的应用程序中,这些文件都将具有相同的原点,并且永远不会重叠,因此您应该能够以某种方式轻松地合并这些文件。提前感谢您的帮助!
发布于 2019-12-08 22:06:44
您可以使用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')此导入器仅支持基本图形,如直线、圆、圆弧和尺寸(没有尺寸样式替代)等。
所有扩展数据和第三方数据都将被忽略,但您的文件似乎足够简单,可以正常工作。
导入程序附加组件的文档可以在here中找到。
发布于 2021-11-06 01:08:36
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')https://stackoverflow.com/questions/58722235
复制相似问题