Thu, 06 Apr 2017 16:21:21 +0200
added 2d image to 3d viewer
11 | 1 | #!/usr/bin/env python |
2 | # -*- coding: UTF-8 -*- | |
14 | 3 | """ |
4 | Web CGI script for 3D View | |
5 | """ | |
11 | 6 | |
7 | import cgi, data, json, config | |
8 | from cylindertransport import CylinderSpacerCalculator | |
18 | 9 | import base64 |
10 | import cStringIO | |
11 | 11 | |
12 | # enable debugging | |
14 | 13 | #import sys |
14 | #sys.stderr = sys.stdout | |
18 | 15 | import cgitb |
16 | cgitb.enable() | |
11 | 17 | |
18 | def do_action(args): | |
14 | 19 | """ |
20 | process some actions (like JSON requests) | |
21 | """ | |
11 | 22 | if args["action"].value == "calculate": |
23 | cylinders = args.getlist("cylinders[]") | |
24 | calc = CylinderSpacerCalculator(cylinders) | |
25 | calc.calculate() | |
18 | 26 | image = calc.render_image() |
27 | buffer = cStringIO.StringIO() | |
28 | image.save(buffer, format="PNG") | |
29 | img_str = base64.b64encode(buffer.getvalue()) | |
11 | 30 | print json.dumps({ |
31 | "objects": calc.circles, | |
18 | 32 | "scale3d": config.SCALE3D, |
33 | "image": img_str | |
11 | 34 | }) |
35 | else: | |
36 | print "unknown Action %s" % args["action"].value | |
37 | ||
14 | 38 | def run(): |
39 | """ | |
40 | Main program, without action displays the html content | |
41 | """ | |
42 | print "Content-Type: text/html;charset=utf-8" | |
43 | print "" | |
11 | 44 | |
14 | 45 | args = cgi.FieldStorage() |
46 | if "action" in args: | |
47 | do_action(args) | |
48 | else: | |
49 | # display the html content | |
50 | content = open("stlviewer.html", "r").read() | |
51 | cyls = "" | |
52 | for cyl in sorted(data.CYLINDER.keys()): | |
53 | cyls += "<li key=\"%s\" weight=\"%s\">%s</li>" % ( | |
54 | cyl, data.CYLINDER[cyl][3], data.CYLINDER[cyl][4]) | |
55 | content = content.replace("<!-- PLACEHOLDER CYLINDERS -->", cyls) | |
11 | 56 | |
14 | 57 | print content |
58 | ||
59 | if __name__ == "__main__": | |
60 | run() |