Exemple #1
0
        void ProcessDxfPolyLine(DxfPolyline dxfPolyLine)
        {
            if (!dxfPolyLine.ContainsVertices)
            {
                return;
            }

            int      iVertexCount = 0;
            Vector2D startPoint   = new Vector2D();

            foreach (var vertex in dxfPolyLine.Vertices)
            {
                Vector2D endPoint = new Vector2D(vertex.Location.X, vertex.Location.Y);
                if (iVertexCount > 0)
                {
                    PicSegment picSegment = Factory.AddSegment(
                        DxfColor2PicLT(dxfPolyLine), 0, 0
                        , startPoint
                        , endPoint);
                }

                startPoint = endPoint;
                ++iVertexCount;
            }
        }
Exemple #2
0
        private static void GenerateSheetOutline(IEnumerable <NFP> sheets, int i, out DxfFile sheetdxf, out double sheetwidth)
        {
            // Generate Sheet Outline in Dxf
            sheetdxf = new DxfFile();
            sheetdxf.Views.Clear();

            List <DxfVertex> sheetverts  = new List <DxfVertex>();
            double           sheetheight = sheets.ElementAt(i).HeightCalculated;

            sheetwidth = sheets.ElementAt(i).WidthCalculated;

            // Bl Point
            sheetverts.Add(new DxfVertex(new DxfPoint(0, 0, 0)));

            // BR Point
            sheetverts.Add(new DxfVertex(new DxfPoint(sheetwidth, 0, 0)));

            // TR Point
            sheetverts.Add(new DxfVertex(new DxfPoint(sheetwidth, sheetheight, 0)));

            // TL Point
            sheetverts.Add(new DxfVertex(new DxfPoint(0, sheetheight, 0)));

            DxfPolyline sheetentity = new DxfPolyline(sheetverts)
            {
                IsClosed = true,
                Layer    = $"Plate H{sheetheight} W{sheetwidth}",
            };

            sheetdxf.Entities.Add(sheetentity);
        }
Exemple #3
0
        public void ClosedPolyLineVertexBulgeIsRespected()
        {
            // Data from https://github.com/ixmilia/dxf/issues/102
            var vertices = new[]
            {
                new DxfVertex(new DxfPoint(0, +0.5, 0)),
                new DxfVertex(new DxfPoint(-1.5, +0.5, 0))
                {
                    Bulge = 1
                },
                new DxfVertex(new DxfPoint(-1.5, -0.5, 0)),
                new DxfVertex(new DxfPoint(0, -0.5, 0))
                {
                    Bulge = 1
                }
            };
            var poly = new DxfPolyline(vertices)
            {
                IsClosed = true
            };
            var expectedMin  = new DxfPoint(-2.0, -0.5, 0);
            var expectedMax  = new DxfPoint(0.5, +0.5, 0);
            var expectedSize = new DxfVector(2.5, 1.0, 0);

            var bb = poly.GetBoundingBox().Value;

            Assert.Equal(expectedMin, bb.MinimumPoint);
            Assert.Equal(expectedMax, bb.MaximumPoint);
            Assert.Equal(expectedSize, bb.Size);
        }
Exemple #4
0
        public void RenderEntity(DxfPolyline dxfPolyline, Graphics graphics, int height)
        {
            var dxfPolylineVertices = dxfPolyline.Vertices;
            var points = dxfPolylineVertices
                         .Select(t => new PointF((float)t.Location.X * ScaleFactor + OffsetX,
                                                 height - (float)t.Location.Y * ScaleFactor + OffsetY))
                         .ToArray();

            graphics.DrawLines(Pens.Black, points);
        }
        public static XElement ToXElement(this DxfPolyline polyline)
        {
            var path = polyline.GetSvgPath();

            return(new XElement(DxfToSvgConverter.Xmlns + "path",
                                new XAttribute("d", path.ToString()),
                                new XAttribute("fill-opacity", 0))
                   .AddStroke(polyline.Color)
                   .AddStrokeWidth(1.0)
                   .AddVectorEffect());
        }
Exemple #6
0
        public static DxfPolyline ToDxfPolyline(this Polyline poly, Layer layer)
        {
            var dp = new DxfPolyline(poly.Vertices.Select(v => v.ToDxfVertex()))
            {
                Elevation = poly.Vertices.Any() ? poly.Vertices.First().Location.Z : 0.0,
                Layer     = layer.Name,
                Normal    = DxfVector.ZAxis
            };

            return(dp);
        }
Exemple #7
0
        private void WritePolyline(DxfPolyline polyline)
        {
            WriteItemType(DxbItemType.Polyline);
            WriteW((short)(polyline.IsClosed ? 1 : 0));
            foreach (var vertex in polyline.Vertices)
            {
                WriteVertex(vertex);
            }

            WriteSeqend();
        }
Exemple #8
0
        public static Polyline ToPolyline(this DxfPolyline poly)
        {
            var vertices = poly.Vertices.Select(v => v.ToVertex()).ToList();

            if (poly.IsClosed && vertices.Count > 0)
            {
                vertices.Add(vertices[0]);
            }

            return(new Polyline(vertices, poly.GetEntityColor(), poly));
        }
Exemple #9
0
        /// <summary>
        /// Convert a polyline to a DXF Polyline entity.
        /// </summary>
        public static DxfPolyline ToDxf(this Polyline polyline)
        {
            if (polyline == null || polyline.Vertices == null || polyline.Vertices.Count < 2)
            {
                return(null);
            }
            var vertices = polyline.Vertices.Select(v => v.ToDxfVertex());
            var dxf      = new DxfPolyline(vertices);

            dxf.IsClosed = polyline is Polygon;
            return(dxf);
        }
Exemple #10
0
        public void EnforceMinimumVertexCountOnPolylineTest()
        {
            // need at least 2 vertices
            Assert.Throws <InvalidOperationException>(() => new DxfPolyline(new[] { new DxfVertex() }));

            var poly = new DxfPolyline(new[] { new DxfVertex(), new DxfVertex() }); // but this should be good

            Assert.Equal(2, poly.Vertices.Count);
            poly.Vertices.Add(new DxfVertex());
            poly.Vertices.RemoveAt(0);                                                  // should be allowed because there will be 2 left
            Assert.Throws <InvalidOperationException>(() => poly.Vertices.RemoveAt(0)); // throws because there is only 1 left
        }
Exemple #11
0
        internal static SvgPath GetSvgPath(this DxfPolyline poly)
        {
            var first    = poly.Vertices.First();
            var segments = new List <SvgPathSegment>();

            segments.Add(new SvgMoveToPath(first.Location.X, first.Location.Y));
            var last = first;

            foreach (var next in poly.Vertices.Skip(1))
            {
                segments.Add(FromPolylineVertices(last, next));
                last = next;
            }

            if (poly.IsClosed)
            {
                segments.Add(FromPolylineVertices(last, first));
            }

            return(new SvgPath(segments));
        }
Exemple #12
0
        public string EntityToJson(DxfPolyline dxfPolyline)
        {
            var dxfPolylineVertices = dxfPolyline.Vertices;

            var json = "";

            using (var enumerator = dxfPolylineVertices.GetEnumerator())
            {
                if (!enumerator.MoveNext())
                {
                    return("{}");
                }

                var last = enumerator.Current;

                enumerator.MoveNext();
                while (true)
                {
                    var current = enumerator.Current;

                    json += string.Format(Format, "[\"line\",{0:F},{1:F},{2:F},{3:F}]",
                                          last.Location.X,
                                          last.Location.Y,
                                          current.Location.X,
                                          current.Location.Y);

                    last = current;
                    if (enumerator.MoveNext())
                    {
                        json += ", ";
                    }
                    else
                    {
                        break;
                    }
                }
            }

            return(json);
        }
Exemple #13
0
        public float GetEntityLength(DxfPolyline dxfPolyline)
        {
            var dxfPolylineVertices = dxfPolyline.Vertices;

            var sumDistance = 0f;

            using (var enumerator = dxfPolylineVertices.GetEnumerator())
            {
                if (!enumerator.MoveNext())
                {
                    return(sumDistance);
                }

                var last = enumerator.Current;
                while (enumerator.MoveNext())
                {
                    var current = enumerator.Current;
                    sumDistance += Distance(current, last);
                    last         = current;
                }
            }

            return(sumDistance);
        }
 internal static SvgPath GetSvgPath(this DxfPolyline poly)
 {
     return(new SvgPath(GetSvgSegmentsFromVertices(poly.Vertices, poly.IsClosed)));
 }
Exemple #15
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);
        }
Exemple #16
0
        public void Write3DPolylineTest()
        {
            var file = new DxfFile();

            file.Header.Version = DxfAcadVersion.R2000; // owner handles only present on R2000+
            var poly = new DxfPolyline();

            poly.Vertices.Add(new DxfVertex());
            poly.Vertices.Add(new DxfVertex());
            poly.Is3DPolyline = true;
            file.Entities.Add(poly);
            VerifyFileContains(file, @"
  0
SECTION
  2
ENTITIES
  0
POLYLINE
  5
#
100
AcDbEntity
  8
0
370
0
100
AcDb3dPolyline
 10
0.0
 20
0.0
 30
0.0
 70
8
  0
VERTEX
  5
#
100
AcDbEntity
  8
0
370
0
100
AcDbVertex
100
AcDb3dPolylineVertex
 10
0.0
 20
0.0
 30
0.0
 70
32
 50
0.0
  0
VERTEX
  5
#
100
AcDbEntity
  8
0
370
0
100
AcDbVertex
100
AcDb3dPolylineVertex
 10
0.0
 20
0.0
 30
0.0
 70
32
 50
0.0
  0
SEQEND
  5
#
100
AcDbEntity
  8
0
370
0
  0
ENDSEC
");
        }
Exemple #17
0
        public static int Export(string path, IEnumerable <NFP> polygons, IEnumerable <NFP> sheets)
        {
            Dictionary <DxfFile, int> dxfexports = new Dictionary <DxfFile, int>();

            for (int i = 0; i < sheets.Count(); i++)
            {
                //Generate Sheet Outline in Dxf

                DxfFile sheetdxf = new DxfFile();
                sheetdxf.Views.Clear();

                List <DxfVertex> sheetverts  = new List <DxfVertex>();
                double           sheetheight = sheets.ElementAt(i).HeightCalculated;
                double           sheetwidth  = sheets.ElementAt(i).WidthCalculated;

                //Bl Point
                sheetverts.Add(new DxfVertex(new DxfPoint(0, 0, 0)));
                //BR Point
                sheetverts.Add(new DxfVertex(new DxfPoint(sheetwidth, 0, 0)));
                //TR Point
                sheetverts.Add(new DxfVertex(new DxfPoint(sheetwidth, sheetheight, 0)));
                //TL Point
                sheetverts.Add(new DxfVertex(new DxfPoint(0, sheetheight, 0)));


                DxfPolyline sheetentity = new DxfPolyline(sheetverts)
                {
                    IsClosed = true,
                    Layer    = $"Plate H{sheetheight} W{sheetwidth}",
                };


                sheetdxf.Entities.Add(sheetentity);

                foreach (NFP nFP in polygons)
                {
                    DxfFile fl;
                    if (nFP.fitted == false || !nFP.Name.ToLower().Contains(".dxf") || nFP.sheet.id != sheets.ElementAt(i).id)
                    {
                        continue;
                    }
                    else
                    {
                        fl = DxfFile.Load(nFP.Name);
                    }

                    double sheetXoffset = -sheetwidth * i;
                    //double sheetyoffset = -sheetheight * i;
                    DxfPoint         offsetdistance = new DxfPoint(nFP.x + sheetXoffset, nFP.y, 0D);
                    List <DxfEntity> newlist        = OffsetToNest(fl.Entities, offsetdistance, nFP.Rotation);

                    foreach (DxfEntity ent in newlist)
                    {
                        sheetdxf.Entities.Add(ent);
                    }
                }


                dxfexports.Add(sheetdxf, sheets.ElementAt(i).id);
            }
            int sheetcount = 0;

            for (int i = 0; i < dxfexports.Count(); i++)
            {
                var dxf = dxfexports.ElementAt(i).Key;
                var id  = dxfexports.ElementAt(i).Value;

                if (dxf.Entities.Count != 1)
                {
                    sheetcount += 1;
                    dxf.Save($"c:\\test\\{id}.dxf", true);
                }
            }
            return(sheetcount);
        }
    /// <summary>
    /// Parse DXF into VectorShape list.
    /// </summary>
    public static List <VectorShape> ReadDXF(Stream dxfStream)
    {
        List <VectorShape> shapes = new List <VectorShape>();

        DxfFile dxfFile = DxfFile.Load(dxfStream);

        Dictionary <string, Color32> layerColors = new Dictionary <string, Color32>();

        foreach (DxfLayer layer in dxfFile.Layers)
        {
            layerColors.Add(layer.Name, ConvertColor(layer.Color));
        }

        foreach (DxfEntity entity in dxfFile.Entities)
        {
            VectorShape shape = null;

            switch (entity.EntityType)
            {
            case DxfEntityType.Point:
                DxfModelPoint point = entity as DxfModelPoint;
                shape = new PointShape(ConvertPoint(point.Location));
                break;

            case DxfEntityType.Line:
                DxfLine   line      = entity as DxfLine;
                Vector2[] endpoints = new Vector2[2];
                endpoints[0] = ConvertPoint(line.P1);
                endpoints[1] = ConvertPoint(line.P2);
                shape        = new PolyShape(endpoints);
                break;

            case DxfEntityType.Spline:
                DxfSpline spline = entity as DxfSpline;
                if ((spline.NumberOfControlPoints % spline.DegreeOfCurve) != 1)
                {
                    Debug.LogError("Invalid spline data! Wrong number of points. " + spline);
                    break;
                }

                Vector2[] controlPoints = new Vector2[spline.NumberOfControlPoints];
                for (int i = 0; i < controlPoints.Length; i++)
                {
                    controlPoints[i] = ConvertPoint(spline.ControlPoints[i]);
                }
                shape = new PolyShape(controlPoints[0]);
                PolyShape shapeSpline = shape as PolyShape;

                switch (spline.DegreeOfCurve)
                {
                case 1:

                    for (int i = 1; i < controlPoints.Length; i++)
                    {
                        shapeSpline.LineTo(controlPoints[i]);
                    }
                    break;

                case 2:
                    for (int i = 1; i < controlPoints.Length; i += 2)
                    {
                        shapeSpline.CurveTo(controlPoints[i + 1], controlPoints[i]);
                    }
                    break;

                case 3:
                    for (int i = 1; i < controlPoints.Length; i += 3)
                    {
                        shapeSpline.CurveTo(controlPoints[i + 2], controlPoints[i], controlPoints[i + 1]);
                    }
                    break;

                default:
                    Debug.LogWarning("Spline with unsupported curve of degree: " + spline.DegreeOfCurve);
                    break;
                }
                break;

            case DxfEntityType.Arc:
                DxfArc arc = entity as DxfArc;
                // If the arc is a complete circle just make one of those
                float startAngle = (float)arc.StartAngle;
                while (startAngle < 0f)
                {
                    startAngle += 360f;
                }
                float endAngle = (float)arc.EndAngle;
                while (endAngle < startAngle)
                {
                    endAngle += 360f;
                }

                float sweep = endAngle - startAngle;
                shape = new CircleShape(ConvertPoint(arc.Center), (float)arc.Radius, startAngle, sweep);
                break;

            case DxfEntityType.Circle:
                DxfCircle circle = entity as DxfCircle;
                shape = new CircleShape(ConvertPoint(circle.Center), (float)circle.Radius * dxfScale);
                break;

            case DxfEntityType.Ellipse:
                DxfEllipse ellipse = entity as DxfEllipse;
                // If the ellipse is actually a circle just make one of those
                if (Mathf.Approximately((float)ellipse.MinorAxisRatio, 1f))
                {
                    shape = new CircleShape(ConvertPoint(ellipse.Center), (float)ellipse.MajorAxis.Length * dxfScale);
                }
                else
                {
                    shape = new EllipseShape(ConvertPoint(ellipse.Center), ConvertVector(ellipse.MajorAxis), (float)ellipse.MinorAxisRatio);
                }
                break;

            case DxfEntityType.Polyline:
                DxfPolyline polyline = entity as DxfPolyline;
                if (polyline.ContainsVertices)
                {
                    Vector2[] vertices = new Vector2[polyline.Vertices.Count];
                    for (int i = 0; i < vertices.Length; i++)
                    {
                        vertices[i] = ConvertPoint(polyline.Vertices[i].Location);
                    }

                    shape = new PolyShape(vertices[0]);
                    PolyShape shapePolyline = shape as PolyShape;

                    for (int i = 1; i < vertices.Length; i++)
                    {
                        float bulge = (float)polyline.Vertices[i - 1].Bulge;
                        shapePolyline.ArcToDXF(vertices[i], bulge);
                    }

                    if (polyline.IsClosed)
                    {
                        float bulge = (float)polyline.Vertices[vertices.Length - 1].Bulge;
                        shapePolyline.ArcToDXF(vertices[0], bulge);
                        shape.Closed = true;
                    }
                }
                break;

            case DxfEntityType.LwPolyline:
            {
                DxfLwPolyline lwPolyline = entity as DxfLwPolyline;
                Vector2[]     vertices   = new Vector2[lwPolyline.Vertices.Count];
                for (int i = 0; i < vertices.Length; i++)
                {
                    DxfLwPolylineVertex lwpVertex = lwPolyline.Vertices[i];
                    vertices[i] = ConvertPoint(lwpVertex.X, lwpVertex.Y);
                }

                shape = new PolyShape(vertices[0]);
                PolyShape shapePolyline = shape as PolyShape;

                for (int i = 1; i < vertices.Length; i++)
                {
                    float bulge = (float)lwPolyline.Vertices[i - 1].Bulge;
                    shapePolyline.ArcToDXF(vertices[i], bulge);
                }

                if (lwPolyline.IsClosed)
                {
                    float bulge = (float)lwPolyline.Vertices[vertices.Length - 1].Bulge;
                    shapePolyline.ArcToDXF(vertices[0], bulge);
                    shape.Closed = true;
                }
            }
            break;

            default:
                Debug.Log("Unhandled entity of type: " + entity.EntityType);
                break;
            }

            if (shape != null)
            {
                if (entity.IsVisible)
                {
                    Color32 shapeColor = ConvertColor(entity.Color);
                    //layerColors.TryGetValue(entity.Layer, out shapeColor);

                    shape.colorOutline = shapeColor;
                    shapes.Add(shape);
                }
            }
        }

        return(shapes);
    }
Exemple #19
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);
        }
Exemple #20
0
        private static List <DxfEntity> OffsetToNest(IList <DxfEntity> dxfEntities, DxfPoint pivot, DxfPoint offset, double rotationAngle)
        {
            List <DxfEntity> dxfreturn = new List <DxfEntity>();
            List <DxfPoint>  tmpPts;

            foreach (DxfEntity entity in dxfEntities)
            {
                switch (entity.EntityType)
                {
                case DxfEntityType.Arc:
                    DxfArc dxfArc = (DxfArc)entity;
                    dxfArc.Center      = RotateLocation(rotationAngle, dxfArc.Center);
                    dxfArc.Center     += offset;
                    dxfArc.StartAngle += rotationAngle;
                    dxfArc.EndAngle   += rotationAngle;
                    dxfreturn.Add(dxfArc);
                    break;

                case DxfEntityType.ArcAlignedText:
                    DxfArcAlignedText dxfArcAligned = (DxfArcAlignedText)entity;
                    dxfArcAligned.CenterPoint  = RotateLocation(rotationAngle, dxfArcAligned.CenterPoint);
                    dxfArcAligned.CenterPoint += offset;
                    dxfArcAligned.StartAngle  += rotationAngle;
                    dxfArcAligned.EndAngle    += rotationAngle;
                    dxfreturn.Add(dxfArcAligned);
                    break;

                case DxfEntityType.Attribute:
                    DxfAttribute dxfAttribute = (DxfAttribute)entity;
                    dxfAttribute.Location  = RotateLocation(rotationAngle, dxfAttribute.Location);
                    dxfAttribute.Location += offset;
                    dxfreturn.Add(dxfAttribute);
                    break;

                case DxfEntityType.AttributeDefinition:
                    DxfAttributeDefinition dxfAttributecommon = (DxfAttributeDefinition)entity;
                    dxfAttributecommon.Location  = RotateLocation(rotationAngle, dxfAttributecommon.Location);
                    dxfAttributecommon.Location += offset;
                    dxfreturn.Add(dxfAttributecommon);
                    break;

                case DxfEntityType.Circle:
                    DxfCircle dxfCircle = (DxfCircle)entity;
                    dxfCircle.Center  = RotateLocation(rotationAngle, dxfCircle.Center);
                    dxfCircle.Center += offset;
                    dxfreturn.Add(dxfCircle);
                    break;

                case DxfEntityType.Ellipse:
                    DxfEllipse dxfEllipse = (DxfEllipse)entity;
                    dxfEllipse.Center  = RotateLocation(rotationAngle, dxfEllipse.Center);
                    dxfEllipse.Center += offset;
                    dxfreturn.Add(dxfEllipse);
                    break;

                case DxfEntityType.Image:
                    DxfImage dxfImage = (DxfImage)entity;
                    dxfImage.Location  = RotateLocation(rotationAngle, dxfImage.Location);
                    dxfImage.Location += offset;

                    dxfreturn.Add(dxfImage);
                    break;

                case DxfEntityType.Leader:
                    DxfLeader dxfLeader = (DxfLeader)entity;
                    tmpPts = new List <DxfPoint>();

                    foreach (DxfPoint vrt in dxfLeader.Vertices)
                    {
                        var tmppnt = RotateLocation(rotationAngle, vrt);
                        tmppnt += offset;
                        tmpPts.Add(tmppnt);
                    }

                    dxfLeader.Vertices.Clear();
                    dxfLeader.Vertices.Concat(tmpPts);
                    dxfreturn.Add(dxfLeader);
                    break;

                case DxfEntityType.Line:
                    DxfLine dxfLine = (DxfLine)entity;
                    dxfLine.P1  = RotateLocation(rotationAngle, dxfLine.P1);
                    dxfLine.P2  = RotateLocation(rotationAngle, dxfLine.P2);
                    dxfLine.P1 += offset;
                    dxfLine.P2 += offset;
                    dxfreturn.Add(dxfLine);
                    break;

                case DxfEntityType.LwPolyline:
                    DxfPolyline dxfPoly = (DxfPolyline)entity;
                    foreach (DxfVertex vrt in dxfPoly.Vertices)
                    {
                        vrt.Location  = RotateLocation(rotationAngle, vrt.Location);
                        vrt.Location += offset;
                    }

                    dxfreturn.Add(dxfPoly);
                    break;

                case DxfEntityType.MLine:
                    DxfMLine mLine = (DxfMLine)entity;
                    tmpPts            = new List <DxfPoint>();
                    mLine.StartPoint += offset;

                    mLine.StartPoint = RotateLocation(rotationAngle, mLine.StartPoint);

                    foreach (DxfPoint vrt in mLine.Vertices)
                    {
                        var tmppnt = RotateLocation(rotationAngle, vrt);
                        tmppnt += offset;
                        tmpPts.Add(tmppnt);
                    }

                    mLine.Vertices.Clear();
                    mLine.Vertices.Concat(tmpPts);
                    dxfreturn.Add(mLine);
                    break;

                case DxfEntityType.Polyline:
                    DxfPolyline polyline = (DxfPolyline)entity;

                    List <DxfVertex> verts = new List <DxfVertex>();
                    foreach (DxfVertex vrt in polyline.Vertices)
                    {
                        var tmppnt = vrt;
                        tmppnt.Location  = RotateLocation(rotationAngle, tmppnt.Location);
                        tmppnt.Location += offset;
                        verts.Add(tmppnt);
                    }

                    DxfPolyline polyout = new DxfPolyline(verts);
                    polyout.Location = polyline.Location + offset;
                    polyout.IsClosed = polyline.IsClosed;
                    polyout.Layer    = polyline.Layer;
                    dxfreturn.Add(polyout);

                    break;

                case DxfEntityType.Body:
                case DxfEntityType.DgnUnderlay:
                case DxfEntityType.Dimension:
                case DxfEntityType.DwfUnderlay:
                case DxfEntityType.Face:
                case DxfEntityType.Helix:
                case DxfEntityType.Insert:
                case DxfEntityType.Light:
                case DxfEntityType.ModelerGeometry:
                case DxfEntityType.MText:
                case DxfEntityType.OleFrame:
                case DxfEntityType.Ole2Frame:
                case DxfEntityType.PdfUnderlay:
                case DxfEntityType.Point:
                case DxfEntityType.ProxyEntity:
                case DxfEntityType.Ray:
                case DxfEntityType.Region:
                case DxfEntityType.RText:
                case DxfEntityType.Section:
                case DxfEntityType.Seqend:
                case DxfEntityType.Shape:
                case DxfEntityType.Solid:
                case DxfEntityType.Spline:
                case DxfEntityType.Text:
                case DxfEntityType.Tolerance:
                case DxfEntityType.Trace:
                case DxfEntityType.Underlay:
                case DxfEntityType.Vertex:
                case DxfEntityType.WipeOut:
                case DxfEntityType.XLine:
                    throw new ArgumentException("unsupported entity type: " + entity.EntityType);
                }
            }

            return(dxfreturn);
        }
Exemple #21
0
 public static Polyline ToPolyline(this DxfPolyline poly)
 {
     return(new Polyline(poly.Vertices.Select(v => v.ToVertex()), poly.GetEntityColor(), poly));
 }
        public static completeDxfStruct processDxfFile(string in_obtainedFileName)
        {
            completeDxfStruct valueToReturn = new completeDxfStruct();
            DxfFile           dxfFile;

            using (System.IO.FileStream fs = new System.IO.FileStream(in_obtainedFileName, System.IO.FileMode.Open))
            {
                dxfFile = DxfFile.Load(fs);
                IList <DxfBlock>  allBlocks = dxfFile.Blocks;
                IList <DxfEntity> usedEntities;
                if ((allBlocks.Count == 0) || (allBlocks[0].Name.ToUpper().Contains("MODEL") == false))
                {
                    usedEntities = dxfFile.Entities;
                }
                else
                {
                    usedEntities = allBlocks[0].Entities;
                }

                foreach (DxfEntity entity in dxfFile.Entities)
                {
                    switch (entity.EntityType)
                    {
                    case DxfEntityType.Line:
                    {
                        DxfLine   line       = (DxfLine)entity;
                        MyDxfLine TransfLine = new MyDxfLine(line.P1.X, line.P1.Y, line.P2.X, line.P2.Y, line.Layer);
                        valueToReturn.addDxfDrawingEntry(TransfLine);
                        break;
                    }

                    case DxfEntityType.Arc:
                    {
                        DxfArc   arc       = (DxfArc)entity;
                        MyDxfArc TransfArc = new MyDxfArc(arc.Center.X, arc.Center.Y, arc.StartAngle, arc.EndAngle, arc.Radius, arc.Layer);
                        valueToReturn.addDxfDrawingEntry(TransfArc);
                        break;
                    }

                    case DxfEntityType.LwPolyline:
                    {         //polyline. It has vertices.
                        DxfLwPolyline polylineS             = entity as DxfLwPolyline;
                        int           totalnumberOfVertices = polylineS.Vertices.Count;
                        for (int i = 0; i < totalnumberOfVertices - 1; i++)
                        {         //iterate through vertices, taking them by 2. A figure is between these two
                            DxfLwPolylineVertex point1 = polylineS.Vertices[i];
                            DxfLwPolylineVertex point2 = polylineS.Vertices[i + 1];
                            if (point1.Bulge == 0)
                            {
                                MyDxfLine TransfLine = new MyDxfLine(point1.X, point1.Y, point2.X, point2.Y, polylineS.Layer);
                                valueToReturn.addDxfDrawingEntry(TransfLine);
                            }
                            else             //it is arc
                            // The bulge is the tangent of one fourth the included angle for an arc segment,
                            // made negative if the arc goes clockwise from the start point to the endpoint.
                            // A bulge of 0 indicates a straight segment, and a bulge of 1 is a semicircle
                            {
                                double angleOfArcRad = System.Math.Atan(Math.Abs(point1.Bulge)) * 4;
                                // http://mymathforum.com/algebra/21368-find-equation-circle-given-two-points-arc-angle.html
                                // tides of Internet have almost washed this post in forum
                                double xA; double xB; double yA; double yB;
                                if (point1.Bulge < 0)
                                {
                                    xA = point2.X; yA = point2.Y;
                                    xB = point1.X; yB = point1.Y;
                                }
                                else
                                {
                                    xA = point1.X;  yA = point1.Y;
                                    xB = point2.X;  yB = point2.Y;
                                }
                                double d_square = (xA - xB) * (xA - xB) + (yA - yB) * (yA - yB);
                                double r_square = (d_square) / (2.0 * (1.0 - Math.Cos(angleOfArcRad)));
                                double m = (xA - xB) / (yB - yA);
                                double a = Math.Sqrt(r_square - d_square / 4.0);
                                double xM = (xA + xB) / 2.0; double yM = (yA + yB) / 2.0;
                                double xC_plus  = xM + a / Math.Sqrt(m * m + 1);
                                double xC_minus = xM - a / Math.Sqrt(m * m + 1);
                                double yC_plus  = yM + m * a / Math.Sqrt(m * m + 1);
                                double yC_minus = yM - m * a / Math.Sqrt(m * m + 1);
                                // https://en.wikipedia.org/wiki/Linear_equation#Point%E2%80%93slope_form
                                double usedXCenter; double usedYCenter; double usedAngle1; double usedAngle2;
                                // https://stackoverflow.com/questions/1311049/how-to-map-atan2-to-degrees-0-360
                                double angle1_candidate1 = (Math.Atan2(yA - yC_plus, xA - xC_plus) * 180 / Math.PI + 360) % 360;
                                double angle2_candidate1 = (Math.Atan2(yB - yC_plus, xB - xC_plus) * 180 / Math.PI + 360) % 360;
                                double angle1_candidate2 = (Math.Atan2(yA - yC_minus, xA - xC_minus) * 180 / Math.PI + 360) % 360;
                                double angle2_candidate2 = (Math.Atan2(yB - yC_minus, xB - xC_minus) * 180 / Math.PI + 360) % 360;
                                //mydxfarc expects angles counterclockwise
                                if (point1.Bulge > 0)
                                {
                                    if (angle1_candidate1 < angle2_candidate1)
                                    {
                                        usedAngle1  = angle1_candidate1;
                                        usedAngle2  = angle2_candidate1;
                                        usedXCenter = xC_plus; usedYCenter = yC_plus;
                                    }
                                    else
                                    {
                                        usedAngle1  = angle1_candidate2;
                                        usedAngle2  = angle2_candidate2;
                                        usedXCenter = xC_minus; usedYCenter = yC_minus;
                                    }
                                }
                                else
                                {
                                    if (angle1_candidate1 > angle2_candidate1)
                                    {
                                        usedAngle1  = angle1_candidate2;
                                        usedAngle2  = angle2_candidate2;
                                        usedXCenter = xC_minus; usedYCenter = yC_minus;
                                    }
                                    else
                                    {
                                        usedAngle1  = angle1_candidate1;
                                        usedAngle2  = angle2_candidate1;
                                        usedXCenter = xC_plus; usedYCenter = yC_plus;
                                    }
                                }
                                MyDxfArc TransfArc = new MyDxfArc(usedXCenter, usedYCenter,
                                                                  usedAngle1,
                                                                  usedAngle2,
                                                                  Math.Sqrt(r_square),
                                                                  polylineS.Layer);
                                valueToReturn.addDxfDrawingEntry(TransfArc);
                            }
                        }
                        if (polylineS.IsClosed)
                        {
                            DxfLwPolylineVertex point1 = polylineS.Vertices[totalnumberOfVertices - 1];
                            DxfLwPolylineVertex point2 = polylineS.Vertices[0];
                            if (point1.Bulge == 0)
                            {
                                MyDxfLine TransfLine = new MyDxfLine(point1.X, point1.Y, point2.X, point2.Y, polylineS.Layer);
                                valueToReturn.addDxfDrawingEntry(TransfLine);
                            }
                            else             // arc
                                             // I so like the code above, so I cannot resist to copypaste it

                            {
                                double angleOfArcRad = System.Math.Atan(Math.Abs(point1.Bulge)) * 4;
                                double xA; double xB; double yA; double yB;
                                if (point1.Bulge < 0)
                                {
                                    xA = point2.X; yA = point2.Y;   xB = point1.X; yB = point1.Y;
                                }
                                else
                                {
                                    xA = point1.X; yA = point1.Y;   xB = point2.X; yB = point2.Y;
                                }
                                double d_square = (xA - xB) * (xA - xB) + (yA - yB) * (yA - yB);
                                double r_square = (d_square) / (2.0 * (1.0 - Math.Cos(angleOfArcRad)));
                                double m = (xA - xB) / (yB - yA);
                                double a = Math.Sqrt(r_square - d_square / 4.0);
                                double xM = (xA + xB) / 2.0; double yM = (yA + yB) / 2.0;
                                double xC_plus  = xM + a / Math.Sqrt(m * m + 1);
                                double xC_minus = xM - a / Math.Sqrt(m * m + 1);
                                double yC_plus  = yM + m * a / Math.Sqrt(m * m + 1);
                                double yC_minus = yM - m * a / Math.Sqrt(m * m + 1);
                                // https://en.wikipedia.org/wiki/Linear_equation#Point%E2%80%93slope_form
                                double usedXCenter; double usedYCenter; double usedAngle1; double usedAngle2;
                                // https://stackoverflow.com/questions/1311049/how-to-map-atan2-to-degrees-0-360
                                double angle1_candidate1 = (Math.Atan2(yA - yC_plus, xA - xC_plus) * 180 / Math.PI + 360) % 360;
                                double angle2_candidate1 = (Math.Atan2(yB - yC_plus, xB - xC_plus) * 180 / Math.PI + 360) % 360;
                                double angle1_candidate2 = (Math.Atan2(yA - yC_minus, xA - xC_minus) * 180 / Math.PI + 360) % 360;
                                double angle2_candidate2 = (Math.Atan2(yB - yC_minus, xB - xC_minus) * 180 / Math.PI + 360) % 360;
                                //mydxfarc expects angles counterclockwise
                                if (point1.Bulge > 0)
                                {
                                    if (angle1_candidate1 < angle2_candidate1)
                                    {
                                        usedAngle1  = angle1_candidate1; usedAngle2 = angle2_candidate1;
                                        usedXCenter = xC_plus; usedYCenter = yC_plus;
                                    }
                                    else
                                    {
                                        usedAngle1  = angle1_candidate2; usedAngle2 = angle2_candidate2;
                                        usedXCenter = xC_minus; usedYCenter = yC_minus;
                                    }
                                }
                                else
                                {
                                    if (angle1_candidate1 > angle2_candidate1)
                                    {
                                        usedAngle1  = angle1_candidate2; usedAngle2 = angle2_candidate2;
                                        usedXCenter = xC_minus; usedYCenter = yC_minus;
                                    }
                                    else
                                    {
                                        usedAngle1  = angle1_candidate1; usedAngle2 = angle2_candidate1;
                                        usedXCenter = xC_plus; usedYCenter = yC_plus;
                                    }
                                }
                                MyDxfArc TransfArc = new MyDxfArc(usedXCenter, usedYCenter,
                                                                  usedAngle1, usedAngle2, Math.Sqrt(r_square), polylineS.Layer);
                                valueToReturn.addDxfDrawingEntry(TransfArc);
                            }
                        }

                        break;
                    }

                    case DxfEntityType.Polyline:
                    {
                        //this is a spawn of autocad. Either copypaste code from LwPolyline section. Or just save the file in QCad.
                        //QCad replaces all polylines by lwPolylines
                        //https://github.com/IxMilia/Dxf/issues/90
                        DxfPolyline polyline = entity as DxfPolyline;

                        break;
                    }
                    }
                }
            }
            return(valueToReturn);
        }
Exemple #23
0
        public completeDxfStruct processDxfFile(string in_obtainedFileName)
        {
            completeDxfStruct valueToReturn = new completeDxfStruct();
            DxfFile           dxfFile;

            using (System.IO.FileStream fs = new System.IO.FileStream(in_obtainedFileName, System.IO.FileMode.Open))
            {
                dxfFile = DxfFile.Load(fs);
                IList <DxfBlock>  allBlocks = dxfFile.Blocks;
                IList <DxfEntity> usedEntities;
                if ((allBlocks.Count == 0) || (allBlocks[0].Name.ToUpper().Contains("MODEL") == false))
                {
                    usedEntities = dxfFile.Entities;
                }
                else
                {
                    usedEntities = allBlocks[0].Entities;
                }

                foreach (DxfEntity entity in dxfFile.Entities)
                {
                    switch (entity.EntityType)
                    {
                    case DxfEntityType.Line:
                    {
                        DxfLine   line       = (DxfLine)entity;
                        MyDxfLine TransfLine = new MyDxfLine(line.P1.X, line.P1.Y, line.P2.X, line.P2.Y);
                        valueToReturn.addDxfDrawingEntry(TransfLine);
                        break;
                    }

                    case DxfEntityType.Arc:
                    {
                        DxfArc   arc       = (DxfArc)entity;
                        MyDxfArc TransfArc = new MyDxfArc(arc.Center.X, arc.Center.Y, arc.StartAngle, arc.EndAngle, arc.Radius);
                        valueToReturn.addDxfDrawingEntry(TransfArc);
                        break;
                    }

                    case DxfEntityType.LwPolyline: {    //polyline. It has vertices.
                        DxfLwPolyline polylineS             = entity as DxfLwPolyline;
                        int           totalnumberOfVertices = polylineS.Vertices.Count;
                        for (int i = 0; i < totalnumberOfVertices - 1; i++)
                        {         //iterate through vertices, taking them by 2. A figure is between these two
                            DxfLwPolylineVertex point1 = polylineS.Vertices[i];
                            DxfLwPolylineVertex point2 = polylineS.Vertices[i + 1];
                            if (point1.Bulge == 0)
                            {
                                MyDxfLine TransfLine = new MyDxfLine(point1.X, point1.Y, point2.X, point2.Y);
                                valueToReturn.addDxfDrawingEntry(TransfLine);
                            }
                            else             //it is arc
                            // The bulge is the tangent of one fourth the included angle for an arc segment,
                            // made negative if the arc goes clockwise from the start point to the endpoint.
                            // A bulge of 0 indicates a straight segment, and a bulge of 1 is a semicircle
                            {
                                double angleOfArc = System.Math.Atan(point1.Bulge) * 180.0 / Math.PI * 4;
                                double startAngle = Math.Atan(point1.Y / point1.X) * 180.0 / Math.PI;
                                //double endAngle =
                            }
                        }

                        break;
                    }

                    case DxfEntityType.Polyline:  {
                        //https://github.com/IxMilia/Dxf/issues/90
                        DxfPolyline polyline = entity as DxfPolyline;

                        break;
                    }
                    }
                }
            }
            return(valueToReturn);
        }