Смекни!
smekni.com

Исследование методов автоматизированного проектирования динамических систем (стр. 24 из 25)

begin

TToolButton(InstControl).OnClick(InstControl);

TAbstractElement(NewElement).LoadFromFile(Stream);

Editor.ListBox1.Items.AddObject(TAbstractElement(NewElement).ObjName, NewElement);

TAbstractElement(NewElement).InputParamDlg.Caption:=TAbstractElement(NewElement).ObjName;

TAbstractElement(NewElement).GraphDlg.Caption:=TAbstractElement(NewElement).ObjName;

if TAbstractElement(NewElement).flType then inc(Count.Masses)

else inc(Count.Relations);

end

else begin


MessageDlg('Неизвестный компонент "'+InstComponentName+'"', mtError, [mbOk], 0 );

exit;

end;

end;

CheckState;

except

raise ERangeError.Create('Ошибка чтения');

end;

end;

function TModel.LoadModel(FileName: string):boolean;

var

i: integer;

Stream: TStream;

Ext: string;

IniF: TIniFile;

begin

Result:=false;

Ext:=UpperCase(ExtractFileExt(FileName));

if Ext='.MDL' then

try

Stream := TFileStream.Create(fileName, fmOpenRead);

ClearData;

flReadFromFile:=true;

LoadFromFile(Stream);

finally

Stream.Free;

Editor.Memo1.Lines.Clear;

TInputDlg(InputParamDlg).RadioGroup1.ItemIndex:=Round(CalcMethod.code);

flReadFromFile:=false;

flIsSaved:=true;

Result:=true;

{//Warning! Add new parameters !

for i:=0 to Editor.ListBox1.Items.Count-1 do

if not TAbstractElement(Editor.ListBox1.Items.Objects[i]).flType then

TAbstractElement(Editor.ListBox1.Items.Objects[i]).AddParam;

flIsSaved:=false;

}

end

else

try

IniF:=TIniFile.Create(FileName);

ClearData;

flReadFromFile:=true;

LoadFromIniFile(IniF,'Model');

finally

IniF.Free;

Editor.Memo1.Lines.Clear;

TInputDlg(InputParamDlg).RadioGroup1.ItemIndex:=Round(CalcMethod.code);

flReadFromFile:=false;

flIsSaved:=true;

Result:=true;

end;

end;

function TModel.CheckModel: boolean;

var i: integer;

begin

Result:=true;

for i:=0 to Editor.ListBox1.Items.Count-1 do

if not TAbstractElement(Editor.ListBox1.Items.Objects[i]).flType then

TAbstractRelation(Editor.ListBox1.Items.Objects[i]).SetPointers(Editor.ListBox1.Items);

for i:=0 to Editor.ListBox1.Items.Count-1 do

if TAbstractElement(Editor.ListBox1.Items.Objects[i]).flType then

if not TAbstractMassa(Editor.ListBox1.Items.Objects[i]).flIsRelated then

begin

Result:=false;

MessageDlg(TAbstractMassa(Editor.ListBox1.Items.Objects[i]).InputParamDlg.Caption+' не связана',

mtError, [mbOk], 0);

exit;

end;

for i:=0 to Editor.ListBox1.Items.Count-1 do

if not TAbstractElement(Editor.ListBox1.Items.Objects[i]).flType then

if TAbstractRelation(Editor.ListBox1.Items.Objects[i]).CheckPointers then

begin

Result:=false;

MessageDlg(TAbstractRelation(Editor.ListBox1.Items.Objects[i]).InputParamDlg.Caption+

' не объединяет 2 массы', mtError, [mbOk], 0);

exit;

end;

end;

procedure TModel.SetParamsInZero;

var i: integer;

begin

for i:=0 to Editor.ListBox1.Items.Count-1 do

TAbstractElement(Editor.ListBox1.Items.Objects[i]).SetParamsInZero;

with VisualDlg do begin

Time:=0;

CountSteps:=0;

Caption:=Editor.Edit1.Text;

RealModelTime:=0;

StartT:=0;

end;

case Round(CalcMethod.code) of

0: CalcMethod.digree:=4;

1: CalcMethod.digree:=2;

2: CalcMethod.digree:=1;

end;//case

end;

procedure TModel.Visio;

var i,j: integer;

begin

SetParamsInZero;

if not CheckModel then exit;

with VisualDlg do begin

TopMargin:= 32000 div 2;

MaxX:=-1e20;

MaxY:=-1e20;

MinX:=1e20;

MinY:=1e20;

for i:=0 to Editor.ListBox1.Items.Count-1 do

begin

if TAbstractElement(Editor.ListBox1.Items.Objects[i]).RecLeft<MinX then MinX:=TAbstractElement(Editor.ListBox1.Items.Objects[i]).RecLeft;

if TAbstractElement(Editor.ListBox1.Items.Objects[i]).RecRight>MaxX then MaxX:=TAbstractElement(Editor.ListBox1.Items.Objects[i]).RecRight;

if TAbstractElement(Editor.ListBox1.Items.Objects[i]).RecTop<MinY then MinY:=TAbstractElement(Editor.ListBox1.Items.Objects[i]).RecTop;

if TAbstractElement(Editor.ListBox1.Items.Objects[i]).RecBottom>MaxY then MaxY:=TAbstractElement(Editor.ListBox1.Items.Objects[i]).RecBottom;

end;

try

ScaleX:= ScrollBox1.ClientWidth*0.8 / abs(MaxX-MinX);

ScaleY:=ScrollBox1.ClientHeight*0.95 / abs(MaxY-MinY);

except

raise ERangeError.Create('Деление на ноль'#10'Некорректные геометрические параметры модели');

exit;

end;

LeftMargin:=(ScrollBox1.ClientWidth - Round((MaxX-MinX)*ScaleX)) div 2 ;

for i:=0 to Editor.ListBox1.Items.Count-1 do

if TAbstractElement(Editor.ListBox1.Items.Objects[i]).flType then

TAbstractElement(Editor.ListBox1.Items.Objects[i]).SetImageSize(MinX, MinY, ScaleX, ScaleY, TopMargin, LeftMargin);

for i:=0 to Editor.ListBox1.Items.Count-1 do

begin

if not TAbstractElement(Editor.ListBox1.Items.Objects[i]).flType then

TAbstractElement(Editor.ListBox1.Items.Objects[i]).SetImageSize(MinX, MinY, ScaleX, ScaleY, TopMargin, LeftMargin);

ScrollBox1.InsertControl(TAbstractElement(Editor.ListBox1.Items.Objects[i]).Image);

end;

//Sort relation by order

Editor.SortRelationListBox.Clear;

for i:=0 to Editor.ListBox1.Items.Count-1 do

if not TAbstractElement(Editor.ListBox1.Items.Objects[i]).flType then

Editor.SortRelationListBox.Items.AddObject(Editor.ListBox1.Items.Strings[i],

Editor.ListBox1.Items.Objects[i]);

for i:=0 to Editor.SortRelationListBox.Items.Count-2 do

for j:=i+1 to Editor.SortRelationListBox.Items.Count-1 do

if TAbstractRelation(Editor.SortRelationListBox.Items.Objects[i]).Order>

TAbstractRelation(Editor.SortRelationListBox.Items.Objects[j]).Order

then Editor.SortRelationListBox.Items.Exchange(i,j);

//Set range image

RangeImage:= TImage.Create(ScrollBox1);

RangeImage.Left:=LeftMargin;

RangeImage.Top:=TopMargin *2;

RangeImage.Height:=1;

RangeImage.Width:=1;

ScrollBox1.InsertControl(RangeImage);

ScrollBox1.VertScrollBar.Position:=TopMargin;

VisualDlg.StatusBar1.SimpleText:=Format(' Итерация %d Время %-10.4f ( %d%% ) Текущий шаг %-10.4f',

[VisualDlg.CountSteps, VisualDlg.time, Round(100*VisualDlg.time/t), VisualDlg.StepT] );

ShowModal;

flIsModelling:=true;

for i:=0 to Editor.ListBox1.Items.Count-1 do

ScrollBox1.RemoveControl(TAbstractElement(Editor.ListBox1.Items.Objects[i]).Image);

ScrollBox1.RemoveControl(RangeImage);

RangeImage.Destroy;

end;

Editor.SortRelationListBox.Clear;

for i:=0 to Editor.ListBox1.Items.Count-1 do

TAbstractElement(Editor.ListBox1.Items.Objects[i]).GetParams; //restore parameters

end;

procedure TModel.Calculate(Sender: TObject);

var

i, j, code: integer;

h: TFloat;

begin

if VisualDlg.Time < t then

begin

VisualDlg.Timer1.Enabled:=false;

inc(VisualDlg.CountSteps);

code:=Round(CalcMethod.code);

for j:=1 to CalcMethod.digree do

begin

for i:=0 to Editor.SortRelationListBox.Items.Count-1 do

begin

TAbstractRelation(Editor.SortRelationListBox.Items.Objects[i]).CalcForce([VisualDlg.Time, j]);

if j=1 then TAbstractRelation(Editor.SortRelationListBox.Items.Objects[i]).AddValueToChart(VisualDlg.Time);

end;

{

for i:=0 to Editor.ListBox1.Items.Count-1 do

if not TAbstractElement(Editor.ListBox1.Items.Objects[i]).flType then

TAbstractRelation(Editor.ListBox1.Items.Objects[i]).CalcForce([VisualDlg.Time]);

}

if j = 1 then begin //calculate min step

VisualDlg.StepT:=ht;

for i:=0 to Editor.ListBox1.Items.Count-1 do

if TAbstractElement(Editor.ListBox1.Items.Objects[i]).flType then

begin

h:=TAbstractMassa(Editor.ListBox1.Items.Objects[i]).CalcStep(e, ht);

if h < VisualDlg.StepT then VisualDlg.StepT:=h;

if VisualDlg.Time = 0 then

TAbstractMassa(Editor.ListBox1.Items.Objects[i]).AddValueToChart(VisualDlg.Time);

end;

VisualDlg.Time:=VisualDlg.Time + VisualDlg.StepT;

end;

for i:=0 to Editor.ListBox1.Items.Count-1 do

if TAbstractElement(Editor.ListBox1.Items.Objects[i]).flType then

TAbstractMassa(Editor.ListBox1.Items.Objects[i]).Calculate(VisualDlg.StepT, VisualDlg.Time, Code, j);

end; //for j

for i:=0 to Editor.ListBox1.Items.Count-1 do

if TAbstractElement(Editor.ListBox1.Items.Objects[i]).flType then

with VisualDlg do

TAbstractElement(Editor.ListBox1.Items.Objects[i]).MoveImage(MinX, MinY, ScaleX, ScaleY, TopMargin, LeftMargin);

for i:=0 to Editor.ListBox1.Items.Count-1 do

if not TAbstractElement(Editor.ListBox1.Items.Objects[i]).flType then

with VisualDlg do

TAbstractElement(Editor.ListBox1.Items.Objects[i]).MoveImage(MinX, MinY, ScaleX, ScaleY, TopMargin, LeftMargin);

VisualDlg.StatusBar1.SimpleText:=Format(' Итерация %d Время %-10.4f ( %d%% ) Текущий шаг %-8.6f',

[VisualDlg.CountSteps, VisualDlg.Time, Round(100*VisualDlg.time/t), VisualDlg.StepT] );

VisualDlg.Timer1.Enabled:=true;

end

else begin

VisualDlg.ToolButton1Click(Sender);

VisualDlg.ToolButton1.Enabled:=false;

MessageDlg('Расчет окончен'#10+'Время моделирования '+TimeToStr(VisualDlg.RealModelTime)+

#10+format('Средний шаг интегрирования %-7.6f',[VisualDlg.Time/VisualDlg.CountSteps]),

mtInformation, [mbOk], 0);

end;

end;

procedure TModel.ShowGraphs;

var i: integer;

begin

{

for i:=0 to Editor.ListBox1.Items.Count-1 do

TAbstractMassa(Editor.ListBox1.Items.Objects[i]).ShowGraphs;

}

MakeReportDlg(1);

end;

procedure TModel.HideGraphs;

var i: integer;

begin

VisualDlg.Timer1.Enabled:=false;

for i:=0 to Editor.ListBox1.Items.Count-1 do

TAbstractMassa(Editor.ListBox1.Items.Objects[i]).HideGraphs;

end;

procedure TModel.ShowReport(FName: string);

var

FN: array[0..255] of Char;

begin

ShellExecute(Editor.Handle, nil,

StrPCopy(FN, FName), nil, nil, Sw_Show);

end;

procedure TModel.MakeReport(FName: string);

begin

WaitReportCreateDlg.Hint:=FName;

WaitReportCreateDlg.ShowModal;

end;

procedure TModel.WaitReportCreateDlgOnShow(Sender: TObject);

begin

with TWaitReportCreateDlg(Sender) do

begin

StaticText1.Font.Color:=clBlue;

StaticText1.Caption:='Создаётся отчёт...';

WaitReportCreateDlg.Refresh;

Animate1.Active:=true;

Animate1.CommonAVI:=aviCopyFile;

if CreateReport(WaitReportCreateDlg.Hint) then

begin

StaticText1.Caption:='Отчёт создан';

ModalResult:=mrOk;

end

else

begin

StaticText1.Font.Color:=clRed;

StaticText1.Caption:='Ошибка создания документа WinWord';

ModalResult:=mrCancel;

end;

Animate1.Active:=false;

end;

end;

function TModel.CreateReport(FName: string): boolean;

var

qi: TQRImage;

i: integer;

ScaleX, ScaleY: TFloat;

TopMargin, LeftMargin: integer;

m: TMetafile;

Report: TQuickRep;

begin

//Result:=true;

WDoc := CreateOleObject('Word.Basic');

Result:= VarType(WDoc) = VarDispatch;

if Result then

try

WDoc.FileNew('Normal');

WDoc.FilePageSetup(PaperSize:='9', TopMargin:='2 см', BottomMargin:='2 см',

LeftMargin:='2 см', RightMargin:='2 см', PageWidth:='21 см',

PageHeight:='29,7 см', Orientation:=0);

WDoc.FormatFont(Points:=14, Font:='Times New Roman Cyr',Bold:=1);

WDoc.FormatParagraph(After:='12 пт',LineSpacingRule:=1,Alignment:=1);

WDoc.Insert('Результаты моделирования'+ #13);

WDoc.FormatFont(Points:=14, Font:='Times New Roman Cyr',Bold:=0);

WDoc.FormatParagraph(Alignment:=1);

WDoc.Insert('Модель: '+Editor.Edit1.Text+#13);

Report:= TQuickRep.CreateParented(0);

Report.Units:=pixels;//TQRUNIT(2);

TopMargin:=0;

ScaleX:= Report.Page.Width / abs(VisualDlg.MaxX-VisualDlg.MinX);

ScaleY:= Report.Page.Length / abs(VisualDlg.MaxY-VisualDlg.MinY);

LeftMargin:=Round(Report.Page.Width - (VisualDlg.MaxX-VisualDlg.MinX)*ScaleX) div 2 ;

for i:=0 to Editor.ListBox1.Items.Count-1 do

if TAbstractElement(Editor.ListBox1.Items.Objects[i]).flType then

TAbstractElement(Editor.ListBox1.Items.Objects[i]).SetImageSize(VisualDlg.MinX,

VisualDlg.MinY, ScaleX, ScaleY, TopMargin, LeftMargin);

for i:=0 to Editor.ListBox1.Items.Count-1 do

begin

if not TAbstractElement(Editor.ListBox1.Items.Objects[i]).flType then

TAbstractElement(Editor.ListBox1.Items.Objects[i]).SetImageSize(VisualDlg.MinX,

VisualDlg.MinY, ScaleX, ScaleY, TopMargin, LeftMargin);

qi:= TQRImage.Create(Report);

qi.AutoSize:=false;

qi.Center:=true;

qi.Stretch:=true;

qi.Name:=TAbstractElement(Editor.ListBox1.Items.Objects[i]).Image.Name;

qi.Left:=TAbstractElement(Editor.ListBox1.Items.Objects[i]).Image.Left;

qi.Top:= TAbstractElement(Editor.ListBox1.Items.Objects[i]).Image.Top;

qi.Height:= TAbstractElement(Editor.ListBox1.Items.Objects[i]).Image.Height;

qi.Width:= TAbstractElement(Editor.ListBox1.Items.Objects[i]).Image.Width;

qi.Picture:=TAbstractElement(Editor.ListBox1.Items.Objects[i]).Image.Picture;

Report.InsertControl(qi);

qi:=nil;

end; //for

Report.Prepare;

m:=Report.QRPrinter.GetPage(Report.QRPrinter.PageNumber);

if m<>nil then begin

m.SaveToFile(TempFName+'wmf');

WDoc.InsertPicture(Name:=TempFName+'wmf',LinkToFile:='0');

{

clipboard.Assign(m);

WDoc.EditPaste;

}

end

else

Result:=false;

Report.Destroy;

WDoc.InsertPara;

for i:=0 to Editor.ListBox1.Items.Count-1 do

begin

WDoc.FormatFont(Points:=14);

WDoc.FormatParagraph(After:='0 пт',Alignment:=3);

WDoc.Insert(IntToStr(i+1)+' - '+TAbstractElement(Editor.ListBox1.Items.Objects[i]).AboutStr+#13);

end;

MakeReportDlg(0);

WaitReportCreateDlg.Refresh;

WDoc.FileSaveAs(ChangeFileExt(FName, '.doc'));

WDoc.FileClose;

except

Result:=false;

end;

WDoc:= UnAssigned;

end;

procedure TModel.OnClickSeriesCheckBox(Sender: TObject);

begin

ReportDlg.Chart1.Series[TCheckBox(Sender).Tag].Active:=TCheckBox(Sender).Checked;

//ReportDlg.Chart1.Series[TCheckBox(Sender).Tag].ShowInLegend:=TCheckBox(Sender).Checked;

end;

procedure TModel.OnClickSaveButtonOnReportDlg(Sender: TObject);

var

i: integer;

begin

try

if ReportDlg.Tag = 0 then

begin

WDoc.Insert(#13);

ReportDlg.Chart1.SaveToMetafile(TempFName+'wmf');

WDoc.InsertPicture(Name:=TempFName+'wmf',LinkToFile:='0');

WDoc.Insert(#13);

for i:=0 to ReportDlg.Chart1.SeriesCount-1 do

if ReportDlg.Chart1.Series[i].Active then

begin

WDoc.Insert(ReportDlg.Chart1.Series[i].Title+#13);

end;

end

else

ReportDlg.Chart1.CopyToClipboardMetafile(true);

except

MessageDlg('Сбой при создания отчета', mtError, [mbOk], 0 );

end;

end;

function TModel.GetModelInformation:string;

var

i: integer;

Mas, C: TFloat;

begin

Mas:=0;

C:=0;

for i:=0 to Editor.ListBox1.Items.Count-1 do

if TAbstractElement(Editor.ListBox1.Items.Objects[i]).flType then

Mas:=Mas+TAbstractMassa(Editor.ListBox1.Items.Objects[i]).GetMassa

else

if TAbstractRelation(Editor.ListBox1.Items.Objects[i]).c<>0

then

C:=C+1/TAbstractRelation(Editor.ListBox1.Items.Objects[i]).c;

Result:=format('Масса колебательной системы = %-10.3f'#13+

'Жесткость системы %-10.3f',[Mas,1/C]);

end;

procedure TModel.ModelInformation(Sender: TObject);

begin

MessageDlg(GetModelInformation,

mtInformation, [mbOK], 0) ;

end;

function TModel.MakeReportDlg;

var

j,n,i,k: integer;

chart: TChart;

ser: TChartSeries;

st,bst: TStaticText;

ch: TCheckBox;

ss: string;

begin

ReportDlg:= TReportDlg.CreateParented(0);

ReportDlg.SaveBtn.OnClick:=OnClickSaveButtonOnReportDlg;