Application.CommandManager.ControlDefinitions.AddButtonDefinition throws COM Exception when it stands in unit tests

Application.CommandManager.ControlDefinitions.AddButtonDefinition throws COM Exception when it stands in unit tests

hongVFGWC
Explorer Explorer
830 Views
9 Replies
Message 1 of 10

Application.CommandManager.ControlDefinitions.AddButtonDefinition throws COM Exception when it stands in unit tests

hongVFGWC
Explorer
Explorer

Hello people,

 

in the development process of an Add-in to Autodesk Inventor we decide to write some integration tests using MS Test unit test framework. Here is a simplified excerpt of test code:

 

using Autodesk.ADN.InvUtility.WinUtils;
using Inventor;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Windows.Forms;
using System.Drawing;

namespace AddIn.Tests
{
    [TestClass]
    public class UnitTest1
    {
        private AssemblyDocument _AsmDoc;
        private const string _IAM_PATH = @"pathToAssemblyFileForTest";
        [TestMethod]
        public void TestMethod1()
        {
            Inventor.Application _Application = InvTestFuncLib.StartInventor(); // This method opens Inventor using System.Activator.CreateInstance
            _AsmDoc= application.Documents.Open(System.IO.Path.GetFullPath(_IAM_PATH ), OpenVisible: true) as AssemblyDocument;
            _AsmDoc.Activate();

            Icon icon = new Icon(@"sample.ico");
            stdole.IPictureDisp plusIcon = ImageConverter.IconToPicture(icon);
            Inventor.ControlDefinitions controlDefs = _Application.CommandManager.ControlDefinitions;
            ButtonDefinition _ButtonApply = controlDefs.AddButtonDefinition("Apply", "ButtonRadialMenuApply", CommandTypesEnum.kNonShapeEditCmdType, null, "", "" , plusIcon);
        }
    }
}

 

 

The last line of code throws an COM Exception with message: 

" System.Runtime.InteropServices.COMException: Catastrophic error (Exception of HRESULT: 0x8000FFFF (E_UNEXPECTED))".

The strange things are:

1. The same codes of using AddButtonDefinition are present in the source codes (not in test codes) and it works normally there.

2. If plusIcon is not passed over to Application.CommandManager.ControlDefinitions.AddButtonDefinition, i.e. the last line will be like the following, then no exception is thrown, but obviously the icon will be missing in the Inventor UI.

 

ButtonDefinition _ButtonApply = controlDefs.AddButtonDefinition(Text.Apply, "ButtonRadialMenuApply", CommandTypesEnum.kNonShapeEditCmdType, null, "", "");

 

 

Could anyone please give me any suggestion: Why does the icon parameter cause problem, when it is run under the unit test framework, and how can I solve it? 

0 Likes
831 Views
9 Replies
Replies (9)
Message 2 of 10

JelteDeJong
Mentor
Mentor

if you expect a problem with the icon then you could check if it's the exact correct size. Inventor is very picky about the size.  Try something like this:

if (icon.Width != 16) // or icon.Width != 32 for the large icon
{
     MessageBox.Show("Icon size does not match!");
}

 

Jelte de Jong
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

EESignature


Blog: hjalte.nl - github.com

0 Likes
Message 3 of 10

hongVFGWC
Explorer
Explorer

Thank you for your hint on the size issue. Please have a look at this piece of original codes in the addIn project:

 

            System.Drawing.Icon icon = Resources.Ok_Light;
            stdole.IPictureDisp okIconStandard = PictureDispConverter.ToIPictureDisp(new System.Drawing.Icon(icon, new System.Drawing.Size(16, 16)));
            stdole.IPictureDisp okIconLarge = PictureDispConverter.ToIPictureDisp(new System.Drawing.Icon(icon, new System.Drawing.Size(32, 32)));
            ButtonDefinition _ButtonOk = _App.CommandManager.ControlDefinitions.AddButtonDefinition(Text.Ok, "ButtonRadialMenuOk", CommandTypesEnum.kNonShapeEditCmdType, null, "", "", okIconStandard, okIconLarge);

 

I used Debug.WriteLine() to check the size: The width of okIconStandard turns out to be 423, and for okIconLarge it is 847. There is no problem at all when we run our AddIn (Including a eventHandler which contains the code above) with Inventor. However, when the test method comes across the same code section, it failed with the "catastrophic error". That is quite confusing ... 

0 Likes
Message 5 of 10

hongVFGWC
Explorer
Explorer

That was posted by me, too.

0 Likes
Message 6 of 10

Michael.Navara
Advisor
Advisor
0 Likes
Message 7 of 10

JelteDeJong
Mentor
Mentor

Did you check if the "Autodesk.Inventor.Interop" reference property "Embed Interop Types" is set to "False".

EmbedInteropTypes.png

If this is set to True strange things start to happen. But im not sure if this can cause your problems.

Jelte de Jong
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

EESignature


Blog: hjalte.nl - github.com

0 Likes
Message 8 of 10

hongVFGWC
Explorer
Explorer

I just checked, the Embed interop Types is set to False, in the test project as well as in the project to be tested.

0 Likes
Message 9 of 10

etaCAD
Advocate
Advocate

Icons can be very tricky. Perhaps you have to set your icon as embedded ressource.

grafik.png

Andreas
etaCAD

0 Likes
Message 10 of 10

hongVFGWC
Explorer
Explorer

Thank you for your input. I just tried it but it doesn't work.

0 Likes