Community
Inventor Programming - iLogic, Macros, AddIns & Apprentice
Inventor iLogic, Macros, AddIns & Apprentice Forum. Share your knowledge, ask questions, and explore popular Inventor topics related to programming, creating add-ins, macros, working with the API or creating iLogic tools.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Store object inside document

11 REPLIES 11
SOLVED
Reply
Message 1 of 12
matt_jlt
1750 Views, 11 Replies

Store object inside document

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.

11 REPLIES 11
Message 2 of 12
jdkriek
in reply to: matt_jlt

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)
Jonathan D. Kriek
MFG Solutions Engineer
KETIV Technologies, Inc.


Message 3 of 12
matt_jlt
in reply to: jdkriek

Thanks mate,

 

Do you know how to accomplish this via the built in Inventor API ?

 

e.g.

 

oDoc.GetPrivateStream()
oDoc.GetPrivateStorage()

 

Thanks, Matt.

Message 4 of 12
xiaodong_liang
in reply to: matt_jlt
Message 5 of 12
matt_jlt
in reply to: xiaodong_liang

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.

Message 6 of 12
matt_jlt
in reply to: matt_jlt

Does anyone know how to use this? I can't find much documentation on it and I'm stuck Smiley Frustrated

 

Autdesk Employees?

 

Thanks again, Matt.

Message 7 of 12
xiaodong_liang
in reply to: matt_jlt

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.

Message 8 of 12
matt_jlt
in reply to: matt_jlt

Pure question of ms? Sorry I don't understand what that means?
I know how to achieve it using the Com interface (thanks to jdkreik for the links) but in the C++ example given it says you should use inventors built in API to handle this not the com interface.
All I would like is a working example of how to use inventors API functions below to achieve the same thing.
oDoc.getprivatestorage
ODoc.getprivatestream

Sorry if I am not clear on what I am trying to do.
Thanks, Matt.
Message 9 of 12
xiaodong_liang
in reply to: matt_jlt

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); 
            } 
        }
 

 

Message 10 of 12

Message 11 of 12
jdkriek
in reply to: xiaodong_liang


@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!

Jonathan D. Kriek
MFG Solutions Engineer
KETIV Technologies, Inc.


Message 12 of 12
dba78
in reply to: matt_jlt

Hi, this is an old one... 

you could save any data as byte-Array in a Document-Attribute.....

 

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report