-
Notifications
You must be signed in to change notification settings - Fork 0
/
CadClassesMin.pas
1826 lines (1734 loc) · 48.3 KB
/
CadClassesMin.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
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
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
unit CadClassesMin;
{$IFDEF FPC}
{$MODE Delphi}
{$M+}
{$ENDIF}
interface
uses
{$IFnDEF FPC}
Windows,
{$ELSE}
LCLIntf, LCLType, LMessages,
{$ENDIF}
classes, Graphics,
EuLibMin, ChainClassesMin, NuclideClassesMin;
{*********************** ChainCAD *************************}
type
TChainCAD = class;
{ TBaseShape }
TBaseShape = class
private
FBrushColor: TColor;
FPenColor: TColor;
FPenSize: Integer;
procedure SetBrushColor(const Value: TColor);
procedure SetPenColor(const Value: TColor);
procedure SetPenSize(const Value: Integer);
procedure SetBottom(const Value: Integer);
procedure SetLeft(const Value: Integer);
procedure SetRight(const Value: Integer);
procedure SetTop(const Value: Integer);
function GetBottom: Integer;
function GetLeft: Integer;
function GetRight: Integer;
function GetTop: Integer;
protected
FRect: TRect;
fChainCAD: TChainCAD;
public
procedure Paint; virtual;
property Rect: TRect read FRect;
published
constructor Create(aChainCAD: TChainCAD);
property PenSize: Integer read FPenSize write SetPenSize;
property PenColor: TColor read FPenColor write SetPenColor;
property BrushColor: TColor read FBrushColor write SetBrushColor;
property Left: Integer read GetLeft write SetLeft;
property Right: Integer read GetRight write SetRight;
property Top: Integer read GetTop write SetTop;
property Bottom: Integer read GetBottom write SetBottom;
end;
TCadState = class(TBaseShape)
private
fState: TChainState;
public
constructor Create4DecayChainImage(aChainImage: TChainCAD; aState: TChainState; aRect: TRect);
property State: TChainState read fState;
constructor Create(aCAD: TChainCad; aState: TChainState);
procedure Paint; override;
end;
TCadLink = class(TBaseShape)
private
fLink: TChainLink;
fStart: TCadState;
fFinish: TCadState;
public
procedure Paint4DecayChainImage;
constructor Create(aCAD: TChainCad; aChainLink: TChainLink);
procedure Paint; override;
procedure Update;
property Link: TChainLink read fLink;
property Start: TCadState read fStart;
property Finish: TCadState read fFinish;
end;
{ TCadStateList }
TCadStateList = class(TList)
private
fCad: TChainCad;
protected
function GetState(Index: integer): TCadState;
procedure SetState(Index: integer; aCadState: TCadState);
public
procedure Delete(Index: Integer); overload;
function Add(Item: Pointer): integer; overload;
property Cad: TChainCad read fCad;
function FindInList(const aThZpA_s: integer): integer;
destructor Destroy; override;
constructor Create(aCad: TChainCad);
property
CadStates[Index: integer]: TCadState read GetState write SetState; default;
end;
{ TCadLinkList }
TCadLinkList = class(TList)
private
fCad: TChainCad;
protected
function GetCadLink(Index: integer): TCadLink;
procedure SetCadLink(Index: integer; aCadLink: TCadLink);
public
procedure Delete(Index: Integer); overload;
function Add(Item: Pointer): integer; overload;
property Cad: TChainCad read fCad;
function FindInList(const aThZpA_sStart, aThZpA_sFinish: integer): integer;
destructor Destroy; override;
constructor Create(aCad: TChainCad);
property
CadLinks[Index: integer]: TCadLink read GetCadLink write SetCadLink; default;
end;
{TChainCad}
TChainCAD = class
private
fCanvas: TCanvas;
fChain: TChain;
fVisibleRect: TRect;
fDX, fDY: integer;
fDA, fDAA: integer;
fStateWidth, fStateHeight: integer;
fDrawFissionLinks: Boolean;
fShowHalfLife: Boolean;
fShowSigmaC: Boolean;
fShowRI: Boolean;
fWidth, fHeight: integer;
FOnChange: TNotifyEvent;
fWorking: Boolean;
protected
fXYGrid: array of array of LongInt; // row (Y = 10*M+s = const
fStates: TCadStateList;
fLinks: TCadLinkList;
procedure AddState(aState: TChainState);
procedure AddLink(aChainLink: TChainLink);
procedure SetCanvas(aCanvas: TCanvas);
procedure AdjustLinks;
procedure NormalizePicture;
public
function ClearCADwoChainClean: Boolean;
function PaintAsDecayChain: Boolean;
procedure ListStates(var Lines: TStringList);
procedure ListLinks(var Lines: TStringList);
function FindLinkAtXY(const X, Y: Integer): integer;
function FindStateAtXY(const X, Y: Integer): integer;
procedure AddStateByName(// tipa Ru-106, U-235g, U-235m, Bi-212m1, Bi-212m2
const aName: string;
const BuildLinks: Boolean = True;
const Transitions: TNuclideTransitions = [ntCapture, ntFission, ntDecay, ntThreshold];
const aNuclideList: TNuclideList = nil;
const aDataModule: Pointer = nil);
procedure AddLinkByName(const Start: string; const Finish: string;
const LoadFromDB: Boolean;
aTransitions: TNuclideTransitions = [ntCapture, ntFission, ntDecay, ntThreshold];
const aNuclideList: TNuclideList = nil;
aDataModule: Pointer = nil);
function GetWidth: integer;
function GetHeight: integer;
constructor Create(aChain: TChain);
procedure CreateStates;
procedure CreateLinks;
procedure CreateLinksForAddedState(aState: TChainState);
procedure ClearPaint;
function PaintStates: Boolean;
function PaintLinks: Boolean;
function SaveChainToFile(const FileName: string): Boolean;
function LoadChainFromFile(const FileName: string): Boolean;
function FindStateByName(aName: string): TCadState;
function FindLinkByName(const NamePart1: string; const NamePart2: string = ''): TCadLink;
function FindStateByThZpA_s(aThZpA_s: integer): TCadState;
function LinkAlreadyExists(aChainLink: TChainLink): Boolean;
procedure DeleteState(aState: TCadState);
procedure DeleteStateEx(aState: TCadState);
procedure DeleteLink(aLink: TCadLink);
procedure ZoomOut(const Mult: integer = 2);
procedure ZoomIn(const Mult: integer = 2);
procedure Zoom(const Factor: double = 1);
property Canvas: TCanvas read fCanvas write SetCanvas;
property VisibleRect: TRect read fVisibleRect write fVisibleRect;
property States: TCadStateList read fStates write fStates;
property Links: TCadLinkList read fLinks write fLinks;
property dX: integer read fDX write fDX;
property dY: integer read fDY write fDY;
property dA: integer read fDA write fDA;
property dAA: integer read fDAA write fDAA;
property StateWidth: integer read fStateWidth write fStateWidth;
property StateHeight: integer read fStateHeight write fStateHeight;
property Width: integer read GetWidth;
property Height: integer read GetHeight;
property OnChange: TNotifyEvent read FOnChange write FOnChange;
property DrawFissionLinks: Boolean read fDrawFissionLinks write fDrawFissionLinks;
property ShowHalfLife: Boolean read fShowHalfLife write fShowHalfLife;
property ShowSigmaC: Boolean read fShowSigmaC write fShowSigmaC;
property ShowRI: Boolean read fShowRI write fShowRI;
property Working: Boolean read fWorking;
end;
implementation
uses
SysUtils, Dialogs, Forms;
function NormalizeRect(const aRect: TRect): TRect;
var
TT: integer;
begin
Result:= aRect;
with Result do
begin
if Top > Bottom then
begin
TT:= Top;
Top:= Bottom;
Bottom:= TT;
end;
if Left > Right then
begin
TT:= Left;
Left:= Right;
Right:= TT;
end;
end;
end;
function IsPointInRect(const X, Y: integer; const aRect: TRect; const Delta: integer = 0): Boolean;
begin
Result:= False;
// NormalizeRect(aRect);
with aRect do
if ((((X >= Left - Delta) and (X <= Right + Delta)) or ((X <= Left + Delta) and (X >= Right - Delta)))
and
(((Y >= Top - Delta) and (Y <= Bottom + Delta)) or ((Y <= Top + Delta) and (Y >= Bottom - Delta)))) then
Result:= True;
end;
{ TBaseShape }
constructor TBaseShape.Create(aChainCAD: TChainCAD);
begin
inherited Create;
fChainCAD:= aChainCAD;
end;
function TBaseShape.GetBottom: Integer;
begin
Result:= fRect.Bottom;
end;
function TBaseShape.GetLeft: Integer;
begin
Result:= fRect.Left;
end;
function TBaseShape.GetRight: Integer;
begin
Result:= fRect.Right;
end;
function TBaseShape.GetTop: Integer;
begin
Result:= fRect.Top;
end;
procedure TBaseShape.Paint;
begin
//set the attributes
if (fChainCAD.Canvas <> nil) then
begin
fChainCAD.Canvas.Pen.Color:= fPenColor;
fChainCAD.Canvas.Pen.Width:= fPenSize;
fChainCAD.Canvas.Brush.Color:= fBrushColor;
end;
end;
procedure TBaseShape.SetBottom(const Value: Integer);
begin
fRect.Bottom:= Value;
end;
procedure TBaseShape.SetBrushColor(const Value: TColor);
begin
FBrushColor:= Value;
end;
procedure TBaseShape.SetLeft(const Value: Integer);
begin
fRect.Left:= Value;
end;
procedure TBaseShape.SetPenColor(const Value: TColor);
begin
FPenColor:= Value;
end;
procedure TBaseShape.SetPenSize(const Value: Integer);
begin
FPenSize:= Value;
end;
procedure TBaseShape.SetRight(const Value: Integer);
begin
fRect.Right:= Value;
end;
procedure TBaseShape.SetTop(const Value: Integer);
begin
fRect.Top:= Value;
end;
{ TCadState }
constructor TCadState.Create(aCAD: TChainCad; aState: TChainState);
function GetXau(State: TChainState): integer;
begin
Result:= (State.ThZpA_s div 10) mod 1000 - (State.ThZpA_s div 10) div 1000; // Amass-Znum
end;
function GetYau(State: TChainState): integer;
begin
Result:= (-((State.ThZpA_s div 10) div 1000) * 10 - State.ThZpA_s mod 10); //10Znum+State
end;
var
I, Difference, XauMin, YauMin, XauMax, YauMax, Xmax, Ymax, X0au, Y0au, X0, Y0, dX, dY, StateW, StateH: integer;
X0Found, Y0Found: Boolean;
begin
inherited Create(aCAD);
fState:= aState;
dX:= fChainCAD.dX;
dY:= fChainCAD.dY;
StateW:= fChainCAD.StateWidth;
StateH:= fChainCAD.StateHeight;
if fChainCAD.States.Count < 1 then
with FRect do
begin
FRect.Left:= dX;
FRect.Top:= dY;
FRect.Right:= dX + StateW;
FRect.Bottom:= dY + StateH;
Exit;
end
else
begin
X0Found:= False;
Y0Found:= False;
X0:= fChainCAD.dX;
Y0:= fChainCAD.dY;
X0au:= GetXau(aState);
Y0au:= GetYau(aState);
// Find X
XauMin:= MaxInt;
Xmax:= -MaxInt;
XauMax:= -MaxInt;
for I:= 0 to fChainCAD.States.Count - 1 do
begin
if (fChainCAD.States[I].Left > Xmax) then
Xmax:= fChainCAD.States[I].Left;
if (GetXau(fChainCAD.States[I].fState) > XauMax) then
XauMax:= GetXau(fChainCAD.States[I].fState);
if (GetXau(fChainCAD.States[I].fState) < XauMin) then
XauMin:= GetXau(fChainCAD.States[I].fState);
if (X0au = GetXau(fChainCAD.States[I].fState)) then
begin
X0:= fChainCad.States[I].Left;
X0Found:= True;
break;
end;
end;
if not (X0Found) then
begin
if X0au < XauMin then
X0:= -StateW
else if X0au > XauMax then
X0:= Xmax + StateW + dX
else
begin //Find NearestNextX
Difference:= MaxInt;
for I:= 0 to fChainCAD.States.Count - 1 do
if (GetXau(fChainCAD.States[I].fState) > X0au) then
if (GetXau(fChainCAD.States[I].fState) - X0au < Difference) then
begin
Difference:= GetXau(fChainCAD.States[I].fState) - X0au;
X0:= fChainCAD.States[I].Left;
end;
for I:= 0 to fChainCAD.States.Count - 1 do
if fChainCAD.States[I].Left >= X0 then
begin
fChainCAD.States[I].Left:= fChainCAD.States[I].Left + StateW + dX;
fChainCAD.States[I].Right:= fChainCAD.States[I].Right + StateW + dX;
end;
end;
end;
// Find Y
YauMin:= MaxInt;
Ymax:= -MaxInt;
YauMax:= -MaxInt;
for I:= 0 to fChainCAD.States.Count - 1 do
begin
if (fChainCAD.States[I].Top > Ymax) then
Ymax:= fChainCAD.States[I].Top;
if (GetYau(fChainCAD.States[I].fState) > YauMax) then
YauMax:= GetYau(fChainCAD.States[I].fState);
if (GetYau(fChainCAD.States[I].fState) < YauMin) then
YauMin:= GetYau(fChainCAD.States[I].fState);
if (Y0au = GetYau(fChainCAD.States[I].fState)) then
begin
Y0:= fChainCad.States[I].Top;
Y0Found:= True;
break;
end;
end;
if not (Y0Found) then
begin
if Y0au < YauMin then
Y0:= -StateH
else if Y0au > YauMax then
Y0:= Ymax + StateH + dY
else
begin //Find NearestNextY
Difference:= MaxInt;
for I:= 0 to fChainCAD.States.Count - 1 do
if (GetYau(fChainCAD.States[I].fState) > Y0au) then
if (GetYau(fChainCAD.States[I].fState) - Y0au < Difference) then
begin
Difference:= GetYau(fChainCAD.States[I].fState) - Y0au;
Y0:= fChainCAD.States[I].Top;
end;
for I:= 0 to fChainCAD.States.Count - 1 do
if fChainCAD.States[I].Top >= Y0 then
begin
fChainCAD.States[I].Top:= fChainCAD.States[I].Top + StateH + dY;
fChainCAD.States[I].Bottom:= fChainCAD.States[I].Bottom + StateH + dY;
end;
end;
end;
with FRect do
begin
Left:= X0;
Right:= X0 + StateW;
Top:= Y0;
Bottom:= Y0 + StateH;
end;
end; //States >1
end;
constructor TCadState.Create4DecayChainImage(aChainImage: TChainCAD;
aState: TChainState; aRect: TRect);
begin
inherited Create(aChainImage);
fState := aState;
with FRect, aRect do
begin
FRect.Left := aRect.Left;
FRect.Top := aRect.Top;
FRect.Right := aRect.Right;
FRect.Bottom := aRect.Bottom;
Exit;
end
end;
procedure TCadState.Paint;
var
aStr: ShortString;
aFloat: double;
begin
inherited;
if (fChainCAD.fCanvas <> nil) then
with fChainCAD.fCanvas do
begin
Brush.Style:= bsClear;
Font.Height:= (fRect.Bottom - fRect.Top) div 3;
Rectangle(fRect.Left, fRect.Top, fRect.Right, fRect.Bottom);
TextOut((fRect.Left + fRect.Right - TextWidth(fState.Name)) div 2, fRect.Top + fChainCAD.Canvas.Pen.Width + 1, fState.Name);
if (fChainCAD <> nil) then
if fChainCAD.fShowHalfLife then
if PrepareToParse(fState.ValuesStr[0], aStr) then
begin // Lambda
if ValEuSilent(aStr, aFloat) then
if (aFloat > 0) then
begin
aStr:= T1_2ToStr(Ln2 / aFloat);
TextOut((fRect.Left + fRect.Right - TextWidth(aStr)) div 2, (fRect.Top + fRect.Bottom) div 2 + fChainCAD.Canvas.Pen.Width + 1, aStr);
end;
end
else if TextT1_2ToNum(aStr, aFloat) then
begin // may be T1_2
if (aFloat > 0) then
begin
aStr:= T1_2ToStr(aFloat);
TextOut((fRect.Left + fRect.Right - TextWidth(aStr)) div 2, (fRect.Top + fRect.Bottom) div 2 + fChainCAD.Canvas.Pen.Width + 1, aStr);
end;
end
else
begin
aStr:= '???';
TextOut((fRect.Left + fRect.Right - TextWidth(aStr)) div 2, (fRect.Top + fRect.Bottom) div 2 + fChainCAD.Canvas.Pen.Width + 1, aStr);
end;
end;
end;
{ TChainCAD }
function LineIntersect(const X10, Y10, X11, Y11, X20, Y20, X21, Y21: integer;
var X, Y: integer): Boolean;
var
A1, B1, C1, A2, B2, C2, D: integer;
begin
Result:= False;
A1:= Y11 - Y10;
B1:= X10 - X11;
C1:= -X10 * Y11 + X11 * Y10;
A2:= Y21 - Y20;
B2:= X20 - X21;
C2:= -X20 * Y21 + X21 * Y20;
D:= A1 * B2 - A2 * B1;
if D <> 0 then
begin
X:= round(1.0 * (B1 * C2 - C1 * B2) / D);
Y:= round(1.0 * (C1 * A2 - A1 * C2) / D);
Result:= True;
end;
end;
function IntervalIntersect(const X10, Y10, X11, Y11, X20, Y20, X21, Y21: integer;
var X, Y: integer): Boolean;
begin
Result:= False;
if LineIntersect(X10, Y10, X11, Y11, X20, Y20, X21, Y21, X, Y) then
if ((X - X10) * (X11 - X) >= 0) and ((X - X20) * (X21 - X) >= 0) and
((Y - Y10) * (Y11 - Y) >= 0) and ((Y - Y20) * (Y21 - Y) >= 0) then
Result:= True;
end;
procedure TChainCAD.AddStateByName(// tipa Ru-106, U-235g, U-235m, Bi-212m1, Bi-212m2
const aName: string;
const BuildLinks: Boolean = True;
const Transitions: TNuclideTransitions = [ntCapture, ntFission, ntDecay, ntThreshold];
const aNuclideList: TNuclideList = nil;
const aDataModule: Pointer = nil);
var
TmpState: TChainState;
I: integer;
AlreadyInCad: Boolean;
begin
fWorking:= True;
try
TmpState:= fChain.AddStateByName(aName, BuildLinks, Transitions, aNuclideList, aDataModule, False);
repeat
Application.ProcessMessages
until not (fChain.Working);
if TmpState <> nil then
begin
AlreadyInCad:= False;
for I:= 0 to fStates.Count - 1 do
if fStates[I].fState = TmpState then
begin
AlreadyInCad:= True;
break;
end;
if not (AlreadyInCad) then
Self.AddState(TmpState);
end;
if BuildLinks then
begin
for I:= 0 to fChain.Links.Count - 1 do
AddLink(fChain.Links[I]); // AddLink - checks for duplicates See LinkAlreadyExists(aChainLink)
if Assigned(FOnChange) then
OnChange(Self);
end;
finally
fWorking:= False;
end;
end;
function TChainCAD.FindStateByName(aName: string): TCadState;
var
aThZpA_s: integer;
begin
aThZpA_s:= StrToThZpA_s(aName);
Result:= FindStateByThZpA_s(aThZpA_s);
end;
function TChainCAD.FindStateByThZpA_s(aThZpA_s: integer): TCadState;
var
I: integer;
begin
Result:= nil;
for I:= 0 to fStates.Count - 1 do
if aThZpA_s = fStates[I].fState.ThZpA_s then
begin
Result:= fStates[I];
break;
end;
end;
function TChainCAD.LinkAlreadyExists(aChainLink: TChainLink): Boolean;
var
I: integer;
begin
Result:= False;
for I:= 0 to fLinks.Count - 1 do
if ((fLinks[I].fStart.State.ThZpA_s = aChainLink.StartThZpA_s) and (fLinks[I].fFinish.State.ThZpA_s = aChainLink.FinishThZpA_s)) then
begin
Result:= True;
break;
end;
end;
procedure TChainCAD.AdjustLinks;
var
I: integer;
aCadLink: TCadLink;
aStart, aFinish: TCadState;
X, Y, X0, Y0, X1, Y1: integer;
aStartRect, aFinishRect: TRect;
begin
for I:= 0 to fLinks.Count - 1 do
begin
aCadLink:= fLinks[I];
aStart:= aCadLink.fStart;
aFinish:= aCadLink.fFinish;
if ((aStart <> nil) and (aFinish <> nil)) then
begin
aStartRect:= NormalizeRect(aStart.Rect);
aFinishRect:= NormalizeRect(aFinish.Rect);
X0:= (aStartRect.Left + aStartRect.Right) div 2;
Y0:= (aStartRect.Top + aStartRect.Bottom) div 2;
X1:= (aFinishRect.Left + aFinishRect.Right) div 2;
Y1:= (aFinishRect.Top + aFinishRect.Bottom) div 2;
with aStart do
if IntervalIntersect(X0, Y0, X1, Y1, Left, Top, Left, Bottom, X, Y) then
begin
aCadLink.Left:= X;
aCadLink.Top:= Y;
end
else if IntervalIntersect(X0, Y0, X1, Y1, Left, Top, Right, Top, X, Y) then
begin
aCadLink.Left:= X;
aCadLink.Top:= Y;
end
else if IntervalIntersect(X0, Y0, X1, Y1, Right, Top, Right, Bottom, X, Y) then
begin
aCadLink.Left:= X;
aCadLink.Top:= Y;
end
else if IntervalIntersect(X0, Y0, X1, Y1, Left, Bottom, Right, Bottom, X, Y) then
begin
aCadLink.Left:= X;
aCadLink.Top:= Y;
end
else
begin
Exit;
end;
// Intersect With Finish Sides
with aFinish do
if IntervalIntersect(X0, Y0, X1, Y1, Left, Top, Left, Bottom, X, Y) then
begin
aCadLink.Right:= X;
aCadLink.Bottom:= Y;
end
else if IntervalIntersect(X0, Y0, X1, Y1, Left, Top, Right, Top, X, Y) then
begin
aCadLink.Right:= X;
aCadLink.Bottom:= Y;
end
else if IntervalIntersect(X0, Y0, X1, Y1, Right, Top, Right, Bottom, X, Y) then
begin
aCadLink.Right:= X;
aCadLink.Bottom:= Y;
end
else if IntervalIntersect(X0, Y0, X1, Y1, Left, Bottom, Right, Bottom, X, Y) then
begin
aCadLink.Right:= X;
aCadLink.Bottom:= Y;
end
else
begin
Exit;
end;
end;
end;
end;
procedure TChainCAD.AddLink(aChainLink: TChainLink);
var
aCadLink: TCadLink;
aStart, aFinish: TCadState;
X, Y, X0, Y0, X1, Y1: integer;
aStartRect, aFinishRect: TRect;
begin
if not (LinkAlreadyExists(aChainLink)) then
begin
aCadLink:= TCadLink.Create(Self, aChainLink);
aStart:= Self.FindStateByThZpA_s(aChainLink.StartThZpA_s);
aFinish:= Self.FindStateByThZpA_s(aChainLink.FinishThZpA_s);
aCadLink.fStart:= aStart;
aCadLink.fFinish:= aFinish;
if ((aStart <> nil) and (aFinish <> nil)) then
begin
aStartRect:= NormalizeRect(aStart.Rect);
aFinishRect:= NormalizeRect(aFinish.Rect);
X0:= (aStartRect.Left + aStartRect.Right) div 2;
Y0:= (aStartRect.Top + aStartRect.Bottom) div 2;
X1:= (aFinishRect.Left + aFinishRect.Right) div 2;
Y1:= (aFinishRect.Top + aFinishRect.Bottom) div 2;
with aStart do
if IntervalIntersect(X0, Y0, X1, Y1, Left, Top, Left, Bottom, X, Y) then
begin
aCadLink.Left:= X;
aCadLink.Top:= Y;
end
else if IntervalIntersect(X0, Y0, X1, Y1, Left, Top, Right, Top, X, Y) then
begin
aCadLink.Left:= X;
aCadLink.Top:= Y;
end
else if IntervalIntersect(X0, Y0, X1, Y1, Right, Top, Right, Bottom, X, Y) then
begin
aCadLink.Left:= X;
aCadLink.Top:= Y;
end
else if IntervalIntersect(X0, Y0, X1, Y1, Left, Bottom, Right, Bottom, X, Y) then
begin
aCadLink.Left:= X;
aCadLink.Top:= Y;
end
else
begin
aCadLink.Free;
Exit;
end;
// Intersect With Finish Sides
with aFinish do
if IntervalIntersect(X0, Y0, X1, Y1, Left, Top, Left, Bottom, X, Y) then
begin
aCadLink.Right:= X;
aCadLink.Bottom:= Y;
end
else if IntervalIntersect(X0, Y0, X1, Y1, Left, Top, Right, Top, X, Y) then
begin
aCadLink.Right:= X;
aCadLink.Bottom:= Y;
end
else if IntervalIntersect(X0, Y0, X1, Y1, Right, Top, Right, Bottom, X, Y) then
begin
aCadLink.Right:= X;
aCadLink.Bottom:= Y;
end
else if IntervalIntersect(X0, Y0, X1, Y1, Left, Bottom, Right, Bottom, X, Y) then
begin
aCadLink.Right:= X;
aCadLink.Bottom:= Y;
end
else
begin
aCadLink.Free;
Exit;
end;
TList(fLinks).Add(aCadLink);
end;
AdjustLinks;
if Assigned(FOnChange) then
OnChange(Self);
end;
end;
procedure TChainCAD.AddState(aState: TChainState);
var
aCadState: TCadState;
begin
aCadState:= TCadState.Create(Self, aState);
TList(fStates).Add(aCadState);
NormalizePicture;
if Assigned(FOnChange) then
OnChange(Self);
end;
constructor TChainCAD.Create(aChain: TChain);
begin
inherited Create;
fDrawFissionLinks:= True;
fShowHalfLife:= False;
fShowSigmaC:= False;
fShowRI:= False;
fCanvas:= nil;
fDX:= 30;
fDY:= 20;
fStateWidth:= 60;
fStateHeight:= 40;
fDA:= 1;
fDAA:= 5;
fChain:= aChain;
fStates:= TCadStateList.Create(Self);
fLinks:= TCadLinkList.Create(Self);
FOnChange:= nil;
end;
procedure TChainCAD.CreateStates;
var
I: integer;
begin
for I:= fStates.Count - 1 downto 0 do
fStates[I].Free;
fStates.Clear;
for I:= 0 to fChain.States.Count - 1 do
begin
AddState(fChain.States[I]);
end;
end;
function CombineStr4DeleteStateEx(const StrStart, StrFinish: string; var DeleteExOutStr: string): Boolean;
var
I: integer;
PercenStr, CommentStr, StartValStr, StartCommentStr, FinishValStr, FinishCommentStr: string;
begin
DeleteExOutStr:= '';
try
CommentStr:= '// DeleteStateEx:';
I:= Pos('//', StrFinish);
if I > 0 then
begin
FinishValStr:= Copy(StrFinish, 1, I - 1);
FinishCommentStr:= Copy(StrFinish, I + 1, Length(StrFinish));
end;
I:= Pos('%', FinishValStr);
if I > 0 then
PercenStr:= Copy(FinishValStr, 1, I) + '*';
I:= Pos('*', FinishValStr);
if I > 0 then
PercenStr:= Copy(StrFinish, 1, I);
I:= Pos('//', StrStart);
if I > 0 then
begin
StartValStr:= Copy(StrStart, 1, I - 1);
StartCommentStr:= Copy(StrStart, I + 1, Length(StrStart));
end;
DeleteExOutStr:= PercenStr + StartValStr + ' ' + CommentStr + StrStart + '->' + StrFinish;
Result:= True;
except
Result:= False;
end;
end;
function IsEmptyToParse(const InStr: string): Boolean;
var
I: integer;
aStr: ShortString;
begin
aStr:= Trim(InStr);
if aStr = '' then
begin
Result:= True;
Exit;
end
else
try
I:= Pos('//', InStr);
if I >= 1 then
aStr:= Trim(Copy(InStr, 1, I - 1));
if aStr = '' then
begin
Result:= True;
Exit;
end;
if PrepareToParse(InStr, aStr) then
if ((Trim(aStr) = '0') or (Trim(aStr) = '0.0')) then
begin
Result:= True;
Exit;
end;
Result:= False;
except
Result:= False;
end;
end;
procedure TChainCAD.DeleteStateEx(aState: TCadState);
var
I, L, K, InLinkNo, OutLinkNo, aStartStateNo, aFinishStateNo: integer;
TmpChainLink: TChainLink;
InLinksList, OutLinksList: TLongIntList;
TmpStr: string;
aStartState, aFinishState: TChainState;
begin
for I:= fStates.Count - 1 downto 0 do
if fStates[I] = aState then
begin
InLinksList:= TLongIntList.Create;
OutLinksList:= TLongIntList.Create;
try
for L:= 0 to fLinks.Count - 1 do
begin
if (fLinks[L].fFinish.fState = aState.fState) then
InLinksList.Add(L);
if (fLinks[L].fStart.fState = aState.fState) then
OutLinksList.Add(L);
end;
for InLinkNo:= 0 to InLinksList.Count - 1 do
begin
//Check Decays Only - If not then exit
for K:= 1 to Self.fChain.Links[InLinksList[InLinkNo]].ValuesStr.Count - 1 do
begin
TmpStr:= Self.fChain.Links[InLinksList[InLinkNo]].ValuesStr[K];
if not (IsEmptyToParse(TmpStr)) then
exit;
end;
end;
for OutLinkNo:= 0 to OutLinksList.Count - 1 do
begin
//Check Decays Only - If not then exit
for K:= 1 to Self.fChain.Links[OutLinksList[OutLinkNo]].ValuesStr.Count - 1 do
begin
TmpStr:= Self.fChain.Links[OutLinksList[OutLinkNo]].ValuesStr[K];
if not (IsEmptyToParse(TmpStr)) then
exit;
end;
end;
for InLinkNo:= 0 to InLinksList.Count - 1 do
for OutLinkNo:= 0 to OutLinksList.Count - 1 do
begin
aStartState:= fChain.Links[InLinksList[InLinkNo]].StartState;
aFinishState:= fChain.Links[OutLinksList[OutLinkNo]].FinishState;
aStartStateNo:= fChain.Links[InLinksList[InLinkNo]].FindStartStateChainNo;
aFinishStateNo:= fChain.Links[OutLinksList[OutLinkNo]].FindFinishStateChainNo;
if ((aStartStateNo > -1) and (aFinishStateNo > -1)) then
begin
TmpChainLink:= TChainLink.Create(fChain, aStartState.ThZpA_s, aFinishState.ThZpA_s);
fChain.AddLink(TmpChainLink);
if CombineStr4DeleteStateEx(fChain.Links[InLinksList[InLinkNo]].ValuesStr[0], fChain.Links[OutLinksList[OutLinkNo]].ValuesStr[0], TmpStr) then
TmpChainLink.ValuesStr[0]:= TmpStr
else
TmpChainLink.ValuesStr[0]:= '//CombineStr4DeleteStateEx failure';
if (TmpChainLink <> nil) then
begin
Self.AddLink(TmpChainLink);
end;
end;
end;
Self.DeleteState(aState);
finally
InLinksList.Free;
OutLinksList.Free;
end;
break;
end;
if Assigned(FOnChange) then
OnChange(Self);
end;
procedure TChainCAD.DeleteState(aState: TCadState);
var
I, L: integer;
begin
for I:= fStates.Count - 1 downto 0 do
if fStates[I] = aState then
begin
// Clear Links
for L:= fLinks.Count - 1 downto 0 do
if ((fLinks[L].fStart.fState = aState.fState) or (fLinks[L].fFinish.fState = aState.fState)) then
DeleteLink(fLinks[L]);
// Clear State
fChain.DeleteState(fStates[I].fState);
TList(fStates).Delete(I);
break;
end;
NormalizePicture;
if Assigned(FOnChange) then
OnChange(Self);
end;
function TChainCAD.GetHeight: integer;
begin
Result:= fHeight;
end;
function TChainCAD.GetWidth: integer;
begin
Result:= fWidth;
end;
function TChainCAD.PaintLinks: Boolean;
var
I: integer;
begin
try
if (fCanvas <> nil) then
for I:= 0 to fLinks.Count - 1 do
begin
Application.ProcessMessages;
fLinks[I].Paint;
end;
Result:= True;
except
Result:= False;
end;
end;
function TChainCAD.PaintStates: Boolean;
var
I: integer;