XCAD.NET
XCAD.NET
Framework for .NET (C# and VB.NET) to create modern application for CAD systems (SOLIDWORKS, SOLIDWORKS Document Manager, Autodesk Inventor, etc.)
Get technical support Connect to xCAD community on Discord server Explore code and application examples xCAD.NET Templates for Visual Studio and VS Code Access source code

Handling the common events of SOLIDWORKS file using xCAD framework


xCAD framework exposes the common events via corresponding interfaces, e.g. IXProperty exposes ValueChanged event to indicate that the property has been changed, while IXSelectionRepository exposes NewSelection event to indicate that new object is selected.

Although it is possible to subscribe to events from any container, it is usually managed within the IDocumentHandler

Application.Documents.RegisterHandler<MyDocumentHandler>();

Explore API reference for more information about the passed parameters.

public class MyDocumentHandler : IDocumentHandler
{
    private IXDocument m_Model;
    private IXProperty m_DescPrp;
    private IXDimension m_D1Dim;

    public void Init(IXApplication app, IXDocument model)
    {
        m_Model = model;

        m_DescPrp = m_Model.Properties["Description"];
        m_D1Dim = m_Model.Dimensions["D1@Sketch1"];

        m_Model.Closing += OnModelClosing;
        m_Model.Selections.NewSelection += OnNewSelection;
        
        m_DescPrp.ValueChanged += OnPropertyValueChanged;
        m_D1Dim.ValueChanged += OnDimensionValueChanged;
    }

    private void OnModelClosing(IXDocument doc, DocumentCloseType_e type)
    {
        //handle closing
    }

    private void OnNewSelection(IXDocument doc, IXSelObject selObject)
    {
        //handle new selection
    }

    private void OnPropertyValueChanged(IXProperty prp, object newValue)
    {
        //handle property change
    }

    private void OnDimensionValueChanged(IXDimension dim, double newVal)
    {
        //handle dimension change
    }

    public void Dispose()
    {
        m_Model.Closing -= OnModelClosing;
        m_Model.Selections.NewSelection -= OnNewSelection;

        m_DescPrp.ValueChanged -= OnPropertyValueChanged;
        m_D1Dim.ValueChanged -= OnDimensionValueChanged;
    }
}

Refer Property As File Name example which demonstrates how to interrupt saving event and supply custom name for the file.


Powered by Docify