public void ConstructPolyLines()
        {
            try
            {
                while (segments.Count != 0)
                {
                    if (!segments[0].isValid)
                    {
                        segments.RemoveAt(0);
                        continue;
                    }
                    Polyline2D ply = new Polyline2D();
                    ply.Add(segments[0].p1);
                    Point2D curpt = segments[0].p2;
                    ply.Add(curpt);
                    polys.Add(ply);
                    segments.RemoveAt(0);
                    bool segmentFound = true;
                    while (segmentFound)
                    {
                        int i = 0;
                        segmentFound = false;
                        while (i < segments.Count)
                        {
                            Segment2D seg = segments[i];

                            if (seg.isValid && Object.ReferenceEquals(seg.p1, curpt))
                            {
                                curpt = seg.p2;
                                if (Object.ReferenceEquals(curpt, ply.points[0]))
                                {
                                    ply.isClosed = true;
                                    segments.RemoveAt(i);
                                    segmentFound = false;
                                    break;
                                }
                                ply.Add(curpt);
                                segments.RemoveAt(i);
                                segmentFound = true;
                            }
                            else if (seg.isValid && Object.ReferenceEquals(seg.p2, ply.points[0]))
                            {
                                ply.points.Insert(0, seg.p1);
                                segments.RemoveAt(i);
                                segmentFound = true;
                            }
                            else
                            {
                                i++;
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                DebugLogger.Instance().LogError(ex);
            }
        }
        public void ConstructPolyLines()
        {
            try
            {
                while (segments.Count != 0)
                {
                    if (!segments[0].isValid)
                    {
                        segments.RemoveAt(0);
                        continue;
                    }
                    Polyline2D ply = new Polyline2D();
                    ply.Add(segments[0].p1);
                    Point2D curpt = segments[0].p2;
                    ply.Add(curpt);
                    polys.Add(ply);
                    segments.RemoveAt(0);
                    bool segmentFound = true;
                    while (segmentFound)
                    {
                        int i = 0;
                        segmentFound = false;
                        while (i < segments.Count)
                        {
                            Segment2D seg = segments[i];

                            if (seg.isValid && Object.ReferenceEquals(seg.p1, curpt))
                            {
                                curpt = seg.p2;
                                if (Object.ReferenceEquals(curpt, ply.points[0]))
                                {
                                    ply.isClosed = true;
                                    segments.RemoveAt(i);
                                    segmentFound = false;
                                    break;
                                }
                                ply.Add(curpt);
                                segments.RemoveAt(i);
                                segmentFound = true;
                            }
                            else if (seg.isValid && Object.ReferenceEquals(seg.p2, ply.points[0]))
                            {
                                ply.points.Insert(0, seg.p1);
                                segments.RemoveAt(i);
                                segmentFound = true;
                            }
                            else
                                i++;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                DebugLogger.Instance().LogError(ex);
            }
        }
        public StreamWriter GenerateSVG(double width, double height, bool isFillPoly)
        {
            MemoryStream ms = new MemoryStream();
            StreamWriter sw = new StreamWriter(ms);

            //double width = UVDLPApp.Instance().m_printerinfo.m_PlatXSize;
            //double height = UVDLPApp.Instance().m_printerinfo.m_PlatYSize;
            sw.WriteLine("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>");
            sw.WriteLine("<!-- Created with CreationWorkshop (http://www.envisionlabs.net/) -->");
            sw.WriteLine();
            sw.WriteLine("<svg width=\"{0}mm\" height=\"{1}mm\" viewBox=\"{2} {3} {0} {1}\" version=\"1.1\" xmlns=\"http://www.w3.org/2000/svg\">", width, height, -width / 2, -height / 2);
            if (isFillPoly)
            {
                // sort polygons into display layers
                //int[] dispLevel = new int[lstPoly.Count];
                int i, j, k;
                int maxLevel = 0;
                for (i = 0; i < polys.Count; i++)
                {
                    if (!polys[i].isClosed)
                    {
                        continue;
                    }
                    polys[i].level = 0;
                    for (j = 0; j < polys.Count; j++)
                    {
                        if ((j == i) || !polys[j].isClosed)
                        {
                            continue;
                        }
                        if (polys[j].PointInPoly(polys[i].points[0]))
                        {
                            polys[i].level++;
                        }
                    }
                    if (polys[i].level > maxLevel)
                    {
                        maxLevel = polys[i].level;
                    }
                }

                // draw polygons layer by layer
                for (k = 0; k <= maxLevel; k++)
                {
                    for (j = 0; j < polys.Count; j++)
                    {
                        if (polys[j].level != k)
                        {
                            continue;
                        }
                        Polyline2D pl   = polys[j];
                        int        plen = pl.nPoints;

                        // determine polygon direction
                        double dir = 0;
                        for (i = 1; i < plen; i++)
                        {
                            dir += (pl.points[i].x - pl.points[i - 1].x) * (pl.points[i].y + pl.points[i - 1].y);
                        }
                        dir += (pl.points[0].x - pl.points[plen - 1].x) * (pl.points[0].y + pl.points[plen - 1].y);

                        // draw polygon
                        sw.Write("<polygon points=\"");
                        for (i = 0; i < plen; i++)
                        {
                            sw.Write("{0},{1}", pl.points[i].x, -pl.points[i].y);
                            if (i < (plen - 1))
                            {
                                sw.Write(" ");
                            }
                        }
                        sw.WriteLine("\" style=\"fill:{0}\" />", dir < 0 ? "black" : "white");
                        // - for some resaon it seems
                        //     that polygon direction does not work properly so i use layer level instead.
                        //sw.WriteLine("\" style=\"fill:{0}\" />", (k & 1) == 1 ? "black" : "white");
                    }
                }
            }
            else
            {
                sw.Write("<path d=\"");
                foreach (Polyline2D pl in polys)
                {
                    int plen = pl.nPoints;
                    for (int i = 0; i < plen; i++)
                    {
                        if (i == 0)
                        {
                            sw.Write("M{0} {1} ", pl.points[i].x, -pl.points[i].y);
                        }
                        else
                        {
                            sw.Write("L{0} {1} ", pl.points[i].x, -pl.points[i].y);
                        }
                    }
                    sw.WriteLine("Z ");
                }
                sw.WriteLine("\" />");
            }

            //<path d="M 15 2 L9.5 18.0 L25.5 22.0 Z M 15.0 0 L7.5 20.0 L22.5 20.0 Z" fill-rule="evenodd"/>
            sw.WriteLine("</svg>");
            sw.Flush();
            sw.BaseStream.Seek(0, SeekOrigin.Begin);

            return(sw);
        }