protoserve: Gerber download works

This commit is contained in:
jaseg 2023-04-09 17:53:54 +02:00
parent c9dff5450f
commit f74bd30c0f
2 changed files with 44 additions and 9 deletions

View file

@ -1,10 +1,10 @@
#!/usr/bin/env python
import importlib.resources
from tempfile import TemporaryDirectory
from tempfile import NamedTemporaryFile, TemporaryDirectory
from pathlib import Path
from quart import Quart, request, Response
from quart import Quart, request, Response, send_file
from . import protoboard as pb
from . import protoserve_data
@ -104,10 +104,7 @@ def deserialize(obj, unit):
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()
def to_board(obj):
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)))
@ -118,13 +115,27 @@ async def preview():
content = deserialize(obj['children'][0], unit)
board = pb.ProtoBoard(w, h, content,
return pb.ProtoBoard(w, h, content,
corner_radius=corner_radius,
mounting_hole_dia=mounting_hole_dia,
mounting_hole_offset=mounting_hole_offset,
unit=unit)
@app.route('/preview.svg', methods=['POST'])
async def preview():
obj = await request.get_json()
board = to_board(obj)
return Response(str(board.pretty_svg()), mimetype='image/svg+xml')
@app.route('/gerbers.zip', methods=['POST'])
async def gerbers():
obj = await request.get_json()
board = to_board(obj)
with NamedTemporaryFile(suffix='.zip') as f:
f = Path(f.name)
board.layer_stack().save_to_zipfile(f)
return Response(f.read_bytes(), mimetype='image/svg+xml')
if __name__ == '__main__':
app.run()

View file

@ -268,7 +268,7 @@ input {
<div id="links">
<a href="#controls">Settings</a>
<a href="#preview">Preview</a>
<a href='/download'>
<a id="link-gerbers" href='#'>
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512" width="1em">
<title>Download</title>
<!--! Font Awesome Pro 6.4.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license (Commercial License) Copyright 2023 Fonticons, Inc. --><path d="M288 32c0-17.7-14.3-32-32-32s-32 14.3-32 32V274.7l-73.4-73.4c-12.5-12.5-32.8-12.5-45.3 0s-12.5 32.8 0 45.3l128 128c12.5 12.5 32.8 12.5 45.3 0l128-128c12.5-12.5 12.5-32.8 0-45.3s-32.8-12.5-45.3 0L288 274.7V32zM64 352c-35.3 0-64 28.7-64 64v32c0 35.3 28.7 64 64 64H448c35.3 0 64-28.7 64-64V416c0-35.3-28.7-64-64-64H346.5l-45.3 45.3c-25 25-65.5 25-90.5 0L165.5 352H64zm368 56a24 24 0 1 1 0 48 24 24 0 1 1 0-48z"/>
@ -863,7 +863,7 @@ input {
}
}
previewBlobURL = null;
let previewBlobURL = null;
previewReloader = new RateLimiter(async () => {
const response = await fetch('preview.svg', {
method: 'POST',
@ -892,6 +892,30 @@ input {
});
}
let downloadObjectURL = null;
document.querySelector('#link-gerbers').addEventListener('click', async () => {
const response = await fetch('gerbers.zip', {
method: 'POST',
mode: 'same-origin',
cache: 'no-cache',
headers: {'Content-Type': 'application/json'},
body: serialize(),
});
const data = await response.blob();
/* cf. https://gist.github.com/devloco/5f779216c988438777b76e7db113d05c */
const zipBlob = new Blob([data], { type: 'application/zip' });
if (downloadObjectURL) {
URL.revokeObjectURL(downloadObjectURL);
}
downloadObjectURL = URL.createObjectURL(zipBlob);
let link = document.createElement('a');
link.href = downloadObjectURL;
link.download = 'gerbers.zip';
link.click();
});
hookupPreviewUpdate(document.querySelector('.group.board'));
previewReloader.scheduleCall();
</script>