diff --git a/OpenNest.Core/Shapes/CircleShape.cs b/OpenNest.Core/Shapes/CircleShape.cs index 1375abc..89fe138 100644 --- a/OpenNest.Core/Shapes/CircleShape.cs +++ b/OpenNest.Core/Shapes/CircleShape.cs @@ -7,6 +7,8 @@ namespace OpenNest.Shapes { public double Diameter { get; set; } + public override string GenerateName() => $"Circle {Dim(Diameter)} Dia"; + public override void SetPreviewDefaults() { Diameter = 8; diff --git a/OpenNest.Core/Shapes/IsoscelesTriangleShape.cs b/OpenNest.Core/Shapes/IsoscelesTriangleShape.cs index 499f55f..5771830 100644 --- a/OpenNest.Core/Shapes/IsoscelesTriangleShape.cs +++ b/OpenNest.Core/Shapes/IsoscelesTriangleShape.cs @@ -8,6 +8,8 @@ namespace OpenNest.Shapes public double Base { get; set; } public double Height { get; set; } + public override string GenerateName() => $"Isosceles Triangle {Dim(Base)}x{Dim(Height)}"; + public override void SetPreviewDefaults() { Base = 8; diff --git a/OpenNest.Core/Shapes/LShape.cs b/OpenNest.Core/Shapes/LShape.cs index c91c9fd..9e54ffb 100644 --- a/OpenNest.Core/Shapes/LShape.cs +++ b/OpenNest.Core/Shapes/LShape.cs @@ -10,6 +10,8 @@ namespace OpenNest.Shapes public double LegWidth { get; set; } public double LegHeight { get; set; } + public override string GenerateName() => $"L {Dim(Width)}x{Dim(Height)}"; + public override void SetPreviewDefaults() { Width = 8; diff --git a/OpenNest.Core/Shapes/NgonShape.cs b/OpenNest.Core/Shapes/NgonShape.cs index ee631c7..55238c1 100644 --- a/OpenNest.Core/Shapes/NgonShape.cs +++ b/OpenNest.Core/Shapes/NgonShape.cs @@ -8,6 +8,8 @@ namespace OpenNest.Shapes public int Sides { get; set; } public double Width { get; set; } + public override string GenerateName() => $"{Sides}-Sided Polygon {Dim(Width)}"; + public override void SetPreviewDefaults() { Sides = 8; diff --git a/OpenNest.Core/Shapes/PipeFlangeShape.cs b/OpenNest.Core/Shapes/PipeFlangeShape.cs index 7c5de3d..980fd79 100644 --- a/OpenNest.Core/Shapes/PipeFlangeShape.cs +++ b/OpenNest.Core/Shapes/PipeFlangeShape.cs @@ -13,6 +13,14 @@ namespace OpenNest.Shapes public double PipeClearance { get; set; } public bool Blind { get; set; } + public override string GenerateName() + { + var name = $"Pipe Flange {Dim(OD)} OD"; + if (!string.IsNullOrEmpty(PipeSize)) + name += $" {PipeSize} Pipe"; + return name; + } + public override void SetPreviewDefaults() { OD = 7.5; diff --git a/OpenNest.Core/Shapes/RectangleShape.cs b/OpenNest.Core/Shapes/RectangleShape.cs index dc7b2e6..5914dc8 100644 --- a/OpenNest.Core/Shapes/RectangleShape.cs +++ b/OpenNest.Core/Shapes/RectangleShape.cs @@ -8,6 +8,8 @@ namespace OpenNest.Shapes public double Length { get; set; } public double Width { get; set; } + public override string GenerateName() => $"Rectangle {Dim(Length)}x{Dim(Width)}"; + public override void SetPreviewDefaults() { Length = 12; diff --git a/OpenNest.Core/Shapes/RightTriangleShape.cs b/OpenNest.Core/Shapes/RightTriangleShape.cs index 7fef22b..7aec564 100644 --- a/OpenNest.Core/Shapes/RightTriangleShape.cs +++ b/OpenNest.Core/Shapes/RightTriangleShape.cs @@ -8,6 +8,8 @@ namespace OpenNest.Shapes public double Width { get; set; } public double Height { get; set; } + public override string GenerateName() => $"Right Triangle {Dim(Width)}x{Dim(Height)}"; + public override void SetPreviewDefaults() { Width = 8; diff --git a/OpenNest.Core/Shapes/RingShape.cs b/OpenNest.Core/Shapes/RingShape.cs index 991ba4d..ba60ce8 100644 --- a/OpenNest.Core/Shapes/RingShape.cs +++ b/OpenNest.Core/Shapes/RingShape.cs @@ -8,6 +8,8 @@ namespace OpenNest.Shapes public double OuterDiameter { get; set; } public double InnerDiameter { get; set; } + public override string GenerateName() => $"Ring {Dim(OuterDiameter)}x{Dim(InnerDiameter)}"; + public override void SetPreviewDefaults() { OuterDiameter = 10; diff --git a/OpenNest.Core/Shapes/RoundedRectangleShape.cs b/OpenNest.Core/Shapes/RoundedRectangleShape.cs index dc5314c..0a61887 100644 --- a/OpenNest.Core/Shapes/RoundedRectangleShape.cs +++ b/OpenNest.Core/Shapes/RoundedRectangleShape.cs @@ -10,6 +10,8 @@ namespace OpenNest.Shapes public double Width { get; set; } public double Radius { get; set; } + public override string GenerateName() => $"Rounded Rectangle {Dim(Length)}x{Dim(Width)} R{Dim(Radius)}"; + public override void SetPreviewDefaults() { Length = 12; diff --git a/OpenNest.Core/Shapes/ShapeDefinition.cs b/OpenNest.Core/Shapes/ShapeDefinition.cs index f078ba5..3aef511 100644 --- a/OpenNest.Core/Shapes/ShapeDefinition.cs +++ b/OpenNest.Core/Shapes/ShapeDefinition.cs @@ -26,6 +26,14 @@ namespace OpenNest.Shapes public abstract Drawing GetDrawing(); + public virtual string GenerateName() + { + var typeName = GetType().Name; + return typeName.EndsWith("Shape") + ? typeName.Substring(0, typeName.Length - 5) + : typeName; + } + public virtual void SetPreviewDefaults() { } public static List LoadFromJson(string path) where T : ShapeDefinition @@ -34,6 +42,8 @@ namespace OpenNest.Shapes return JsonSerializer.Deserialize>(json, JsonOptions); } + protected static string Dim(double value) => value.ToString("0.###"); + protected Drawing CreateDrawing(List entities) { var pgm = ConvertGeometry.ToProgram(entities); diff --git a/OpenNest.Core/Shapes/TShape.cs b/OpenNest.Core/Shapes/TShape.cs index d889479..8a8bfee 100644 --- a/OpenNest.Core/Shapes/TShape.cs +++ b/OpenNest.Core/Shapes/TShape.cs @@ -10,6 +10,8 @@ namespace OpenNest.Shapes public double StemWidth { get; set; } public double BarHeight { get; set; } + public override string GenerateName() => $"T {Dim(Width)}x{Dim(Height)}"; + public override void SetPreviewDefaults() { Width = 10; diff --git a/OpenNest.Core/Shapes/TrapezoidShape.cs b/OpenNest.Core/Shapes/TrapezoidShape.cs index 7414bc6..660c9be 100644 --- a/OpenNest.Core/Shapes/TrapezoidShape.cs +++ b/OpenNest.Core/Shapes/TrapezoidShape.cs @@ -9,6 +9,8 @@ namespace OpenNest.Shapes public double BottomWidth { get; set; } public double Height { get; set; } + public override string GenerateName() => $"Trapezoid {Dim(TopWidth)}x{Dim(BottomWidth)}x{Dim(Height)}"; + public override void SetPreviewDefaults() { TopWidth = 6; diff --git a/OpenNest/Forms/EditNestForm.Designer.cs b/OpenNest/Forms/EditNestForm.Designer.cs index 479b0c5..03449da 100644 --- a/OpenNest/Forms/EditNestForm.Designer.cs +++ b/OpenNest/Forms/EditNestForm.Designer.cs @@ -47,11 +47,9 @@ drawingListBox1 = new OpenNest.Controls.DrawingListBox(); toolStrip2 = new System.Windows.Forms.ToolStrip(); toolStripButton2 = new System.Windows.Forms.ToolStripButton(); - toolStripSeparator4 = new System.Windows.Forms.ToolStripSeparator(); + shapeLibraryButton = new System.Windows.Forms.ToolStripButton(); editDrawingsButton = new System.Windows.Forms.ToolStripButton(); - toolStripSeparator1 = new System.Windows.Forms.ToolStripSeparator(); toolStripButton3 = new System.Windows.Forms.ToolStripButton(); - toolStripSeparator2 = new System.Windows.Forms.ToolStripSeparator(); hideNestedButton = new System.Windows.Forms.ToolStripButton(); ((System.ComponentModel.ISupportInitialize)splitContainer).BeginInit(); splitContainer.Panel1.SuspendLayout(); @@ -219,7 +217,7 @@ // toolStrip2.GripStyle = System.Windows.Forms.ToolStripGripStyle.Hidden; toolStrip2.ImageScalingSize = new System.Drawing.Size(20, 20); - toolStrip2.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { toolStripButton2, toolStripSeparator4, editDrawingsButton, toolStripSeparator1, toolStripButton3, toolStripSeparator2, hideNestedButton }); + toolStrip2.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { toolStripButton2, shapeLibraryButton, editDrawingsButton, toolStripButton3, hideNestedButton }); toolStrip2.Location = new System.Drawing.Point(4, 3); toolStrip2.Name = "toolStrip2"; toolStrip2.Size = new System.Drawing.Size(265, 27); @@ -237,14 +235,19 @@ toolStripButton2.Size = new System.Drawing.Size(34, 24); toolStripButton2.Text = "Import Drawings"; toolStripButton2.Click += ImportDrawings_Click; - // - // toolStripSeparator4 - // - toolStripSeparator4.Name = "toolStripSeparator4"; - toolStripSeparator4.Size = new System.Drawing.Size(6, 27); - // + // + // shapeLibraryButton + // + shapeLibraryButton.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image; + shapeLibraryButton.Image = Properties.Resources.shapes; + shapeLibraryButton.Name = "shapeLibraryButton"; + shapeLibraryButton.Padding = new System.Windows.Forms.Padding(5, 0, 5, 0); + shapeLibraryButton.Size = new System.Drawing.Size(34, 24); + shapeLibraryButton.Text = "Shape Library"; + shapeLibraryButton.Click += ShapeLibrary_Click; + // // editDrawingsButton - // + // editDrawingsButton.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image; editDrawingsButton.Image = (System.Drawing.Image)resources.GetObject("editDrawingsButton.Image"); editDrawingsButton.Name = "editDrawingsButton"; @@ -252,14 +255,9 @@ editDrawingsButton.Size = new System.Drawing.Size(34, 24); editDrawingsButton.Text = "Edit Drawings in Converter"; editDrawingsButton.Click += EditDrawingsInConverter_Click; - // - // toolStripSeparator1 - // - toolStripSeparator1.Name = "toolStripSeparator1"; - toolStripSeparator1.Size = new System.Drawing.Size(6, 27); - // + // // toolStripButton3 - // + // toolStripButton3.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image; toolStripButton3.Image = (System.Drawing.Image)resources.GetObject("toolStripButton3.Image"); toolStripButton3.Name = "toolStripButton3"; @@ -268,12 +266,7 @@ toolStripButton3.Size = new System.Drawing.Size(34, 24); toolStripButton3.Text = "Cleanup unused Drawings"; toolStripButton3.Click += CleanUnusedDrawings_Click; - // - // toolStripSeparator2 - // - toolStripSeparator2.Name = "toolStripSeparator2"; - toolStripSeparator2.Size = new System.Drawing.Size(6, 27); - // + // // hideNestedButton // hideNestedButton.CheckOnClick = true; @@ -329,11 +322,9 @@ private System.Windows.Forms.ColumnHeader utilColumn; private System.Windows.Forms.ToolStrip toolStrip2; private System.Windows.Forms.ToolStripButton toolStripButton2; - private System.Windows.Forms.ToolStripSeparator toolStripSeparator4; + private System.Windows.Forms.ToolStripButton shapeLibraryButton; private System.Windows.Forms.ToolStripButton editDrawingsButton; - private System.Windows.Forms.ToolStripSeparator toolStripSeparator1; private System.Windows.Forms.ToolStripButton toolStripButton3; - private System.Windows.Forms.ToolStripSeparator toolStripSeparator2; private System.Windows.Forms.ToolStripButton hideNestedButton; private System.Windows.Forms.ToolStripSeparator toolStripSeparator3; private System.Windows.Forms.ToolStripButton toolStripLabel1; diff --git a/OpenNest/Forms/EditNestForm.cs b/OpenNest/Forms/EditNestForm.cs index f26abcc..1e68e2e 100644 --- a/OpenNest/Forms/EditNestForm.cs +++ b/OpenNest/Forms/EditNestForm.cs @@ -875,6 +875,18 @@ namespace OpenNest.Forms Import(); } + private void ShapeLibrary_Click(object sender, EventArgs e) + { + var form = new ShapeLibraryForm(Nest.Drawings.Select(d => d.Name)); + form.ShowDialog(); + + var drawings = form.GetDrawings(); + if (drawings.Count == 0) return; + + drawings.ForEach(d => Nest.Drawings.Add(d)); + UpdateDrawingList(); + } + private void EditDrawingsInConverter_Click(object sender, EventArgs e) { if (Nest.Drawings.Count == 0) diff --git a/OpenNest/Forms/MainForm.cs b/OpenNest/Forms/MainForm.cs index 566eae4..09f8e51 100644 --- a/OpenNest/Forms/MainForm.cs +++ b/OpenNest/Forms/MainForm.cs @@ -837,7 +837,7 @@ namespace OpenNest.Forms { if (activeForm == null) return; - var form = new ShapeLibraryForm(); + var form = new ShapeLibraryForm(activeForm.Nest.Drawings.Select(d => d.Name)); form.ShowDialog(); var drawings = form.GetDrawings(); diff --git a/OpenNest/Forms/ShapeLibraryForm.cs b/OpenNest/Forms/ShapeLibraryForm.cs index 3c9e6bd..123ab7f 100644 --- a/OpenNest/Forms/ShapeLibraryForm.cs +++ b/OpenNest/Forms/ShapeLibraryForm.cs @@ -21,12 +21,17 @@ namespace OpenNest.Forms private readonly List addedDrawings = new List(); private readonly List shapeEntries = new List(); private readonly List parameterBindings = new List(); + private readonly HashSet existingNames; private ShapeEntry selectedEntry; private bool suppressPreview; - public ShapeLibraryForm() + public ShapeLibraryForm(IEnumerable existingDrawingNames = null) { + existingNames = existingDrawingNames != null + ? new HashSet(existingDrawingNames, StringComparer.OrdinalIgnoreCase) + : new HashSet(StringComparer.OrdinalIgnoreCase); + InitializeComponent(); DiscoverShapes(); PopulateShapeList(); @@ -259,6 +264,7 @@ namespace OpenNest.Forms if (shape == null) return; var drawing = shape.GetDrawing(); + nameTextBox.Text = shape.GenerateName(); previewBox.ShowDrawing(drawing); if (drawing?.Program != null) @@ -405,10 +411,12 @@ namespace OpenNest.Forms if (shape == null) return; var drawing = shape.GetDrawing(); + drawing.Name = GetUniqueName(drawing.Name); drawing.Color = Drawing.GetNextColor(); drawing.Quantity.Required = (int)quantityUpDown.Value; addedDrawings.Add(drawing); + existingNames.Add(drawing.Name); DialogResult = DialogResult.OK; addButton.Text = $"Added ({addedDrawings.Count})"; @@ -423,6 +431,19 @@ namespace OpenNest.Forms } } + private string GetUniqueName(string baseName) + { + if (!existingNames.Contains(baseName)) + return baseName; + + for (var i = 2; ; i++) + { + var candidate = $"{baseName} ({i})"; + if (!existingNames.Contains(candidate)) + return candidate; + } + } + private static string FriendlyName(string name) { if (name.EndsWith("Shape")) diff --git a/OpenNest/Properties/Resources.Designer.cs b/OpenNest/Properties/Resources.Designer.cs index 6873a6e..099fce3 100644 --- a/OpenNest/Properties/Resources.Designer.cs +++ b/OpenNest/Properties/Resources.Designer.cs @@ -249,7 +249,17 @@ namespace OpenNest.Properties { return ((System.Drawing.Bitmap)(obj)); } } - + + /// + /// Looks up a localized resource of type System.Drawing.Bitmap. + /// + internal static System.Drawing.Bitmap shapes { + get { + object obj = ResourceManager.GetObject("shapes", resourceCulture); + return ((System.Drawing.Bitmap)(obj)); + } + } + /// /// Looks up a localized resource of type System.Drawing.Bitmap. /// diff --git a/OpenNest/Properties/Resources.resx b/OpenNest/Properties/Resources.resx index dbaeb93..327c346 100644 --- a/OpenNest/Properties/Resources.resx +++ b/OpenNest/Properties/Resources.resx @@ -187,4 +187,7 @@ ..\Resources\delete.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a + + ..\Resources\shapes.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a + \ No newline at end of file diff --git a/OpenNest/Resources/shapes.png b/OpenNest/Resources/shapes.png new file mode 100644 index 0000000..c31e38a Binary files /dev/null and b/OpenNest/Resources/shapes.png differ