Hide Table of Contents
SOLIDWORKS Electrical API  2021.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);
}

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);
}

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);
}

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();
}
}
}

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();
}

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();
}

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();
}

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);
}

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);
}

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);
}

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);
}
Use this interface to manage the report used inside the project.
Definition: EwProjectExportObjects.idl:782
EwOpenProjectMode
All possible open modes for a project.
Definition: EwEnumeration.idl:443
VARIANT getEwProjectFileArray(EwErrorCode *errorCode)
Return the array of IEwProjectFile of associated project.
EwErrorCode doExcelExport()
Export reports in Excel type.
EwFileType
File types used in a project.
Definition: EwEnumeration.idl:361
Use this interface to manage IEwProjectX objects.
Definition: EwProjectObjects.idl:1862
Use this interface to manage the export reports used inside the project.
Definition: EwProjectExportObjects.idl:947
IEwProjectX findEwProjectByName(BSTR strName, EwErrorCode *errorCode)
Look for the first project from a name.
EwErrorCode
All errors codes for this API.
Definition: EnumDefinition.idl:21
EwErrorCode setEwProjectDataExportType(EwProjectDataExportType eProjectDataExportType)
Update the type of report to export.
EwErrorCode setBookOrFolderId(long lBookOrFolderId)
Set the id of the destination folder or book where the reports will be exported.
IEwEnvironmentX getEwEnvironment(EwErrorCode *errorCode)
EwFileExtension
All file types extension.
Definition: EwEnumeration.idl:519
EwErrorCode getSystemInformation(BSTR strPath)
Collect information on electrical application.
Use this interface to manage IEwProjectBookX objects.
Definition: EwProjectObjects.idl:816
IEwProjectX findProjectFromFilePath(BSTR strFilePath, EwErrorCode *errorCode)
Return an IEwProjectX from a file path.
EwErrorCode setEwProjectReportIDArray(VARIANT vProjectReportIDArray)
Set array of ID of EwProjectReport objects.
EwErrorCode setEwFileExtension(EwFileExtension eFileExtension)
Update the report type file extension.
EwErrorCode setTargetFolder(BSTR strFolderPath)
Update the exported target folder path.
EwErrorCode openEwProjectID(LONG lID)
Open a project in the application interface.
EwNumberWireAction
All the possible actions for renumber wires.
Definition: EnumDefinition.idl:238
EwErrorCode setExportAllReports(VARIANT_BOOL bExportAllReports)
Set this flag to true to export all reports of the project.
EwErrorCode setTag(BSTR strValue)
Set the mark of this object.
Use this interface to manage a project.
Definition: EwProjectObjects.idl:1104
Use this interface to manage a component inside a project.
Definition: EwProjectComponentObjects.idl:297
EwSelectionType
All the selection types.
Definition: EnumDefinition.idl:253
EwErrorCode setOneSheetByBreak(VARIANT_BOOL bOneSheetByBreak)
Set this flag to true to export one sheet by break.
EwProjectDataExportType
All data export configuration.
Definition: EwEnumeration.idl:465
EwErrorCode open(EwOpenProjectMode eOpenMode)
open the project
Use this interface to manage file inside a project.
Definition: EwProjectObjects.idl:355
VARIANT getEwProjectBookArray(EwErrorCode *errorCode)
Return the array of IEwProjectBook of associated project.
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 book inside a project.
Definition: EwProjectObjects.idl:618
Use this interface to manage EwProjectFileManagerx objects.
Definition: EwProjectObjects.idl:662
EwErrorCode setAddCreatedFileToProject(VARIANT_BOOL bAddCreatedFileToProject)
Set this value to true to add exported files to the project.


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) 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.