Artem Taturevych

Introduction of SwEx Framework for advanced development of SOLIDWORKS add-ins

Discussion created by Artem Taturevych on Aug 26, 2018

SwEx is a free and open-source framework which is designed to simplify development of SOLIDWORKS add-ins in .NET (C# and VB.NET).

Frameworks hides the complexity and allows developing robust solutions which are scalable, type safe, reliable and easily maintainable.

Main features of the framework:

Registering Add-In:

To Register add-in just add the AutoRegister attribute (no need to run custom regasm commands, no need to call any static classes)

[AutoRegister("My C# SOLIDWORKS Add-In", "Sample SOLIDWORKS add-in in C#", true)]
[ComVisible(true)]
public class SwExportComponentAddIn : SwAddInEx
{
}

Adding Commands:

Commands can be defined by creating an enumerations. Commands can be customized by adding attributes to assign title, tooltip, icon etc. Commands can be grouped under sub menus. Simply specify the image (transparency is supported) and framework will create required bimaps compatible with SOLIDWORKS. No need to assign gray background to enable transparency, no need to scale images to fit the required sizes - simply use any image and framework will do the rest.

[Title(typeof(Resources), nameof(Resources.ToolbarTitle))]
[Description("Exports selected component to neutral format")]
[Icon(typeof(Resources), nameof(Resources.export))]
public enum ExportCommands_e
{
[Title("Export To Parasolid")]
[Description("Exports selected component to parasolid")]
[Icon(typeof(Resources), nameof(Resources.export_parasolid))]
[CommandItemInfo(true, true, swWorkspaceTypes_e.Assembly,
true, swCommandTabButtonTextDisplay_e.swCommandTabButton_TextBelow)]
Parasolid,

[Title("Export To Iges")]
[Description("Exports selected component to iges")]
[Icon(typeof(Resources), nameof(Resources.export_iges))]
[CommandItemInfo(true, true, swWorkspaceTypes_e.Assembly,
true, swCommandTabButtonTextDisplay_e.swCommandTabButton_TextBelow)]
Iges,

[Title("Export To Step")]
[Description("Exports selected component to step")]
[Icon(typeof(Resources), nameof(Resources.export_step))]
[CommandItemInfo(true, true, swWorkspaceTypes_e.Assembly,
true, swCommandTabButtonTextDisplay_e.swCommandTabButton_TextBelow)]
Step
}
...
this.AddCommandGroup<ExportCommands_e>(OnButtonClick);
...
private void OnButtonClick(ExportCommands_e cmd)
{
switch (cmd)
{
case ExportCommands_e.Parasolid:
//TODO: do something
break;
}
}

Managing Documents Lifecycle

Framework will manage the lifecycle of documents by wrapping them in the specified class:

private IDocumentsHandler<MyDocHandler> m_DocHandler;
...
m_DocHandler = CreateDocumentsHandler<MyDocHandler>();
...
public class MyDocHandler : DocumentHandler
{
public override void OnInit()
{
//TODO: init
}

public override void OnDestroy()
{
//TODO: release
}
}

Reading and Writing to 3rd Party Storage and Store

It has never been easier to read and write data to the internal SOLIDWORKS file storage. Simply override the corresponding event and serialize/deserialize the data using XML, DataContract, Binary etc. serializers:

public override void OnSaveToStream()
{
using (var streamHandler = Model.Access3rdPartyStream(STREAM_NAME, true))
{
using (var str = streamHandler.Stream)
{
var xmlSer = new XmlSerializer(typeof(RevData));

xmlSer.Serialize(str, m_RevData);
}
}
}

Hosting User Controls In SOLIDWORKS Panels

Just specify User Control to host and framework will do the rest:

public enum TaskPaneCommands_e
{
Command1
}

...
TaskPaneControl ctrl;
var taskPaneView = CreateTaskPane<TaskPaneControl, TaskPaneCommands_e>(OnTaskPaneCommandClick, out ctrl);
...

private void OnTaskPaneCommandClick(TaskPaneCommands_e cmd)
{
switch (cmd)
{
case TaskPaneCommands_e.Command1:
//TODO: handle command
break;
}
}

More information: SwEx.AddIn

Outcomes