Skip to content

Commit

Permalink
Add ability to change local library folder
Browse files Browse the repository at this point in the history
  • Loading branch information
visose committed Mar 23, 2024
1 parent 4b63cb7 commit 050163f
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 26 deletions.
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<Product>Robots</Product>
<Version>1.6.7</Version>
<Version>1.7.0</Version>
<Authors>Robots Authors</Authors>
<Description>Create and simulate ABB, KUKA, UR, and Staubli robot programs.</Description>
<PackageTags>Robots;ABB;KUKA;UR;Staubli;Robotics</PackageTags>
Expand Down
1 change: 1 addition & 0 deletions RELEASE
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
17 changes: 17 additions & 0 deletions src/Robots.Grasshopper/RobotSystem/LibraryForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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",
Expand Down
30 changes: 5 additions & 25 deletions src/Robots/IO/FileIO.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System.Xml;
using System.Xml.Linq;
using Rhino;
using Rhino.FileIO;
using Rhino.Geometry;

Expand Down Expand Up @@ -55,33 +54,14 @@ public static Frame LoadFrame(string name)
// library files

/// <summary>
/// Win: C:\Users\userName\Documents\Robots
/// Mac: /Users/userName/Robots
/// Default Win: C:\Users\userName\Documents\Robots
/// Default Mac: /Users/userName/Robots
/// </summary>
public static string LocalLibraryPath =>
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "Robots");
Settings.Load().LocalLibraryPath;

/// <summary>
/// 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
/// </summary>
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<string> GetLibraryPaths()
{
Expand Down
56 changes: 56 additions & 0 deletions src/Robots/IO/Settings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using Newtonsoft.Json;

namespace Robots;

public record Settings(string LocalLibraryPath)
{
/// <summary>
/// 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
/// </summary>
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<Settings>(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);
}
}

0 comments on commit 050163f

Please sign in to comment.