ADG Lua 

ADG Lua Commit Details

Date:2013-03-18 21:53:44 (6 years 5 months ago)
Author:Nicola Fontana
Branch:master
Commit:a9846a05458b398e829cd9c7dc0504cef186c6eb
Parents: c1129ebc5e2d68e39f876d48dfd09a0333b27a17
Message:Refactored dimensions in piston.lua

Dimensions are not a model: moved into the view section and stripped
from the model table.
Changes:
Mpiston.lua (3 diffs)

File differences

piston.lua
219219
220220
221221
222
223
224
225
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
226276
227277
278
228279
229280
230281
231282
232
283
233284
234285
235286
236287
237
288
238289
239290
240291
241
292
242293
243294
244295
245
296
246297
247298
248
299
249300
250301
251
302
252303
253304
254305
255306
256307
257
308
258309
259310
260311
261312
262313
263
314
315
264316
265317
266318
267319
268320
269321
270
322
271323
272324
273325
274326
275
327
276328
277329
278330
279331
280
332
281333
282334
283335
284336
285
337
286338
287339
288340
289341
290342
291
343
292344
293345
294346
295347
296348
297349
298
350
299351
300352
301353
302354
303
355
304356
305357
306358
307359
308
360
361
309362
310363
311364
......
313366
314367
315368
316
369
317370
318371
319372
320373
321
374
322375
323376
324377
325378
326379
327
380
328381
329382
330383
331
384
385
332386
333387
334388
335389
336390
337391
338
392
339393
340394
341395
342396
343397
344
398
345399
346400
347401
348402
349403
350
351
352
404
353405
354406
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406407
407408
408409
......
421422
422423
423424
424
425
425426
426427
427428
return path
end
function model.dimensions(piston)
local body = piston.model.body
local hole = piston.model.hole
local dims = {}
-- VIEW
-----------------------------------------------------------------
local view = {}
-- Inject the export method into Adg.Canvas
rawset(Adg.Canvas, 'export', function (canvas, file)
-- The export format is guessed from the file suffix
local suffix = file:match('%..*$')
local size = canvas:get_size()
size.x = size.x + canvas:get_left_margin() + canvas:get_right_margin()
size.y = size.y + canvas:get_top_margin() + canvas:get_bottom_margin()
-- Create the cairo surface
local surface
if suffix == '.png' and cairo.ImageSurface then
surface = cairo.ImageSurface.create(cairo.Format.RGB24, size.x, size.y)
elseif suffix == '.svg' and cairo.SvgSurface then
surface = cairo.SvgSurface.create(file, size.x, size.y)
elseif suffix == '.pdf' and cairo.PdfSurface then
surface = cairo.PdfSurface.create(file, size.x, size.y)
elseif suffix == '.ps' and cairo.PsSurface then
-- Pull request: http://github.com/pavouk/lgi/pull/46
surface = cairo.PsSurface.create(file, size.x, size.y)
surface:dsc_comment('%%Title: adg-lua demonstration program')
surface:dsc_comment('%%Copyright: Copyleft (C) 2013 Fontana Nicola')
surface:dsc_comment('%%Orientation: Portrait')
surface:dsc_begin_setup()
surface:dsc_begin_page_setup()
surface:dsc_comment('%%IncludeFeature: *PageSize A4')
end
if not surface then return nil, 'Requested format not supported' end
-- Render the canvas content
local cr = cairo.Context.create(surface);
canvas:render(cr)
local status
if cairo.Surface.get_type(surface) == 'IMAGE' then
status = cairo.Surface.write_to_png(surface, file)
else
cr:show_page()
status = cr.status
end
if status ~= 'SUCCESS' then
return nil, cairo.Status.to_string(cairo.Status[status])
end
end)
local function add_dimensions(canvas, model)
local body = model.body
local hole = model.hole
local dim
-- North
dim = Adg.LDim.new_full_from_model(body, '-D3I_X', '-D3F_X', '-D3F_Y', -math.pi/2)
dim:set_outside(Adg.ThreeState.OFF)
table.insert(dims, dim)
canvas:add(dim)
dim = Adg.LDim.new_full_from_model(body, '-D6I_X', '-D67', '-East', -math.pi/2)
dim:set_level(0)
dim:switch_extension1(false)
table.insert(dims, dim)
canvas:add(dim)
dim = Adg.LDim.new_full_from_model(body, '-D6I_X', '-D7F', '-East', -math.pi/2)
dim:set_limits('-0.06', nil)
table.insert(dims, dim)
canvas:add(dim)
dim = Adg.ADim.new_full_from_model(body, '-D6I_Y', '-D6F', '-D6F', '-D67', '-D6F')
dim:set_level(2)
table.insert(dims, dim)
canvas:add(dim)
dim = Adg.RDim.new_full_from_model(body, '-RD34', '-RD34_R', '-RD34_XY')
table.insert(dims, dim)
canvas:add(dim)
dim = Adg.LDim.new_full_from_model(body, '-DGROOVEI_X', '-DGROOVEF_X', '-DGROOVEX_POS', -math.pi/2)
table.insert(dims, dim)
canvas:add(dim)
dim = Adg.LDim.new_full_from_model(body, 'D2I', '-D2I', '-D2_POS', math.pi)
dim:set_limits('-0.1', nil)
dim:set_outside(Adg.ThreeState.OFF)
dim:set_value('\226\140\128 <>')
table.insert(dims, dim)
canvas:add(dim)
dim = Adg.LDim.new_full_from_model(body, 'DGROOVEI_Y', '-DGROOVEI_Y', '-DGROOVEY_POS', math.pi)
dim:set_limits('-0.1', nil)
dim:set_outside(Adg.ThreeState.OFF)
dim:set_value('\226\140\128 <>')
table.insert(dims, dim)
canvas:add(dim)
-- South
dim = Adg.ADim.new_full_from_model(body, 'D1F', 'D1I', 'D2I', 'D1F', 'D1F')
dim:set_level(2)
dim:switch_extension2(false)
table.insert(dims, dim)
canvas:add(dim)
dim = Adg.LDim.new_full_from_model(body, 'D1I', nil, 'West', math.pi / 2)
dim:set_ref2_from_model(hole, '-LHOLE')
dim:switch_extension1(false)
table.insert(dims, dim)
canvas:add(dim)
dim = Adg.LDim.new_full_from_model(body, 'D1I', 'DGROOVEI_X', 'West', math.pi / 2)
dim:switch_extension1(false)
dim:set_level(2)
table.insert(dims, dim)
canvas:add(dim)
dim = Adg.LDim.new_full_from_model(body, 'D4F', 'D6I_X', 'D4_POS', math.pi / 2)
dim:set_limits(nil, '+0.2')
dim:set_outside(Adg.ThreeState.OFF)
table.insert(dims, dim)
canvas:add(dim)
dim = Adg.LDim.new_full_from_model(body, 'D1F', 'D3I_X', 'D2_POS', math.pi / 2)
dim:set_level(2)
dim:switch_extension2(false)
dim:set_outside(Adg.ThreeState.OFF)
table.insert(dims, dim)
canvas:add(dim)
dim = Adg.LDim.new_full_from_model(body, 'D3I_X', 'D7F', 'East', math.pi / 2)
dim:set_limits(nil, '+0.1')
dim:set_level(2)
dim:set_outside(Adg.ThreeState.OFF)
dim:switch_extension2(false)
table.insert(dims, dim)
canvas:add(dim)
dim = Adg.LDim.new_full_from_model(body, 'D1I', 'D7F', 'D3F_Y', math.pi / 2)
dim:set_limits('-0.05', '+0.05')
dim:set_level(3)
table.insert(dims, dim)
canvas:add(dim)
dim = Adg.ADim.new_full_from_model(body, 'D4F', 'D4I', 'D5I', 'D4F', 'D4F')
dim:set_level(1.5)
dim:switch_extension2(false)
table.insert(dims, dim)
canvas:add(dim)
-- East
dim:set_limits('-0.1', nil)
dim:set_level(4)
dim:set_value('\226\140\128 <>')
table.insert(dims, dim)
canvas:add(dim)
dim = Adg.LDim.new_full_from_model(body, 'D4F', '-D4F', 'East', 0)
dim:set_level(3)
dim:set_value('\226\140\128 <>')
table.insert(dims, dim)
canvas:add(dim)
dim = Adg.LDim.new_full_from_model(body, 'D5F', '-D5F', 'East', 0)
dim:set_limits('-0.1', nil)
dim:set_level(2)
dim:set_value('\226\140\128 <>')
table.insert(dims, dim)
canvas:add(dim)
dim = Adg.LDim.new_full_from_model(body, 'D7F', '-D7F', 'East', 0)
dim:set_value('\226\140\128 <>')
table.insert(dims, dim)
canvas:add(dim)
-- West
dim = Adg.LDim.new_full_from_model(hole, 'DHOLE', '-DHOLE', nil, math.pi)
dim:set_pos_from_model(body, '-West')
dim:set_value('\226\140\128 <>')
table.insert(dims, dim)
canvas:add(dim)
dim = Adg.LDim.new_full_from_model(body, 'D1I', '-D1I', '-West', math.pi)
dim:set_limits('-0.05', '+0.05')
dim:set_level(2)
dim:set_value('\226\140\128 <>')
table.insert(dims, dim)
canvas:add(dim)
dim = Adg.LDim.new_full_from_model(body, 'D3I_Y', '-D3I_Y', '-West', math.pi)
dim:set_limits('-0.25', nil)
dim:set_level(3)
dim:set_value('\226\140\128 <>')
table.insert(dims, dim)
return dims
canvas:add(dim)
end
-- VIEW
-----------------------------------------------------------------
local view = {}
-- Inject the export method into Adg.Canvas
rawset(Adg.Canvas, 'export', function (canvas, file)
-- The export format is guessed from the file suffix
local suffix = file:match('%..*$')
local size = canvas:get_size()
size.x = size.x + canvas:get_left_margin() + canvas:get_right_margin()
size.y = size.y + canvas:get_top_margin() + canvas:get_bottom_margin()
-- Create the cairo surface
local surface
if suffix == '.png' and cairo.ImageSurface then
surface = cairo.ImageSurface.create(cairo.Format.RGB24, size.x, size.y)
elseif suffix == '.svg' and cairo.SvgSurface then
surface = cairo.SvgSurface.create(file, size.x, size.y)
elseif suffix == '.pdf' and cairo.PdfSurface then
surface = cairo.PdfSurface.create(file, size.x, size.y)
elseif suffix == '.ps' and cairo.PsSurface then
-- Pull request: http://github.com/pavouk/lgi/pull/46
surface = cairo.PsSurface.create(file, size.x, size.y)
surface:dsc_comment('%%Title: adg-lua demonstration program')
surface:dsc_comment('%%Copyright: Copyleft (C) 2013 Fontana Nicola')
surface:dsc_comment('%%Orientation: Portrait')
surface:dsc_begin_setup()
surface:dsc_begin_page_setup()
surface:dsc_comment('%%IncludeFeature: *PageSize A4')
end
if not surface then return nil, 'Requested format not supported' end
-- Render the canvas content
local cr = cairo.Context.create(surface);
canvas:render(cr)
local status
if cairo.Surface.get_type(surface) == 'IMAGE' then
status = cairo.Surface.write_to_png(surface, file)
else
cr:show_page()
status = cr.status
end
if status ~= 'SUCCESS' then
return nil, cairo.Status.to_string(cairo.Status[status])
end
end)
function view.detailed(model)
local canvas = Adg.Canvas {
title_block = Adg.TitleBlock {
canvas:add(Adg.Stroke { trail = model.edges })
canvas:add(Adg.Hatch { trail = model.hole })
canvas:add(Adg.Stroke { trail = model.hole })
for _, dim in pairs(model.dimensions) do canvas:add(dim) end
add_dimensions(canvas, model)
return canvas
end

Archive Download the corresponding diff file

Branches

Tags