File size: 2,601 Bytes
3efc7f6
 
 
 
 
a3bae4b
3efc7f6
 
 
 
 
5a8852a
3efc7f6
 
 
a3bae4b
3efc7f6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5a8852a
3efc7f6
 
 
 
5a8852a
 
 
 
 
 
 
 
 
3efc7f6
 
a3bae4b
3efc7f6
 
 
 
a3bae4b
 
3efc7f6
 
 
 
 
 
 
a3bae4b
3efc7f6
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import matplotlib.pyplot as plt
from matplotlib.collections import LineCollection
import io
import numpy as np

def export_plywood_layout_pdf(sheets_data, title="Cut Patterns", color='black'):
    """
    Exports the packed meshes from multiple sheets to a multi-page PDF.
    'sheets_data' is a list of dicts: [{'meshes': [...], 'w': ..., 'h': ...}, ...]
    """
    from matplotlib.backends.backend_pdf import PdfPages
    from code.packing import get_mesh_2d_bbox
    
    buf = io.BytesIO()
    with PdfPages(buf) as pdf:
        scale = 39.37 # Meters to Inches for 1:1 scale
        
        for sheet in sheets_data:
            meshes = sheet['meshes']
            sheet_w = sheet['w']
            sheet_h = sheet['h']
            
            fig, ax = plt.subplots(figsize=(sheet_w * scale, sheet_h * scale))
            
            # 1. Collect all edges
            all_lines = []
            
            # Add Sheet Boundary
            sheet_boundary = [
                (0, 0), (sheet_w, 0), (sheet_w, sheet_h), (0, sheet_h), (0, 0)
            ]
            for i in range(len(sheet_boundary) - 1):
                all_lines.append([sheet_boundary[i], sheet_boundary[i+1]])

            # Add Mesh Edges and Labels
            for mesh in meshes:
                for edge in mesh.edges():
                    p1, p2 = mesh.edge_coordinates(edge)
                    all_lines.append([(p1[0], p1[1]), (p2[0], p2[1])])
                
                # Add Label
                name = mesh.attributes.get('name', '')
                if name:
                    # Simple bbox center for label placement
                    min_x, min_y, max_x, max_y = get_mesh_2d_bbox(mesh)
                    ax.text((min_x + max_x)/2, (min_y + max_y)/2, name, 
                            color=color, ha='center', va='center', 
                            fontsize=8, fontweight='bold')
                    
            # 2. Draw lines
            lc = LineCollection(all_lines, colors=color, linewidths=4.0)
            ax.add_collection(lc)
            
            # 3. Setup axes
            ax.set_aspect('equal')
            ax.set_xlim(0, sheet_w)
            ax.set_ylim(0, sheet_h)
            ax.axis('off')
            
            plt.subplots_adjust(top=1, bottom=0, right=1, left=0, hspace=0, wspace=0)
            plt.margins(0,0)
            ax.xaxis.set_major_locator(plt.NullLocator())
            ax.yaxis.set_major_locator(plt.NullLocator())

            pdf.savefig(fig, transparent=True, pad_inches=0)
            plt.close(fig)
            
    buf.seek(0)
    return buf