C# SolidWorks 二次开发 API—增加第三方数据流

C# SolidWorks 二次开发 API—增加第三方数据流

今天增加了如何保存文件的第三数据流到solidworks的文件中.

例如xml文件,文本信息.

 private ModelDoc2 m_RefDoc; //增加第三方数据流 共用模型.

        private void btn_ThridData_Click(object sender, EventArgs e)
        {
            //https://www.codestack.net/solidworks-api/data-storage/third-party/embed-file/
            ISldWorks swApp = Utility.ConnectToSolidWorks();

            ModelDoc2 swModel = default(ModelDoc2);
            ModelDocExtension swModelDocExt = default(ModelDocExtension);

            swModel = swApp.ActiveDoc;
            m_RefDoc = swModel;

            switch (swModel.GetType())
            {
                case (int)swDocumentTypes_e.swDocPART:
                    (swModel as PartDoc).SaveToStorageNotify += new DPartDocEvents_SaveToStorageNotifyEventHandler(OnSaveToStorage);
                    break;

                case (int)swDocumentTypes_e.swDocASSEMBLY:
                    (swModel as AssemblyDoc).SaveToStorageNotify += new DAssemblyDocEvents_SaveToStorageNotifyEventHandler(OnSaveToStorage);
                    break;
            }

            swModel.SetSaveFlag();
 swApp.SendMsgToUser("请手动保存文件!这样会把数据流写入文件中.");
        }

        private int OnSaveToStorage()
        {
            IStream iStr = (IStream)m_RefDoc.IGet3rdPartyStorage("Tool.Name", true);

            using (ComStream comStr = new ComStream(iStr))
            {
                byte[] data = Encoding.Unicode.GetBytes("Paine's Tool");
                comStr.Write(data, 0, data.Length);
            }

            m_RefDoc.IRelease3rdPartyStorage("Tool.Name");

            return 0;
        }

        private void btn_LoadThrid_Click(object sender, EventArgs e)
        {
            ISldWorks swApp = Utility.ConnectToSolidWorks();

            // ModelDoc2 swModel = default(ModelDoc2);

            IModelDoc2 doc = swApp.IActiveDoc2;
            ISelectionMgr selMgr = doc.ISelectionManager;
            //  IComponent2 comp = selMgr.GetSelectedObjectsComponent3(1, -1);

            IStream iStr = (IStream)doc.IGet3rdPartyStorage("Tool.Name", false);

            if (iStr != null)
            {
                using (ComStream comStr = new ComStream(iStr))
                {
                    byte[] data = new byte[comStr.Length];
                    comStr.Read(data, 0, (int)comStr.Length);

                    string strData = Encoding.Unicode.GetString(data);
                    MessageBox.Show(strData);
                }

                doc.IRelease3rdPartyStorage("Tool.Name");
            }
        }
   
    public class ComStream : Stream
    {
        //The managed stream being wrapped
        private IStream originalStream_;

        public ComStream(IStream stream)
        {
            if (stream != null)
            {
                originalStream_ = stream;
            }
            else
            {
                throw new ArgumentNullException("stream");
            }
        }

        ~ComStream()
        {
            Close();
        }

        // property to get original stream object
        public IStream UnderlyingStream
        {
            get
            {
                return originalStream_;
            }
        }

        // reads a specified number of bytes from the stream object
        // into memory starting at the current seek pointer
        public unsafe override int Read(byte[] buffer, int offset, int count)
        {
            if (originalStream_ == null)
            {
                throw new ObjectDisposedException("originalStream_");
            }

            if (offset != 0)
            {
                throw new NotSupportedException("only 0 offset is supported");
            }

            int bytesRead;

            IntPtr address = new IntPtr(&bytesRead);

            originalStream_.Read(buffer, count, address);

            return bytesRead;
        }

        // writes a specified number of bytes into the stream object
        // starting at the current seek pointer
        public override void Write(byte[] buffer, int offset, int count)
        {
            if (originalStream_ == null)
            {
                throw new ObjectDisposedException("originalStream_");
            }

            if (offset != 0)
            {
                throw new NotSupportedException("only 0 offset is supported");
            }

            originalStream_.Write(buffer, count, IntPtr.Zero);
        }

        // changes the seek pointer to a new location relative to the beginning
        // of the stream, the end of the stream, or the current seek position

        public unsafe override long Seek(long offset, SeekOrigin origin)
        {
            if (originalStream_ == null)
            {
                throw new ObjectDisposedException("originalStream_");
            }

            long position = 0;

            IntPtr address = new IntPtr(&position);

            originalStream_.Seek(offset, (int)origin, address);

            return position;
        }

        public override long Length
        {
            get
            {
                if (originalStream_ == null)
                {
                    throw new ObjectDisposedException("originalStream_");
                }

                STATSTG statstg;

                originalStream_.Stat(out statstg, 1 /* STATSFLAG_NONAME*/ );

                return statstg.cbSize;
            }
        }

        public override long Position
        {
            get
            {
                return Seek(0, SeekOrigin.Current);
            }
            set
            {
                Seek(value, SeekOrigin.Begin);
            }
        }

        // changes the size of the stream object
        public override void SetLength(long value)
        {
            if (originalStream_ == null)
            {
                throw new ObjectDisposedException("originalStream_");
            }

            originalStream_.SetSize(value);
        }

        // closes (disposes) the stream
        public override void Close()
        {
            if (originalStream_ != null)
            {
                originalStream_.Commit(0);

                // Marshal.ReleaseComObject( originalStream_ );
                originalStream_ = null;

                GC.SuppressFinalize(this);
            }
        }

        public override void Flush()
        {
            originalStream_.Commit(0);
        }

        public override bool CanRead
        {
            get
            {
                return true;
            }
        }

        public override bool CanWrite
        {
            get
            {
                return true;
            }
        }

        public override bool CanSeek
        {
            get
            {
                return true;
            }
        }
    }

 

posted @
2019-10-05 22:05 
painezeng  阅读(
87)  评论(
0
编辑 
收藏 
举报