I´m using netDxf
to convert a .dxf
file to a bitmap, and I can't figure out how to get the polyline's vertices coordinates, and then draw it using .net
.
using netDxf.Entities;
using netDxf;
using System.Drawing.Drawing2D;
namespace teste_bitmap
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
LoadDxfFile();
}
private void LoadDxfFile()
{
string filePath = "C:/molde 2.dxf";
try
{
DxfDocument dxfDocument = DxfDocument.Load(filePath);
if (dxfDocument == null)
{
Application.Exit();
}
else
{
// Get the Image object from DrawDxfEntities method
System.Drawing.Image image = DrawDxfEntities(dxfDocument);
// Display the image in a PictureBox
pictureBox1.Image = image;
}
string entityInfoText = string.Join(Environment.NewLine, entityInfos);
MessageBox.Show(entityInfoText, "Entity Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch { }
}
public System.Drawing.Image DrawDxfEntities(DxfDocument dxfDocument)
{
Bitmap bitmap = new Bitmap(pictureBox1.Width, pictureBox1.Height);
Graphics graphics = Graphics.FromImage(bitmap);
graphics.SmoothingMode = SmoothingMode.AntiAlias;
graphics.Clear(Color.White);
foreach (var block in dxfDocument.Blocks)
{
foreach (var entity in block.Entities)
{
DrawDxfEntity(graphics, entity);
}
}
return bitmap;
}
static List<string> entityInfos = new List<string>();
static void DrawDxfEntity(Graphics graphics, EntityObject entity)
{
string entityInfo = string.Empty;
if (entity is Line line)
{
entityInfo = $"Processing Line: StartPoint=({line.StartPoint.X}, {line.StartPoint.Y}), EndPoint=({line.EndPoint.X}, {line.EndPoint.Y})";
graphics.DrawLine(Pens.Black, (float)line.StartPoint.X, (float)line.StartPoint.Y, (float)line.EndPoint.X, (float)line.EndPoint.Y);
}
else if (entity is Circle circle)
{
entityInfo = $"Processing Circle: Center=({circle.Center.X}, {circle.Center.Y}), Radius={circle.Radius}";
graphics.DrawEllipse(Pens.Black, (float)(circle.Center.X - circle.Radius), (float)(circle.Center.Y - circle.Radius),
(float)(2 * circle.Radius), (float)(2 * circle.Radius));
}
else if (entity is Arc arc)
{
entityInfo = $"Processing Arc: Start Angle=({arc.StartAngle}), End Angle=({arc.EndAngle}), Radius={arc.Radius}";
double sweepAngle = arc.EndAngle - arc.StartAngle;
arc.EndAngle = sweepAngle;
graphics.DrawArc(Pens.Black, (float)(arc.Center.X - arc.Radius), (float)(arc.Center.Y - arc.Radius),
(float)(2 * arc.Radius), (float)(2 * arc.Radius), (float)arc.StartAngle, (float)arc.EndAngle);
}
else if (entity is Polyline2D polyline)
{
}
entityInfos.Add(entityInfo);
}
}
}
I tried to get the vertices by using
polyline.Vertexes
but I can't draw them with
graphics.DrawLines()
because it needs to be PointF
and I can't get the coordinates of each vertex
You can try converting the vertex coordinates to PointF.
using System.Collections.Generic;
else if (entity is Polyline2D polyline)
{
List<PointF> points = new List<PointF>();
foreach (var vertex in polyline.Vertexes)
{
points.Add(new PointF((float)vertex.Position.X, (float)vertex.Position.Y));
}
entityInfo = $"Processing Polyline: Vertices={polyline.Vertexes.Count}";
graphics.DrawLines(Pens.Black, points.ToArray());
}
With this code you will iterate through each vertex in Polyline2D, then convert the coordinates to PointF and add it to the list of points, and finally graphics.DrawLines()
to draw the polyline.