Handling the common events of SOLIDWORKS file using SwEx.AddIn framework

Edit ArticleEdit Article

SwEx.AddIn framework exposes the common events via generic DocumentHandler:

  • Save
  • Selection
  • Access3rdPartyData
  • CustomPropertyModify
  • ItemModify
  • ConfigurationChange
  • Rebuild
  • Dimension Change

Call the ISwAddInEx.CreateDocumentsHandler to create a generic events handler.

It is recommended to use the HandleCreated notification which will notify that new document is loaded to subscribe to the document events.

Unsubscribe from the events from Destroyed notification.

using CodeStack.SwEx.AddIn;
using CodeStack.SwEx.AddIn.Attributes;
using CodeStack.SwEx.AddIn.Base;
using CodeStack.SwEx.AddIn.Core;
using CodeStack.SwEx.AddIn.Delegates;
using CodeStack.SwEx.AddIn.Enums;
using SolidWorks.Interop.sldworks;
using SolidWorks.Interop.swconst;
using System;
using System.Runtime.InteropServices;

namespace CodeStack.SwEx
{
    [AutoRegister]
    [ComVisible(true), Guid("76045173-E27C-4DF9-AE5F-147B893CE3DC")]
    public class EventsAddIn : SwAddInEx    
    {   
        private IDocumentsHandler<DocumentHandler> m_DocHandlerGeneric;

        public override bool OnConnect()
        {
            m_DocHandlerGeneric = CreateDocumentsHandler();
            m_DocHandlerGeneric.HandlerCreated += OnHandlerCreated;
            return true;
        }

        private void OnHandlerCreated(DocumentHandler doc)
        {
            doc.Initialized += OnInitialized;
            doc.Activated += OnActivated;
            doc.ConfigurationChange += OnConfigurationOrSheetChanged;
            doc.CustomPropertyModify += OnCustomPropertyModified;
            doc.Access3rdPartyData += OnAccess3rdPartyData;
            doc.ItemModify += OnItemModified;
            doc.Save += OnSave;
            doc.Selection += OnSelection;
            doc.Rebuild += OnRebuild;
            doc.DimensionChange += OnDimensionChange;
            doc.Destroyed += OnDestroyed;
        }

        private void OnDestroyed(DocumentHandler handler)
        {
            handler.Initialized -= OnInitialized;
            handler.Activated -= OnActivated;
            handler.ConfigurationChange -= OnConfigurationOrSheetChanged;
            handler.CustomPropertyModify -= OnCustomPropertyModified;
            handler.ItemModify -= OnItemModified;
            handler.Save -= OnSave;
            handler.Selection -= OnSelection;
            handler.Rebuild -= OnRebuild;
            handler.DimensionChange -= OnDimensionChange;
            handler.Destroyed -= OnDestroyed;

            Logger.Log($"'{handler.Model.GetTitle()}' destroyed");
        }

        public override bool OnDisconnect()
        {
            m_DocHandlerGeneric.HandlerCreated -= OnHandlerCreated;
            return true;
        }

    }
}

Event handlers provide additional information about event, such as is it a pre or post notification and any additional parameters. Explore API reference for more information about the passed parameters.

private bool OnRebuild(DocumentHandler docHandler, RebuildState_e type)
{
    Logger.Log($"'{docHandler.Model.GetTitle()}' rebuilt ({type})");

    if(type == RebuildState_e.PreRebuild)
    {
        //return false to cancel regeneration
    }

    return true;
}

private void OnInitialized(DocumentHandler docHandler)
{
    Logger.Log($"'{docHandler.Model.GetTitle()}' initialized");
}

private bool OnSelection(DocumentHandler docHandler, swSelectType_e selType, SelectionState_e type)
{
    Logger.Log($"'{docHandler.Model.GetTitle()}' selection ({type}) of {selType}");

    if (type != SelectionState_e.UserPreSelect) //dynamic selection
    {
        //return false to cancel selection
    }

    return true;
}

private bool OnSave(DocumentHandler docHandler, string fileName, SaveState_e type)
{
    Logger.Log($"'{docHandler.Model.GetTitle()}' saving ({type})");

    if (type == SaveState_e.PreSave)
    {
        //return false to cancel saving
    }

    return true;
}

private void OnItemModified(DocumentHandler docHandler, ItemModificationAction_e type, swNotifyEntityType_e entType, string name, string oldName = "")
{
    Logger.Log($"'{docHandler.Model.GetTitle()}' item modified ({type}) of {entType}. Name: {name} (from {oldName})");
}

private void OnCustomPropertyModified(DocumentHandler docHandler, CustomPropertyModifyData[] modifications)
{
    foreach (var mod in modifications)
    {
        Logger.Log($"'{docHandler.Model.GetTitle()}' custom property '{mod.Name}' changed ({mod.Action}) in '{mod.Configuration}' to '{mod.Value}'");
    }
}

private void OnAccess3rdPartyData(DocumentHandler docHandler, Access3rdPartyDataState_e state)
{
    Logger.Log($"'{docHandler.Model.GetTitle()}' accessing 3rd party data ({state})");
}

private void OnConfigurationOrSheetChanged(DocumentHandler docHandler, ConfigurationChangeState_e type, string confName)
{
    Logger.Log($"'{docHandler.Model.GetTitle()}' configuration {confName} changed ({type})");
}

private void OnDimensionChange(DocumentHandler docHandler, IDisplayDimension dispDim)
{
    var dim = dispDim.GetDimension2(0);

    Logger.Log($"'{docHandler.Model.GetTitle()}' dimension change: {dim.FullName} = {dim.Value}");

    Marshal.ReleaseComObject(dim);
    dim = null;
}

private void OnActivated(DocumentHandler docHandler)
{
    Logger.Log($"'{docHandler.Model.GetTitle()}' activated");
}

Product of Xarial Product of Xarial