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

Purpose

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

Note
mEwApplicationX: application object previously created.

Samples

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.

\Example 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);

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

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:821
EwOpenProjectMode
All possible open modes for a project.
Definition: EwEnumeration.idl:434
EwErrorCode doExcelExport()
Export reports in Excel type.
EwFileType
File types used in a project.
Definition: EwEnumeration.idl:352
Use this interface to manage IEwProjectX objects.
Definition: EwProjectObjects.idl:1793
Use this interface to manage the export reports used inside the project.
Definition: EwProjectExportObjects.idl:986
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.
EwErrorCode setEwProjectReportIDArray(VARIANT ewProjectReportIDArray)
Set array of ID of EwProjectReport objects.
IEwEnvironmentX getEwEnvironment(EwErrorCode *errorCode)
EwFileExtension
All file types extension.
Definition: EwEnumeration.idl:510
Use this interface to manage IEwProjectBookX objects.
Definition: EwProjectObjects.idl:788
IEwProjectX findProjectFromFilePath(BSTR strFilePath, EwErrorCode *errorCode)
Return an IEwProjectX from a file path.
EwErrorCode setTargetFolder(BSTR strFolderPath)
Update the exported target folder path.
EwErrorCode openEwProjectID(LONG lID)
Open a project in the application interface.
EwErrorCode setExportAllReports(VARIANT_BOOL bExportAllReports)
Set this flag to true to export all reports of the project.
Use this interface to manage a project.
Definition: EwProjectObjects.idl:1079
EwErrorCode setOneSheetByBreak(VARIANT_BOOL bOneSheetByBreak)
Set this flag to true to export one sheet by break.
EwProjectDataExportType
All data export configuration.
Definition: EwEnumeration.idl:456
EwErrorCode open(EwOpenProjectMode eOpenMode)
open the project
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.
EwErrorCode getSystemInformation(BSTR bstrPath)
Collect information on electrical application.
VARIANT unarchive(BSTR strFilePath, VARIANT_BOOL bWithDependencies, EwErrorCode *errorCode)
Unarchive projects.
Use this interface to manage book inside a project.
Definition: EwProjectObjects.idl:590
EwErrorCode setAddCreatedFileToProject(VARIANT_BOOL bAddCreatedFileToProject)
Set this value to true to add exported files to the project.
EwErrorCode setEwFileExtension(EwFileExtension ewExtension)
Update the report type file extension.


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