Initial demo using LGI bindings
[adg-lua.git] / adg-demo.lua
blob252cf3e81d37bfa062e6e6a9486574fdafd704c5
1 #! /usr/bin/env lua
3 local lgi = require 'lgi'
4 local cairo = lgi.require 'cairo'
5 local Gtk = lgi.require 'Gtk'
6 local Cpml = lgi.require 'Cpml'
7 local Adg = lgi.require 'Adg'
9 math.SQRT3 = math.sqrt(3)
12 -- ADG overrides
14 local function boxed_wrapper(boxed, struct)
15 -- Merge struct methods, giving precence to the boxed ones
16 local method = rawget(boxed, '_method') or {}
17 boxed._method = struct._method
18 for k, v in pairs(method) do boxed._method[k] = v end
20 -- Set struct properties on the boxed
21 boxed._field = rawget(struct, '_field')
22 boxed._size = rawget(struct, '_size')
23 end
25 boxed_wrapper(Adg.Pair, Cpml.Pair)
26 boxed_wrapper(Adg.Primitive, Cpml.Primitive)
27 --Adg.Matrix = cairo.Matrix
30 -- Adjust ADG inheritances
31 --Adg.Pair._parent = Cpml.Pair
32 --Adg.Primitive._parent = Cpml.Primitive
33 --Adg.Matrix = cairo.Matrix
36 -- DEFINING THE MODEL
38 function Adg.Primitive.put_point() print('put_point') end
40 local part = {
41 A = 62.35,
42 B = 20.6,
43 D1 = 9.3,
44 D2 = 7.5,
45 LD2 = 7,
46 D3 = 12.4,
47 LD3 = 3.5,
48 RD34 = 1,
49 D4 = 6.5,
50 D5 = 4.5,
51 LD5 = 4.5,
52 D6 = 7.2,
53 LD6 = 1,
54 C = 2,
55 D7 = 2.5,
56 LD7 = 0.5,
57 LHOLE = 12.5,
58 DHOLE = 3,
60 cache = {}
63 function part:model()
64 local pair = Adg.Pair()
65 local tmp = Adg.Pair()
66 local path = Adg.Path()
68 pair.x = 0
69 pair.y = self.D1 / 2
70 path:move_to(pair)
71 path:set_named_pair('D1I', pair)
73 pair.x = self.A - self.B - self.LD2
74 path:line_to(pair)
76 pair.y = self.D3 / 2
77 path:set_named_pair('D2_POS', pair)
79 pair.x = pair.x + (self.D1 - self.D2) / 2
80 pair.y = self.D2 / 2
81 path:line_to(pair)
82 path:set_named_pair('D2I', pair)
84 pair.x = self.A - self.B
85 path:line_to(pair)
86 path:fillet(0.4)
88 pair.x = self.A - self.B
89 pair.y = self.D3 / 2
90 path:line_to(pair)
91 path:set_named_pair('D3I', pair)
93 pair.x = self.A
94 path:set_named_pair('East', pair)
96 pair.x = 0
97 path:set_named_pair('West', pair)
99 path:chamfer(0.3, 0.3)
101 pair.x = self.A - self.B + self.LD3
102 pair.y = self.D3 / 2
103 path:line_to(pair)
105 local primitive = path:over_primitive()
106 primitive:put_point(0, tmp)
107 path:set_named_pair('D3I_X', tmp)
109 primitive:put_point(-1, tmp)
110 path:set_named_pair('D3I_Y', tmp)
112 path:chamfer(0.3, 0.3)
114 pair.y = self.D4 / 2
115 path:line_to(pair)
117 primitive = path:over_primitive()
118 primitive:put_point(0, tmp)
119 path:set_named_pair('D3F_Y', tmp)
120 primitive:put_point(-1, tmp)
121 path:set_named_pair('D3F_X', tmp)
123 path:fillet(self.RD34)
125 pair.x = pair.x + self.RD34
126 path:set_named_pair('D4I', pair)
128 pair.x = self.A - self.C - self.LD5
129 path:line_to(pair)
130 path:set_named_pair('D4F', pair)
132 pair.y = self.D3 / 2
133 path:set_named_pair('D4_POS', pair)
135 primitive = path:over_primitive()
136 primitive:put_point(0, tmp)
137 tmp.x = tmp.x + self.RD34
138 path:set_named_pair('RD34', tmp)
140 tmp.x = tmp.x - math.cos(math.pi / 4) * self.RD34
141 tmp.y = tmp.y - math.sin(math.pi / 4) * self.RD34
142 path:set_named_pair('RD34_R', tmp)
144 tmp.x = tmp.x + self.RD34
145 tmp.y = tmp.y + self.RD34
146 path:set_named_pair('RD34_XY', tmp)
148 pair.x = pair.x + (self.D4 - self.D5) / 2
149 pair.y = self.D5 / 2
150 path:line_to(pair)
151 path:set_named_pair('D5I', pair)
153 pair.x = self.A - self.C
154 path:line_to(pair)
156 path:fillet(0.2)
158 pair.y = self.D6 / 2
159 path:line_to(pair)
161 primitive = path:over_primitive()
162 primitive:put_point(0, tmp)
163 path:set_named_pair('D5F', tmp)
165 path:fillet(0.1)
167 pair.x = pair.x + self.LD6
168 path:line_to(pair)
169 path:set_named_pair('D6F', pair)
171 primitive = path:over_primitive()
172 primitive:put_point(0, tmp)
173 path:set_named_pair('D6I_X', tmp)
175 primitive = path:over_primitive()
176 primitive:put_point(-1, tmp)
177 path:set_named_pair('D6I_Y', tmp)
179 pair.x = self.A - self.LD7
180 pair.y = pair.y - (self.C - self.LD7 - self.LD6) / math.SQRT3
181 path:line_to(pair)
182 path:set_named_pair('D67', pair)
184 pair.y = self.D7 / 2
185 path:line_to(pair)
187 pair.x = self.A
188 path:line_to(pair)
189 path:set_named_pair('D7F', pair)
191 path:reflect_explicit(1, 0)
192 path:close()
194 return path
197 --[=[
198 function part:edges()
199 if not self.cache.edges then
200 self.cache.edges = adg.Edges.new_with_source(self:model())
203 return self.cache.edges
206 function part:hole()
207 if not self.cache.hole then
208 local pair = adg.Pair.new()
209 local tmp = adg.Pair.new()
210 local path = adg.Path.new()
212 pair.x = self.LHOLE
213 pair.y = 0
214 path:move_to(pair)
215 path:set_named_pair('LHOLE', pair)
217 tmp.y = self.DHOLE / 2
218 tmp.x = pair.x - tmp.y / math.SQRT3
219 path:line_to(tmp)
221 pair.x = 0
222 pair.y = tmp.y
223 path:line_to(pair)
224 path:set_named_pair('DHOLE', pair)
226 path:line_to_explicit(0, (self.D1 + self.DHOLE) / 4)
227 path:curve_to_explicit(self.LHOLE / 2, self.DHOLE / 2, self.LHOLE + 2, self.D1 / 2, self.LHOLE + 2, 0)
228 path:reflect_explicit(1, 0)
229 path:close()
231 path:move_to(tmp)
232 tmp.y = -tmp.y
233 path:line_to(tmp)
235 self.cache.hole = path
238 return self.cache.hole
241 function part:dimensions()
242 if not self.cache.dimensions then
243 local model = self:model()
244 local hole = self:hole()
245 local dims = {}
246 local dim
248 -- North
250 dim = adg.LDim.new_full_from_model(model, '-D3I_X', '-D3F_X', '-D3F_Y', -math.pi/2)
251 dim:set_outside(adg.THREE_STATE_OFF)
252 table.insert(dims, dim)
254 dim = adg.LDim.new_full_from_model(model, '-D6I_X', '-D67', '-East', -math.pi/2)
255 dim:set_level(0)
256 dim:switch_extension1(false)
257 table.insert(dims, dim)
259 dim = adg.LDim.new_full_from_model(model, '-D6I_X', '-D7F', '-East', -math.pi/2)
260 dim:set_limits('-0.06', nil)
261 table.insert(dims, dim)
263 dim = adg.ADim.new_full_from_model(model, '-D6I_Y', '-D6F', '-D6F', '-D67', '-D6F')
264 dim:set_level(2)
265 table.insert(dims, dim)
267 dim = adg.RDim.new_full_from_model(model, '-RD34', '-RD34_R', '-RD34_XY')
268 table.insert(dims, dim)
270 dim = adg.LDim.new_full_from_model(model, '-DGROOVEI_X', '-DGROOVEF_X', '-DGROOVEX_POS', -math.pi/2)
271 table.insert(dims, dim)
273 dim = adg.LDim.new_full_from_model(model, 'D2I', '-D2I', '-D2_POS', math.pi)
274 dim:set_limits('-0.1', nil)
275 dim:set_outside(adg.THREE_STATE_OFF)
276 dim:set_value('\226\140\128 <>')
277 table.insert(dims, dim)
279 dim = adg.LDim.new_full_from_model(model, 'DGROOVEI_Y', '-DGROOVEI_Y', '-DGROOVEY_POS', math.pi)
280 dim:set_limits('-0.1', nil)
281 dim:set_outside(adg.THREE_STATE_OFF)
282 dim:set_value('\226\140\128 <>')
283 table.insert(dims, dim)
285 -- South
287 dim = adg.ADim.new_full_from_model(model, 'D1F', 'D1I', 'D2I', 'D1F', 'D1F')
288 dim:set_level(2)
289 dim:switch_extension2(false)
290 table.insert(dims, dim)
292 dim = adg.LDim.new_full_from_model(model, 'D1I', nil, 'West', math.pi / 2)
293 dim:set_ref2_from_model(hole, '-LHOLE')
294 dim:switch_extension1(false)
295 table.insert(dims, dim)
297 dim = adg.LDim.new_full_from_model(model, 'D1I', 'DGROOVEI_X', 'West', math.pi / 2)
298 dim:switch_extension1(false)
299 dim:set_level(2)
300 table.insert(dims, dim)
302 dim = adg.LDim.new_full_from_model(model, 'D4F', 'D6I_X', 'D4_POS', math.pi / 2)
303 dim:set_limits(nil, '+0.2')
304 dim:set_outside(adg.THREE_STATE_OFF)
305 table.insert(dims, dim)
307 dim = adg.LDim.new_full_from_model(model, 'D1F', 'D3I_X', 'D2_POS', math.pi / 2)
308 dim:set_level(2)
309 dim:switch_extension2(false)
310 dim:set_outside(adg.THREE_STATE_OFF)
311 table.insert(dims, dim)
313 dim = adg.LDim.new_full_from_model(model, 'D3I_X', 'D7F', 'East', math.pi / 2)
314 dim:set_limits(nil, '+0.1')
315 dim:set_level(2)
316 dim:set_outside(adg.THREE_STATE_OFF)
317 dim:switch_extension2(false)
318 table.insert(dims, dim)
320 dim = adg.LDim.new_full_from_model(model, 'D1I', 'D7F', 'D3F_Y', math.pi / 2)
321 dim:set_limits('-0.05', '+0.05')
322 dim:set_level(3)
323 table.insert(dims, dim)
325 dim = adg.ADim.new_full_from_model(model, 'D4F', 'D4I', 'D5I', 'D4F', 'D4F')
326 dim:set_level(1.5)
327 dim:switch_extension2(false)
328 table.insert(dims, dim)
330 -- East
332 dim = adg.LDim.new_full_from_model(model, 'D6F', '-D6F', 'East', 0)
333 dim:set_limits('-0.1', nil)
334 dim:set_level(4)
335 dim:set_value('\226\140\128 <>')
336 table.insert(dims, dim)
338 dim = adg.LDim.new_full_from_model(model, 'D4F', '-D4F', 'East', 0)
339 dim:set_level(3)
340 dim:set_value('\226\140\128 <>')
341 table.insert(dims, dim)
343 dim = adg.LDim.new_full_from_model(model, 'D5F', '-D5F', 'East', 0)
344 dim:set_limits('-0.1', nil)
345 dim:set_level(2)
346 dim:set_value('\226\140\128 <>')
347 table.insert(dims, dim)
349 dim = adg.LDim.new_full_from_model(model, 'D7F', '-D7F', 'East', 0)
350 dim:set_value('\226\140\128 <>')
351 table.insert(dims, dim)
353 -- West
355 dim = adg.LDim.new_full_from_model(hole, 'DHOLE', '-DHOLE', nil, math.pi)
356 dim:set_pos_from_model(model, '-West')
357 dim:set_value('\226\140\128 <>')
358 table.insert(dims, dim)
360 dim = adg.LDim.new_full_from_model(model, 'D1I', '-D1I', '-West', math.pi)
361 dim:set_limits('-0.05', '+0.05')
362 dim:set_level(2)
363 dim:set_value('\226\140\128 <>')
364 table.insert(dims, dim)
366 dim = adg.LDim.new_full_from_model(model, 'D3I_Y', '-D3I_Y', '-West', math.pi)
367 dim:set_limits('-0.25', nil)
368 dim:set_level(3)
369 dim:set_value('\226\140\128 <>')
370 table.insert(dims, dim)
372 self.cache.dimensions = dims
375 return self.cache.dimensions
380 -- POPULATING THE CANVAS
382 local canvas = Adg.Canvas {
383 --local_map = Adg.Matrix { x0 = 140, y0 = 180, xx = 8, yy = 8 },
385 canvas:set_paper('iso_a4', Gtk.PageOrientation.LANDSCAPE)
386 canvas:add(Adg.Stroke.new(part:model()))
388 --[=[
389 canvas:add(Adg.Stroke.new(part:edges()))
390 canvas:add(Adg.Hatch.new(part:hole()))
391 canvas:add(Adg.Stroke.new(part:hole()))
392 for _, dim in pairs(part:dimensions()) do canvas:add(dim) end
396 -- THE RENDERING PROCESS
398 local window = Gtk.Window {
399 type = Gtk.WindowType.TOPLEVEL,
400 window_position = Gtk.WindowPosition.CENTER,
401 child = Gtk.ScrolledWindow {
402 child = Adg.GtkLayout {
403 canvas = canvas,
404 autozoom = true,
407 on_delete_event = Gtk.main_quit,
410 window:show_all()
411 Gtk.main()
413 print((arg[0] or 'Program') .. ' terminated correctly')