Created
April 7, 2015 09:33
-
-
Save CapnKernel/aadea8530771485a6daa to your computer and use it in GitHub Desktop.
Generating a stencil border
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
from decimal import Decimal | |
from copy import copy | |
from django.shortcuts import render | |
from django.contrib import messages | |
from django.contrib.auth.decorators import login_required | |
from .forms import StencilBorderForm | |
from groups import group_required | |
unit_conversions = { | |
'cm': Decimal(10), | |
'mm': Decimal(1), | |
'in': Decimal(25.4), | |
'mil': Decimal(25.4)/1000, | |
} | |
def convert_to_mm(length, unit): | |
return Decimal(length) * unit_conversions[unit] | |
def convert_from_mm(length, unit): | |
return Decimal(length) / unit_conversions[unit] | |
# TODO: Handle up is +ve/-ve | |
# TODO: Generate a gerber file | |
# TODO: Do the translation in the gerber file | |
@login_required | |
@group_required('backend') | |
def stencil_border(request): | |
# GET: Show "stencil param" form | |
# POST: Accept and validate stencil info, then create it. | |
data = {} | |
if request.method == 'POST': | |
# print "order_change: in POST" | |
form = StencilBorderForm(request.POST) | |
if form.is_valid(): | |
print "stencil_border: form is valid" | |
print "form.cleaned_data=", form.cleaned_data | |
# error = False | |
board_size_x = convert_to_mm(form.cleaned_data['board_size_x'], form.cleaned_data['board_size_units']) | |
board_size_y = convert_to_mm(form.cleaned_data['board_size_y'], form.cleaned_data['board_size_units']) | |
board_tl_x = convert_to_mm(form.cleaned_data['board_tl_x'], form.cleaned_data['board_tl_units']) | |
board_tl_y = convert_to_mm(form.cleaned_data['board_tl_y'], form.cleaned_data['board_tl_units']) | |
stencil_size_x = convert_to_mm(form.cleaned_data['stencil_size_x'], form.cleaned_data['stencil_size_units']) | |
stencil_size_y = convert_to_mm(form.cleaned_data['stencil_size_y'], form.cleaned_data['stencil_size_units']) | |
# print "pre: ssx=", stencil_size_x, "ssy=", stencil_size_y | |
if form.cleaned_data['stencil_size_x'].startswith('+'): | |
stencil_size_x = (stencil_size_x * 2) + board_size_x | |
if form.cleaned_data['stencil_size_y'].startswith('+'): | |
stencil_size_y = (stencil_size_y * 2) + board_size_y | |
# print "post: ssx=", stencil_size_x, "ssy=", stencil_size_y | |
board_middle_x = board_tl_x + (board_size_x / 2) | |
board_middle_y = board_tl_y - (board_size_y / 2) | |
stencil_middle_x = stencil_size_x / 2 | |
stencil_middle_y = -stencil_size_y / 2 | |
stencil_translation_x = board_middle_x - stencil_middle_x | |
stencil_translation_y = stencil_middle_y - board_middle_y | |
radius = 5 | |
# Top left, top right, bottom left, bottom right | |
tl = {'x': stencil_translation_x, 'y': stencil_translation_y} | |
tr = {'x': stencil_translation_x + stencil_size_x, 'y': stencil_translation_y} | |
bl = {'x': stencil_translation_x, 'y': stencil_translation_y + stencil_size_y} | |
br = {'x': stencil_translation_x + stencil_size_x, 'y': stencil_translation_y + stencil_size_y} | |
# tle=top left then east, brw=bottom right then west | |
tle = copy(tl); tle['x'] += radius | |
tls = copy(tl); tls['y'] += radius | |
trw = copy(tr); trw['x'] -= radius | |
trs = copy(tr); trs['y'] += radius | |
ble = copy(bl); ble['x'] += radius | |
bln = copy(bl); bln['y'] -= radius | |
brw = copy(br); brw['x'] -= radius | |
brn = copy(br); brn['y'] -= radius | |
data = { | |
'form': form, | |
'board_middle_x': convert_from_mm(board_middle_x, form.cleaned_data['board_middle_units']), | |
'board_middle_y': convert_from_mm(board_middle_y, form.cleaned_data['board_middle_units']), | |
'stencil_middle_x': convert_from_mm(stencil_middle_x, form.cleaned_data['stencil_middle_units']), | |
'stencil_middle_y': convert_from_mm(stencil_middle_y, form.cleaned_data['stencil_middle_units']), | |
'stencil_size_x': convert_from_mm(stencil_size_x, form.cleaned_data['stencil_translation_units']), | |
'stencil_size_y': convert_from_mm(stencil_size_y, form.cleaned_data['stencil_translation_units']), | |
'tl': tl, | |
'bl': bl, | |
'tle': tle, | |
'tls': tls, | |
'trw': trw, | |
'trs': trs, | |
'ble': ble, | |
'bln': bln, | |
'brw': brw, | |
'brn': brn, | |
} | |
# No point redirecting, as we haven't changed anything. | |
# return HttpResponseRedirect(reverse('backend.views.order', args={order.hash})) | |
else: | |
messages.error(request, 'Invalid data') | |
else: | |
data['form'] = StencilBorderForm() | |
return render(request, 'backend/stencil-border.html', data) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment