Managing SOLIDWORKS documents life cycle via SwEx.AddIn framework

Edit ArticleEdit Article

SwEx.AddIn frameworks provides utility class to manage document life cycle by creating a specified instance handler as a wrapper of a model.

Call ISwAddInEx.CreateDocumentsHandler method and pass the type of document handler as a generic argument or use a second overload to create a generic document handler which exposes common events (e.g. saving, selection, rebuilding, 3rd party storage access).

private IDocumentsHandler<MyDocHandler> m_DocHandler;
private IDocumentsHandler<DocumentHandler> m_DocHandlerGeneric;

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

private void OnHandlerCreated(DocumentHandler doc)
{
    //TODO: implement
}

Define the document handler either by implementing the IDocumentHandler interface or DocumentHandler class.

public class MyDocHandler : IDocumentHandler
{
    private IModelDoc2 m_Model;

    public void Init(ISldWorks app, IModelDoc2 model)
    {
        if (model is PartDoc)
        {
            m_Model = model;
            (m_Model as PartDoc).AddItemNotify += OnAddItemNotify;
        }
        //TODO: handle other doc types
    }

    private int OnAddItemNotify(int EntityType, string itemName)
    {
        //Implement
        return 0;
    }

    public void Dispose()
    {
        if (m_Model is PartDoc)
        {
            (m_Model as PartDoc).AddItemNotify -= OnAddItemNotify;
        }
    }
}

Override methods of document handler and implement required functionality attached for each specific SOLIDWORKS model (such as handle events, load, write data etc.)

Framework will automatically dispose the handler. Unsubscribe from the custom events within the Dispose or OnDestroy method. The pointer to the document attached to the handler is assigned to Model property.


Product of Xarial Product of Xarial