dxf格式为R12格式
Python的ezdxf包操作dxf文件-矩形排版之最低水平线法
pip install ezdxf==1.0.2 -i https://pypi.mirrors.ustc.edu.cn/simple/
下面是文件代码
sf001.py
# 水平线类(起始x位置,终止x位置,高度)
class OutLine:
def __init__(self, origin, end, height):
self.origin = origin
self.end = end
self.height = height
def __str__(self):
return "OutLine:origin:{}, end:{}, height:{}".format(self.origin, self.end, self.height)
# 矩形物品类(宽度,高度,编号)
class Product:
def __init__(self, w, h, num=0):
self.w = w
self.h = h
self.num = num
def __str__(self):
return "product:w:{}, h:{}, num:{}".format(self.w, self.h, self.num)
# 根据长度搜索矩形件(找出宽度小于目标长度的首个矩形件)
@staticmethod
def search_by_width(target_width, data):
for index, p in enumerate(data):
if p.w <= target_width:
return index
return None
# 根据长度搜索矩形件(找出宽度或高度小于目标长度的首个矩形件)
@staticmethod
def search_by_size(target_width, data):
for index, p in enumerate(data):
if p.w <= target_width or p.h <= target_width:
return index
return None
# 旋转90度并返回新对象
def rotate_new(self):
return Product(self.h, self.w, self.num)
# 布局类
class RectLayout:
def __init__(self, width, line_list=[]):
self.width = width
# 水平线集合(起始x位置,终止x位置,高度)
self.line_list = line_list
# 水平线(起始x位置,终止x位置,高度)
self.lowest_line = None
# 水平线索引(起始x位置,终止x位置,高度)
self.lowest_line_idx = 0
# 最终位置结果[[矩形件编号,左下角横坐标,左下角纵坐标,矩形件宽度,矩形件高度], ...]
self.result_pos = []
# 板材利用率
self.ratio = 0.0
# 初始化水平线集合(起始x位置,终止x位置,高度)
def init_line_list(self, origin, end, height):
init_line = OutLine(origin, end, height)
self.line_list = [init_line]
self.lowest_line = init_line
self.lowest_line_idx = 0
# 提升最低水平线
def enhance_line(self, index):
if len(self.line_list) > 1:
# 获取高度较低的相邻水平线索引,并更新水平线集
neighbor_idx = 0
if index == 0:
neighbor_idx = 1