From cf25a4c260e857d03320a99e2b1bcd8e86d2c00c Mon Sep 17 00:00:00 2001 From: pawelsapiecha Date: Sat, 15 Nov 2025 15:28:50 -0800 Subject: [PATCH 1/4] preview update --- list to preview.cs | 85 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 list to preview.cs diff --git a/list to preview.cs b/list to preview.cs new file mode 100644 index 0000000..3bd2fc2 --- /dev/null +++ b/list to preview.cs @@ -0,0 +1,85 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Windows.Forms; +using Rhino; +using Rhino.UI; + +public class GrasshopperFilePreview +{ + public static void Run() + { + string folderPath; + using (var folderDialog = new FolderBrowserDialog()) + { + folderDialog.Description = "Select Folder with Grasshopper Files"; + if (folderDialog.ShowDialog() != DialogResult.OK) + { + RhinoApp.WriteLine("No folder selected. Exiting script."); + return; + } + folderPath = folderDialog.SelectedPath; + } + + if (string.IsNullOrEmpty(folderPath)) + { + RhinoApp.WriteLine("No folder selected. Exiting script."); + return; + } + + var ghFiles = Directory.GetFiles(folderPath, "*.gh") + .Concat(Directory.GetFiles(folderPath, "*.ghx")) + .ToList(); + + if (ghFiles.Count == 0) + { + RhinoApp.WriteLine("No Grasshopper files (.gh, .ghx) found in the selected folder."); + return; + } + + RhinoApp.WriteLine("Grasshopper files found:"); + for (int i = 0; i < ghFiles.Count; i++) + { + RhinoApp.WriteLine($"{i + 1}: {Path.GetFileName(ghFiles[i])}"); + } + + int fileIndex = -1; + var rc = Rhino.Input.RhinoGet.GetInteger("Enter the number of the file to preview:", false, ref fileIndex); + + if (rc != Rhino.Commands.Result.Success || fileIndex < 1 || fileIndex > ghFiles.Count) + { + RhinoApp.WriteLine("--- Debug Info ---"); + RhinoApp.WriteLine($"Input result: {rc}"); + RhinoApp.WriteLine($"Entered number: {fileIndex}"); + RhinoApp.WriteLine($"Number of files found: {ghFiles.Count}"); + RhinoApp.WriteLine($"Valid range is 1 to {ghFiles.Count}"); + RhinoApp.WriteLine("--------------------"); + RhinoApp.WriteLine("Invalid selection. Exiting script."); + return; + } + + string selectedFile = ghFiles[fileIndex - 1]; + OpenFile(selectedFile); + } + + private static void OpenFile(string filePath) + { + if (!File.Exists(filePath)) + { + RhinoApp.WriteLine($"File not found: {filePath}"); + return; + } + + // This command should open the file, which will also open the Grasshopper editor window. + string cmd = $"-_Grasshopper _Document _Open \"{filePath}\" _Enter"; + RhinoApp.RunScript(cmd, true); + + RhinoApp.WriteLine($"Sent command to open: {Path.GetFileName(filePath)}"); + } +} + +// To run this script in Rhino, you would typically call: +// GrasshopperFilePreview.Run(); + +GrasshopperFilePreview.Run(); From a63c4371794104d6e2c0dbb2cc97d47860829bad Mon Sep 17 00:00:00 2001 From: pawelsapiecha Date: Sat, 15 Nov 2025 16:54:49 -0800 Subject: [PATCH 2/4] update --- list to preview.cs | 127 ++++++++++++++++++++++++++++++--------------- 1 file changed, 84 insertions(+), 43 deletions(-) diff --git a/list to preview.cs b/list to preview.cs index 3bd2fc2..ed684ad 100644 --- a/list to preview.cs +++ b/list to preview.cs @@ -1,32 +1,28 @@ +// +// !-r "Eto.dll" +// !-r "Rhino.UI.dll" + using System; using System.Collections.Generic; using System.IO; using System.Linq; -using System.Windows.Forms; using Rhino; -using Rhino.UI; +using Eto.Forms; +using Eto.Drawing; +using Grasshopper; +using Grasshopper.Kernel; -public class GrasshopperFilePreview +public class GrasshopperFileLister { public static void Run() { - string folderPath; - using (var folderDialog = new FolderBrowserDialog()) - { - folderDialog.Description = "Select Folder with Grasshopper Files"; - if (folderDialog.ShowDialog() != DialogResult.OK) - { - RhinoApp.WriteLine("No folder selected. Exiting script."); - return; - } - folderPath = folderDialog.SelectedPath; - } - - if (string.IsNullOrEmpty(folderPath)) + var folderDialog = new SelectFolderDialog(); + if (folderDialog.ShowDialog(Rhino.UI.RhinoEtoApp.MainWindow) != DialogResult.Ok) { RhinoApp.WriteLine("No folder selected. Exiting script."); return; } + string folderPath = folderDialog.Directory; var ghFiles = Directory.GetFiles(folderPath, "*.gh") .Concat(Directory.GetFiles(folderPath, "*.ghx")) @@ -38,29 +34,15 @@ public static void Run() return; } - RhinoApp.WriteLine("Grasshopper files found:"); - for (int i = 0; i < ghFiles.Count; i++) - { - RhinoApp.WriteLine($"{i + 1}: {Path.GetFileName(ghFiles[i])}"); - } - - int fileIndex = -1; - var rc = Rhino.Input.RhinoGet.GetInteger("Enter the number of the file to preview:", false, ref fileIndex); + var etoForm = new EtoFileSelector(ghFiles); + etoForm.ShowModal(Rhino.UI.RhinoEtoApp.MainWindow); + + string selectedFile = etoForm.SelectedFile; - if (rc != Rhino.Commands.Result.Success || fileIndex < 1 || fileIndex > ghFiles.Count) + if (!string.IsNullOrEmpty(selectedFile)) { - RhinoApp.WriteLine("--- Debug Info ---"); - RhinoApp.WriteLine($"Input result: {rc}"); - RhinoApp.WriteLine($"Entered number: {fileIndex}"); - RhinoApp.WriteLine($"Number of files found: {ghFiles.Count}"); - RhinoApp.WriteLine($"Valid range is 1 to {ghFiles.Count}"); - RhinoApp.WriteLine("--------------------"); - RhinoApp.WriteLine("Invalid selection. Exiting script."); - return; + OpenFile(selectedFile); } - - string selectedFile = ghFiles[fileIndex - 1]; - OpenFile(selectedFile); } private static void OpenFile(string filePath) @@ -70,16 +52,75 @@ private static void OpenFile(string filePath) RhinoApp.WriteLine($"File not found: {filePath}"); return; } + + var io = new Grasshopper.Kernel.GH_DocumentIO(filePath); + if (io.Open()) + { + var doc = io.Document; + if (doc != null) + { + // Add the new document to the server. This makes it appear in the GH window. + Grasshopper.Instances.DocumentServer.AddDocument(doc); + RhinoApp.WriteLine($"Successfully opened: {Path.GetFileName(filePath)}"); + return; + } + } + + RhinoApp.WriteLine($"Failed to open: {Path.GetFileName(filePath)}"); + } +} - // This command should open the file, which will also open the Grasshopper editor window. - string cmd = $"-_Grasshopper _Document _Open \"{filePath}\" _Enter"; - RhinoApp.RunScript(cmd, true); +public class EtoFileSelector : Dialog +{ + public string SelectedFile { get; private set; } - RhinoApp.WriteLine($"Sent command to open: {Path.GetFileName(filePath)}"); + public EtoFileSelector(List files) + { + Title = "Select a Grasshopper File"; + Padding = new Padding(10); + + var listBox = new ListBox(); + files.ForEach(f => listBox.Items.Add(Path.GetFileName(f))); + + var openButton = new Button { Text = "Open" }; + openButton.Click += (sender, e) => + { + if (listBox.SelectedIndex != -1) + { + SelectedFile = files[listBox.SelectedIndex]; + Close(); + } + else + { + MessageBox.Show("Please select a file.", MessageBoxType.Information); + } + }; + + var cancelButton = new Button { Text = "Cancel" }; + cancelButton.Click += (sender, e) => + { + SelectedFile = null; + Close(); + }; + + Content = new StackLayout + { + Spacing = 5, + Items = + { + new Label { Text = "Please select a file to open:" }, + listBox, + } + }; + + PositiveButtons.Add(openButton); + NegativeButtons.Add(cancelButton); + + // This is needed to handle button clicks from Positive/Negative buttons + this.DefaultButton.Click += (s, e) => openButton.PerformClick(); } } -// To run this script in Rhino, you would typically call: -// GrasshopperFilePreview.Run(); -GrasshopperFilePreview.Run(); +// To run this script in Rhino, you would typically call: +GrasshopperFileLister.Run(); From 2ac7fa108f48a79a0310ced8dafebde1d49a5181 Mon Sep 17 00:00:00 2001 From: pawelsapiecha Date: Sat, 15 Nov 2025 17:16:35 -0800 Subject: [PATCH 3/4] update --- list to preview.cs | 61 +++++++++++++++++----------------------------- 1 file changed, 23 insertions(+), 38 deletions(-) diff --git a/list to preview.cs b/list to preview.cs index ed684ad..85d955b 100644 --- a/list to preview.cs +++ b/list to preview.cs @@ -36,44 +36,11 @@ public static void Run() var etoForm = new EtoFileSelector(ghFiles); etoForm.ShowModal(Rhino.UI.RhinoEtoApp.MainWindow); - - string selectedFile = etoForm.SelectedFile; - - if (!string.IsNullOrEmpty(selectedFile)) - { - OpenFile(selectedFile); - } - } - - private static void OpenFile(string filePath) - { - if (!File.Exists(filePath)) - { - RhinoApp.WriteLine($"File not found: {filePath}"); - return; - } - - var io = new Grasshopper.Kernel.GH_DocumentIO(filePath); - if (io.Open()) - { - var doc = io.Document; - if (doc != null) - { - // Add the new document to the server. This makes it appear in the GH window. - Grasshopper.Instances.DocumentServer.AddDocument(doc); - RhinoApp.WriteLine($"Successfully opened: {Path.GetFileName(filePath)}"); - return; - } - } - - RhinoApp.WriteLine($"Failed to open: {Path.GetFileName(filePath)}"); } } -public class EtoFileSelector : Dialog +public class EtoFileSelector : Dialog { - public string SelectedFile { get; private set; } - public EtoFileSelector(List files) { Title = "Select a Grasshopper File"; @@ -87,7 +54,8 @@ public EtoFileSelector(List files) { if (listBox.SelectedIndex != -1) { - SelectedFile = files[listBox.SelectedIndex]; + string selectedFilePath = files[listBox.SelectedIndex]; + OpenFile(selectedFilePath); Close(); } else @@ -99,7 +67,6 @@ public EtoFileSelector(List files) var cancelButton = new Button { Text = "Cancel" }; cancelButton.Click += (sender, e) => { - SelectedFile = null; Close(); }; @@ -116,11 +83,29 @@ public EtoFileSelector(List files) PositiveButtons.Add(openButton); NegativeButtons.Add(cancelButton); - // This is needed to handle button clicks from Positive/Negative buttons - this.DefaultButton.Click += (s, e) => openButton.PerformClick(); + DefaultButton = openButton; + AbortButton = cancelButton; + } + +private void OpenFile(string filePath) +{ + if (!File.Exists(filePath)) + { + Rhino.RhinoApp.WriteLine($"File not found: {filePath}"); + return; } + + // Load Grasshopper if not already running + Rhino.RhinoApp.RunScript("_Grasshopper _Load", false); + + // Open the file via the command-line API + string cmd = $"-_Grasshopper _Open \"{filePath}\" _Enter"; + Rhino.RhinoApp.RunScript(cmd, false); + + Rhino.RhinoApp.WriteLine($"Opened: {Path.GetFileName(filePath)}"); } +} // To run this script in Rhino, you would typically call: GrasshopperFileLister.Run(); From 085ae96dfe4ba83ab7b0586415401c894f510076 Mon Sep 17 00:00:00 2001 From: pawelsapiecha Date: Sat, 15 Nov 2025 17:25:57 -0800 Subject: [PATCH 4/4] update --- list to preview.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/list to preview.cs b/list to preview.cs index 85d955b..d343489 100644 --- a/list to preview.cs +++ b/list to preview.cs @@ -98,8 +98,8 @@ private void OpenFile(string filePath) // Load Grasshopper if not already running Rhino.RhinoApp.RunScript("_Grasshopper _Load", false); - // Open the file via the command-line API - string cmd = $"-_Grasshopper _Open \"{filePath}\" _Enter"; + // Open the file via the command-line API, under the Document menu + string cmd = $"-_Grasshopper _Document _Open \"{filePath}\" _Enter"; Rhino.RhinoApp.RunScript(cmd, false); Rhino.RhinoApp.WriteLine($"Opened: {Path.GetFileName(filePath)}");