Skip to content

Commit

Permalink
add FileFromRelativePath
Browse files Browse the repository at this point in the history
  • Loading branch information
chuongmep committed Apr 29, 2024
1 parent 26739dd commit 53a6f53
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion OpenMEPRevit/Application/Windows.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using Autodesk.DesignScript.Runtime;
using System.IO;
using Autodesk.DesignScript.Runtime;
using Dynamo.Applications;

namespace OpenMEPRevit.Application;

Expand Down Expand Up @@ -38,4 +40,28 @@ private Windows()
{"version", version}
};
}

/// <summary>
/// Return the full path of relative path same folder with current script.
/// e.g. FileFromRelativePath("test.txt") will return the full path of test.txt in the same folder with current script.
/// </summary>
/// <param name="relativeFileName">The file name.extension</param>
/// <returns name="filePath">path</returns>
public static string FileFromRelativePath(string relativeFileName)
{
try
{
string fileName = DynamoRevit.RevitDynamoModel.CurrentWorkspace.FileName;
if (string.IsNullOrEmpty(fileName)) throw new ArgumentNullException(nameof(relativeFileName));
var directory = Path.GetDirectoryName(fileName);
if (directory == null) throw new ArgumentNullException(nameof(relativeFileName));
string filePath = Path.Combine(directory, relativeFileName);
if (!File.Exists(filePath)) throw new FileNotFoundException("File not found");
return filePath;
}
catch (Exception e)
{
return string.Empty;
}
}
}

0 comments on commit 53a6f53

Please sign in to comment.