diff --git a/DeepNestLib/NFP.cs b/DeepNestLib/NFP.cs index 30aa069..6d0e946 100644 --- a/DeepNestLib/NFP.cs +++ b/DeepNestLib/NFP.cs @@ -31,7 +31,7 @@ public NFP() { this.points = new SvgPoint[0]; } - + public NFP(IEnumerable points) { this.points = points.DeepClone(); diff --git a/DeepNestLib/NestingContext.cs b/DeepNestLib/NestingContext.cs index a2b62da..f5518fa 100644 --- a/DeepNestLib/NestingContext.cs +++ b/DeepNestLib/NestingContext.cs @@ -323,49 +323,15 @@ public void LoadInputData(string path, int count) public bool TryImportFromRawDetail(RawDetail raw, int src, out NFP loadedNfp) { - NFP po = null; - List nfps = new List(); - foreach (var item in raw.Outers) + loadedNfp = raw.ToNfp(); + if (loadedNfp == null) { - var nn = new NFP(); - nfps.Add(nn); - foreach (var pitem in item.Points) - { - nn.AddPoint(new SvgPoint(pitem.X, pitem.Y)); - } - } - - if (nfps.Any()) - { - var tt = nfps.OrderByDescending(z => z.Area).First(); - po = tt; // Reference caution needed here; should be cloning not messing with the original object? - po.Name = raw.Name; - - foreach (var r in nfps) - { - if (r == tt) - { - continue; - } - - if (po.Children == null) - { - po.Children = new List(); - } - - po.Children.Add(r); - } - - po.Source = src; - Polygons.Add(po); - loadedNfp = po; - return true; - } - else - { - loadedNfp = null; return false; } + + loadedNfp.Source = src; + Polygons.Add(loadedNfp); + return true; } public int GetNextSource() @@ -376,6 +342,7 @@ public int GetNextSource() } return 0; } + public int GetNextSheetSource() { if (Sheets.Any()) diff --git a/DeepNestLib/RawDetail.cs b/DeepNestLib/RawDetail.cs index 7022f37..79044f4 100644 --- a/DeepNestLib/RawDetail.cs +++ b/DeepNestLib/RawDetail.cs @@ -1,12 +1,64 @@ namespace DeepNestLib { - using System.Collections.Generic; + using System.Collections.Generic; + using System.Drawing; + using System.Drawing.Drawing2D; + using System.Linq; - public class RawDetail + public class RawDetail + { + public List Outers = new List(); + public List Holes = new List(); + + public RectangleF BoundingBox() { - public List Outers = new List(); - public List Holes = new List(); + GraphicsPath gp = new GraphicsPath(); + foreach (var item in Outers) + { + gp.AddPolygon(item.Points.ToArray()); + } + + return gp.GetBounds(); + } + + public string Name { get; set; } + + public NFP ToNfp() + { + NFP po = null; + List nfps = new List(); + foreach (var item in this.Outers) + { + var nn = new NFP(); + nfps.Add(nn); + foreach (var pitem in item.Points) + { + nn.AddPoint(new SvgPoint(pitem.X, pitem.Y)); + } + } + + if (nfps.Any()) + { + var tt = nfps.OrderByDescending(z => z.Area).First(); + po = tt; // Reference caution needed here; should be cloning not messing with the original object? + po.Name = Name; + + foreach (var r in nfps) + { + if (r == tt) + { + continue; + } + + if (po.Children == null) + { + po.Children = new List(); + } + po.Children.Add(r); + } + } - public string Name { get; set; } + return po; } -} + } +} \ No newline at end of file diff --git a/DeepNestLib/SvgNestConfig.cs b/DeepNestLib/SvgNestConfig.cs index 5dbbb14..342d950 100644 --- a/DeepNestLib/SvgNestConfig.cs +++ b/DeepNestLib/SvgNestConfig.cs @@ -11,6 +11,9 @@ public class SvgNestConfig public double TimeRatio = 0.5; public bool MergeLines = false; + // port features (don't exist in the original DeepNest project) + public bool clipByHull = false; + public double CurveTolerance { get diff --git a/DeepNestPort/DrawingContext.cs b/DeepNestPort/DrawingContext.cs index e93e5e8..6de4036 100644 --- a/DeepNestPort/DrawingContext.cs +++ b/DeepNestPort/DrawingContext.cs @@ -1,4 +1,5 @@ -using System; +using DeepNestLib; +using System; using System.Drawing; using System.Drawing.Drawing2D; using System.Linq; @@ -6,152 +7,229 @@ namespace DeepNestPort { - public class DrawingContext + public class DrawingContext + { + public DrawingContext(PictureBox pb) { - public DrawingContext(PictureBox pb) + box = pb; + + bmp = new Bitmap(pb.Width, pb.Height); + pb.SizeChanged += Pb_SizeChanged; + gr = Graphics.FromImage(bmp); + gr.SmoothingMode = SmoothingMode.AntiAlias; + box.Image = bmp; + + pb.MouseDown += PictureBox1_MouseDown; + pb.MouseUp += PictureBox1_MouseUp; + pb.MouseMove += Pb_MouseMove; + sx = box.Width / 2; + sy = -box.Height / 2; + pb.MouseWheel += Pb_MouseWheel; + } + + GraphicsPath GetGraphicsPath(NFP nfp) + { + GraphicsPath gp = new GraphicsPath(); + gp.AddPolygon(nfp.Points.Select(z => Transform(z.x, z.y)).ToArray()); + if (nfp.Children != null) + { + foreach (var item in nfp.Children) { - box = pb; - - bmp = new Bitmap(pb.Width, pb.Height); - pb.SizeChanged += Pb_SizeChanged; - gr = Graphics.FromImage(bmp); - gr.SmoothingMode = SmoothingMode.AntiAlias; - box.Image = bmp; - - pb.MouseDown += PictureBox1_MouseDown; - pb.MouseUp += PictureBox1_MouseUp; - pb.MouseMove += Pb_MouseMove; - sx = box.Width / 2; - sy = -box.Height / 2; - pb.MouseWheel += Pb_MouseWheel; + gp.AddPolygon(item.Points.Select(z => Transform(z.x, z.y)).ToArray()); } + } - private void Pb_MouseWheel(object sender, MouseEventArgs e) - { - float zold = zoom; - if (e.Delta > 0) { zoom *= 1.5f; ; } - else { zoom *= 0.5f; } - if (zoom < 0.08) { zoom = 0.08f; } - if (zoom > 1000) { zoom = 1000f; } + return gp; + } - var pos = box.PointToClient(Cursor.Position); + GraphicsPath GetGraphicsPath(RawDetail det) + { + GraphicsPath gp = new GraphicsPath(); + foreach (var item in det.Outers) + { + gp.AddPolygon(item.Points.Select(z => Transform(z)).ToArray()); + } - sx = -(pos.X / zold - sx - pos.X / zoom); - sy = (pos.Y / zold + sy - pos.Y / zoom); - } + return gp; + } - public bool FocusOnMove = true; - private void Pb_MouseMove(object sender, MouseEventArgs e) - { - if (!FocusOnMove) return; - box.Focus(); - } + public float GetLabelHeight() + { + return SystemFonts.DefaultFont.GetHeight(); + } - private void PictureBox1_MouseUp(object sender, MouseEventArgs e) - { - isDrag = false; + public SizeF DrawLabel(string text, Brush fontBrush, Color backColor, int x, int y, int opacity = 128) + { + var ms = this.gr.MeasureString(text, SystemFonts.DefaultFont); + this.gr.FillRectangle(new SolidBrush(Color.FromArgb(opacity, backColor)), x, y, ms.Width, ms.Height); + this.gr.DrawString(text, SystemFonts.DefaultFont, fontBrush, x, y); + return ms; + } - var p = box.PointToClient(Cursor.Position); - var pos = box.PointToClient(Cursor.Position); - var posx = (pos.X / zoom - sx); - var posy = (-pos.Y / zoom - sy); - } + public GraphicsPath Draw(NFP nfp, Pen pen = null, Brush brush = null) + { + var gp = GetGraphicsPath(nfp); + if (brush != null) + { + this.gr.FillPath(brush, gp); + } + + if (pen != null) + { + this.gr.DrawPath(pen, gp); + } + + return gp; + } - private void PictureBox1_MouseDown(object sender, MouseEventArgs e) - { - var pos = box.PointToClient(Cursor.Position); - var p = Transform(pos); - - if (e.Button == MouseButtons.Right) - { - isDrag = true; - startx = pos.X; - starty = pos.Y; - origsx = sx; - origsy = sy; - } - } - float startx, starty; - float origsx, origsy; - bool isDrag = false; - - PictureBox box; - public float sx, sy; - public float zoom = 1; - public Graphics gr; - public Bitmap bmp; - public bool InvertY = true; - public virtual PointF Transform(PointF p1) - { - return new PointF((p1.X + sx) * zoom, (InvertY ? (-1) : 1) * (p1.Y + sy) * zoom); - } - public virtual PointF Transform(double x, double y) - { - return new PointF(((float)(x) + sx) * zoom, (InvertY ? (-1) : 1) * ((float)(y) + sy) * zoom); - } + public GraphicsPath Draw(RawDetail det, Pen pen = null, Brush brush = null) + { + var gp = GetGraphicsPath(det); + if (brush != null) + gr.FillPath(brush, gp); + if (pen != null) + gr.DrawPath(pen, gp); + return gp; + } + private void Pb_MouseWheel(object sender, MouseEventArgs e) + { + float zold = zoom; + if (e.Delta > 0) { zoom *= 1.5f; ; } + else { zoom *= 0.5f; } + if (zoom < 0.08) { zoom = 0.08f; } + if (zoom > 1000) { zoom = 1000f; } - private void Pb_SizeChanged(object sender, EventArgs e) - { - bmp = new Bitmap(box.Width, box.Height); - gr = Graphics.FromImage(bmp); + var pos = box.PointToClient(Cursor.Position); - box.Image = bmp; - } + sx = -(pos.X / zold - sx - pos.X / zoom); + sy = (pos.Y / zold + sy - pos.Y / zoom); + } - public PointF GetPos() - { - var pos = box.PointToClient(Cursor.Position); - var posx = (pos.X / zoom - sx); - var posy = (-pos.Y / zoom - sy); + public bool FocusOnMove = true; + private void Pb_MouseMove(object sender, MouseEventArgs e) + { + if (!FocusOnMove) return; + box.Focus(); + } - return new PointF(posx, posy); - } - public void Update() - { - if (isDrag) - { - var p = box.PointToClient(Cursor.Position); + private void PictureBox1_MouseUp(object sender, MouseEventArgs e) + { + isDrag = false; - sx = origsx + ((p.X - startx) / zoom); - sy = origsy + (-(p.Y - starty) / zoom); - } - } + var p = box.PointToClient(Cursor.Position); + var pos = box.PointToClient(Cursor.Position); + var posx = (pos.X / zoom - sx); + var posy = (-pos.Y / zoom - sy); + } - public void Setup() - { - box.Invalidate(); - } + private void PictureBox1_MouseDown(object sender, MouseEventArgs e) + { + var pos = box.PointToClient(Cursor.Position); + var p = Transform(pos); + + if (e.Button == MouseButtons.Right) + { + isDrag = true; + startx = pos.X; + starty = pos.Y; + origsx = sx; + origsy = sy; + } + } - public void FitToPoints(PointF[] points, int gap = 0) - { - var maxx = points.Max(z => z.X) + gap; - var minx = points.Min(z => z.X) - gap; - var maxy = points.Max(z => z.Y) + gap; - var miny = points.Min(z => z.Y) - gap; + internal void Clear(Color color) + { + gr.Clear(color); + } + + float startx, starty; - var w = box.Width; - var h = box.Height; + internal void Reset() + { + gr.ResetTransform(); + } - var dx = maxx - minx; - var kx = w / dx; - var dy = maxy - miny; - var ky = h / dy; + float origsx, origsy; + bool isDrag = false; - var oz = zoom; - var sz1 = new Size((int)(dx * kx), (int)(dy * kx)); - var sz2 = new Size((int)(dx * ky), (int)(dy * ky)); - zoom = kx; - if (sz1.Width > w || sz1.Height > h) zoom = ky; + PictureBox box; + public float sx, sy; + public float zoom = 1; + public Graphics gr; + public Bitmap bmp; + public bool InvertY = true; + public virtual PointF Transform(PointF p1) + { + return new PointF((p1.X + sx) * zoom, (InvertY ? (-1) : 1) * (p1.Y + sy) * zoom); + } + public virtual PointF Transform(double x, double y) + { + return new PointF(((float)(x) + sx) * zoom, (InvertY ? (-1) : 1) * ((float)(y) + sy) * zoom); + } - var x = dx / 2 + minx; - var y = dy / 2 + miny; - sx = ((w / 2f) / zoom - x); - sy = -((h / 2f) / zoom + y); + private void Pb_SizeChanged(object sender, EventArgs e) + { + bmp = new Bitmap(box.Width, box.Height); + gr = Graphics.FromImage(bmp); - var test = Transform(new PointF(x, y)); + box.Image = bmp; + } + + public PointF GetPos() + { + var pos = box.PointToClient(Cursor.Position); + var posx = (pos.X / zoom - sx); + var posy = (-pos.Y / zoom - sy); + + return new PointF(posx, posy); + } + public void Update() + { + if (isDrag) + { + var p = box.PointToClient(Cursor.Position); + + sx = origsx + ((p.X - startx) / zoom); + sy = origsy + (-(p.Y - starty) / zoom); + } + } + + public void Setup() + { + box.Invalidate(); + } + + public void FitToPoints(PointF[] points, int gap = 0) + { + var maxx = points.Max(z => z.X) + gap; + var minx = points.Min(z => z.X) - gap; + var maxy = points.Max(z => z.Y) + gap; + var miny = points.Min(z => z.Y) - gap; + + var w = box.Width; + var h = box.Height; + + var dx = maxx - minx; + var kx = w / dx; + var dy = maxy - miny; + var ky = h / dy; + + var oz = zoom; + var sz1 = new Size((int)(dx * kx), (int)(dy * kx)); + var sz2 = new Size((int)(dx * ky), (int)(dy * ky)); + zoom = kx; + if (sz1.Width > w || sz1.Height > h) zoom = ky; + + var x = dx / 2 + minx; + var y = dy / 2 + miny; + + sx = ((w / 2f) / zoom - x); + sy = -((h / 2f) / zoom + y); + + var test = Transform(new PointF(x, y)); - } } + } } diff --git a/DeepNestPort/Form1.Designer.cs b/DeepNestPort/Form1.Designer.cs index a0a1dfe..17c7002 100644 --- a/DeepNestPort/Form1.Designer.cs +++ b/DeepNestPort/Form1.Designer.cs @@ -1,33 +1,33 @@ namespace DeepNestPort { - partial class Form1 - { - /// - /// Required designer variable. - /// - private System.ComponentModel.IContainer components = null; + partial class Form1 + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; - /// - /// Clean up any resources being used. - /// - /// true if managed resources should be disposed; otherwise, false. - protected override void Dispose(bool disposing) - { - if (disposing && (components != null)) - { - components.Dispose(); - } - base.Dispose(disposing); - } + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } - #region Windows Form Designer generated code + #region Windows Form Designer generated code - /// - /// Required method for Designer support - do not modify - /// the contents of this method with the code editor. - /// - private void InitializeComponent() - { + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { this.components = new System.ComponentModel.Container(); System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1)); this.pictureBox1 = new System.Windows.Forms.PictureBox(); @@ -58,6 +58,7 @@ private void InitializeComponent() this.toolStripButton4 = new System.Windows.Forms.ToolStripButton(); this.toolStripButton6 = new System.Windows.Forms.ToolStripButton(); this.tabPage2 = new System.Windows.Forms.TabPage(); + this.checkBox5 = new System.Windows.Forms.CheckBox(); this.label2 = new System.Windows.Forms.Label(); this.textBox6 = new System.Windows.Forms.TextBox(); this.groupBox8 = new System.Windows.Forms.GroupBox(); @@ -153,6 +154,8 @@ private void InitializeComponent() this.timer1 = new System.Windows.Forms.Timer(this.components); this.statusStrip1 = new System.Windows.Forms.StatusStrip(); this.toolStripStatusLabel1 = new System.Windows.Forms.ToolStripStatusLabel(); + this.groupBox9 = new System.Windows.Forms.GroupBox(); + this.checkBox6 = new System.Windows.Forms.CheckBox(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit(); this.tabControl1.SuspendLayout(); this.tabPage3.SuspendLayout(); @@ -191,6 +194,7 @@ private void InitializeComponent() ((System.ComponentModel.ISupportInitialize)(this.pictureBox2)).BeginInit(); this.tabPage5.SuspendLayout(); this.statusStrip1.SuspendLayout(); + this.groupBox9.SuspendLayout(); this.SuspendLayout(); // // pictureBox1 @@ -397,19 +401,19 @@ private void InitializeComponent() this.deleteToolStripMenuItem2, this.quantityToolStripMenuItem}); this.contextMenuStrip4.Name = "contextMenuStrip4"; - this.contextMenuStrip4.Size = new System.Drawing.Size(133, 76); + this.contextMenuStrip4.Size = new System.Drawing.Size(119, 70); // // clearToolStripMenuItem // this.clearToolStripMenuItem.Name = "clearToolStripMenuItem"; - this.clearToolStripMenuItem.Size = new System.Drawing.Size(132, 24); + this.clearToolStripMenuItem.Size = new System.Drawing.Size(118, 22); this.clearToolStripMenuItem.Text = "clear"; this.clearToolStripMenuItem.Click += new System.EventHandler(this.clearToolStripMenuItem_Click); // // deleteToolStripMenuItem2 // this.deleteToolStripMenuItem2.Name = "deleteToolStripMenuItem2"; - this.deleteToolStripMenuItem2.Size = new System.Drawing.Size(132, 24); + this.deleteToolStripMenuItem2.Size = new System.Drawing.Size(118, 22); this.deleteToolStripMenuItem2.Text = "delete"; this.deleteToolStripMenuItem2.Click += new System.EventHandler(this.deleteToolStripMenuItem2_Click); // @@ -420,27 +424,27 @@ private void InitializeComponent() this.multiplyToolStripMenuItem, this.divideToolStripMenuItem}); this.quantityToolStripMenuItem.Name = "quantityToolStripMenuItem"; - this.quantityToolStripMenuItem.Size = new System.Drawing.Size(132, 24); + this.quantityToolStripMenuItem.Size = new System.Drawing.Size(118, 22); this.quantityToolStripMenuItem.Text = "quantity"; // // setToToolStripMenuItem // this.setToToolStripMenuItem.Name = "setToToolStripMenuItem"; - this.setToToolStripMenuItem.Size = new System.Drawing.Size(146, 26); + this.setToToolStripMenuItem.Size = new System.Drawing.Size(118, 22); this.setToToolStripMenuItem.Text = "set to"; this.setToToolStripMenuItem.Click += new System.EventHandler(this.setToToolStripMenuItem_Click); // // multiplyToolStripMenuItem // this.multiplyToolStripMenuItem.Name = "multiplyToolStripMenuItem"; - this.multiplyToolStripMenuItem.Size = new System.Drawing.Size(146, 26); + this.multiplyToolStripMenuItem.Size = new System.Drawing.Size(118, 22); this.multiplyToolStripMenuItem.Text = "multiply"; this.multiplyToolStripMenuItem.Click += new System.EventHandler(this.multiplyToolStripMenuItem_Click); // // divideToolStripMenuItem // this.divideToolStripMenuItem.Name = "divideToolStripMenuItem"; - this.divideToolStripMenuItem.Size = new System.Drawing.Size(146, 26); + this.divideToolStripMenuItem.Size = new System.Drawing.Size(118, 22); this.divideToolStripMenuItem.Text = "divide"; this.divideToolStripMenuItem.Click += new System.EventHandler(this.divideToolStripMenuItem_Click); // @@ -478,6 +482,8 @@ private void InitializeComponent() // // tabPage2 // + this.tabPage2.Controls.Add(this.groupBox9); + this.tabPage2.Controls.Add(this.checkBox5); this.tabPage2.Controls.Add(this.label2); this.tabPage2.Controls.Add(this.textBox6); this.tabPage2.Controls.Add(this.groupBox8); @@ -505,6 +511,17 @@ private void InitializeComponent() this.tabPage2.Text = "Settings"; this.tabPage2.UseVisualStyleBackColor = true; // + // checkBox5 + // + this.checkBox5.AutoSize = true; + this.checkBox5.Location = new System.Drawing.Point(389, 11); + this.checkBox5.Name = "checkBox5"; + this.checkBox5.Size = new System.Drawing.Size(111, 17); + this.checkBox5.TabIndex = 31; + this.checkBox5.Text = "simplification draw"; + this.checkBox5.UseVisualStyleBackColor = true; + this.checkBox5.CheckedChanged += new System.EventHandler(this.checkBox5_CheckedChanged); + // // label2 // this.label2.AutoSize = true; @@ -1561,6 +1578,27 @@ private void InitializeComponent() this.toolStripStatusLabel1.Size = new System.Drawing.Size(15, 20); this.toolStripStatusLabel1.Text = ".."; // + // groupBox9 + // + this.groupBox9.Controls.Add(this.checkBox6); + this.groupBox9.Location = new System.Drawing.Point(10, 228); + this.groupBox9.Name = "groupBox9"; + this.groupBox9.Size = new System.Drawing.Size(200, 100); + this.groupBox9.TabIndex = 32; + this.groupBox9.TabStop = false; + this.groupBox9.Text = "Port features"; + // + // checkBox6 + // + this.checkBox6.AutoSize = true; + this.checkBox6.Location = new System.Drawing.Point(6, 21); + this.checkBox6.Name = "checkBox6"; + this.checkBox6.Size = new System.Drawing.Size(169, 17); + this.checkBox6.TabIndex = 33; + this.checkBox6.Text = "clip by hull during simplification"; + this.checkBox6.UseVisualStyleBackColor = true; + this.checkBox6.CheckedChanged += new System.EventHandler(this.checkBox6_CheckedChanged); + // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); @@ -1622,135 +1660,140 @@ private void InitializeComponent() this.tabPage5.PerformLayout(); this.statusStrip1.ResumeLayout(false); this.statusStrip1.PerformLayout(); + this.groupBox9.ResumeLayout(false); + this.groupBox9.PerformLayout(); this.ResumeLayout(false); this.PerformLayout(); - } - - #endregion - private System.Windows.Forms.PictureBox pictureBox1; - private System.Windows.Forms.Timer timer1; - private System.Windows.Forms.TabControl tabControl1; - private System.Windows.Forms.TabPage tabPage1; - private System.Windows.Forms.TabPage tabPage2; - private System.Windows.Forms.ListView listView1; - private System.Windows.Forms.ColumnHeader columnHeader1; - private System.Windows.Forms.ColumnHeader columnHeader2; - private System.Windows.Forms.ContextMenuStrip contextMenuStrip1; - private System.Windows.Forms.ToolStripMenuItem clearAllToolStripMenuItem; - private System.Windows.Forms.ToolStripMenuItem deleteToolStripMenuItem; - private System.Windows.Forms.StatusStrip statusStrip1; - private System.Windows.Forms.ToolStripStatusLabel toolStripStatusLabel1; - private System.Windows.Forms.Label label3; - private System.Windows.Forms.TextBox textBox1; - private System.Windows.Forms.Label label4; - private System.Windows.Forms.TextBox textBox2; - private System.Windows.Forms.ListView listView2; - private System.Windows.Forms.ColumnHeader columnHeader3; - private System.Windows.Forms.ColumnHeader columnHeader4; - private System.Windows.Forms.Label label5; - private System.Windows.Forms.TextBox textBox3; - private System.Windows.Forms.ToolStripMenuItem moveToSheetsToolStripMenuItem; - private System.Windows.Forms.ContextMenuStrip contextMenuStrip2; - private System.Windows.Forms.ToolStripMenuItem clearAllToolStripMenuItem1; - private System.Windows.Forms.ToolStripMenuItem moveToPolygonsToolStripMenuItem; - private System.Windows.Forms.ListView listView3; - private System.Windows.Forms.ColumnHeader columnHeader5; - private System.Windows.Forms.ContextMenuStrip contextMenuStrip3; - private System.Windows.Forms.ToolStripMenuItem importSelectedToolStripMenuItem; - private System.Windows.Forms.ColumnHeader columnHeader7; - private System.Windows.Forms.ColumnHeader columnHeader6; - private System.Windows.Forms.ToolStripMenuItem cloneQntToolStripMenuItem; - private System.Windows.Forms.Button button10; - private System.Windows.Forms.CheckBox checkBox2; - private System.Windows.Forms.CheckBox checkBox3; - private System.Windows.Forms.CheckBox checkBox4; - private System.Windows.Forms.ComboBox comboBox1; - private System.Windows.Forms.Label label7; - private System.Windows.Forms.NumericUpDown numericUpDown1; - private System.Windows.Forms.ListView listView4; - private System.Windows.Forms.ColumnHeader columnHeader8; - private System.Windows.Forms.Label label6; - private System.Windows.Forms.NumericUpDown numericUpDown2; - private System.Windows.Forms.TabPage tabPage4; - private System.Windows.Forms.TableLayoutPanel tableLayoutPanel1; - private System.Windows.Forms.Panel panel1; - private System.Windows.Forms.GroupBox groupBox1; - private System.Windows.Forms.Button button1; - private System.Windows.Forms.Label label8; - private System.Windows.Forms.NumericUpDown numericUpDown3; - private System.Windows.Forms.GroupBox groupBox2; - private System.Windows.Forms.Button button13; - private System.Windows.Forms.Button button15; - private System.Windows.Forms.Button button14; - private System.Windows.Forms.Button button16; - private System.Windows.Forms.Button button17; - private System.Windows.Forms.ToolStrip toolStrip1; - private System.Windows.Forms.ToolStripButton toolStripButton1; - private System.Windows.Forms.ToolStripButton toolStripButton2; - private System.Windows.Forms.GroupBox groupBox3; - private System.Windows.Forms.Label label10; - private System.Windows.Forms.Label label9; - private System.Windows.Forms.TextBox textBox5; - private System.Windows.Forms.TextBox textBox4; - private System.Windows.Forms.GroupBox groupBox4; - private System.Windows.Forms.Button button5; - private System.Windows.Forms.Button button6; - private System.Windows.Forms.ToolStripMenuItem deleteToolStripMenuItem1; - private System.Windows.Forms.ComboBox comboBox2; - private System.Windows.Forms.Label label11; - private System.Windows.Forms.Button button2; - private System.Windows.Forms.ToolStripButton toolStripButton3; - private System.Windows.Forms.TableLayoutPanel tableLayoutPanel2; - private System.Windows.Forms.TableLayoutPanel tableLayoutPanel3; - private System.Windows.Forms.Panel panel2; - private System.Windows.Forms.TableLayoutPanel tableLayoutPanel4; - private System.Windows.Forms.GroupBox groupBox5; - private System.Windows.Forms.GroupBox groupBox6; - private System.Windows.Forms.Button button3; - private System.Windows.Forms.GroupBox groupBox7; - private System.Windows.Forms.Button button4; - private System.Windows.Forms.PictureBox pictureBox2; - private System.Windows.Forms.Button button7; - private System.Windows.Forms.CheckBox checkBox1; - private System.Windows.Forms.GroupBox groupBox8; - private System.Windows.Forms.RadioButton radioButton3; - private System.Windows.Forms.RadioButton radioButton2; - private System.Windows.Forms.RadioButton radioButton1; - private System.Windows.Forms.Button button8; - private System.Windows.Forms.ColumnHeader columnHeader9; - private System.Windows.Forms.ColumnHeader columnHeader10; - private System.Windows.Forms.TabPage tabPage3; - private System.Windows.Forms.ToolStrip toolStrip2; - private System.Windows.Forms.ToolStripButton toolStripButton4; - private System.Windows.Forms.PictureBox pictureBox3; - private System.Windows.Forms.ToolStripButton toolStripButton5; - private BrightIdeasSoftware.ObjectListView objectListView1; - private BrightIdeasSoftware.OLVColumn olvColumn1; - private BrightIdeasSoftware.OLVColumn olvColumn2; - private System.Windows.Forms.ToolStripButton toolStripButton6; - private BrightIdeasSoftware.ObjectListView objectListView2; - private BrightIdeasSoftware.OLVColumn olvColumn3; - private BrightIdeasSoftware.OLVColumn olvColumn4; - private BrightIdeasSoftware.OLVColumn olvColumn5; - private System.Windows.Forms.ContextMenuStrip contextMenuStrip4; - private System.Windows.Forms.ToolStripMenuItem clearToolStripMenuItem; - private System.Windows.Forms.ToolStripMenuItem deleteToolStripMenuItem2; - private System.Windows.Forms.TabPage tabPage5; - private System.Windows.Forms.Label label1; - private System.Windows.Forms.LinkLabel linkLabel1; - private System.Windows.Forms.Panel panel3; - private System.Windows.Forms.ToolStrip toolStrip3; - private System.Windows.Forms.ToolStripButton toolStripButton8; - private System.Windows.Forms.ToolStripButton toolStripButton9; - private System.Windows.Forms.TableLayoutPanel tableLayoutPanel7; - private System.Windows.Forms.Panel panel4; - private System.Windows.Forms.Label label2; - private System.Windows.Forms.TextBox textBox6; - private System.Windows.Forms.ToolStripMenuItem quantityToolStripMenuItem; - private System.Windows.Forms.ToolStripMenuItem setToToolStripMenuItem; - private System.Windows.Forms.ToolStripMenuItem multiplyToolStripMenuItem; - private System.Windows.Forms.ToolStripMenuItem divideToolStripMenuItem; } + + #endregion + private System.Windows.Forms.PictureBox pictureBox1; + private System.Windows.Forms.Timer timer1; + private System.Windows.Forms.TabControl tabControl1; + private System.Windows.Forms.TabPage tabPage1; + private System.Windows.Forms.TabPage tabPage2; + private System.Windows.Forms.ListView listView1; + private System.Windows.Forms.ColumnHeader columnHeader1; + private System.Windows.Forms.ColumnHeader columnHeader2; + private System.Windows.Forms.ContextMenuStrip contextMenuStrip1; + private System.Windows.Forms.ToolStripMenuItem clearAllToolStripMenuItem; + private System.Windows.Forms.ToolStripMenuItem deleteToolStripMenuItem; + private System.Windows.Forms.StatusStrip statusStrip1; + private System.Windows.Forms.ToolStripStatusLabel toolStripStatusLabel1; + private System.Windows.Forms.Label label3; + private System.Windows.Forms.TextBox textBox1; + private System.Windows.Forms.Label label4; + private System.Windows.Forms.TextBox textBox2; + private System.Windows.Forms.ListView listView2; + private System.Windows.Forms.ColumnHeader columnHeader3; + private System.Windows.Forms.ColumnHeader columnHeader4; + private System.Windows.Forms.Label label5; + private System.Windows.Forms.TextBox textBox3; + private System.Windows.Forms.ToolStripMenuItem moveToSheetsToolStripMenuItem; + private System.Windows.Forms.ContextMenuStrip contextMenuStrip2; + private System.Windows.Forms.ToolStripMenuItem clearAllToolStripMenuItem1; + private System.Windows.Forms.ToolStripMenuItem moveToPolygonsToolStripMenuItem; + private System.Windows.Forms.ListView listView3; + private System.Windows.Forms.ColumnHeader columnHeader5; + private System.Windows.Forms.ContextMenuStrip contextMenuStrip3; + private System.Windows.Forms.ToolStripMenuItem importSelectedToolStripMenuItem; + private System.Windows.Forms.ColumnHeader columnHeader7; + private System.Windows.Forms.ColumnHeader columnHeader6; + private System.Windows.Forms.ToolStripMenuItem cloneQntToolStripMenuItem; + private System.Windows.Forms.Button button10; + private System.Windows.Forms.CheckBox checkBox2; + private System.Windows.Forms.CheckBox checkBox3; + private System.Windows.Forms.CheckBox checkBox4; + private System.Windows.Forms.ComboBox comboBox1; + private System.Windows.Forms.Label label7; + private System.Windows.Forms.NumericUpDown numericUpDown1; + private System.Windows.Forms.ListView listView4; + private System.Windows.Forms.ColumnHeader columnHeader8; + private System.Windows.Forms.Label label6; + private System.Windows.Forms.NumericUpDown numericUpDown2; + private System.Windows.Forms.TabPage tabPage4; + private System.Windows.Forms.TableLayoutPanel tableLayoutPanel1; + private System.Windows.Forms.Panel panel1; + private System.Windows.Forms.GroupBox groupBox1; + private System.Windows.Forms.Button button1; + private System.Windows.Forms.Label label8; + private System.Windows.Forms.NumericUpDown numericUpDown3; + private System.Windows.Forms.GroupBox groupBox2; + private System.Windows.Forms.Button button13; + private System.Windows.Forms.Button button15; + private System.Windows.Forms.Button button14; + private System.Windows.Forms.Button button16; + private System.Windows.Forms.Button button17; + private System.Windows.Forms.ToolStrip toolStrip1; + private System.Windows.Forms.ToolStripButton toolStripButton1; + private System.Windows.Forms.ToolStripButton toolStripButton2; + private System.Windows.Forms.GroupBox groupBox3; + private System.Windows.Forms.Label label10; + private System.Windows.Forms.Label label9; + private System.Windows.Forms.TextBox textBox5; + private System.Windows.Forms.TextBox textBox4; + private System.Windows.Forms.GroupBox groupBox4; + private System.Windows.Forms.Button button5; + private System.Windows.Forms.Button button6; + private System.Windows.Forms.ToolStripMenuItem deleteToolStripMenuItem1; + private System.Windows.Forms.ComboBox comboBox2; + private System.Windows.Forms.Label label11; + private System.Windows.Forms.Button button2; + private System.Windows.Forms.ToolStripButton toolStripButton3; + private System.Windows.Forms.TableLayoutPanel tableLayoutPanel2; + private System.Windows.Forms.TableLayoutPanel tableLayoutPanel3; + private System.Windows.Forms.Panel panel2; + private System.Windows.Forms.TableLayoutPanel tableLayoutPanel4; + private System.Windows.Forms.GroupBox groupBox5; + private System.Windows.Forms.GroupBox groupBox6; + private System.Windows.Forms.Button button3; + private System.Windows.Forms.GroupBox groupBox7; + private System.Windows.Forms.Button button4; + private System.Windows.Forms.PictureBox pictureBox2; + private System.Windows.Forms.Button button7; + private System.Windows.Forms.CheckBox checkBox1; + private System.Windows.Forms.GroupBox groupBox8; + private System.Windows.Forms.RadioButton radioButton3; + private System.Windows.Forms.RadioButton radioButton2; + private System.Windows.Forms.RadioButton radioButton1; + private System.Windows.Forms.Button button8; + private System.Windows.Forms.ColumnHeader columnHeader9; + private System.Windows.Forms.ColumnHeader columnHeader10; + private System.Windows.Forms.TabPage tabPage3; + private System.Windows.Forms.ToolStrip toolStrip2; + private System.Windows.Forms.ToolStripButton toolStripButton4; + private System.Windows.Forms.PictureBox pictureBox3; + private System.Windows.Forms.ToolStripButton toolStripButton5; + private BrightIdeasSoftware.ObjectListView objectListView1; + private BrightIdeasSoftware.OLVColumn olvColumn1; + private BrightIdeasSoftware.OLVColumn olvColumn2; + private System.Windows.Forms.ToolStripButton toolStripButton6; + private BrightIdeasSoftware.ObjectListView objectListView2; + private BrightIdeasSoftware.OLVColumn olvColumn3; + private BrightIdeasSoftware.OLVColumn olvColumn4; + private BrightIdeasSoftware.OLVColumn olvColumn5; + private System.Windows.Forms.ContextMenuStrip contextMenuStrip4; + private System.Windows.Forms.ToolStripMenuItem clearToolStripMenuItem; + private System.Windows.Forms.ToolStripMenuItem deleteToolStripMenuItem2; + private System.Windows.Forms.TabPage tabPage5; + private System.Windows.Forms.Label label1; + private System.Windows.Forms.LinkLabel linkLabel1; + private System.Windows.Forms.Panel panel3; + private System.Windows.Forms.ToolStrip toolStrip3; + private System.Windows.Forms.ToolStripButton toolStripButton8; + private System.Windows.Forms.ToolStripButton toolStripButton9; + private System.Windows.Forms.TableLayoutPanel tableLayoutPanel7; + private System.Windows.Forms.Panel panel4; + private System.Windows.Forms.Label label2; + private System.Windows.Forms.TextBox textBox6; + private System.Windows.Forms.ToolStripMenuItem quantityToolStripMenuItem; + private System.Windows.Forms.ToolStripMenuItem setToToolStripMenuItem; + private System.Windows.Forms.ToolStripMenuItem multiplyToolStripMenuItem; + private System.Windows.Forms.ToolStripMenuItem divideToolStripMenuItem; + private System.Windows.Forms.CheckBox checkBox5; + private System.Windows.Forms.GroupBox groupBox9; + private System.Windows.Forms.CheckBox checkBox6; + } } diff --git a/DeepNestPort/Form1.cs b/DeepNestPort/Form1.cs index 8ab88b0..c333e7b 100644 --- a/DeepNestPort/Form1.cs +++ b/DeepNestPort/Form1.cs @@ -16,6 +16,8 @@ public partial class Form1 : Form { + bool drawSimplification = true; + public Form1() { InitializeComponent(); @@ -52,6 +54,8 @@ public Form1() this.comboBox1.SelectedItem = SvgNest.Config.PlacementType.ToString(); this.textBox1.Text = SvgNest.Config.Spacing.ToString(); + this.checkBox6.Checked = SvgNest.Config.clipByHull; + UpdateFilesList(@"dxfs"); Load += Form1_Load; } @@ -127,92 +131,57 @@ public SvgNest nest public void RedrawPreview(DrawingContext ctx2, object previewObject) { ctx2.Update(); + ctx2.Clear(Color.White); - ctx2.gr.Clear(Color.White); - - //ctx2.gr.DrawLine(Pens.Blue, ctx2.Transform(new PointF(0, 0)), ctx2.Transform(100, 0)); - //ctx2.gr.DrawLine(Pens.Red, ctx2.Transform(new PointF(0, 0)), ctx2.Transform(0, 100)); + // ctx2.gr.DrawLine(Pens.Blue, ctx2.Transform(new PointF(0, 0)), ctx2.Transform(100, 0)); + // ctx2.gr.DrawLine(Pens.Red, ctx2.Transform(new PointF(0, 0)), ctx2.Transform(0, 100)); + ctx2.Reset(); if (previewObject != null) { - ctx2.gr.ResetTransform(); - GraphicsPath gp = new GraphicsPath(); - GraphicsPath gp2 = new GraphicsPath(); - - if (previewObject is RawDetail raw) - { - DrawOuters(ctx2, gp, gp2, raw); - ApplyInk(ctx2, gp); - AddApproximation(ctx2, raw); - } - else if (previewObject is NFP nfp) + RectangleF bnd; + if (previewObject is RawDetail || previewObject is NFP) { - DrawNfp(ctx2, gp, nfp); - ApplyInk(ctx2, gp); - } - - var bnd = gp2.GetBounds(); + if (previewObject is RawDetail raw) + { + ctx2.Draw(raw, Pens.Black, Brushes.LightBlue); + if (this.drawSimplification) + { + AddApproximation(ctx2, raw); + } - ctx2.gr.ResetTransform(); - var cap = $"{bnd.Width:N2} x {bnd.Height:N2}"; - var ms = ctx2.gr.MeasureString(cap, SystemFonts.DefaultFont); - ctx2.gr.FillRectangle(new SolidBrush(Color.FromArgb(128, Color.LightGreen)), 5, 5, ms.Width, ms.Height); + bnd = raw.BoundingBox(); + } + else + { + var g = ctx2.Draw(previewObject as NFP, Pens.Black, Brushes.LightBlue); + bnd = g.GetBounds(); + } - ctx2.gr.DrawString(cap, SystemFonts.DefaultFont, Brushes.Black, 5, 5); + var cap = $"{bnd.Width:N2} x {bnd.Height:N2}"; + ctx2.DrawLabel(cap, Brushes.Black, Color.LightGreen, 5, 5); + } } ctx2.Setup(); } - private static void ApplyInk(DrawingContext ctx2, GraphicsPath gp) - { - ctx2.gr.FillPath(Brushes.LightBlue, gp); - ctx2.gr.DrawPath(Pens.Black, gp); - } - - private static void DrawOuters(DrawingContext ctx2, GraphicsPath gp, GraphicsPath gp2, RawDetail raw) - { - foreach (var item in raw.Outers) - { - gp.AddPolygon(item.Points.Select(z => ctx2.Transform(z)).ToArray()); - gp2.AddPolygon(item.Points.ToArray()); - } - } - - private static void DrawNfp(DrawingContext ctx2, GraphicsPath gp, NFP nfp) - { - gp.AddPolygon(nfp.Points.Select(z => ctx2.Transform(z.x, z.y)).ToArray()); - if (nfp.Children != null) - { - foreach (var item in nfp.Children) - { - gp.AddPolygon(item.Points.Select(z => ctx2.Transform(z.x, z.y)).ToArray()); - } - } - } - /// /// Display the bounds that will be used by the nesting algorithym. /// - /// Drawing context upon which to draw. + /// Drawing context upon which to draw. /// The part to approximate. - private void AddApproximation(DrawingContext ctx2, RawDetail raw) + private void AddApproximation(DrawingContext ctx, RawDetail raw) { var nestingContext = new NestingContext(new MessageBoxService()); NFP part; nestingContext.TryImportFromRawDetail(raw, 0, out part); - GraphicsPath gp = new GraphicsPath(); - var simplification = SvgNest.simplifyFunction(part, false); - DrawNfp(ctx2, gp, simplification); + ctx.Draw(simplification, Pens.Red); - ctx2.gr.DrawPath(Pens.Red, gp); var pointsChange = $"{part.Points.Length} => {simplification.Points.Length} points"; - var ms = ctx2.gr.MeasureString(pointsChange, SystemFonts.DefaultFont); - var topLeftY = (5 * 2) + ms.Height; - ctx2.gr.FillRectangle(new SolidBrush(Color.FromArgb(128, Color.Orange)), 5, topLeftY, ms.Width, ms.Height); - ctx2.gr.DrawString(pointsChange, SystemFonts.DefaultFont, Brushes.Black, 5, topLeftY); + ctx.DrawLabel(pointsChange, Brushes.Black, Color.Orange, 5, (int)(10 + ctx.GetLabelHeight())); } public void Redraw() @@ -231,9 +200,9 @@ public void Redraw() #endregion ctx.gr.SmoothingMode = SmoothingMode.AntiAlias; - ctx.gr.Clear(Color.White); + ctx.Clear(Color.White); - ctx.gr.ResetTransform(); + ctx.Reset(); ctx.gr.DrawLine(Pens.Red, ctx.Transform(new PointF(0, 0)), ctx.Transform(new PointF(1000, 0))); ctx.gr.DrawLine(Pens.Blue, ctx.Transform(new PointF(0, 0)), ctx.Transform(new PointF(0, 1000))); @@ -336,7 +305,6 @@ public void Redraw() bool was = false; foreach (var zitem in fr.placements.First()) { - var sheetid = zitem.sheetId; if (sheetid != item.Id) continue; var sheet = sheets.FirstOrDefault(z => z.Id == sheetid); @@ -346,7 +314,6 @@ public void Redraw() was = true; foreach (var ssitem in zitem.sheetplacements) { - var poly = polygons.FirstOrDefault(z => z.Id == ssitem.id); if (poly != null) { @@ -372,10 +339,8 @@ public void Redraw() public void RenderSheet() { ctx.gr.SmoothingMode = SmoothingMode.AntiAlias; - ctx.gr.Clear(Color.White); - - ctx.gr.ResetTransform(); - + ctx.Clear(Color.White); + ctx.Reset(); foreach (var item in polygons.Union(sheets)) { @@ -1639,5 +1604,15 @@ private void divideToolStripMenuItem_Click(object sender, EventArgs e) } objectListView1.RefreshObjects(objectListView1.SelectedObjects); } + + private void checkBox5_CheckedChanged(object sender, EventArgs e) + { + drawSimplification = checkBox5.Checked; + } + + private void checkBox6_CheckedChanged(object sender, EventArgs e) + { + SvgNest.Config.clipByHull = checkBox6.Checked; + } } } \ No newline at end of file diff --git a/DeepNestPort/Form1.resx b/DeepNestPort/Form1.resx index 49f3f4c..f09bb5e 100644 --- a/DeepNestPort/Form1.resx +++ b/DeepNestPort/Form1.resx @@ -120,6 +120,9 @@ 1155, 17 + + 1155, 17 + @@ -151,6 +154,9 @@ TgDQASA1MVpwzwAAAABJRU5ErkJggg== + + 895, 17 + 1000, 17