diff --git a/Directory.Build.props b/Directory.Build.props index 2c74759..086f11d 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -2,7 +2,7 @@ Robots - 1.6.7 + 1.7.0 Robots Authors Create and simulate ABB, KUKA, UR, and Staubli robot programs. Robots;ABB;KUKA;UR;Staubli;Robotics diff --git a/RELEASE b/RELEASE index ea8f5aa..9571894 100644 --- a/RELEASE +++ b/RELEASE @@ -6,6 +6,7 @@ - Added persistance to Joints parameter. - Added support for Fanuc (by a third party contributor). - Added deconstruct Toolpath component. + - Added ability to change folder path for local libraries. - version: 1.6.7 changes: diff --git a/src/Robots.Grasshopper/RobotSystem/LibraryForm.cs b/src/Robots.Grasshopper/RobotSystem/LibraryForm.cs index 7cc26eb..aecfde0 100644 --- a/src/Robots.Grasshopper/RobotSystem/LibraryForm.cs +++ b/src/Robots.Grasshopper/RobotSystem/LibraryForm.cs @@ -79,6 +79,22 @@ public LibraryForm(OnlineLibrary library) _grid.SelectedRowsChanged += (s, e) => _detailView.DataContext = _grid.SelectedItem; } + async Task ChangeLocalPathAsync() + { + var settings = Settings.Load(); + SelectFolderDialog dialog = new() + { + Title = "Select folder for local robot libraries", + Directory = settings.LocalLibraryPath, + }; + + if (dialog.ShowDialog(this) != DialogResult.Ok) + return; + + Settings.Save(settings with { LocalLibraryPath = dialog.Directory }); + await RefreshAsync(); + } + async Task RefreshAsync() { try @@ -183,6 +199,7 @@ async Task DownloadAsync() Items = { new StackLayoutItem(NewAsyncButton(RefreshAsync, label: "Refresh", runOnce: true), true), + new StackLayoutItem(NewAsyncButton(ChangeLocalPathAsync, label: "Set local folder"), false), new LinkButton { Text = "Help", diff --git a/src/Robots/IO/FileIO.cs b/src/Robots/IO/FileIO.cs index 9f5a9ec..84b20ca 100644 --- a/src/Robots/IO/FileIO.cs +++ b/src/Robots/IO/FileIO.cs @@ -1,6 +1,5 @@ using System.Xml; using System.Xml.Linq; -using Rhino; using Rhino.FileIO; using Rhino.Geometry; @@ -55,33 +54,14 @@ public static Frame LoadFrame(string name) // library files /// - /// Win: C:\Users\userName\Documents\Robots - /// Mac: /Users/userName/Robots + /// Default Win: C:\Users\userName\Documents\Robots + /// Default Mac: /Users/userName/Robots /// public static string LocalLibraryPath => - Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "Robots"); + Settings.Load().LocalLibraryPath; - /// - /// Win: C:\Users\userName\AppData\Roaming\McNeel\Rhinoceros\packages\7.0\Robots\libraries - /// Mac: /Users/userName/.config/McNeel/Rhinoceros/packages/7.0/Robots/libraries - /// Lib: {appData}\Robots\libraries - /// - public static string OnlineLibraryPath - { - get - { - var appData = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData, Environment.SpecialFolderOption.DoNotVerify); - int version = 7; -#if (NET48) - version = RhinoApp.Version.Major; -#endif -#if (NET48 || DEBUG) - return Path.Combine(appData, "McNeel", "Rhinoceros", "packages", $"{version:0.0}", "Robots", "libraries"); -#elif NETSTANDARD2_0 - return Path.Combine(appData, "Robots", "libraries"); -#endif - } - } + public static string OnlineLibraryPath => + Path.Combine(Settings.PluginPath, "libraries"); static IEnumerable GetLibraryPaths() { diff --git a/src/Robots/IO/Settings.cs b/src/Robots/IO/Settings.cs new file mode 100644 index 0000000..61fa0d4 --- /dev/null +++ b/src/Robots/IO/Settings.cs @@ -0,0 +1,56 @@ +using Newtonsoft.Json; + +namespace Robots; + +public record Settings(string LocalLibraryPath) +{ + /// + /// Win: C:\Users\userName\AppData\Roaming\McNeel\Rhinoceros\packages\7.0\Robots\libraries + /// Mac: /Users/userName/.config/McNeel/Rhinoceros/packages/7.0/Robots/libraries + /// Lib: {appData}\Robots\libraries + /// + public static string PluginPath + { + get + { + var appData = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData, Environment.SpecialFolderOption.DoNotVerify); + int version = 7; +#if NET48 + version = Rhino.RhinoApp.Version.Major; +#endif +#if (NET48 || DEBUG) + return Path.Combine(appData, "McNeel", "Rhinoceros", "packages", $"{version:0.0}", "Robots"); +#elif NETSTANDARD2_0 + return Path.Combine(appData, "Robots"); +#endif + } + } + + static string SettingsPath => Path.Combine(PluginPath, "settings.json"); + + static Settings GetDefault() + { + var localLibraryPath = + Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "Robots"); + + return new(localLibraryPath); + } + + public static Settings Load() + { + if (!File.Exists(SettingsPath)) + return GetDefault(); + + var json = File.ReadAllText(SettingsPath); + var settings = JsonConvert.DeserializeObject(json) + ?? throw new(" Could not load settings file."); + + return settings; + } + + public static void Save(Settings settings) + { + var json = JsonConvert.SerializeObject(settings, Formatting.Indented); + File.WriteAllText(SettingsPath, json); + } +}