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

Storing data in the 3rd party storage (stream) via SwEx.AddIn framework


Call IXDocument::OpenStream method to access the 3rd party stream. Pass the access parameter to read or write stream.

Use this approach when it is required to store a single structure at the model.

Stream Access Handler

To simplify the handling of the stream lifecycle, use the Documents Manager API from the SwEx.AddIn framework:

private void HandleStream(ISwDocument doc) 
{
    doc.StreamReadAvailable += OnStreamReadAvailable;
    doc.StreamWriteAvailable += OnStreamWriteAvailable;
}

private void OnStreamWriteAvailable(IXDocument doc)
{
    SaveToStream(doc as ISwDocument);
}

private void OnStreamReadAvailable(IXDocument doc)
{
    LoadFromStream(doc as ISwDocument);
}

Reading data

IXDocument::OpenStream method throws an exception when storage does not exist. Use IXDocument::TryOpenStream extension method which returns null for the storage which not exists on reading.

private void LoadFromStream(ISwDocument model)
{
    using (var str = model.TryOpenStream(STREAM_NAME, AccessType_e.Read))
    {
        if (str != null)
        {
            var xmlSer = new XmlSerializer(typeof(StreamData));
            m_StreamData = xmlSer.Deserialize(str) as StreamData;
        }
    }
}

Writing data

IXDocument::OpenStream will always return the pointer to the stream (stream is automatically created if it doesn't exist).

private void SaveToStream(ISwDocument model)
{
    using (var str = model.OpenStream(STREAM_NAME, AccessType_e.Write))
    {
        var xmlSer = new XmlSerializer(typeof(StreamData));
        xmlSer.Serialize(str, m_StreamData);
    }
}


Powered by Docify