Hide Table of Contents
SOLIDWORKS Electrical API 2023.0.0
Electrical API
C# Samples

Purpose

This document presents some samples of using API in C#.

Note
mEwApplicationX: application object previously created.

Create a new project

Create a project from specified template

public void createProjectFromTemplate()
{
EwErrorCode ewErrorCode = EwErrorCode.EW_UNDEFINED_ERROR;
EwEnvironmentX ewEnvironment = mEwApplicationX.getEwEnvironment(out ewErrorCode);
IEwProjectManagerX iEwProjectManagerX = ewEnvironment.getEwProjectManager(out ewErrorCode);
// strFilePath : The full path of the template file
Array lIdArray = iEwProjectManagerX.unarchive(strFilePath, true, out ewErrorCode);
// ID of project is unique
long lId = (long)lIdArray.GetValue(0);
IEwProjectX ewProjectX = iEwProjectManagerX.findEwProjectByID((int)lId, out ewErrorCode);
// Open the project in the application
ewErrorCode = mEwApplicationX.openEwProjectID((int) lId);
}
EwErrorCode
All errors codes for this API.
Definition: EnumDefinition.idl:21
Use this interface to manage IEwProjectX objects.
Definition: EwProjectObjects.idl:2398
IEwProjectX findEwProjectByID(LONG lId, EwErrorCode *errorCode)
Look for a project from an ID.
VARIANT unarchive(BSTR strFilePath, VARIANT_BOOL bWithDependencies, EwErrorCode *errorCode)
Unarchive projects.
Use this interface to manage a project.
Definition: EwProjectObjects.idl:1557

Open a project

Open a project from name

public void openProjectbyName()
{
EwErrorCode ewErrorCode = EwErrorCode.EW_UNDEFINED_ERROR;
EwEnvironmentX ewEnvironment = mEwApplicationX.getEwEnvironment(out ewErrorCode);
IEwProjectManagerX iEwProjectManagerX = ewEnvironment.getEwProjectManager(out ewErrorCode);
// Return the first project with this name
IEwProjectX iEwProjectX = iEwProjectManagerX.findEwProjectByName("FooProject", out ewErrorCode);
ewErrorCode = iEwProjectX.open(EwOpenProjectMode.kOpenForWrite);
}
EwOpenProjectMode
All possible open modes for a project.
Definition: EwEnumeration.idl:474
IEwProjectX findEwProjectByName(BSTR strName, EwErrorCode *errorCode)
Look for the first project from a name.
EwErrorCode open(EwOpenProjectMode eOpenMode)
open the project

Open a project from id

public void openProjectbyId(int lID)
{
EwErrorCode ewErrorCode = EwErrorCode.EW_UNDEFINED_ERROR;
EwEnvironmentX ewEnvironment = mEwApplicationX.getEwEnvironment(out ewErrorCode);
IEwProjectManagerX iEwProjectManagerX = ewEnvironment.getEwProjectManager(out ewErrorCode);
// ID of project is unique
IEwProjectX iEwProjectX = iEwProjectManagerX.findEwProjectByID(lID, out ewErrorCode);
ewErrorCode = iEwProjectX.open(EwOpenProjectMode.kOpenForWrite);
}

Open a project from id in the Application

public void openProjectbyName(int lID)
{
EwErrorCode ewErrorCode = EwErrorCode.EW_UNDEFINED_ERROR;
// ID of project is unique
ewErrorCode = mEwApplicationX.openEwProjectID(lID);
}

Open a project from file path

public void openProjectbyFilePath()
{
EwErrorCode ewErrorCode = EwErrorCode.EW_UNDEFINED_ERROR;
EwEnvironmentX ewEnvironment = mEwApplicationX.getEwEnvironment(out ewErrorCode);
IEwProjectManagerX iEwProjectManagerX = ewEnvironment.getEwProjectManager(out ewErrorCode);
IEwProjectX iEwProjectX = iEwProjectManagerX.findProjectFromFilePath("FilePath", out ewErrorCode);
ewErrorCode = iEwProjectX.open(EwOpenProjectMode.kOpenForWrite);
}
IEwProjectX findProjectFromFilePath(BSTR strFilePath, EwErrorCode *errorCode)
Return an IEwProjectX from a file path.

Use project file

Note
mEwProjectX: project object previously created.

Delete a Project file

public void deleteProjectFile()
{
EwErrorCode ewErrorCode = EwErrorCode.EW_UNDEFINED_ERROR;
EwProjectFileManagerX ewProjectFileManager = mEwProjectX.getEwProjectFileManager(out ewErrorCode);
if (ewProjectFileManager.getCount(out ewErrorCode) > 0)
{
EwProjectFileX[] array = ewProjectFileManager.getEwProjectFileArray(out ewErrorCode);
for (int i = 0; i < ewProjectFileManager.getCount(out ewErrorCode); i++)
{
EwProjectFileX ewProjectFile = array[0];
// To delete all the Line Diagram files from the current project
if(ewProjectFile.getFileType() == EwFileType.kFileLineDiagram)
ewErrorCode = ewProjectFile.remove();
}
}
}
EwFileType
File types used in a project.
Definition: EwEnumeration.idl:386

Open a project file

To do that you must use the command EWOPEN command.

EWOPENFILE "D:\\ProgramData\\...\\Projects\\1216\\Drawings\\11319.ewg"

This command takes one parameter, the file path to open. To obtain this path use the getFilePath API from EwProjectFile object.

EwErrorCode ewErrorCode = EwErrorCode.EW_UNDEFINED_ERROR;
...
/* ewProjectFile: the file to open. Project must be opened and must be the current one.*/
string strFilePath = ewProjectFile.getFilePath();
string strCommand = "EWOPEN ";
strCommand += strFilePath;
mEwApplicationX.runcommand(strCommand);

Get the project file description

public void descriptionFileProject()
{
EwErrorCode ewErrorCode = EwErrorCode.EW_UNDEFINED_ERROR;
EwProjectFileManagerX ewProjectFileManager = mEwProjectX.getEwProjectFileManager(out ewErrorCode);
if (ewProjectFileManager.getCount(out ewErrorCode) > 0)
{
EwProjectFileX[] array = ewProjectFileManager.getEwProjectFileArray(out ewErrorCode);
EwProjectConfigurationX mEwProjectConfigurationX = mEwProjectX.getEwProjectConfiguration(out ewErrorCode);
string strLanguage = mEwProjectConfigurationX.getCurrentCodeLanguage(out ewErrorCode);
for (int i = 0; i < ewProjectFileManager.getCount(out ewErrorCode); i++)
{
EwProjectFileX ewProjectFile = array[0];
// Get the description of the project file
string strDescription = ewProjectFile.getTranslatedData(0, strLanguage, out ewErrorCode);
}
}
}

Insert a symbol in a project file

public void insertSymbol()
{
EwErrorCode ewErrorCode = EwErrorCode.EW_UNDEFINED_ERROR;
EwProjectComponentManagerX ewProjectComponentManager = mEwProjectX.getEwProjectComponentManager(out ewErrorCode);
IEwProjectComponentX ewProjectComponentX = ewProjectComponentManager.newEwProjectComponent();
ewProjectComponentX.setTag("ComponentMark");
ewErrorCode = ewProjectComponentX.insert();
mEwProjectSymbolX = ewProjectFileX.newEwProjectSymbol(out ewErrorCode);
mEwProjectSymbolX.setEwSymbolName("TR-DI007");
mEwProjectSymbolX.setRotationAngle(90);
mEwProjectSymbolX.setXPosition(150);
mEwProjectSymbolX.setYPosition(100);
mEwProjectSymbolX.setObjectID(ewProjectComponentX.getID());
ewErrorCode = mEwProjectSymbolX.insert();
}
EwErrorCode insert()
Insert the object inside the current project.
LONG getID()
Use this interface to manage a component inside a project.
Definition: EwProjectComponentObjects.idl:522
EwErrorCode setTag(BSTR strValue)
Set the mark of this object.

Use classification

Get a class name

public void classificationGetClassName()
{
EwErrorCode ewErrorCode = EwErrorCode.EW_UNDEFINED_ERROR;
IEwClassificationManagerX iEwClassificationManagerX = mEwEnvironment.getEwClassificationManager(out ewErrorCode);
IEwClassificationX iEwClassificationX = iEwClassificationManagerX.getEwClassification(EwClassificationType.kCableReferenceType, out ewErrorCode);
dynamic ewClassArray = iEwClassificationManagerX.getEwClassArray(out ewErrorCode);
foreach(IEwClassX iclass in ewClassArray)
{
string strClassComplete = "";
string strClass = iclass.getTranslatedData(0, "en", out ewErrorCode);
IEwClassX ewClassX = iEwClassificationManagerX.findEwClassByID(iclass.getParentID(out ewErrorCode), out ewErrorCode);
string strParent = "";
if (ewClassX != null)
{
strParent = ewClassX.getTranslatedData(0, "en", out ewErrorCode);
strClassComplete = strParent + "\\" + strClass;
}
else
strClassComplete = strClass;
}
}
EwClassificationType
Classification for components, symbols and manufacturer parts Enum Change due to TEW-49235 kComponent...
Definition: EwEnumeration.idl:580
Use this interface to manage an EwClassX object.
Definition: EwClassificationObjects.idl:25
LONG getParentID(EwErrorCode *errorCode)
Return the ID of the parent of the class.
Use this interface to manage EwClassificationX objects.
Definition: EwClassificationObjects.idl:392
IEwClassificationX getEwClassification(EwClassificationType eClassificationType, EwErrorCode *errorCode)
Get an interface of classificationX from classification type.
IEwClassX findEwClassByID(long lID, EwErrorCode *errorCode)
Get node find by id in current classification.
VARIANT getEwClassArray(EwErrorCode *errorCode)
Get array of all nodes found in current classification.
Use this interface to manage an IEwClassificationX object.
Definition: EwClassificationObjects.idl:214
BSTR getTranslatedData(LONG nUserDataNumber, BSTR strLanguage, EwErrorCode *errorCode)
Get the value of a translated text.

Get a class name from a component

public void classificationGetClassNameByComponent()
{
EwErrorCode ewErrorCode = EwErrorCode.EW_UNDEFINED_ERROR;
IEwClassificationManagerX iEwClassificationManagerX = mEwEnvironment.getEwClassificationManager(out ewErrorCode);
EwProjectComponentX ewComponent = nullptr;
...
int iClassId = ewComponent.getClassComponentType(out ewErrorCode);
IEwClassX ewClassX = iEwClassificationManagerX.findEwClassByID(iClassId, out ewErrorCode);
string strClassName = ewClassX.getTranslatedData(0, "en", out ewErrorCode);
}

Export report

Export report in Excel

public void doExportReportIn()
{
EwErrorCode ewErrorCode = EwErrorCode.EW_UNDEFINED_ERROR;
IEwProjectExportReportX mIEwProjectExportReportX = mEwProjectX.newEwProjectExportReport(out ewErrorCode);
string strTempFolder = Path.GetTempPath();
ewErrorCode = mIEwProjectExportReportX.setTargetFolder(strTempFolder);
ewErrorCode = mIEwProjectExportReportX.setExportAllReports(true);
ewErrorCode = mIEwProjectExportReportX.setEwProjectDataExportType(EwProjectDataExportType.kProjectDataExportReport);
ewErrorCode = mIEwProjectExportReportX.setEwFileExtension(EwFileExtension.kFileExtensionXLS);
ewErrorCode = mIEwProjectExportReportX.setOneSheetByBreak(true);
ewErrorCode = mIEwProjectExportReportX.doExcelExport();
}
EwProjectDataExportType
All data export configuration.
Definition: EwEnumeration.idl:497
EwFileExtension
All file types extension.
Definition: EwEnumeration.idl:554
Use this interface to manage the export reports used inside the project.
Definition: EwProjectExportObjects.idl:997
EwErrorCode setExportAllReports(VARIANT_BOOL bExportAllReports)
Set this flag to true to export all reports of the project.
EwErrorCode doExcelExport()
Export reports in Excel type.
EwErrorCode setOneSheetByBreak(VARIANT_BOOL bOneSheetByBreak)
Set this flag to true to export one sheet by break.
EwErrorCode setTargetFolder(BSTR strFolderPath)
Update the exported target folder path.
EwErrorCode setEwFileExtension(EwFileExtension eFileExtension)
Update the report type file extension.
EwErrorCode setEwProjectDataExportType(EwProjectDataExportType eProjectDataExportType)
Update the type of report to export.

Export specified report in Excel

public void exportSpecifiedReportInExcel()
{
EwErrorCode ewErrorCode = EwErrorCode.EW_UNDEFINED_ERROR;
EwProjectReportManagerX ewProjectReportManagerX = mEwProjectX.getEwProjectReportManager(out ewErrorCode);
string strToFolder = Path.GetTempPath() + Guid.NewGuid() + "\\";
if (Directory.Exists(strToFolder) == true)
Directory.Delete(strToFolder, true);
Directory.CreateDirectory(strToFolder);
ewErrorCode = mIEwProjectExportReportX.setTargetFolder(strToFolder);
ewErrorCode = mIEwProjectExportReportX.setExportAllReports(false);
ewErrorCode = mIEwProjectExportReportX.setEwProjectDataExportType(EwProjectDataExportType.kProjectDataExportReport);
ewErrorCode = mIEwProjectExportReportX.setEwFileExtension(EwFileExtension.kFileExtensionXLS);
ewErrorCode = mIEwProjectExportReportX.setAddCreatedFileToProject(false);
IEwProjectBookManagerX iEwProjectBookManagerX = mEwProjectX.getEwProjectBookManager(out ewErrorCode);
Array array = iEwProjectBookManagerX.getEwProjectBookArray(out ewErrorCode);
IEwProjectBookX ewProjectBook = (IEwProjectBookX)array.GetValue(0);
int lBookOrFolderId = ewProjectBook.getID();
ewErrorCode = mIEwProjectExportReportX.setBookOrFolderId(lBookOrFolderId);
long[] aArrayId = new long[2];
IEwProjectReportX ewProjectReport = ewProjectReportManagerX.find("Folio_Metric.xml", "", out ewErrorCode);
if (ewProjectReport == null)
return;
aArrayId[0] = ewProjectReport.getID();
ewProjectReport = ewProjectReportManagerX.find(“CableGroupedByReference_Metric.xml”, "", out ewErrorCode)
if (ewProjectReport == null)
return;
aArrayId[1] = ewProjectReport.getID();
ewErrorCode = mIEwProjectExportReportX.setEwProjectReportIDArray(aArrayId);
ewErrorCode = mIEwProjectExportReportX.doExcelExport();
}
Use this interface to manage IEwProjectBookX objects.
Definition: EwProjectObjects.idl:1260
VARIANT getEwProjectBookArray(EwErrorCode *errorCode)
Return the array of IEwProjectBook of associated project.
Use this interface to manage book inside a project.
Definition: EwProjectObjects.idl:1043
EwErrorCode setBookOrFolderId(long lBookOrFolderId)
Set the id of the destination folder or book where the reports will be exported.
EwErrorCode setEwProjectReportIDArray(VARIANT vProjectReportIDArray)
Set array of ID of EwProjectReport objects.
EwErrorCode setAddCreatedFileToProject(VARIANT_BOOL bAddCreatedFileToProject)
Set this value to true to add exported files to the project.
Use this interface to manage the report used inside the project.
Definition: EwProjectExportObjects.idl:825

Renumber Wire

Renumber Wire Manually

public void doRenumberWireManuallyInABook()
{
EwErrorCode ewErrorCode = EwErrorCode.EW_UNDEFINED_ERROR;
EwProjectNumberWiresX ewProjectNumberWires = mEwProjectX.newEwProjectNumberWires(out ewErrorCode);
long[] aBookArrayId = new long[1];
aBookArrayId[0] = 1;
ewErrorCode = mEwProjectNumberWires.setSelectionType(EwSelectionType.kSelectionBook);
ewErrorCode = mEwProjectNumberWires.setSelection(aBookArrayId);
ewErrorCode = mEwProjectNumberWires.process(action);
}
public void doRenumberWireManuallyInScheme()
{
EwErrorCode ewErrorCode = EwErrorCode.EW_UNDEFINED_ERROR;
EwProjectNumberWiresX ewProjectNumberWires = mEwProjectX.newEwProjectNumberWires(out ewErrorCode);
IEwProjectFileManagerX ewProjectFileManagerX = mEwProjectX.getEwProjectFileManager(out ewErrorCode);
Array aFileArray = ewProjectFileManagerX.getEwProjectFileArray(out ewErrorCode);
int iSize = aFileArray.Length;
long[] aFileArrayId = new long[iSize];
for (int i = 0; i < iSize; i++)
{
IEwProjectFileX ewProjectFileX = (IEwProjectFileX)aFileArray.GetValue(i);
aFileArrayId[i] = ewProjectFileX.getID();
}
ewErrorCode = mEwProjectNumberWires.setSelectionType(EwSelectionType.kSelectionScheme);
ewErrorCode = mEwProjectNumberWires.setSelection(aFileArrayId);
ewErrorCode = mEwProjectNumberWires.process(action);
}
EwSelectionType
All the selection types.
Definition: EnumDefinition.idl:298
Use this interface to manage EwProjectFileManagerx objects.
Definition: EwProjectObjects.idl:1088
VARIANT getEwProjectFileArray(EwErrorCode *errorCode)
Return the array of IEwProjectFile of associated project.
Use this interface to manage file inside a project.
Definition: EwProjectObjects.idl:757

Renumber Wire

public void doRenumberWireAutomatic()
{
EwErrorCode ewErrorCode = EwErrorCode.EW_UNDEFINED_ERROR;
EwProjectNumberWiresX ewProjectNumberWires = mEwProjectX.newEwProjectNumberWires(out ewErrorCode);
ewErrorCode = ewProjectNumberWires.setActionOnManualNumber(false);
ewErrorCode = ewProjectNumberWires.setResetPosition(false);
ewErrorCode = ewProjectNumberWires.setSelectionType(EwSelectionType.kSelectionAll);
ewErrorCode = ewProjectNumberWires.process(EwNumberWireAction.kReNumberWiresAction);
}
EwNumberWireAction
All the possible actions for renumber wires.
Definition: EnumDefinition.idl:283

Add Origin-Destination Arrow

Add Origin-Destination Arrow Manually

public void doOriginDestinationArrowManually()
{
EwErrorCode ewErrorCode = EwErrorCode.EW_UNDEFINED_ERROR;
// Add Origin Arrow at the end of the First wire
EwProjectSymbolX ewProjectSymbolX = mEwProjectFile.newEwProjectSymbol(out ewErrorCode);
//The Symbol used for the Origin Arrow
ewProjectSymbolX.setEwSymbolName("TR-OLINK");
ewErrorCode = ewProjectSymbolX.setXPosition(160;
ewErrorCode = ewProjectSymbolX.setYPosition(180);
ewErrorCode = ewProjectSymbolX.setRotationAngle(90);
ewErrorCode = ewProjectSymbolX.insert();
// If no component had set before the insertion of a symbol, they are a default new component created.
// We get the ID of this new component to set it for the destination arrow symbol
int iComponentID = ewProjectSymbolX.getObjectID(out ewErrorCode);
// Add Destination Arrow at the begin of the second wire, in the same scheme or in another scheme.
// In this sample, the second wire is in the same scheme.
ewProjectSymbolX = mEwProjectFile.newEwProjectSymbol(out ewErrorCode);
ewProjectSymbolX.setEwSymbolName("TR-ILINK");
ewErrorCode = ewProjectSymbolX.setXPosition(180);
ewErrorCode = ewProjectSymbolX.setYPosition(180);
ewErrorCode = ewProjectSymbolX.setRotationAngle(90);
// Here we set the previous component ID for the new destination symbol.
// With this, the two symbols (origin and destination arrow) have the same component and are connect each other.
ewErrorCode = ewProjectSymbolX.setObjectID(iComponentID);
ewErrorCode = ewProjectSymbolX.insert();
// Renumber Wire to update information of the connected wire
EwProjectNumberWiresX ewProjectNumberWires = mEwProjectX.newEwProjectNumberWires(out ewErrorCode);
ewErrorCode = ewProjectNumberWires.setActionOnManualNumber(false);
ewErrorCode = ewProjectNumberWires.setResetPosition(false);
ewErrorCode = ewProjectNumberWires.setSelectionType(EwSelectionType.kSelectionAll);
ewErrorCode = ewProjectNumberWires.process(EwNumberWireAction.kReNumberWiresAction);
}

Add Origin-Destination Arrow Automatically

public void doOriginDestinationArrowAutomatically()
{
EwErrorCode ewErrorCode = EwErrorCode.EW_UNDEFINED_ERROR;
EwProjectAutomaticArrowsX mEwProjectAutomaticArrows;
// ...
mEwProjectAutomaticArrows = mEwProjectX.newEwProjectAutomaticArrows(out ewErrorCode);
ewErrorCode = mEwProjectAutomaticArrows.setActionOnManual(false);
ewErrorCode = mEwProjectAutomaticArrows.setSelectionType(EwSelectionType.kSelectionAll);
ewErrorCode = mEwProjectAutomaticArrows.setEquipotentialConnectionType(EwAutoArrowMarkType.kAutoArrowMarkManualAndAuto);
ewErrorCode = mEwProjectAutomaticArrows.setLocationConnectionType(EwAutoArrowLocConnectType.kAutoArrowLocConnectAllLocation);
ewErrorCode = mEwProjectAutomaticArrows.process(EwAutoArrowActionType.kAutoArrowActionAutoConnect);
}
EwAutoArrowActionType
Enum which contains all action for the origin destination automatic insertion.
Definition: EwEnumeration.idl:910
EwAutoArrowMarkType
Enum which contains marks type that we want to be aware for the origin destination automatic insertio...
Definition: EwEnumeration.idl:899
EwAutoArrowLocConnectType
Enum which contains all connection types related with the file location for the Origin Destination au...
Definition: EwEnumeration.idl:887

Some samples with the application

Collect information on electrical application

public void collectInformation()
{
EwErrorCode ewErrorCode = EwErrorCode.EW_UNDEFINED_ERROR;
string strPathAndName = Path.Combine(Path.GetTempPath(), "getSystemInformation\\"); // or any path that you want
if (Directory.Exists(strPathAndName) == true)
Directory.Delete(strPathAndName, true);
Directory.CreateDirectory(strPathAndName);
EwErrorCode ewErrorCode = mEwApplicationX.getSystemInformation(strPathAndName);
string[] arrayOfFiles = Directory.GetFiles(strPathAndName);
}


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: C# Samples
*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) 2023 SP04

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.