Author Topic: [.NET Plugin] Loading Resources from External DLL  (Read 345 times)

0 Members and 1 Guest are viewing this topic.

flyte

  • Mosquito
  • Posts: 14
[.NET Plugin] Loading Resources from External DLL
« on: November 29, 2018, 12:21:08 AM »
I have constructed a .NET plugin and have successfully been able to call commands within it by netload-ing it. My plugin creates a palette (visuals are UserControls) and it'll display it.

 
However, my plugin's DLL references a theme suite Material Design In Xaml Toolkit (https://github.com/MaterialDesignInXAML/MaterialDesignInXamlToolkit). Typically this is easy to use in a regular WPFapp by adding the resources in the app.xaml file and using the pack URI scheme to be able to load the resource dictionaries, as outlined in the Super Quick Start guide, (https://github.com/MaterialDesignInXAML/MaterialDesignInXamlToolkit/wiki/Super-Quick-Start) . I do the same and add the resources to my user control as such:

Code: [Select]
<UserControl.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Light.xaml" />
                <ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Defaults.xaml" />
                <ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Primary/MaterialDesignColor.Grey.xaml" />
                <ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Accent/MaterialDesignColor.DeepOrange.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>

So what I am realizing is that with the pack://application, it'll never seem to find the DLLs. I have also tried pack://siteoforigin, and I still can't manipulate the path to be able to recognize the additional dlls.

 

The question is, what must be done to be able to reference 3rd party resources held in an external DLL .

 

I have tried various combinations of plain AutoCAD using a netload command in various locations, (actual ACAD.exe path, and even common ApplicationPlugins folder using both pack://application: and siteoforigin. As well as with Adv Steel and putting the DLLs in various locations, but no joy.

 

Does anyone have any working example of a Plugin that can reference and use a 3rd party control suite, or point me in the proper direction?

flyte

  • Mosquito
  • Posts: 14
Re: [.NET Plugin] Loading Resources from External DLL
« Reply #1 on: December 07, 2018, 01:03:18 AM »
Kinda shocked by the lack of responses :/

Is it even possible to be able to load an external resource, (image, resource dictionary, etc.) from an AutoCAD plugin that doesn't exist in the same location as the executing assembly?

huiz

  • Swamp Rat
  • Posts: 825
  • Certified Prof C3D
Re: [.NET Plugin] Loading Resources from External DLL
« Reply #2 on: December 10, 2018, 09:23:53 AM »
Maybe because of WPF there are not many responses :-)


I've not used images from external DLL's in WPF but I always added them directly to the resources. It's also useful to change the Build Action to Resource otherwise it might not be found. I have used components from external DLL's and that will work if the DLL is placed in the same map as the plugin.


Since I've started with WPF I found there is much to learn and never assume that things will work as expected.
The conclusion is justified that the initialization of the development of critical subsystem optimizes the probability of success to the development of the technical behavior over a given period.

flyte

  • Mosquito
  • Posts: 14
Re: [.NET Plugin] Loading Resources from External DLL
« Reply #3 on: January 04, 2019, 09:38:51 PM »
Maybe because of WPF there are not many responses :-)


I've not used images from external DLL's in WPF but I always added them directly to the resources. It's also useful to change the Build Action to Resource otherwise it might not be found. I have used components from external DLL's and that will work if the DLL is placed in the same map as the plugin.


Since I've started with WPF I found there is much to learn and never assume that things will work as expected.

I figured this out a while back, but forgot to update this post with the solution. Basically this issue comes up when the plugin DLL isn't in the same path as the executing assembly (acad.exe), as is the case with how plugins are detected with Advance Steel.

The solution is to ensure the dependencies can be located. Here is the solution:

Code: [Select]
public class Locator
{
private string _path;

public Locator()
{

}

public void InstallLocator(string path)
{
_path = path;
AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(AssemblyResolve);
}

private Assembly AssemblyResolve(object sender, ResolveEventArgs args)
{
int position = args.Name.IndexOf(",");
if (position > -1)
{
try
{
string assemblyName = args.Name.Substring(0, position);
string assemblyFullPath = string.Empty;

//look in main folder
assemblyFullPath = _path + "\\" + assemblyName + ".dll";
if (File.Exists(assemblyFullPath))
return Assembly.LoadFrom(assemblyFullPath);
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.Message);
}
}
return null;
}
}

and then I use this when my plugin is initialized:

Code: [Select]
public class Plugin : IExtensionApplication
{
static Locator loc = new Locator();

public void Initialize()
{
var currentPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
loc.InstallLocator(currentPath);
}

public void Terminate()
{

}
}

...and now I have zero issues in using any external resources from an AutoCAD plugin. This allows me to use any 3rd party UX library and make my (interactive) palettes look great.

MickD

  • Gator
  • Posts: 3223
  • !false...it's funny 'cause it's true!
Re: [.NET Plugin] Loading Resources from External DLL
« Reply #4 on: January 04, 2019, 09:51:30 PM »
Thanks for the update and your solution flyte! :)
A programmer's wife tells him, "Run to the store and pick up a loaf of bread. If they have eggs, get a dozen."

The programmer comes home with 12 loaves of bread.