unit Unit3;

{$mode objfpc}{$H+}

interface

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

type

  { TConnectionForm }

  TConnectionForm = class(TForm)
    AddStyleEdit: TEdit;
    BaseNameEdit: TEdit;
    ConnStartComboBox: TComboBox;
    ConnEndComboBox: TComboBox;
    CreateNewConnectionsCheckBox: TCheckBox;
    ConnStartGroupBox: TGroupBox;
    ConnEndGroupBox: TGroupBox;
    EndPortCheckBox: TCheckBox;
    BatchSpeedButton: TSpeedButton;
    ArrangeSpeedButton: TSpeedButton;
    StartPortNameColSpinEdit: TSpinEdit;
    StartPortCheckBox: TCheckBox;
    Label1: TLabel;
    Label2: TLabel;
    ModifyEndGroupBox: TGroupBox;
    EndObjectParamDeltaEdit: TEdit;
    StartObjectParamEdit: TEdit;
    ModifyStartGroupBox: TGroupBox;
    FlowPanel1: TFlowPanel;
    MakeToAppSpeedButton: TSpeedButton;
    MakeToClipBrdSpeedButton: TSpeedButton;
    MakeToFileSpeedButton: TSpeedButton;
    NameColSpinEdit: TSpinEdit;
    ConnNameGroupBox: TGroupBox;
    StartObjectParamDeltaEdit: TEdit;
    EndObjectParamEdit: TEdit;
    StartObjNameColSpinEdit: TSpinEdit;
    EndObjNameColSpinEdit: TSpinEdit;
    Panel1: TPanel;
    SeqStartSpinEdit: TSpinEdit;
    SeqEndSpinEdit: TSpinEdit;
    EndPortNameColSpinEdit: TSpinEdit;
    UseColumnAsNameRadioButton: TRadioButton;
    UseColumnAsStartObjNameRadioButton: TRadioButton;
    UseColumnAsEndObjNameRadioButton: TRadioButton;
    UseSequenceAsNameRadioButton: TRadioButton;
    SeqBaseSpinEdit: TSpinEdit;
    UseSequenceAsStartNameRadioButton: TRadioButton;
    UseSequenceAsEndNameRadioButton: TRadioButton;
    procedure MakeToFileSpeedButtonClick(Sender: TObject);
  private
    { private declarations }
  public
    { public declarations }
  end;

var
  ConnectionForm: TConnectionForm;

implementation
Uses unit1;
{$R *.lfm}

// ToDo
// логотип - ok
// шапка с нумерацией строк и колонок - ok
// поле basename для sequential - ok
// порты
// модификаторов не один, а список
// send to app
// команды поточного и поэлементного рисования -> Pencil
// +
// update all objects -> Pencil
// refine/verify columns
// использование в качестве имен комбинации столбцов
// пробел -> подчерк в именах - ok
// не создавать пусте - ok
//
// положение/размер/толщина - по формуле
// маппинг значения в цвет или имя шаблона
// фильтры grep по колонке



{ TConnectionForm }

procedure TConnectionForm.MakeToFileSpeedButtonClick(Sender: TObject);
Var I,P,Numerator,NumeratorBase,EffNumerator:Longint;
    Script:TStringList;
    ConnName,StartObj,EndObj,StartPort,EndPort: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');}

{   Conn.Color:=GetValByName(Conn.Style,'Color');
    If Conn.Color='' Then Conn.Color:='clBlack';
    Conn.BGColor:=GetValByName(Conn.Style,'BGColor');
    If Conn.BGColor='' Then Conn.BGColor:='clNone';
    Conn.Width:=GetValByName(Conn.Style,'Width');
    If Conn.Width='' Then Conn.Width:='1';
    Conn.StartObj:=GetValByName(Conn.Style,'StartObj');
    Conn.EndObj:=GetValByName(Conn.Style,'EndObj');
    Conn.StartPort:=GetValByName(Conn.Style,'StartPort');
    Conn.EndPort:=GetValByName(Conn.Style,'EndPort');
 }


  Script:=TStringList.Create;
  Numerator:=0;NumeratorBase:=0;
  If UseSequenceAsNameRadioButton.Checked Then Numerator:=SeqBaseSpinEdit.Value;
  If Form1.StringGrid.Selection.IsEmpty Then Begin
    For I:=2 To Form1.StringGrid.RowCount-1 Do Begin
//      If Use0ForCoordsCheckBox.Checked Then EffNumerator:=NumeratorBase Else EffNumerator:=Numerator;
      If UseColumnAsNameRadioButton.Checked Then Begin
        ConnName:=Form1.StringGrid.Cells[NameColSpinEdit.Value,I];
        While Pos(' ',ConnName)>0 Do Begin
          P:=Pos(' ',ConnName);
          ConnName[P]:='_';
        End;
        If ConnName='' Then Continue;
      End;

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

      If UseColumnAsStartObjNameRadioButton.Checked Then Begin
        StartObj:=Form1.StringGrid.Cells[StartObjNameColSpinEdit.Value,I];
        While Pos(' ',StartObj)>0 Do Begin
          P:=Pos(' ',StartObj);
          StartObj[P]:='_';
        End;
      End;
      If StartObj='' Then Continue;

      If UseColumnAsEndObjNameRadioButton.Checked Then Begin
        EndObj:=Form1.StringGrid.Cells[endObjNameColSpinEdit.Value,I];
        While Pos(' ',EndObj)>0 Do Begin
          P:=Pos(' ',EndObj);
          EndObj[P]:='_';
        End;
      End;
      If EndObj='' Then Continue;

      If CreateNewConnectionsCheckBox.Checked Then Begin
        If StartPortCheckBox.Checked Then
          StartPort:=Form1.StringGrid.Cells[StartPortNameColSpinEdit.Value,I]
        Else StartPort:='';
        If EndPortCheckBox.Checked Then
          EndPort:=Form1.StringGrid.Cells[EndPortNameColSpinEdit.Value,I]
        Else EndPort:='';
          Script.Add('self::CAD_CreateConnection:Name='+ConnName+' StartObj='+StartObj+' StartPort='+StartPort+' EndObj='+EndObj+' EndPort='+EndPort+' StartStyle='+ConnStartComboBox.Text+' EndStyle='+ConnEndComboBox.Text+' '+AddStyleEdit.Text);
      end;

      If StartObjectParamEdit.Text<>'' Then Begin
        If Form1.StringGrid.Cells[StartObjNameColSpinEdit.Value,I]<>'' Then Begin
          Script.Add('self::CAD_ModAttr:'+StartObj+':'+StartObjectParamEdit.Text+':'+StartObjectParamDeltaEdit.Text);
          If Not BatchSpeedButton.Down Then Begin
            Script.Add('self::CAD_FixItem:'+StartObj);
            Script.Add('self::CAD_UpdateDraw');
          end;
        End;
      End;
      If EndObjectParamEdit.Text<>'' Then Begin
        If Form1.StringGrid.Cells[EndObjNameColSpinEdit.Value,I]<>'' Then Begin
          Script.Add('self::CAD_ModAttr:'+EndObj+':'+EndObjectParamEdit.Text+':'+EndObjectParamDeltaEdit.Text);
          If Not BatchSpeedButton.Down Then Begin
            Script.Add('self::CAD_FixItem:'+EndObj);
            Script.Add('self::CAD_UpdateDraw');
          end;
        End;
      End;

      If ArrangeSpeedButton.Down And (Not BatchSpeedButton.Down) Then Begin
        Script.Add('self::CAD_ArrangePortsByRect:'+StartObj+':0:0');
        Script.Add('self::CAD_ArrangePortsByRect:'+EndObj+':0:0');
      end;
      If Not BatchSpeedButton.Down Then  Script.Add('self::CAD_UpdateDraw');
    {  If ArrangeSpeedButton.Down Then
        Script.Add('self::CAD_FixItem:'+StartObj);}
      Inc(Numerator);Inc(NumeratorBase);
    end;
  End Else Begin
    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
        ConnName:=Form1.StringGrid.Cells[NameColSpinEdit.Value,I];
      If UseSequenceAsNameRadioButton.Checked Then
        ConnName:='Object'+IntToStr(Numerator);
//      Script.Add('self::CAD_CreateObject:Name='+SectName+' '+SectX+' '+SectY+' '+SectA+' '+SectS+' '+SectW+' '+SectH);
      Inc(Numerator);
      Inc(NumeratorBase);
    end;
  End;

   If ArrangeSpeedButton.Down And BatchSpeedButton.Down Then Begin
        Script.Add('self::CAD_ArrangePortsByRectAll:0:0');
      end;
  Script.Add('self::CAD_FixAll');
  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.CloseSocket;
       TCPSocket.Free;
     End;
  End;
  Script.Free;
end;

end.

