Hide Table of Contents
SOLIDWORKS Electrical API  2021.0.0
Electrical API
Add a command sample in C#

Create C# class with Visual Studio

  • Open your project in Visual Studio
  • Select your project and click on
  • Add -> New Item...
  • Select Visual C# Items -> Class
  • Rename your class here with EwUsersCommandX

Click OK.

Create a new Command

For create a new command derive your EwUsersCommandX class with the EwCommandX interface.

public class EwUsersCommandX : EwCommandX
{

After that you must override 4 functions.

The Second step is to:

  • give a unique name of the command.
  • give a description of the command.
  • give a flag instruction (see help for more information).
public string getName(out EwErrorCode errorCode)
{
errorCode = EwErrorCode.EW_NO_ERROR;
return "HelloWorld";
}
public string getDescription(out EwErrorCode errorCode)
{
errorCode = EwErrorCode.EW_NO_ERROR;
return "An Hello World Message";
}
public int getFlags(out EwErrorCode errorCode)
{
errorCode = EwErrorCode.EW_NO_ERROR;
return (int) (EwCommandType.kCommandSession | EwCommandType.kCommandNoUndoMarker);
}

The next step is to code the goal of your command.

public EwErrorCode execute(EwCommandContextX commandContextX)
{
// your function here: Sample
EwErrorCode ewErrorCode = EwErrorCode.EW_UNDEFINED_ERROR;
if (mEwApplicationX == null) // mEwApplicationX declare previously see
return ewErrorCode;
EwDialogTaskX ewDialogTask = mEwApplicationX.getEwDialogTask(out ewErrorCode);
if ((ewErrorCode != EwErrorCode.EW_NO_ERROR) || (ewDialogTask == null))
return ewErrorCode;
ewDialogTask.setContent("Hello World.");
ewDialogTask.addButton("Ok", 1);
ewDialogTask.show(out ewErrorCode);
return ewErrorCode;
}

Here is a C# example of implementation:

using System.Runtime.InteropServices;
using EwAPI;
namespace UsersCommand
{
[ComVisible(true)]
public class EwUsersCommandX : EwCommandX
{
public IEwApplicationX mEwApplicationX = null;
public string getName(out EwErrorCode errorCode)
{
errorCode = EwErrorCode.EW_NO_ERROR;
return "HelloWorld";
}
public string getDescription(out EwErrorCode errorCode)
{
errorCode = EwErrorCode.EW_NO_ERROR;
return "An Hello World Message";
}
public int getFlags(out EwErrorCode errorCode)
{
errorCode = EwErrorCode.EW_NO_ERROR;
return (int) (EwCommandType.kCommandSession | EwCommandType.kCommandNoUndoMarker);
}
public EwErrorCode execute(EwCommandContextX commandContextX)
{
// your function here: Sample
EwErrorCode ewErrorCode = EwErrorCode.EW_UNDEFINED_ERROR;
if (mEwApplicationX == null) // mEwApplicationX declare previously see
return ewErrorCode;
EwDialogTaskX ewDialogTask = mEwApplicationX.getEwDialogTask(out ewErrorCode);
if ((ewErrorCode != EwErrorCode.EW_NO_ERROR) || (ewDialogTask == null))
return ewErrorCode;
ewDialogTask.setContent("Hello World.");
ewDialogTask.addButton("Ok", 1);
ewDialogTask.show(out ewErrorCode);
return ewErrorCode;
}
public void test()
{
mEwApplicationX.runCommand("HelloWorld");
}
}
}

How to use a new Command

Now it's time to add the new command to

public void addCommand()
{
EwErrorCode ewErrorCode = EwErrorCode.EW_UNDEFINED_ERROR;
IEwCommandManagerX ewCommandManager = mEwApplicationX.getEwCommandManager(out ewErrorCode);
if ((ewErrorCode != EwErrorCode.EW_NO_ERROR) || (ewCommandManager == null))
return;
EwUsersCommandX ewCommand = new EwUsersCommandX();
ewErrorCode = ewCommandManager.addCommand(ewCommand);
}

And to test it.

public void test()
{
mEwApplicationX.runCommand("HelloWorld");
}

To knowing the list of parameters of the last command

public void getParameterList()
{
EwErrorCode ewErrorCode = EwErrorCode.EW_UNDEFINED_ERROR;
IEwCommandManagerX ewCommandManager = mEwApplicationX.getEwCommandManager(out ewErrorCode);
if ((ewErrorCode != EwErrorCode.EW_NO_ERROR) || (ewCommandManager == null))
return;
dynamic vArray = ewCommandManager.getParameterList(out ewErrorCode);
int iSize = vArray.Length;
for(int i = 0; i < iSize; i++)
{
string strParam = vArray[i];
}
}

And to remove the command

public void removeCommand()
{
EwErrorCode ewErrorCode = EwErrorCode.EW_UNDEFINED_ERROR;
IEwCommandManagerX ewCommandManager = mEwApplicationX.getEwCommandManager(out ewErrorCode);
if ((ewErrorCode != EwErrorCode.EW_NO_ERROR) || (ewCommandManager == null))
return;
ewErrorCode = ewCommandManager.removeCommand("HelloWorld");
}

Here is a C# example of use:

using System;
using System.Runtime.InteropServices;
using EwAPI;
namespace UsersCommand
{
public class EwUserCommandManagerX
{
public void addCommand()
{
EwErrorCode ewErrorCode = EwErrorCode.EW_UNDEFINED_ERROR;
IEwCommandManagerX ewCommandManager = mEwApplicationX.getEwCommandManager(out ewErrorCode);
if ((ewErrorCode != EwErrorCode.EW_NO_ERROR) || (ewCommandManager == null))
return;
EwUsersCommandX ewCommand = new EwUsersCommandX();
ewErrorCode = ewCommandManager.addCommand(ewCommand);
}
public void removeCommand()
{
EwErrorCode ewErrorCode = EwErrorCode.EW_UNDEFINED_ERROR;
IEwCommandManagerX ewCommandManager = mEwApplicationX.getEwCommandManager(out ewErrorCode);
if ((ewErrorCode != EwErrorCode.EW_NO_ERROR) || (ewCommandManager == null))
return;
ewErrorCode = ewCommandManager.removeCommand("HelloWorld");
}
public void getParameterList()
{
EwErrorCode ewErrorCode = EwErrorCode.EW_UNDEFINED_ERROR;
IEwCommandManagerX ewCommandManager = mEwApplicationX.getEwCommandManager(out ewErrorCode);
if ((ewErrorCode != EwErrorCode.EW_NO_ERROR) || (ewCommandManager == null))
return;
dynamic vArray = ewCommandManager.getParameterList(out ewErrorCode);
int iSize = vArray.Length;
for(int i = 0; i < iSize; i++)
{
string strParam = vArray[i];
}
}
}
}
Note
mEwApplicationX: application object previously created.
void setContent(BSTR strContent)
Set the content.
EwErrorCode addCommand(IEwCommandX *iCommand)
Add an object derived from IEwCommandX.
EwErrorCode
All errors codes for this API.
Definition: EnumDefinition.idl:21
IEwCommandManagerX getEwCommandManager(EwErrorCode *errorCode)
Get the IEwCommandManagerX command manager interface.
EwErrorCode removeCommand(BSTR strCommandName)
Remove a command.
EwCommandType
Flag type for custom commands created by users.
Definition: EwEnumeration.idl:679
EwErrorCode runCommand(BSTR strCommand)
Run a command.
Use this manager to add or remove custom commands.
Definition: EwApplicationObjects.idl:842
VARIANT getParameterList(EwErrorCode *errorCode)
Return the list of parameters of the last command executed.
IEwDialogTaskX getEwDialogTask(EwErrorCode *errorCode)
To get the task dialog wrapper.
Use this interface to manage the application.
Definition: EwAPI.idl:622


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 a command 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) 2021 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.