const
  c_Format1 = '%s'+#9+'%s'+#9+'%*.*n'+#9+'%s'+#13#10;
  c_Format1_Integer = '%s'+#9+'%s'+#9+'%d'+#9+'%s'+#13#10;
  c_Format2 = '- %s'+#9+'%8.0n bis'+#9+'%8.0n'+#9+'%s'+#13#10;
  c_Format2_1 = '%s'+#9+'%8.0n bis'+#9+'%8.0n'+#9+'%s'+#13#10;
  c_Format3 = '%s'+#9#9+'%s'+#13#10;



procedure TfBaseForm.AddRichHeader(const aTitle: string;
  aRichEdit: TcxRichEdit);
begin
  with aRichEdit do
  begin
    PrepareResults(aRichEdit);
    SelAttributes.Style := [fsBold];
    SelText := aTitle + #13#10#13#10;
    SelAttributes.Style := [];
    SelText := 'Kunde: ' + ed_Customer.Text + #13#10;
    SelText := 'Projekt: ' + ed_Project.Text + #13#10;
    SelText := 'Bearbeiter: ' + ed_User.Text + #13#10;
    SelText := 'Beschreibung: ' + ed_Description.Text + #13#10;
    SelText := 'Datum: ' + ed_Date.Text + #13#10;
    SelText := 'Datei: ' + lb_FileName.Caption + #13#10;
    SelText := '================================================================' + #13#10;
  end;
end;



procedure TfC_Passfeder.Calculate;
var
  I: Integer;
  J: Integer;
  Dw: Double;
  Tnenn: Double;
  Teq: Double;
  Htr: Double;
  Npf: Integer;
  Ltr: Double;
  L2tr: Double;
  Ltr_calc: Double;
  Rho: Double;
  dN_W: Integer;
  dN_N: Integer;
  dN_P: Integer;
  Da_N: Double;
  Kt_Rm_W: Double;
  Kt_Rp_W: Double;
  Kt_Rm_N: Double;
  Kt_Rp_N: Double;
  Br: Double;
  Kt_Rm_P: Double;
  Kt_Rp_P: Double;
  FH_W: Double;
  Hart_W: Boolean;
  FH_N: Double;
  Hart_N: Boolean;
  FH_P: Double;
  Hart_P: Boolean;
  Sv: Double;
  RmN_W: Double;
  Rmzul_W: Double;
  RmN_N: Double;
  Rmzul_N: Double;
  RmN_P: Double;
  Rmzul_P: Double;
  RpN_W: Double;
  Rpzul_W: Double;
  RpN_N: Double;
  Rpzul_N: Double;
  RpN_P: Double;
  Rpzul_P: Double;
  Pm: Double;
  MatGruppe_W: Integer;
  MatGruppe_N: Integer;
  MatGruppe_P: Integer;
  Pzul_W: Double;
  Pzul_N: Double;
  Pzul_P: Double;
  Svorh_W: Double;
  Svorh_N: Double;
  Svorh_P: Double;
  Svorh: Double;
  Lempf: Double;
  DnGGmin: Double;
  DnGGmax: Double;
  DnStmin: Double;
  DnStmax: Double;
  LnGGmin: Double;
  LnGGmax: Double;
  LnStmin: Double;
  LnStmax: Double;
  Lcalc: Double;
  Pzul: Double;
  KA: Double;
  PForm: string;
  H: Double;
  L: Double;
begin
  inherited;
  ErrCnt := 0;
  FReadyForPrint := false;
  re_Results.Clear;
  for I := 0 to lc_PF.ControlCount -1 do
  begin
    if lc_PF.Controls[I] is TcxCustomEdit then
      TcxCustomEdit(lc_PF.Controls[I]).ValidateEdit(false);
    if lc_PF.Controls[I] is TcxCustomGroupBox then
      for J := 0 to TcxCustomGroupBox(lc_PF.Controls[I]).ControlCount -1 do
        if TcxCustomGroupBox(lc_PF.Controls[I]).Controls[J] is TcxCustomEdit then
          TcxCustomEdit(TcxCustomGroupBox(lc_PF.Controls[I]).Controls[J]).ValidateEdit(false);
  end;
  try
    Dw := ed_D1.EditValue;
    Tnenn := ed_Tnenn.Value;
    KA := ed_KA.EditValue;
    Teq := Tnenn * KA;  //äquivalentes Drehmoment
    PForm := ed_PForm.Items[ed_PForm.ItemIndex];
    Htr := 0.45 * ed_Hoehe.EditValue;  //tragende Passfederhöhe
    Npf := ed_Np.EditValue; //Anzahl Passfedern
    Ltr := ed_Ltr.EditValue; //tragende Passfederlänge
    if (VarIsNull(ed_L2tr.EditValue)) or (ed_L2tr.EditValue = 0) then
      L2tr := Ltr
    else
      L2tr := ed_L2tr.EditValue; //tragende Nabennutlänge
    Ltr_calc := Min(Ltr, L2tr);
    case Npf of  //Tragfaktor
      1: Rho := 1.0;
      2: Rho := 0.75;
    end;
    dN_W := ed_dN_W.EditValue;
    dN_N := ed_dN_N.EditValue;
    dN_P := ed_dN_P.EditValue;
    Da_N := 2 * Dw; //virtueller Nabendurchmesser
    Br := ed_Breite.EditValue;
    H := ed_Hoehe.EditValue;
    L := ed_L.EditValue;
    MatGruppe_W := ed_WGr_W.EditValue;
    MatGruppe_N := ed_WGr_N.EditValue;
    MatGruppe_P := ed_WGr_P.EditValue;
    Kt_Rm_W := DMod.GetMatKt_Rm(Dw, MatID_W);
    Kt_Rp_W := DMod.GetMatKt_Re(Dw, MatID_W);
    Kt_Rm_N := DMod.GetMatKt_Rm(Da_N, MatID_N);
    Kt_Rp_N := DMod.GetMatKt_Re(Da_N, MatID_N);
    Kt_Rm_P := DMod.GetMatKt_Rm(Br, MatID_P);
    Kt_Rp_P := DMod.GetMatKt_Re(Br, MatID_P);
    FH_W := 1; //Härteeinflussfaktor
    Hart_W := ed_Hart_W.Checked;
    if Hart_W then
      FH_W := 1.15;
    FH_N := 1;
    Hart_N := ed_Hart_N.Checked;
    if Hart_N then
      FH_N := 1.15;
    FH_P := 1;
    Hart_P := ed_Hart_P.Checked;
    if Hart_P then
      FH_P := 1.15;
    Sv := ed_Sv.EditValue;
    RmN_W := ed_RmN_W.EditValue;
    Rmzul_W := RmN_W * Kt_Rm_W * FH_W;
    RmN_N := ed_RmN_N.EditValue;
    Rmzul_N := RmN_N * Kt_Rm_N * FH_N;
    RmN_P := ed_RmN_P.EditValue;
    Rmzul_P := RmN_P * Kt_Rm_P * FH_P;
    RpN_W := ed_RpN_W.EditValue;
    Rpzul_W := RpN_W * Kt_Rp_W * FH_W;
    RpN_N := ed_RpN_N.EditValue;
    Rpzul_N := RpN_N * Kt_Rp_N * FH_N;
    RpN_P := ed_RpN_P.EditValue;
    Rpzul_P := RpN_P * Kt_Rp_P * FH_P;
    Pm := (2 * Teq * 1000) / (Dw * Htr * Ltr_calc * Npf * Rho); //vorhandene mittlere Flächenpressung
    if MatGruppe_W = 10 then //Grauguss - GJL...
      Pzul_W := Rmzul_W
    else
      Pzul_W := Rpzul_W;
    if MatGruppe_N = 10 then //Grauguss - GJL...
      Pzul_N := Rmzul_N
    else
      Pzul_N := Rpzul_N;
    if MatGruppe_P = 10 then //Grauguss - GJL...
      Pzul_P := Rmzul_P
    else
      Pzul_P := Rpzul_P;
    Pzul := Min(Pzul_W, Pzul_N);
    Pzul := Min(Pzul, Pzul_P);
    Svorh_W := Pzul_W / Pm; //vorhandene Sicherheit
    Svorh_N := Pzul_N / Pm; //vorhandene Sicherheit
    Svorh_P := Pzul_P / Pm; //vorhandene Sicherheit
    Svorh := Pzul / Pm; //kleinste Sicherheit
    Lempf := 1.3 * Dw; //empfohlene tragende Passfederlänge
    DnGGmin := 2 * Dw;  //Nabendurchmesser
    DnGGmax := 2.2 * Dw;
    DnStmin := 1.8 * Dw;
    DnStmax := 2 * Dw;
    LnGGmin := 1.6 * Dw; // Nabenlänge
    LnGGmax := 2.1 * Dw;
    LnStmin := 1.1 * Dw;
    LnStmax := 1.4 * Dw;
    Lcalc := (2 * Teq * 1000) / (Dw * Htr * Pzul * Npf * Rho); //kalkulatorische tragende Passfederlänge
  except
    on E: EMathError do
    begin
      re_Results.Clear;
      re_Results.SelStart := 0;
      re_Results.SelAttributes.Color := clRed;
      re_Results.SelAttributes.Style := [fsbold];
      re_Results.SelText := 'FEHLER: '+ E.Message;
      re_Results.SelAttributes.Color := clDefault;
      re_Results.SelAttributes.Style := [];
      Exit;
    end;
    on E: EVariantTypeCastError do
    begin

    end;
  end;
  with re_Results do
  begin
    AddRichHeader(c_CalcTitle[FCalcID], re_Results);
    SelText := Format(c_Format1, ['Wellendurchmesser', 'd1', 8, c_Precision, Dw, 'mm']);
    SelText := Format(c_Format3, ['Passfederabmessungen', ed_PDim.Text]);
    SelText := Format(c_Format3, ['Form der Passfeder', PForm]);
    SelText := Format(c_Format1_Integer, ['Anzahl Passfeder(n)', 'n', Npf, '-']);
    SelText := Format(c_Format1, ['Breite Passfeder', 'b', 8, c_Precision, Br, 'mm']);
    SelText := Format(c_Format1, ['Höhe Passfeder', 'h', 8, c_Precision, H, 'mm']);
    SelText := Format(c_Format1, ['Länge Passfeder', 'L', 8, c_Precision, L, 'mm']);
    SelText := Format(c_Format1, ['Tragende Höhe', 'h''', 8, c_Precision, Htr, 'mm']);
    SelText := Format(c_Format1, ['Tragende Länge', 'Ltr', 8, c_Precision, Ltr, 'mm']);
    SelText := Format(c_Format1, ['Tragende Nabennutlänge', 'LtrN', 8, c_Precision, L2tr, 'mm']);
    SelText := #13#10;

    SelAttributes.Style := [fsbold];
    SelText := 'Werkstoff Welle'+#13#10;
    SelAttributes.Style := [];
    SelText := Format(c_Format3, ['Werkstoffbezeichnung', ed_Mat_W.Text]);
    SelText := Format(c_Format3, ['Werkstoffnummer', ed_WNr_W.Text]);
    SelText := Format(c_Format3, ['Werkstoffgruppe/Wärmebehandlung', ed_WGr_W.Text + ', ' + ed_Zustand_W.Text]);
    SelText := Format(c_Format3, ['Randschichtgehärtet', StrUtils.IfThen(Hart_W, 'Ja', 'Nein')]);
    SelText := Format(c_Format1, ['Zugfestigkeit für dN', 'RmN', 8, c_Precision, RmN_W, 'N/mm²']);
    SelText := Format(c_Format1, ['Fließgrenze für dN', 'RpN', 8, c_Precision, RpN_W, 'N/mm²']);
    SelText := Format(c_Format1_Integer, ['Normdurchmesser', 'dN', dN_W, 'mm']);
    SelText := #13#10;

    SelAttributes.Style := [fsbold];
    SelText := 'Werkstoff Nabe'+#13#10;
    SelAttributes.Style := [];
    SelText := Format(c_Format3, ['Werkstoffbezeichnung', ed_Mat_N.Text]);
    SelText := Format(c_Format3, ['Werkstoffnummer', ed_WNr_N.Text]);
    SelText := Format(c_Format3, ['Werkstoffgruppe/Wärmebehandlung', ed_WGr_N.Text + ', ' + ed_Zustand_N.Text]);
    SelText := Format(c_Format3, ['Randschichtgehärtet', StrUtils.IfThen(Hart_N, 'Ja', 'Nein')]);
    SelText := Format(c_Format1, ['Zugfestigkeit für dN', 'RmN', 8, c_Precision, RmN_N, 'N/mm²']);
    SelText := Format(c_Format1, ['Fließgrenze für dN', 'RpN', 8, c_Precision, RpN_N, 'N/mm²']);
    SelText := Format(c_Format1_Integer, ['Normdurchmesser', 'dN', dN_N, 'mm']);
    SelText := #13#10;

    SelAttributes.Style := [fsbold];
    SelText := 'Werkstoff Passfeder'+#13#10;
    SelAttributes.Style := [];
    SelText := Format(c_Format3, ['Werkstoffbezeichnung', ed_Mat_P.Text]);
    SelText := Format(c_Format3, ['Werkstoffnummer', ed_WNr_P.Text]);
    SelText := Format(c_Format3, ['Werkstoffgruppe/Wärmebehandlung', ed_WGr_P.Text + ', ' + ed_Zustand_P.Text]);
    SelText := Format(c_Format3, ['Randschichtgehärtet', StrUtils.IfThen(Hart_P, 'Ja', 'Nein')]);
    SelText := Format(c_Format1, ['Zugfestigkeit für dN', 'RmN', 8, c_Precision, RmN_P, 'N/mm²']);
    SelText := Format(c_Format1, ['Fließgrenze für dN', 'RpN', 8, c_Precision, RpN_P, 'N/mm²']);
    SelText := Format(c_Format1_Integer, ['Normdurchmesser', 'dN', dN_P, 'mm']);
    SelText := #13#10;
    SelText := Format(c_Format1, ['Nenn-Drehmoment', 'TNenn', 8, c_Precision, Tnenn, 'Nm']);
    SelText := Format(c_Format1, ['Anwendungsfaktor', 'KA', 8, c_Precision, KA, '-']);
    SelText := Format(c_Format1, ['Tragende Keillänge', 'Ltr', 8, c_Precision, Ltr_calc, 'mm']);
    SelText := Format(c_Format1, ['Soll-Sicherheit', 'Sv', 8, c_Precision, Sv, '-']);
    SelText := Format(c_Format1, ['Äquivalentes Drehmoment', 'Teq', 8, c_Precision, Teq, 'Nm']);
    SelText := Format(c_Format1, ['Tragfaktor', 'ρ', 8, c_Precision, Rho, '-']);
    SelText := Format(c_Format1, ['Vorhandene mittlere Flächenpressung', 'Pm', 8, c_Precision, Pm, 'N/mm²']);
    SelText := Format(c_Format1, ['Zulässige Flächenpressung Welle', 'Pzul', 8, c_Precision, Pzul_W, 'N/mm²']);
    SelText := Format(c_Format1, ['Vorhandene Sicherheit Welle', 'Sw', 8, c_Precision, Svorh_W, '-']);
    SelText := Format(c_Format1, ['Zulässige Flächenpressung Nabe', 'Pzul', 8, c_Precision, Pzul_N, 'N/mm²']);
    SelText := Format(c_Format1, ['Vorhandene Sicherheit Nabe', 'Sn', 8, c_Precision, Svorh_N, '-']);
    SelText := Format(c_Format1, ['Zulässige Flächenpressung Passfeder', 'Pzul', 8, c_Precision, Pzul_P, 'N/mm²']);
    SelText := Format(c_Format1, ['Vorhandene Sicherheit Passfeder', 'Sp', 8, c_Precision, Svorh_P, '-']);
    SelText := #13#10;
    if Svorh < Sv then
      SelAttributes.Color := clRed;
    SelAttributes.Style := [fsbold];
    SelText := Format(c_Format1, ['Vorhandene Sicherheit', 'S', 8, c_Precision, Svorh, '-']);
    SelAttributes.Color := clDefault;
    SelAttributes.Style := [];
    SelText := #13#10;
    SelAttributes.Style := [fsbold];
    SelText := 'Hinweise:'+#13#10;
    SelAttributes.Style := [];
    SelText := Format(c_Format1, ['Nötige tragende Nabenlänge', 'lcalc', 8, c_Precision, Lcalc, 'mm']);
    SelText := Format(c_Format1, ['Empfohlene max. tragende Nabenlänge', 'l''', 8, c_Precision, Lempf, 'mm']);
    SelText := 'Richtwerte Nabendurchmesser:'+#13#10;
    SelText := Format(c_Format2, ['für Werkstoff GJL (GG)', DnGGmin, DnGGmax, 'mm']);
    SelText := Format(c_Format2, ['für Werkstoff Stahl, GS', DnStmin, DnStmax, 'mm']);
    SelText := 'Richtwerte Nabenlänge:'+#13#10;
    SelText := Format(c_Format2, ['für Werkstoff GJL (GG)', LnGGmin, LnGGmax, 'mm']);
    SelText := Format(c_Format2, ['für Werkstoff Stahl, GS', LnStmin, LnStmax, 'mm']);
    if ErrCnt > 0 then
    begin
      SelAttributes.Color := clRed;
      SelText := Format(c_FormatErr1, [ErrCnt]);
      SelAttributes.Color := clDefault;
    end;
  end;
  FReadyForPrint := true;
end;