summaryrefslogtreecommitdiff
path: root/gerbonara/cad/protoserve.py
blob: 0bf9dce0703c5f1e4d0196edf7fbbb210a4efb00 (plain)
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#!/usr/bin/env python

import importlib.resources
from tempfile import TemporaryDirectory
from pathlib import Path

from quart import Quart, request, Response

from . import protoboard as pb
from . import protoserve_data
from ..utils import MM, Inch


def extract_importlib(package):
    root = TemporaryDirectory()

    stack = [(importlib.resources.files(package), Path(root.name))]
    while stack:
        res, out = stack.pop()

        for item in res.iterdir():
            item_out = out / item.name
            if item.is_file():
                item_out.write_bytes(item.read_bytes())
            else:
                assert item.is_dir()
                item_out.mkdir()
                stack.push((item, item_out))

    return root

static_folder = extract_importlib(protoserve_data)
app = Quart(__name__, static_folder=static_folder.name)

@app.route('/')
async def index():
    return await app.send_static_file('protoserve.html')

def deserialize(obj, unit):
    pitch_x = float(obj.get('pitch_x', 1.27))
    pitch_y = float(obj.get('pitch_y', 1.27))
    clearance = float(obj.get('clearance', 0.2))

    match obj['type']:
        case 'layout':
            proportions = [float(child['layout_prop']) for child in obj['children']]
            content = [deserialize(child, unit) for child in obj['children']]
            return pb.PropLayout(content, obj['direction'], proportions)

        case 'placeholder':
            return pb.EmptyProtoArea()

        case 'smd':
            match obj['pad_shape']:
                case 'rect':
                    pad = pb.SMDPad.rect(0, 0, pitch_x-clearance, pitch_y-clearance, paste=False, unit=unit)
                case 'circle':
                    pad = pb.SMDPad.circle(0, 0, min(pitch_x, pitch_y)-clearance, paste=False, unit=unit)
            return pb.PatternProtoArea(pitch_x, pitch_y, obj=pad, unit=unit)

        case 'tht':
            hole_dia = float(obj['hole_dia'])
            match obj['plating']:
                case 'plated':
                    oneside, plated = False, True
                case 'nonplated':
                    oneside, plated = False, False
                case 'singleside':
                    oneside, plated = True, False

            match obj['pad_shape']:
                case 'rect':
                    pad = pb.THTPad.rect(0, 0, hole_dia, pitch_x-clearance, pitch_y-clearance, paste=False, plated=plated, unit=unit)
                case 'circle':
                    pad = pb.THTPad.circle(0, 0, hole_dia, min(pitch_x, pitch_y)-clearance, paste=False, plated=plated, unit=unit)
                case 'obround':
                    pad = pb.THTPad.obround(0, 0, hole_dia, pitch_x-clearance, pitch_y-clearance, paste=False, plated=plated, unit=unit)

            if oneside:
                pad.pad_bottom = None

            return pb.PatternProtoArea(pitch_x, pitch_y, obj=pad, unit=unit)

        case 'manhattan':
            return pb.PatternProtoArea(pitch_x, pitch_y, obj=pb.ManhattanPads(pitch_x, pitch_y, clearance, unit=unit), unit=unit)

        case 'powered':
            pitch = float(obj.get('pitch', 2.54))
            hole_dia = float(obj['hole_dia'])
            via_drill = float(obj['via_hole_dia'])
            trace_width = float(obj['trace_width'])
            return pb.PatternProtoArea(pitch, pitch, pb.PoweredProto(pitch, hole_dia, clearance, via_size=via_drill, trace_width=trace_width, unit=unit), unit=unit)

        case 'flower':
            pitch = float(obj.get('pitch', 2.54))
            hole_dia = float(obj['hole_dia'])
            pattern_dia = float(obj['pattern_dia'])
            return pb.PatternProtoArea(2*pitch, 2*pitch, pb.THTFlowerProto(pitch, hole_dia, pattern_dia, unit=unit), unit=unit)

        case 'rf':
            pitch = float(obj.get('pitch', 2.54))
            hole_dia = float(obj['hole_dia'])
            via_dia = float(obj['via_dia'])
            via_drill = float(obj['via_hole_dia'])
            return pb.PatternProtoArea(pitch, pitch, pb.RFGroundProto(pitch, hole_dia, clearance, via_dia, via_drill, unit=MM), unit=MM)

@app.route('/preview.svg', methods=['POST'])
async def preview():
    obj = await request.get_json()

    unit = Inch if obj.get('units' == 'us') else MM
    w = float(obj.get('width', unit(100, MM)))
    h = float(obj.get('height', unit(80, MM)))
    corner_radius = float(obj.get('round_corners', {}).get('radius', unit(1.5, MM)))
    holes = obj.get('mounting_holes', {})
    mounting_hole_dia = float(holes.get('diameter', unit(3.2, MM)))
    mounting_hole_offset = float(holes.get('offset', unit(5, MM)))

    content = deserialize(obj['children'][0], unit)

    board = pb.ProtoBoard(w, h, content,
                       corner_radius=corner_radius,
                       mounting_hole_dia=mounting_hole_dia,
                       mounting_hole_offset=mounting_hole_offset,
                       unit=unit)
    return Response(str(board.pretty_svg()), mimetype='image/svg+xml')


if __name__ == '__main__':
    app.run()