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

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(SwDocument doc) 
{
    doc.StreamReadAvailable += OnStreamReadAvailable;
    doc.StreamWriteAvailable += OnStreamWriteAvailable;
}

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

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

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(SwDocument 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(SwDocument 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