From 26558ed3eddd7fc39d29761c7c584457e2799438 Mon Sep 17 00:00:00 2001 From: Anton Kerezov Date: Fri, 22 Oct 2021 16:02:28 +0900 Subject: [PATCH] Added: Double click to open explorer and file name indication as output --- Component/ScriptParasiteComponent.cs | 72 ++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) diff --git a/Component/ScriptParasiteComponent.cs b/Component/ScriptParasiteComponent.cs index ddcd365..bed49e7 100644 --- a/Component/ScriptParasiteComponent.cs +++ b/Component/ScriptParasiteComponent.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Diagnostics; using System.Drawing; using System.IO; using System.Linq; @@ -67,6 +68,15 @@ protected string FileNameSafe return Path.Combine($"{Folder}",$"{name}-{componentId}.cs"); } } + protected string FileNameOnly + { + get + { + var name = Regex.Replace(TargetComponent.NickName, @"\W", "_"); + var componentId = TargetComponent.InstanceGuid.ToString().Replace(" - ", "").Substring(0, 5); + return $"{name}-{componentId}.cs"; + } + } protected Component_CSNET_Script TargetComponent { get; set; } @@ -90,6 +100,7 @@ private static string ReadDefaultFolder() protected override void RegisterOutputParams(GH_OutputParamManager pManager) { + pManager.AddTextParameter("File", "F", "The file that corresponds to the scrip being monitored", GH_ParamAccess.item); } private int _iteration; @@ -173,6 +184,9 @@ protected override void SolveInstance(IGH_DataAccess da) EnsureProject(Path.Combine(directory, "GrasshopperScripts.csproj")); EnsureEditorConfig(Path.Combine(directory, ".editorconfig")); + + //show which file is used for the selected script + da.SetData(0, FileNameOnly); } catch (Exception ex) { @@ -630,6 +644,64 @@ public void EnsureProject(string file) }; File.WriteAllText(file, output.TransformText()); } + + public override void CreateAttributes() + { + m_attributes = new DetectDoubleClick(this); + } + + public void OpenFileBrowesr() + { + OperatingSystem os = Environment.OSVersion; + PlatformID pid = os.Platform; + switch (pid) + { + case PlatformID.Win32NT: + case PlatformID.Win32S: + case PlatformID.Win32Windows: + case PlatformID.WinCE: + //"I'm on windows!" + if (Directory.Exists(Folder)) + { + ProcessStartInfo startInfo = new ProcessStartInfo + { + Arguments = Folder, + FileName = "explorer.exe" + }; + + Process.Start(startInfo); + } + else + { + System.Windows.Forms.MessageBox.Show($"{0} directory does not exist! Double click cannot open explorer."); + } + break; + case PlatformID.Unix: + //"I'm a linux box!" + break; + case PlatformID.MacOSX: + //"I'm a mac!" + break; + default: + AddRuntimeMessage(GH_RuntimeMessageLevel.Remark, "Unknonw OS. Double click to open is disabled"); + break; + } + } + + public class DetectDoubleClick : Grasshopper.Kernel.Attributes.GH_ComponentAttributes + { + public DetectDoubleClick(IGH_Component component) : base(component) + { + } + + public override GH_ObjectResponse RespondToMouseDoubleClick(GH_Canvas sender, GH_CanvasMouseEvent e) + { + (Owner as ScriptParasiteComponent)?.OpenFileBrowesr(); + return GH_ObjectResponse.Handled; + } + } + + } public class TimeoutException : Exception