public selectedVertex inside(int index, Point target)
        {
            //find shortest dist
            selectedVertex result = new selectedVertex();
            double         distance = double.MaxValue, buff;

            result.vertex = new Point(-1, -1);
            foreach (var i in vertexes[index])
            {
                buff = Schemes_Editor.distance(i, target);
                if (buff < 30 && buff < distance)
                {
                    result.vertex = i;
                    distance      = buff;
                }
            }
            bool firstTime = true;

            for (int i = 1; i < points[index].Count - 1; i++)
            {
                buff = Schemes_Editor.distance(points[index][i], target);
                if (buff < 30 && (buff < distance || firstTime))
                {
                    firstTime            = false;
                    distance             = buff;
                    result.vertex        = points[index][i];
                    result.isExists      = true;
                    result.ExistingIndex = i;
                }
            }
            return(result);
        }
        public int insertPoint(Point a, int index)
        {
            //найти между каким точками раположена
            points[index][0] = new Point(firstEquip.locations[index].X + firstEquip.scales[index].X / 2, firstEquip.locations[index].Y + firstEquip.scales[index].Y / 2);
            points[index][points[index].Count - 1] = new Point(secondEquip.locations[index].X + secondEquip.scales[index].X / 2, secondEquip.locations[index].Y + secondEquip.scales[index].Y / 2);

            //calculate length
            double step = 0;

            for (int i = 0; i < points[index].Count - 1; i++)
            {
                step += Schemes_Editor.distance(points[index][i], points[index][i + 1]);
            }
            step /= 100.0;

            vertexes[index].Clear();

            for (int i = 0; i < points[index].Count - 1; i++)
            {
                double localdistance = Schemes_Editor.distance(points[index][i], points[index][i + 1]);
                double angle         = Math.Atan((points[index][i + 1].Y - points[index][i].Y) / ((points[index][i + 1].X - points[index][i].X) * 1.0));
                if (points[index][i].X > points[index][i + 1].X)
                {
                    for (int j = 0; j < localdistance / step; j++)
                    {
                        Point getted = new Point((int)(points[index][i].X - j * step * Math.Cos(angle)), (int)(points[index][i].Y - j * step * Math.Sin(angle)));
                        if (getted.X == a.X && getted.Y == a.Y)
                        {
                            points[index].Insert(i + 1, a);
                            return(i + 1);
                        }
                    }
                }
                else
                {
                    for (int j = 0; j < localdistance / step; j++)
                    {
                        Point getted = new Point((int)(points[index][i].X + j * step * Math.Cos(angle)), (int)(points[index][i].Y + j * step * Math.Sin(angle)));
                        if (getted.X == a.X && getted.Y == a.Y)
                        {
                            points[index].Insert(i + 1, a);
                            return(i + 1);
                        }
                    }
                }
            }
            return(-1);
        }
        public void rebuild(int index)
        {
            points[index][0] = new Point(firstEquip.locations[index].X + firstEquip.scales[index].X / 2, firstEquip.locations[index].Y + firstEquip.scales[index].Y / 2);
            points[index][points[index].Count - 1] = new Point(secondEquip.locations[index].X + secondEquip.scales[index].X / 2, secondEquip.locations[index].Y + secondEquip.scales[index].Y / 2);

            //calculate length
            double step = 0;

            for (int i = 0; i < points[index].Count - 1; i++)
            {
                step += Schemes_Editor.distance(points[index][i], points[index][i + 1]);
            }
            step /= 100.0;

            vertexes[index].Clear();

            for (int i = 0; i < points[index].Count - 1; i++)
            {
                double localdistance = Schemes_Editor.distance(points[index][i], points[index][i + 1]);
                double angle         = Math.Atan((points[index][i + 1].Y - points[index][i].Y) / ((points[index][i + 1].X - points[index][i].X) * 1.0));
                if (points[index][i].X > points[index][i + 1].X)
                {
                    for (int j = 0; j < localdistance / step; j++)
                    {
                        vertexes[index].Add(new Point((int)(points[index][i].X - j * step * Math.Cos(angle)), (int)(points[index][i].Y - j * step * Math.Sin(angle))));
                    }
                }
                else
                {
                    for (int j = 0; j < localdistance / step; j++)
                    {
                        vertexes[index].Add(new Point((int)(points[index][i].X + j * step * Math.Cos(angle)), (int)(points[index][i].Y + j * step * Math.Sin(angle))));
                    }
                }
            }
        }
        static public void MOVE(object sender, MouseEventArgs e)
        {
            mousePosition = e.Location;
            switch (Mode)
            {
            case modeStruct.moveVinosku:
                float distt = Math.Abs(movable.vinoska[localSheet].vertex1.X - movable.vinoska[localSheet].vertex2.X);
                movable.vinoska[localSheet].vertex1 = new Point((int)(e.Location.X - distt / 2.0f), e.Location.Y);
                movable.vinoska[localSheet].vertex2 = new Point((int)(e.Location.X + distt / 2.0f), e.Location.Y);

                if (movable.vinoska[localSheet].vertex2.X < movable.vinoska[localSheet].startPoint.X)
                {
                    Point t = movable.vinoska[localSheet].vertex1;
                    movable.vinoska[localSheet].vertex1 = movable.vinoska[localSheet].vertex2;
                    movable.vinoska[localSheet].vertex2 = t;
                }
                break;

            case modeStruct.dragVertex:
                Schemes_Editor.wires[SelectedWireIndex].points[localSheet][VertexNumber] = new Point(e.Location.X, e.Location.Y);
                break;

            case modeStruct.moveRoom:
                movable.move(new Point(e.X - Prev.X, e.Y - Prev.Y), localSheet);
                break;

            case modeStruct.doNothing_NOSCALEMODE:
                indexToSurround = -1;
                isRoomSelected  = false;
                isWireSelected  = false;

                for (int i = 0; i < Schemes_Editor.wires.Count; i++)
                {
                    selectedVertex ver = Schemes_Editor.wires[i].inside(localSheet, e.Location);
                    if (ver.vertex.X != -1)
                    {
                        isWireSelected    = true;
                        SelectedWireIndex = i;
                        break;
                    }
                }
                if (isWireSelected)
                {
                    break;
                }
                for (int i = Schemes_Editor.mainWorkList.Count - 1; i > -1; i--)
                {
                    if (Schemes_Editor.mainWorkList[i] is inboxes)
                    {
                        continue;
                    }

                    if (Schemes_Editor.mainWorkList[i].inside(e.Location, localSheet))
                    {
                        indexToSurround = i;
                        break;
                    }
                }
                if (indexToSurround != -1)
                {
                    break;
                }
                else
                {
                    for (int i = 0; i < Schemes_Editor.rooms.Count; i++)
                    {
                        if (Schemes_Editor.rooms[i].inside(e.Location, localSheet))
                        {
                            isRoomSelected  = true;
                            indexToSurround = i;
                            break;
                        }
                    }
                }
                break;


            case modeStruct.doNothing_SCALEMODE:
                scalePoint = new Point(-1, -1);
                //finding nearest dots
                for (int i = 0; i < Schemes_Editor.mainWorkList.Count; i++)
                {
                    //if (Schemes_Editor.mainWorkList[i] is wire_s)
                    //    continue;
                    if (Schemes_Editor.mainWorkList[i] is inboxes && ((inboxes)Schemes_Editor.mainWorkList[i]).inbox)
                    {
                        continue;
                    }
                    else
                    {
                        int a = Schemes_Editor.mainWorkList[i].locations[localSheet].X, b = Schemes_Editor.mainWorkList[i].locations[localSheet].Y, c = Schemes_Editor.mainWorkList[i].locations[localSheet].X + Schemes_Editor.mainWorkList[i].scales[localSheet].X, d = Schemes_Editor.mainWorkList[i].locations[localSheet].Y + Schemes_Editor.mainWorkList[i].scales[localSheet].Y;
                        if (Schemes_Editor.distance(new Point(a, b), e.Location) < 20)
                        {
                            scalePoint = new Point(a, b);
                            break;
                        }
                        if (Schemes_Editor.distance(new Point(a, d), e.Location) < 20)
                        {
                            scalePoint = new Point(a, d);
                            break;
                        }
                        if (Schemes_Editor.distance(new Point(c, b), e.Location) < 20)
                        {
                            scalePoint = new Point(c, b);
                            break;
                        }
                        if (Schemes_Editor.distance(new Point(c, d), e.Location) < 20)
                        {
                            scalePoint = new Point(c, d);
                            break;
                        }
                    }
                }
                if (scalePoint.X != -1)
                {
                    break;
                }
                else
                {
                    for (int i = 0; i < Schemes_Editor.rooms.Count; i++)
                    {
                        int a = Schemes_Editor.rooms[i].locations[localSheet].X, b = Schemes_Editor.rooms[i].locations[localSheet].Y, c = Schemes_Editor.rooms[i].locations[localSheet].X + Schemes_Editor.rooms[i].locations[localSheet].Width, d = Schemes_Editor.rooms[i].locations[localSheet].Y + Schemes_Editor.rooms[i].locations[localSheet].Height;
                        if (Schemes_Editor.distance(new Point(a, b), e.Location) < 20)
                        {
                            scalePoint = new Point(a, b);
                            break;
                        }
                        if (Schemes_Editor.distance(new Point(a, d), e.Location) < 20)
                        {
                            scalePoint = new Point(a, d);
                            break;
                        }
                        if (Schemes_Editor.distance(new Point(c, b), e.Location) < 20)
                        {
                            scalePoint = new Point(c, b);
                            break;
                        }
                        if (Schemes_Editor.distance(new Point(c, d), e.Location) < 20)
                        {
                            scalePoint = new Point(c, d);
                            break;
                        }
                    }
                }
                break;

            case modeStruct.dragShkaf:
                movable.move(new Point(e.Location.X - Prev.X, e.Location.Y - Prev.Y), localSheet);
                break;

            case modeStruct.dragShkafnoe:
                movable.move(new Point(e.Location.X - Prev.X, e.Location.Y - Prev.Y), localSheet);
                break;

            case modeStruct.scaleSomething:
                if (isRoomSelected)
                {
                    var buff = Schemes_Editor.rooms[moveTargetIndex];
                    if (pointNum == 0)
                    {
                        //num 3
                        Point p = new Point(buff.locations[localSheet].X + buff.locations[localSheet].Width, buff.locations[localSheet].Y + buff.locations[localSheet].Height);

                        buff.locations[localSheet] = new Rectangle(e.Location.X, e.Location.Y, p.X - e.Location.X, p.Y - e.Location.Y);

                        scalePoint = e.Location;
                    }
                    if (pointNum == 1)
                    {
                        //num 3
                        Point p = new Point(buff.locations[localSheet].X, buff.locations[localSheet].Y + buff.locations[localSheet].Height);

                        buff.locations[localSheet] = new Rectangle(buff.locations[localSheet].X, e.Location.Y, e.Location.X - p.X, p.Y - e.Location.Y);

                        scalePoint = e.Location;
                    }
                    if (pointNum == 2)
                    {
                        //num 3
                        buff.locations[localSheet] = new Rectangle(buff.locations[localSheet].X, buff.locations[localSheet].Y, e.Location.X - buff.locations[localSheet].X, e.Location.Y - buff.locations[localSheet].Y);
                        // buff.locations[2] = e.Location;
                        scalePoint = e.Location;
                    }
                    if (pointNum == 3)
                    {
                        //num 3
                        Point p = new Point(buff.locations[localSheet].X + buff.locations[localSheet].Width, buff.locations[localSheet].Y);

                        buff.locations[localSheet] = new Rectangle(e.Location.X, p.Y, p.X - e.Location.X, e.Location.Y - p.Y);

                        scalePoint = e.Location;
                    }
                }
                else
                {
                    var buff = Schemes_Editor.mainWorkList[moveTargetIndex];
                    if (pointNum == 0)
                    {
                        //num 3
                        Point p = new Point(buff.locations[localSheet].X + buff.scales[localSheet].X, buff.locations[localSheet].Y + buff.scales[localSheet].Y);
                        buff.scales[localSheet]    = new Point(p.X - e.Location.X, p.Y - e.Location.Y);
                        buff.locations[localSheet] = e.Location;
                        scalePoint = e.Location;
                    }
                    if (pointNum == 1)
                    {
                        //num 3
                        Point p = new Point(buff.locations[localSheet].X, buff.locations[localSheet].Y + buff.scales[localSheet].Y);
                        buff.scales[localSheet]    = new Point(e.Location.X - p.X, p.Y - e.Location.Y);
                        buff.locations[localSheet] = new Point(e.Location.X - buff.scales[localSheet].X, e.Location.Y);
                        scalePoint = e.Location;
                    }
                    if (pointNum == 2)
                    {
                        //num 3
                        buff.scales[localSheet] = new Point(e.Location.X - buff.locations[localSheet].X, e.Location.Y - buff.locations[localSheet].Y);
                        // buff.locations[2] = e.Location;
                        scalePoint = e.Location;
                    }
                    if (pointNum == 3)
                    {
                        //num 3
                        Point p = new Point(buff.locations[localSheet].X + buff.scales[localSheet].X, buff.locations[localSheet].Y);
                        buff.scales[localSheet]    = new Point(p.X - e.Location.X, e.Location.Y - p.Y);
                        buff.locations[localSheet] = new Point(e.Location.X, p.Y);
                        scalePoint = e.Location;
                    }
                }
                break;
            }
            draw();
        }
        static public void DOWN(object sender, MouseEventArgs e)
        {
            mousePosition = e.Location;
            if (e.Button == MouseButtons.Right)
            {
                movable        = null;
                isDrawSelected = false;
                if (isWireSelected)
                {
                    movable = Schemes_Editor.wires[SelectedWireIndex];
                    ContextMenu menushka = new ContextMenu(new MenuItem[] { new MenuItem("Добавить выноску", handler), new MenuItem("Копировать", handler), new MenuItem("Удалить", handler), new MenuItem("Удалить узел", handler), new MenuItem("Изменить название", handler) });
                    menushka.Show(father.strct, e.Location);
                    return;
                }
                for (int i = Schemes_Editor.mainWorkList.Count - 1; i > -1; i--)
                {
                    if (Schemes_Editor.mainWorkList[i].inside(e.Location, localSheet))
                    {
                        if (Schemes_Editor.mainWorkList[i] is inboxes)
                        {
                            movable = Schemes_Editor.mainWorkList[i];

                            isDrawSelected = true;
                            ContextMenu menushka = new ContextMenu(new MenuItem[] { new MenuItem("Добавить выноску", handler), new MenuItem("Копировать", handler), new MenuItem("Удалить", handler), new MenuItem("Удалить узел", handler), new MenuItem("Изменить название", handler) });
                            menushka.Show(father.strct, e.Location);
                            return;
                        }
                        if (Schemes_Editor.mainWorkList[i] is boxes)
                        {
                            movable        = Schemes_Editor.mainWorkList[i];
                            isDrawSelected = true;
                            ContextMenu menushka = new ContextMenu(new MenuItem[] { new MenuItem("Добавить выноску", handler), new MenuItem("Копировать", handler), new MenuItem("Удалить", handler), new MenuItem("Удалить узел", handler), new MenuItem("Изменить название", handler) });
                            menushka.Show(father.strct, e.Location);
                            return;
                        }
                        if (Schemes_Editor.mainWorkList[i] is free)
                        {
                            movable        = Schemes_Editor.mainWorkList[i];
                            isDrawSelected = true;
                            ContextMenu menushka = new ContextMenu(new MenuItem[] { new MenuItem("Добавить выноску", handler), new MenuItem("Копировать", handler), new MenuItem("Удалить", handler), new MenuItem("Удалить узел", handler), new MenuItem("Изменить название", handler) });
                            menushka.Show(father.strct, e.Location);
                            return;
                        }
                        break;
                    }
                }
                for (int i = 0; i < Schemes_Editor.rooms.Count; i++)
                {
                    if (Schemes_Editor.rooms[i].inside(e.Location, localSheet))
                    {
                        isRoomSelected = true;
                        movable        = Schemes_Editor.rooms[i];
                        ContextMenu menushka = new ContextMenu(new MenuItem[] { new MenuItem("Добавить выноску", handler), new MenuItem("Копировать", handler), new MenuItem("Удалить", handler), new MenuItem("Удалить узел", handler), new MenuItem("Изменить название", handler) });
                        menushka.Show(father.strct, e.Location);
                        return;
                    }
                }
            }
            else
            {
                switch (Mode)
                {
                case modeStruct.doNothing_NOSCALEMODE:
                    movable = null;

                    if (isWireSelected)
                    {
                        var t = Schemes_Editor.wires[SelectedWireIndex].inside(localSheet, e.Location);
                        if (t.isExists)
                        {
                            VertexNumber = t.ExistingIndex;
                            if (VertexNumber != -1)
                            {
                                Mode    = modeStruct.dragVertex;
                                movable = 1;
                            }
                        }
                        else     //создать новую опорную точку
                        {
                            VertexNumber = Schemes_Editor.wires[SelectedWireIndex].insertPoint(t.vertex, localSheet);
                            if (VertexNumber != -1)
                            {
                                Mode    = modeStruct.dragVertex;
                                movable = 1;
                            }
                        }
                    }
                    if (movable != null)
                    {
                        break;
                    }
                    for (int i = Schemes_Editor.mainWorkList.Count - 1; i > -1; i--)
                    {
                        if (Schemes_Editor.mainWorkList[i].inside(e.Location, localSheet))
                        {
                            if (Schemes_Editor.mainWorkList[i] is inboxes)
                            {
                                Prev    = new Point(e.Location.X - ((inboxes)Schemes_Editor.mainWorkList[i]).locations[localSheet].X, e.Location.Y - ((inboxes)Schemes_Editor.mainWorkList[i]).locations[localSheet].Y);
                                Mode    = modeStruct.dragShkafnoe;
                                movable = Schemes_Editor.mainWorkList[i];
                            }
                            if (Schemes_Editor.mainWorkList[i] is boxes)
                            {
                                movable = Schemes_Editor.mainWorkList[i];
                                Prev    = new Point(e.Location.X - ((boxes)Schemes_Editor.mainWorkList[i]).locations[localSheet].X, e.Location.Y - ((boxes)Schemes_Editor.mainWorkList[i]).locations[localSheet].Y);
                                Mode    = modeStruct.dragShkaf;
                            }
                            if (Schemes_Editor.mainWorkList[i] is free)
                            {
                                movable = Schemes_Editor.mainWorkList[i];
                                Prev    = new Point(e.Location.X - ((free)Schemes_Editor.mainWorkList[i]).locations[localSheet].X, e.Location.Y - ((free)Schemes_Editor.mainWorkList[i]).locations[localSheet].Y);
                                Mode    = modeStruct.dragShkaf;
                            }
                            break;
                        }
                    }
                    if (movable != null)
                    {
                        break;
                    }
                    else
                    {
                        for (int i = 0; i < Schemes_Editor.rooms.Count; i++)
                        {
                            if (Schemes_Editor.rooms[i].inside(e.Location, localSheet))
                            {
                                isRoomSelected = true;
                                Prev           = new Point(e.Location.X - Schemes_Editor.rooms[i].locations[localSheet].X, e.Location.Y - Schemes_Editor.rooms[i].locations[localSheet].Y);
                                movable        = Schemes_Editor.rooms[i];
                                Mode           = modeStruct.moveRoom;
                            }
                        }
                    }
                    break;


                case modeStruct.doNothing_SCALEMODE:
                    moveTargetIndex = -1;
                    for (int i = 0; i < Schemes_Editor.mainWorkList.Count; i++)
                    {
                        //if (Schemes_Editor.mainWorkList[i] is wire_s)
                        //    continue;
                        if (Schemes_Editor.mainWorkList[i] is inboxes && ((inboxes)Schemes_Editor.mainWorkList[i]).inbox)
                        {
                            continue;
                        }

                        int a = Schemes_Editor.mainWorkList[i].locations[localSheet].X, b = Schemes_Editor.mainWorkList[i].locations[localSheet].Y, c = Schemes_Editor.mainWorkList[i].locations[localSheet].X + Schemes_Editor.mainWorkList[i].scales[localSheet].X, d = Schemes_Editor.mainWorkList[i].locations[localSheet].Y + Schemes_Editor.mainWorkList[i].scales[localSheet].Y;
                        if (Schemes_Editor.distance(new Point(a, b), e.Location) < 20)
                        {
                            scalePoint = new Point(a, b); moveTargetIndex = i; pointNum = 0;
                            Mode       = modeStruct.scaleSomething;
                            break;
                        }
                        if (Schemes_Editor.distance(new Point(a, d), e.Location) < 20)
                        {
                            scalePoint = new Point(a, d); moveTargetIndex = i; pointNum = 3;
                            Mode       = modeStruct.scaleSomething;
                            break;
                        }
                        if (Schemes_Editor.distance(new Point(c, b), e.Location) < 20)
                        {
                            scalePoint = new Point(c, b); moveTargetIndex = i; pointNum = 1;
                            Mode       = modeStruct.scaleSomething;
                            break;
                        }
                        if (Schemes_Editor.distance(new Point(c, d), e.Location) < 20)
                        {
                            scalePoint = new Point(c, d); moveTargetIndex = i; pointNum = 2;
                            Mode       = modeStruct.scaleSomething;
                            break;
                        }
                    }
                    if (moveTargetIndex != -1)
                    {
                        break;
                    }
                    else
                    {
                        for (int i = 0; i < Schemes_Editor.rooms.Count; i++)
                        {
                            int a = Schemes_Editor.rooms[i].locations[localSheet].X, b = Schemes_Editor.rooms[i].locations[localSheet].Y, c = Schemes_Editor.rooms[i].locations[localSheet].X + Schemes_Editor.rooms[i].locations[localSheet].Width, d = Schemes_Editor.rooms[i].locations[localSheet].Y + Schemes_Editor.rooms[i].locations[localSheet].Height;
                            if (Schemes_Editor.distance(new Point(a, b), e.Location) < 20)
                            {
                                isRoomSelected = true;
                                scalePoint     = new Point(a, b); moveTargetIndex = i; pointNum = 0;
                                Mode           = modeStruct.scaleSomething;
                                break;
                            }
                            if (Schemes_Editor.distance(new Point(a, d), e.Location) < 20)
                            {
                                isRoomSelected = true;
                                scalePoint     = new Point(a, d); moveTargetIndex = i; pointNum = 3;
                                Mode           = modeStruct.scaleSomething;
                                break;
                            }
                            if (Schemes_Editor.distance(new Point(c, b), e.Location) < 20)
                            {
                                isRoomSelected = true;
                                scalePoint     = new Point(c, b); moveTargetIndex = i; pointNum = 1;
                                Mode           = modeStruct.scaleSomething;
                                break;
                            }
                            if (Schemes_Editor.distance(new Point(c, d), e.Location) < 20)
                            {
                                isRoomSelected = true;
                                scalePoint     = new Point(c, d); moveTargetIndex = i; pointNum = 2;
                                Mode           = modeStruct.scaleSomething;
                                break;
                            }
                        }
                    }
                    break;
                }
            }
        }
        public static void MOVE(object sender, MouseEventArgs e)
        {
            try
            {
                switch (Mode)
                {
                case modeShkaf.moveVinosku:
                    float distt = Math.Abs(element.vinoska[localSheet].vertex1.X - element.vinoska[localSheet].vertex2.X);
                    element.vinoska[localSheet].vertex1 = new Point((int)(e.Location.X - distt / 2.0f), e.Location.Y);
                    element.vinoska[localSheet].vertex2 = new Point((int)(e.Location.X + distt / 2.0f), e.Location.Y);

                    if (element.vinoska[localSheet].vertex2.X < element.vinoska[localSheet].startPoint.X)
                    {
                        Point t = element.vinoska[localSheet].vertex1;
                        element.vinoska[localSheet].vertex1 = element.vinoska[localSheet].vertex2;
                        element.vinoska[localSheet].vertex2 = t;
                    }
                    break;

                case modeShkaf.dragShkaf:
                    movable.move(new Point(e.Location.X - Prev.X, e.Location.Y - Prev.Y), 2);
                    break;

                case modeShkaf.dragShkafnoe:
                    hasRect = false;
                    inboxes pointer = (inboxes)movable;
                    pointer.inbox = false;
                    foreach (boxes j in Schemes_Editor.mainWorkList.FindAll(x => x is boxes))
                    {
                        if (!j.inside(e.Location, 2))
                        {
                            continue;
                        }
                        //если внутри необходимых свободных ячеек, то выделить их
                        List <int> acceptable = new List <int>();
                        for (int k = 0; k < j.units; k++)
                        {
                            acceptable.Add(k);
                        }
                        for (int k = 0; k < j.equipInside.Count; k++)
                        {
                            //метка всех занятых
                            for (int d = j.positions[k]; d < j.positions[k] + j.unitsSeized[k]; d++)
                            {
                                acceptable[d] = -1;
                            }
                        }
                        acceptable.RemoveAll(x => x == -1);
                        for (int k = 0; k < acceptable.Count - 1; k++)
                        {
                            int  current = acceptable[k];
                            bool good    = true;
                            for (int d = 1; d < pointer.numberOfUnits; d++)
                            {
                                if (k + d == acceptable.Count)
                                {
                                    good = false;
                                    break;
                                }
                                if (acceptable[k + d] != current + 1)
                                {
                                    good = false;
                                    break;
                                }
                                current++;
                            }
                            if (!good)
                            {
                                acceptable.RemoveAt(k);
                                k--;
                            }
                        }
                        //поиск тех, внутри которых находимся
                        foreach (var k in acceptable)
                        {
                            if (
                                e.X > j.locations[2].X + 20 &&
                                e.X <j.locations[2].X + j.scales[2].X - 20 &&
                                     e.Y> j.locations[2].Y + 30 + k * (j.unitSize + 1) &&
                                e.Y < j.locations[2].Y + 30 + (k + 1) * (j.unitSize + 1))
                            {
                                hasRect            = true;
                                rect               = new Rectangle(j.locations[2].X + 20, (int)(j.locations[2].Y + 30 + k * (j.unitSize)), j.scales[2].X - 40, (int)j.unitSize);
                                boxForInser        = j;
                                indexToInsertInBox = k;
                                movable.move(new Point(e.Location.X - Prev.X, e.Location.Y - Prev.Y), 2);
                                draw();
                                return;
                            }
                        }
                    }
                    movable.move(new Point(e.Location.X - Prev.X, e.Location.Y - Prev.Y), 2);
                    break;

                case modeShkaf.doNothing_NOSCALEMODE:
                    indexToSurround = -1;
                    for (int i = Schemes_Editor.mainWorkList.Count - 1; i > -1; i--)
                    {
                        if (Schemes_Editor.mainWorkList[i].inside(e.Location, 2))
                        {
                            indexToSurround = i;
                            break;
                        }
                    }
                    break;

                case modeShkaf.doNothing_SCALEMODE:
                    scalePoint = new Point(-1, -1);
                    //finding nearest dots
                    for (int i = 0; i < Schemes_Editor.mainWorkList.Count; i++)
                    {
                        //if (Schemes_Editor.mainWorkList[i] is wire_s)
                        //    continue;
                        if (Schemes_Editor.mainWorkList[i] is inboxes && ((inboxes)Schemes_Editor.mainWorkList[i]).inbox)
                        {
                            continue;
                        }
                        else if (Schemes_Editor.mainWorkList[i] is boxes)
                        {
                            int a = Schemes_Editor.mainWorkList[i].locations[2].X, b = Schemes_Editor.mainWorkList[i].locations[2].Y, c = Schemes_Editor.mainWorkList[i].locations[2].X + Schemes_Editor.mainWorkList[i].scales[2].X, d = Schemes_Editor.mainWorkList[i].locations[2].Y + Schemes_Editor.mainWorkList[i].scales[2].Y;
                            if (Schemes_Editor.distance(new Point(a, b), e.Location) < 20)
                            {
                                scalePoint = new Point(a, b);
                                break;
                            }
                            if (Schemes_Editor.distance(new Point(a, d), e.Location) < 20)
                            {
                                scalePoint = new Point(a, d);
                                break;
                            }
                            if (Schemes_Editor.distance(new Point(c, b), e.Location) < 20)
                            {
                                scalePoint = new Point(c, b);
                                break;
                            }
                            if (Schemes_Editor.distance(new Point(c, d), e.Location) < 20)
                            {
                                scalePoint = new Point(c, d);
                                break;
                            }
                        }
                    }
                    break;

                case modeShkaf.scaleSomething:
                    var buff = Schemes_Editor.mainWorkList[moveTargetIndex];
                    if (pointNum == 0)
                    {
                        //num 3
                        Point p = new Point(buff.locations[2].X + buff.scales[2].X, buff.locations[2].Y + buff.scales[2].Y);
                        buff.scales[2]    = new Point(p.X - e.Location.X, p.Y - e.Location.Y);
                        buff.locations[2] = e.Location;
                        scalePoint        = e.Location;
                    }
                    if (pointNum == 1)
                    {
                        //num 3
                        Point p = new Point(buff.locations[2].X, buff.locations[2].Y + buff.scales[2].Y);
                        buff.scales[2]    = new Point(e.Location.X - p.X, p.Y - e.Location.Y);
                        buff.locations[2] = new Point(e.Location.X - buff.scales[2].X, e.Location.Y);
                        scalePoint        = e.Location;
                    }
                    if (pointNum == 2)
                    {
                        //num 3
                        buff.scales[2] = new Point(e.Location.X - buff.locations[2].X, e.Location.Y - buff.locations[2].Y);
                        // buff.locations[2] = e.Location;
                        scalePoint = e.Location;
                    }
                    if (pointNum == 3)
                    {
                        //num 3
                        Point p = new Point(buff.locations[2].X + buff.scales[2].X, buff.locations[2].Y);
                        buff.scales[2]    = new Point(p.X - e.Location.X, e.Location.Y - p.Y);
                        buff.locations[2] = new Point(e.Location.X, p.Y);
                        scalePoint        = e.Location;
                    }
                    break;
                }
                draw();
            }
            catch (Exception ex) { Mode = modeShkaf.doNothing_NOSCALEMODE; }
        }
        //
        public static void DOWN(object sender, MouseEventArgs e)
        {
            switch (e.Button)
            {
            case MouseButtons.Right:
                switch (Mode)
                {
                case modeShkaf.doNothing_NOSCALEMODE:
                    for (int i = Schemes_Editor.mainWorkList.Count - 1; i > -1; i--)
                    {
                        if (Schemes_Editor.mainWorkList[i].inside(e.Location, 2))
                        {
                            if (Schemes_Editor.mainWorkList[i] is inboxes)
                            {
                            }
                            if (Schemes_Editor.mainWorkList[i] is boxes)
                            {
                            }
                            element = Schemes_Editor.mainWorkList[i];
                            break;
                        }
                    }



                    ContextMenu menushka = new ContextMenu(new MenuItem[] { new MenuItem("Добавить выноску", handler), new MenuItem("Копировать", handler), new MenuItem("Удалить", handler), new MenuItem("Изменить название", handler) });
                    menushka.Show(father.pictureBox1, e.Location);
                    break;
                }
                break;



            case MouseButtons.Left:
                switch (Mode)
                {
                case modeShkaf.moveVinosku:
                    Mode = modeShkaf.doNothing_NOSCALEMODE;
                    break;

                case modeShkaf.doNothing_NOSCALEMODE:         //
                    movable = null;
                    for (int i = Schemes_Editor.mainWorkList.Count - 1; i > -1; i--)
                    {
                        if (Schemes_Editor.mainWorkList[i].inside(e.Location, 2))
                        {
                            if (Schemes_Editor.mainWorkList[i] is inboxes)
                            {
                                Prev    = new Point(e.Location.X - ((inboxes)Schemes_Editor.mainWorkList[i]).locations[2].X, e.Location.Y - ((inboxes)Schemes_Editor.mainWorkList[i]).locations[2].Y);
                                Mode    = modeShkaf.dragShkafnoe;
                                hasRect = false;
                                movable = Schemes_Editor.mainWorkList[i];
                                foreach (boxes j in Schemes_Editor.mainWorkList.FindAll(x => x is boxes))
                                {
                                    int pos = j.equipInside.IndexOf(j.equipInside.Find(x => x.localID == movable.localID));
                                    if (pos != -1)
                                    {
                                        j.equipInside.RemoveAt(pos);
                                        j.positions.RemoveAt(pos);
                                        j.unitsSeized.RemoveAt(pos);
                                    }
                                }
                                ((inboxes)Schemes_Editor.mainWorkList[i]).inbox = false;
                            }
                            if (Schemes_Editor.mainWorkList[i] is boxes)
                            {
                                movable = Schemes_Editor.mainWorkList[i];
                                Prev    = new Point(e.Location.X - ((boxes)Schemes_Editor.mainWorkList[i]).locations[2].X, e.Location.Y - ((boxes)Schemes_Editor.mainWorkList[i]).locations[2].Y);
                                Mode    = modeShkaf.dragShkaf;
                            }

                            break;
                        }
                    }
                    break;

                case modeShkaf.doNothing_SCALEMODE:
                    moveTargetIndex = -1;
                    for (int i = 0; i < Schemes_Editor.mainWorkList.Count; i++)
                    {
                        //if (Schemes_Editor.mainWorkList[i] is wire_s)
                        //    continue;
                        if (Schemes_Editor.mainWorkList[i] is inboxes && ((inboxes)Schemes_Editor.mainWorkList[i]).inbox)
                        {
                            continue;
                        }

                        int a = Schemes_Editor.mainWorkList[i].locations[2].X, b = Schemes_Editor.mainWorkList[i].locations[2].Y, c = Schemes_Editor.mainWorkList[i].locations[2].X + Schemes_Editor.mainWorkList[i].scales[2].X, d = Schemes_Editor.mainWorkList[i].locations[2].Y + Schemes_Editor.mainWorkList[i].scales[2].Y;
                        if (Schemes_Editor.distance(new Point(a, b), e.Location) < 20)
                        {
                            scalePoint = new Point(a, b); moveTargetIndex = i; pointNum = 0;
                            Mode       = modeShkaf.scaleSomething;
                            break;
                        }
                        if (Schemes_Editor.distance(new Point(a, d), e.Location) < 20)
                        {
                            scalePoint = new Point(a, d); moveTargetIndex = i; pointNum = 3;
                            Mode       = modeShkaf.scaleSomething;
                            break;
                        }
                        if (Schemes_Editor.distance(new Point(c, b), e.Location) < 20)
                        {
                            scalePoint = new Point(c, b); moveTargetIndex = i; pointNum = 1;
                            Mode       = modeShkaf.scaleSomething;
                            break;
                        }
                        if (Schemes_Editor.distance(new Point(c, d), e.Location) < 20)
                        {
                            scalePoint = new Point(c, d); moveTargetIndex = i; pointNum = 2;
                            Mode       = modeShkaf.scaleSomething;
                            break;
                        }
                    }
                    break;
                }
                break;
            }
        }