XCAD.NET
XCAD.NET
Framework for .NET (C# and VB.NET) to create modern application for CAD systems (SOLIDWORKS)
Get technical support Access source code Explore code and application examples

xCAD Framework enables easy and robust development of add-ins with SOLIDWORKS API


xCAD provides utilities for simplified development of SOLIDWORKS add-ins. These types of applications running in-process and enables best user experience.

The functionality includes

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

Features Overview

Although some of the feature below, such as documents and events management, reading or writing to the 3rd party streams can be used from Stand-Alone applications, in most cases this functionality is used within add-ins.

Registering Add-In

To Register add-in just declare a public class and add COMVisible attribute (no need to run custom regasm commands, no need to call any static classes).

[ComVisible(true), Guid("736EEACF-B294-40F6-8541-CFC8E7C5AA61")]
public class SampleAddIn : 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 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, WorkspaceTypes_e.Assembly, true, RibbonTabTextDisplay_e.TextBelow)]
    Command1,
    Command2
}
this.CommandManager.AddCommandGroup<Commands_e>().CommandClick += 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:

public class MyDocumentHandler : SwDocumentHandler
{
    protected override void AttachAssemblyEvents(AssemblyDoc assm)
    {
        assm.FileSaveNotify += OnFileSave;
        assm.RegenNotify += OnRegen;
    }

    protected override void AttachPartEvents(PartDoc part)
    {
        part.FileSaveNotify += OnFileSave;
        part.RegenNotify += OnRegen;
    }

    protected override void AttachDrawingEvents(DrawingDoc draw)
    {
        draw.FileSaveNotify += OnFileSave;
        draw.RegenNotify += OnRegen;
    }

    private int OnFileSave(string FileName)
    {
        //TODO: handle saving
        return S_OK;
    }

    private int OnRegen()
    {
        //TODO: handle rebuild
        return S_OK;
    }

    protected override void DetachAssemblyEvents(AssemblyDoc assm)
    {
        assm.FileSaveNotify -= OnFileSave;
        assm.RegenNotify -= OnRegen;
    }

    protected override void DetachPartEvents(PartDoc part)
    {
        part.FileSaveNotify -= OnFileSave;
        part.RegenNotify -= OnRegen;
    }

    protected override void DetachDrawingEvents(DrawingDoc draw)
    {
        draw.FileSaveNotify -= OnFileSave;
        draw.RegenNotify -= OnRegen;
    }
}
Application.Documents.RegisterHandler<MyDocumentHandler>();

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:

Application.Documents.Active.StreamWriteAvailable += OnWriteToStream;
private void OnWriteToStream(IXDocument doc)
{
    const string STREAM_NAME = "CodeStackStream";

    using (var str = doc.OpenStream(STREAM_NAME, AccessType_e.Write))
    {
        var xmlSer = new XmlSerializer(typeof(string[]));

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

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
}
var taskPane = this.CreateTaskPane<TaskPaneControl, TaskPaneCommands_e>();
taskPane.ButtonClick += OnTaskPaneCommandClick;
TaskPaneControl ctrl = taskPane.Control;
private void OnTaskPaneCommandClick(TaskPaneCommands_e cmd)
{
    switch (cmd)
    {
        case TaskPaneCommands_e.Command1:
            //TODO: handle command
            break;
    }
}


Powered by Docify