SwEx.AddIn Framework enables easy and robust development of add-ins with SOLIDWORKS API

Edit ArticleEdit Article

SwEx.AddIn framework for SOLIDWORKS
SwEx.AddIn framework for SOLIDWORKS

SwEx.AddIn provides utilities for simplified development of SOLIDWORKS add-ins.

The functionality includes

  • Automatic registration of the add-in
  • Simplified commands groups management
  • Events management (future versions)
  • Task Panes, Feature Manager Tab, Model View Tab (future versions)

Source code is available on GitHub

Features

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), Guid("736EEACF-B294-40F6-8541-CFC8E7C5AA61")]
public class SampleAddIn : SwAddInEx
{
    public override bool OnConnect()
    {
        return true;
    }
}

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 bitmaps 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. Use resources to localize the add-in.

[Title(typeof(Resources), nameof(Resources.ToolbarTitle)), Description("Toolbar with commands")]
[Icon(typeof(Resources), nameof(Resources.commands))]
public enum Commands_e
{
    [Title("Command 1"), Description("Sample command 1")]
    [Icon(typeof(Resources), nameof(Resources.command1))]
    [CommandItemInfo(true, true, swWorkspaceTypes_e.Assembly, true, swCommandTabButtonTextDisplay_e.swCommandTabButton_TextBelow)]
    Command1,
    Command2
}
    //...
    AddCommandGroup<Commands_e>(OnButtonClick);
    //...
private void OnButtonClick(Commands_e cmd)
{
    //TODO: handle commands
}

Managing Documents Lifecycle and Events

Framework will manage the lifecycle of documents by wrapping them in the specified class and allows to handle common events:

//...
private IDocumentsHandler<DocumentHandler> m_DocHandler;
//...
    //...
    m_DocHandler = CreateDocumentsHandler();
    m_DocHandler.HandlerCreated += OnDocHandlerCreated;
    //...
private void OnDocHandlerCreated(DocumentHandler doc)
{
    doc.Rebuild += OnDocRebuild;
    doc.Save += OnDocSave;

}

private bool OnDocRebuild(DocumentHandler docHandler, RebuildState_e state)
{
    //TODO: handle rebuild
    return true;
}

private bool OnDocSave(DocumentHandler docHandler, string fileName, SaveState_e state)
{
    //TODO: handle saving
    return true;
}

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:

    //...
    doc.Access3rdPartyData += OnAccess3rdPartyData;
    //...
private void OnAccess3rdPartyData(DocumentHandler docHandler, Access3rdPartyDataState_e state)
{
    const string STREAM_NAME = "CodeStackStream";

    switch (state)
    {
        case Access3rdPartyDataState_e.StreamWrite:
            using (var streamHandler = docHandler.Model.Access3rdPartyStream(STREAM_NAME, true))
            {
                using (var str = streamHandler.Stream)
                {
                    var xmlSer = new XmlSerializer(typeof(string[]));

                    xmlSer.Serialize(str, new string[] { "A", "B" });
                }
            }
            break;
    }
}

Hosting User Controls In SOLIDWORKS Panels

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

Task Pane

public class TaskPaneControl : UserControl
{
}
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;
    }
}

Video Tutorials

Introduction

Detailed Guide


Product of Xarial Product of Xarial