Skip to content

Commit 117ac90

Browse files
committed
Added PathExtension.TryGetFullyQualifiedPath.
1 parent bd6e5bd commit 117ac90

File tree

6 files changed

+77
-20
lines changed

6 files changed

+77
-20
lines changed

src/RhinoInside.Revit.External/Extensions/System.IO.cs

Lines changed: 62 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,63 @@
33

44
namespace System.IO
55
{
6-
#pragma warning disable CA1060 // Move pinvokes to native methods class
76
static class PathExtension
87
{
8+
internal static readonly StringComparer Comparer = new PathComparer();
9+
10+
private class PathComparer : StringComparer
11+
{
12+
private static readonly StringComparer StringComparer = OrdinalIgnoreCase;
13+
14+
public PathComparer() { }
15+
16+
private static string Normalize(string path) => Path.GetFullPath(path);
17+
18+
internal static string Canonicalize(string path) => new FileInfo(path).FullName;
19+
20+
public override int Compare(string x, string y)
21+
{
22+
var comparison = StringComparer.Compare(x, y);
23+
if (comparison == 0) return 0;
24+
25+
try
26+
{
27+
if (StringComparer.Compare(Normalize(x), Normalize(y)) == 0) return 0;
28+
return StringComparer.Compare(Canonicalize(x), Canonicalize(y));
29+
}
30+
catch { return comparison; }
31+
}
32+
33+
public override bool Equals(string x, string y)
34+
{
35+
if (StringComparer.Equals(x, y)) return true;
36+
try
37+
{
38+
if (StringComparer.Equals(Normalize(x), Normalize(y))) return true;
39+
return StringComparer.Equals(Canonicalize(x), Canonicalize(y));
40+
}
41+
catch { return false; }
42+
}
43+
44+
public override int GetHashCode(string obj)
45+
{
46+
try { return obj is null ? 0 : StringComparer.GetHashCode(Canonicalize(obj)); }
47+
catch { return int.MinValue; }
48+
}
49+
}
50+
51+
public static bool TryGetFullyQualifiedPath(ref string path)
52+
{
53+
if (string.IsNullOrWhiteSpace(path)) return false;
54+
if (!IsFullyQualifiedPath(path)) return false;
55+
try
56+
{
57+
path = PathComparer.Canonicalize(path);
58+
return true;
59+
}
60+
catch { return false; }
61+
}
62+
963
public static bool IsFullyQualifiedPath(this string path)
1064
{
1165
if (path == null) return false;
@@ -29,17 +83,20 @@ public static bool IsFullyQualifiedPath(this string path)
2983
#endif
3084
}
3185

32-
[DllImport("SHLWAPI", CharSet = CharSet.Unicode)]
33-
static extern bool PathCompactPathExW([Out] Text.StringBuilder pszOut, string szPath, int cchMax, int dwFlags);
3486
internal static string TripleDotPath(this string path, int maxLength)
3587
{
3688
if (path is null) return null;
3789
var builder = new Text.StringBuilder(maxLength + 1);
38-
PathCompactPathExW(builder, path, maxLength, 0);
90+
SafeNativeMethods.PathCompactPathExW(builder, path, maxLength, 0);
3991
return builder.ToString();
4092
}
93+
94+
static class SafeNativeMethods
95+
{
96+
[DllImport("SHLWAPI", CharSet = CharSet.Unicode)]
97+
public static extern bool PathCompactPathExW([Out] Text.StringBuilder pszOut, string szPath, int cchMax, int dwFlags);
98+
}
4199
}
42-
#pragma warning restore CA1060 // Move pinvokes to native methods class
43100

44101
internal static class DirectoryInfoExtension
45102
{

src/RhinoInside.Revit.GH/Components/Documents/DocumentSave.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public class DocumentSave : ZuiComponent
1818
public DocumentSave() : base
1919
(
2020
name: "Save Document",
21-
nickname: "Save",
21+
nickname: "D-Save",
2222
description: "Saves a document to a given file path",
2323
category: "Revit",
2424
subCategory: "Document"
@@ -104,7 +104,7 @@ protected override void TrySolveInstance(IGH_DataAccess DA)
104104
if (filePath.Last() == Path.DirectorySeparatorChar)
105105
filePath = Path.Combine(filePath, doc.Title);
106106

107-
if (filePath.IsFullyQualifiedPath())
107+
if (PathExtension.TryGetFullyQualifiedPath(ref filePath))
108108
{
109109
if (!Path.HasExtension(filePath))
110110
{

src/RhinoInside.Revit.GH/Components/Family/New.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -650,7 +650,7 @@ static string GetDefaultTemplatePath(ARDB.Document doc, ARDB.ElementId categoryI
650650

651651
static bool FindTemplatePath(ARDB.Document doc, ref string templatePath, out bool pathWasRelative)
652652
{
653-
pathWasRelative = !templatePath.IsFullyQualifiedPath();
653+
pathWasRelative = !PathExtension.TryGetFullyQualifiedPath(ref templatePath);
654654

655655
// Validate input
656656
foreach (var invalid in Path.GetInvalidPathChars())

src/RhinoInside.Revit.GH/Components/Family/Save.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ protected override void TrySolveInstance(IGH_DataAccess DA)
126126
if (!Path.HasExtension(filePath))
127127
filePath += ".rfa";
128128

129-
if (!filePath.IsFullyQualifiedPath())
129+
if (!PathExtension.TryGetFullyQualifiedPath(ref filePath))
130130
{
131131
filePath = Path.Combine(Types.Document.FromValue(family.Document).SwapFolder.Directory.FullName, "Families", filePath);
132132
Directory.CreateDirectory(Path.GetDirectoryName(filePath));

src/RhinoInside.Revit.GH/Components/Import/IFCImportOptions.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,17 @@
66
namespace RhinoInside.Revit.GH.Components.Insert
77
{
88
[ComponentVersion(introduced: "1.36")]
9-
public class IFCImportOptions : ZuiComponent
9+
public class IFCReadOptions : ZuiComponent
1010
{
1111
public override Guid ComponentGuid => new Guid("A8F3E5C1-9D4B-4E2A-B8F1-3C5D6E7F8A9B");
1212
public override GH_Exposure Exposure => GH_Exposure.secondary;
1313
protected override string IconTag => string.Empty;
1414

15-
public IFCImportOptions() : base
15+
public IFCReadOptions() : base
1616
(
17-
name: "IFC Import Options",
18-
nickname: "IFCOpt",
19-
description: "Configure options for IFC file import or link operations",
17+
name: "IFC Read Options",
18+
nickname: "IFC-Read",
19+
description: "Configure options to read IFC files.",
2020
category: "Revit",
2121
subCategory: "Insert"
2222
)
@@ -128,7 +128,7 @@ public IFCImportOptions() : base
128128
NickName = "AC",
129129
Description = "Autocorrect off-axis lines enabled",
130130
Access = GH_ParamAccess.item
131-
}
131+
}, ParamRelevance.Secondary
132132
)
133133
};
134134

@@ -162,10 +162,10 @@ protected override void TrySolveInstance(IGH_DataAccess DA)
162162

163163
if (options != null)
164164
{
165-
DA.SetData("Intent", (int)options.Intent);
166-
DA.SetData("Action", (int)options.Action);
167-
DA.SetData("Auto Join", options.AutoJoin);
168-
DA.SetData("Autocorrect Off Axis Lines", options.AutocorrectOffAxisLines);
165+
Params.TrySetData(DA, "Intent", () => (int)options.Intent);
166+
Params.TrySetData(DA, "Action", () => (int)options.Action);
167+
Params.TrySetData(DA, "Auto Join", () => options.AutoJoin);
168+
Params.TrySetData(DA, "Autocorrect Off Axis Lines", () => options.AutocorrectOffAxisLines);
169169
}
170170
}
171171
}

src/RhinoInside.Revit.GH/Components/Views/ExportImage.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ protected override void TrySolveInstance(IGH_DataAccess DA)
277277
if (folder?.Any(x => Path.GetInvalidPathChars().Contains(x)) is true)
278278
throw new Exceptions.RuntimeArgumentException("Folder", $"'{folder}' is not a valid folder name", folder);
279279

280-
if (folder?.IsFullyQualifiedPath() is false)
280+
if (!PathExtension.TryGetFullyQualifiedPath(ref folder))
281281
throw new Exceptions.RuntimeArgumentException("Folder", $"'{folder}' is not a valid absolute path", folder);
282282
}
283283

0 commit comments

Comments
 (0)