Ejemplo n.º 1
0
        public NFP ImportFromRawDetail(RawDetail raw, int src)
        {
            var d = raw.ToNfp();

            if (d == null)
            {
                return(null);
            }
            d.source = src;
            Polygons.Add(d);
            return(d);
        }
Ejemplo n.º 2
0
        public void LoadXml(string v)
        {
            var d   = XDocument.Load(v);
            var f   = d.Descendants().First();
            var gap = int.Parse(f.Attribute("gap").Value);

            SvgNest.Config.spacing = gap;

            foreach (var item in d.Descendants("sheet"))
            {
                int src = GetNextSheetSource();
                var cnt = int.Parse(item.Attribute("count").Value);
                var ww  = int.Parse(item.Attribute("width").Value);
                var hh  = int.Parse(item.Attribute("height").Value);

                for (int i = 0; i < cnt; i++)
                {
                    AddSheet(ww, hh, src);
                }
            }
            foreach (var item in d.Descendants("part"))
            {
                var       cnt  = int.Parse(item.Attribute("count").Value);
                var       path = item.Attribute("path").Value;
                RawDetail r    = null;
                if (path.ToLower().EndsWith("svg"))
                {
                    r = SvgParser.LoadSvg(path);
                }
                else if (path.ToLower().EndsWith("dxf"))
                {
                    r = DxfParser.LoadDxf(path);
                }
                else
                {
                    continue;
                }

                var src = GetNextSource();

                for (int i = 0; i < cnt; i++)
                {
                    ImportFromRawDetail(r, src);
                }
            }
        }
Ejemplo n.º 3
0
        public NFP ImportFromRawDetail(RawDetail raw, int src)
        {
            NFP        po   = null;
            List <NFP> nfps = new List <NFP>();

            foreach (var item in raw.Outers)
            {
                var nn = new NFP();
                nfps.Add(nn);
                foreach (var pitem in item.Points)
                {
                    nn.AddPoint(new SvgPoint(pitem.X, pitem.Y));
                }
            }

            if (nfps.Any())
            {
                var tt = nfps.OrderByDescending(z => z.Area).First();
                po      = tt;
                po.Name = raw.Name;

                foreach (var r in nfps)
                {
                    if (r == tt)
                    {
                        continue;
                    }
                    if (po.children == null)
                    {
                        po.children = new List <NFP>();
                    }
                    po.children.Add(r);
                }

                po.source = src;
                Polygons.Add(po);
            }
            return(po);
        }
Ejemplo n.º 4
0
        public static RawDetail LoadSvg(string path)
        {
            XDocument doc = XDocument.Load(path);
            var       fi  = new FileInfo(path);
            RawDetail s   = new RawDetail();

            s.Name = fi.Name;
            List <GraphicsPath> paths = new List <GraphicsPath>();
            var ns = doc.Descendants().First().Name.Namespace.NamespaceName;


            foreach (var item in doc.Descendants("path"))
            {
                var dd = (item.Attribute("d").Value);

                List <string> cmnds = new List <string>();
                StringBuilder sb    = new StringBuilder();
                for (int i = 0; i < dd.Length; i++)
                {
                    if (char.IsLetter(dd[i]))
                    {
                        if (sb.Length > 0)
                        {
                            cmnds.Add(sb.ToString());
                        }
                        sb = new StringBuilder();
                    }
                    sb.Append(dd[i]);
                }
                if (sb.Length > 0)
                {
                    cmnds.Add(sb.ToString());
                }
                //GraphicsPath p = new GraphicsPath();


                //polygons.Add(new SvgNestPort.Polygon() { orig = item,
                //    /*Points = p.PathPoints.Select(z => new SvgPoint(z.X, z.Y)).ToArray()*/ });
            }
            foreach (var item in doc.Descendants("rect"))
            {
                float xx = 0;
                float yy = 0;
                if (item.Attribute("x") != null)
                {
                    xx = float.Parse(item.Attribute("x").Value);
                }
                if (item.Attribute("y") != null)
                {
                    yy = float.Parse(item.Attribute("y").Value);
                }
                var          ww = float.Parse(item.Attribute("width").Value);
                var          hh = float.Parse(item.Attribute("height").Value);
                GraphicsPath p  = new GraphicsPath();
                p.AddRectangle(new RectangleF(xx, yy, ww, hh));
                s.Outers.Add(new LocalContour()
                {
                    Points = p.PathPoints.ToList()
                });
            }

            foreach (var item in doc.Descendants(XName.Get("polygon", ns)))
            {
                var           str    = item.Attribute("points").Value.ToString();
                var           spl    = str.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);
                List <PointF> points = new List <PointF>();
                foreach (var sitem in spl)
                {
                    var spl2 = sitem.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries).ToArray();
                    var ar   = spl2.Select(z => float.Parse(z, CultureInfo.InvariantCulture)).ToArray();
                    points.Add(new PointF(ar[0], ar[1]));
                }
                s.Outers.Add(new LocalContour()
                {
                    Points = points.ToList()
                });
            }


            return(s);
        }
Ejemplo n.º 5
0
        public static RawDetail LoadDxf(string path)
        {
            FileInfo  fi      = new FileInfo(path);
            DxfFile   dxffile = DxfFile.Load(fi.FullName);
            RawDetail s       = new RawDetail();

            s.Name = fi.FullName;
            IEnumerable <DxfEntity> entities = dxffile.Entities.ToArray();

            List <LineElement> elems = new List <LineElement>();

            foreach (DxfEntity ent in entities)
            {
                switch (ent.EntityType)
                {
                case DxfEntityType.LwPolyline:
                {
                    DxfLwPolyline poly = (DxfLwPolyline)ent;
                    if (poly.Vertices.Count() < 2)
                    {
                        continue;
                    }
                    LocalContour points = new LocalContour();
                    foreach (DxfLwPolylineVertex vert in poly.Vertices)
                    {
                        points.Points.Add(new PointF((float)vert.X, (float)vert.Y));
                    }
                    for (int i = 0; i < points.Points.Count; i++)
                    {
                        var p0 = points.Points[i];
                        var p1 = points.Points[(i + 1) % points.Points.Count];
                        elems.Add(new LineElement()
                            {
                                Start = p0, End = p1
                            });
                    }
                }
                break;

                case DxfEntityType.Arc:
                {
                    DxfArc        arc = (DxfArc)ent;
                    List <PointF> pp  = new List <PointF>();

                    if (arc.StartAngle > arc.EndAngle)
                    {
                        arc.StartAngle -= 360;
                    }

                    for (double i = arc.StartAngle; i < arc.EndAngle; i += 15)
                    {
                        var tt = arc.GetPointFromAngle(i);
                        pp.Add(new PointF((float)tt.X, (float)tt.Y));
                    }
                    var t = arc.GetPointFromAngle(arc.EndAngle);
                    pp.Add(new PointF((float)t.X, (float)t.Y));
                    for (int j = 1; j < pp.Count; j++)
                    {
                        var p1 = pp[j - 1];
                        var p2 = pp[j];
                        elems.Add(new LineElement()
                            {
                                Start = new PointF((float)p1.X, (float)p1.Y), End = new PointF((float)p2.X, (float)p2.Y)
                            });
                    }
                }
                break;

                case DxfEntityType.Circle:
                {
                    DxfCircle    cr = (DxfCircle)ent;
                    LocalContour cc = new LocalContour();

                    for (int i = 0; i <= 360; i += 15)
                    {
                        var ang = i * Math.PI / 180f;
                        var xx  = cr.Center.X + cr.Radius * Math.Cos(ang);
                        var yy  = cr.Center.Y + cr.Radius * Math.Sin(ang);
                        cc.Points.Add(new PointF((float)xx, (float)yy));
                    }
                    for (int i = 1; i < cc.Points.Count; i++)
                    {
                        var p1 = cc.Points[i - 1];
                        var p2 = cc.Points[i];
                        elems.Add(new LineElement()
                            {
                                Start = p1, End = p2
                            });
                    }
                }
                break;

                case DxfEntityType.Line:
                {
                    DxfLine poly = (DxfLine)ent;
                    elems.Add(new LineElement()
                        {
                            Start = new PointF((float)poly.P1.X, (float)poly.P1.Y), End = new PointF((float)poly.P2.X, (float)poly.P2.Y)
                        });
                    break;
                }

                case DxfEntityType.Polyline:
                {
                    DxfPolyline poly = (DxfPolyline)ent;
                    if (poly.Vertices.Count() < 2)
                    {
                        continue;
                    }
                    LocalContour points = new LocalContour();
                    for (int i = 0; i < poly.Vertices.Count; i++)
                    {
                        DxfVertex vert = poly.Vertices[i];
                        points.Points.Add(new PointF((float)vert.Location.X, (float)vert.Location.Y));
                    }
                    for (int i = 0; i < points.Points.Count; i++)
                    {
                        var p0 = points.Points[i];
                        var p1 = points.Points[(i + 1) % points.Points.Count];
                        elems.Add(new LineElement()
                            {
                                Start = p0, End = p1
                            });
                    }
                    break;
                }

                default:
                    throw new ArgumentException("unsupported entity type: " + ent);
                }
                ;
            }


            elems = elems.Where(z => z.Start.DistTo(z.End) > RemoveThreshold).ToList();
            var cntrs2 = ConnectElements(elems.ToArray());

            s.Outers.AddRange(cntrs2);
            if (s.Outers.Any(z => z.Points.Count < 3))
            {
                throw new Exception("few points");
            }

            return(s);
        }
Ejemplo n.º 6
0
        public static RawDetail loadDxf(string path)
        {
            FileInfo  fi      = new FileInfo(path);
            DxfFile   dxffile = DxfFile.Load(fi.FullName);
            RawDetail s       = new RawDetail();

            //used to replace the dxf on a nested sheet
            s.Name = fi.FullName;

            //for now only store used types less for each iterations required
            IEnumerable <DxfEntity> entities = dxffile.Entities.Where(ent => ent.EntityType == DxfEntityType.Polyline || ent.EntityType == DxfEntityType.LwPolyline);

            foreach (DxfEntity ent in entities)
            {
                LocalContour points = new LocalContour();

                switch (ent.EntityType)
                {
                case DxfEntityType.LwPolyline:
                {
                    DxfLwPolyline poly = (DxfLwPolyline)ent;
                    if (poly.Vertices.Count() < 2)
                    {
                        continue;
                    }
                    foreach (DxfLwPolylineVertex vert in poly.Vertices)
                    {
                        points.Points.Add(new PointF((float)vert.X, (float)vert.Y));
                    }
                    break;
                }

                case DxfEntityType.Polyline:
                {
                    DxfPolyline poly = (DxfPolyline)ent;
                    if (poly.Vertices.Count() < 2)
                    {
                        continue;
                    }

                    foreach (DxfVertex vert in poly.Vertices)
                    {
                        points.Points.Add(new PointF((float)vert.Location.X, (float)vert.Location.Y));
                    }

                    break;
                }
                }
                ;


                if (points.Points.Count() < 3)
                {
                    continue;
                }
                s.Outers.Add(points);
            }


            return(s);
        }