-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbsunit.pas
More file actions
2726 lines (2476 loc) · 77 KB
/
bsunit.pas
File metadata and controls
2726 lines (2476 loc) · 77 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
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 bsunit;
{Revision History
Version 0.2 released 1/8/2011
22/8/2011 Fix field centre error,
Fix Y resolution error for MapCheck 2
28/09/2011 removed MultiDoc
added invert
added normalise
added windowing
fixed profile display
fixed symmetry calculation
cleaned up printout
15/2/2012 Fix CAX normalisation error,
2/4/2013 Add read for XiO Dose plane file
20/6/2014 Removed redundant DICOM read code causing memory bug
24/6/2014 Fixed XiO read offset by 1
Fixed MapCheck read if dose cal file not present
Included Min/Max as part of beam class
Fixed panel maximise to form area
21/5/2015 Combine open dialog and DICOM dialog
20/7/2015 Add messaging system
Version 0.3 released 20/7/2015
28/6/2016 Support PTW 729 mcc
26/8/2016 Add normalise to max
29/9/2016 Fix PTW 729 memory error
21/10/2016 Add PowerPDF for output
24/10/2016 Fix Profile event misfire
15/8/2017 Fix image integer conversion
13/10/2017 Support IBA Matrix and StartTrack opg
16/10/2017 Fix Diff divide by zero error
Fix profile offset limit error
7/12/2017 Fix even number of detectors offset
15/12/2017 Support Brainlab iPlan Dose plane format
Add text file format identification
8/1/2018 Add help system
Fix windowing error on normalise to CAX
26/1/2018 Fix panel maximise under QT5
Fix window level control size under max/min
30/1/2018 Fix area symmetry off by 1
Fix CAX for even no of detectors
Fix image autoscale
1/2/2018 Add mouse control for profiles
2/2/2018 Fix off by 1 error profile limits
Version 0.4 released 2/2/2018
27/3/2018 Fix regional settings decimal separator
3/4/2018 Add mean and standard deviation
Fix profile increment
30/4/2019 Fix DTrackbar if image max = maxlongint
3/5/2019 Fix DICOM off by one and pointer conversion
17/7/2019 Update about unit
18/7/2019 Update status bar
23/7/2019 Add expression parser
30/7/2019 Add multipage output
31/7/2019 Fix profile export dirs
1/8/2019 Add expression editor
14/8/2019 Remove auto normalisation of profile values.
Fix previous image and profile display on open image cancel
11/9/2019 Fix prompt for overwrite results
Fix protocol list unsorted on reload
Add Quit Edit menu item
16/9/2019 Fix Y axis swapped for IBA files
10/10/2019 Change BitButtons to SpeedButtons on protocol edit toolbar
Fix cancel on protocol save
Fix edit flag on protocol edit exit
Correct result window title on edit
23/10/2019 Updated help
Fix click on empty Image pane crash
25/10/2019 Fix user protocol path
Version 0.5 released 25/10/2019
16/4/2020 Fix various memory leaks
6/8/2020 use Form2PDf for printing PDF
fix SaveDialog titles
remove results unit and PowerPDF
24/8/2020 add get correct resolution for tiff images
18/9/2020 fix range check error in calcparams
add inflection points
neaten filename display
22/9/2020 support raw text file
29/9/2020 shift maths routines to unit mathsfuncs
30/9/2020 shift types and constants to unit bstypes
use Hill function non linear regression to determine inflection points
add copy profiles to clipboard
1/10/2020 use parser.SetVariable for performance enhancement
fix status warning display
add FFF params inflection point, 0.4*InfP (20%) and 1.6*InfP (80%)
7/10/2020 add copy results to clipboard
make Protocol read only while not in edit mode
add context menus for X Y profiles and results
8/10/2020 fix duplicate text file open
fix RAWOpen range check error
add sigmoid slope for penumbra
add position of max
fix protocol name change on edit
add profile points for FFF
14/10/2020 add app version
20/10/2020 fix file extensions
5/11/2020 update help
17/11/2020 fix resolution for tiff files
7/12/2020 add normalisation to max for calcs
11/12/2020 make normalisation modal, i.e. non destructive
change toolbar panel to TToolBar
14/12/2020 select default protocol on startup
3/3/2021 fix FFF penumbra slope
11/9/2021 fix recognise files with tiff extension
4/10/2021 fix ShowProfile and refactor
fix initialise vars
profile draw on trackbar change
14/10/2021 Remove sExePath in file Open
22/10/2021 fix profile position on mouse click}
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, LResources, Forms, Controls, Graphics, Dialogs, Menus,
ExtCtrls, Buttons, TAGraph, TASeries, TATools, StdCtrls, Graphtype, Clipbrd,
IntfGraphics, Spin, ComCtrls, LazHelpHTML, Grids, Tracker2, lNetComponents,
lwebserver, FPImage, bstypes;
{ TBSForm }
type
TBSForm = class(TForm)
ChartToolsetY: TChartToolset;
ChartToolsetX: TChartToolset;
ChartToolsetXDataPointHintTool: TDataPointHintTool;
ChartToolsetYDataPointHintTool: TDataPointHintTool;
cbProtocol: TComboBox;
cXProfile: TChart;
cXprof: TLabel;
HTMLBrowserHelpViewer: THTMLBrowserHelpViewer;
HTMLHelpDatabase: THTMLHelpDatabase;
HelpServer: TLHTTPServerComponent;
ImageList: TImageList;
Label7: TLabel;
miResClip: TMenuItem;
miResExp: TMenuItem;
miYToFile: TMenuItem;
miYToClip: TMenuItem;
miXToFile: TMenuItem;
miXToClip: TMenuItem;
pmiCopytoFile: TMenuItem;
pmiCopyClip: TMenuItem;
miQuitEdit: TMenuItem;
miProtocol: TMenuItem;
miSaveP: TMenuItem;
miEditP: TMenuItem;
miContents: TMenuItem;
miAbout: TMenuItem;
miHelp: TMenuItem;
miExportY: TMenuItem;
miExportX: TMenuItem;
miExport: TMenuItem;
Panel8: TPanel;
pmContext: TPopupMenu;
SaveDialog: TSaveDialog;
sbSaveP: TSpeedButton;
sbAddP: TSpeedButton;
sbDelP: TSpeedButton;
sbExitP: TSpeedButton;
StatusBar: TStatusBar;
StatusMessages: TStringList;
sgResults: TStringGrid;
ToolBar: TToolBar;
tbOpen: TToolButton;
ToolButton1: TToolButton;
tbInvert: TToolButton;
tbNormCax: TToolButton;
tbNormMax: TToolButton;
tbCentre: TToolButton;
ToolButton2: TToolButton;
tbPDF: TToolButton;
ToolButton3: TToolButton;
tbExit: TToolButton;
YProfile: TLineSeries;
XProfile: TLineSeries;
cYProf: TLabel;
cResults: TLabel;
cYProfile: TChart;
DTrackBar: TDTrackBar;
iBeam: TImage;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Label4: TLabel;
Label5: TLabel;
Label6: TLabel;
cImage: TLabel;
lMin: TLabel;
Label8: TLabel;
MainMenu: TMainMenu;
miRestore: TMenuItem;
miResults: TMenuItem;
miYProfile: TMenuItem;
miXProfile: TMenuItem;
miImage: TMenuItem;
Panel4: TPanel;
Panel5: TPanel;
Panel6: TPanel;
Panel7: TPanel;
pMaxMin: TPanel;
pResults: TPanel;
pYProfile: TPanel;
Panel2: TPanel;
pBeam: TPanel;
pXProfile: TPanel;
Panel3: TPanel;
sbXMax: TSpeedButton;
sbYMax: TSpeedButton;
sbIMin: TSpeedButton;
sbRMax: TSpeedButton;
sbYMin: TSpeedButton;
sbXMin: TSpeedButton;
sbRMin: TSpeedButton;
seXAngle: TSpinEdit;
seXOffset: TSpinEdit;
seXWidth: TSpinEdit;
seYAngle: TSpinEdit;
seYOffset: TSpinEdit;
seYWidth: TSpinEdit;
sbIMax: TSpeedButton;
Window: TMenuItem;
miPrint: TMenuItem;
miExit: TMenuItem;
miOpen: TMenuItem;
miFile: TMenuItem;
OpenDialog: TOpenDialog;
procedure bbAddLClick(Sender: TObject);
procedure bbDelLClick(Sender: TObject);
procedure bbExitPClick(Sender: TObject);
procedure bbSavePClick(Sender: TObject);
procedure cbProtocolChange(Sender: TObject);
procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
procedure miResClipClick(Sender: TObject);
procedure pmContextPopup(Sender: TObject);
procedure pmiCopyClipClick(Sender: TObject);
procedure pmiCopytoFileClick(Sender: TObject);
procedure sbPDFClick(Sender: TObject);
procedure StatusBarDrawPanel(SBar: TStatusBar;Panel: TStatusPanel; const Rect: TRect);
procedure BSError(sError:string);
procedure BSWarning(sWarning:string);
procedure BSMessage(sMess:string);
procedure ClearStatus;
procedure ChartToolsetXDataPointHintToolHint(ATool: TDataPointHintTool;
const APoint: TPoint; var AHint: String);
procedure ChartToolsetYDataPointHintToolHint(ATool: TDataPointHintTool;
const APoint: TPoint; var AHint: String);
procedure DTrackBarChange(Sender: TObject);
procedure DTrackBarClick(Sender: TObject);
procedure BuildProtocolList;
procedure LoadProtocol;
procedure FormCreate(Sender: TObject);
function FormHelp(Command: Word; Data: PtrInt; var CallHelp: Boolean): Boolean;
function DICOMOpen(sFileName:string):boolean;
procedure FormResize(Sender: TObject);
procedure iBeamClick(Sender: TObject);
procedure miEditPClick(Sender: TObject);
function TextOpen(sFileName:string):boolean;
function MapCheckOpen(sFileName:string):boolean;
function IBAOpen(sFileName:string):boolean;
function PTWOpen(sFileName:string):boolean;
procedure sbCentreClick(Sender: TObject);
procedure sbMaxNormClick(Sender: TObject);
function XioOpen(sFileName:string):boolean;
function BrainLabOpen(sFileName:string):boolean;
function BMPOpen(sFileName:string):boolean;
function HISOpen(sFileName:string):boolean;
function RAWOpen(sFileName:string):boolean;
procedure miExitClick(Sender: TObject);
procedure miExportXClick(Sender: TObject);
procedure miExportYClick(Sender: TObject);
procedure miXClipBClick(Sender: TObject);
procedure miYClipBClick(Sender: TObject);
procedure miOpenClick(Sender: TObject);
procedure miRestoreClick(Sender: TObject);
procedure miAboutClick(Sender: TObject);
procedure miContentsClick(Sender: TObject);
procedure sbIMinClick(Sender: TObject);
procedure sbInvertClick(Sender: TObject);
procedure sbNormClick(Sender: TObject);
procedure sbRMaxClick(Sender: TObject);
procedure sbRMinClick(Sender: TObject);
procedure sbXMaxClick(Sender: TObject);
procedure sbXMinClick(Sender: TObject);
procedure sbYMaxClick(Sender: TObject);
procedure sbYMinClick(Sender: TObject);
procedure seYAngleChange(Sender: TObject);
procedure seXAngleChange(Sender: TObject);
procedure ShowXResults(BeamParams:TBeamParams);
procedure ShowYResults(BeamParams:TBeamParams);
procedure sbIMaxClick(Sender: TObject);
private
{ private declarations }
public
{ public declarations }
FileHandler: TFileHandler;
CGIHandler: TCGIHandler;
PHPCGIHandler: TPHPFastCGIHandler;
end;
var
BSForm :TBSForm;
Beam :TBeam;
XPArr,
YPArr :TPArr;
XBParams,
YBParams :TBeamParams;
sProtPath :string;
Safe,
Editing :boolean;
Normalisation:TNorm;
procedure DisplayBeam;
procedure ShowProfile(ThebitMap:Tbitmap; Wdth:double;
TopLeft,TopRight,BottomRight,BottomLeft:TPoint);
procedure DrawProfile(TheBitmap:TBitmap;Beam:TBeam ;Angle,Offset,Width:double;
Profile:TLineSeries; var PArr:TPArr; var TopLeft,TopRight,BottomLeft,BottomRight:TPoint;
var PrevW:double);
implementation
uses DICOM, define_types, types, math, StrUtils, helpintfs, aboutunit,
LazFileUtils, Parser10, form2pdf, mathsfuncs, fileinfo;
var YPTL, {Y profile top right}
YPTR, {Y profile top left}
YPBL, {Y profile bottom right}
YPBR: TPoint; {Y profile bottom left}
XPTL, {X profile top right}
XPTR, {X profile top left}
XPBL, {X profile bottom right}
XPBR: TPoint; {X profile bottom left}
YPW, {Y previous width}
XPW: double; {X previous width}
function GetAppVersionString(IncludeBuildInfo:Boolean=True; MinorDigits:Byte=2;
IncludePosRelease:Boolean=False; IncludeAnyRelease:Boolean=False): String;
var VersionInfo: TVersionInfo;
begin
Result:= '';
try
VersionInfo:= TVersionInfo.Create;
VersionInfo.Load(HINSTANCE);
Result:= IntToStr(VersionInfo.FixedInfo.FileVersion[0]) + '.' +
Format('%0.*d',[MinorDigits,VersionInfo.FixedInfo.FileVersion[1]]);
if (IncludeAnyRelease or (IncludePosRelease and (VersionInfo.FixedInfo.FileVersion[2]<>0))) then
Result:= Result + '.' + IntToStr(VersionInfo.FixedInfo.FileVersion[2]);
if IncludeBuildInfo then
Result:= Result + '.' + IntToStr(VersionInfo.FixedInfo.FileVersion[3]);
finally
if assigned(VersionInfo) then
VersionInfo.Free;
end;
end; {getappversionstring}
function RobustStrToFloat(s:string):extended;
var fs: TFormatSettings;
begin
fs := DefaultFormatSettings;
Result := 0;
try
Result := StrToFloat(s);
except
on EConvertError do {try again using other decimal separator}
begin
if fs.DecimalSeparator = ',' then
fs.DecimalSeparator := '.'
else
fs.DecimalSeparator := ',';
Result := StrToFloat(s,fs);
end;
end;
end;
procedure DisplayBeam;
{Transfers the array data to a bitmap for viewing. Image is autoscaled}
var IntFImage :TLazIntfImage;
Description:TRawImageDescription;
I,J: integer;
BMax,
BMin,
z: double;
Val: word;
GreyVal: TFPColor;
begin
with Beam do if (Rows>0) and (Cols>0) then
begin
IntFImage := TLazIntFImage.Create(0,0);
Description.Init_BPP32_B8G8R8A8_BIO_TTB(Beam.Cols-2,Beam.Rows);
IntFImage.DataDescription := Description;
BMax := BSForm.DTrackBar.PositionU;
BMin := BSForm.DTrackBar.PositionL;
if BMax <> BMin then
for I:=0 to Beam.Rows - 1 do
for J:=2 to Beam.Cols - 1 do
begin
Z := Beam.Data[I,J];
if Z < BMin then Z := BMin;
if Z > BMax then Z := BMax;
Val := Round((Z - BMin)*65535/(BMax - BMin));
GreyVal := FPColor(Val,Val,Val);
IntFImage.Colors[J-2,I] := GreyVal;
end;
BSForm.iBeam.Picture.Bitmap.LoadFromIntfImage(IntFImage);
IntFImage.Free;
end;
end;
procedure ShowProfile(ThebitMap:Tbitmap; Wdth:double;
TopLeft,TopRight,BottomRight,BottomLeft:TPoint);
begin
{Draw profile on array}
TheBitmap.Canvas.Pen.Color := clRed xor clWhite;
TheBitmap.Canvas.Pen.Mode := pmXor;
if Wdth > 0 then
TheBitmap.Canvas.Polygon([TopLeft,TopRight,BottomRight,BottomLeft])
else
TheBitmap.Canvas.Line(TopLeft,BottomRight);
end;
procedure DrawProfile(TheBitmap:TBitmap; Beam:TBeam; Angle,Offset,Width:double;
Profile:TLineSeries; var PArr:TPArr; var TopLeft,TopRight,BottomLeft,BottomRight:TPoint;
var PrevW:double);
{Draw a profile on the bitmap at any angle}
var I,J,K,L,
WI,WJ,
LimX,
LimY,
OLP,OLN,
DTBPL, {DTrackBar.Position Low}
DTBPU :longint; {DTrackBar.Position Up}
WNX,
WNY,
WPX,
WPY,
MidX,
MidY,
X,Y,
XInc,
YInc,
MidP,
PLen,
TanA,
Phi,
Z,
CMax, {profile max or cax value}
CMin :double; {profile min value}
Start,
Stop: TPoint;
DLine: TRect;
OverLimit: Boolean;
begin
{clear previous profile if it exists}
Profile.Clear;
if (TopLeft.x or TopRight.x or BottomLeft.x or BottomRight.x or
TopLeft.y or TopRight.y or BottomLeft.y or BottomRight.y <> 0) then
ShowProfile(TheBitMap,PrevW,Topleft,Topright,BottomRight,BottomLeft);
{Draw new profile}
Angle := Angle*2*pi/360; //convert angle to radians
TanA := arctan(TheBitMap.Height/TheBitmap.Width); // get limit of corners
Phi := Angle + pi/2; {add 90 degrees to get perpendicular}
LimX := TheBitmap.Width;
LimY := TheBitmap.Height;
MidX := LimX/2 - 0.5;
MidY := LimY/2 - 0.5;
PrevW := trunc((Width - 1)/2);
{get coords of wide profile}
if (Angle > -TanA) and (Angle <= TanA) then {Profile is X profile}
begin
TopLeft := Limit(1,0,-MidX,Phi,Offset + PrevW,MidX,MidY);
TopRight := Limit(1,0,MidX,Phi,Offset + PrevW,MidX,MidY);
BottomRight := Limit(1,0,MidX,Phi,Offset - PrevW,MidX,MidY);
BottomLeft := Limit(1,0,-MidX,Phi,Offset - PrevW,MidX,MidY);
end
else {Profile is Y profile}
begin
TopLeft := Limit(0,1,-MidY,Phi,Offset + PrevW,MidX,MidY);
TopRight := Limit(0,1,MidY,Phi,Offset + PrevW,MidX,MidY);
BottomRight := Limit(0,1,MidY,Phi,Offset - PrevW,MidX,MidY);
BottomLeft := Limit(0,1,-MidY,Phi,Offset - PrevW,MidX,MidY);
end;
{Draw profile on array}
ShowProfile(TheBitMap,PrevW,Topleft,Topright,BottomRight,BottomLeft);
DLine := LimitL(Angle,Phi,TanA,Offset,MidX,MidY,0,0,LimX,LimY);
Start := DLine.TopLeft;
Stop := DLine.BottomRight;
{Add points to profile array}
PLen := sqrt(sqr(Stop.X - Start.X)+ sqr(Stop.Y - Start.Y));
Setlength(PArr,round(PLen) + 1);
For I := 0 to round(PLen) do with PArr[I] do
begin
x := 0;
y := 0;
end;
X := Start.X;
Y := Start.Y;
I := Start.Y;
J := Start.X;
XInc := (Stop.X - Start.X)/PLen;
YInc := (Stop.Y - Start.Y)/PLen;
K:= 0;
MidP := sqrt(sqr((Stop.X - Start.X)*Beam.XRes) + sqr((Stop.Y - Start.Y)*Beam.YRes))/2;
DTBPL := BSForm.DTrackBar.PositionL;
DTBPU := BSForm.DTrackBar.PositionU;
{Add first point}
OverLimit := false;
PArr[K].X := sqrt(sqr((X - Start.X)*Beam.XRes) + sqr((Y - Start.Y)*Beam.YRes)) - MidP;
Z := Beam.Data[I,J+2];
if Z < DTBPL then Z := DTBPL;
if Z > DTBPU then Z := DTBPU;
PArr[K].Y := Z;
{add wide profile}
if PrevW > 0 then
begin
WNX := X;
WNY := Y;
WPX := X;
WPY := Y;
OLP := 0;
OLN := 0;
for L:=1 to round(PrevW) do
begin
WNX := WNX + YInc; {increments are inverted because slope is}
WNY := WNY - XInc; {perpendicular to profile}
WPX := WPX - YInc;
WPY := WPY + XInc;
{add negative profile}
WI := round(WNY);
WJ := round(WNX);
if (WJ >= 0) and (WJ < LimX) and (WI >= 0) and (WI < LimY) then
begin
Z := Beam.Data[WI,WJ+2];
if Z < DTBPL then Z := DTBPL;
if Z > DTBPU then Z := DTBPU;
PArr[K].Y := PArr[K].Y + Z;
inc(OLN);
end
else
OverLimit := true;
{add positive profile}
WI := round(WPY);
WJ := round(WPX);
if (WJ >= 0) and (WJ < LimX) and (WI >= 0) and (WI < LimY) then
begin
Z := Beam.Data[WI,WJ+2];
if Z < DTBPL then Z := DTBPL;
if Z > DTBPU then Z := DTBPU;
PArr[K].Y := PArr[K].Y + Z;
inc(OLP);
end
else
OverLimit := true;
end;
if OverLimit then PArr[K].Y := PArr[K].Y*Width/(OLN+1+OLP);
end;
{add rest of points}
repeat
X := X + XInc;
Y := Y + YInc;
I := Round(Y);
J := Round(X);
Inc(K);
OverLimit := false;
PArr[K].X := sqrt(sqr((X - Start.X)*Beam.XRes) + sqr((Y - Start.Y)*Beam.YRes)) - MidP;
Z := Beam.Data[I,J+2];
if Z < DTBPL then Z := DTBPL;
if Z > DTBPU then Z := DTBPU;
PArr[K].Y := Z;
{add wide profiles}
if PrevW > 0 then
begin
WNX := X;
WNY := Y;
WPX := X;
WPY := Y;
OLP := 0;
OLN := 0;
for L:=1 to round(PrevW) do
begin
WNX := WNX + YInc; {increments are inverted because slope is}
WNY := WNY - XInc; {perpendicular to profile}
WPX := WPX - YInc;
WPY := WPY + XInc;
{add negative profile}
WI := round(WNY);
WJ := round(WNX);
if (WJ >= 0) and (WJ < LimX) and (WI >= 0) and (WI < LimY) then
begin
Z := Beam.Data[WI,WJ+2];
if Z < DTBPL then Z := DTBPL;
if Z > DTBPU then Z := DTBPU;
PArr[K].Y := PArr[K].Y + Z;
inc(OLN);
end
else
OverLimit := true;
{add positive profile}
WI := round(WPY);
WJ := round(WPX);
if (WJ >= 0) and (WJ < LimX) and (WI >= 0) and (WI < LimY) then
begin
Z := Beam.Data[WI,WJ+2];
if Z < DTBPL then Z := DTBPL;
if Z > DTBPU then Z := DTBPU;
PArr[K].Y := PArr[K].Y + Z;
inc(OLP);
end
else
OverLimit := true;
end;
if OverLimit then PArr[K].Y := PArr[K].Y*Width/(OLN+1+OLP);
end;
until (I = Stop.Y) and (J = Stop.X);
{normalise array}
CMax := 0;
CMin := 0;
if Normalisation <> none then
begin
CMax := PArr[(length(PArr) - 1) div 2].Y;
if Normalisation = norm_max then
for K := 0 to length(PArr) - 1 do
if PArr[K].Y > CMax then
CMax := PArr[K].Y;
CMin := CMax;
for K := 0 to length(PArr) - 1 do
if PArr[K].Y < CMin then
CMin := PArr[K].Y;
end;
{write array to profile}
for K := 0 to length(PArr) - 1 do with PArr[K] do
begin
if Normalisation <> none then
Y := (Y - CMin)*100/(CMax - CMin);
if (X <> 0) or (Y <> 0) then
Profile.AddXY(X,Y,'',clRed)
end;
end;
{ TBSForm }
procedure TBSForm.StatusBarDrawPanel(SBar: TStatusBar;Panel: TStatusPanel;
const Rect: TRect);
begin
with SBar.Canvas do
begin
Brush.Color := StatusBar.Color;
FillRect(Rect);
TextRect(Rect,2 + Rect.Left, 1 + Rect.Top,Panel.Text) ;
end;
end;
procedure TBSForm.BSError(sError:string);
begin
StatusBar.SimpleText := sError;
StatusBar.Color := FaintRed;
StatusMessages.Add(StatusBar.SimpleText);
StatusBar.Hint := StatusMessages.Text;
end;
procedure TBSForm.BSWarning(sWarning:string);
begin
StatusBar.SimpleText := sWarning;
StatusBar.Color := FaintYellow;
StatusMessages.Add(StatusBar.SimpleText);
StatusBar.Hint := StatusMessages.Text;
end;
procedure TBSForm.BSMessage(sMess:string);
begin
StatusBar.SimpleText := sMess;
StatusBar.Color := FaintGreen;
StatusMessages.Add(StatusBar.SimpleText);
StatusBar.Hint := StatusMessages.Text;
end;
procedure TBSForm.ClearStatus;
begin
StatusBar.SimpleText := '';
StatusBar.Color := clDefault;
end;
procedure TBSForm.miExitClick(Sender: TObject);
begin
Close;
end;
procedure TBSForm.miExportXClick(Sender: TObject);
var Outfile: textfile;
I: integer;
begin
SaveDialog.Title := 'Export profile as';
{$ifdef WINDOWS}
SaveDialog.Filter := 'Text files|*.txt|All files|*.*';
{$else}
SaveDialog.Filter := 'Text files|*.txt|All files|*';
{$endif}
SaveDialog.InitialDir := OpenDialog.InitialDir;
SaveDialog.DefaultExt := '.txt';
SaveDialog.FileName := ExtractFileNameOnly(OpenDialog.FileName);
if XPArr <> nil then
if SaveDialog.Execute then
begin
AssignFile(Outfile,SaveDialog.FileName);
Rewrite(Outfile);
for I := 0 to length(XPArr) - 1 do
writeln(Outfile,XPArr[I].X:5:2,',',XPArr[I].Y:5:1);
{writeln(Outfile,XPArr[I].X,',',XPArr[I].Y);}
CloseFile(Outfile);
end;
end;
procedure TBSForm.miExportYClick(Sender: TObject);
var Outfile: textfile;
I: integer;
begin
SaveDialog.Title := 'Export profile as';
{$ifdef WINDOWS}
SaveDialog.Filter := 'Text files|*.txt|All files|*.*';
{$else}
SaveDialog.Filter := 'Text files|*.txt|All files|*';
{$endif}
SaveDialog.InitialDir := OpenDialog.InitialDir;
SaveDialog.DefaultExt := '.txt';
SaveDialog.FileName := ExtractFileNameOnly(OpenDialog.FileName);
if YPArr <> nil then
if SaveDialog.Execute then
begin
AssignFile(Outfile,SaveDialog.FileName);
Rewrite(Outfile);
for I := 0 to length(YPArr) - 1 do
writeln(Outfile,YPArr[I].X:5:2,',',YPArr[I].Y:5:1);
{writeln(Outfile,YPArr[I].X,',',YPArr[I].Y);}
CloseFile(Outfile);
end;
end;
procedure TBSForm.miXClipBClick(Sender: TObject);
{put the array in a comma delimited string on the clipboard}
var I :integer;
sClip :string;
begin
if XPArr <> nil then
begin
sClip := '';
for I := 0 to length(XPArr) - 1 do
sClip := sClip + FloatToStrF(XPArr[I].X,ffFixed,5,2) + ',' +
FloatToStrF(XPArr[I].Y,ffFixed,5,2) + LineEnding;
Clipboard.AsText := sClip;
end;
end;
procedure TBSForm.miYClipBClick(Sender: TObject);
{put the array in a comma delimited string on the clipboard}
var I :integer;
sClip :string;
begin
if YPArr <> nil then
begin
sClip := '';
for I := 0 to length(YPArr) - 1 do
sClip := sClip + FloatToStrF(YPArr[I].X,ffFixed,5,2) + ',' +
FloatToStrF(YPArr[I].Y,ffFixed,5,2) + LineEnding;
Clipboard.AsText := sClip;
end;
end;
procedure TBSForm.miResClipClick(Sender: TObject);
begin
sgResults.CopyToClipboard(false);
end;
procedure TBSForm.pmContextPopup(Sender: TObject);
begin
if pmContext.PopupComponent = pResults then
begin
pmiCopyToFile.Enabled := false;
pmicopyToFile.Visible := false;
end
else
begin
pmiCopyToFile.Enabled := true;
pmicopyToFile.Visible := true;
end;
end;
procedure TBSForm.pmiCopyClipClick(Sender: TObject);
begin
if pmContext.PopupComponent = pXProfile then miXClipBClick(Sender);
if pmContext.PopupComponent = pYProfile then miYClipBClick(Sender);
if pmContext.PopupComponent = pResults then miResClipClick(Sender);
end;
procedure TBSForm.pmiCopytoFileClick(Sender: TObject);
begin
if pmContext.PopupComponent = pXProfile then miExportXClick(Sender);
if pmContext.PopupComponent = pYProfile then miExportYClick(Sender);
end;
procedure TBSForm.bbSavePClick(Sender: TObject);
var I :integer;
ProtName, {protocol name}
ProtPath :string; {protocol file path}
begin
ClearStatus;
ProtName := cbProtocol.Text;
if ProtName <> '' then
begin
SaveDialog.Title := 'Save parameters';
{$ifdef WINDOWS}
ProtPath := GetAppConfigDir(true);
SaveDialog.Filter := 'Comma delimited files|*.csv|All files|*.*';
{$else}
ProtPath := GetAppConfigDir(false);
SaveDialog.Filter := 'Comma delimited files|*.csv|All files|*';
{$endif}
if not DirectoryExists(ProtPath) then CreateDir(ProtPath);
SaveDialog.DefaultExt := '.csv';
SaveDialog.FileName := AppendPathDelim(ProtPath) + DelSpace(ProtName) + '.csv';
for I:=1 to sgResults.Rowcount-1 do
begin
sgResults.Cells[2,I] := '';
sgResults.Cells[3,I] := '';
end;
if SaveDialog.Execute then
begin
try;
sgResults.SaveToCSVFile(SaveDialog.FileName,',',false);
except
on E:Exception do
BSError('Could not save protocol.');
end;
cbProtocol.Style := csDropDownList;
sbSaveP.Enabled := false;
sbSaveP.Visible := false;
miSaveP.Enabled := false;
miEditP.Enabled := true;
miQuitEdit.Enabled := false;
sbAddP.Enabled := false;
sbAddP.Visible := false;
sbDelP.Enabled := false;
sbDelP.Visible := false;
sbExitP.Enabled := false;
sbExitP.Visible := false;
CResults.Caption := 'Results';
sgResults.Columns.Items[1].Visible := false;
sgResults.Columns.Items[2].Visible := true;
sgResults.Columns.Items[3].Visible := true;
sgResults.Options := sgResults.Options - [goEditing];
BuildProtocolList;
if cbProtocol.Items.IndexOf(ProtName) >= 0 then
cbProtocol.ItemIndex := cbProtocol.Items.IndexOf(ProtName);
LoadProtocol;
seXAngleChange(Self);
seYAngleChange(Self);
Editing := false;
end;
end
else
BSWarning('Please define a protocol name.');
end;
procedure TBSForm.cbProtocolChange(Sender: TObject);
begin
ClearStatus;
LoadProtocol;
seXAngleChange(Self);
seYAngleChange(Self);
end;
procedure TBSForm.FormClose(Sender: TObject; var CloseAction: TCloseAction);
begin
FileHandler.Free;
CGIHandler.Free;
PHPCGIHandler.Free;
StatusMessages.Free;
Beam.Free
end;
procedure TBSForm.sbPDFClick(Sender: TObject);
begin
SaveDialog.Title := 'Save PDF as';
{$ifdef WINDOWS}
SaveDialog.Filter := 'PDF files|*.pdf|All files|*.*';
{$else}
SaveDialog.Filter := 'PDF files|*.pdf|All files|*';
{$endif}
SaveDialog.InitialDir := OpenDialog.InitialDir;
SaveDialog.DefaultExt := '.pdf';
SaveDialog.FileName := ExtractFileNameOnly(OpenDialog.FileName) + '.pdf';
if SaveDialog.Execute then FormToPDF(BSForm,SaveDialog.FileName);
end;
procedure TBSForm.bbExitPClick(Sender: TObject);
begin
ClearStatus;
cbProtocol.Style := csDropDownList;
sbSaveP.Enabled := false;
sbSaveP.Visible := false;
miSaveP.Enabled := false;
miEditP.Enabled := true;
miQuitEdit.Enabled := false;
sbAddP.Enabled := false;
sbAddP.Visible := false;
sbDelP.Enabled := false;
sbDelP.Visible := false;
sbExitP.Enabled := false;
sbExitP.Visible := false;
cResults.Caption := 'Results';
sgResults.Columns.Items[1].Visible := false;
sgResults.Columns.Items[2].Visible := true;
sgResults.Columns.Items[3].Visible := true;
sgResults.Options := sgResults.Options - [goEditing];
Editing := false;
LoadProtocol; {reload to wipe changes}
seXAngleChange(Self);
seYAngleChange(Self);
end;
procedure TBSForm.bbAddLClick(Sender: TObject);
begin
if sgResults.Row >= 0 then sgResults.InsertRowWithValues(sgResults.Row,['','','','']);
end;
procedure TBSForm.bbDelLClick(Sender: TObject);
begin
if sgResults.Row >= 0 then sgResults.DeleteRow(sgResults.Row);
end;
procedure TBSForm.BuildProtocolList;
var sExePath,
sSearchPath,
FileName :string;
ProtList :TStringList;
SearchRec :TSearchRec;
begin
{look for parameter definition files and make list}
sExePath := ExtractFilePath(Application.ExeName);
sProtPath := AppendPathDelim(sExePath) + 'Protocols';
sSearchPath := AppendPathDelim(sProtPath) + '*.csv';
cbProtocol.Clear;
ProtList := TStringList.Create;
if FindFirst(sSearchPath,0,SearchRec) = 0 then
begin
repeat
FileName := ExtractFileNameOnly(SearchRec.Name);
ProtList.Add(Filename);