netDxf icon indicating copy to clipboard operation
netDxf copied to clipboard

Drawing Extents

Open MikeRensing opened this issue 5 years ago • 5 comments

Is there a was with netDxf to find the extents of the model? The min and max points for everything drawing in the model.

MikeRensing avatar Mar 07 '19 14:03 MikeRensing

No. There are some entities I cannot calculate, it like Text, MText, and Tolerance; I have no way of measuring strings based in TTF and SHX fonts. Also the Underlay and Shape entities have its size defined by the scale of its original dimensions that comes from the PDF, DWF, DGN, and SHX file.

For newly loaded files you can read from the DrawingVariables of the document.

haplokuon avatar Mar 09 '19 16:03 haplokuon

I would just complete @haplokuon 's answer above, that in reality it is usually not an issue, it is easy to calculate bounding box of the whole 2D drawing or 3D model with "engineering precision", meaning it will not get issues under any common circumstances.

OakLD avatar Sep 26 '19 05:09 OakLD

if this is just for drawings components like lines and circles you can use something like this

private double[] GetBoundingBox(DxfDocument document)
{
            var lines = document.Lines;
            var circles = document.Circles;
            var ellipses = document.Ellipses;
            var polys = document.Polylines;
            var arcs = document.Arcs;
            var splines = document.Splines;
            List<Vector2> vectors = new List<Vector2>();
            foreach (var line in lines)
            {
                var vector = new Vector2(line.StartPoint.X, line.EndPoint.Y);
                if (!vectors.Contains(vector))
                    vectors.Add(vector);
            }
            foreach (var circle in circles)
            {
                vectors.AddRange(circle.PolygonalVertexes(4));
            }
//continue the same logic for the rest
//return the result
return new double[4] { vectors.Min(v => v.X), 
                                    vectors.Min(v => v.Y), 
                                    vectors.Max(v => v.X), 
                                    vectors.Max(v => v.Y) 
                                  };
}

HYMMA avatar Feb 10 '21 14:02 HYMMA

for line and circle of course calculation is simple. but what about arc for example?

LeiYangGH avatar Nov 21 '21 13:11 LeiYangGH

For the arc, you have to check if it passes 0° for X max, 180° for X min, 90° for Y max and 270° for Y min. If that extreme point is passed the bounding point in that direction is center plus/minus radius, otherwise it's max/min of Start and End point coordinate.

OakLD avatar Nov 21 '21 20:11 OakLD