unit Unit2;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, Spin,
  ExtCtrls, StdCtrls, Buttons, ClipBrd, blcksock, synsock, synautil, synamisc;

type

  { TObjectForm }

  TObjectForm = class(TForm)
    AddStyleEdit: TEdit;
    BaseNameEdit: TEdit;
    CreateObjectsCheckBox: TCheckBox;
    PortNameColSpinEdit: TSpinEdit;
    PortGroupBox: TGroupBox;
    Label1: TLabel;
    Label2: TLabel;
    Use0ForCoordsCheckBox: TCheckBox;
    DWFloatSpinEdit: TFloatSpinEdit;
    DHFloatSpinEdit: TFloatSpinEdit;
    DAFloatSpinEdit: TFloatSpinEdit;
    DSFloatSpinEdit: TFloatSpinEdit;
    DYFloatSpinEdit: TFloatSpinEdit;
    EllipseRadioButton: TRadioButton;
    HFloatSpinEdit: TFloatSpinEdit;
    AFloatSpinEdit: TFloatSpinEdit;
    SeqStartSpinEdit: TSpinEdit;
    SFloatSpinEdit: TFloatSpinEdit;
    CreateObjectPortsCheckBox: TCheckBox;
    UseColumnAsNameRadioButton1: TRadioButton;
    XFloatSpinEdit: TFloatSpinEdit;
    FlowPanel1: TFlowPanel;
    ObjectNameGroupBox: TGroupBox;
    ObjectStyleGroupBox: TGroupBox;
    UseAsHRadioButton: TRadioButton;
    HGroupBox: TGroupBox;
    UseAsARadioButton: TRadioButton;
    UseAsSRadioButton: TRadioButton;
    AColSpinEdit: TSpinEdit;
    SColSpinEdit: TSpinEdit;
    WFloatSpinEdit: TFloatSpinEdit;
    DXFloatSpinEdit: TFloatSpinEdit;
    YFloatSpinEdit: TFloatSpinEdit;
    YConstantRadioButton: TRadioButton;
    WConstantRadioButton: TRadioButton;
    HConstantRadioButton: TRadioButton;
    AConstantRadioButton: TRadioButton;
    SConstantRadioButton: TRadioButton;
    XGroupBox: TGroupBox;
    AGroupBox: TGroupBox;
    SGroupBox: TGroupBox;
    ASequentialRadioButton: TRadioButton;
    SSequentialRadioButton: TRadioButton;
    HColSpinEdit: TSpinEdit;
    XConstantRadioButton: TRadioButton;
    YGroupBox: TGroupBox;
    WGroupBox: TGroupBox;
    NameColSpinEdit: TSpinEdit;
    RectRadioButton: TRadioButton;
    UseAsXRadioButton: TRadioButton;
    UseAsYRadioButton: TRadioButton;
    UseAsWRadioButton: TRadioButton;
    RRectRadioButton: TRadioButton;
    TemplateNameEdit: TEdit;
    TemplateRadioButton: TRadioButton;
    UseColumnAsNameRadioButton: TRadioButton;
    UseSequenceAsNameRadioButton: TRadioButton;
    XColSpinEdit: TSpinEdit;
    XSequentialRadioButton: TRadioButton;
    MakeToAppSpeedButton: TSpeedButton;
    MakeToClipBrdSpeedButton: TSpeedButton;
    MakeToFileSpeedButton: TSpeedButton;
    Panel1: TPanel;
    YColSpinEdit: TSpinEdit;
    WColSpinEdit: TSpinEdit;
    YSequentialRadioButton: TRadioButton;
    WSequentialRadioButton: TRadioButton;
    HSequentialRadioButton: TRadioButton;
    procedure MakeToFileSpeedButtonClick(Sender: TObject);
    Procedure RectRadioButtonChange(Sender: TObject);
    Procedure TemplateRadioButtonChange(Sender: TObject);
  private
    { private declarations }
  public
    { public declarations }
  end;

var
  ObjectForm: TObjectForm;

implementation
Uses Unit1;
{$R *.lfm}

{ TObjectForm }

procedure TObjectForm.MakeToFileSpeedButtonClick(Sender: TObject);
Var I,P,Numerator,NumeratorBase,EffNumerator:Longint;
    Script:TStringList;
    ObjName,SectA,SectX,SectY,SectS,SectW,SectH,SectTemplate,PortName:String;
begin
{'self::CAD_CreateObject:Name=<name> X= Y= A= S= W= H=');
 'self::CAD_CreateObjectPort:<ObjName>:<Style>');
 'self::CAD_CreateCallout:Name=<name> ...');
 'self::CAD_CreateConnection:StartObj= StartPort= EndObj= EndPort=');
 'self::CAD_CreateNPConnection:StartObj= EndObj=');
 'self::CAD_SetSource:Name:Src');
 'self::CAD_SetText:Name:Text');}
  Script:=TStringList.Create;
  Numerator:=0;NumeratorBase:=0;
  If UseSequenceAsNameRadioButton.Checked Then Numerator:=SeqStartSpinEdit.Value;
  If Form1.StringGrid.Selection.IsEmpty Then Begin
    For I:=2 To Form1.StringGrid.RowCount-1 Do Begin
      SectX:='X=';SectY:='Y=';SectW:='W=';SectH:='H=';SectA:='A=';SectS:='S=';
      If Use0ForCoordsCheckBox.Checked Then EffNumerator:=NumeratorBase Else EffNumerator:=Numerator;
      If UseColumnAsNameRadioButton.Checked Then Begin
        ObjName:=Form1.StringGrid.Cells[NameColSpinEdit.Value,I];
        While Pos(' ',ObjName)>0 Do Begin
          P:=Pos(' ',ObjName);
          ObjName[P]:='_';
        End;
        If ObjName='' Then Continue;
      End;

      If UseSequenceAsNameRadioButton.Checked Then
        ObjName:=BaseNameEdit.Text+IntToStr(Numerator);

      If RectRadioButton.Checked Then SectA:='A=720';
      If RRectRadioButton.Checked Then SectA:='A=1080';//720+360
      If EllipseRadioButton.Checked Then SectA:='A=1440';//720+2*360

      If UseAsXRadioButton.Checked Then
        SectX:='X='+Form1.StringGrid.Cells[XColSpinEdit.Value,I];
      If XConstantRadioButton.Checked Then
        SectX:='X='+XFloatSpinEdit.Text;
      If XSequentialRadioButton.Checked Then
        SectX:='X='+FloatToStr(XFloatSpinEdit.Value+DXFloatSpinEdit.Value*EffNumerator);

      If UseAsYRadioButton.Checked Then
        SectY:='Y='+Form1.StringGrid.Cells[YColSpinEdit.Value,I];
      If YConstantRadioButton.Checked Then
        SectY:='Y='+YFloatSpinEdit.Text;
      If YSequentialRadioButton.Checked Then
        SectY:='Y='+FloatToStr(YFloatSpinEdit.Value+DYFloatSpinEdit.Value*EffNumerator);

      If TemplateRadioButton.Checked Then Begin // Template controlled by XYAS
        SectTemplate:='Template='+TemplateNameEdit.Text;
        If UseAsARadioButton.Checked Then
          SectA:='A='+Form1.StringGrid.Cells[AColSpinEdit.Value,I];
        If AConstantRadioButton.Checked Then
          SectA:='A='+AFloatSpinEdit.Text;
        If ASequentialRadioButton.Checked Then
          SectA:='A='+FloatToStr(AFloatSpinEdit.Value+DAFloatSpinEdit.Value*EffNumerator);
        If UseAsSRadioButton.Checked Then
          SectS:='S='+Form1.StringGrid.Cells[SColSpinEdit.Value,I];
        If SConstantRadioButton.Checked Then
          SectS:='S='+SFloatSpinEdit.Text;
        If SSequentialRadioButton.Checked Then
          SectS:='S='+FloatToStr(SFloatSpinEdit.Value+DSFloatSpinEdit.Value*EffNumerator);
      End Else Begin //rect-rrect-ellipse controlled by XYWH
        SectTemplate:='';
        If UseAsWRadioButton.Checked Then
          SectW:='W='+Form1.StringGrid.Cells[WColSpinEdit.Value,I];
        If AConstantRadioButton.Checked Then
          SectW:='W='+WFloatSpinEdit.Text;
        If ASequentialRadioButton.Checked Then
          SectW:='W='+FloatToStr(WFloatSpinEdit.Value+DWFloatSpinEdit.Value*EffNumerator);
        If UseAsHRadioButton.Checked Then
          SectH:='H='+Form1.StringGrid.Cells[HColSpinEdit.Value,I];
        If HConstantRadioButton.Checked Then
          SectH:='H='+HFloatSpinEdit.Text;
        If HSequentialRadioButton.Checked Then
          SectH:='H='+FloatToStr(HFloatSpinEdit.Value+DHFloatSpinEdit.Value*EffNumerator);
      End;
      If CreateObjectsCheckBox.Checked Then
        Script.Add('self::CAD_CreateObject:Name='+ObjName+' '+SectTemplate+' '+SectX+' '+SectY+' '+SectA+' '+SectS+' '+SectW+' '+SectH+' '+AddStyleEdit.Text);
      If CreateObjectPortsCheckBox.Checked Then Begin
        PortName:=Form1.StringGrid.Cells[PortNameColSpinEdit.Value,I];
        Script.Add('self::CAD_CreateObjectPort:'+ObjName+':Name='+PortName);
      End;
      Inc(Numerator);      Inc(NumeratorBase);
    end;
  End Else Begin
    Exit;//недописанный кусок
    For I:=0 To Form1.StringGrid.RowCount-1 Do
      If (Form1.StringGrid.Selection.Top<=I) And (I<=Form1.StringGrid.Selection.Bottom) Then Begin
      If UseColumnAsNameRadioButton.Checked Then
        ObjName:=Form1.StringGrid.Cells[NameColSpinEdit.Value,I];
      If UseSequenceAsNameRadioButton.Checked Then
        ObjName:='Object'+IntToStr(Numerator);
      Script.Add('self::CAD_CreateObject:Name='+ObjName+' '+SectX+' '+SectY+' '+SectA+' '+SectS+' '+SectW+' '+SectH);
      Inc(Numerator);
      Inc(NumeratorBase);
    end;
  End;
  Script.Add('self::CAD_UpdateDraw');
  Case (Sender As TSpeedButton).Tag Of
  1: Begin Script.SaveToFile('test');
     End;
  2: Begin
       ClipBoard.AsText:=Script.Text;
     End;
  3: Begin
       TCPSocket:=TTCPBlockSocket.Create;
       TCPSocket.Connect('127.0.0.1','33333');
       For I:=0 To Script.Count-1 Do
         TCPSocket.SendString(Script[I]+#10);
       TCPSocket.SendString('disconnect');
       TCPSocket.CloseSocket;
       TCPSocket.Free;
     End;
  End;
  Script.Free;
end;

Procedure TObjectForm.RectRadioButtonChange(Sender: TObject);
Begin

End;


Procedure TObjectForm.TemplateRadioButtonChange(Sender: TObject);
Begin
  If TemplateRadioButton.Checked Then begin
    WGroupBox.Hide;  HGroupBox.Hide;
    AGroupBox.Show;  SGroupBox.Show;
  End Else Begin
    WGroupBox.Show;  HGroupBox.Show;
    AGroupBox.Hide;  SGroupBox.Hide;
  End;
end;

end.

