-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathladdergraphunit.pas
More file actions
619 lines (568 loc) · 19.9 KB
/
laddergraphunit.pas
File metadata and controls
619 lines (568 loc) · 19.9 KB
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
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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
unit laddergraphunit;
{$mode ObjFPC}{$H+}
interface
uses
Classes, SysUtils,usimplegraph,ComCtrls,ExtCtrls,Graphics, Dialogs,controls,forms,
buttons,laddersymbol,lad2pas,menus,LMessages
;
type
TGraphError=(errNone,errSingle,errNoVar);
type TLadderGraph = class(TEvsSimpleGraph)
protected
ftabsheet : TTabSheet;
fpanel : TPanel;
fname : string;
faowner : TForm;
fform : TForm;
fsurface : TPageControl;
fstartInsert : boolean;
fcurrentLink : TEvsGraphLink;
fListeSymbole : TEvsGraphObjectList;
fVisibleVar : TStringList;
fconvertisseur : TladToPas;
fobjectUnderMouse : TEvsGraphObject;
fgraphChanged : boolean;
fRectBeginDrag : TRect;
fOldMouseX : integer;
fSymbolError : string;
fOldVisibleBounds : TRect;
procedure DoNodeMoveResize(aNode: TEvsGraphNode); override;
procedure DoObjectMouseEnter(aGraphObject: TEvsGraphObject);override;
procedure DoObjectMouseLeave(aGraphObject: TEvsGraphObject);override;
procedure mouseup(Sender: TObject;Button: TMouseButton;Shift: TShiftState; X, Y: Integer);
procedure mouseDown(Sender: TObject;Button: TMouseButton;Shift: TShiftState; X, Y: Integer);
procedure mouseMove(Sender: TObject; Shift: TShiftState;X, Y: Integer);
procedure objectInserted(Graph: TEvsSimpleGraph; GraphObject: TEvsGraphObject);
procedure objectRemoved(Graph: TEvsSimpleGraph; GraphObject: TEvsGraphObject);
procedure objectChanged(Graph: TEvsSimpleGraph; GraphObject: TEvsGraphObject);
procedure objectClick(Graph: TEvsSimpleGraph;aGraphObject: TEvsGraphObject);
procedure onClickEvent(sender:TObject);
procedure keyDown(sender:TObject;var key:word;shiftState:TShiftState);
procedure commandModeChange(Sender : TObject);
procedure dblClick(grapheCurrent:TEvsSimpleGraph;objet:TEvsGraphObject);
procedure objectBeginDrag(graphe:TEvsSimpleGraph;objet:TEvsGraphObject;ht:DWord);
procedure ObjectEndDrag(graphe:TEvsSimpleGraph;objet:TEvsGraphObject;ht:DWord;value : boolean);
procedure showTab(sender:TObject);
procedure graphChange(Sender:TObject);
procedure showErrorForm(message:string;x,y:integer);
procedure askInsert(lien:TEvsGraphLink;symbole:TSymbol);
public
constructor create(aOwner:TForm;surface:TPageControl;nom:string;visibility:boolean);
constructor create(aOwner:TForm;nom:string);
procedure expandGraphOnPanel;
procedure deleteSymbol;
procedure renameSymbol;
function checkGraph:TGraphError; // check error in graph
procedure findInGraph(varName:string); // find a var name in this graph
procedure unHighLightGraph; // clear highlighting of symbol
property name:string read fname write fname; // get name of graph
property convertisseur : TladToPas read fconvertisseur;
property objectUnderMouse : TEvsGraphObject read fobjectUnderMouse;
property graphChanged:boolean read fgraphChanged write FgraphChanged;
property graphTabSheet:TTabSheet read ftabsheet;
property SymbolError:string read fSymbolError;
// var related with visible symbol on the graph
property VisibleVar:TStringlist read fVisibleVar;
end;
implementation
uses main;
constructor TLadderGraph.create(aOwner:TForm;surface:TPageControl;nom:string;visibility:boolean);
begin
inherited create(aowner);
faowner:=aowner;
fsurface:=surface;
fstartInsert:=false;
self.color:=clBtnFace;
self.SnapToGrid:=true;
// création de l'onglet
ftabsheet:=surface.AddTabSheet;
fname:=nom;
ftabsheet.caption:=fname;
ftabsheet.OnShow:=@showTab;
// création du fpanel dans l'onglet
fpanel:=TPanel.create(ftabsheet);
fpanel.Parent:=ftabsheet;
fpanel.Top:=0;
fpanel.left:=0;
fpanel.height:=surface.height;
fpanel.width:=surface.width;
// définitions du graphe
self.parent:=fpanel;
self.GridSize:=8;
self.Left:=fpanel.Left;
self.top:=fpanel.top;
self.AnchorSide[akLeft].side:=asrRight;
self.AnchorSide[akLeft].Control :=surface;
self.Anchors := self.Anchors + [akLeft];
self.SetBounds(fpanel.Left, fpanel.Top, fpanel.Width-10, fpanel.Height-40);
self.FixedScrollBars:=true;
self.MinNodeSize:=2;
//evenements
self.OnMouseUp:=@mouseup;
self.OnMouseDown:=@mouseDown;
self.OnMouseMove:=@mouseMove;
self.OnObjectInsert:=@objectInserted;
self.OnObjectDblClick:=@dblClick;
self.OnObjectClick:=@objectClick;
self.OnKeyDown:=@keyDown;
self.OnCommandModeChange:=@commandModeChange;
self.OnObjectRemove:=@objectRemoved;
self.OnObjectChange:=@objectChanged;
self.OnObjectBeginDrag:=@objectBeginDrag;
self.OnObjectEndDrag:=@objectEndDrag;
self.OnClick:=@onClickEvent;
self.OnGraphChange:=@graphChange;
fOldVisibleBounds:=VisibleBounds;
TControl(self).ShowHint:=true;
// creation de la liste des symboles
fListeSymbole:=TEvsGraphObjectList.create;
// list of var related to visible symbol on the screen
fVisibleVar:=TStringList.create;
if not visibility then
ftabsheet.TabVisible:=false
else
ftabsheet.TabVisible:=true;
self.graphChanged:=false;
end;
// simplified constructor for virtual graph
constructor TLadderGraph.create(aOwner:TForm;nom:string);
begin
inherited create(aowner);
faowner:=aowner;
// creation de la liste des symboles
fListeSymbole:=TEvsGraphObjectList.create;
end;
procedure TLadderGraph.DoNodeMoveResize(aNode: TEvsGraphNode);
var
i : integer;
xpos,ypos : integer ;
begin
Inherited;
// mise à jour des positions des symboles
if self.Objects.Count>0 then
for i:=0 to self.Objects.Count-1 do
if (aNode=self.Objects[i]) and
(aNode.isNode) and
(aNode.ClassParent.ClassName<>'TEvsEllipticNode') and
(aNode.ClassName<>'TAllias') then
begin
xpos:=aNode.left;
ypos:=aNode.top;
if (xpos>10) and (xpos<1410) then
begin
if (self.Objects[i] is TSymbol) then
TSymbol(self.Objects[i]).updatePosition(xpos,ypos)
else
if not(self.Objects[i] is TConnexion) then
begin
TEvsRectangularNode(self.Objects[i]).Top:=ypos;
TEvsRectangularNode(self.Objects[i]).left:=xpos;
end;
end;
if xpos<=10 then aNode.Left:=18;
if xpos>=1410 then anode.left:=1401;
end;
end;
procedure TLadderGraph.DoObjectMouseEnter(aGraphObject: TEvsGraphObject);
var
oldGraphChange : boolean;
begin
inherited;
oldGraphChange:=graphChanged;
fobjectUnderMouse:=aGraphObject;
if aGraphObject.IsNode then
// si point de connexion
if aGraphObject.ClassParent.ClassName='TEvsEllipticNode' then
begin
TEvsEllipticNode(aGraphObject).Brush.Color:=clRed;
fstartInsert:=true;
if not(oldGraphChange) then graphChanged:=false;
end;
end;
procedure TLadderGraph.DoObjectMouseLeave(aGraphObject: TEvsGraphObject);
var
oldGraphChange : boolean;
begin
Inherited;
fobjectUnderMouse:=nil;
if aGraphObject.IsNode then
// si point de connexion
if aGraphObject.ClassParent.ClassName='TEvsEllipticNode' then
begin
fstartInsert:=false;
TEvsEllipticNode(aGraphObject).Brush.Color:=clWhite;
if not(oldGraphChange) then graphChanged:=false;
end;
end;
procedure TLadderGraph.mouseup(Sender: TObject;Button: TMouseButton;Shift: TShiftState; X, Y: Integer);
var
i : integer;
allTerminal : boolean;
testObj,selObj : TEvsGraphObject;
begin
cursor:=crDefault;
Refresh;
if self.SelectedObjects.Count>0 then
if self.SelectedObjects[0].IsLink then
// si insertion de lien en cours
begin
fcurrentlink:=TEvsGraphLink(self.SelectedObjects[0]);
// link non connecté d'un coté
if fcurrentLink.HookedPointCount<2 then
begin
showErrorForm('Liaison non valide',fcurrentLink.BoundsRect.Left,fcurrentLink.BoundsRect.top);
exit;
end;
// link connecté entre deux entrées ou deux sorties
if fcurrentLink.HookedPointCount=2 then
begin
if (TConnexion(fcurrentLink.HookedObjectOf(0)).isInput and TConnexion(fcurrentLink.HookedObjectOf(1)).isInput) then
begin
showErrorForm('link can not connect 2 input',fcurrentLink.BoundsRect.Left,fcurrentLink.BoundsRect.top);
exit;
end;
if (not(TConnexion(fcurrentLink.HookedObjectOf(0)).isInput) and not(TConnexion(fcurrentLink.HookedObjectOf(1)).isInput)) then
begin
showErrorForm('link can not connect 2 output',fcurrentLink.BoundsRect.Left,fcurrentLink.BoundsRect.top);
exit;
end;
if (TConnexion(fcurrentLink.HookedObjectOf(0)).isInput) then
begin
showErrorForm('link should be connected from symbol output to symbol input',fcurrentLink.BoundsRect.Left,fcurrentLink.BoundsRect.top);
exit;
end;
if (TConnexion(fcurrentLink.HookedObjectOf(1)).symbolOwner.isTerminal) then
if (Tconnexion(fcurrentLink.HookedObjectOf(0)).LinkOutputCount>1) then
begin
allTerminal:=true;
for i:=0 to Tconnexion(fcurrentLink.HookedObjectOf(0)).LinkOutputCount-1 do
if not (TConnexion(Tconnexion(fcurrentLink.HookedObjectOf(0)).LinkOutputs[i].HookedObjectOf(1)).symbolOwner.isTerminal) then
allTerminal:=false;
if not allTerminal then
begin
showErrorForm('terminal can not be connected here',fcurrentLink.BoundsRect.Left,fcurrentLink.BoundsRect.top);
exit;
end;
end;
end;
end; //----------------
// test while moving symbols
if SelectedObjects.Count>0 then
for i:=0 to self.ObjectsCount()-1 do
begin
testObj := Objects.Items[i];
selObj := SelectedObjects[0];
if testObj.IsLink then
if self.SelectedObjectsCount(TEvsGraphNode)>0 then
if (testObj.ContainsRect(selObj.BoundsRect)) and
(selObj.ClassName<>'TConnexion') and
(selObj.ClassName<>'TAllias') and
(TConnexion(TEvsGraphLink(testObj).HookedObjectOf(1)).symbolOwner<>selObj)
then
askInsert(TEvsGraphLink(testObj),TSymbol(selObj));
end;
end;
procedure TLadderGraph.mouseDown(Sender: TObject;Button: TMouseButton;Shift: TShiftState; X, Y: Integer);
begin
if fstartInsert then self.CommandMode:=cmInsertLink;
if button=mbMiddle then
begin
Cursor:=crSizeWE;
fOldMouseX:=mouse.CursorPos.x;
end;
end;
procedure TLadderGraph.mouseMove(Sender: TObject; Shift: TShiftState;X, Y: Integer);
begin
if cursor=crSizeWE then
begin
if CursorPos.x>fOldMouseX then
begin
HorzScrollBar.position:=HorzScrollBar.position-(CursorPos.x-fOldMouseX);
end;
if CursorPos.x<fOldMouseX then
begin
HorzScrollBar.position:=HorzScrollBar.position+(fOldMouseX-CursorPos.x);
end;
end;
end;
procedure TLadderGraph.objectInserted(Graph: TEvsSimpleGraph; GraphObject: TEvsGraphObject);
begin
// debut d'insertion de lien
if self.CommandMode=cmInsertLink then
begin
fcurrentLink:=TEvsGraphLink(graphObject);
end;
graphChanged:=true;
form1.compileIndicator.Brush.Color:=clRed;
end;
procedure TLadderGraph.objectRemoved(Graph: TEvsSimpleGraph; GraphObject: TEvsGraphObject);
begin
graphChanged:=true;
form1.compileIndicator.Brush.Color:=clRed;
end;
procedure TLadderGraph.objectChanged(Graph: TEvsSimpleGraph; GraphObject: TEvsGraphObject);
begin
if not(GraphObject is TConnexion) then
begin
graphChanged:=true;
form1.compileIndicator.Brush.Color:=clRed;
end;
end;
procedure TLadderGraph.objectClick(Graph: TEvsSimpleGraph;aGraphObject: TEvsGraphObject);
begin
end;
procedure TLadderGraph.keyDown(sender:TObject;var key:word;shiftState:TShiftState);
var
i : integer;
begin
// suppress
if (key=46) and (self.SelectedObjectsCount()>0) then
deleteSymbol;
// copy
if (ssCtrl in shiftState) and (upcase(char(key))='C') then
Form1.itemCopySymbolClick(sender);
// paste
if (ssCtrl in shiftState) and (upcase(char(key))='V') then
Form1.itemPasteSymbolClick(sender);
end;
procedure TLadderGraph.commandModeChange(Sender : TObject);
begin
// if self.CommandMode=cmInsertLink then
// begin
// fcurrentLink:=nil;
// end;
end;
procedure TLadderGraph.dblClick(grapheCurrent:TEvsSimpleGraph;objet:TEvsGraphObject);
begin
if objet.IsNode then
if objet.ClassName='TComment' then
TComment(objet).doubleClick(objet)
else
TSymbol(objet).doubleClick(objet);
end;
// error form
procedure TLadderGraph.showErrorForm(message:string;x,y:integer);
begin
MessageDlgPos(message,mtWarning,[mbOK],0,x,y);
fcurrentLink.Destroy;
end;
// dialogue permettant d'insérer un symbole sur un lien existant
// appelé par l'evenement onMouseUp
procedure TLadderGraph.askInsert(lien:TEvsGraphLink;symbole:TSymbol);
var
connecteur : array[0..1] of TConnexion;
newLien : array[0..1] of TEvsGraphLink;
i : integer;
begin
if MessageDlgPos('Insérer le symbole ?',mtConfirmation,[mbCancel,mbOK],0,self.GraphToScreen(symbole.left,symbole.top).x,self.GraphToScreen(symbole.left,symbole.top).y)=mrOK then
begin
for i:=0 to lien.HookedPointCount-1 do
begin
connecteur[i]:=TConnexion(lien.HookedObjectOf(i));
newlien[i]:=TevsGraphLink.Create(self);
if not connecteur[i].isInput then
newlien[i].Link(connecteur[i],TSymbol(symbole).symbolInput)
else
newlien[i].Link(Tconnexion(TSymbol(symbole).symbolOutput),connecteur[i]);
end;
lien.Delete;
end;
end;
procedure TLadderGraph.deleteSymbol;
var
i,j : integer;
symbol : TSymbol;
begin
i:=SelectedObjects.count;
while i>0 do
begin
if SelectedObjects[0].IsNode then
begin
symbol:=TSymbol(SelectedObjects.Items[0]);
if SelectedObjects[0].ClassName<>'TComment' then
begin
if symbol.symbolInput<>nil then
begin
j:=symbol.symbolInput.LinkInputCount;
while j>0 do
begin
symbol.symbolInput.LinkInputs[0].Delete;
j:=symbol.symbolInput.LinkInputCount;
end;
end;
if symbol.symbolOutput<>nil then
begin
j:=symbol.symbolOutput.LinkOutputCount;
while j>0 do
begin
symbol.symbolOutput.LinkOutputs[0].Delete;
j:=symbol.symbolOutput.LinkOutputCount;
end;
end;
if symbol.symbolInput<>nil then symbol.symbolInput.Delete;
if symbol.symbolOutput<>nil then symbol.symbolOutput.Delete;
if symbol.textAllias<>nil then symbol.textAllias.delete;
symbol.delete;
end
else SelectedObjects[0].Delete;
end
else SelectedObjects[0].Delete;
i:=SelectedObjects.count;
end;
end;
procedure TLadderGraph.renameSymbol;
var
symbol : TSymbol;
begin
if SelectedObjects[0].IsNode then
if SelectedObjects[0].ClassName<>'TComment' then
begin
symbol:=TSymbol(SelectedObjects.Items[0]);
symbol.doubleClick(symbol);
end;
end;
procedure TLadderGraph.showTab(sender:TObject);
begin
expandGraphOnPanel;
end;
// expand graph on all panel area
procedure TLadderGraph.expandGraphOnPanel;
begin
if grapheCurrent<>nil then
begin
//ftabsheet.SetBounds(fsurface.left,fsurface.Top-100,fsurface.Width,fsurface.Height);
fsurface.width:=form1.width-form1.traitProjectTree.left-form1.VariablesPageControl.width+104;
fpanel.Width:=fsurface.width;
self.width:=fpanel.Width-10;
end;
end;
{ Object move }
procedure TLadderGraph.objectBeginDrag(graphe:TEvsSimpleGraph;objet:TEvsGraphObject;ht:DWord);
begin
// for over writing
fRectBeginDrag:=objet.BoundsRect;
end;
procedure TLadderGraph.ObjectEndDrag(graphe:TEvsSimpleGraph;objet:TEvsGraphObject;ht:DWord;value : boolean);
var
i : integer;
begin
// test over writing
for i:=0 to grapheCurrent.Objects.count-1 do
if (grapheCurrent.Objects[i].IsNode) and
(grapheCurrent.Objects[i].ClassName<>'TConnexion') and
(grapheCurrent.Objects[i].ClassName<>'TAllias')
then
begin
if (objet.ContainsRect(grapheCurrent.Objects[i].BoundsRect)) and
(objet<>grapheCurrent.Objects[i]) then
objet.BoundsRect:=fRectBeginDrag;
end;
end;
procedure TLadderGraph.onClickEvent(sender:TObject);
var
i : integer;
begin
for i:=0 to ObjectsCount()-1 do
if Objects[i] is Tsymbol
then if TSymbol(Objects[I]).highlighted then
TSymbol(Objects[I]).unhighLight;
end;
procedure TLadderGraph.graphChange(Sender:TObject);
begin
graphChanged:=true;
end;
function TLadderGraph.checkGraph:TGraphError;
var
i,j,k : integer;
tryNumber : longint;
objet : TEvsGraphObject;
varSymbol : TStringList;
varExist : boolean;
begin
// check unconnected symbols
for i:=0 to Objects.Count-1 do
begin
objet:=Objects[i];
if objet is TSymbol then
begin
if TSymbol(objet).symbolInput<>nil then
if (TSymbol(objet).symbolInput.LinkInputCount=0) then
begin
result:=errSingle;
exit;
end;
if (not TSymbol(objet).isTerminal) and
(TSymbol(objet).symbolOutput<>nil) then
if (TSymbol(objet).symbolOutput.LinkOutputCount=0) then
begin
result:=errSingle;
exit;
end;
end;
end;
// check existing var
for i:=0 to ObjectsCount-1 do
if Objects[i] is TSymbol then
if TSymbol(Objects[i]).ClassName<>'TPowerRail' then
begin
varSymbol:=TSymbol(Objects[i]).getVarList;
for j:=0 to varSymbol.Count-1 do
if not trystrtoint(varSymbol[j],tryNumber) then
begin
varExist:=false;
for k:=0 to varList.Count-1 do
if varList[k].nameVar=varSymbol[j]
then varExist:=true;
if not varExist then
for k:=0 to ioPlcList.Count-1 do
if ioPlcList[k].nameVar=varSymbol[j]
then varExist:=true;
if not varExist then
for k:=0 to form1.SystemGrid.RowCount-1 do
if form1.SystemGrid.Cells[0,k]=varSymbol[j]
then varExist:=true;
if not varExist then
for k:=0 to timerList.Count-1 do
if timerList[k].nameVar=varSymbol[j]
then varExist:=true;
// error var doesn't exist
if not varExist then
begin
fSymbolError:=varSymbol[j];
result:=errNoVar;
exit;
end;
end;
end;
result:=errNone;
end;
procedure TLadderGraph.findInGraph(varName:string);
var
i,j : integer;
varSymbol : TStringList;
tryNumber : longint;
begin
for i:=0 to ObjectsCount-1 do
if Objects[i] is TSymbol then
if TSymbol(Objects[i]).ClassName<>'TPowerRail' then
begin
varSymbol:=TSymbol(Objects[i]).getVarList;
for j:=0 to varSymbol.Count-1 do
if not trystrtoint(varSymbol[j],tryNumber) then
begin
if varSymbol[j]=varName then
begin
crossRefList.add(TSymbol(Objects[i]));
classCrossRefList.add(TSymbol(Objects[i]).classname);
end;
end;
end;
end;
procedure TLadderGraph.unHighLightGraph;
var
i : integer;
begin
for i:=0 to ObjectsCount-1 do
if Objects[i] is TSymbol then TSymbol(Objects[i]).unHighlight;
end;
end.