end;
//графическое отображение рационального раскроя
procedure drawRationalCut(
image: TImage;
materialLength: integer;
detailAmount: integer;
details: array of TDetail;
x: array of integer);
var
i, j: integer;
sum: integer;
k_x: real;
curr_x: integer;
curr_x_scr: real;
begin
sum:=0;
for i:=0 to detailAmount-1 do
begin
sum:=sum+x[i]*details[i].l;
end;
k_x:=image.width/materialLength;
with image.Canvas do
begin
brush.Style:=bsSolid;
brush.Color:=clBtnFace;
fillRect(rect(0, 0, image.width, image.height));
brush.Color:=clGray;
pen.Color:=clGray;
rectangle(trunc(k_x*sum), 0, trunc(k_x*materialLength), 50);
brush.Color:=clWhite;
pen.Color:=clGray;
rectangle(0, 0, trunc(k_x*sum), 50);
pen.Color:=clRed;
brush.Style:=bsClear;
textOut(0,51,'0');
curr_x:=0;
curr_x_scr:=0;
for i:=0 to detailAmount-1 do
begin
for j:=0 to x[i]-1 do
begin
curr_x:=curr_x+details[i].l;
curr_x_scr:=curr_x_scr+k_x*details[i].l;
if curr_x<>materialLength then
begin
moveTo(trunc(curr_x_scr),0);
lineTo(trunc(curr_x_scr),50);
textOut(trunc(curr_x_scr), 51, intToStr(curr_x));
// textOut(trunc(curr_x_scr-15), 21, '('+intToStr(i+1)+')');
end;
end;
end;
end;
end;
//выход из программы
procedure TForm_Main.Button_ExitClick(Sender: TObject);
begin
close;
end;
//изменение кол-ва детеалей
procedure TForm_Main.Edit_DetailAmountChange(Sender: TObject);
var
new_d_a: integer;
i: integer;
begin
new_d_a:=strToInt(Form_Main.Edit_DetailAmount.Text);
if (new_d_a>=1) then
begin
if (new_d_a<=MAX_DETAIL_AMOUNT) then
begin
Form_Main.StringGrid_In.RowCount:=new_d_a+1;
for i:=1 to new_d_a do
begin
Form_Main.StringGrid_In.Cells[0,i]:=intToStr(i);
end;
end
else
begin
Form_Main.Edit_DetailAmount.Text:=intToStr(MAX_DETAIL_AMOUNT);
end;
end
else
begin
Form_Main.Edit_DetailAmount.Text:=intToStr(1);
end;
end;
//инициализация программы
procedure TForm_Main.FormCreate(Sender: TObject);
begin
Form_Main.UpDown_MaterialLength.Min:=1;
Form_Main.UpDown_MaterialLength.Max:=MAX_MATERIAL_LENGTH;
Form_Main.UpDown_DetailAmount.Min:=1;
Form_Main.UpDown_DetailAmount.Max:=MAX_DETAIL_AMOUNT;
Form_Main.StringGrid_In.Cells[0,0]:='№ детали';
Form_Main.StringGrid_In.Cells[1,0]:='Длина';
Form_Main.StringGrid_In.Cells[2,0]:='Цена';
Form_Main.StringGrid_In.Cells[0,1]:='1';
Form_Main.StringGrid_Out1.Cells[0,0]:='№ детали';
Form_Main.StringGrid_Out1.Cells[1,0]:='Количество';
end;
//изменение длины материала
procedure TForm_Main.Edit_MaterialLengthChange(Sender: TObject);
var
new_m_l: integer;
begin
new_m_l:=strToInt(Form_Main.Edit_MaterialLength.Text);
if (new_m_l>=1) then
begin
if not (new_m_l<=MAX_MATERIAL_LENGTH) then
begin
Form_Main.Edit_MaterialLength.Text:=intToStr(MAX_MATERIAL_LENGTH);
end;
end
else
begin
Form_Main.Edit_MaterialLength.Text:=intToStr(1);
end;
end;
//сортировка данных по возрастанию длины детали
procedure StrGridSort;
var
i: integer;
do_next: boolean;
begin
do_next:=true;
while do_next do
begin
do_next:=false;
for i:=1 to Form_Main.StringGrid_In.RowCount-2 do
begin
if strToInt(Form_Main.StringGrid_In.Cells[1,i])>
strToInt(Form_Main.StringGrid_In.Cells[1,i+1]) then
begin
Form_Main.StringGrid_In.cols[1].Exchange(i,i+1);
Form_Main.StringGrid_In.cols[2].Exchange(i,i+1);
do_next:=true;
end;
end;
end;
end;
//вычисление рационального раскроя и отображение результата
procedure TForm_Main.Button_CalculateClick(Sender: TObject);
var
i,sum,cost: integer;
begin
strGridSort;
updateData;
searchRationalCut(materialLength, detailAmount, details, x);
Form_Main.StringGrid_Out1.RowCount:=detailAmount+1;
sum:=0; cost:=0;
for i:=1 to detailAmount do
begin
Form_Main.StringGrid_Out1.Cells[0,i]:=intToStr(i);
Form_Main.StringGrid_Out1.Cells[1,i]:=intToStr(x[i]);
sum:=sum+x[i]*details[i].l;
cost:=cost+x[i]*details[i].c;
end;
Form_Main.Edit1.Text:=intToStr(cost);
Form_Main.Edit2.Text:=intToStr(materialLength-sum);
drawRationalCut(Form_Main.Image_Cut, materialLength, detailAmount, details, x);
end;
procedure TForm_Main.Button1Click(Sender: TObject);
begin
Form2.Show;
end;
end.
Литература
1. Э.А. Мухачева "Рациональный раскрой промышленных материалов". Москва, Машиностроение, 1984 г.
2. Э.А. Мухачева, Г.Ш. Рубинштейн "Математическое программирование". Новосибирск, Наука, 1977 г.