ADG Lua 

ADG Lua Git Source Tree

Root/adg-web.lua

1--[[
2
3This file is part of adg-lua.
4Copyright (C) 2012-2013 Nicola Fontana <ntd at entidi.it>
5
6adg-lua is free software; you can redistribute it and/or modify
7it under the terms of the GNU Lesser General Public License as
8published by the Free Software Foundation; either version 2 of
9the License, or (at your option) any later version.
10
11adg-lua is distributed in the hope that it will be useful, but
12WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU Lesser General Public License for more details.
15
16You should have received a copy of the GNU Lesser General
17Public License along with adg-lua; if not, write to the Free
18Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19Boston, MA 02110-1301, USA.
20
21]]
22
23
24-- Convenient function to load scripts from the same directory
25-- of this very same file
26local function local_require(filename)
27 local FILE = arg[0]
28 local basedir = FILE and FILE:match('^(.*/).*$') or './'
29 local filename = basedir .. filename .. '.lua'
30 return assert(loadfile(filename)())
31end
32
33
34local lgi = require 'lgi'
35local cairo = lgi.require 'cairo'
36local Adg = lgi.require 'Adg'
37local Piston = local_require 'piston'
38
39
40
41-- Command line parsing
42-----------------------------------------------------------------
43
44
45local request = {}
46for n, s in ipairs(arg) do
47 -- Arguments are expected in k=v tuples
48 local k, v = s:match('(.-)=(.*)')
49 if not k then error('Invalid argument ' .. n) end
50 request[k] = v
51end
52
53-- Provide fallback values
54for k, v in pairs {
55 A = 55,
56 B = 20.6,
57 C = 2,
58 DHOLE = 2,
59 LHOLE = 3,
60 D1 = 9.3,
61 D2 = 6.5,
62 D3 = 13.8,
63 D4 = 6.5,
64 D5 = 4.5,
65 D6 = 7.2,
66 D7 = 2,
67 RD34 = 1,
68 RD56 = 0.2,
69 LD2 = 7,
70 LD3 = 3.5,
71 LD5 = 5,
72 LD6 = 1,
73 LD7 = 0.5,
74 GROOVE = false,
75 ZGROOVE = 16,
76 DGROOVE = 8.3,
77 LGROOVE = 1,
78 CHAMFER = 0.3,
79
80 -- Metadata
81 TITLE = 'SAMPLE DRAWING',
82 DRAWING = 'PISTON',
83 AUTHOR = 'adg-web',
84 DATE = os.date('%d/%m/%Y'),
85} do
86 if not request[k] then request[k] = v end
87end
88
89
90-- Part definition
91-----------------------------------------------------------------
92
93local piston = Piston.new(request)
94
95
96-- Canvas settings
97-----------------------------------------------------------------
98
99local n = 1
100local canvas = piston.view.detailed
101canvas:set_margins(10, 10, 10, 10)
102canvas:set_paddings(0, 0, 0, 0)
103canvas:set_size_explicit(800, 600)
104
105-- Adjust the zoom factor based on the (optional) requested size
106local zoom = 1
107if request.width then
108 local x_zoom = request.width / 800
109 if x_zoom < zoom then zoom = x_zoom end
110end
111if request.height then
112 local y_zoom = request.height / 600
113 if y_zoom < zoom then zoom = y_zoom end
114end
115canvas:set_global_map(cairo.Matrix { xx = zoom, yy = zoom })
116
117piston:refresh()
118canvas:autoscale()
119
120
121-- File generation
122-----------------------------------------------------------------
123
124local _, err = canvas:export('/dev/stdout', 'png')
125if err then error(err) end

Archive Download this file

Branches

Tags