Ejemplo n.º 1
0
        private Zones.TPolygon[] CalcOutOfBoundsPoly(Zones.TPolygon Tile, Zones.TPolygon BoxRect, ClipperLib.ClipType clipType)
        {
            List <List <ClipperLib.IntPoint> > subj = new List <List <ClipperLib.IntPoint> >();

            subj.Add(new List <ClipperLib.IntPoint>());
            for (int i = 0; i < Tile.Dots.Length; i++)
            {
                subj[0].Add(new ClipperLib.IntPoint((long)Tile[i].X, (long)Tile[i].Y));
            }

            List <List <ClipperLib.IntPoint> > clip = new List <List <ClipperLib.IntPoint> >();

            clip.Add(new List <ClipperLib.IntPoint>(4));
            for (int i = 0; i < BoxRect.Dots.Length; i++)
            {
                clip[0].Add(new ClipperLib.IntPoint((long)BoxRect[i].X, (long)BoxRect[i].Y));
            }

            List <ClipperLib.ExPolygon> solution = new List <ClipperLib.ExPolygon>();

            ClipperLib.Clipper c = new ClipperLib.Clipper();
            c.AddPolygons(clip, ClipperLib.PolyType.ptSubject);
            c.AddPolygons(subj, ClipperLib.PolyType.ptClip);

            bool res = (c.Execute(clipType, solution)); // , ClipperLib.PolyFillType.pftEvenOdd, ClipperLib.PolyFillType.pftEvenOdd

            if (res)
            {
                if (solution.Count > 0)
                {
                    List <Zones.TPolygon> pols = new List <Zones.TPolygon>();
                    for (int s = 0; s < solution.Count; s++)
                    {
                        Zones.TPolygon poly = new Zones.TPolygon((ushort)solution[s].outer.Count);
                        for (int i = 0; i < poly.Dots.Length; i++)
                        {
                            poly[i] = new Zones.TPoint((double)solution[s].outer[i].X, (double)solution[s].outer[i].Y);
                        }
                        pols.Add(poly);
                    }
                    ;
                    return(pols.ToArray());
                }
            }
            ;

            return(null);
        }
Ejemplo n.º 2
0
        public void CalcTiles(Zones.TPolygon BoxRect, SizeF TileSize, double Gap)
        {
            Hashtable ht = new Hashtable();

            double tmpH = BoxRect.Height + TileSize.Height * 2;
            double tmpW = BoxRect.Width + TileSize.Width * 2;

            List <Zones.TPolygon> TotalTiles = new List <Zones.TPolygon>();

            if (Angle45.Checked)
            {
                double step_xy = (TileSize.Width + Gap) * Math.Sqrt(2);

                double y = 0;
                while (y < tmpH)
                {
                    double x = 0;
                    while (x < tmpW)
                    {
                        Zones.TPolygon pl;
                        pl = GetPlitka45(new PointF((float)x, (float)y), TileSize);
                        if (Zones.PolygonInPolygon(pl, BoxRect))
                        {
                            TotalTiles.Add(pl);
                        }
                        pl = GetPlitka45(new PointF(
                                             (float)(x - step_xy / 2.0),
                                             (float)(y + step_xy / 2.0)), TileSize);
                        if (Zones.PolygonInPolygon(pl, BoxRect))
                        {
                            TotalTiles.Add(pl);
                        }
                        x += step_xy;
                    }
                    ;
                    y += step_xy;
                }
                ;
            }
            ;

            if (Angle90.Checked)
            {
                double y = 0;// wallPlan.Gap;
                while (y < tmpH)
                {
                    double x = 0;//wallPlan.Gap;
                    while (x < tmpW)
                    {
                        Zones.TPolygon pl = GetPlitka90(new PointF((float)x, (float)y), TileSize);
                        if (Zones.PolygonInPolygon(pl, BoxRect))
                        {
                            TotalTiles.Add(pl);
                        }
                        x += TileSize.Width + Gap;
                    }
                    ;
                    y += TileSize.Height + Gap;
                }
                ;
            }
            ;

            Bitmap   b         = null;
            Graphics g         = null;
            int      scaleSize = 1;

            if (TileSize.Width >= 100)
            {
                if (gOneTwo.Checked)
                {
                    scaleSize = 2;
                }
                if (gOneThree.Checked)
                {
                    scaleSize = 3;
                }
                if (gOneFour.Checked)
                {
                    scaleSize = 4;
                }
                if (gOneFive.Checked)
                {
                    scaleSize = 5;
                }
                if (gOneTen.Checked)
                {
                    scaleSize = 10;
                }
            }
            ;
            if (makeImage.Checked)
            {
                b = new Bitmap((int)((BoxRect.Width + TileSize.Width * 2) / scaleSize), (int)((BoxRect.Height + TileSize.Height * 2) / scaleSize));
                g = Graphics.FromImage(b);

                Zones.TPolygon pBox = new Zones.TPolygon(new Zones.TPoint(0 - TileSize.Width, 0 - +TileSize.Height), new SizeF((float)(BoxRect.Width + TileSize.Width * 2), (float)(BoxRect.Height + TileSize.Height * 2)));
                g.FillRectangle(new SolidBrush(Color.FromArgb(50, Color.Silver)), new Rectangle(0, 0, b.Width, b.Height));
                PointF[] pts = new PointF[BoxRect.Dots.Length];
                for (int i = 0; i < pts.Length; i++)
                {
                    pts[i] = new PointF((float)((BoxRect.Dots[i].X + TileSize.Width) / scaleSize), (float)((BoxRect.Dots[i].Y + TileSize.Height) / scaleSize));
                }
                g.FillPolygon(new SolidBrush(Color.White), pts);
            }
            ;

            Area1Storage ac_black        = new Area1Storage();
            Area1Storage ac_red          = new Area1Storage();
            Area1Storage ac_green        = new Area1Storage();
            double       DefaultTileArea = (new Zones.TPolygon(new Zones.TPoint(0, 0), TileSize)).Area;
            int          tCo             = 0;

            foreach (Zones.TPolygon CurrentTile in TotalTiles)
            {
                TileIdentity tid = new TileIdentity(new Pen(new SolidBrush(Color.Gray)), ++tCo);
                CurrentTile.ID = tid;
                if (makeImage.Checked)
                {
                    CurrentTile.Draw(g, tid.pen, (int)TileSize.Width, (int)TileSize.Height, scaleSize);
                }
                Zones.TPolygon poly = CalcOutOfBoundsPoly(CurrentTile, BoxRect, ClipperLib.ClipType.ctIntersection)[0];
                if (poly != null)                             // выходит за границы
                {
                    if (poly.Area <= (DefaultTileArea / 2.0)) // меньше половины плитки
                    {
                        ac_green.Add(poly.Area, ((TileIdentity)CurrentTile.ID).ID);
                        tid.pen = new Pen(new SolidBrush(Color.Green));
                    }
                    else if (poly.Area >= (DefaultTileArea * 0.85)) // Плитка полностью входит площадь
                    {
                        ac_black.Add(DefaultTileArea, ((TileIdentity)CurrentTile.ID).ID);
                        tid.pen = new Pen(new SolidBrush(Color.Black));
                    }
                    else // больше половины плитки
                    {
                        ac_red.Add(poly.Area, ((TileIdentity)CurrentTile.ID).ID);
                        tid.pen = new Pen(new SolidBrush(Color.Red));
                    };
                }
                ;
            }
            ;

            LoadWallPlan();

            StatusList.Text += "\r\n";
            StatusList.Text += "Черных: " + ac_black.TilesCount.ToString() + "\r\n";
            foreach (double d in ac_black.Areas)
            {
                StatusList.Text += "   " + d.ToString() + ": " + ac_black.Get(d).Length.ToString() + "\r\n";
            }
            StatusList.Text += "Красных: " + ac_red.TilesCount.ToString() + "\r\n";
            foreach (double d in ac_red.Areas)
            {
                StatusList.Text += "   " + d.ToString() + ": " + ac_red.Get(d).Length.ToString() + "\r\n";
            }
            StatusList.Text += "Зеленых: " + ac_green.TilesCount.ToString() + "\r\n";
            foreach (double d in ac_green.Areas)
            {
                StatusList.Text += "   " + d.ToString() + ": " + ac_green.Get(d).Length.ToString() + "\r\n";
            }
            StatusList.Text += "------------\r\nВсего: " + TotalTiles.Count.ToString() + "\r\n\r\n";

            // CALC MERGE
            Area1Storage ac_all = new Area1Storage();

            foreach (double d in ac_red.Areas)
            {
                ac_all.Add(d, ac_red.Get(d));
            }
            foreach (double d in ac_green.Areas)
            {
                ac_all.Add(d, ac_green.Get(d));
            }

            Area2AreaStorage a2c = new Area2AreaStorage();

            while (ac_all.Areas.Length > 0)
            {
                double val1 = ac_all.Areas[0];
                double val2 = 0;

                int tile1 = ac_all.Get(val1)[0];
                int tile2 = 0;

                double max1 = val1 + val2;
                ac_all.Remove(val1, tile1);
                for (int i = 0; i < ac_all.Areas.Length; i++)
                {
                    double tmp_max = val1 + ac_all.Areas[i];
                    if ((tmp_max > max1) && (tmp_max < DefaultTileArea))
                    {
                        max1 = tmp_max;
                        val2 = ac_all.Areas[i];
                    }
                    ;
                }
                ;
                if (val2 > 0)
                {
                    tile2 = ac_all.Get(val2)[0];
                    ac_all.Remove(val2, tile2);
                }
                ;
                a2c.Add(val1, val2, tile1, tile2);
            }
            ;

            if (makeImage.Checked)
            {
                if (drawWalls.Checked)
                {
                    BoxRect.Draw(g, new Pen(new SolidBrush(Color.FromArgb(150, Color.Navy)), 1), (int)TileSize.Width, (int)TileSize.Height, scaleSize);
                }

                int btc = 0;
                foreach (double d in ac_black.Areas)
                {
                    foreach (int id in ac_black.Get(d))
                    {
                        foreach (Zones.TPolygon CurrentTile in TotalTiles)
                        {
                            if (((TileIdentity)CurrentTile.ID).ID == id)
                            {
                                CurrentTile.Title = (++btc).ToString();
                                if (tileEnum.Checked)
                                {
                                    CurrentTile.DrawTitle(g, ((TileIdentity)CurrentTile.ID).pen, (int)TileSize.Width, (int)TileSize.Height, scaleSize, wallPlan.Angle);
                                }
                            }
                        }
                    }
                }
                ;

                foreach (Area2AreaStorage.TileTwinStorage a12 in a2c.Areas)
                {
                    foreach (int[] ids in a12.TileTwins)
                    {
                        btc++;
                        foreach (Zones.TPolygon CurrentTile in TotalTiles)
                        {
                            if ((((TileIdentity)CurrentTile.ID).ID == ids[0]) || (((TileIdentity)CurrentTile.ID).ID == ids[1]))
                            {
                                CurrentTile.Title = btc.ToString();
                                CurrentTile.Fill(g, new Pen(new SolidBrush(Color.FromArgb(25, (((TileIdentity)CurrentTile.ID).pen.Color)))), (int)TileSize.Width, (int)TileSize.Height, scaleSize);
                                if (tileEnum.Checked)
                                {
                                    CurrentTile.DrawTitle(g, ((TileIdentity)CurrentTile.ID).pen, (int)TileSize.Width, (int)TileSize.Height, scaleSize, wallPlan.Angle);
                                }
                            }
                        }
                        ;
                    }
                }
                ;
                pictureBox1.Image = b;
            }
            ;

            int ttl = 0;

            foreach (Area2AreaStorage.TileTwinStorage a12 in a2c.Areas)
            {
                ttl += a12.TileTwins.Count;
            }
            StatusList.Text += "\r\n";
            StatusList.Text += "С компоновкой: " + ac_black.TilesCount.ToString() + " + " + ttl.ToString() + " = " + (ac_black.TilesCount + ttl).ToString() + "\r\n";
            foreach (double d in ac_black.Areas)
            {
                StatusList.Text += "   " + d.ToString() + ": " + ac_black.Get(d).Length.ToString() + "\r\n";
            }
            foreach (Area2AreaStorage.TileTwinStorage a12 in a2c.Areas)
            {
                StatusList.Text += "   " + a12.area1.ToString() + "+" + a12.area2.ToString() + ": " + a12.TileTwins.Count.ToString() + "\r\n";
            }

            // SQUARE
            StatusList.Text += "\r\n";
            StatusList.Text += "Площадь контура: " + BoxRect.Area.ToString() + " мм2\r\n";
            Zones.TPolygon tilp = new Zones.TPolygon(new Zones.TPoint(0, 0), new SizeF((float)(wallPlan.TileWidth + Gap), (float)(wallPlan.TileHeight + Gap)));
            StatusList.Text += "Площадь плитки: " + tilp.Area.ToString() + " мм2\r\n";
            int tilesSquare = (int)Math.Truncate(BoxRect.Area / tilp.Area + 1);

            StatusList.Text += "Плиток по площади: " + tilesSquare.ToString() + "\r\n";
        }