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 store via xCAD framework


Call IXDocument::OpenStorage method to access the 3rd storage store. Pass the access parameter to read or write storage.

Use this approach when it is required to store multiple data structures which need to be accessed and managed independently. Prefer this instead of creating multiple streams

Storage Access Handler

To simplify the handling of the storage lifecycle, use the Documents Manager API from the xCAD framework:

private void HandleStorage(ISwDocument doc) 
{
    doc.StorageReadAvailable += OnStorageReadAvailable;
    doc.StorageWriteAvailable += OnStorageWriteAvailable;
}

private void OnStorageWriteAvailable(IXDocument doc)
{
    SaveToStorageStore(doc as ISwDocument);
}

private void OnStorageReadAvailable(IXDocument doc)
{
    LoadFromStorageStore(doc as ISwDocument);
}

Reading data

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

private void LoadFromStorageStore(ISwDocument model)
{
    using (var storage = model.TryOpenStorage(STORAGE_NAME, AccessType_e.Read))
    {
        if (storage != null)
        {
            using (var str = storage.TryOpenStream(STREAM1_NAME, false))
            {
                if (str != null)
                {
                    var xmlSer = new XmlSerializer(typeof(StorageStreamData));
                    m_StorageData = xmlSer.Deserialize(str) as StorageStreamData;
                }
            }

            using (var subStorage = storage.TryOpenStorage(SUB_STORAGE_NAME, false))
            {
                if (subStorage != null)
                {
                    using (var str = subStorage.TryOpenStream(STREAM2_NAME, false))
                    {
                        if (str != null)
                        {
                            var buffer = new byte[str.Length];
                            str.Read(buffer, 0, buffer.Length);
                            var dateStr = Encoding.UTF8.GetString(buffer);
                            var date = DateTime.Parse(dateStr);
                        }
                    }
                }
            }
        }
    }
}

Writing data

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

private void SaveToStorageStore(ISwDocument model)
{
    using (var storage = model.OpenStorage(STORAGE_NAME, AccessType_e.Write))
    {
        using (var str = storage.TryOpenStream(STREAM1_NAME, true))
        {
            var xmlSer = new XmlSerializer(typeof(StorageStreamData));

            xmlSer.Serialize(str, m_StorageData);
        }

        using (var subStorage = storage.TryOpenStorage(SUB_STORAGE_NAME, true))
        {
            using (var str = subStorage.TryOpenStream(STREAM2_NAME, true))
            {
                var buffer = Encoding.UTF8.GetBytes(DateTime.Now.ToString("yyyy-MM-dd-hh-mm-ss"));
                str.Write(buffer, 0, buffer.Length);
            }
        }
    }
}

Explore the methods of IStorage for information of how to create sub streams or sub storages and enumerate the existing elements.


Powered by Docify