Hide Table of Contents
SOLIDWORKS Electrical API 2024.0.0
Electrical API
Add-in sample in Visual Basic

Purpose

This document explains how to create a Visual Basic dll.

Create Visual Basic project with Visual Studio

  • Open Visual Studio
  • File -> New -> Project
  • Select Visual CBasic -> 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"

Rename Class1.vb file with an appropriate name for the add-in.

Modify code to make the Add-in

Open AssemblyInfo.vb in My Project folder, cut selected lines below:

  • Paste it in the MyVBAddin.vb
  • Verify that you have ComVisible(true) and not ComVisible(false).
  • Add: Imports 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: Imports EwAPI;

if Erreur BC31541 Change EwAPI (in references on the right) to incorporate... to false

Inherit your main class from EwAddIn

Implement those two methods:

Public Class MyVBAddin
'Inherit your main class from EwAddIn
Inherits EwAddInClass
'Implement those two methods:
Private Shared mEwApplicationX As IEwApplicationX
Private Shared mEwInteropFactory As IEwInteropFactoryX
Public Overrides Function connectToEwAPI(ByVal ewInteropFactory As Object) As Boolean
mEwInteropFactory = CType(ewInteropFactory, IEwInteropFactoryX)
If mEwInteropFactory Is Nothing Then
Return False
End If
Dim ewErrorCode As EwErrorCode = EwErrorCode.EW_UNDEFINED_ERROR
mEwApplicationX = mEwInteropFactory.getEwApplication("License Code(*)", ewErrorCode)
If (ewErrorCode <> EwErrorCode.EW_NO_ERROR) OrElse (mEwApplicationX Is Nothing) Then
Return False
End If
hello()
Return True
End Function
Public Overrides Function disconnectFromEwApi() As Boolean
goodBye()
Return True
End Function

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

<ComRegisterFunctionAttribute>
Public Shared Sub RegisterFunction(ByVal t As Type)
Try
Dim hklm As Microsoft.Win32.RegistryKey = Microsoft.Win32.Registry.LocalMachine
Dim hkcu As Microsoft.Win32.RegistryKey = Microsoft.Win32.Registry.CurrentUser
Dim keyname As String = "SOFTWARE\SolidWorks\SOLIDWORKS Electrical\Addins\{" & t.GUID.ToString() & "}"
Dim addinkey As Microsoft.Win32.RegistryKey = hklm.CreateSubKey(keyname)
addinkey.SetValue(Nothing, 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 nl As System.NullReferenceException
Console.WriteLine("There was a problem registering this dll. " & vbLf & """" & nl.Message & """")
Catch e As System.Exception
Console.WriteLine(e.Message)
End Try
End Sub
<ComUnregisterFunctionAttribute>
Public Shared Sub UnregisterFunction(ByVal t As Type)
Try
Dim hklm As Microsoft.Win32.RegistryKey = Microsoft.Win32.Registry.LocalMachine
Dim hkcu As Microsoft.Win32.RegistryKey = Microsoft.Win32.Registry.CurrentUser
Dim keyname As String = "SOFTWARE\SolidWorks\SOLIDWORKS Electrical\Addins\{" & t.GUID.ToString() & "}"
hklm.DeleteSubKey(keyname)
hkcu.DeleteSubKey(keyname)
Catch nl As System.NullReferenceException
Console.WriteLine("There was a problem unregistering this dll: " & nl.Message)
Catch e As System.Exception
Console.WriteLine("There was a problem unregistering this dll: " & e.Message)
End Try
End Sub

Here is an example of implementation:

Imports System.Runtime.InteropServices
Imports EwAPI 'add reference in MyProject
Imports Microsoft.VisualBasic.Strings
Imports System.IO
'Cut from AssemblyInfo.vb in My Project folder
<Assembly: ComVisible(True)>
<Assembly: Guid("9A110FC6-F7D5-4EE0-ADF1-E2E170BA8F52")>
'! [VB_Get_connectToEwAPI]
Public Class MyVBAddin
'Inherit your main class from EwAddIn
Inherits EwAddInClass
'Implement those two methods:
Private Shared mEwApplicationX As IEwApplicationX
Private Shared mEwInteropFactory As IEwInteropFactoryX
Public Overrides Function connectToEwAPI(ByVal ewInteropFactory As Object) As Boolean
mEwInteropFactory = CType(ewInteropFactory, IEwInteropFactoryX)
If mEwInteropFactory Is Nothing Then
Return False
End If
Dim ewErrorCode As EwErrorCode = EwErrorCode.EW_UNDEFINED_ERROR
mEwApplicationX = mEwInteropFactory.getEwApplication("License Code(*)", ewErrorCode)
If (ewErrorCode <> EwErrorCode.EW_NO_ERROR) OrElse (mEwApplicationX Is Nothing) Then
Return False
End If
hello()
Return True
End Function
Public Overrides Function disconnectFromEwApi() As Boolean
goodBye()
Return True
End Function
'! [VB_Get_connectToEwAPI]
Public Function hello() As Boolean
Return True
End Function
Public Function goodBye() As Boolean
Return True
End Function
'Finally add those two methods to your main class to register this Add-in:
'! [VB_Get_Registry]
<ComRegisterFunctionAttribute>
Public Shared Sub RegisterFunction(ByVal t As Type)
Try
Dim hklm As Microsoft.Win32.RegistryKey = Microsoft.Win32.Registry.LocalMachine
Dim hkcu As Microsoft.Win32.RegistryKey = Microsoft.Win32.Registry.CurrentUser
Dim keyname As String = "SOFTWARE\SolidWorks\SOLIDWORKS Electrical\Addins\{" & t.GUID.ToString() & "}"
Dim addinkey As Microsoft.Win32.RegistryKey = hklm.CreateSubKey(keyname)
addinkey.SetValue(Nothing, 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 nl As System.NullReferenceException
Console.WriteLine("There was a problem registering this dll. " & vbLf & """" & nl.Message & """")
Catch e As System.Exception
Console.WriteLine(e.Message)
End Try
End Sub
<ComUnregisterFunctionAttribute>
Public Shared Sub UnregisterFunction(ByVal t As Type)
Try
Dim hklm As Microsoft.Win32.RegistryKey = Microsoft.Win32.Registry.LocalMachine
Dim hkcu As Microsoft.Win32.RegistryKey = Microsoft.Win32.Registry.CurrentUser
Dim keyname As String = "SOFTWARE\SolidWorks\SOLIDWORKS Electrical\Addins\{" & t.GUID.ToString() & "}"
hklm.DeleteSubKey(keyname)
hkcu.DeleteSubKey(keyname)
Catch nl As System.NullReferenceException
Console.WriteLine("There was a problem unregistering this dll: " & nl.Message)
Catch e As System.Exception
Console.WriteLine("There was a problem unregistering this dll: " & e.Message)
End Try
End Sub
'! [VB_Get_Registry]
End Class
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 Visual Basic
*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.