api.py 2.5 KB
Newer Older
gijs's avatar
gijs committed
1 2 3 4 5 6 7 8 9 10 11 12
#!/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
13
import shutil
gijs's avatar
gijs committed
14 15 16
from flask import Flask, Response, app, request, render_template

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

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

pen = 1
speed = 10
force = 1

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

app = Flask(__name__)

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

gijs's avatar
gijs committed
41
  shutil.copy(svgfile, './svg/snaps/{}-{}.svg'.format(charnum, int(time.time())))
42

gijs's avatar
gijs committed
43 44
  # converting eps to hpgl
  subprocess.call([
45 46 47 48 49
      'pstoedit',
      '-f',
      'plot-hpgl',
      epsfile,
      hpglfile
gijs's avatar
gijs committed
50 51
  ])

gijs's avatar
gijs committed
52
  handle = open(hpglfile) 
53
  dirtyhpgl = ''.join(handle.readlines()).replace('\n', '')
gijs's avatar
gijs committed
54 55
  cleanhpglchunks = []

gijs's avatar
gijs committed
56 57
  pen = 1
  pencount = 4
gijs's avatar
gijs committed
58
  penmap = {}
gijs's avatar
gijs committed
59 60 61 62 63

  for chunk in dirtyhpgl.split(';'):
    if chunk[0:2] in ['PA', 'PR', 'PU', 'PD']:
      cleanhpglchunks.append(chunk)
    elif chunk[0:2] == 'SP':
gijs's avatar
gijs committed
64 65 66
      sourcepen = int(chunk[2])
      if not sourcepen in penmap:
        pen += 1
67

gijs's avatar
gijs committed
68 69 70 71 72 73
        if pen > pencount:
          pen = 1

        penmap[sourcepen] = pen

      cleanhpglchunks.append('SP{}'.format(penmap[sourcepen]))
gijs's avatar
gijs committed
74 75

  cleanhpgl = ';'.join(cleanhpglchunks)
gijs's avatar
gijs committed
76 77 78

  textbox = Textbox(
      font=font,
gijs's avatar
gijs committed
79 80
      width = 75 * 40,
      position = Coordinate(page['right'] - (80 * 40), page['bottom'] + (12 * 40)),
gijs's avatar
gijs committed
81 82 83 84 85 86
      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
87
  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
88 89 90 91 92 93 94 95
  # 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
96
  app.run(host="localhost", port=5555, debug=True)