My goal is to be able to store any object type (mainly an image jpg/bmp) inside a part that is not accessable through the normal user interface to be used with my addin.
I have been searching around and it seems like GetPrivateStream is the way to go.
Can anyone please give a working example of GetPrivateStream in vb.net and confirm if it actually stores the data inside the part.
I can't find a solution anywhere or any decent referencing material for this. If there is a better method of achieving this it would be much appreciated if you could help / point me in the right direction.
The only info i could find is a post from Brian Ekins which can be found here but it's all C# and i can't understand it.
Regards, Matt.
Solved! Go to Solution.
Solved by xiaodong_liang. Go to Solution.
Seems pretty simple in VB. Have you read up on COM interfaces?
IStorage: http://msdn2.microsoft.com/en-us/library/aa380015.aspx
IStream: http://msdn2.microsoft.com/en-us/library/aa380034.aspx
Dim instance As IStorage Dim pwcsName As String Dim grfMode As UInteger Dim reserved1 As UInteger Dim reserved2 As UInteger Dim ppstg As IStorage Dim ppstm As IStream instance.CreateStorage(pwcsName, grfMode, reserved1, reserved2, ppstg) instance.CreateStream(pwcsName, grfMode, reserved1, reserved2, ppstm) instance.OpenStorage(pwcsName, pstgPriority, grfMode, snbExclude, reserved, ppstg) instance.OpenStream(pwcsName, reserved1, grfMode, reserved2, ppstm)
Thanks mate,
Do you know how to accomplish this via the built in Inventor API ?
e.g.
oDoc.GetPrivateStream()
oDoc.GetPrivateStorage()
Thanks, Matt.
Hi,
is this blog helpful to you?
http://adndevblog.typepad.com/manufacturing/2012/08/how-to-use-private-storage-and-stream-in-c.html
Hi, I think that is the page I linked to in my first post. I am not very good at C++, I can't really work out what is happening. If you had a .net version of that it would be great.
Thanks, Matt.
Does anyone know how to use this? I can't find much documentation on it and I'm stuck
Autdesk Employees?
Thanks again, Matt.
It is a pure question of MS. It would not be much difficult to convert the C++ code to .NET. If you googled, you may find some clues.
I will try to write a code for you.
In that blog I wrote, the comment just means you should not use MS APIs to get/edit the storage directly. i.e. you firstly need to use Inventor API to access the storage we provide for you: Document. GetPrivateStorage. But after you get it, the remaining jobs are all MS APIs. So what we need to do is to just convert them to .NET. Yes, there are some tricks to use the relevant APIs in .NET, though.
I have worked out a sample as below. I also googled something on internet. It works well with Creating. However it only works with Reading in one scenario . I have not figured it out why. If I get anything, I will update the post.
Anyway, hope the code is a bit helpful for you to get started with the relevant APIs in .NET.
Thanks to the links:
http://www.pinvoke.net/default.aspx/Enums/STGM.html
http://bytes.com/topic/c-sharp/answers/760093-accessing-com-c-istream-parameter
using MVOI = Microsoft.VisualStudio.OLE.Interop; [Flags] public enum STGM : int { DIRECT = 0x00000000, TRANSACTED = 0x00010000, SIMPLE = 0x08000000, READ = 0x00000000, WRITE = 0x00000001, READWRITE = 0x00000002, SHARE_DENY_NONE = 0x00000040, SHARE_DENY_READ = 0x00000030, SHARE_DENY_WRITE = 0x00000020, SHARE_EXCLUSIVE = 0x00000010, PRIORITY = 0x00040000, DELETEONRELEASE = 0x04000000, NOSCRATCH = 0x00100000, CREATE = 0x00001000, CONVERT = 0x00020000, FAILIFTHERE = 0x00000000, NOSNAPSHOT = 0x00200000, DIRECT_SWMR = 0x00400000, } bool CreatePrivateStorageAndStream(Document pDoc, string StorageName, string StreamName, string data) { try { MVOI.IStorage pStg = (MVOI.IStorage)pDoc.GetPrivateStorage(StorageName, true); if (pStg == null) returnfalse; MVOI.IStream pStream = null; pStg.CreateStream(StreamName, (uint)(STGM.DIRECT | STGM.CREATE | STGM.READWRITE | STGM.SHARE_EXCLUSIVE), 0, 0, out pStream); if (pStream == null) returnfalse ; byte[] byteVsize = System.BitConverter.GetBytes(data.Length); byte[] byteVData = Encoding.Default.GetBytes(data); uint dummy; pStream.Write(byteVsize, (uint)(sizeof(int)), out dummy); pStream.Write(byteVData, (uint)(byteVData.Length), out dummy); // Save the data pStream.Commit((uint)(MVOI.STGC.STGC_OVERWRITE | MVOI.STGC.STGC_DEFAULT)); //Don't forget to commit changes also in storage pStg.Commit((uint)(MVOI.STGC.STGC_DEFAULT | MVOI.STGC.STGC_OVERWRITE)); //pDoc.Dirty = true; //pDoc.Save(); returntrue; } catch (Exception ex) { MessageBox.Show(ex.ToString()); returnfalse; } } bool ReadPrivateStorageAndStream(Document pDoc, string StorageName, string StreamName, outstring outDataStr) { outDataStr = ""; try { MVOI.IStorage pStg = (MVOI.IStorage)pDoc.GetPrivateStorage(StorageName, false); if (pStg == null) returnfalse; MVOI.IStream pStream = null; pStg.OpenStream(StreamName,IntPtr.Zero, (uint)(STGM.DIRECT| STGM.READWRITE | STGM.SHARE_EXCLUSIVE), 0, out pStream); if (pStream == null) returnfalse; byte[] byteVsize = newbyte[16]; uint intSize = sizeof(int); uint dummy; pStream.Read(byteVsize, (uint)intSize, out dummy); int lSize = System.BitConverter.ToInt16(byteVsize,0); byte[] outDataByte = newbyte[8192]; pStream.Read(outDataByte, (uint)lSize, out dummy); outDataStr = Encoding.Default.GetString(outDataByte, 0, lSize); returntrue; } catch (Exception ex) { MessageBox.Show(ex.ToString()); returnfalse; } } privatevoid test() { string progId = "Inventor.Application"; Inventor.Application m_inventorApp = (Inventor.Application)Marshal.GetActiveObject(progId); Document pDoc = m_inventorApp.ActiveDocument; bool Result = CreatePrivateStorageAndStream(pDoc, "MyPrvStorage1", "MyStream1", "Some private stored data"); if (Result) { pDoc = m_inventorApp.ActiveDocument; string outPutStr = null; bool readResult = ReadPrivateStorageAndStream(pDoc, "MyPrvStorage1", "MyStream1", out outPutStr); MessageBox.Show(outPutStr); } }
Hi,
long delay... but good news 🙂
http://adndevblog.typepad.com/manufacturing/2013/03/save-extra-data-in-inventor-file-3.html
@xiaodong_liang wrote:Hi,
long delay... but good news 🙂
http://adndevblog.typepad.com/manufacturing/2013/03/save-extra-data-in-inventor-file-3.html
Ah! Marshal.ReleaseComObject()
Thanks xiaodong!