Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix schema not loading when the assembly is in a folder containing accented characters in their name. #190

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions Source/Revit.IFC.Export/Exporter/Exporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@
using Autodesk.Revit.DB.ExternalService;
using Revit.IFC.Export.Properties;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;

using Autodesk.Revit.DB.Steel;

namespace Revit.IFC.Export.Exporter
Expand Down Expand Up @@ -96,6 +99,14 @@ private void OnApplicationInitialized(object sender, EventArgs eventArgs)
/// </summary>
public class Exporter : IExporterIFC
{
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
private static extern int GetShortPathName(
[MarshalAs(UnmanagedType.LPTStr)]
string path,
[MarshalAs(UnmanagedType.LPTStr)]
StringBuilder shortPath,
int shortPathLength);

RevitStatusBar statusBar = null;

// Used for debugging tool "WriteIFCExportedElements"
Expand Down Expand Up @@ -983,6 +994,9 @@ private string LocateSchemaFile(string schemaFileName)
#if IFC_OPENSOURCE
// Find the alternate schema file from the open source install folder
filePath = Path.Combine(Path.GetDirectoryName(Assembly.GetCallingAssembly().Location), schemaFileName);
// If the path contains accented characters, the schema loading will fail, so let's
// try to get the short path form of the specified path.
filePath = TryGetShortPathName(filePath);
if (!File.Exists(filePath))
#endif
{
Expand All @@ -991,6 +1005,28 @@ private string LocateSchemaFile(string schemaFileName)
return filePath;
}

private static string TryGetShortPathName(string filePath)
{
try
{
var shortPathBuilder = new StringBuilder(255);
if (GetShortPathName(filePath, shortPathBuilder, shortPathBuilder.Capacity) != 0)
{
var shortPath = shortPathBuilder.ToString();
if (!string.IsNullOrWhiteSpace(shortPath))
{
return shortPath;
}
}

return filePath;
}
catch (Exception)
{
return filePath;
}
}

/// <summary>
/// Sets the lists of property sets to be exported. This can be overriden.
/// </summary>
Expand Down