Catastrophic Failure when adding ClientNodeResource

Catastrophic Failure when adding ClientNodeResource

basautomationservices
Advocate Advocate
307 Views
3 Replies
Message 1 of 4

Catastrophic Failure when adding ClientNodeResource

basautomationservices
Advocate
Advocate

Hi all,

 

I'm running into a problem with an addin I am developing. I'm trying to override the icon of an assembly browser folder, for this I'm adding a ClientNodeResource. I have an image (bmp/icon both failed) that I want to use as the icon of the folder. 

 

For testing purposed I am running the code from a forms application that hooks into Inventor. 

 

This is the code I am using to add the resource.

 

 

            var panes = assembly.BrowserPanes;            
            var clientNodeResources = panes.ClientNodeResources;
            var resourceId = "TestResource";
            var bmp = Properties.Resources.duplicate_bmp;
            var pictureDisp = PictureDispConverter.ToIPictureDisp(bmp);
            ClientNodeResource clientNodeResource;
            try
            {
                clientNodeResource = clientNodeResources.ItemById(resourceId, 1);
            }
            catch (Exception ex)
            {
                clientNodeResource = clientNodeResources.Add(resourceId, 1, pictureDisp);
            }

 

 

 

I'm using the following class to convert from bmp/icon to IPictureDisp. I have a feeling theres something wrong with the conversion. I can't find the LoadPicture(path) method that I see in examples.

 

Does anybody know why the Catastrophic Failure error is being thrown?

 

 

    public sealed class PictureDispConverter
    {
        [DllImport("OleAut32.dll", EntryPoint = "OleCreatePictureIndirect", ExactSpelling = true, PreserveSig = false)]
        private extern static stdole.IPictureDisp OleCreatePictureIndirect([MarshalAs(UnmanagedType.AsAny)] object picdesc, ref Guid iid, [MarshalAs(UnmanagedType.Bool)] bool fOwn);

        private static Guid iPictureDispGuid = typeof(stdole.IPictureDisp).GUID;

        private sealed class PICTDESC
        {
            private PICTDESC()
            {
            }

            // Picture Types
            public const short PICTYPE_BITMAP = 1;
            public const short PICTYPE_ICON = 3;

            [StructLayout(LayoutKind.Sequential)]
            public class Icon
            {
                internal int cbSizeOfStruct = Marshal.SizeOf(typeof(PICTDESC.Icon));
                internal int picType = PICTDESC.PICTYPE_ICON;
                internal IntPtr hicon = IntPtr.Zero;
                internal int unused1;
                internal int unused2;

                internal Icon(System.Drawing.Icon icon)
                {
                    this.hicon = icon.ToBitmap().GetHicon();
                }
            }

            [StructLayout(LayoutKind.Sequential)]
            public class Bitmap
            {
                internal int cbSizeOfStruct = Marshal.SizeOf(typeof(PICTDESC.Bitmap));
                internal int picType = PICTDESC.PICTYPE_BITMAP;
                internal IntPtr hbitmap = IntPtr.Zero;
                internal IntPtr hpal = IntPtr.Zero;
                internal int unused;

                internal Bitmap(System.Drawing.Bitmap bitmap)
                {
                    this.hbitmap = bitmap.GetHbitmap();
                }
            }
        }

        public static stdole.IPictureDisp ToIPictureDisp(System.Drawing.Icon icon)
        {
            PICTDESC.Icon pictIcon = new PICTDESC.Icon(icon);
            return OleCreatePictureIndirect(pictIcon, ref iPictureDispGuid, true);
        }

        public static stdole.IPictureDisp ToIPictureDisp(System.Drawing.Bitmap bmp)
        {
            PICTDESC.Bitmap pictBmp = new PICTDESC.Bitmap(bmp);
            return OleCreatePictureIndirect(pictBmp, ref iPictureDispGuid, true);
        }
    }

 

 

Contact me for custom app development info@basautomationservices.com. Follow below links to view my Inventor appstore apps.

Free apps: Smart Leader | Part Visibility Utility | Mate Origins

Paid apps: Frame Stiffener Tool | Constrain Plane Toggle | Property Editor Pro


0 Likes
Accepted solutions (1)
308 Views
3 Replies
Replies (3)
Message 2 of 4

Ralf_Krieg
Advisor
Advisor

Hello

 

No solution, just some thoughts.

What's the size of the bmp? Only 16x16pixel is valid.

A bitmap should be loadable from file with the line (VB.Net) below. Just to test if there's something wrong with your resource.

Dim oBMP As New Bitmap(sFullFileName as String)

 


R. Krieg
RKW Solutions
www.rkw-solutions.com
0 Likes
Message 3 of 4

basautomationservices
Advocate
Advocate

Thanks for the reply Krieg. It loads just fine.

Bmp is not 16x16. Will try out resized bmp to 16x16 when i get back.

Contact me for custom app development info@basautomationservices.com. Follow below links to view my Inventor appstore apps.

Free apps: Smart Leader | Part Visibility Utility | Mate Origins

Paid apps: Frame Stiffener Tool | Constrain Plane Toggle | Property Editor Pro


0 Likes
Message 4 of 4

Accepted solution

Edit: The problem was the ClientID. When running the code as an addin and providing the correct AddInClientID then the image is loaded. The problem I have now is that the transparent background of the image is not working.. First it made it white, now it's loading the background as black.

 

will open a new topic for this.

 

basautomationservices_0-1650103663243.png

 

basautomationservices_1-1650104702315.png

 

 

Contact me for custom app development info@basautomationservices.com. Follow below links to view my Inventor appstore apps.

Free apps: Smart Leader | Part Visibility Utility | Mate Origins

Paid apps: Frame Stiffener Tool | Constrain Plane Toggle | Property Editor Pro


0 Likes