unit U_PEDialogEqChoice;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, exgrid, RapTree, FlytreePro, ExtCtrls, RzPanel,
  U_SCSLists, U_SCSComponent, RzButton, siComp, siLngLnk;


type
  TF_PEDialogEqChoice = class(TForm)
    RzPanel1: TRzPanel;
    RzPanel2: TRzPanel;
    StaticText1: TStaticText;
    RzPanel3: TRzPanel;
    bOk: TRzBitBtn;
    bCancel: TRzBitBtn;
    tvChoiceEndObject: TFlyTreeViewPro;
    lng_Forms: TsiLangLinked;
    procedure RzPanel3Resize(Sender: TObject);
    procedure bOkClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure tvChoiceEndObjectDeletion(Sender: TObject; Node: TFlyNode);
  private
    { Private declarations }
  public
    { Public declarations }
//    LineComponForConnect: TSCSComponent;
    ComponForConnect: TSCSComponent;
    ComponInterfaces: TSCSInterfaces;
    procedure BuildTreeEndCompon(AListCompon: TSCSComponents; ASCSCatalog: TscsCatalog);

  end;

var
  F_PEDialogEqChoice: TF_PEDialogEqChoice;

implementation

{$R *.dfm}
uses U_MAIN,U_PECommon, U_BaseCommon, U_BaseConstants;



procedure TF_PEDialogEqChoice.BuildTreeEndCompon(AListCompon: TSCSComponents; ASCSCatalog: TscsCatalog);
var
  currNode: TFlyNode;
  currCompon: TSCSComponent;
  i, j: integer;
begin
  if tvChoiceEndObject.Images.Count = 0 then
    tvChoiceEndObject.Images.AddImages(F_NormBase.DM.ImageList_Dir);
  tvChoiceEndObject.Items.Clear;
  //tvChoiceEndObject.Refresh;
  tvChoiceEndObject.NodeStateRefresh(nil);
  currNode := AddNode(tvChoiceEndObject, nil, nil, ASCSCatalog.GetNameForVisible);
  currNode.Height := -1;
  for i := 0 to AListCompon.Count -1 do
  begin
    AddNode(F_PEDialogEqChoice.tvChoiceEndObject, currNode, AListCompon[i]);
  end;
  tvChoiceEndObject.TopItem.Expand(True);

//  For i := 0 to AListId.Count -1 do
//  begin
//    currNode := Nil;
// //   currNode := AddNode(F_PEDialogEqChoice.tvChoiceEndObject, currNode, nil, intto str(AListId[i]));
//    for j := 0 to TList(ListObject//[i]).Count - 1 do
//    begin
//      currCompon := TscsComponent(TList(ListObject[i]).Items[j]);
//      currNode := AddNode(Atree, currNode, currCompon);
//    end;
//    currNode := Atree.Items[i];
//    currNode.StateIndex := 2;
//    Atree.NodeStateRefreshChildren(currNode, false);
//  end;
//  i := currNode.StateIndex;
                                
end;

procedure TF_PEDialogEqChoice.RzPanel3Resize(Sender: TObject);
var
   Center: integer;
begin
  Center := Round(RzPanel3.Width/2);
  bCancel.Left := Center + 12;
  bOk.Left := Center - 12 - bOk.Width
end;

procedure TF_PEDialogEqChoice.bOkClick(Sender: TObject);
var
  Compon: TScsComponent;
  InterfList: TSCSInterfaces;
  Interf: TSCSInterface;
  i, j: integer;
  IOfIRel: TSCSIOfIRel;
begin
  //if (LineComponForConnect <> Nil) then
  begin                                   //проверка, подключенного к кабелю для автотрассировки интерфейса, на возможность подключения к кабелю
    if F_PEDialogEqChoice.Visible then
      if tvChoiceEndObject.Selected = tvChoiceEndObject.GetFirstVisibleNode then
        MessageModal(cPEMes20, cPEMes2, MB_ICONWARNING)
      else
      begin
        Compon := F_ProjMan.GSCSBase.CurrProject.CurrList.GetComponentFromReferences(TNodeData(tvChoiceEndObject.Selected.Data).ID);
        if Compon = Nil then
          MessageModal(cPEMes6, cPEMes2, MB_ICONWARNING);
      end
    else
      Compon := ComponForConnect;
    if Compon <> Nil  then
    begin
      InterfList := TSCSInterfaces.Create(false);
      try
        for i := 0 to Compon.Interfaces.Count - 1 do
        begin
          Interf := Compon.Interfaces[i];
          if {(Interf.IOfIRelOut.Count > 0) and }(Interf.Multiple = biTrue)  and (Interf.TypeI = itFunctional) then
          begin
            if Assigned(Interf.IOfIRelOut) then
              For j := 0 to Interf.IOfIRelOut.Count - 1 do
              begin
                IOfIRel := TSCSIOfIRel(Interf.IOfIRelOut[j]);
                if IOfIRel.InterfaceTo.Multiple = biTrue then
          //      If IOfIRel.InterfaceTo.ID_Component > U_PECommon.GLastIdComponent then
                begin
                  InterfList.Add(Interf);
                  break;
                end;
              end;
            if Assigned(Interf.ConnectedInterfaces) then
              For j := 0 to Interf.ConnectedInterfaces.Count - 1 do
              begin
                If Interf.ConnectedInterfaces[j].Multiple = biTrue then
              //If Interf.ConnectedInterfaces[j].ID_Component > U_PECommon.GLastIdComponent then
                begin
                  InterfList.Add(Interf);
                  //break;
                end;
              end;
          end;
        end;
        if InterfList.Count > 0 then
        begin
//          if LineComponForConnect.CheckJoinTo(Compon,-1,-1,true, nil, InterfList).CanConnect then
//          begin
          ComponForConnect := Compon;
          ComponInterfaces := InterfList;
          ModalResult := mrOK;
//          end;
        end
        else
        begin
          if F_PEDialogEqChoice.Visible then
            MessageModal(cPEMes6, cPEMes2, MB_ICONWARNING);
        end;
      finally
        if InterfList.Count = 0 then
          InterfList.Free;
      end;
    end;
  end;
  //else
//    ModalResult := mrCancel;
end;

procedure TF_PEDialogEqChoice.FormCreate(Sender: TObject);
begin
//  LineComponForConnect := Nil;
  ComponForConnect := Nil;
  ComponInterfaces := Nil;
end;

procedure TF_PEDialogEqChoice.tvChoiceEndObjectDeletion(Sender: TObject;
  Node: TFlyNode);
begin
  if  Assigned(Node.Data)then
    FreeMemory(Node.Data);
end;

end.
