ADG Lua 

ADG Lua Commit Details

Date:2013-03-21 10:27:45 (6 years 5 months ago)
Author:Nicola Fontana
Branch:master
Commit:ebb308fd73f817cc13c61a66b0c0641025c15577
Parents: 2de1a6fd8257afbc388d8afba021b5751469cf5b
Message:adg-demo: moved data into part.data

Collected part data on its own table, so operations to be applied on the
whole set of data can iterate over this table fields.
Changes:
Madg-demo.lua.in (6 diffs)
Mpiston.lua (13 diffs)

File differences

adg-demo.lua.in
6161
6262
6363
64
65
66
67
6468
6569
6670
......
104108
105109
106110
107
108
109
110
111
112
113111
114112
115113
......
126124
127125
128126
129
130
127
128
131129
132130
133131
......
142140
143141
144142
145
143
146144
147145
148146
......
160158
161159
162160
163
164
165
161
162
163
166164
167165
168
166
169167
170168
171169
......
184182
185183
186184
187
188
185
186
189187
190188
191
189
192190
193191
194192
DGROOVE = 8.3,
LGROOVE = 1,
ZGROOVE = 16,
TITLE = 'SAMPLE DRAWING',
DRAWING = 'PISTON',
AUTHOR = 'adg-demo',
DATE = os.date('%d/%m/%Y'),
}
Adg.gtk_use_default_icons(adg_data_dir)
-- Edit dialog
local widgets = {
'A', 'B', 'C', 'DHOLE', 'LHOLE', 'GROOVE', 'ZGROOVE', 'DGROOVE', 'LGROOVE',
'D1', 'D2', 'LD2', 'D3', 'LD3', 'D4', 'D5', 'LD5', 'D6', 'LD6', 'D7',
'TITLE', 'DRAWING', 'AUTHOR', 'DATE',
}
local function entry_info(widget)
if Gtk.ToggleButton:is_type_of(widget) then
-- Boolean value
end
end
for _, name in pairs(widgets) do
local widget = ui['edit' .. name]
for field in pairs(piston.data) do
local widget = ui['edit' .. field]
if widget then
-- Synchronize GtkSpinButton to GtkAdjustment, that is
-- initialize the widget to its default value
ui.btnApply:set_sensitive(true)
ui.btnReset:set_sensitive(true)
end
piston[name] = widget[getter](widget)
piston.data[field] = widget[getter](widget)
end
end
end
ui.btnApply.on_clicked = function ()
-- Update the piston data using the widgets
for _, name in pairs(widgets) do
local widget = ui['edit' .. name]
-- Refresh the piston data using the widgets
for field in pairs(piston.data) do
local widget = ui['edit' .. field]
local getter = entry_info(widget)
if getter then
piston[name] = widget[getter](widget)
piston.data[field] = widget[getter](widget)
end
end
ui.btnReset.on_clicked = function ()
-- Update the widgets using the piston data
for _, name in pairs(widgets) do
local widget = ui['edit' .. name]
for field, value in pairs(piston.data) do
local widget = ui['edit' .. field]
local _, setter = entry_info(widget)
if setter then
widget[setter](widget, piston[name])
widget[setter](widget, value)
end
end
piston.lua
5757
5858
5959
60
60
6161
6262
6363
64
64
65
66
6567
6668
6769
68
70
6971
7072
71
73
7274
7375
74
75
76
77
7678
7779
7880
79
81
8082
8183
8284
83
84
85
86
8587
8688
8789
88
90
8991
9092
9193
......
9395
9496
9597
96
97
98
99
98100
99101
100102
......
106108
107109
108110
109
111
110112
111113
112114
......
115117
116118
117119
118
120
119121
120
122
121123
122124
123
125
124126
125127
126128
127
129
128130
129131
130132
131133
132
134
133135
134136
135
136
137
138
137139
138140
139
140
141
142
141143
142144
143
144
145
146
145147
146148
147149
148
150
149151
150152
151153
152154
153
155
154156
155157
156158
......
159161
160162
161163
162
164
163165
164166
165167
......
171173
172174
173175
174
175
176
177
176178
177179
178180
179
181
180182
181183
182
184
183185
184186
185187
......
189191
190192
191193
192
194
193195
194196
195197
196
198
197199
198200
199201
200
202
201203
202204
203205
204
206
207
208
205209
206210
207211
208212
209
213
210214
211215
212216
......
215219
216220
217221
218
219
220
221
222
223
224
225
222226
223227
224228
......
280284
281285
282286
283
287
288
289
284290
285
286
287
288
291
292
293
294
289295
290296
291
292297
293298
294299
......
427432
428433
429434
430
435
431436
437
432438
433
439
434440
435441
436442
......
447453
448454
449455
450
456
457
458
459
451460
461
452462
453463
454464
......
463473
464474
465475
476
466477
467478
468479
......
470481
471482
472483
473
484
474485
475486
476487
constructor[model](part, model)
end)
function model.body(piston, path)
function model.body(part, path)
path = path or Adg.Path {}
constructor[path] = model.body
local pair = Cpml.Pair { x = 0, y = piston.D1 / 2 }
local data = part.data
local pair = Cpml.Pair { x = 0, y = data.D1 / 2 }
path:move_to(pair)
path:set_named_pair('D1I', pair)
pair.x = piston.A - piston.B - piston.LD2
pair.x = data.A - data.B - data.LD2
path:line_to(pair)
pair.y = piston.D3 / 2
pair.y = data.D3 / 2
path:set_named_pair('D2_POS', pair)
pair.x = pair.x + (piston.D1 - piston.D2) / 2
pair.y = piston.D2 / 2
pair.x = pair.x + (data.D1 - data.D2) / 2
pair.y = data.D2 / 2
path:line_to(pair)
path:set_named_pair('D2I', pair)
pair.x = piston.A - piston.B
pair.x = data.A - data.B
path:line_to(pair)
path:fillet(0.4)
pair.x = piston.A - piston.B
pair.y = piston.D3 / 2
pair.x = data.A - data.B
pair.y = data.D3 / 2
path:line_to(pair)
path:set_named_pair('D3I', pair)
pair.x = piston.A
pair.x = data.A
path:set_named_pair('East', pair)
pair.x = 0
path:chamfer(0.3, 0.3)
pair.x = piston.A - piston.B + piston.LD3
pair.y = piston.D3 / 2
pair.x = data.A - data.B + data.LD3
pair.y = data.D3 / 2
path:line_to(pair)
local primitive = path:over_primitive()
path:chamfer(0.3, 0.3)
pair.y = piston.D4 / 2
pair.y = data.D4 / 2
path:line_to(pair)
primitive = path:over_primitive()
tmp = primitive:put_point(-1)
path:set_named_pair('D3F_X', tmp)
path:fillet(piston.RD34)
path:fillet(data.RD34)
pair.x = pair.x + piston.RD34
pair.x = pair.x + data.RD34
path:set_named_pair('D4I', pair)
pair.x = piston.A - piston.C - piston.LD5
pair.x = data.A - data.C - data.LD5
path:line_to(pair)
path:set_named_pair('D4F', pair)
pair.y = piston.D3 / 2
pair.y = data.D3 / 2
path:set_named_pair('D4_POS', pair)
primitive = path:over_primitive()
tmp = primitive:put_point(0)
tmp.x = tmp.x + piston.RD34
tmp.x = tmp.x + data.RD34
path:set_named_pair('RD34', tmp)
tmp.x = tmp.x - math.cos(math.pi / 4) * piston.RD34
tmp.y = tmp.y - math.sin(math.pi / 4) * piston.RD34
tmp.x = tmp.x - math.cos(math.pi / 4) * data.RD34
tmp.y = tmp.y - math.sin(math.pi / 4) * data.RD34
path:set_named_pair('RD34_R', tmp)
tmp.x = tmp.x + piston.RD34
tmp.y = tmp.y + piston.RD34
tmp.x = tmp.x + data.RD34
tmp.y = tmp.y + data.RD34
path:set_named_pair('RD34_XY', tmp)
pair.x = pair.x + (piston.D4 - piston.D5) / 2
pair.y = piston.D5 / 2
pair.x = pair.x + (data.D4 - data.D5) / 2
pair.y = data.D5 / 2
path:line_to(pair)
path:set_named_pair('D5I', pair)
pair.x = piston.A - piston.C
pair.x = data.A - data.C
path:line_to(pair)
path:fillet(0.2)
pair.y = piston.D6 / 2
pair.y = data.D6 / 2
path:line_to(pair)
primitive = path:over_primitive()
path:fillet(0.1)
pair.x = pair.x + piston.LD6
pair.x = pair.x + data.LD6
path:line_to(pair)
path:set_named_pair('D6F', pair)
tmp = primitive:put_point(-1)
path:set_named_pair('D6I_Y', tmp)
pair.x = piston.A - piston.LD7
pair.y = pair.y - (piston.C - piston.LD7 - piston.LD6) / SQRT3
pair.x = data.A - data.LD7
pair.y = pair.y - (data.C - data.LD7 - data.LD6) / SQRT3
path:line_to(pair)
path:set_named_pair('D67', pair)
pair.y = piston.D7 / 2
pair.y = data.D7 / 2
path:line_to(pair)
pair.x = piston.A
pair.x = data.A
path:line_to(pair)
path:set_named_pair('D7F', pair)
return path
end
function model.edges(piston, edges)
function model.edges(part, edges)
edges = edges or Adg.Edges {}
constructor[edges] = model.edges
edges:set_source(piston.model.body)
edges:set_source(part.model.body)
return edges
end
function model.hole(piston, path)
function model.hole(part, path)
path = path or Adg.Path {}
constructor[path] = model.hole
local pair = Cpml.Pair {x = piston.LHOLE, y = 0 }
local data = part.data
local pair = Cpml.Pair {x = data.LHOLE, y = 0 }
path:move_to(pair)
path:set_named_pair('LHOLE', pair)
local tmp = Cpml.Pair {}
tmp.y = piston.DHOLE / 2
tmp.y = data.DHOLE / 2
tmp.x = pair.x - tmp.y / SQRT3
path:line_to(tmp)
path:line_to(pair)
path:set_named_pair('DHOLE', pair)
path:line_to_explicit(0, (piston.D1 + piston.DHOLE) / 4)
path:curve_to_explicit(piston.LHOLE / 2, piston.DHOLE / 2,
piston.LHOLE + 2, piston.D1 / 2,
piston.LHOLE + 2, 0)
path:line_to_explicit(0, (data.D1 + data.DHOLE) / 4)
path:curve_to_explicit(data.LHOLE / 2, data.DHOLE / 2,
data.LHOLE + 2, data.D1 / 2,
data.LHOLE + 2, 0)
path:reflect_explicit(1, 0)
path:close()
end
end)
local function add_title_block(canvas, title)
local function add_title_block(canvas, part)
local data = part.data
canvas:set_title_block(Adg.TitleBlock {
title = title or 'Generic title',
author = 'adg-demo',
date = os.date('%d/%m/%Y'),
drawing = 'PISTON',
title = data.TITLE,
author = data.AUTHOR,
date = data.DATE,
drawing = data.DRAWING,
logo = Adg.Logo {},
projection = Adg.Projection { scheme = Adg.ProjectionScheme.FIRST_ANGLE },
scale = '---',
size = 'A4',
})
end
canvas:add(dim)
end
function view.detailed(model)
function view.detailed(part)
local canvas = Adg.Canvas {}
local model = part.model
add_title_block(canvas, 'Detailed view')
add_title_block(canvas, part)
canvas:add(Adg.Stroke { trail = model.body })
canvas:add(Adg.Stroke { trail = model.edges })
canvas:add(Adg.Hatch { trail = model.hole })
local controller = {}
function controller.new(data)
local part = data or {}
local part = {}
-- data: numbers and strings needed to define the whole part
part.data = data or {}
-- model: different models (AdgModel instances) generated from data
part.model = {}
setmetatable(part.model, {
__index = function (self, key)
end
})
-- view: drawings (AdgCanvas) availables for a single set of data
part.view = {}
setmetatable(part.view, {
__index = function (self, key)
if not view[key] then return end
-- Create the view and store it into the cache
self[key] = view[key](part.model)
self[key] = view[key](part)
-- Return the cached result
return self[key]

Archive Download the corresponding diff file

Branches

Tags