api.py 2.48 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')
37
  svgfile = './svg/{}.svg'.format(os.path.splitext(os.path.basename(epsfile))[0])
gijs's avatar
gijs committed
38
  hpglfile = './{}.hpgl'.format(os.path.splitext(epsfile)[0])
gijs's avatar
gijs committed
39

40 41
  shutil.copy(svgfile, './svg/{}.svg'.format(int(time.time()))

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

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

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

  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
63 64 65
      sourcepen = int(chunk[2])
      if not sourcepen in penmap:
        pen += 1
66

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

        penmap[sourcepen] = pen

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

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

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