2019년 8월 19일 월요일

c# A4 네모영역 채우기

뭐 누군가한테는 도움이 되지 않을까나.

------------------------------------------
using System;
using System.Drawing;
using System.Collections.Generic;

namespace MySpace
{
    //VO클래스
    class RectWithPage
    {
        public Rectangle rect;
        public int nPage;
        public int nIndex;
    }

    //생성클래스
    class RectMaker
    {
        //내부변수
        public int nGap { get; set; }
        public Size pageSize { get; set; }
        private List<RectWithPage> list_rects;
        private Point CurPoint;
        private int CurOrder = 0;
        private int CurPage = 1;       

        //Default 생성자
        public RectMaker() {    //Default
            this.nGap = 5;
            this.pageSize = new Size(827, 1170);    //A4 Default.           
        }       
        //매개생성자
        public RectMaker(int aGap, Size aSize)
        {
            this.nGap = aGap;
            this.pageSize = aSize;
        }

        //초기화
        private void clearValues()
        {
            if(this.list_rects != null){ this.list_rects.Clear(); }
            this.list_rects = new List<RectWithPage>();
            this.CurPoint = new Point(this.nGap, this.nGap);
            this.CurOrder = 0;
            this.CurPage = 1;
        }

        //영역가져오기
        public List<RectWithPage> getRectPosition(List<Rectangle> aRectList)
        {
            //초기화
            clearValues();
            //영역만들기 (갭만큼 합산하여 호출한다.)
            for (int i = 0; i < aRectList.Count; i++) {
                //
                Rectangle rt = aRectList[i];
                rt.Width = rt.Width + this.nGap;
                rt.Height = rt.Height + this.nGap;
                insertRectPage(rt);
            }
            //갭차감
            for (int i = 0; i < list_rects.Count; i++)
            {
                list_rects[i].rect.Width = list_rects[i].rect.Width - this.nGap;
                list_rects[i].rect.Height = list_rects[i].rect.Height - this.nGap;
            }
            //
            return this.list_rects;
        }

        //최초에 호출할경우 rt에 gap값을 추가한상태에서 호출!!
        private int insertRectPage(Rectangle rt)
        {
            //전체범위영역검사
            if ((rt.Width) > this.pageSize.Width) { return -1; }
            if ((rt.Height) > this.pageSize.Height) { return -1; }

            // y flow 이면 page를 갱신하고 위치를 초기화 하고 재호출
            if ((this.CurPoint.Y + rt.Height) > this.pageSize.Height)
            {
                this.CurPage = this.CurPage + 1;
                this.CurPoint.X = this.nGap;
                this.CurPoint.Y = this.nGap;
                return insertRectPage(rt);
            }
           
            // x flow 이면 현재페이지 blob에서 새로운 y포지션을 잡고 x는 갭초기화 하고 재호출
            if ((this.CurPoint.X + rt.Width) > this.pageSize.Width)
            {
                this.CurPoint.X = this.nGap;
                int groupMaxY = 0;
                for (int i = 0; i < list_rects.Count; i++)
                {
                    //자신의 페이지가 아닌것은 거르기
                    if(list_rects[i].nPage != this.CurPage){ continue; }
                    //최고 y2값 찾기
                    int now_x2 = list_rects[i].rect.Y + list_rects[i].rect.Height;
                    if (now_x2 > groupMaxY) { groupMaxY = now_x2; }
                }
                this.CurPoint.Y = groupMaxY;
                return insertRectPage(rt);
            }

            // 위에 조건들이 통과되면 현재 Index와 Rectangle을 부여하고 CurPos를 부여한 Rect에 x2,y1으로 변경           
            RectWithPage now_rpg = new RectWithPage();
            now_rpg.nPage = this.CurPage;
            now_rpg.nIndex = this.CurOrder;
            this.CurOrder++;
            now_rpg.rect.X = this.CurPoint.X;
            now_rpg.rect.Y = this.CurPoint.Y;
            now_rpg.rect.Width = rt.Width;   
            now_rpg.rect.Height = rt.Height; 
            this.list_rects.Add(now_rpg);
            //
            this.CurPoint.X = now_rpg.rect.X + now_rpg.rect.Width;
            this.CurPoint.Y = now_rpg.rect.Y;
            return 0;
        }
    }
}


댓글 없음:

댓글 쓰기