forked from MakeMagazinDE/GRBLize
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcam_view.pas
376 lines (336 loc) · 10.8 KB
/
cam_view.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
unit cam_view;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, Buttons, StdCtrls, ComCtrls, Registry, MMsystem,
VFrames;
type
TForm3 = class(TForm)
VideoBox: TPaintBox;
RadioGroupCam: TRadioGroup;
TrackBar1: TTrackBar;
StaticText1: TStaticText;
StaticText6: TStaticText;
OverlayColor: TPanel;
BtnCamAtZero: TSpeedButton;
ColorDialog1: TColorDialog;
Label1: TLabel;
BtnCamAtPoint: TSpeedButton;
Timer1: TTimer;
Label2: TLabel;
BtnMoveCamZero: TSpeedButton;
Label3: TLabel;
Label4: TLabel;
BtnMoveToolZero: TSpeedButton;
BtnMoveCamPoint: TSpeedButton;
BtnMoveToolPoint: TSpeedButton;
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure BtnCamAtPointClick(Sender: TObject);
procedure BtnCamAtZeroClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure OverlayColorClick(Sender: TObject);
procedure RadioGroupCamClick(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
procedure BtnMoveCamZeroClick(Sender: TObject);
procedure BtnMoveCamPointClick(Sender: TObject);
procedure BtnMoveToolPointClick(Sender: TObject);
procedure BtnMoveToolZeroClick(Sender: TObject);
private
{ Private-Deklarationen }
public
{ Public-Deklarationen }
fVideoImage: TVideoImage;
fVideoBitmap: TBitmap;
procedure OnNewVideoFrame(Sender : TObject; Width, Height: integer; DataPtr: pointer);
end;
var
Form3: TForm3;
fActivated, fCamPresent, CamIsOn : boolean;
overlay_color: Tcolor;
implementation
uses grbl_player_main, import_files, drawing_window, grbl_com, glscene_view;
{$R *.dfm}
procedure TForm3.RadioGroupCamClick(Sender: TObject);
begin
case RadioGroupCam.ItemIndex of
0:
begin
Label1.Caption:=' Webcam/Video Device off';
if fActivated then begin
CamIsOn:= false;
fActivated := false;
fVideoImage.VideoStop;
end;
end;
1:
if fCamPresent then begin
Label1.Caption:=' Initializing Webcam...';
Application.ProcessMessages;
if not fActivated then
fVideoImage.VideoStart(DeviceList[0]);
fActivated:= true;
CamIsOn:= true;
end else
RadioGroupCam.ItemIndex:= 0;
end;
Repaint;
end;
procedure TForm3.OnNewVideoFrame(Sender : TObject; Width, Height: integer; DataPtr: pointer);
var
r : integer;
bm_center_x, bm_center_y: Integer;
begin
// Retreive latest video image
if not fActivated then
exit;
fVideoImage.GetBitmap(fVideoBitmap);
with fVideoBitmap do begin
// Paint a crosshair onto video image
bm_center_x:= VideoBox.width div 2;
bm_center_y:= VideoBox.height div 2;
Canvas.Brush.Style := bsClear;
Canvas.Pen.Width := 1;
Canvas.Pen.Color:= overlay_color;
Canvas.moveto(0, bm_center_y);
Canvas.lineto(Width, bm_center_y);
Canvas.moveto(bm_center_x, 0);
Canvas.lineto(bm_center_x, Height);
r := (VideoBox.height * TrackBar1.Position div 256);
Canvas.ellipse(bm_center_x -r, bm_center_y -r,
bm_center_x +r, bm_center_y +r);
VideoBox.Canvas.Draw(0, 0, fVideoBitmap);
end;
end;
procedure TForm3.OverlayColorClick(Sender: TObject);
begin
ColorDialog1.Color:= OverlayColor.Color;
if not ColorDialog1.Execute then Exit;
OverlayColor.Color:= ColorDialog1.Color;
overlay_color:= OverlayColor.Color;
end;
procedure TForm3.FormCreate(Sender: TObject);
var
grbl_ini:TRegistry;
begin
grbl_ini:= TRegistry.Create;
try
grbl_ini.RootKey := HKEY_CURRENT_USER;
grbl_ini.OpenKey('SOFTWARE\Make\GRBlize\'+c_VerStr,true);
if grbl_ini.ValueExists('CamFormTop') then
Top:= grbl_ini.ReadInteger('CamFormTop');
if grbl_ini.ValueExists('CamFormLeft') then
Left:= grbl_ini.ReadInteger('CamFormLeft');
if grbl_ini.ValueExists('CamOn') then
CamIsOn:= grbl_ini.ReadBool('CamOn');
{
if grbl_ini.ValueExists('CamFormVisible') then
form_visible:= grbl_ini.ReadBool('CamFormVisible');
} finally
grbl_ini.Free;
end;
fActivated:= false;
// Create instance of our video image class.
fVideoImage:= TVideoImage.Create;
// Tell fVideoImage where to paint the images it receives from the camera
// (Only in case we do not want to modify the images by ourselves)
fVideoImage.SetDisplayCanvas(VideoBox.Canvas);
fVideoBitmap:= TBitmap.create;
fVideoBitmap.Height:= VideoBox.Height;
fVideoBitmap.Width:= VideoBox.Width;
// Create instance of our video image class.
fVideoImage:= TVideoImage.Create;
// Tell fVideoImage where to paint the images it receives from the camera
// (Only in case we do not want to modify the images by ourselves)
fVideoImage.SetDisplayCanvas(VideoBox.Canvas);
overlay_color:= OverlayColor.Color;
DeviceList := TStringList.Create;
fVideoImage.GetListOfDevices(DeviceList);
if DeviceList.Count < 1 then begin
// If no camera has been found, terminate program
fCamPresent:= false;
DeviceList.Free;
RadioGroupCam.ItemIndex:= 0;
Label1.Caption:='No Webcam/Video Device found';
CamIsOn:= false;
end else begin
fCamPresent:= true;
fVideoImage:= TVideoImage.Create;
fVideoImage.OnNewVideoFrame := OnNewVideoFrame;
Label1.Caption:=' Webcam/Video Device off';
if CamIsOn then
// RadioGroupCam.ItemIndex:= 1;
end;
end;
procedure TForm3.FormClose(Sender: TObject; var Action: TCloseAction);
var
grbl_ini:TRegistry;
begin
grbl_ini:= TRegistry.Create;
try
grbl_ini.RootKey := HKEY_CURRENT_USER;
grbl_ini.OpenKey('SOFTWARE\Make\GRBlize\'+c_VerStr, true);
grbl_ini.WriteInteger('CamFormTop',Top);
grbl_ini.WriteInteger('CamFormLeft',Left);
grbl_ini.WriteBool('CamOn', CamIsOn);
finally
grbl_ini.Free;
end;
if fCamPresent then begin
if fActivated then
fVideoImage.VideoStop;
end;
fActivated := false;
//Form1.ShowSpindleCam1.Checked:= false;
end;
// #############################################################################
procedure TForm3.BtnCamAtZeroClick(Sender: TObject);
begin
WaitForIdle;
Form1.Memo1.lines.add('');
Form1.Memo1.lines.add('Offset cam to part zero');
grbl_offsXY(-job.cam_x, -job.cam_y);
SendListToGrbl;
WorkZero.X:= grbl_mpos.X + job.cam_x;
Jog.X:= WorkZero.X;
WorkZero.Y:= grbl_mpos.Y + job.cam_x;
Jog.Y:= WorkZero.Y;
WorkZeroXdone:= true;
WorkZeroYdone:= true;
NeedsRedraw:= true;
end;
procedure TForm3.BtnCamAtPointClick(Sender: TObject);
var x,y: Double;
begin
if (HilitePoint < 0) and (HiliteBlock < 0) then
exit;
Form1.Memo1.lines.add('');
if HilitePoint >= 0 then begin
Form1.Memo1.lines.add('Offset cam to point');
hilite_to(x,y);
end else begin
Form1.Memo1.lines.add('Offset cam to center');
hilite_center_to(x,y);
end;
x:= x - job.cam_x;
y:= y - job.cam_y;
grbl_offsXY(x, y);
SendListToGrbl;
WorkZero.X:= grbl_mpos.X - x;
Jog.X:= WorkZero.X;
WorkZero.Y:= grbl_mpos.Y - y;
Jog.Y:= WorkZero.Y;
WorkZeroXdone:= true;
WorkZeroYdone:= true;
NeedsRedraw:= true;
end;
// #############################################################################
procedure TForm3.BtnMoveCamPointClick(Sender: TObject);
var x,y: Double;
begin
if (HilitePoint < 0) and (HiliteBlock < 0) then
exit;
Form1.Memo1.lines.add('');
if HilitePoint >= 0 then begin
Form1.Memo1.lines.add('Move cam to point');
hilite_to(x,y);
end else begin
Form1.Memo1.lines.add('Move cam to center');
hilite_center_to(x, y);
end;
x:= x - job.cam_x;
y:= y - job.cam_y;
if WorkZeroXdone and WorkZeroYdone then begin
grbl_moveZ(0, true); // move Z up
grbl_moveXY(x, y, false);
grbl_moveZ(job.cam_z_abs, true);
SendListToGrbl;
end else begin
Form1.Memo1.lines.add('WARNING: X,Y Zero not set!');
PlaySound('SYSTEMHAND', 0, SND_ASYNC);
end;
end;
procedure TForm3.BtnMoveCamZeroClick(Sender: TObject);
begin
Form1.Memo1.lines.add('');
Form1.Memo1.lines.add('Move cam to part zero');
if WorkZeroXdone and WorkZeroYdone then begin
grbl_moveZ(0, true); // move Z up absolute
grbl_moveXY(-job.cam_x,-job.cam_y, false);
grbl_moveZ(job.cam_z_abs, true);
SendListToGrbl;
end else begin
Form1.Memo1.lines.add('WARNING: X,Y Zero not set!');
PlaySound('SYSTEMHAND', 0, SND_ASYNC);
end;
end;
procedure TForm3.BtnMoveToolPointClick(Sender: TObject);
var x,y: Double;
begin
Form1.Memo1.lines.add('');
if HilitePoint >= 0 then begin
Form1.Memo1.lines.add('Move tool to point');
hilite_to(x,y);
end else begin
Form1.Memo1.lines.add('Move tool to center');
hilite_center_to(x,y);
end;
if WorkZeroXdone and WorkZeroYdone then begin
grbl_moveZ(0, true); // move Z up absolute
grbl_moveXY(x, y, false);
if WorkZeroAllDone then begin
grbl_moveZ(job.z_penlift, false);
end else begin
Form1.Memo1.lines.add('WARNING: Z Zero not set!');
PlaySound('SYSTEMHAND', 0, SND_ASYNC);
end;
SendListToGrbl;
end else begin
Form1.Memo1.lines.add('WARNING: X,Y Zero not set!');
PlaySound('SYSTEMHAND', 0, SND_ASYNC);
end;
SendListToGrbl;
end;
procedure TForm3.BtnMoveToolZeroClick(Sender: TObject);
begin
Form1.Memo1.lines.add('');
Form1.Memo1.lines.add('Move tool to part zero');
if WorkZeroXdone and WorkZeroYdone then begin
grbl_moveZ(0, true); // move Z up absolute
grbl_moveXY(0,0, false);
if WorkZeroAllDone then begin
grbl_moveZ(job.z_penlift, false);
end else begin
Form1.Memo1.lines.add('WARNING: Z Zero not set!');
PlaySound('SYSTEMHAND', 0, SND_ASYNC);
end;
SendListToGrbl;
end else begin
Form1.Memo1.lines.add('WARNING: X,Y Zero not set!');
PlaySound('SYSTEMHAND', 0, SND_ASYNC);
end;
end;
// #############################################################################
procedure TForm3.Timer1Timer(Sender: TObject);
begin
if CamIsOn and (RadioGroupCam.ItemIndex = 0) then
RadioGroupCam.ItemIndex:= 1;
if (HilitePoint < 0) and (HiliteBlock < 0) then begin
BtnCamAtPoint.Enabled:= false;
BtnMoveToolPoint.Enabled:= false;
BtnMoveCamPoint.Enabled:= false;
end else begin
BtnCamAtPoint.Enabled:= true;
BtnMoveToolPoint.Enabled:= true;
BtnMoveCamPoint.Enabled:= true;
if HilitePoint >= 0 then begin
BtnCamAtPoint.Caption:= 'Hilite Point';
BtnMoveCamPoint.Caption:= 'Hilite Point';
BtnMoveToolPoint.Caption:= 'Hilite Point';
end else begin
BtnCamAtPoint.Caption:= 'Object Center';
BtnMoveCamPoint.Caption:= 'Object Center';
BtnMoveToolPoint.Caption:= 'Object Center';
end;
end;
end;
end.