Files
OpenNest/OpenNest/Controls/EntityView.cs
AJ Isaacs 2d956fd3f7 Restructure project layout to flatten directory structure
Move all projects from Source/ to repository root for simpler navigation.
- Remove External/ dependency DLLs (will use NuGet packages)
- Remove Installer/ NSIS script
- Replace PartCollection/PlateCollection with ObservableList
- Add packages.config for NuGet dependencies

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-27 20:29:12 -05:00

215 lines
6.0 KiB
C#

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
using OpenNest.Geometry;
namespace OpenNest.Controls
{
public class EntityView : DrawControl
{
public List<Entity> Entities;
private Pen pen = new Pen(Color.FromArgb(70, 70, 70));
public EntityView()
{
Entities = new List<Entity>();
this.BackColor = Color.FromArgb(33, 40, 48);
this.Cursor = Cursors.Cross;
SetStyle(
ControlStyles.AllPaintingInWmPaint |
ControlStyles.OptimizedDoubleBuffer |
ControlStyles.UserPaint, true);
}
protected override void OnMouseClick(MouseEventArgs e)
{
base.OnMouseClick(e);
if (!Focused) Focus();
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
e.Graphics.SmoothingMode = SmoothingMode.HighSpeed;
e.Graphics.DrawLine(pen, origin.X, 0, origin.X, Height);
e.Graphics.DrawLine(pen, 0, origin.Y, Width, origin.Y);
e.Graphics.TranslateTransform(origin.X, origin.Y);
foreach (var entity in Entities)
DrawEntity(e.Graphics, entity, Pens.White);
#if DRAW_OFFSET
var offsetShape = new Shape();
offsetShape.Entities.AddRange(Entities);
foreach (var entity in ((Shape)offsetShape.OffsetEntity(0.25, OffsetSide.Left)).Entities)
DrawEntity(e.Graphics, entity, Pens.RoyalBlue);
#endif
}
protected override void OnMouseWheel(MouseEventArgs e)
{
base.OnMouseWheel(e);
float multiplier = Math.Abs(e.Delta / 120.0f);
if (e.Delta > 0)
ZoomToControlPoint(e.Location, (float)Math.Pow(ZoomInFactor, multiplier));
else
ZoomToControlPoint(e.Location, (float)Math.Pow(ZoomOutFactor, multiplier));
Invalidate();
}
protected override void OnMouseMove(MouseEventArgs e)
{
if (e.Button == MouseButtons.Middle)
{
var diffx = e.X - lastPoint.X;
var diffy = e.Y - lastPoint.Y;
origin.X += diffx;
origin.Y += diffy;
Invalidate();
}
else
{
LastPoint = CurrentPoint;
CurrentPoint = PointControlToWorld(e.Location);
}
lastPoint = e.Location;
base.OnMouseMove(e);
}
protected override void OnMouseDoubleClick(MouseEventArgs e)
{
base.OnMouseDoubleClick(e);
if (e.Button == MouseButtons.Middle)
ZoomToFit();
}
private void DrawEntity(Graphics g, Entity e, Pen pen)
{
if (!e.Layer.IsVisible)
return;
switch (e.Type)
{
case EntityType.Arc:
DrawArc(g, (Arc)e, pen);
break;
case EntityType.Circle:
DrawCircle(g, (Circle)e, pen);
break;
case EntityType.Line:
DrawLine(g, (Line)e, pen);
break;
}
}
private void DrawLine(Graphics g, Line line, Pen pen)
{
var pt1 = PointWorldToGraph(line.StartPoint);
var pt2 = PointWorldToGraph(line.EndPoint);
g.DrawLine(pen, pt1, pt2);
}
private void DrawArc(Graphics g, Arc arc, Pen pen)
{
var center = PointWorldToGraph(arc.Center);
var radius = LengthWorldToGui(arc.Radius);
var diameter = radius * 2.0f;
var startAngle = arc.IsReversed
? -(float)Angle.ToDegrees(arc.EndAngle)
: -(float)Angle.ToDegrees(arc.StartAngle);
g.DrawArc(
pen,
center.X - radius,
center.Y - radius,
diameter,
diameter,
startAngle,
-(float)Angle.ToDegrees(arc.SweepAngle()));
}
private void DrawCircle(Graphics g, Circle circle, Pen pen)
{
var center = PointWorldToGraph(circle.Center);
var radius = LengthWorldToGui(circle.Radius);
var diameter = radius * 2.0f;
g.DrawEllipse(pen,
center.X - radius,
center.Y - radius,
diameter,
diameter);
}
private void DrawPoint(Graphics g, Vector pt, Pen pen)
{
var pt1 = PointWorldToGraph(pt);
g.DrawLine(pen, pt1.X - 3, pt1.Y, pt1.X + 3, pt1.Y);
g.DrawLine(pen, pt1.X, pt1.Y - 3, pt1.X, pt1.Y + 3);
}
private void DrawPoint(Graphics g, Vector pt, Pen pen, Brush fillBrush)
{
var pt1 = PointWorldToGraph(pt);
var rect = new RectangleF(pt1.X - 3, pt1.Y - 3, 6, 6);
g.FillEllipse(fillBrush, rect);
g.DrawEllipse(pen, rect);
}
private void DrawIntersections(Graphics g)
{
for (int i = 0; i < Entities.Count; ++i)
{
var entity1 = Entities[i];
if (entity1.Type != EntityType.Line)
continue;
for (int j = i + 1; j < Entities.Count; ++j)
{
var entity2 = Entities[j];
if (entity2.Type != EntityType.Line)
continue;
var line1 = (Line)entity1;
var line2 = (Line)entity2;
Vector pt;
if (line1.Intersects(line2, out pt))
DrawPoint(g, pt, Pens.Yellow, Brushes.Red);
}
}
}
public void ZoomToFit(bool redraw = true)
{
ZoomToArea(Entities.GetBoundingBox(), redraw);
}
}
}