From 2240275bf78b156347fa57ef32a75c27d6c41911 Mon Sep 17 00:00:00 2001 From: Luiz Henrique Cassettari Date: Tue, 28 Nov 2023 20:46:07 -0300 Subject: [PATCH] SetImage works with `Resources` without assembly name. --- CHANGELOG.md | 5 ++ ricaun.Revit.UI.Example/Resources/Revit.ico | Bin 0 -> 982 bytes ricaun.Revit.UI.Example/Revit/AppResource.cs | 33 ++++++++++++ .../ricaun.Revit.UI.Example.csproj | 2 + .../Resources/Images/Revit.ico | Bin 0 -> 982 bytes .../Resources/ResourceTests.cs | 34 +++++++++++++ .../Resources/StackTaskTests.cs | 23 +++++++++ .../ricaun.Revit.UI.Tests.csproj | 9 +++- ricaun.Revit.UI/BitmapExtension.cs | 36 ++++++++++---- ricaun.Revit.UI/Utils/StackTraceUtils.cs | 47 ++++++++++++++++++ 10 files changed, 179 insertions(+), 10 deletions(-) create mode 100644 ricaun.Revit.UI.Example/Resources/Revit.ico create mode 100644 ricaun.Revit.UI.Example/Revit/AppResource.cs create mode 100644 ricaun.Revit.UI.Tests/Resources/Images/Revit.ico create mode 100644 ricaun.Revit.UI.Tests/Resources/ResourceTests.cs create mode 100644 ricaun.Revit.UI.Tests/Resources/StackTaskTests.cs create mode 100644 ricaun.Revit.UI/Utils/StackTraceUtils.cs diff --git a/CHANGELOG.md b/CHANGELOG.md index 6d76c5b..29955bc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/ricaun.Revit.UI.Example/Resources/Revit.ico b/ricaun.Revit.UI.Example/Resources/Revit.ico new file mode 100644 index 0000000000000000000000000000000000000000..c44940179da1e3336534965e6b9cc41b0c1de8b2 GIT binary patch literal 982 zcmZQzU}Ruo5D;Jh(h3aij0_BF3=9kk3J|^~69dCqpjc;spF1y?6cNS%G}c0*}aI1_r*vAk26? ze?isc;7FDbxKi|Vf5HjPMTYBtOLZ`= zG@kct*_oPY$8wDy%1X02?kcV6q#@I=zT_HvrdZthJIZ5uO| zNVP?EGn^3Xw`!1JUtXfwiMOnL)o_iMqkGvqw+?*^ z69ztpRyLk9_8#i&UvgO0c0JnvKlb+jfAt@vv^ZEQxnDkE$Vqtpv;P0mO&edcCeE^Y zaO?7aU?gvN-JH0}ieU=dlHz~=>c6mQF|ZsIE?~Fk2)@gxuyNB1 zQTXxxf87If5&}B4*bj?XHw4+dYB>0|;l#Y?|DlJ&4~bqBa$ehR{Xlv}=$UMD&ecg# z2`j!Tu-n|_dBxvw!=ZEk3$D!8gVPo>>&@o;+%T!6kmbs$K!XVljf@O+og0-NY(97e J829jm0swF6qmKXp literal 0 HcmV?d00001 diff --git a/ricaun.Revit.UI.Example/Revit/AppResource.cs b/ricaun.Revit.UI.Example/Revit/AppResource.cs new file mode 100644 index 0000000..fc0cad4 --- /dev/null +++ b/ricaun.Revit.UI.Example/Revit/AppResource.cs @@ -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("Revit") + .SetLargeImage("/Resources/Revit.ico"), + ribbonPanel.CreatePushButton("Revit") + .SetLargeImage($"/{typeof(AppResource).Assembly.GetName().Name};component/Resources/revit.ico"), + ribbonPanel.CreatePushButton("Revit") + .SetLargeImage("UIFrameworkRes;component/ribbon/images/revit.ico"), + ribbonPanel.CreatePushButton("Revit") + .SetLargeImage("/UIFrameworkRes;component/ribbon/images/revit.ico") + ); + + return Result.Succeeded; + } + + public Result OnShutdown(UIControlledApplication application) + { + ribbonPanel?.Remove(); + return Result.Succeeded; + } + } +} \ No newline at end of file diff --git a/ricaun.Revit.UI.Example/ricaun.Revit.UI.Example.csproj b/ricaun.Revit.UI.Example/ricaun.Revit.UI.Example.csproj index a1fd222..f6773ad 100644 --- a/ricaun.Revit.UI.Example/ricaun.Revit.UI.Example.csproj +++ b/ricaun.Revit.UI.Example/ricaun.Revit.UI.Example.csproj @@ -79,6 +79,7 @@ + @@ -106,6 +107,7 @@ + diff --git a/ricaun.Revit.UI.Tests/Resources/Images/Revit.ico b/ricaun.Revit.UI.Tests/Resources/Images/Revit.ico new file mode 100644 index 0000000000000000000000000000000000000000..c44940179da1e3336534965e6b9cc41b0c1de8b2 GIT binary patch literal 982 zcmZQzU}Ruo5D;Jh(h3aij0_BF3=9kk3J|^~69dCqpjc;spF1y?6cNS%G}c0*}aI1_r*vAk26? ze?isc;7FDbxKi|Vf5HjPMTYBtOLZ`= zG@kct*_oPY$8wDy%1X02?kcV6q#@I=zT_HvrdZthJIZ5uO| zNVP?EGn^3Xw`!1JUtXfwiMOnL)o_iMqkGvqw+?*^ z69ztpRyLk9_8#i&UvgO0c0JnvKlb+jfAt@vv^ZEQxnDkE$Vqtpv;P0mO&edcCeE^Y zaO?7aU?gvN-JH0}ieU=dlHz~=>c6mQF|ZsIE?~Fk2)@gxuyNB1 zQTXxxf87If5&}B4*bj?XHw4+dYB>0|;l#Y?|DlJ&4~bqBa$ehR{Xlv}=$UMD&ecg# z2`j!Tu-n|_dBxvw!=ZEk3$D!8gVPo>>&@o;+%T!6kmbs$K!XVljf@O+og0-NY(97e J829jm0swF6qmKXp literal 0 HcmV?d00001 diff --git a/ricaun.Revit.UI.Tests/Resources/ResourceTests.cs b/ricaun.Revit.UI.Tests/Resources/ResourceTests.cs new file mode 100644 index 0000000..4efa09f --- /dev/null +++ b/ricaun.Revit.UI.Tests/Resources/ResourceTests.cs @@ -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); + } + } +} \ No newline at end of file diff --git a/ricaun.Revit.UI.Tests/Resources/StackTaskTests.cs b/ricaun.Revit.UI.Tests/Resources/StackTaskTests.cs new file mode 100644 index 0000000..e2ec287 --- /dev/null +++ b/ricaun.Revit.UI.Tests/Resources/StackTaskTests.cs @@ -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); + } + } +} \ No newline at end of file diff --git a/ricaun.Revit.UI.Tests/ricaun.Revit.UI.Tests.csproj b/ricaun.Revit.UI.Tests/ricaun.Revit.UI.Tests.csproj index c15771b..540bbd5 100644 --- a/ricaun.Revit.UI.Tests/ricaun.Revit.UI.Tests.csproj +++ b/ricaun.Revit.UI.Tests/ricaun.Revit.UI.Tests.csproj @@ -39,7 +39,6 @@ - @@ -50,6 +49,10 @@ + + + + @@ -64,6 +67,10 @@ <_Parameter2>true + + + + diff --git a/ricaun.Revit.UI/BitmapExtension.cs b/ricaun.Revit.UI/BitmapExtension.cs index e61b3b2..6109ae9 100644 --- a/ricaun.Revit.UI/BitmapExtension.cs +++ b/ricaun.Revit.UI/BitmapExtension.cs @@ -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]; + } + /// /// Transform string base64 or Uri to BitmapSource /// @@ -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 { } diff --git a/ricaun.Revit.UI/Utils/StackTraceUtils.cs b/ricaun.Revit.UI/Utils/StackTraceUtils.cs new file mode 100644 index 0000000..6f7bf55 --- /dev/null +++ b/ricaun.Revit.UI/Utils/StackTraceUtils.cs @@ -0,0 +1,47 @@ +using System.Diagnostics; +using System.Reflection; +using System.Runtime.CompilerServices; + +namespace ricaun.Revit.UI.Utils +{ + internal static class StackTraceUtils + { + /// + /// GetCallingAssembly using StackTrace + /// + /// + [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; + } + + /// + /// This method is for test if is working with StackTrace. + /// + /// + internal static Assembly GetCallingAssemblyNested() + { + return GetCallingAssembly(); + } + } +}