|
@@ -14,6 +14,8 @@ namespace InABox.Dxf;
|
|
|
internal interface IDxfObject
|
|
|
{
|
|
|
void Draw(DrawData data);
|
|
|
+
|
|
|
+ RectangleF? GetBounds(TransformData data);
|
|
|
}
|
|
|
|
|
|
internal class DxfLine : IDxfObject
|
|
@@ -22,8 +24,21 @@ internal class DxfLine : IDxfObject
|
|
|
|
|
|
public void Draw(DrawData data)
|
|
|
{
|
|
|
- if (!Line.IsVisible || !data.Data.HasLayer(Line.Layer)) return;
|
|
|
+ if (!data.Data.ShouldDraw(Line)) return;
|
|
|
+
|
|
|
+ data.PushTransform();
|
|
|
+ //data.ArbitraryAxis(Line.Normal);
|
|
|
data.Graphics.DrawLine(new Pen(Color.Black, data.ConvertThickness((float)Line.Thickness)), DrawData.ConvertPoint(Line.StartPoint), DrawData.ConvertPoint(Line.EndPoint));
|
|
|
+ data.PopTransform();
|
|
|
+ }
|
|
|
+
|
|
|
+ public RectangleF? GetBounds(TransformData data)
|
|
|
+ {
|
|
|
+ if (!data.Data.ShouldDraw(Line)) return null;
|
|
|
+
|
|
|
+ return Utils.RectangleFromPoints(
|
|
|
+ data.TransformPoint(Line.StartPoint),
|
|
|
+ data.TransformPoint(Line.EndPoint));
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -41,7 +56,7 @@ internal class DxfInsert : IDxfObject
|
|
|
|
|
|
public void Draw(DrawData data)
|
|
|
{
|
|
|
- if (!Insert.IsVisible || !data.Data.HasLayer(Insert.Layer)) return;
|
|
|
+ if (!data.Data.ShouldDraw(Insert)) return;
|
|
|
|
|
|
// var transformation = Insert.GetTransformation();
|
|
|
// var translation = Insert.Position - transformation * Insert.Block.Origin;
|
|
@@ -71,6 +86,23 @@ internal class DxfInsert : IDxfObject
|
|
|
// DxfUtils.ConvertEl(obj)?.Draw(data);
|
|
|
// }
|
|
|
}
|
|
|
+
|
|
|
+ public RectangleF? GetBounds(TransformData data)
|
|
|
+ {
|
|
|
+ if (!data.Data.ShouldDraw(Insert)) return null;
|
|
|
+
|
|
|
+ data.PushTransform();
|
|
|
+ data.Translate(new PointF((float)Insert.Position.X, (float)Insert.Position.Y));
|
|
|
+ data.ArbitraryAxis(Insert.Normal);
|
|
|
+ data.Rotate((float)Insert.Rotation);
|
|
|
+ data.Scale((float)Insert.Scale.X, (float)Insert.Scale.Y);
|
|
|
+
|
|
|
+ var bounds = Utils.CombineBounds(Objects.Select(x => x.GetBounds(data)));
|
|
|
+
|
|
|
+ data.PopTransform();
|
|
|
+
|
|
|
+ return bounds;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
internal class DxfEllipse : IDxfObject
|
|
@@ -84,7 +116,7 @@ internal class DxfEllipse : IDxfObject
|
|
|
|
|
|
public void Draw(DrawData data)
|
|
|
{
|
|
|
- if (!Ellipse.IsVisible || !data.Data.HasLayer(Ellipse.Layer)) return;
|
|
|
+ if (!data.Data.ShouldDraw(Ellipse)) return;
|
|
|
|
|
|
foreach(var obj in Ellipse.ToPolyline2D(100).Explode())
|
|
|
{
|
|
@@ -98,6 +130,16 @@ internal class DxfEllipse : IDxfObject
|
|
|
|
|
|
// data.Graphics.DrawArc(new Pen(Color.Black, data.ConvertThickness((float)Ellipse.Thickness)), center.X - size.Width / 2, center.Y - size.Height / 2, size.Width, size.Height, startAngle, Utils.Mod(endAngle - startAngle, 360));
|
|
|
}
|
|
|
+
|
|
|
+ public RectangleF? GetBounds(TransformData data)
|
|
|
+ {
|
|
|
+ if (!data.Data.ShouldDraw(Ellipse)) return null;
|
|
|
+
|
|
|
+ var halfSize = new netDxf.Vector3(Ellipse.MajorAxis / 2, Ellipse.MinorAxis / 2, 0);
|
|
|
+ return Utils.RectangleFromPoints(
|
|
|
+ data.TransformPoint(Ellipse.Center - halfSize),
|
|
|
+ data.TransformPoint(Ellipse.Center + halfSize));
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
internal class DxfSolid : IDxfObject
|
|
@@ -111,7 +153,7 @@ internal class DxfSolid : IDxfObject
|
|
|
|
|
|
public void Draw(DrawData data)
|
|
|
{
|
|
|
- if (!Solid.IsVisible || !data.Data.HasLayer(Solid.Layer)) return;
|
|
|
+ if (!data.Data.ShouldDraw(Solid)) return;
|
|
|
|
|
|
var vertices = new Vector2[]
|
|
|
{
|
|
@@ -122,6 +164,21 @@ internal class DxfSolid : IDxfObject
|
|
|
};
|
|
|
data.Graphics.FillPolygon(new SolidBrush(Color.Black), vertices.ToArray(x => DrawData.ConvertPoint(x)));
|
|
|
}
|
|
|
+
|
|
|
+ public RectangleF? GetBounds(TransformData data)
|
|
|
+ {
|
|
|
+ if (!data.Data.ShouldDraw(Solid)) return null;
|
|
|
+
|
|
|
+ var vertices = new Vector2[]
|
|
|
+ {
|
|
|
+ Solid.FirstVertex,
|
|
|
+ Solid.SecondVertex,
|
|
|
+ Solid.FourthVertex, // Apparently the third and fourth are the wrong way round, so I've mirrored that here.
|
|
|
+ Solid.ThirdVertex
|
|
|
+ };
|
|
|
+ return Utils.RectangleFromPoints(
|
|
|
+ vertices.ToArray(data.TransformPoint));
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
internal class DxfPolyline2D : IDxfObject
|
|
@@ -135,7 +192,7 @@ internal class DxfPolyline2D : IDxfObject
|
|
|
|
|
|
public void Draw(DrawData data)
|
|
|
{
|
|
|
- if (!Polyline.IsVisible || !data.Data.HasLayer(Polyline.Layer)) return;
|
|
|
+ if (!data.Data.ShouldDraw(Polyline)) return;
|
|
|
|
|
|
var entities = Polyline.Explode();
|
|
|
foreach(var entity in entities)
|
|
@@ -164,6 +221,14 @@ internal class DxfPolyline2D : IDxfObject
|
|
|
|
|
|
// }
|
|
|
}
|
|
|
+
|
|
|
+ public RectangleF? GetBounds(TransformData data)
|
|
|
+ {
|
|
|
+ if (!data.Data.ShouldDraw(Polyline)) return null;
|
|
|
+
|
|
|
+ var entities = Polyline.Explode();
|
|
|
+ return Utils.CombineBounds(entities.Select(x => DxfUtils.ConvertEl(x)?.GetBounds(data)));
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
internal class DxfMText : IDxfObject
|
|
@@ -175,10 +240,8 @@ internal class DxfMText : IDxfObject
|
|
|
MText = text;
|
|
|
}
|
|
|
|
|
|
- public void Draw(DrawData data)
|
|
|
+ private Font GetFont()
|
|
|
{
|
|
|
- if (!MText.IsVisible || !data.Data.HasLayer(MText.Layer)) return;
|
|
|
-
|
|
|
FontFamily fontFamily;
|
|
|
if (MText.Style.FontFamilyName.IsNullOrWhiteSpace())
|
|
|
{
|
|
@@ -188,21 +251,28 @@ internal class DxfMText : IDxfObject
|
|
|
{
|
|
|
fontFamily = new FontFamily(MText.Style.FontFamilyName);
|
|
|
}
|
|
|
- var font = new Font(fontFamily, (float)MText.Height, MText.Style.FontStyle switch
|
|
|
+ return new Font(fontFamily, (float)MText.Height, MText.Style.FontStyle switch
|
|
|
{
|
|
|
netDxf.Tables.FontStyle.Bold => FontStyle.Bold,
|
|
|
netDxf.Tables.FontStyle.Italic => FontStyle.Italic,
|
|
|
netDxf.Tables.FontStyle.Regular or _ => FontStyle.Regular,
|
|
|
});
|
|
|
+ }
|
|
|
+
|
|
|
+ private string GetText()
|
|
|
+ {
|
|
|
+ return MText.PlainText().Replace("^M", "");
|
|
|
+ }
|
|
|
|
|
|
- var text = MText.PlainText().Replace("^M", "");
|
|
|
+ private static Bitmap _placeholderBitmap = new Bitmap(1, 1);
|
|
|
|
|
|
- data.PushTransform();
|
|
|
+ private void Transform(TransformData data, Font font, string text, Graphics g)
|
|
|
+ {
|
|
|
data.Translate(new PointF((float)MText.Position.X, (float)MText.Position.Y));
|
|
|
data.Rotate((float)MText.Rotation);
|
|
|
data.Scale(1, -1);
|
|
|
|
|
|
- var size = data.Graphics.MeasureString(text, font, new PointF(), StringFormat.GenericTypographic);
|
|
|
+ var size = g.MeasureString(text, font, new PointF(), StringFormat.GenericTypographic);
|
|
|
switch (MText.AttachmentPoint)
|
|
|
{
|
|
|
case MTextAttachmentPoint.MiddleLeft:
|
|
@@ -219,7 +289,7 @@ internal class DxfMText : IDxfObject
|
|
|
var ascent = font.FontFamily.GetCellAscent(font.Style);
|
|
|
var lineSpace = font.FontFamily.GetLineSpacing(font.Style);
|
|
|
var baseline = ascent * font.Height / font.FontFamily.GetEmHeight(font.Style);
|
|
|
- var ratio = font.GetHeight(data.Graphics) / lineSpace;
|
|
|
+ var ratio = font.GetHeight(g) / lineSpace;
|
|
|
data.Translate(new PointF(0, -baseline + ascent * ratio));
|
|
|
break;
|
|
|
}
|
|
@@ -241,34 +311,76 @@ internal class DxfMText : IDxfObject
|
|
|
data.Translate(new PointF(-(float)size.Width, 0));
|
|
|
break;
|
|
|
}
|
|
|
+ }
|
|
|
+
|
|
|
+ public void Draw(DrawData data)
|
|
|
+ {
|
|
|
+ if (!data.Data.ShouldDraw(MText)) return;
|
|
|
|
|
|
+ var font = GetFont();
|
|
|
+ var text = GetText();
|
|
|
+
|
|
|
+ data.PushTransform();
|
|
|
+ Transform(data, font, text, data.Graphics);
|
|
|
data.Graphics.DrawString(text, font, new SolidBrush(Color.Black), new PointF(0, 0), StringFormat.GenericTypographic);
|
|
|
data.PopTransform();
|
|
|
}
|
|
|
+
|
|
|
+ public RectangleF? GetBounds(TransformData data)
|
|
|
+ {
|
|
|
+ if (!data.Data.ShouldDraw(MText)) return null;
|
|
|
+
|
|
|
+ var font = GetFont();
|
|
|
+ var text = GetText();
|
|
|
+
|
|
|
+ data.PushTransform();
|
|
|
+ using var g = Graphics.FromImage(_placeholderBitmap);
|
|
|
+ Transform(data, font, text, g);
|
|
|
+
|
|
|
+ var size = g.MeasureString(text, font, new PointF(), StringFormat.GenericTypographic);
|
|
|
+ var bounds = Utils.RectangleFromPoints(
|
|
|
+ data.TransformPoint(0, 0),
|
|
|
+ data.TransformPoint(size.Width, size.Height));
|
|
|
+
|
|
|
+ data.PopTransform();
|
|
|
+ return bounds;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
internal class DxfDimension : IDxfObject
|
|
|
{
|
|
|
public Dimension Dimension { get; set; }
|
|
|
|
|
|
+ public List<IDxfObject> Objects { get; set; }
|
|
|
+
|
|
|
public DxfDimension(Dimension dimension)
|
|
|
{
|
|
|
Dimension = dimension;
|
|
|
+
|
|
|
+ if(Dimension.Block is null)
|
|
|
+ {
|
|
|
+ Objects = new();
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ Objects = Dimension.Block.Entities.Select(DxfUtils.ConvertEl).NotNull().ToList();
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
public void Draw(DrawData data)
|
|
|
{
|
|
|
- if (!Dimension.IsVisible || !data.Data.HasLayer(Dimension.Layer)) return;
|
|
|
+ if (!data.Data.ShouldDraw(Dimension)) return;
|
|
|
|
|
|
- if(Dimension.Block is null)
|
|
|
+ foreach(var entity in Objects)
|
|
|
{
|
|
|
- return;
|
|
|
+ entity.Draw(data);
|
|
|
}
|
|
|
+ }
|
|
|
|
|
|
- var entities = Dimension.Block.Entities;
|
|
|
- foreach(var entity in entities)
|
|
|
- {
|
|
|
- DxfUtils.ConvertEl(entity)?.Draw(data);
|
|
|
- }
|
|
|
+ public RectangleF? GetBounds(TransformData data)
|
|
|
+ {
|
|
|
+ if (!data.Data.ShouldDraw(Dimension)) return null;
|
|
|
+
|
|
|
+ return Utils.CombineBounds(Objects.Select(x => x.GetBounds(data)));
|
|
|
}
|
|
|
}
|