{$A+,B-,C+,D+,E-,F-,G+,H+,I-,J-,K-,L+,M-,N+,O-,P+,Q-,R-,S-,T-,U+,V+,W-,X+,Y+,Z1}
//{$MINSTACKSIZE $00004000}
//{$MAXSTACKSIZE $00100000}
//{$IMAGEBASE $00400000}
//{$APPTYPE GUI}
//{$J+}

unit U_ELCommon;
interface

uses
  Windows, Forms, StdCtrls, Classes, ComCtrls, Controls, DrawEngine, PCTypesUtils, SysUtils, Dialogs,
  Contnrs, DrawObjects, PCDrawBox, PCDrawing, PowerCad, Graphics, FastStrings, FastStringFuncs, U_Cad, U_ESCadClasess, U_SCSLists, U_SCSComponent,
  U_Common_Classes, ActnList, U_BaseCommon, U_Progress, U_Splash, Math, U_Navigator, Messages, LibJpeg, ClipBrd, ExtCtrls,
  IniFiles, Registry, MiTeC_PsAPI, U_SCSInterfPath, U_Common;


Procedure BuildElectricianChemeList(aAVR_Compon: TSCSComponent; aBoxList: TSCSComponents; aSwitchList, aConnectedList: TList; aCableList: TSCSComponents; aConnectedCablesList: TList);
Procedure BuildWiringSchemeList;

implementation
uses USCS_Main, Menus, U_main, U_MasterNewList, U_MasterNewListLite, U_AutoTraceType, U_Layers, FPlan,
  U_EndPoints, U_TrunkSCS, U_Constants, U_ChooseDesignBoxParams, U_BaseConstants,
  PCPanel, U_SCSClasses, U_ShieldConfig;

Procedure BuildElectricianChemeList(aAVR_Compon: TSCSComponent; aBoxList: TSCSComponents; aSwitchList, aConnectedList: TList; aCableList: TSCSComponents; aConnectedCablesList: TList);
type CableSection = array of Double;

var i, j, k, l, m: integer;
    Line_x1, Line_x2,Line_y1,Line_y2: Double;
    //Phaze_Line, NullLine, GroundLine: TOrthoLine; // Земля, фаза, ноль...
    Phaze_Line, NullLine, GroundLine, SLine: TLine; // Земля, фаза, ноль...
    ListParams: TListParams;
    OldGCadForm: TF_Cad;

    TextFigList: TList;

    Line_Count: Integer;
    Switches, Connections, UzoList: TSCSComponents;
    ConnectedCompon: TSCSComponent;
    CurrText: TRichText;
    TextList: TStringList;
    IconFigList: TList;
    ParentCatalog: TSCSCatalog;
    ComponFigure: TFigure;
    ComponCad, currCad: TF_Cad;
    ComponList: TSCSList;
    DrawFigureStream: TMemoryStream;
    ComponDrawFigure: TFigureGrpMod;
    ObjImage: TBMPObject;
    Stream: Classes.TStream;

    DownObjectList, ColObjList: TList;
    LineCounter: Integer;
    ConnectedSwitchCompon: TSCSComponent;
    SwitchNames: TStringList;
    MaxTableHeightArray, MaxTableWidthArray: array of double; //

    AllChemeFiguresList: TList; // все фигуры, из которых нарисована схема

    CableSectArray, CopperAmperVals, AllumAmperVals, CopperPowerVals, AllumPowerVals: CableSection;
    ElCounter: TSCSComponent;
    PhazeLinePointFigures: TList;
    CurrColor: TColor;

    function GetComponNamesCounted(aList: TSCSComponents): TStringList;
    var i: integer;
        CanProceed: Boolean;
        currCount: integer;
        ComponName: string;
        LocalList: TSCSComponents;
    begin
      Result := TStringList.Create;
      if aList <> nil then
      begin
        if aList.Count > 0 then
        begin
          if aList.Count = 1 then
          begin
            Result.Add(aList[0].Name + ' 1' + cMasterCompl_Msg5);
          end
          else
          begin
            LocalList := TSCSComponents.Create(false);
            for i := 0 to aList.Count - 1 do
              LocalList.Add(aList[i]);

            while LocalList.Count > 0 do
            begin
              currCount := 0;
              ComponName := LocalList[0].Name;
              for i := LocalList.Count - 1 downto 0 do
              begin
                if LocalList[i].Name = ComponName then
                begin
                  inc(currCount);
                  LocalList.Delete(i);
                end;
              end;
              if currCount > 1 then
              begin
                for i := 0 to aList.Count - 1 do
                begin
                  if aList[i].Name = ComponName then
                  begin
                    ComponName := aList[i].ComponentType.NamePlural;
                    break;
                  end;
                end;
              end;
              Result.Add(ComponName + ' ' + inttostr(currCount) + cMasterCompl_Msg5)
            end;
            LocalList.Free;
          end;
        end;
      end;
    end;

    Procedure CountMaxValues(aList: TList);
    var i, j: integer;
        ObjList: tList;
        Fig: TFigure;
        ML, MW: integer;
        MaxWVal, MaxHVal: Double;
        maxx, maxy, minx, miny: Double;
        TableHeightArray, TableWidthArray: array of array of double;
        MaxWidth, MaxHeight: Double;
    begin

      SetLength(TableHeightArray, 6);
      SetLength(TableWidthArray, 6);

      SetLength(MaxTableHeightArray, 6); // высота каждой строчки
      SetLength(MaxTableWidthArray, aList.Count); // ширина каждого столбика

      for i := 0 to 5 do
         SetLength(TableHeightArray[i], aList.Count);

      for i := 0 to 5 do
         SetLength(TableWidthArray[i], aList.Count);

      ML := 0;
      MW := 0;
      for i := 0 to aList.Count - 1 do
      begin
        ObjList := TList(aList[0]);
        for j := 0 to ObjList.Count - 1 do
        begin
          Fig := TFigure(ObjList[j]);
          if fig is TRichText then
            TRichText(Fig).GetBounds(maxx, maxy, minx, miny)
          else
            if fig is TFigureGrpMod then
              TFigureGrpMod(Fig).GetBounds(maxx, maxy, minx, miny)
            else
              Fig.GetBounds(maxx, maxy, minx, miny);

          TableHeightArray[j, i] := maxy - miny;
          TableWidthArray[j, i] := maxx - minx;
        end;
      end;

       // ширина каждого столбика
      for i := 0 to aList.Count - 1  do
      begin
        MaxWidth := TableWidthArray[i, 0];
        for j := 0 to Length(TableWidthArray[i]) - 1 do
        begin
          if CompareValue(MaxWidth, TableWidthArray[j,i]) = -1 then
            MaxWidth := TableWidthArray[j, i];
        end;
        //MaxTableWidthArray[i] := Maxwidth;
        if Maxwidth > 35  then
          MaxTableWidthArray[i] := Maxwidth
        else
          MaxTableWidthArray[i] := 35;
      end;

      // высота каждой строчки
      for j := 0 to 5  do
      begin
        MaxHeight := TableHeightArray[j, 0];
        for i := 0 to aList.Count - 1  do
        begin
          if CompareValue(MaxHeight, TableHeightArray[j, i]) = -1 then
            MaxHeight := TableHeightArray[j, i];
        end;
        MaxTableHeightArray[j] := MaxHeight;
      end;

      SetLength(TableHeightArray, 0);
      SetLength(TableWidthArray, 0);
    end;

    //Procedure DrawPoint(ax, ay: Double; aColor: TColor);
    function DrawPoint(ax, ay: Double; aColor: TColor): TCircle;
    var Circle: TCircle;
    begin
     // Circle := TCircle.Create(ax, ay, 1, 1, ord(psSolid), aColor, ord(bsClear), clBlack,
     //                       GCadForm.PCad.GetLayerHandle(0), mydsNormal, GCadForm.PCad);
      Circle := TCircle.Create(ax, ay, 1, 1, ord(psSolid), aColor, ord(bsSolid), clBlack,
                            GCadForm.PCad.GetLayerHandle(0), mydsNormal, GCadForm.PCad);
      GCadForm.PCad.AddCustomFigure(1, TFigure(Circle), false);
      Result := Circle;
    end;

    Procedure DrawConnectionLine(ax,ay: Double; aIndex: Integer);
    var i: integer;
        dist: Double;
        isBadCable: Boolean;
        ComponProp: PProperty;
        isCopper: Boolean;
        Text: TRichText;
        CountVal: Double;
        CableSection: double; // сечение кабеля
        Circ: TCircle;
        UserRect: TRectangle;
        CurrSwitch, UZOCompon: TSCScomponent;
        Ellips: TEllipse;
        TraceLength: Double;
        CableCompon: TSCSComponent;
        TraceCatalog: TSCSCatalog;
        TraceFigure: TFigure;
        CableList: TSCSComponents;
        TraceList, TraceCatalogList: TList;
        ComponList: TSCSList;
        CadList: TF_CAD;

        WareVal: string;

        function CheckCableSection(aCopper: boolean; aVal: Double): Boolean;
        var i, index: integer;
            TableVal, ComponVal: Double;
        begin
          Result := False;
          index := 0;
          ComponProp := ConnectedSwitchCompon.GetPropertyBySysName(pnWireSection);
          if ComponProp <> nil then
          begin
            ComponVal := StrToFloat_My(ComponProp.Value); //сечение проводника
            //CableSectArray, CopperAmperVals, AllumAmperVals, CopperPowerVals, AllumPowerVals
            if aCopper  then
            begin
              for i := 0 to 10 do
              begin
                index := i;
                if CompareValue(CopperAmperVals[i], aVal) = 1 then
                  break;
              end;
            end
            else
            begin
              for i := 0 to 10 do
              begin
                index := i;
                if CompareValue(AllumAmperVals[i], aVal) = 1 then
                  break;
              end;
            end;
          end
          else
            exit;

          TableVal := CableSectArray[index]; // расчетное сечени проводника
          if CompareValue(TableVal, ComponVal) = 1 then
            Result := True;
        end;

        function CalcLineCaptionOne: string;
        var i, j: integer;
            MaxCablesTogetherCount: integer;
            ParentComponent: TSCSComponent;
            ParentComponCablesCount: integer;
            LineText: TRichText;
            LineVal: Double;
            wareS: PProperty;
        begin
          Result := '';
          TraceLength := 0;
          TraceList := TList.create;
          TraceCatalogList := TList.create;
          MaxCablesTogetherCount := 0;

          CableList := TSCSComponents(aConnectedCablesList[aIndex - 1]);
          //TraceLength
          for i := 0 to CableList.Count - 1 do
          begin
            CableCompon := CableList[i];
            if isCableComponent(CableCompon) then
            begin
              if Result = '' then
              begin
                Result := Result + CableCompon.NameMark;
                WareVal := '';
                WareS := CableCompon.GetPropertyBySysName(pnWireSection);
                if Wares <> nil then
                  WareVal := Wares.Value;
              end;

              TraceCatalog := CableCompon.GetFirstParentCatalog;
              if TraceCatalog <> nil then
              begin
                ComponList := F_ProjMan.GSCSBase.CurrProject.GetListBySCSID(TraceCatalog.ListID);
                if ComponList <> nil then
                begin
                  CadList := GetListByID(ComponList.SCSID);
                  if CadList <> nil then
                  begin
                    TraceFigure := GetFigureByID(CadList, TraceCatalog.SCSID);
                    if traceFigure <> nil then
                    begin
                      if TraceFigure is TOrthoLine then
                      begin
                        TraceLength := TraceLength + TOrthoLine(TraceFigure).LengthCalc;

                        if TraceList.IndexOf(TraceFigure) = -1 then
                          TraceList.Add(TraceFigure);
                        if TraceCatalogList.IndexOf(TraceCatalog) = -1 then
                          TraceCatalogList.Add(TraceCatalog);

                        ParentComponent := CableCompon.GetParentComponent;
                        if ParentComponent <> nil then
                        begin
                          if ParentComponent.ComponentType.SysName = ctsnCableChannel then
                          begin
                            ParentComponCablesCount := 0;
                            for j := 0 to ParentComponent.ChildReferences.Count - 1 do
                            begin
                              if isCableComponent(ParentComponent.ChildReferences[j]) then
                                if ParentComponent.ChildReferences[j].IDNetType = 3 then
                                  Inc(ParentComponCablesCount);
                            end;
                            if MaxCablesTogetherCount < ParentComponCablesCount then
                              MaxCablesTogetherCount := ParentComponCablesCount;
                          end;
                        end;
                      end;
                    end;
                  end;
                end;
              end;
            end;
          end;
          // 08/06/2021 --  что в подписи кабеля оставляем только маркировку и длину
          {
          LineText := TRichText(TList(DownObjectList[aIndex])[2]);
          LineVal := StrToFloat_My(LineText.re.Text);
          if LineVal <> 0 then
            Result := Result + ' - ' + FloatTostr(Roundx(TraceLength * LineVal, 2)) + ' кВт*м'; // 08/06/2021 --

          LineText := TRichText(TList(DownObjectList[aIndex])[4]);
          LineVal := StrToFloat_My(LineText.re.Text);
          if LineVal <> 0 then
            Result := Result + ' - ' + FloatTostr(Roundx(LineVal, 2)) + ' A';
          }
          Result := Result + ' - ' + FloatTostr(Roundx(TraceLength, 2)) + GetUOMString(GCurrProjUnitOfMeasure);
        end;

        function CalcLineCaptionTwo: string;
        var i: integer;
            LinePower: Double;
            LineVal: TRichText;
        begin
          Result := '';
          if AIndex <= DownObjectList.Count then
          begin
            LineVal := TRichText(TList(DownObjectList[aIndex])[2]);
            LinePower := StrToFloat_My(LineVal.re.Text);
            if LinePower <> 0 then
              Result := Result + FloatTostr(Roundx(TraceLength * LinePower, 2)) + El_Mess8;
          end;
          if WareVal <> '' then
            Result := Result + ' - ' + WareVal + ' - ';

        end;
    begin
      currSwitch := nil;
      UZOCompon := nil;
      TraceList := nil;
      TraceCatalogList := nil;

      if AIndex <= Switches.Count then
        currSwitch := Switches[aIndex - 1];
      if CurrSwitch <> nil then // define Connected UZO
      begin
        for i := 0 to CurrSwitch.JoinedComponents.Count - 1 do
        begin
          if TSCSComponent(CurrSwitch.JoinedComponents[i]).ComponentType.SysName  = ctsnUZO then
          begin
            UZOCompon := TSCSComponent(CurrSwitch.JoinedComponents[i]);
            break;
          end;
        end;
      end;
      Dist := ay - GroundLine.ap1.y;

      DrawPoint(ax, ay, clBlack);
      PhazeLinePointFigures.Add(DrawPoint(ax, Phaze_Line.ap1.y, clBlack));//фаза

      SLine := TLine.create(ax, Phaze_Line.ap1.y, ax, GroundLine.ap1.y + 10, 2, ord(psSolid), clBlack, 0, GCadForm.PCad.GetLayerHandle(0), mydsNormal, GCadForm.PCad);
      GCadForm.PCad.AddCustomFigure(1, TFigure(SLine), false);
      //крестик
      {
      SLine := TLine.create(ax - 2, GroundLine.ap1.y + 10, ax + 2, GroundLine.ap1.y + 10, 2, ord(psSolid), clBlack, 0, GCadForm.PCad.GetLayerHandle(0), mydsNormal, GCadForm.PCad);
      GCadForm.PCad.AddCustomFigure(1, TFigure(SLine), false);
      SLine.Rotate((45/180)*PI);
      SLine := TLine.create(ax - 2, GroundLine.ap1.y + 10, ax + 2, GroundLine.ap1.y + 10, 2, ord(psSolid), clBlack, 0, GCadForm.PCad.GetLayerHandle(0), mydsNormal, GCadForm.PCad);
      GCadForm.PCad.AddCustomFigure(1, TFigure(SLine), false);
      SLine.Rotate((-45/180)*PI);
      }
      SLine := TLine.create(ax - 2, GroundLine.ap1.y + 4, ax + 2, GroundLine.ap1.y + 4, 2, ord(psSolid), clBlack, 0, GCadForm.PCad.GetLayerHandle(0), mydsNormal, GCadForm.PCad);
      GCadForm.PCad.AddCustomFigure(1, TFigure(SLine), false);
      SLine.Rotate((-45/180)*PI);  // перечеркнуть
      // подписать фазу
      TextList.Clear;
      TextList.Add('L1'); // вывести только маркировку кабеля
      CurrText := CreateTextObject(ax - 5, GroundLine.ap1.y + 2, TextList, True);


      DrawPoint(ax + 5, NullLine.ap1.y, clBlack); // ноль
      //SLine := TLine.create(ax + 5, NullLine.ap1.y, ax + 5, GroundLine.ap1.y + Round(Dist* 0.6), 2, ord(psSolid), clBlack, 0, GCadForm.PCad.GetLayerHandle(0), mydsNormal, GCadForm.PCad);
      SLine := TLine.create(ax + 5, NullLine.ap1.y, ax + 5, GroundLine.ap1.y + Round(Dist* 0.45), 2, ord(psSolid), clBlack, 0, GCadForm.PCad.GetLayerHandle(0), mydsNormal, GCadForm.PCad);
      GCadForm.PCad.AddCustomFigure(1, TFigure(SLine), false);
      // перечеркнуть
      SLine := TLine.create(ax + 3, GroundLine.ap1.y + 4, ax + 7, GroundLine.ap1.y + 4, 2, ord(psSolid), clBlack, 0, GCadForm.PCad.GetLayerHandle(0), mydsNormal, GCadForm.PCad);
      GCadForm.PCad.AddCustomFigure(1, TFigure(SLine), false);
      SLine.Rotate((-45/180)*PI);
      //точка (на ноль)
      Circ := DrawPoint(ax + 6.5, GroundLine.ap1.y + 2.5, clBlack); //земля
      Circ.Radius := 0.7;
      //Circ.Move(0.5, 0.5);
      if UZOCompon = nil then // отисовка без УЗО (или что-то типа диффАвтомата)
      begin
        //SLine := TLine.create(ax + 5, GroundLine.ap1.y + Round(Dist* 0.6), ax, GroundLine.ap1.y + Round(Dist* 0.7), 2, ord(psSolid), clBlack, 0, GCadForm.PCad.GetLayerHandle(0), mydsNormal, GCadForm.PCad);
        SLine := TLine.create(ax + 5, GroundLine.ap1.y + Round(Dist* 0.45), ax, GroundLine.ap1.y + Round(Dist* 0.55), 2, ord(psSolid), clBlack, 0, GCadForm.PCad.GetLayerHandle(0), mydsNormal, GCadForm.PCad);
        GCadForm.PCad.AddCustomFigure(1, TFigure(SLine), false);

        DrawPoint(ax + 10, GroundLine.ap1.y, clBlack); //земля
        //SLine := TLine.create(ax + 10 , GroundLine.ap1.y, ax + 10 , GroundLine.ap1.y + Round(Dist* 0.6), 2, ord(psSolid), clBlack, 0, GCadForm.PCad.GetLayerHandle(0), mydsNormal, GCadForm.PCad);
        SLine := TLine.create(ax + 10 , GroundLine.ap1.y, ax + 10 , GroundLine.ap1.y + Round(Dist* 0.45), 2, ord(psSolid), clBlack, 0, GCadForm.PCad.GetLayerHandle(0), mydsNormal, GCadForm.PCad);
        GCadForm.PCad.AddCustomFigure(1, TFigure(SLine), false);

        SLine := TLine.create(ax + 8, GroundLine.ap1.y + 4, ax + 12, GroundLine.ap1.y + 4, 2, ord(psSolid), clBlack, 0, GCadForm.PCad.GetLayerHandle(0), mydsNormal, GCadForm.PCad);
        GCadForm.PCad.AddCustomFigure(1, TFigure(SLine), false);
        SLine.Rotate((-45/180)*PI);  // перечеркнуть
        //боковуха на перечеркивание земли
        SLine := TLine.create(ax + 11.5, GroundLine.ap1.y + 2, ax + 11.5, GroundLine.ap1.y + 3.2, 2, ord(psSolid), clBlack, 0, GCadForm.PCad.GetLayerHandle(0), mydsNormal, GCadForm.PCad);
        GCadForm.PCad.AddCustomFigure(1, TFigure(SLine), false);

        //SLine := TLine.create(ax + 10 , GroundLine.ap1.y + Round(Dist* 0.6), ax, GroundLine.ap1.y + Round(Dist* 0.7), 2, ord(psSolid), clBlack, 0, GCadForm.PCad.GetLayerHandle(0), mydsNormal, GCadForm.PCad);
        SLine := TLine.create(ax + 10 , GroundLine.ap1.y + Round(Dist* 0.45), ax, GroundLine.ap1.y + Round(Dist* 0.55), 2, ord(psSolid), clBlack, 0, GCadForm.PCad.GetLayerHandle(0), mydsNormal, GCadForm.PCad);
        GCadForm.PCad.AddCustomFigure(1, TFigure(SLine), false);

        // снизу
        //SLine := TLine.create(ax, ay, ax , GroundLine.ap1.y + Round(Dist* 0.4), 2, ord(psSolid), clBlack, 0, GCadForm.PCad.GetLayerHandle(0), mydsNormal, GCadForm.PCad);
        SLine := TLine.create(ax, ay, ax , GroundLine.ap1.y + Round(Dist* 0.2), 2, ord(psSolid), clBlack, 0, GCadForm.PCad.GetLayerHandle(0), mydsNormal, GCadForm.PCad);
        GCadForm.PCad.AddCustomFigure(1, TFigure(SLine), false);
      end
      else
      begin
        //SLine := TLine.create(ax + 5, GroundLine.ap1.y + Round(Dist* 0.6), ax, GroundLine.ap1.y + Round(Dist* 0.7), 2, ord(psSolid), clBlack, 0, GCadForm.PCad.GetLayerHandle(0), mydsNormal, GCadForm.PCad);
        SLine := TLine.create(ax + 5, GroundLine.ap1.y + Round(Dist* 0.45), ax, GroundLine.ap1.y + Round(Dist* 0.55), 2, ord(psSolid), clBlack, 0, GCadForm.PCad.GetLayerHandle(0), mydsNormal, GCadForm.PCad);
        GCadForm.PCad.AddCustomFigure(1, TFigure(SLine), false);

        DrawPoint(ax + 10, GroundLine.ap1.y, clBlack); //земля
        //SLine := TLine.create(ax + 10 , GroundLine.ap1.y, ax + 10 , GroundLine.ap1.y + Round(Dist* 0.6), 2, ord(psSolid), clBlack, 0, GCadForm.PCad.GetLayerHandle(0), mydsNormal, GCadForm.PCad);
        SLine := TLine.create(ax + 10 , GroundLine.ap1.y, ax + 10 , GroundLine.ap1.y + Round(Dist* 0.45), 2, ord(psSolid), clBlack, 0, GCadForm.PCad.GetLayerHandle(0), mydsNormal, GCadForm.PCad);
        GCadForm.PCad.AddCustomFigure(1, TFigure(SLine), false);

        SLine := TLine.create(ax + 8, GroundLine.ap1.y + 4, ax + 12, GroundLine.ap1.y + 4, 2, ord(psSolid), clBlack, 0, GCadForm.PCad.GetLayerHandle(0), mydsNormal, GCadForm.PCad);
        GCadForm.PCad.AddCustomFigure(1, TFigure(SLine), false);
        SLine.Rotate((-45/180)*PI);  // перечеркнуть
        //боковуха на перечеркивание земли
        SLine := TLine.create(ax + 11.5, GroundLine.ap1.y + 2, ax + 11.5, GroundLine.ap1.y + 3.2, 2, ord(psSolid), clBlack, 0, GCadForm.PCad.GetLayerHandle(0), mydsNormal, GCadForm.PCad);
        GCadForm.PCad.AddCustomFigure(1, TFigure(SLine), false);

        //SLine := TLine.create(ax + 10 , GroundLine.ap1.y + Round(Dist* 0.6), ax, GroundLine.ap1.y + Round(Dist* 0.7), 2, ord(psSolid), clBlack, 0, GCadForm.PCad.GetLayerHandle(0), mydsNormal, GCadForm.PCad);
        SLine := TLine.create(ax + 10 , GroundLine.ap1.y + Round(Dist* 0.45), ax, GroundLine.ap1.y + Round(Dist* 0.55), 2, ord(psSolid), clBlack, 0, GCadForm.PCad.GetLayerHandle(0), mydsNormal, GCadForm.PCad);
        GCadForm.PCad.AddCustomFigure(1, TFigure(SLine), false);

        // сверху
        SLine := TLine.create(ax, GroundLine.ap1.y + Round(Dist* 0.2), ax, GroundLine.ap1.y + Round(Dist* 0.2) + 5, 2, ord(psSolid), clBlack, 0, GCadForm.PCad.GetLayerHandle(0), mydsNormal, GCadForm.PCad);
        GCadForm.PCad.AddCustomFigure(1, TFigure(SLine), false);

        // УЗО
        SLine := TLine.create(ax - 3, GroundLine.ap1.y + Round(Dist* 0.2) + 5, ax, GroundLine.ap1.y + Round(Dist* 0.2) + 15, 2, ord(psSolid), clBlack, 0, GCadForm.PCad.GetLayerHandle(0), mydsNormal, GCadForm.PCad);
        GCadForm.PCad.AddCustomFigure(1, TFigure(SLine), false);

        Ellips := TEllipse.create(ax, GroundLine.ap1.y + Round(Dist* 0.2) + 18, 3, 1.5, 0, 2, 0, 0, ord(bsClear), 0, GCadForm.PCad.GetLayerHandle(0), mydsNormal, GCadForm.PCad);
        GCadForm.PCad.AddCustomFigure(1, TFigure(Ellips), false);

        SLine := TLine.create(ax - 5, GroundLine.ap1.y + Round(Dist* 0.2) + 18, ax - 3, GroundLine.ap1.y + Round(Dist* 0.2) + 18, 2, ord(psSolid), clBlack, 0, GCadForm.PCad.GetLayerHandle(0), mydsNormal, GCadForm.PCad);
        GCadForm.PCad.AddCustomFigure(1, TFigure(SLine), false);

        SLine := TLine.create(ax - 5, GroundLine.ap1.y + Round(Dist* 0.2) + 18, ax - 5, GroundLine.ap1.y + Round(Dist* 0.2) + 10, 2, ord(psSolid), clBlack, 0, GCadForm.PCad.GetLayerHandle(0), mydsNormal, GCadForm.PCad);
        GCadForm.PCad.AddCustomFigure(1, TFigure(SLine), false);

        SLine := TLine.create(ax - 5, GroundLine.ap1.y + Round(Dist* 0.2) + 10, ax - 1.8, GroundLine.ap1.y + Round(Dist* 0.2) + 10, 2, ord(psSolid), clBlack, 0, GCadForm.PCad.GetLayerHandle(0), mydsNormal, GCadForm.PCad);
        GCadForm.PCad.AddCustomFigure(1, TFigure(SLine), false);

        // снизу
        //SLine := TLine.create(ax, ay, ax , GroundLine.ap1.y + Round(Dist* 0.4), 2, ord(psSolid), clBlack, 0, GCadForm.PCad.GetLayerHandle(0), mydsNormal, GCadForm.PCad);
        SLine := TLine.create(ax, ay, ax , GroundLine.ap1.y + Round(Dist* 0.2) + 15, 2, ord(psSolid), clBlack, 0, GCadForm.PCad.GetLayerHandle(0), mydsNormal, GCadForm.PCad);
        GCadForm.PCad.AddCustomFigure(1, TFigure(SLine), false);
        // подписать
        TextList.Clear;
        //TextList.Add(ConnectedSwitchCompon.GetNameForVisible);
        TextList.Add('QD' + inttostr(UZOLIST.IndexOf(UzoCompon)+ 1));
        CurrText := CreateTextObject(ax - 5, GroundLine.ap1.y + Round(Dist* 0.2) + 10, TextList, True);
        currText.Move(ax - 5.5 - currText.Ap2.x, 0);
      end;
      // Кабель подписать
      if ConnectedSwitchCompon <> nil then
      begin
        //проверка на нагрузку и сечение кабеля
        isBadCable := False;
        currColor := clBlack;
        ComponProp := ConnectedSwitchCompon.GetPropertyBySysName(pnAluminium); // Проверка на материал кабеля(медь - по умолчанию, аллюминий - если задано свойство)
        // выбрать таблицу для расчета нагрузки на кабель в зависимости от его сечения
        if ComponProp = nil then //
          isCopper := True
        else
         if ComponProp.Value = '1' then
          isCopper := False;

        ComponProp := ConnectedSwitchCompon.GetPropertyBySysName(pnWireSection);
        if ComponProp = nil then
          isBadCable := True; // не задано сечени проводника(жилы)

        if not isBadCable then // проверить кабель на нагрузку
        begin
          Text := TList(DownObjectList[aIndex])[4];
          CountVal := StrToFloat_My(Text.re.text);
          if CountVal > 0 then
            isBadCable := CheckCableSection(isCopper, CountVal)
          else
            isBadCable := true;
        end;
        if isBadCable then
          currColor := clRed;

        TextList.Clear;
        //TextList.Add(ConnectedSwitchCompon.NameMark); // вывести только маркировку кабеля
        TextList.Add(CalcLineCaptionOne);
        CurrText := CreateTextObject(ax, ay, TextList, True);
        CurrText.Move(ax - 2 - CurrText.CenterPoint.x , ay - Dist/3 - 3 - currText.CenterPoint.y);
        CurrText.Rotate(-0.5 * PI);
        CurrText.Move(ax - 1 - currText.ap4.x, ay - currText.ap1.y - 3);

        //if isBadCable then
        //begin
        //  CurrText.re.font.Color := clRed;
        //  SLine.Color := clRed;
        //end;

        CurrText.re.font.Color := currColor;
        SLine.Color := currColor;

        // длину кабеля(с той стороны от линии)
        if aConnectedCablesList <> nil then
        begin
          // 08/06/2021 --
          {
          if aIndex <= aConnectedCablesList.Count then
          begin
            TextList.Clear;
            //TextList.Add('L = ' + FloatToStr(Roundx(TraceLength, 2)) + ' ' + GetUOMString(GCurrProjUnitOfMeasure));

           TextList.Add(CalcLineCaptionTwo);
            CurrText := CreateTextObject(ax, ay, TextList, True);
            CurrText.Move(ax + 2 - CurrText.CenterPoint.x , ay - Dist/3 - 3 - currText.CenterPoint.y);
            CurrText.Rotate(-0.5 * PI);
            CurrText.Move(ax + 1 - currText.ap1.x, ay - currText.ap1.y - 3);

            if isBadCable then
              CurrText.re.font.Color := clRed;
          end;
          }
        end;

        //разрыв линии
        //SLine := TLine.create(ax, GroundLine.ap1.y + Round(Dist* 0.3), ax - 3, GroundLine.ap1.y + 10, 2, ord(psSolid), clBlack, 0, GCadForm.PCad.GetLayerHandle(0), mydsNormal, GCadForm.PCad);
        SLine := TLine.create(ax, GroundLine.ap1.y + Round(Dist* 0.2), ax - 3, GroundLine.ap1.y + 10, 2, ord(psSolid), clBlack, 0, GCadForm.PCad.GetLayerHandle(0), mydsNormal, GCadForm.PCad);
        GCadForm.PCad.AddCustomFigure(1, TFigure(SLine), false);
        //значок автомата
        UserRect := TRectangle.create(ax - 2.9, GroundLine.ap1.y + Round(Dist* 0.13) - 2, ax - 1.9, GroundLine.ap1.y + Round(Dist* 0.15), 2, ord(psSolid){ord(psDash)}, clBlack, ord(bsClear), clRed, GCadForm.PCad.GetLayerHandle(0), mydsNormal, GCadForm.PCad);
        GCadForm.PCad.AddCustomFigure(1, TFigure(UserRect), false);
        UserRect.Rotate((-14.5/180)*PI);
      end;
    end;

    Procedure DrawElCounter;
    var i: integer;
        PointC: TCircle;
        sLine: tLine;
        BlockStream: TMemoryStream;
        ELCounterDrawFigure: TfigureGrpMod;
        BlockFigure: TBlock;
        Bnd: TDoubleRect;
        delta: Double;
        Rect: TRectangle;
    begin
      if PhazeLinePointFigures.Count > 1  then
        PointC := TCircle(PhazeLinePointFigures[PhazeLinePointFigures.Count - 2])
      else
        PointC := TCircle(PhazeLinePointFigures[0]);

      DrawPoint(PointC.ap1.x + 10, PointC.ap1.y, clBlack);
      DrawPoint(PointC.ap1.x + 15, PointC.ap1.y + 5, clBlack);

      SLine := TLine.create(PointC.ap1.x + 10, PointC.ap1.y, PointC.ap1.x + 10, PointC.ap1.y - 30, 2, ord(psSolid), clBlack, 0, GCadForm.PCad.GetLayerHandle(0), mydsNormal, GCadForm.PCad);
      GCadForm.PCad.AddCustomFigure(1, TFigure(SLine), false);
      SLine.Color := clRed;

      SLine := TLine.create(PointC.ap1.x + 15, PointC.ap1.y + 5, PointC.ap1.x + 15, PointC.ap1.y - 30, 2, ord(psSolid), clBlack, 0, GCadForm.PCad.GetLayerHandle(0), mydsNormal, GCadForm.PCad);
      GCadForm.PCad.AddCustomFigure(1, TFigure(SLine), false);
      SLine.Color := clBlue;

      BlockStream := ElCounter.GetObjectIconBlk;

      if BlockStream <> nil then
      begin
        if BlockStream.Size > 0 then
        begin
          BlockStream.Position := 0;
          ELCounterDrawFigure := TFigureGrpMod.Create(GCadForm.PCad.GetLayerHandle(0),GCadForm.PCad);

          BlockFigure := TBlock(GCadForm.PCad.InsertBlockFromStream(2, BlockStream, 0, 0));
          if BlockFigure <> nil then
          begin
            if BlockFigure.Selected then
              BlockFigure.deselect;
            try
              SetLayerHandleForFigureGrp(TFigureGrp(BlockFigure), GCadForm.PCad.GetLayerHandle(0));
            except
            end;

            Bnd := BlockFigure.GetBoundRect;
              delta := abs(Bnd.Top - Bnd.Bottom) / 2;

            for i := 0 to BlockFigure.InFigures.Count - 1 do
            begin
              ELCounterDrawFigure.AddFigure(TFigure(BlockFigure.InFigures[i]));
              TFigure(BlockFigure.InFigures[i]).Move(0, delta);
            end;
            GCadForm.PCad.Figures.Remove(BlockFigure);
            GCadForm.PCad.AddCustomFigure(1, TFigure(ELCounterDrawFigure), false);
            BlockFigure.InFigures.Clear;
            BlockFigure.Free;
          end;
        end;
        Bnd := ELCounterDrawFigure.GetBoundRect;
        Rect := TRectangle.create(Bnd.Left, Bnd.Top, Bnd.Right, Bnd.Bottom, 2, ord(psClear), clWhite, ord(psSolid), clWhite, GCadForm.PCad.GetLayerHandle(0), mydsNormal, GCadForm.PCad);
        ELCounterDrawFigure.InFigures.Insert(0, rect);
        ELCounterDrawFigure.Move(PointC.ap1.x + 9.5 - ELCounterDrawFigure.Ap1.x, PointC.ap1.y - 5 - ELCounterDrawFigure.Ap3.y);
      end;

      if BlockStream <> nil then
        BlockStream.free;
    end;

    Procedure DrawTable;
    var i, j: integer;
        bx, by, sx, sy, xDelta: Double;
        figList: TList;
        MaxHeight, MaxWidth: double;
        Fig: TFIgure;
        SLine: TLine;
        NPoint: TDoublePoint;
        UserRect: TRectangle;
        ComponProper: PProperty;
        //Circ: TCircle;

        function CheckSwitchAmperage(aIndex: integer): Boolean;
        var ComponProp: PProperty;
            TableVal: Double;
            Text: TRichText;
            SwitchCompon: TSCSComponent;
        begin
          Result := True;
          if aIndex <= Switches.Count then
          begin
            SwitchCompon := Switches[aIndex - 1];
            if SwitchCompon <> nil then
            begin
              ComponProp := SwitchCompon.GetPropertyBySysName(pnNominalA); // номинал автомата
              if ComponProp <> nil then
              begin
                Text := TList(DownObjectList[aIndex])[4];
                TableVal := StrToFloat_My(Text.re.text);
                if CompareValue(TableVal, StrToFloat_My(ComponProp.Value)) = 1 then
                  result := False;
              end
              else
                Result := False;
            end;
          end;
        end;
    begin
      //bx := GCadForm.PCad.WorkWidth/10 + 5;
      //bx := GCadForm.PCad.WorkWidth/10 + 1;
      bx := GCadForm.PCad.WorkWidth/10 - 10;
      //Tolik 21/07/2021 - -
      //sx := FloatInUOM(0.065, umMetr, GCurrProjUnitOfMeasure) * GCadForm.PCad.MapScale * 10;
      //sy := FloatInUOM(0.01, umMetr, GCurrProjUnitOfMeasure) * GCadForm.PCad.MapScale * 10;

      //sx := FloatInUOM(0.065, umMetr, GCurrProjUnitOfMeasure);// * GCadForm.PCad.MapScale * 10;
      //sy := FloatInUOM(0.01, umMetr, GCurrProjUnitOfMeasure); //* GCadForm.PCad.MapScale * 10;

      sx := 65;
      sy := 10;
      //
      for i := 0 to DownObjectList.Count - 1  do
      begin
        ConnectedSwitchCompon := nil; // кабель от автомата
        if i > 0 then
        begin
          if aCableList.Count >= (i - 1) then
            ConnectedSwitchCompon := aCableList[i - 1];
        end;

        FigList := TList(DownObjectList[i]);
        //by := 190; //(GCadForm.PCad.WorkHeight/2);
        by := 190;

        if i = 0  then
        begin
          SLine := TLine.create(bx, by - 3 - MaxTableHeightArray[0], bx + sx, by  - 3 - MaxTableHeightArray[0], 2, ord(psSolid), clBlack, 0, GCadForm.PCad.GetLayerHandle(0), mydsNormal, GCadForm.PCad);
          GCadForm.PCad.AddCustomFigure(1, TFigure(SLine), false);
        end
        else
        begin
          SLine := TLine.create(bx, by - 3 - MaxTableHeightArray[0], bx + MaxTableWidthArray[i] + 1, by  - 3 - MaxTableHeightArray[0], 2, ord(psSolid), clBlack, 0, GCadForm.PCad.GetLayerHandle(0), mydsNormal, GCadForm.PCad);
          GCadForm.PCad.AddCustomFigure(1, TFigure(SLine), false);
        end;
        if i = 0  then
        begin
          for j := 0 to FigList.Count - 1 do
          begin
            Fig := TFigure(FigList[j]);
            Fig.Move(bx - Fig.ap4.x + 2, by - Fig.ap4.y);
            SLine := TLine.create(bx, by + 3 , bx + sx, by + 3, 2, ord(psSolid), clBlack, 0, GCadForm.PCad.GetLayerHandle(0), mydsNormal, GCadForm.PCad);
            by := by + MaxTableHeightArray[j] + 5;
            GCadForm.PCad.AddCustomFigure(1, TFigure(SLine), false);
          end;
        end
        else
        begin
          for j := 0 to FigList.Count - 1 do
          begin
            Fig := TFigure(FigList[j]);
            Fig.Move(bx - Fig.ap4.x + 2, by - Fig.ap4.y);
            SLine := TLine.create(bx, by + 3 , bx + MaxTableWidthArray[i] + 1, by + 3, 2, ord(psSolid), clBlack, 0, GCadForm.PCad.GetLayerHandle(0), mydsNormal, GCadForm.PCad);
            by := by + MaxTableHeightArray[j] + 5;
            GCadForm.PCad.AddCustomFigure(1, TFigure(SLine), false);
          end;
        end;

        if i > 0 then
        begin
          DrawConnectionLine(bx + (MaxTableWidthArray[i])/2 ,190 - 3 - MaxTableHeightArray[0], i);
          // автомат подписать
          TextList.Clear;
          //TextList.Add(SwitchNames[i - 1]);
          TextList.Add('QF' + inttostr(i));
          TextList.Add(Switches[i - 1].NameMark);
          ComponProper := Switches[i - 1].GetPropertyBySysName(pnNominalA); // номинал автомата
          if ComponProper <> nil then
            //TextList.Add(ComponProper.Value + ' A' );  // если задан номинал автомата -- добавить к подписи
            TextList.Add('C' + ComponProper.Value);  // если задан номинал автомата -- добавить к подписи
          {
          ComponProper := Switches[i - 1].GetPropertyBySysName(pnNominalA); // номинал автомата
          if ComponProper <> nil then
            TextList.Add(Switches[i - 1].NameMark + ' ' + ComponProper.Value + ' A' )  // если задан номинал автомата -- добавить к подписи
          else
            TextList.Add(Switches[i - 1].NameMark);
          }
          //CurrText := CreateTextObject(bx + (MaxTableWidthArray[i])/2 - 25, GroundLine.ap1.y + 5, TextList, True);
          CurrText := CreateTextObject(bx + (MaxTableWidthArray[i])/2 - 15, GroundLine.ap1.y + 10, TextList, True);
          CurrText.Move(bx + (MaxTableWidthArray[i])/2 - 3.3 - CurrText.Ap3.x, 0);
          // проверить нагрузку на соответствие номиналу автомата
          if not CheckSwitchAmperage(i) then
            CurrText.re.font.Color := clRed;
          TextList.Clear;
        end;

        //bx := bx + MaxTableWidthArray[i] + 10;
        if i = 0  then
          bx := bx + sx - 10
        else
          bx := bx + MaxTableWidthArray[i] + 1;


        SLine := TLine.create(bx, 190 - 3 - MaxTableHeightArray[0], bx, by - 5, 2, ord(psSolid), clBlack, 0, GCadForm.PCad.GetLayerHandle(0), mydsNormal, GCadForm.PCad);
        GCadForm.PCad.AddCustomFigure(1, TFigure(SLine), false);
        if i = 0 then
        begin
          //Левая
          SLine := TLine.create(GCadForm.PCad.WorkWidth/10 - 20, 190 - 3 - MaxTableHeightArray[0], GCadForm.PCad.WorkWidth/10 - 20, by - 5, 2, ord(psSolid), clBlack, 0, GCadForm.PCad.GetLayerHandle(0), mydsNormal, GCadForm.PCad);
          GCadForm.PCad.AddCustomFigure(1, TFigure(SLine), false);
          //Правая
          SLine := TLine.create(GCadForm.PCad.WorkWidth/10 - 10, 190 - 3 - MaxTableHeightArray[0], GCadForm.PCad.WorkWidth/10 - 10, by - 5, 2, ord(psSolid), clBlack, 0, GCadForm.PCad.GetLayerHandle(0), mydsNormal, GCadForm.PCad);
          GCadForm.PCad.AddCustomFigure(1, TFigure(SLine), false);
          //Верхняя
          SLine := TLine.create(GCadForm.PCad.WorkWidth/10 - 20, 190 - 3 - MaxTableHeightArray[0], GCadForm.PCad.WorkWidth/10 - 10, 190 - 3 - MaxTableHeightArray[0], 2, ord(psSolid), clBlack, 0, GCadForm.PCad.GetLayerHandle(0), mydsNormal, GCadForm.PCad);
          GCadForm.PCad.AddCustomFigure(1, TFigure(SLine), false);
          //Нижняя
          //SLine := TLine.create(GCadForm.PCad.WorkWidth/10 - 20, by - 6, GCadForm.PCad.WorkWidth/10 - 10, by - 6, 2, ord(psSolid), clBlack, 0, GCadForm.PCad.GetLayerHandle(0), mydsNormal, GCadForm.PCad);
          //SLine := TLine.create(GCadForm.PCad.WorkWidth/10 - 20, by - 8, GCadForm.PCad.WorkWidth/10 - 10, by - 8, 2, ord(psSolid), clBlack, 0, GCadForm.PCad.GetLayerHandle(0), mydsNormal, GCadForm.PCad);
          SLine := TLine.create(GCadForm.PCad.WorkWidth/10 - 20, by - 5, GCadForm.PCad.WorkWidth/10 - 10, by - 5, 2, ord(psSolid), clBlack, 0, GCadForm.PCad.GetLayerHandle(0), mydsNormal, GCadForm.PCad);
          GCadForm.PCad.AddCustomFigure(1, TFigure(SLine), false);
        end;
      end;
      //bx := bx + 10;
      bx := bx + 2;

      // подравнять длину
      if aSwitchList.Count = 1 then // если будет только 1 автомат - оставить место для приконнекчивания "земли"
        NPoint.x := bx + 20
      else
        NPoint.x := bx;

      NPoint.z := 0;
      NPoint.y := Phaze_Line.ap2.y;
      Phaze_Line.ActualPoints[2] := NPoint;
      NPoint.y := NullLine.ap2.y;
      NullLine.ActualPoints[2] := NPoint;
      NPoint.y := GroundLine.ap2.y;
      GroundLine.ActualPoints[2] := NPoint;

      // Зона ответственности пользователя
      UserRect := TRectangle.create(Phaze_Line.ap1.x - 9, Phaze_Line.ap1.y - 20, Phaze_Line.ap2.x + 5, 160 , 2, ord(psDash), clBlack, ord(bsClear), clRed, GCadForm.PCad.GetLayerHandle(0), mydsNormal, GCadForm.PCad);
      GCadForm.PCad.AddCustomFigure(1, TFigure(UserRect), false);
      // Электросчетчик, если есть
      if ElCounter <> nil then
        DrawElCounter;

      //Приконнектить землю к зазeмлению
      drawPoint(GroundLine.AP2.x - 4, GroundLine.Ap2.y, clBlack);
      drawPoint(Phaze_Line.ap2.x + 5, GroundLine.Ap2.y + 5, clBlack);

      SLine := TLine.create(GroundLine.AP2.x - 4, GroundLine.Ap2.y, GroundLine.AP2.x - 4,  GroundLine.Ap2.y + 5, 2, ord(psSolid), clBlack, 0, GCadForm.PCad.GetLayerHandle(0), mydsNormal, GCadForm.PCad);
      GCadForm.PCad.AddCustomFigure(1, TFigure(SLine), false);

      SLine := TLine.create(GroundLine.AP2.x - 4, GroundLine.Ap2.y + 5, GroundLine.AP2.x + 15,  GroundLine.Ap2.y + 5, 2, ord(psSolid), clBlack, 0, GCadForm.PCad.GetLayerHandle(0), mydsNormal, GCadForm.PCad);
      GCadForm.PCad.AddCustomFigure(1, TFigure(SLine), false);

      SLine := TLine.create(GroundLine.AP2.x + 15 , GroundLine.Ap2.y, GroundLine.AP2.x + 15,  GroundLine.Ap2.y + 10, 2, ord(psSolid), clBlack, 0, GCadForm.PCad.GetLayerHandle(0), mydsNormal, GCadForm.PCad);
      GCadForm.PCad.AddCustomFigure(1, TFigure(SLine), false);

      SLine := TLine.create(GroundLine.AP2.x + 16 , GroundLine.Ap2.y + 2, GroundLine.AP2.x + 16,  GroundLine.Ap2.y + 8, 2, ord(psSolid), clBlack, 0, GCadForm.PCad.GetLayerHandle(0), mydsNormal, GCadForm.PCad);
      GCadForm.PCad.AddCustomFigure(1, TFigure(SLine), false);

      SLine := TLine.create(GroundLine.AP2.x + 17 , GroundLine.Ap2.y + 4, GroundLine.AP2.x + 17,  GroundLine.Ap2.y + 6, 2, ord(psSolid), clBlack, 0, GCadForm.PCad.GetLayerHandle(0), mydsNormal, GCadForm.PCad);
      GCadForm.PCad.AddCustomFigure(1, TFigure(SLine), false);
      //
      // верхняя часть таблицы
      bx := GCadForm.PCad.WorkWidth/10 - 20;
      by := 190;

      sy := 70;
      //sy := FloatInUOM(0.07, umMetr, GCurrProjUnitOfMeasure) * GCadForm.PCad.MapScale * 10;

      SLine := TLine.create(bx, by, bx, by - sy, 2, ord(psSolid), clBlack, 0, GCadForm.PCad.GetLayerHandle(0), mydsNormal, GCadForm.PCad);
          GCadForm.PCad.AddCustomFigure(1, TFigure(SLine), false);

      SLine := TLine.create(bx, by - sy, bx + sx, by - sy, 2, ord(psSolid), clBlack, 0, GCadForm.PCad.GetLayerHandle(0), mydsNormal, GCadForm.PCad);
          GCadForm.PCad.AddCustomFigure(1, TFigure(SLine), false);

      SLine := TLine.create(bx + sx, by - sy, bx + sx, by, 2, ord(psSolid), clBlack, 0, GCadForm.PCad.GetLayerHandle(0), mydsNormal, GCadForm.PCad);
          GCadForm.PCad.AddCustomFigure(1, TFigure(SLine), false);

      SLine := TLine.create(bx + sx/2, by - sy, bx + sx/2, by - 6, 2, ord(psSolid), clBlack, 0, GCadForm.PCad.GetLayerHandle(0), mydsNormal, GCadForm.PCad);
          GCadForm.PCad.AddCustomFigure(1, TFigure(SLine), false); // тут пополам

      TextList.Clear;
      TextList.Add(El_Mess9);
      TextList.Add(El_Mess10);
      TextList.Add(El_Mess11);
      TextList.Add(El_Mess12);

      CurrText := CreateTextObject(bx + 2, by - sy + 2, TextList, true);
      CurrText.Rotate(((-90)/180) * PI, CurrText.CenterPoint);
      CurrText.Move(bx + 16 - CurrText.CenterPoint.x, by - sy/2 - CurrText.CenterPoint.y);

      TextList.Clear;
      TextList.Add(El_Mess13);
      TextList.Add(El_Mess14);
      TextList.Add(El_Mess15);
      TextList.Add(El_Mess16);

      CurrText := CreateTextObject(bx + sx/2 + 2, by - sy + 2, TextList, true);
      CurrText.Rotate(((-90)/180) * PI, CurrText.CenterPoint);
      CurrText.Move(bx + 48 - CurrText.CenterPoint.x, by - sy/2 - CurrText.CenterPoint.y);

      by := by - sy;
      //sy := FloatInUOM(0.025, umMetr, GCurrProjUnitOfMeasure) * GCadForm.PCad.MapScale * 10;
      sy := 25;

      SLine := TLine.create(bx, by, bx, by - sy, 2, ord(psSolid), clBlack, 0, GCadForm.PCad.GetLayerHandle(0), mydsNormal, GCadForm.PCad);
          GCadForm.PCad.AddCustomFigure(1, TFigure(SLine), false);

      SLine := TLine.create(bx, by - sy, bx + sx, by - sy, 2, ord(psSolid), clBlack, 0, GCadForm.PCad.GetLayerHandle(0), mydsNormal, GCadForm.PCad);
          GCadForm.PCad.AddCustomFigure(1, TFigure(SLine), false);

      SLine := TLine.create(bx + sx, by - sy, bx + sx, by, 2, ord(psSolid), clBlack, 0, GCadForm.PCad.GetLayerHandle(0), mydsNormal, GCadForm.PCad);
          GCadForm.PCad.AddCustomFigure(1, TFigure(SLine), false);

      TextList.Clear;
      TextList.Add(El_Mess17);
      TextList.Add(El_Mess18);
      TextList.Add(El_Mess19);
      TextList.Add(El_Mess20);
      TextList.Add(El_Mess21);
      CurrText := CreateTextObject(bx + 2, by - sy + 2, TextList, true);

      by := by - sy;

      SLine := TLine.create(bx, by, bx, by - sy, 2, ord(psSolid), clBlack, 0, GCadForm.PCad.GetLayerHandle(0), mydsNormal, GCadForm.PCad);
          GCadForm.PCad.AddCustomFigure(1, TFigure(SLine), false);

      SLine := TLine.create(bx, by - sy, bx + sx, by - sy, 2, ord(psSolid), clBlack, 0, GCadForm.PCad.GetLayerHandle(0), mydsNormal, GCadForm.PCad);
          GCadForm.PCad.AddCustomFigure(1, TFigure(SLine), false);

      SLine := TLine.create(bx + sx, by - sy, bx + sx, by, 2, ord(psSolid), clBlack, 0, GCadForm.PCad.GetLayerHandle(0), mydsNormal, GCadForm.PCad);
          GCadForm.PCad.AddCustomFigure(1, TFigure(SLine), false);

      TextList.Clear;
      TextList.Add(El_Mess22);
      TextList.Add(El_Mess23);
      TextList.Add(El_Mess24);
      TextList.Add(El_Mess25);
      TextList.Add(El_Mess26);
      CurrText := CreateTextObject(bx + 2, by - sy + 2, TextList, true);

      by := by - sy;

      SLine := TLine.create(bx, by, bx, by - sy, 2, ord(psSolid), clBlack, 0, GCadForm.PCad.GetLayerHandle(0), mydsNormal, GCadForm.PCad);
          GCadForm.PCad.AddCustomFigure(1, TFigure(SLine), false);

      SLine := TLine.create(bx, by - sy, bx + sx, by - sy, 2, ord(psSolid), clBlack, 0, GCadForm.PCad.GetLayerHandle(0), mydsNormal, GCadForm.PCad);
          GCadForm.PCad.AddCustomFigure(1, TFigure(SLine), false);

      SLine := TLine.create(bx + sx, by - sy, bx + sx, by, 2, ord(psSolid), clBlack, 0, GCadForm.PCad.GetLayerHandle(0), mydsNormal, GCadForm.PCad);
          GCadForm.PCad.AddCustomFigure(1, TFigure(SLine), false);

      TextList.Clear;
      TextList.Add(El_Mess17);
      TextList.Add(El_Mess18);
      TextList.Add(El_Mess19);
      TextList.Add(El_Mess20);
      TextList.Add(El_Mess21);

      //TextList.Add('Пускатель магнитный');
      //TextList.Add('(устройство защитного');
      //TextList.Add('отключения или другие');
      //TextList.Add('аппараты): номер; тип;');
      //TextList.Add('номинальный ток, А');
      CurrText := CreateTextObject(bx + 2, by - sy + 2, TextList, true);
      CurrText.width := 60;

      by := by - sy;
      //sy := FloatInUOM(0.023, umMetr, GCurrProjUnitOfMeasure) * GCadForm.PCad.MapScale * 10;
      sy := 23;

      SLine := TLine.create(bx, by, bx, by - sy, 2, ord(psSolid), clBlack, 0, GCadForm.PCad.GetLayerHandle(0), mydsNormal, GCadForm.PCad);
          GCadForm.PCad.AddCustomFigure(1, TFigure(SLine), false);

      SLine := TLine.create(bx, by - sy, bx + sx, by - sy, 2, ord(psSolid), clBlack, 0, GCadForm.PCad.GetLayerHandle(0), mydsNormal, GCadForm.PCad);
          GCadForm.PCad.AddCustomFigure(1, TFigure(SLine), false);

      SLine := TLine.create(bx + sx, by - sy, bx + sx, by, 2, ord(psSolid), clBlack, 0, GCadForm.PCad.GetLayerHandle(0), mydsNormal, GCadForm.PCad);
          GCadForm.PCad.AddCustomFigure(1, TFigure(SLine), false);

      TextList.Clear;
      TextList.Add(El_Mess27);
      CurrText := CreateTextObject(bx + 2, by - sy/2, TextList, true);
    end;

   function GetNominalAmperage(aSwitchCompon: TSCSComponent): string;
   var ComponProp: PProperty;
   begin
     Result := ' 0 '; // Default Swintch 16 A
     if aSwitchCompon <> nil then
     begin
       ComponProp := aSwitchCompon.GetPropertyBySysName(pnNominalA);
       if ComponProp <> nil then
         Result := ComponProp.Value;
     end;
   end;

   function GetCountedPower(aList: TSCSComponents): String;
   var i: integer;
       ResPower: Double;
       ComponProp: PProperty;
   begin
     Result := ' 0 ';

     if aList <> nil then
     begin
       ResPower := 0;
       for i := 0 to aList.Count - 1 do
       begin
         ComponProp := aList[i].GetPropertyBySysName(pnMaxPower); // максимальная нагрузка
         if ComponProp = nil then
           ComponProp := aList[i].GetPropertyBySysName(pnPower); // мощность

         if ComponProp <> nil then
           ResPower := ResPower + StrToFloat_My(ComponProp.Value);
       end;
       if ResPower <> 0 then
         ResPower := RoundUP(ResPower);//RoundX(ResPower, 3);

       Result := ' ' + FloatTostr(ResPower) + ' ';
     end;
   end;

   Function GetCountedNominal(aList: TSCSComponents): string;
   var i: integer;
       ResVal, PowerVal: double;
       ComponProp: PProperty;
   begin
     Result := ' 0 ';
     if aList <> nil then
     begin
       ResVal := 0;
       for i := 0 to aList.Count - 1 do
       begin
         ComponProp := aList[i].GetPropertyBySysName(pnMaxPower);
         if ComponProp = nil then
           ComponProp := aList[i].GetPropertyBySysName(pnPower);
         if ComponProp <> nil then
         begin
           ResVal := ResVal + (StrToFloat_My(ComponProp.Value) * 1000)/220;
         end;
       end;
     end;
     if ResVal <> 0 then
     begin
       if aList.Count > 2 then
         ResVal := ResVal * 0.8;

       ResVal := RoundUP(ResVal);//Roundx(ResVal, 3);

       Result := ' ' + FloatToStr(ResVal) + ' ';
     end;
   end;

Begin
  try
    OldGCadForm := GCadForm;
    ListParams := GetListParamsForNewList;

    ListParams.Caption := El_Mess28;//cChooseComponType_Mes1;

    ListParams.Name := ListParams.Caption;
    ListParams.MarkID := 0;
    //ListParams.Settings.ListType := lt_ProjectPlan;
    ListParams.Settings.ListType := lt_ElScheme;
    MakeEditList(meMake, ListParams, False);
    AllChemeFiguresList := TList.Create;

    //DrawProjectPlan(GCadForm, ComponTypes, cbDivideGroupsByJoinedNetTypes.Checked, cbShowGroupContents.Checked);
    //DisableOptionsForDesignList;
  except
    on e: Exception do;
  end;
  SetLength(CableSectArray, 11);
  SetLength(CopperAmperVals,11);
  SetLength(AllumAmperVals,11);
  SetLength(CopperPowerVals, 11);
  SetLength(AllumPowerVals, 11);

  // сечения жил кабеля, мм2
  CableSectArray[0] := 0.5;
  CableSectArray[1] := 0.75;
  CableSectArray[2] := 1;
  CableSectArray[3] := 1.5;
  CableSectArray[4] := 2;
  CableSectArray[5] := 2.5;
  CableSectArray[6] := 4;
  CableSectArray[7] := 6;
  CableSectArray[8] := 10;
  CableSectArray[9] := 16;
  CableSectArray[10] := 25;
  // Медь
  //сила тока, А
  CopperAmperVals[0] := 11;
  CopperAmperVals[1] := 15;
  CopperAmperVals[2] := 17;
  CopperAmperVals[3] := 23;
  CopperAmperVals[4] := 26;
  CopperAmperVals[5] := 30;
  CopperAmperVals[6] := 41;
  CopperAmperVals[7] := 50;
  CopperAmperVals[8] := 80;
  CopperAmperVals[9] := 100;
  CopperAmperVals[10] := 140;
  // мощность, кВт
  CopperPowerVals[0] := 2.4;
  CopperPowerVals[1] := 3.3;
  CopperPowerVals[2] := 3.7;
  CopperPowerVals[3] := 5;
  CopperPowerVals[4] := 5.7;
  CopperPowerVals[5] := 6.6;
  CopperPowerVals[6] := 9;
  CopperPowerVals[7] := 11;
  CopperPowerVals[8] := 17;
  CopperPowerVals[9] := 22;
  CopperPowerVals[10] := 30;
  //Аллюминий
  //сила тока, А
  AllumAmperVals[0] := 0;
  AllumAmperVals[1] := 0;
  AllumAmperVals[2] := 0;
  AllumAmperVals[3] := 0;
  AllumAmperVals[4] := 21;
  AllumAmperVals[5] := 24;
  AllumAmperVals[6] := 32;
  AllumAmperVals[7] := 39;
  AllumAmperVals[8] := 60;
  AllumAmperVals[9] := 75;
  AllumAmperVals[10] := 100;
  // мощность, квт
  AllumPowerVals[0] := 0;
  AllumPowerVals[1] := 0;
  AllumPowerVals[2] := 0;
  AllumPowerVals[3] := 0;
  AllumPowerVals[4] := 4.6;
  AllumPowerVals[5] := 5.2;
  AllumPowerVals[6] := 7;
  AllumPowerVals[7] := 8.5;
  AllumPowerVals[8] := 13;
  AllumPowerVals[9] := 16;
  AllumPowerVals[10] := 23;

  UzoList := TSCSComponents.Create(false);
  ElCounter := nil;
  PhazeLinePointFigures := TList.Create;

  CurrColor := clBlack;

  // список УЗО в Щитке (и электросчетчик, если есть)
  for i := 0 to aBoxList[0].ChildReferences.Count - 1 do
  begin
    if aBoxList[0].ChildReferences[i].ComponentType.SysName = ctsnUZO then
      UzoList.Add(aBoxList[0].ChildReferences[i])
    else
    begin
      if ElCounter = nil then
        if aBoxList[0].ChildReferences[i].ComponentType.SysName =  ctsnElCounter then
          ElCounter := aBoxList[0].ChildReferences[i]; // первый попавшийся (пока что)
    end;
  end;

  if GCadForm <> OldGCadForm then
  begin
    Line_x1 := GCadForm.PCad.WorkWidth/10 + 55;
    //Line_y1 := GCadForm.PCad.WorkHeight/2;
    Line_y1 := GCadForm.PCad.WorkHeight/3 - 37 ;
    Line_x2 := GCadForm.PCad.WorkWidth/2;
    {
    Phaze_Line := TOrthoLine.create(Line_x1, Line_y1, 0, Line_x2, Line_y1, 0, 4, ord(psSolid), clBlue, 0, GCadForm.PCad.GetLayerHandle(0), mydsNormal, GCadForm.PCad, False, False);
    GCadForm.PCad.AddCustomFigure(1, TFigure(Phaze_Line), false);

    NullLine := TOrthoLine.create(Line_x1, Line_y1 - 20, 0, Line_x2, Line_y1 - 20, 0, 4, ord(psSolid), clBlack, 0, GCadForm.PCad.GetLayerHandle(0), mydsNormal, GCadForm.PCad, False, False);
    GCadForm.PCad.AddCustomFigure(1, TFigure(NullLine), false);

    GroundLine := TOrthoLine.create(Line_x1, Line_y1 + 20, 0, Line_x2, Line_y1 + 20, 0, 4, ord(psSolid), clRed, 0, GCadForm.PCad.GetLayerHandle(0), mydsNormal, GCadForm.PCad, False, False);
    GCadForm.PCad.AddCustomFigure(1, TFigure(GroundLine), false);
    }
    // Lines Count
    Line_Count := aSwitchList.Count;
    TextList := TStringList.Create;

    // фаза
    Phaze_Line := TLine.create(Line_x1, Line_y1, Line_x2, Line_y1, 3, ord(psSolid), clRed, 0, GCadForm.PCad.GetLayerHandle(0), mydsNormal, GCadForm.PCad);
    GCadForm.PCad.AddCustomFigure(1, TFigure(Phaze_Line), false);
    TextList.Clear;
    //TextList.Add('L1 L2 L3');
    TextList.Add('L1'); // пока что будет только 220 В
    CurrText := CreateTextObject(Line_x1 + 2, Line_y1 - 4, TextList, true);
    //ноль
    NullLine := TLine.create(Line_x1, Line_y1 + 5,  Line_x2, Line_y1 + 5, 3, ord(psSolid), clBlue, 0, GCadForm.PCad.GetLayerHandle(0), mydsNormal, GCadForm.PCad);
    GCadForm.PCad.AddCustomFigure(1, TFigure(NullLine), false);
    TextList.Clear;
    TextList.Add('N');
    CurrText := CreateTextObject(Line_x1 + 2, Line_y1 + 1, TextList, true);
    //земля
    GroundLine := TLine.create(Line_x1, Line_y1 + 10,  Line_x2, Line_y1 + 10, 3, ord(psSolid), clBlack, 0, GCadForm.PCad.GetLayerHandle(0), mydsNormal, GCadForm.PCad);
    GCadForm.PCad.AddCustomFigure(1, TFigure(GroundLine), false);
    TextList.Clear;
    TextList.Add('PE');
    CurrText := CreateTextObject(Line_x1 + 2, Line_y1 + 6, TextList, true);

    Line_y1 := Line_y1 + 70;

    TextFigList := TList.Create;

    TextList.Clear;
    TextList.Add(El_Mess1);
    CurrText := CreateTextObject(Line_x1 - 80, Line_y1 + 80, TextList);
    CurrText.Rotate(((-90)/180) * PI, CurrText.CenterPoint);
    //TextFigList.Add(CurrText);

    ColObjList := TList.Create;

    TextList.Clear;
    TextList.Add(El_Mess2);
    CurrText := CreateTextObject(Line_x1 - 30, Line_y1 + 10, TextList);
    TextFigList.Add(CurrText);

    TextList.Clear;
    TextList.Add(El_Mess3);
    CurrText := CreateTextObject(Line_x1, Line_y1 + 20, TextList);
    TextFigList.Add(CurrText);

    TextList.Clear;
    TextList.Add(El_Mess4);
    CurrText := CreateTextObject(Line_x1, Line_y1 + 30, TextList);
    TextFigList.Add(CurrText);

    TextList.Clear;
    TextList.Add(El_Mess5);
    CurrText := CreateTextObject(Line_x1, Line_y1 + 40, TextList);
    TextFigList.Add(CurrText);

    TextList.Clear;
    TextList.Add(El_Mess6);
    CurrText := CreateTextObject(Line_x1, Line_y1 + 50, TextList);
    TextFigList.Add(CurrText);

    TextList.Clear;
    TextList.Add(El_Mess7);
    CurrText := CreateTextObject(Line_x1, Line_y1 + 60, TextList);
    TextFigList.Add(CurrText);

    DownObjectList := TList.Create;
    DownObjectList.Add(TextFigList);

    k := 0;

    SwitchNames := TStringList.Create;
    LineCounter := 0;
    for i := 0 to aSwitchList.Count - 1 do
    begin
      Switches := TSCSComponents(aSwitchList[i]);
      for j := 0 to Switches.Count - 1 do // Автомат(линия)
      begin
        SwitchNames.Add(Switches[j].GetNameForVisible);
        ComponDrawFigure := nil;
        Line_x1 := Line_x2;
        Connections := TSCSComponents(aConnectedList[k]);
        ColObjList := TList.Create;
        TextList.Clear;
        inc(LineCounter);
        //получить угошку потребителя
        for l := 0 to Connections.Count - 1 do
        begin
          TextList.Add(Connections[l].NAme);
          if Connections[l].IsLine = biFalse then
          begin
            ParentCatalog := Connections[l].GetFirstParentCatalog;
            if ParentCatalog <> nil then
            begin
              ComponList := Connections[l].GetListOwner;
              if ComponList <> nil then
              begin
                ComponCad := GetListByID(ComponList.SCSID);
                if ComponCad <> nil then
                begin
                  ComponFigure := GetFigureByID(ComponCad, ParentCatalog.SCSID);
                  if ComponFigure <> nil then
                  begin
                    if ComponFigure is TConnectorObject then
                      if TConnectorObject(ComponFigure).ConnectorType = ct_NB then
                        if TConnectorObject(ComponFigure).DrawFigure <> nil then
                        begin
                          ComponDrawFigure := TFigureGrpMod(TConnectorObject(ComponFigure).DrawFigure.Duplicate);
                          GCadForm.PCad.AddCustomFigure(1, TFigure(ComponDrawFigure), False);
                          ColObjList.Add(ComponDrawFigure);
                          break;
                       end;
                  end;
                end;
              end;
            end;
          end;
        end;
        if ComponDrawFigure = nil then
          ColObjList.Add(Nil); // если нет фигуры отрисовки
        ConnectedSwitchCompon := nil;
        if aCableList.Count >= k  then
          ConnectedSwitchCompon := aCableList[k];

        // номер линии
        TextList.Clear;
        TextList.Add(inttostr(LineCounter));
        CurrText := CreateTextObject(1, 1, TextList);
        ColObjList.Add(CurrText);
        // Расчетная мощность, кВт'
        TextList.Clear;

        if ParentCatalog <> nil then
          TextList.Add(GetCountedPower(Connections))
        else
          TextList.Add(' 0 ');

        CurrText := CreateTextObject(1, 1, TextList);
        ColObjList.Add(CurrText);
        // Номинальный ток, А
        TextList.Clear;
        //TextList.Add(inttostr(LineCounter));
        TextList.Add(GetNominalAmperage(Switches[i]));
        CurrText := CreateTextObject(1, 1, TextList);
        ColObjList.Add(CurrText);
        //Расчетный ток, А
        TextList.Clear;
        //TextList.Add(inttostr(LineCounter));
        TextList.Add(GetCountedNominal(Connections));
        CurrText := CreateTextObject(1, 1, TextList);
        ColObjList.Add(CurrText);
        // получить список нагрузок
        TextList.Free;
        TextList := GetComponNamesCounted(Connections);
        CurrText := CreateTextObject(1, 1, TextList);
        ColObjList.Add(CurrText);
        inc(k);

        DownObjectList.Add(ColObjList);
      end;
    end;

    if DownObjectList.Count > 0  then
    begin
      CountMaxValues(DownObjectList);
      DrawTable;
    end;
    SwitchNames.Free;
    AllChemeFiguresList.Free;
    SetLength(CableSectArray, 0);
    SetLength(CopperAmperVals, 0);
    SetLength(AllumAmperVals, 0);
    SetLength(CopperPowerVals, 0);
    SetLength(AllumPowerVals, 0);
    GCadForm.PCad.Refresh;
  end;
end;

Procedure BuildWiringSchemeList;
var
    i: integer;
    ListParams: TListParams;
    OldGCadForm: TF_Cad;
    Switches, Connections, UzoList: TSCSComponents;
    ConnectedCompon: TSCSComponent;
    CurrText: TRichText;
    TextList: TStringList;
    IconFigList: TList;
    ParentCatalog: TSCSCatalog;
    ComponFigure: TFigure;
    ComponCad, currCad: TF_Cad;
    ComponList: TSCSList;
    DrawFigureStream: TMemoryStream;
    ComponDrawFigure: TFigureGrpMod;
    ObjImage: TBMPObject;
    Stream: Classes.TStream;

    ShieldCatalog: TSCSCatalog;
    ShieldCompon: TSCSComponent;
    Uzo_List, AutomatList: TList;

    Bmp: TBmpObject;
    BmpList: TList;
    start_x: integer;
    FlDir: string;
    LineL, LineP, LineN: TLine;
    MaxHeight: Double;
Begin
  try
    if GpopupFigure <> nil then
    begin
      ShieldCatalog := nil;
      ShieldCatalog := F_ProjMan.GSCSBase.CurrProject.CurrList.GetCatalogFromReferencesBySCSID(GPopupFigure.Id);
      if ShieldCatalog <> nil then
      begin
        //Shield Compon
        ShieldCompon := nil;
        for i := 0 to ShieldCatalog.ComponentReferences.Count - 1 do
        begin
          if ShieldCatalog.ComponentReferences[i].IsTop then
          begin
            if ShieldCatalog.ComponentReferences[i].ComponentType.SysName = ctsnShield then
            begin
              ShieldCompon := ShieldCatalog.ComponentReferences[i];
              break;
            end;
          end;
        end;

       { F_ShieldConfig.Memo1.Clear;
        F_ShieldConfig.Memo2.Clear; }
       { F_ShieldConfig.ListBox1.clear;
        F_ShieldConfig.ListBox2.Clear;}
        F_ShieldConfig.InitForm;

        if ShieldCompon.ChildReferences.Count > 0 then
        begin
          for i := 0 to ShieldCompon.ChildReferences.Count - 1 do
          begin
            //F_ShieldConfig.Memo1.Lines.Add(ShieldCompon.ChildReferences[i].Name + ' ' + ShieldCompon.ChildReferences[i].NameMark);
            F_ShieldConfig.ListBox1.AddItem(ShieldCompon.ChildReferences[i].Name + ' ' + ShieldCompon.ChildReferences[i].NameMark, nil);
          end;
        end;

        if F_ShieldConfig.ShowModal = mrOk  then
        begin
          //Shield Components
          if ShieldCompon.ChildReferences.Count > 0 then
          begin
            Uzo_List := TList.Create;
            AutomatList := TList.Create;
            for i := 0 to ShieldCompon.ChildReferences.Count - 1 do
            begin
              if ShieldCompon.ChildReferences[i].ComponentType.SysName = ctsnUZO then
                Uzo_List.Add(ShieldCompon.ChildReferences[i])
              else
              if ShieldCompon.ChildReferences[i].ComponentType.SysName = ctsnAutoSwitch then
                AutomatList.Add(ShieldCompon.ChildReferences[i]);
            end;
          end;

          if ((Uzo_List.Count > 0) or (AutomatList.Count > 0)) then
          begin
            OldGCadForm := GCadForm;
            ListParams := GetListParamsForNewList;
            ListParams.Caption := El_Mess30;
            ListParams.Name := ListParams.Caption;
            ListParams.MarkID := 0;
            ListParams.Settings.ListType := lt_AScheme;
            MakeEditList(meMake, ListParams, False);
            i := 0;
            try
              FlDir := ExtractFileDir(Application.ExeName);
              start_x := 50;
              GLoadImageAsIs := true;
              BmpList := TList.Create;
              MaxHeight := 0;
              //вводный автомат
              //электросчетчик
              //
              //УЗО
              for i := 0 to Uzo_List.Count - 1 do
              begin
                Bmp := TBmpObject(GCadForm.PCad.InsertBitmap(0, start_x, 50, FlDir + '\IMG\Electro\Uzo.bmp', false, false, false));
                BmpList.Add(Bmp);
                Bmp.Selected := False;
                start_x := start_x + Round(Bmp.ap2.x  - Bmp.ap1.x) + 30;
                MaxHeight := max(MaxHeight, bmp.ap4.y - bmp.ap1.y);
              end;
              //автоматы
              for i := 0 to AutomatList.Count - 1 do
              begin
                Bmp := TBmpObject(GCadForm.PCad.InsertBitmap(0, start_x, 50, FlDir + '\IMG\Electro\avt.bmp', false, false, false));
                start_x := start_x + Round(Bmp.ap2.x  - Bmp.ap1.x) + 30;
                MaxHeight := max(MaxHeight, bmp.ap4.y);
                BmpList.Add(Bmp);
                Bmp.Selected := False;
              end;
            finally
              GLoadImageAsIs := false;
            end;
            i := 0;
            //здесь линии рисовать
            MaxHeight := MaxHeight + 30;
            Bmp := TBmpObject(BmpList[BmpList.Count - 1]);
            //Line
            LineL := TLine.create(27, 30, Bmp.ap2.x - ((Bmp.ap2.x - Bmp.ap1.x)/2), 30, 5, ord(psSolid), clRed, 0, GCadForm.PCad.GetLayerHandle(0), mydsNormal, GCadForm.PCad);
            GCadForm.PCad.AddCustomFigure(1, TFigure(LineL), false);
            //Zero
            LineL := TLine.create(27, 35, 45, 35, 5, ord(psSolid), clBlue, 0, GCadForm.PCad.GetLayerHandle(0), mydsNormal, GCadForm.PCad);
            GCadForm.PCad.AddCustomFigure(1, TFigure(LineL), false);
            LineL := TLine.create(45, 35, 45, MaxHeight - 20, 5, ord(psSolid), clBlue, 0, GCadForm.PCad.GetLayerHandle(0), mydsNormal, GCadForm.PCad);
            GCadForm.PCad.AddCustomFigure(1, TFigure(LineL), false);
            LineL := TLine.create(45, MaxHeight - 20, Bmp.ap1.x + (Bmp.ap2.x - Bmp.ap1.x)/2 + 3, MaxHeight - 20, 5, ord(psSolid), clBlue, 0, GCadForm.PCad.GetLayerHandle(0), mydsNormal, GCadForm.PCad);
            GCadForm.PCad.AddCustomFigure(1, TFigure(LineL), false);
            //Ground
            LineL := TLine.create(27, 40, 40, 40, 5, ord(psSolid), clGreen, 0, GCadForm.PCad.GetLayerHandle(0), mydsNormal, GCadForm.PCad);
            GCadForm.PCad.AddCustomFigure(1, TFigure(LineL), false);
            LineL := TLine.create(40, 40, 40, MaxHeight - 10, 5, ord(psSolid), clGreen, 0, GCadForm.PCad.GetLayerHandle(0), mydsNormal, GCadForm.PCad);
            GCadForm.PCad.AddCustomFigure(1, TFigure(LineL), false);
            LineL := TLine.create(40, MaxHeight - 10, Bmp.ap1.x + (Bmp.ap2.x - Bmp.ap1.x)/2 - 3, MaxHeight - 10, 5, ord(psSolid), clGreen, 0, GCadForm.PCad.GetLayerHandle(0), mydsNormal, GCadForm.PCad);
            GCadForm.PCad.AddCustomFigure(1, TFigure(LineL), false);

            for i := 0 to  BmpList.Count - 1 do
            begin
              Bmp := TBmpObject(BmpList[i]);
              // Line
              LineL := TLine.create(Bmp.ap2.x - ((Bmp.ap2.x - Bmp.ap1.x)/2), 30, Bmp.ap2.x - ((Bmp.ap2.x - Bmp.ap1.x)/2), Bmp.ap1.y, 5, ord(psSolid), clRed, 0, GCadForm.PCad.GetLayerHandle(0), mydsNormal, GCadForm.PCad);
              GCadForm.PCad.AddCustomFigure(1, TFigure(LineL), false);
              LineL := TLine.create(Bmp.ap2.x - ((Bmp.ap2.x - Bmp.ap1.x)/2), Bmp.ap3.y, Bmp.ap2.x - ((Bmp.ap2.x - Bmp.ap1.x)/2), Maxheight, 5, ord(psSolid), clRed, 0, GCadForm.PCad.GetLayerHandle(0), mydsNormal, GCadForm.PCad);
              GCadForm.PCad.AddCustomFigure(1, TFigure(LineL), false);
              //Zero
              LineL := TLine.create(Bmp.ap2.x - ((Bmp.ap2.x - Bmp.ap1.x)/2) + 3, Bmp.ap3.y + 10, Bmp.ap2.x - ((Bmp.ap2.x - Bmp.ap1.x)/2) + 3, Maxheight, 5, ord(psSolid), clBlue, 0, GCadForm.PCad.GetLayerHandle(0), mydsNormal, GCadForm.PCad);
              GCadForm.PCad.AddCustomFigure(1, TFigure(LineL), false);
              //Ground
              LineL := TLine.create(Bmp.ap2.x - ((Bmp.ap2.x - Bmp.ap1.x)/2) - 3, Bmp.ap3.y + 20, Bmp.ap2.x - ((Bmp.ap2.x - Bmp.ap1.x)/2) - 3, Maxheight, 5, ord(psSolid), clGreen, 0, GCadForm.PCad.GetLayerHandle(0), mydsNormal, GCadForm.PCad);
              GCadForm.PCad.AddCustomFigure(1, TFigure(LineL), false);
            end;
            TextList := TStringList.Create;
            TextList.Add('L1');
            CurrText := CreateTextObject(23, 28, TextList, True);
            TextList.Clear;
            TextList.Add('P');
            CurrText := CreateTextObject(23, 33, TextList, True);
            TextList.Clear;
            TextList.Add('N');
            CurrText := CreateTextObject(23, 38, TextList, True);
            TextList.Free;
          end;
        end;
      end;
    end;
  except
    on e: Exception do;
  end;
  //CurrColor := clBlack;
  GCadForm.PCad.Refresh;
  end;
End.