.Net WPF support for Solidworks PDM add-ins

Hello all,

At my place of employment we have a lot of “legacy” software written in windows forms. Because of this writing c# windows forms add-ins for PDM is made easier because all the software colleagues around me can help me with pretty much anything that isn’t PDM API specific (and even then most of them know their way around a wild API if it crosses their path).

Now I say “legacy” because the resistance among coworkers to create new software with windows forms to stay “compatible” (read, it looks the same bla bla bla) is increasing slowly. Some have already started to rewrite the old software in WPF.

Now I have already tried to create PDM add-ins using the C# library with WPF in it, but I can’t for the life of me get those libraries into the PDM administration tool.

Is WPF just not supported, or am I doing something wrong while writing/building the library?

Technically, WPF is not supported. However, you can get WPF running within a Windows Form or a WinForms UserControl by utilizing an ElementHost object to hold your WPF user control.

It’s a workaround but I’ve gotten it to work in a couple of applications.

The below will host a WPF UserControl called MainUserControl within a windows form with MainViewModel as its data context.

MainForm code behind:

    public partial class MainForm : Form
    {
        private ElementHost ctrlHost;
        private MainUserControl wpfControl;

        public MainForm(object vault)
        {

            InitializeComponent();
        }

        private void Load_WPF_Control(object dataContext)
        {
            ctrlHost = new ElementHost();
            ctrlHost.Dock = DockStyle.Fill;
            ctrlHost.AutoSize = true;

            wpfControl = new MainUserControl(dataContext);
            ctrlHost.Child = wpfControl;

            this.Controls.Add(ctrlHost);

            ctrlHost.AutoSize = false;
        }

        private void MainForm_Shown(object sender, EventArgs e)
        {
            MainViewModel mainViewModel = new MainViewModel();

            Load_WPF_Control(mainViewModel);
        }
    }

WPF MainUserControl Code Behind (.xaml.cs):

    public partial class MainUserControl : UserControl
    {
        public MainUserControl(object dataContext)
        {
            InitializeComponent();
            DataContext = dataContext;
        }
    }

Disclaimer: I removed a lot of code that I don’t think is applicable for this example at least. If you have any issues getting something like this set up, please comment.

WPF works fine with PDM with few exceptions for resolving resources assemblies and adding user input in setup dialog.

You need to be more specific about your errors.

Our products PDM Converted Task Extended and PDM2Excel both use WPF for task setup dialog but they are a hassle to maintain.

Thanks for the replies, I’m gonna give BigCrazyAl’s setup a go to see if that works.

To come back to your reply, the main issue that i have right now is to register the add-in as COM accessible, which i get the feeling from the administration tool is a necessity.

Yup. You need to. Also, do not run the Administration tool as admin.

To register a .NET assembly for COM Interop in Visual Studio, you can follow the steps for both C# and VB.NET projects. This allows COM clients to use your .NET classes as if they were COM objects. Here’s how you can set this up:

For C# Projects

  1. Open Your Project in Visual Studio: Load your C# project where you want to enable COM interop.
  2. Project Properties: Right-click on the project in the Solution Explorer and select Properties.
  3. Build Settings: Go to the Build tab in the project properties window.
  4. Register for COM Interop: Check the box labeled Register for COM Interop. This option is generally found near the bottom of the Build tab.
  5. Assembly Information:
  • Go to the Application tab and click on the Assembly Information button.
  • Ensure that your assembly has a strong name by signing it. You can do this under the Signing tab by checking Sign the assembly and choosing a strong name key file.
  1. Build the Project: Rebuild your project. This action generates the necessary type library (.tlb) and registers it along with the assembly.

For VB.NET Projects

  1. Open Your Project in Visual Studio: Load your VB.NET project where you want to enable COM interop.
  2. Project Properties: Right-click on the project in the Solution Explorer and select Properties.
  3. Compile Settings: Navigate to the Compile tab in the project properties window.
  4. Register for COM Interop: Scroll down to the Output section and check the box labeled Register for COM Interop.
  5. Assembly Information:
  • Go to the Application tab and then to Assembly Information.
  • Ensure that your assembly has a strong name by signing it. This can be set under the Signing tab by selecting Sign the assembly and choosing a strong name key file.
  1. Build the Project: Rebuild your project to generate and register the type library.

Additional Considerations

  • Administrator Rights: Running Visual Studio as an administrator may be required because registering an assembly for COM interop involves writing to the system registry, which is a privileged operation.

Best,
Amen
Blue Byte Systems Inc.

1 Like