Ejemplo n.º 1
0
        public HashSet <string> ReadLayers()
        {
            var buffer = new HashSet <string>();

            using (var reader = new AdvancedLineStreamReader(_filePath))
            {
                while (!reader.EndOfStream)
                {
                    var result = reader.FindConsecutiveLines(
                        MatchSubClassMarker,
                        AcDbEntity
                        );

                    if (result.Success)
                    {
                        reader.FindLine(MatchLayerGroupCode);
                        buffer.Add(reader.ReadLine().Trim());
                    }
                    else
                    {
                        break;
                    }
                }
            }

            return(buffer);
        }
Ejemplo n.º 2
0
        /// <summary>
        ///     Get geometries in DXF file.
        /// </summary>
        /// <param name="callback">An action to consume geometries as they are read from the file.</param>
        /// <param name="howmany">Howmany geometries to read</param>
        /// <returns>Returns a count and Extents of the geometries consumed.</returns>
        public (int Count, GeometryExtents <double> Extents) BeginGetGeometries(Action <string, IMarkGeometry> callback, int howmany = int.MaxValue)
        {
            _count  = 0;
            howmany = howmany < 0 ? int.MaxValue : howmany;
            var extents = GeometryExtents <double> .CreateDefaultDouble();

            using (var reader = new AdvancedLineStreamReader(_filePath))
            {
                string layerName;
                List <IMarkGeometry> geometries = null;

                while (
                    !reader.EndOfStream &&
                    _count < howmany
                    )
                {
                    (layerName, geometries) = ParseEntity(reader);

                    if (geometries != null)
                    {
                        foreach (var geometry in geometries)
                        {
                            _count += 1;
                            extents = GeometryExtents <double> .Combine(extents, geometry.Extents);

                            callback((layerName == null) ? "0" : layerName, geometry);
                        }
                    }
                }
            }

            return(_count, extents);
        }
Ejemplo n.º 3
0
        private MarkGeometryPoint ReadPointAdvance(AdvancedLineStreamReader readerIn)
        {
            var result = readerIn.FindConsecutiveLines(
                MatchDoubleParams,
                MatchDouble
                );

            if (result.Success)
            {
                double X = double.Parse(MatchDouble.Match(result.LineB).Value);

                result = readerIn.FindConsecutiveLines(
                    MatchDoubleParams,
                    MatchDouble
                    );

                double Y = double.Parse(MatchDouble.Match(result.LineB).Value);
                double Z = 0;

                if (MatchDoubleParams.IsMatch(readerIn.PeekLine()))
                {
                    result = readerIn.FindConsecutiveLines(
                        MatchDoubleParams,
                        MatchDouble
                        );

                    Z = double.Parse(MatchDouble.Match(result.LineB).Value);
                }

                return(new MarkGeometryPoint(X, Y, Z));
            }

            return(null);
        }
Ejemplo n.º 4
0
        private (string LayerName, MarkGeometryCircle Circle) ParseCircle(AdvancedLineStreamReader readerIn)
        {
            var(success, layerName) = ReadLayerName(readerIn, "AcDbCircle");

            if (success)
            {
                var result1 = readerIn.FindConsecutiveLines(
                    "100",
                    "AcDbCircle"
                    );

                if (!result1.Success)
                {
                    return(null, null);
                }
            }

            var centrePoint = ReadPointFast(readerIn);

            // read radius 40
            readerIn.ReadLine();
            double radius = double.Parse(readerIn.ReadLine());

            var circle = new MarkGeometryCircle(centrePoint, radius);

            return(layerName, circle);
        }
Ejemplo n.º 5
0
        private (string LayerName, MarkGeometryPath Path) ParsePolyline(AdvancedLineStreamReader readerIn)
        {
            var(success, layerName) = ReadLayerName(readerIn, MatchPolylineEntity);

            if (success)
            {
                var result = readerIn.FindConsecutiveLines(
                    MatchSubClassMarker,
                    MatchPolylineEntity
                    );

                if (!result.Success)
                {
                    return(null, null);
                }
            }

            // read number of vertices 90
            var line             = readerIn.ReadLine();
            int numberOfVertices = int.Parse(readerIn.ReadLine());

            var points = new List <MarkGeometryPoint>(numberOfVertices);

            for (int i = 0; i < numberOfVertices; i++)
            {
                points.Add(ReadPointFast2D(readerIn));
            }

            return(layerName, new MarkGeometryPath(points.ToArray()));
        }
Ejemplo n.º 6
0
        public (string LayerName, List <IMarkGeometry> Entity) ParseEntity(AdvancedLineStreamReader readerIn)
        {
            var result = readerIn.FindConsecutiveLines(
                MatchEntityGroupCode,
                SupportedEntities
                );

            if (!result.Success)
            {
                return(null, null);
            }

            switch (result.LineB.Trim())
            {
            case "POINT":
                var pData = ParsePoint(readerIn);
                return(pData.LayerName, new List <IMarkGeometry>()
                {
                    pData.Point
                });

            case "LINE":
                var lData = ParseLine(readerIn);
                return(lData.LayerName, new List <IMarkGeometry>()
                {
                    lData.Line
                });

            case "CIRCLE":
                var cData = ParseCircle(readerIn);
                return(cData.LayerName, new List <IMarkGeometry>()
                {
                    cData.Circle
                });

            case "ARC":
                var aData = ParseArc(readerIn);
                return(aData.LayerName, new List <IMarkGeometry>()
                {
                    aData.Arc
                });

            case "LWPOLYLINE":
                return(ParseLWPolyline(readerIn));

            case "SPLINE":
                var sData = ParseSpline(readerIn);
                return(sData.LayerName, new List <IMarkGeometry>()
                {
                    sData.Spline
                });
            }

            return(null, null);
        }
Ejemplo n.º 7
0
        private (string LayerName, MarkGeometrySpline Spline) ParseSpline(AdvancedLineStreamReader readerIn)
        {
            var(success, layerName) = ReadLayerName(readerIn, MatchSplineEntity);

            if (success)
            {
                var result1 = readerIn.FindConsecutiveLines(
                    MatchSubClassMarker,
                    MatchSplineEntity
                    );

                if (!result1.Success)
                {
                    return(null, null);
                }
            }

            // read spline flag
            int flag = ReadInteger(readerIn, "70");

            // read degree of spline curve
            int degree = ReadInteger(readerIn, "71");

            // read number of knots
            int nKnots = ReadInteger(readerIn, "72");

            // read number of control points
            int nControlPoints = ReadInteger(readerIn, "73");

            // read number of fit points
            int nFitPoints = ReadInteger(readerIn, "74");

            var knots = new List <double>(nKnots);

            for (int i = 0; i < nKnots; i++)
            {
                knots.Add(ReadDouble(readerIn, "40"));
            }

            var controlPoints = new List <MarkGeometryPoint>(nControlPoints);

            for (int i = 0; i < nControlPoints; i++)
            {
                controlPoints.Add(ReadPointFast(readerIn));
            }

            var fitPoints = new List <MarkGeometryPoint>(nFitPoints);

            for (int i = 0; i < nFitPoints; i++)
            {
                fitPoints.Add(ReadPointFast(readerIn));
            }

            return(layerName, new MarkGeometrySpline(flag, degree, knots, controlPoints, fitPoints));
        }
Ejemplo n.º 8
0
        private MarkGeometryPoint ReadPointFast2D(AdvancedLineStreamReader readerIn)
        {
            // read doble params 10, 11, etc
            readerIn.FindLine(MatchDoubleParams);
            double X = double.Parse(readerIn.ReadLine());

            // read doble params 20, 21, etc
            readerIn.ReadLine();
            double Y = double.Parse(readerIn.ReadLine());

            return(new MarkGeometryPoint(X, Y));
        }
Ejemplo n.º 9
0
        private double ReadDoubleAdvanced(AdvancedLineStreamReader readerIn, string paramCode)
        {
            readerIn.FindLine(paramCode);
            var line = readerIn.ReadLine();

            if (line.Contains("e"))
            {
                var components = line.Split('e');
                return(double.Parse(components[0]) * Math.Pow(10, double.Parse(components[1])));
            }

            return(double.Parse(readerIn.ReadLine()));
        }
Ejemplo n.º 10
0
        private (string LayerName, MarkGeometryArc Arc) ParseArc(AdvancedLineStreamReader readerIn)
        {
            var(success, layerName) = ReadLayerName(readerIn, "AcDbCircle");

            if (success)
            {
                var result1 = readerIn.FindConsecutiveLines(
                    "100",
                    "AcDbCircle"
                    );

                if (!result1.Success)
                {
                    return(null, null);
                }
            }

            MarkGeometryPoint centrePoint = ReadPointFast(readerIn);

            // read radius 40
            readerIn.ReadLine();
            double radius = double.Parse(readerIn.ReadLine());

            var result2 = readerIn.FindConsecutiveLines(
                "100",
                "AcDbArc"
                );

            if (!result2.Success)
            {
                return(null, null);
            }

            // read angle 50
            readerIn.ReadLine();
            var startAngle = double.Parse(readerIn.ReadLine());

            // read angle 60
            readerIn.ReadLine();
            var endAngle = double.Parse(readerIn.ReadLine());

            var arc = new MarkGeometryArc(
                centrePoint,
                radius, // convert angle to radians
                GeometricArithmeticModule.ToRadians(startAngle),
                GeometricArithmeticModule.ToRadians(endAngle)
                );

            return(layerName, arc);
        }
Ejemplo n.º 11
0
        private (bool Success, string) ReadLayerName(AdvancedLineStreamReader readerIn, Regex terminationRegexIn)
        {
            var result = readerIn.FindConsecutiveLines(
                "100",
                "AcDbEntity"
                );

            if (!result.Success)
            {
                return(false, "0");
            }

            var result2 = readerIn.FindLineUntil(
                new Regex(@"^\s*8$"),
                terminationRegexIn
                );

            return(result2.Success, result2.Success ? readerIn.ReadLine().Trim() : "0");
        }
Ejemplo n.º 12
0
        public string ReadVersion()
        {
            using (var reader = new AdvancedLineStreamReader(_filePath))
            {
                var result = reader.FindConsecutiveLines(
                    MatchVersionGroupCode,
                    AcadVersionMarker
                    );

                if (result.Success)
                {
                    // skip sub group code
                    reader.ReadLine();
                    return(reader.ReadLine().Trim());
                }

                return(null);
            }
        }
Ejemplo n.º 13
0
        private (string LayerName, MarkGeometryPoint Point) ParsePoint(AdvancedLineStreamReader readerIn)
        {
            var(success, layerName) = ReadLayerName(readerIn, "AcDbPoint");

            if (success)
            {
                var result1 = readerIn.FindConsecutiveLines(
                    "100",
                    "AcDbPoint"
                    );

                if (!result1.Success)
                {
                    return(null, null);
                }
            }

            return(layerName, ReadPointFast(readerIn));
        }
Ejemplo n.º 14
0
        private MarkGeometryPoint ReadPointFast(AdvancedLineStreamReader readerIn)
        {
            // read doble params 10, 11, etc
            readerIn.FindLine(MatchDoubleParams);
            double X = double.Parse(readerIn.ReadLine());

            // read doble params 20, 21, etc
            readerIn.ReadLine();
            double Y = double.Parse(readerIn.ReadLine());

            double Z = 0;

            if (MatchDoubleLastParams.IsMatch(readerIn.PeekLine()))
            {
                // read doble params 30, 31, etc
                readerIn.ReadLine();
                Z = double.Parse(readerIn.ReadLine());
            }

            return(new MarkGeometryPoint(X, Y, Z));
        }
Ejemplo n.º 15
0
        private (string LayerName, MarkGeometryLine Line) ParseLine(AdvancedLineStreamReader readerIn)
        {
            var(success, layerName) = ReadLayerName(readerIn, "AcDbLine");

            if (success)
            {
                var result = readerIn.FindConsecutiveLines(
                    "100",
                    "AcDbLine"
                    );

                if (!result.Success)
                {
                    return(null, null);
                }
            }

            var line = new MarkGeometryLine(
                ReadPointFast(readerIn),
                ReadPointFast(readerIn)
                );

            return(layerName, line);
        }
Ejemplo n.º 16
0
 private int ReadInteger(AdvancedLineStreamReader readerIn, string paramCode)
 {
     readerIn.FindLine(paramCode);
     return(int.Parse(readerIn.ReadLine()));
 }
Ejemplo n.º 17
0
 private double ReadDouble(AdvancedLineStreamReader readerIn, string paramCode)
 {
     readerIn.FindLine(paramCode);
     return(double.Parse(readerIn.ReadLine()));
 }
Ejemplo n.º 18
0
        private (string LayerName, List <IMarkGeometry> Path) ParseLWPolyline(AdvancedLineStreamReader readerIn)
        {
            var(success, layerName) = ReadLayerName(readerIn, MatchLWPolylineEntity);

            if (success)
            {
                var result1 = readerIn.FindConsecutiveLines(
                    MatchSubClassMarker,
                    MatchLWPolylineEntity
                    );

                if (!result1.Success)
                {
                    return(null, null);
                }
            }

            readerIn.ReadLine(); // consume number of vertices 90
            int numberOfVertices = int.Parse(readerIn.ReadLine());
            int flag             = ReadInteger(readerIn, "70");

            var bulges = new List <double>(numberOfVertices);
            var points = new List <MarkGeometryPoint>(numberOfVertices - 1);

            for (int i = 0; i < numberOfVertices; i++)
            {
                points.Add(ReadPointFast2D(readerIn));
                bulges.Add((readerIn.PeekLine().Trim() == "42") ? ReadDouble(readerIn, "42") : 0d);
            }

            if (points.Count > 0 && flag == 1) // i.e. is closed
            {
                points.Add(points[0]);
            }

            var buffer = new List <IMarkGeometry>();
            var path   = new MarkGeometryPath();

            for (int i = 0; i < points.Count - 1; i++)
            {
                var p1    = points[i];
                var p2    = points[i + 1];
                var bulge = bulges[i];

                if (Math.Abs(bulge) <= double.Epsilon)
                {
                    path.Add(p1, true);
                    path.Add(p2, true);
                }
                else
                {
                    if (path.Points.Count > 0)
                    {
                        path.Update(); // force path to re-compute it's properties
                        buffer.Add(path);
                        path = new MarkGeometryPath();
                    }

                    buffer.Add(new MarkGeometryArc(p1, p2, bulge));
                }
            }

            if (path.Points.Count > 0)
            {
                path.Update(); // force path to re-compute it's properties
                buffer.Add(path);
            }

            return(layerName, buffer);
        }