Skip to content

Commit

Permalink
Merge pull request #99 from bvn-architecture/RBP-19
Browse files Browse the repository at this point in the history
RBP-19 Changes to handle non-std rvt install paths
  • Loading branch information
petersmithfromengland authored Nov 17, 2022
2 parents 3aecbfc + 847bae7 commit 4e4acf1
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 83 deletions.
1 change: 1 addition & 0 deletions BatchRvt/BatchRvt.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
<HintPath>..\References\IronPython-2.7.3\Microsoft.Scripting.Metadata.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.Win32.Registry, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" />
<Reference Include="Newtonsoft.Json, Version=4.5.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\References\JSON.NET\Newtonsoft.Json.dll</HintPath>
Expand Down
159 changes: 76 additions & 83 deletions BatchRvtUtil/RevitVersion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
using System.Collections.Generic;
using System.Linq;
using System.IO;
using Microsoft.Win32;

namespace BatchRvtUtil
{
Expand Down Expand Up @@ -68,77 +69,83 @@ public enum SupportedRevitVersion {
{ SupportedRevitVersion.Revit2023, "2023" }
};

private static readonly Dictionary<SupportedRevitVersion, IEnumerable<string>> REVIT_EXECUTABLE_FOLDER_PATHS =
new Dictionary<SupportedRevitVersion, IEnumerable<string>>()
private static Dictionary<SupportedRevitVersion,string> REVIT_EXECUTABLE_FOLDER_PATHS()
{
var revitInstallPaths = new Dictionary<SupportedRevitVersion, string>();
foreach (var versionName in Enum.GetNames(typeof(SupportedRevitVersion)))
{
SupportedRevitVersion enumOfVersion = (SupportedRevitVersion)Enum.Parse(typeof(SupportedRevitVersion), versionName);

var installLocation = GetRevitInstallPath(enumOfVersion);
if (installLocation == null)
{
SupportedRevitVersion.Revit2015,
new [] {
@"C:\Program Files\Autodesk\Revit 2015",
@"C:\Program Files\Autodesk\Revit Architecture 2015",
@"D:\Program Files\Autodesk\Revit 2015",
@"D:\Program Files\Autodesk\Revit Architecture 2015"
}
},
{
SupportedRevitVersion.Revit2016,
new [] {
@"C:\Program Files\Autodesk\Revit 2016",
@"C:\Program Files\Autodesk\Revit Architecture 2016",
@"D:\Program Files\Autodesk\Revit 2016",
@"D:\Program Files\Autodesk\Revit Architecture 2016",
}
},
{
SupportedRevitVersion.Revit2017,
new [] {
@"C:\Program Files\Autodesk\Revit 2017",
@"D:\Program Files\Autodesk\Revit 2017"
}
},
{
SupportedRevitVersion.Revit2018,
new [] {
@"C:\Program Files\Autodesk\Revit 2018",
@"D:\Program Files\Autodesk\Revit 2018"
}
},
{
SupportedRevitVersion.Revit2019,
new [] {
@"C:\Program Files\Autodesk\Revit 2019",
@"D:\Program Files\Autodesk\Revit 2019"
}
},
continue;
}
revitInstallPaths.Add(enumOfVersion, GetRevitInstallPath(enumOfVersion));
}

return revitInstallPaths;
}

private static string GetRevitInstallPath(SupportedRevitVersion revitVersion)
{
var versionName = Enum.GetName(typeof(SupportedRevitVersion), revitVersion);
var version = versionName?.Remove(0, 5);
var appPath = $@"SOFTWARE\Autodesk\Revit\{version}";
if (appPath == null) throw new ArgumentNullException(nameof(appPath));
using (var sk = Registry.LocalMachine.OpenSubKey(appPath))
{
if (sk is null)
{
SupportedRevitVersion.Revit2020,
new [] {
@"C:\Program Files\Autodesk\Revit 2020",
@"D:\Program Files\Autodesk\Revit 2020"
}
},
return null;
}

string revitSubkey = null;
foreach (var revitKey in sk.GetSubKeyNames())
{
SupportedRevitVersion.Revit2021,
new [] {
@"C:\Program Files\Autodesk\Revit 2021",
@"D:\Program Files\Autodesk\Revit 2021"
if (!revitKey.Contains("REVIT-"))
{
continue;
}
},

revitSubkey = revitKey;

}
if (revitSubkey == null)
{
SupportedRevitVersion.Revit2022,
new [] {
@"C:\Program Files\Autodesk\Revit 2022",
@"D:\Program Files\Autodesk\Revit 2022"
}
},
return null;
}

using (var rk = sk.OpenSubKey(revitSubkey))
{
SupportedRevitVersion.Revit2023,
new [] {
@"C:\Program Files\Autodesk\Revit 2023",
@"D:\Program Files\Autodesk\Revit 2023"
if (rk is null)
{
return null;
}
var displayName = rk.GetValue("ProductName");
var installLocation = rk.GetValue("InstallationLocation");
return installLocation?.ToString();
}
};
}
}

public static string GetRevitExecutableFolderPath(SupportedRevitVersion revitVersion)
{

if (GetRevitInstallPath(revitVersion) == null)
{
return null;
}
return File.Exists((Path.Combine(GetRevitInstallPath(revitVersion) ?? string.Empty, REVIT_EXECUTABLE_FILE_NAME))) ? GetRevitInstallPath(revitVersion) : null;
}

public static List<SupportedRevitVersion> GetInstalledRevitVersions()
{
return REVIT_EXECUTABLE_FOLDER_PATHS().Keys
.Where(IsRevitVersionInstalled)
.Where(BatchRvt.IsBatchRvtAddinInstalled)
.ToList();
}

private static readonly Dictionary<SupportedRevitVersion, string> REVIT_LOCAL_FOLDER_PATHS =
new Dictionary<SupportedRevitVersion, string>()
Expand All @@ -154,20 +161,6 @@ public enum SupportedRevitVersion {
{ SupportedRevitVersion.Revit2023, @"C:\REVIT_LOCAL2023" }
};

public static string GetRevitExecutableFolderPath(SupportedRevitVersion revitVersion)
{
string revitExecutableFolderPath = null;

if (REVIT_EXECUTABLE_FOLDER_PATHS.ContainsKey(revitVersion))
{
revitExecutableFolderPath =
REVIT_EXECUTABLE_FOLDER_PATHS[revitVersion]
.FirstOrDefault(folderPath => File.Exists(Path.Combine(folderPath, REVIT_EXECUTABLE_FILE_NAME)));
}

return revitExecutableFolderPath;
}

public static string GetRevitExecutableFilePath(SupportedRevitVersion revitVersion)
{
var folderPath = GetRevitExecutableFolderPath(revitVersion);
Expand Down Expand Up @@ -207,13 +200,13 @@ public static SupportedRevitVersion GetMinimumInstalledRevitVersion()
return GetInstalledRevitVersions().OrderBy(supportedRevitVersion => supportedRevitVersion).FirstOrDefault();
}

public static List<SupportedRevitVersion> GetInstalledRevitVersions()
{
return REVIT_EXECUTABLE_FOLDER_PATHS.Keys
.Where(IsRevitVersionInstalled)
.Where(BatchRvt.IsBatchRvtAddinInstalled)
.ToList();
}
//public static List<SupportedRevitVersion> GetInstalledRevitVersions()
//{
// return REVIT_EXECUTABLE_FOLDER_PATHS.Keys
// .Where(IsRevitVersionInstalled)
// .Where(BatchRvt.IsBatchRvtAddinInstalled)
// .ToList();
//}

public static string GetRevitVersionText(SupportedRevitVersion revitVersion)
{
Expand Down

0 comments on commit 4e4acf1

Please sign in to comment.