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)
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')
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
38 function Adg
.Primitive
.put_point() print('put_point') end
64 local pair
= Adg
.Pair()
65 local tmp
= Adg
.Pair()
66 local path
= Adg
.Path()
71 path
:set_named_pair('D1I', pair
)
73 pair
.x
= self
.A
- self
.B
- self
.LD2
77 path
:set_named_pair('D2_POS', pair
)
79 pair
.x
= pair
.x
+ (self
.D1
- self
.D2
) / 2
82 path
:set_named_pair('D2I', pair
)
84 pair
.x
= self
.A
- self
.B
88 pair
.x
= self
.A
- self
.B
91 path
:set_named_pair('D3I', pair
)
94 path
:set_named_pair('East', pair
)
97 path
:set_named_pair('West', pair
)
99 path
:chamfer(0.3, 0.3)
101 pair
.x
= self
.A
- self
.B
+ self
.LD3
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)
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
130 path
:set_named_pair('D4F', pair
)
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
151 path
:set_named_pair('D5I', pair
)
153 pair
.x
= self
.A
- self
.C
161 primitive
= path
:over_primitive()
162 primitive
:put_point(0, tmp
)
163 path
:set_named_pair('D5F', tmp
)
167 pair
.x
= pair
.x
+ self
.LD6
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
182 path
:set_named_pair('D67', pair
)
189 path
:set_named_pair('D7F', pair
)
191 path
:reflect_explicit(1, 0)
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
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()
215 path
:set_named_pair('LHOLE', pair
)
217 tmp
.y
= self
.DHOLE
/ 2
218 tmp
.x
= pair
.x
- tmp
.y
/ math
.SQRT3
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)
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()
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)
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')
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
)
287 dim
= adg
.ADim
.new_full_from_model(model
, 'D1F', 'D1I', 'D2I', 'D1F', 'D1F')
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)
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)
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')
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')
323 table.insert(dims
, dim
)
325 dim
= adg
.ADim
.new_full_from_model(model
, 'D4F', 'D4I', 'D5I', 'D4F', 'D4F')
327 dim
:switch_extension2(false)
328 table.insert(dims
, dim
)
332 dim
= adg
.LDim
.new_full_from_model(model
, 'D6F', '-D6F', 'East', 0)
333 dim
:set_limits('-0.1', nil)
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)
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)
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
)
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')
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)
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()))
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
{
407 on_delete_event
= Gtk
.main_quit
,
413 print((arg
[0] or 'Program') .. ' terminated correctly')