Skip to content

Instantly share code, notes, and snippets.

@AlexMarshall12
Created March 10, 2013 02:17
Show Gist options
  • Save AlexMarshall12/5126797 to your computer and use it in GitHub Desktop.
Save AlexMarshall12/5126797 to your computer and use it in GitHub Desktop.
import shortuuid
@view_config(route_name='home_page', renderer='templates/edit.pt')
def home_page(request):
if 'form.submitted' in request.params:
name= request.params['name']
input_file=request.POST['stl'].file
vertices, normals = [],[]
for line in input_file:
parts = line.split()
if parts[0] == 'vertex':
vertices.append(map(float, parts[1:4]))
elif parts[0] == 'facet':
normals.append(map(float, parts[2:5]))
ordering=[]
N=len(normals)
for i in range(0,N):
p1=vertices[3*i]
p2=vertices[3*i+1]
p3=vertices[3*i+2]
x1=p1[0]
y1=p1[1]
z1=p1[2]
x2=p2[0]
y2=p2[1]
z2=p2[2]
x3=p3[0]
y3=p3[1]
z3=p3[2]
a=[x2-x1,y2-y1,z2-z1]
b=[x3-x1,y3-y1,z3-z1]
a1=x2-x1
a2=y2-y1
a3=z2-z1
b1=x3-x1
b2=y3-y1
b3=z3-z1
normal=normals[i]
cross_vector=[a2*b3-a3*b2,a3*b1-a1*b3,a1*b2-a2*b1]
dot=reduce( operator.add, map( operator.mul, cross_vector, normal))
if dot>0:
ordering.append([3*i,3*i+1,3*i+2])
else:
ordering.append([3*i,3*i+1,3*i+2])
data=[vertices,ordering]
jsdata=json.dumps(data)
renderer_dict = dict(name=name,data=jsdata)
url=shortuuid.uuid()
html_string = render('tutorial:templates/view.pt', renderer_dict, request=request)
s3=boto.connect_s3(aws_access_key_id = 'AKIAIJEXF25B6H5F4L7Q', aws_secret_access_key = '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' )
bucket=s3.get_bucket('alexmarshalltest')
k=Key(bucket)
k.key='%(url)s' % {'url':url}
k.set_contents_from_string(html_string, headers={'Content-Type': 'text/html'})
k.set_acl('public-read')
return HTTPFound(location="https://s3.amazonaws.com/alexmarshalltest/%(url)s" % {'url':url})
return {}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment