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); }
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); }