Skip to content

Commit

Permalink
SetImage works with Resources without assembly name.
Browse files Browse the repository at this point in the history
  • Loading branch information
ricaun committed Nov 28, 2023
1 parent a8a3635 commit 2240275
Show file tree
Hide file tree
Showing 10 changed files with 179 additions and 10 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,17 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
## [0.6.0] / 2023-11-19 - 2023-11-28
### Features
- Update to support `net7.0-windows`
- SetImage works with `Resources` without assembly name.
### Updated
- Update `Build` project
- Update `Example` project
- Fix `MovePanelTo` remove panel from `RibbonTabsDictionary`
- Add `StackTraceUtils` to find the caller assembly.
- Update `BitmapExtension` to enable `Resources` without assembly name.
### Tests
- Test `MovePanelTo_Modify_CreatePanel_SameName`
- Test `StackTraceUtils`
- Test `Resources` without assembly name.

## [0.5.7] / 2023-11-17
### Features
Expand Down
Binary file added ricaun.Revit.UI.Example/Resources/Revit.ico
Binary file not shown.
33 changes: 33 additions & 0 deletions ricaun.Revit.UI.Example/Revit/AppResource.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using Autodesk.Revit.UI;

namespace ricaun.Revit.UI.Example.Revit
{
[AppLoader]
public class AppResource : IExternalApplication
{
private static RibbonPanel ribbonPanel;
public Result OnStartup(UIControlledApplication application)
{
ribbonPanel = application.CreatePanel("Resource");

ribbonPanel.RowLargeStackedItems(
ribbonPanel.CreatePushButton<Commands.CommandAvailable>("Revit")
.SetLargeImage("/Resources/Revit.ico"),
ribbonPanel.CreatePushButton<Commands.CommandAvailable>("Revit")
.SetLargeImage($"/{typeof(AppResource).Assembly.GetName().Name};component/Resources/revit.ico"),
ribbonPanel.CreatePushButton<Commands.CommandAvailable>("Revit")
.SetLargeImage("UIFrameworkRes;component/ribbon/images/revit.ico"),
ribbonPanel.CreatePushButton<Commands.CommandAvailable>("Revit")
.SetLargeImage("/UIFrameworkRes;component/ribbon/images/revit.ico")
);

return Result.Succeeded;
}

public Result OnShutdown(UIControlledApplication application)
{
ribbonPanel?.Remove();
return Result.Succeeded;
}
}
}
2 changes: 2 additions & 0 deletions ricaun.Revit.UI.Example/ricaun.Revit.UI.Example.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@

<ItemGroup>
<None Remove="Resources\icon.png" />
<None Remove="Resources\Revit.ico" />
</ItemGroup>

<!-- Fody -->
Expand Down Expand Up @@ -106,6 +107,7 @@

<ItemGroup>
<Resource Include="Resources\icon.png" />
<Resource Include="Resources\Revit.ico" />
</ItemGroup>

<ItemGroup>
Expand Down
Binary file added ricaun.Revit.UI.Tests/Resources/Images/Revit.ico
Binary file not shown.
34 changes: 34 additions & 0 deletions ricaun.Revit.UI.Tests/Resources/ResourceTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using NUnit.Framework;
using System;

namespace ricaun.Revit.UI.Tests.Resources
{
public class ResourceTests
{
const string ResourceName = "Resources/Images/Revit.ico";

[Test]
public void GetBitmapSource_NotNull()
{
Console.WriteLine(ResourceName.GetBitmapSource());
Assert.IsNotNull(ResourceName.GetBitmapSource());
Assert.IsNotNull(("/" + ResourceName).GetBitmapSource());
}

[Test]
public void GetBitmapSource_NotNull_Component()
{
var component = "ricaun.Revit.UI.Tests;component/";
Assert.IsNotNull((component + ResourceName).GetBitmapSource());
Assert.IsNotNull(("/" + component + ResourceName).GetBitmapSource());
}

[Test]
public void GetBitmapSource_Null_NotExist()
{
var component = "NotExist/" + ResourceName;
var source = component.GetBitmapSource();
Assert.IsNull(source);
}
}
}
23 changes: 23 additions & 0 deletions ricaun.Revit.UI.Tests/Resources/StackTaskTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using NUnit.Framework;
using ricaun.Revit.UI.Utils;
using System.Reflection;

namespace ricaun.Revit.UI.Tests.Resources
{
public class StackTaskTests
{
[Test]
public void GetCallingAssembly_Equal()
{
var assembly = StackTraceUtils.GetCallingAssembly();
Assert.AreEqual(Assembly.GetExecutingAssembly(), assembly);
}

[Test]
public void GetCallingAssembly_Nested()
{
var assembly = StackTraceUtils.GetCallingAssemblyNested();
Assert.AreEqual(Assembly.GetExecutingAssembly(), assembly);
}
}
}
9 changes: 8 additions & 1 deletion ricaun.Revit.UI.Tests/ricaun.Revit.UI.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
</PropertyGroup>
</Otherwise>
</Choose>


<ItemGroup>
<PackageReference Include="NUnit" Version="3.13.3" />
Expand All @@ -50,6 +49,10 @@
<ProjectReference Include="..\ricaun.Revit.UI\ricaun.Revit.UI.csproj" />
</ItemGroup>

<ItemGroup Condition="$(TargetFramework.StartsWith('net7'))">
<PackageReference Include="System.Drawing.Common" Version="7.*" IncludeAssets="build; compile" PrivateAssets="All" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Revit_All_Main_Versions_API_x64" Version="$(RevitVersion).*" IncludeAssets="build; compile" PrivateAssets="All" />
</ItemGroup>
Expand All @@ -64,6 +67,10 @@
<_Parameter2>true</_Parameter2>
</AssemblyAttribute>
</ItemGroup>

<ItemGroup>
<Resource Include="Resources\Images\Revit.ico" />
</ItemGroup>
<!--
-->

Expand Down
36 changes: 27 additions & 9 deletions ricaun.Revit.UI/BitmapExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,20 @@ public static BitmapSource GetBitmapSource(this System.Drawing.Image image)
return bitmap.GetBitmapSource();
}

internal static BitmapSource Base64ToBitmapSource(string base64)
{
var convert = Convert.FromBase64String(base64);
var image = System.Drawing.Bitmap.FromStream(new MemoryStream(convert));
return image.GetBitmapSource();
}

internal static BitmapFrame UriToBitmapFrame(string uriString)
{
var uri = new Uri(uriString, UriKind.RelativeOrAbsolute);
var decoder = BitmapDecoder.Create(uri, BitmapCreateOptions.None, BitmapCacheOption.Default);
return decoder.Frames[0];
}

/// <summary>
/// Transform string base64 or Uri to BitmapSource
/// </summary>
Expand All @@ -67,25 +81,29 @@ public static BitmapSource GetBitmapSource(this string base64orUri)
{
try
{
var uri = new Uri(base64orUri, UriKind.RelativeOrAbsolute);
var decoder = BitmapDecoder.Create(uri, BitmapCreateOptions.None, BitmapCacheOption.Default);
return decoder.Frames[0];
return UriToBitmapFrame(base64orUri);
}
catch { }

try
{
var componentUri = "pack://application:,,,/" + base64orUri.TrimStart('/');
return UriToBitmapFrame(componentUri);
}
catch { }

try
{
var uri = new Uri("pack://application:,,," + base64orUri, UriKind.RelativeOrAbsolute);
var decoder = BitmapDecoder.Create(uri, BitmapCreateOptions.None, BitmapCacheOption.Default);
return decoder.Frames[0];
var executingAssembly = Utils.StackTraceUtils.GetCallingAssembly();
var assemblyName = executingAssembly.GetName().Name;
var componentUri = $"pack://application:,,,/{assemblyName};component/" + base64orUri.TrimStart('/');
return UriToBitmapFrame(componentUri);
}
catch { }

try
{
var convert = Convert.FromBase64String(base64orUri);
var image = System.Drawing.Bitmap.FromStream(new MemoryStream(convert));
return image.GetBitmapSource();
return Base64ToBitmapSource(base64orUri);
}
catch { }

Expand Down
47 changes: 47 additions & 0 deletions ricaun.Revit.UI/Utils/StackTraceUtils.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;

namespace ricaun.Revit.UI.Utils
{
internal static class StackTraceUtils
{
/// <summary>
/// GetCallingAssembly using StackTrace
/// </summary>
/// <returns></returns>
[MethodImpl(MethodImplOptions.NoInlining)]
public static Assembly GetCallingAssembly()
{
var callingAssembly = Assembly.GetCallingAssembly();
var executingAssembly = Assembly.GetExecutingAssembly();
if (callingAssembly != executingAssembly) { return callingAssembly; }

const int skipFrames = 1; // Skip 'StackTraceUtils' method.
var stackFrames = new StackTrace(skipFrames).GetFrames();
if (stackFrames == null) return null;

foreach (var frame in stackFrames)
{
var assembly = frame.GetMethod().DeclaringType.Assembly;
//System.Console.WriteLine($"[{frame.GetMethod().Name}] \t{assembly}");
if (assembly != executingAssembly)
{
callingAssembly = assembly;
break;
}
}

return callingAssembly;
}

/// <summary>
/// This method is for test if <see cref="GetCallingAssembly"/> is working with StackTrace.
/// </summary>
/// <returns></returns>
internal static Assembly GetCallingAssemblyNested()
{
return GetCallingAssembly();
}
}
}

0 comments on commit 2240275

Please sign in to comment.