Hide Table of Contents
SOLIDWORKS Electrical API 2024.0.0
Electrical API
Add-in sample in C#

Purpose

This document explains how to create a c# dll.

Create C# project with Visual Studio

  • Open Visual Studio
  • File -> New -> Project
  • Select Visual C# -> Class Library

Click OK.

In the solution tree -> Go to project properties.

Verify that solution platform is x64

Otherwise create it.

In Build tab, select platform x64 and check "Register for COM interop"

In the Build Events tab, In the Post-build events command line Add this comand line: C:\WINDOWS\Microsoft.NET\Framework64\$(frameworkversion)\regasm.exe "$(TargetPath)" /codebase

Rename Class1.cs file with an appropriate name for the Add-in.

Modify code to make the Add-in

Open Assemblyinfo.cs, cut selected lines below:

In your Class interface (HelloWorld.cs):

  • Paste previously copied information
  • Verify that you have ComVisible(true) and not ComVisible(false).
  • Add: using System.Runtime.InteropServices;

Like in screen shot below:

Include reference to EwAPIX.dll

Right click on Reference and select Add reference...

Browse to the location of EwAPIX.dll

Add: using EwAPI;

Inherit your main class from EwAddIn

Implement those two methods:

public class HelloWorld : EwAPI.EwAddIn
{
IEwApplicationX mEwApplicationX;
IEwInteropFactoryX mEwInteropFactory;
public bool connectToEwAPI(object ewInteropFactory)
{
mEwInteropFactory = (IEwInteropFactoryX)ewInteropFactory;
if (mEwInteropFactory == null)
return false;
EwErrorCode ewErrorCode = EwErrorCode.EW_UNDEFINED_ERROR;
mEwApplicationX = mEwInteropFactory.getEwApplication("License Code(*)", out ewErrorCode);
if ((ewErrorCode != EwErrorCode.EW_NO_ERROR) ||(mEwApplicationX == null))
return false;
hello();
return true;
}
public bool disconnectFromEwApi()
{
goodBye();
return true;
}
EwErrorCode
All errors codes for this API.
Definition: EnumDefinition.idl:24
Use this interface to manage the application.
Definition: EwAPI.idl:694
Use this interface to connect to the application.
Definition: EwAPI.idl:1500
IEwApplicationX getEwApplication(BSTR strKey, EwErrorCode *errorCode)
Return an IEwApplicationX object connect to the application.

Finally add those two methods to your main class to register this Add-in:

[ComRegisterFunctionAttribute]
public static void RegisterFunction(Type t)
{
try
{
Microsoft.Win32.RegistryKey hklm = Microsoft.Win32.Registry.LocalMachine;
Microsoft.Win32.RegistryKey hkcu = Microsoft.Win32.Registry.CurrentUser;
string keyname = "SOFTWARE\\SolidWorks\\SOLIDWORKS Electrical\\AddIns\\{" + t.GUID.ToString() + "}";
Microsoft.Win32.RegistryKey addinkey = hklm.CreateSubKey(keyname);
addinkey.SetValue(null, 0);
addinkey.SetValue("Description", "Your add in description");
addinkey.SetValue("Title", "Your Add-in Name");
addinkey.SetValue("Path", System.Reflection.Assembly.GetExecutingAssembly().Location);
addinkey = hkcu.CreateSubKey(keyname);
addinkey.SetValue("StartUp", Convert.ToInt32(1), Microsoft.Win32.RegistryValueKind.DWord);
}
catch (System.NullReferenceException nl)
{
Console.WriteLine("There was a problem registering this dll. \n\"" + nl.Message + "\"");
}
catch (System.Exception e)
{
Console.WriteLine(e.Message);
}
}
[ComUnregisterFunctionAttribute]
public static void UnregisterFunction(Type t)
{
try
{
Microsoft.Win32.RegistryKey hklm = Microsoft.Win32.Registry.LocalMachine;
Microsoft.Win32.RegistryKey hkcu = Microsoft.Win32.Registry.CurrentUser;
string keyname = "Software\\SolidWorks\\SOLIDWORKS Electrical\\AddIns\\{" + t.GUID.ToString() + "}";
hklm.DeleteSubKey(keyname);
hkcu.DeleteSubKey(keyname);
}
catch (System.NullReferenceException nl)
{
Console.WriteLine("There was a problem unregistering this dll: " + nl.Message);
}
catch (System.Exception e)
{
Console.WriteLine("There was a problem unregistering this dll: " + e.Message);
}
}

Here is an example of implementation:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
using EwAPI;
namespace MyCSharpAddin
{
[ComVisible(true)]
[Guid("edb6bfd9-22a6-486a-98b0-07be0d9b34d0")]
public class HelloWorld : EwAPI.EwAddIn
{
IEwApplicationX mEwApplicationX;
IEwInteropFactoryX mEwInteropFactory;
public bool connectToEwAPI(object ewInteropFactory)
{
mEwInteropFactory = (IEwInteropFactoryX)ewInteropFactory;
if (mEwInteropFactory == null)
return false;
EwErrorCode ewErrorCode = EwErrorCode.EW_UNDEFINED_ERROR;
mEwApplicationX = mEwInteropFactory.getEwApplication("License Code(*)", out ewErrorCode);
if ((ewErrorCode != EwErrorCode.EW_NO_ERROR) ||(mEwApplicationX == null))
return false;
hello();
return true;
}
public bool disconnectFromEwApi()
{
goodBye();
return true;
}
public void hello()
{
// your function here: Sample
if (mEwApplicationX == null)
return;
EwErrorCode ewErrorCode = EwErrorCode.EW_UNDEFINED_ERROR;
EwDialogTaskX ewDialogTask = mEwApplicationX.getEwDialogTask(out ewErrorCode);
if ((ewErrorCode != EwErrorCode.EW_NO_ERROR) || (ewDialogTask == null))
return;
ewDialogTask.setContent("Hello World.");
ewDialogTask.addButton("Ok", 1);
ewDialogTask.show(out ewErrorCode);
}
public void goodBye()
{
// your function here
if (mEwApplicationX == null)
return;
EwErrorCode ewErrorCode = EwErrorCode.EW_UNDEFINED_ERROR;
EwDialogTaskX ewDialogTask = mEwApplicationX.getEwDialogTask(out ewErrorCode);
if ((ewErrorCode != EwErrorCode.EW_NO_ERROR) || (ewDialogTask == null))
return;
ewDialogTask.setContent("Good Bye.");
ewDialogTask.addButton("Ok", 1);
ewDialogTask.show(out ewErrorCode);
}
[ComRegisterFunctionAttribute]
public static void RegisterFunction(Type t)
{
try
{
Microsoft.Win32.RegistryKey hklm = Microsoft.Win32.Registry.LocalMachine;
Microsoft.Win32.RegistryKey hkcu = Microsoft.Win32.Registry.CurrentUser;
string keyname = "SOFTWARE\\SolidWorks\\SOLIDWORKS Electrical\\AddIns\\{" + t.GUID.ToString() + "}";
Microsoft.Win32.RegistryKey addinkey = hklm.CreateSubKey(keyname);
addinkey.SetValue(null, 0);
addinkey.SetValue("Description", "Your add in description");
addinkey.SetValue("Title", "Your Add-in Name");
addinkey.SetValue("Path", System.Reflection.Assembly.GetExecutingAssembly().Location);
addinkey = hkcu.CreateSubKey(keyname);
addinkey.SetValue("StartUp", Convert.ToInt32(1), Microsoft.Win32.RegistryValueKind.DWord);
}
catch (System.NullReferenceException nl)
{
Console.WriteLine("There was a problem registering this dll. \n\"" + nl.Message + "\"");
}
catch (System.Exception e)
{
Console.WriteLine(e.Message);
}
}
[ComUnregisterFunctionAttribute]
public static void UnregisterFunction(Type t)
{
try
{
Microsoft.Win32.RegistryKey hklm = Microsoft.Win32.Registry.LocalMachine;
Microsoft.Win32.RegistryKey hkcu = Microsoft.Win32.Registry.CurrentUser;
string keyname = "Software\\SolidWorks\\SOLIDWORKS Electrical\\AddIns\\{" + t.GUID.ToString() + "}";
hklm.DeleteSubKey(keyname);
hkcu.DeleteSubKey(keyname);
}
catch (System.NullReferenceException nl)
{
Console.WriteLine("There was a problem unregistering this dll: " + nl.Message);
}
catch (System.Exception e)
{
Console.WriteLine("There was a problem unregistering this dll: " + e.Message);
}
}
}
}
IEwDialogTaskX getEwDialogTask(EwErrorCode *errorCode)
To get the task dialog wrapper.
void setContent(BSTR strContent)
Set the content.
Note
License Code(*): Contact your reseller to get your license code


Provide feedback on this topic

SOLIDWORKS welcomes your feedback concerning the presentation, accuracy, and thoroughness of the documentation. Use the form below to send your comments and suggestions about this topic directly to our documentation team. The documentation team cannot answer technical support questions. Click here for information about technical support.

* Required

 
*Email:  
Subject:   Feedback on Help Topics
Page:   SOLIDWORKS Electrical API: Add-in sample in C#
*Comment:  
*   I acknowledge I have read and I hereby accept the privacy policy under which my Personal Data will be used by Dassault Systèmes

Print Topic

Select the scope of content to print:




x

We have detected you are using a browser version older than Internet Explorer 7. For optimized display, we suggest upgrading your browser to Internet Explorer 7 or newer.

 Never show this message again
x

Web Help Content Version: API Help (English only) 2024 SP05

To disable Web help from within SOLIDWORKS and use local help instead, click Help > Use SOLIDWORKS Web Help.

To report problems encountered with the Web help interface and search, contact your local support representative. To provide feedback on individual help topics, use the “Feedback on this topic” link on the individual topic page.