api.py 2.18 KB
Newer Older
gijs's avatar
gijs committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#!/usr/bin/env python2
# -*- coding: utf-8 -*-

from datetime import datetime
import subprocess
import time
from plottertools.shapes import box
from plottertools.font import Font
from plottertools.textbox import Textbox
from chiplotle.geometry.core.coordinate import Coordinate
from settings import db
import os.path
from flask import Flask, Response, app, request, render_template

marginleft = 30 # margin left in mm
gijs's avatar
gijs committed
16
marginbottom = (271.75 / 3) # margin bottom in mm
gijs's avatar
gijs committed
17 18

page = {
gijs's avatar
gijs committed
19 20 21 22
  'left': int(marginleft * -40),
  'right': int((190 - marginleft) * 40),
  'bottom': int(marginbottom * -40),
  'top': int((271.75 - marginbottom) * 40)
gijs's avatar
gijs committed
23 24 25 26 27 28
}

pen = 1
speed = 10
force = 1

gijs's avatar
gijs committed
29
font=Font(path='fonts/converted/futural.fnt', resolution=1, scale=4.5)
gijs's avatar
gijs committed
30 31 32 33 34 35

app = Flask(__name__)

@app.route("/api/ploteps/")
def ploteps():
  epsfile = request.args.get('epsfile')
gijs's avatar
gijs committed
36
  hpglfile = './{}.hpgl'.format(os.path.splitext(epsfile)[0])
gijs's avatar
gijs committed
37 38 39

  # converting eps to hpgl
  subprocess.call([
40 41 42 43 44
      'pstoedit',
      '-f',
      'plot-hpgl',
      epsfile,
      hpglfile
gijs's avatar
gijs committed
45 46
  ])

gijs's avatar
gijs committed
47
  handle = open(hpglfile) 
48
  dirtyhpgl = ''.join(handle.readlines()).replace('\n', '')
gijs's avatar
gijs committed
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
  cleanhpglchunks = []

  pencount = 3

  for chunk in dirtyhpgl.split(';'):
    if chunk[0:2] in ['PA', 'PR', 'PU', 'PD']:
      cleanhpglchunks.append(chunk)
    elif chunk[0:2] == 'SP':
      pen += 1

      if pen > pencount:
        pen = 1

      cleanhpglchunks.append('SP{}'.format(pen))

  cleanhpgl = ';'.join(cleanhpglchunks)
gijs's avatar
gijs committed
65 66 67

  textbox = Textbox(
      font=font,
gijs's avatar
gijs committed
68 69
      width = 75 * 40,
      position = Coordinate(page['right'] - (80 * 40), page['bottom'] + (12 * 40)),
gijs's avatar
gijs committed
70 71 72 73 74 75
      align = Textbox.alignLeft,
      lineHeight=1.40
  )

  textbox.insertText('{0} \n{1} \n\ntype.code, Maison du Livre, Bruxelles'.format(hpglfile, datetime.now().strftime("%d-%m-%Y %H:%M")))

gijs's avatar
gijs committed
76
  hpgl = 'IN;RO90;IP0,0,7600,10870;SC{0},{1},{2},{3};FS{5};SP{4};VS{6};PU;{7};PU;'.format(page['left'], page['right'], page['bottom'], page['top'], pen, force, speed, cleanhpgl)
gijs's avatar
gijs committed
77 78 79 80 81 82 83 84
  # hpgl += box((page['left'] + 200, page['bottom'] + 200), 200 * 40, 287 * 40)
  hpgl += textbox.hpgl()

  db.plots.insert({'hpgl': hpgl, 'plotted': False})

  return '100'

if __name__ == "__main__":
gijs's avatar
gijs committed
85
  app.run(host="localhost", port=5555, debug=True)