ds-blue-logo
Preview  |  SOLIDWORKS USER FORUM
Use your SOLIDWORKS ID or 3DEXPERIENCE ID to log in.
BWBrant Williams10/02/2017

I am having problems getting the proper functionality out of some of the basic API Methods for an AddIn.  I have taken the Std CSharp AddIn, and made some slight modification.  Below is my AddCommandMgr() method where the issues seem to be.  I commented out all the code relating to FlyoutGroups.  I don't need them for this app.  The problems I am having are as follows:

1.  icmdGroup.ShowInDocumentType does nothing.  I set it up to only show only for PARTS and ASSEMBLIES...but when I bring up a drawing, the Command Manager is still there.

//This does nothing.  Toolbars show in all doc types.
icmdGroup.ShowInDocumentType = (int)(swDocTemplateTypes_e.swDocTemplateTypePART | swDocTemplateTypes_e.swDocTemplateTypeASSEMBLY);

Even if I put in just a single integer (swDocTemplateTypes_e.swDocTemplateTypePART = 1), it does nothing.  It shows for all doc types.

2. I can not seem to get the AddCommandItem2 and icmdGroup.HasToolbar or icmdGroup.HasMenu to work together.  Perhaps I am not understanding how the menuToolbarOption bitmask works.

If I change HasMenu to false, and leave HasToolbar at true...the Menu goes away, and Toolbar is still there....so this seems to work.

If I change HasToolbar to false, and leave HasMenu at true...Menu is still there.  The Cube and PMP Menu Items are still there, but the icons are replaced with generic symbols.  The flyout/dropdown is disabled, and the label now reads "Form New Subassembly".  Where the heck did that come from!

int menuToolbarOption = (int)(swCommandItemType_e.swMenuItem | swCommandItemType_e.swToolbarItem);
cmdIndex0 = icmdGroup.AddCommandItem2("CreateCube", -1, "Create a cube", "Create cube", 0, "CreateCube", "", mainItemID1, menuToolbarOption);
cmdIndex1 = icmdGroup.AddCommandItem2("Show PMP", -1, "Display sample property manager", "Show PMP", 2, "ShowPMP", "EnablePMP", mainItemID2, menuToolbarOption);

icmdGroup.HasToolbar = true;
icmdGroup.HasMenu = true;
icmdGroup.Activate();

If I play around with the menuToolbarOption, to make it match the HasToolbar and HasMenu settings, there seems to be no effect.  Here are 3 different ways I tried this...no effect.  Behavior is controlled by the HasMenu / HasToolbar settings, and the menuToolbarOption seems to do nothing at all.

//int menuToolbarOption = (int)(swCommandItemType_e.swMenuItem | swCommandItemType_e.swToolbarItem); //Toolbar and Menu
int menuToolbarOption = (int)(swCommandItemType_e.swMenuItem); //Menu Only
//int menuToolbarOption = (int)(swCommandItemType_e.swToolbarItem); //Toolbar Only

Here is the full AddCommandMgr() method... with some optional code I can comment in and out to change things....

Is the issue possibly the foreach loop near the end that adds commands to each command tab?  Are these pulling in the controls?

public void AddCommandMgr()

        {

            if (bmpHndlr == null)

                bmpHndlr = new BitmapHandler();

            Assembly thisAssembly;

            int cmdIndex0, cmdIndex1;

            string Title = "SwAI15CMOnly";

            string ToolTip = "Tool Dropdown";

            int[] docTypes = new int[]{(int)swDocumentTypes_e.swDocASSEMBLY,

                                       (int)swDocumentTypes_e.swDocDRAWING,

                                       (int)swDocumentTypes_e.swDocPART};

            //swDocPART =1, swDocASSEMBLY =2, swDocDRAWING =3,  docTypes =[2,3,1]

            thisAssembly = System.Reflection.Assembly.GetAssembly(this.GetType());

            int cmdGroupErr = 0;

            bool ignorePrevious = false;

            //get ID information stored in the registry, then compare to known IDs...if the IDs don't match, reset the commandGroup

            object registryIDs;

            bool getDataResult = iCmdMgr.GetGroupDataFromRegistry(mainCmdGroupID, out registryIDs);

            int[] knownIDs = new int[2] { mainItemID1, mainItemID2 };

            if (getDataResult)

            {

                if (!CompareIDs((int[])registryIDs, knownIDs))

                {

                    ignorePrevious = true;

                }

            }

            //Set up command group, menus, toolbars

            icmdGroup = iCmdMgr.CreateCommandGroup2(mainCmdGroupID, Title, ToolTip, "", -1, ignorePrevious, ref cmdGroupErr);

            icmdGroup.LargeIconList = bmpHndlr.CreateFileFromResourceBitmap("SwAI15CMOnly.ToolbarLarge.bmp", thisAssembly);

            icmdGroup.SmallIconList = bmpHndlr.CreateFileFromResourceBitmap("SwAI15CMOnly.ToolbarSmall.bmp", thisAssembly);

            icmdGroup.LargeMainIcon = bmpHndlr.CreateFileFromResourceBitmap("SwAI15CMOnly.MainIconLarge.bmp", thisAssembly);

            icmdGroup.SmallMainIcon = bmpHndlr.CreateFileFromResourceBitmap("SwAI15CMOnly.MainIconSmall.bmp", thisAssembly);

            //THIS DOES NOT WORK!  SHOWS IN ALL DOC Types....

            icmdGroup.ShowInDocumentType = (int)(swDocTemplateTypes_e.swDocTemplateTypePART| swDocTemplateTypes_e.swDocTemplateTypeASSEMBLY);

            //int menuToolbarOption = (int)(swCommandItemType_e.swMenuItem | swCommandItemType_e.swToolbarItem);

            int menuToolbarOption = (int)(swCommandItemType_e.swMenuItem);

            //int menuToolbarOption = (int)(swCommandItemType_e.swToolbarItem);

            cmdIndex0 = icmdGroup.AddCommandItem2("CreateCube", -1, "Create a cube", "Create cube", 0, "CreateCube", "", mainItemID1, menuToolbarOption);

            cmdIndex1 = icmdGroup.AddCommandItem2("Show PMP", -1, "Display sample property manager", "Show PMP", 2, "ShowPMP", "EnablePMP", mainItemID2, menuToolbarOption);

            icmdGroup.HasToolbar = false;

            icmdGroup.HasMenu = true;

            icmdGroup.Activate();

            bool bResult;

            /*DISALBE FLYOUTS

            iflyoutGroup = iCmdMgr.CreateFlyoutGroup(flyoutGroupID, "Dynamic Flyout", "Flyout Tooltip", "Flyout Hint",

              icmdGroup.SmallMainIcon, icmdGroup.LargeMainIcon, icmdGroup.SmallIconList, icmdGroup.LargeIconList, "FlyoutCallback", "FlyoutEnable");

            // Add the FlyoutGroup to the context-sensitive menus of faces in parts

            bResult = iflyoutGroup.AddContextMenuFlyout((int)swDocumentTypes_e.swDocPART, (int)swSelectType_e.swSelFACES);

            Debug.Print("Context menu flyout created for faces in parts: " + bResult.ToString());

            // Get the total number of FlyoutGroups in CommandManager

            Debug.Print("Number of FlyoutGroups is " + iCmdMgr.NumberOfFlyoutGroups);

            // Get the FlyoutGroups

            object[] objGroups;

            objGroups = (object[])iCmdMgr.GetFlyoutGroups();

            Debug.Print("Find all FlyoutGroups in CommandManager:");

            int i;

            for (i = 0; i <= objGroups.GetUpperBound(0); i++)

            {

                Debug.Print("FlyoutGroup found");

            }

            // Get a FlyoutGroup by its user-defined ID

            IFlyoutGroup fogrp;

            fogrp = iCmdMgr.GetFlyoutGroup(flyoutGroupID);

            Debug.Print("  CmdID: " + fogrp.CmdID);

            Debug.Print("  Button count: " + fogrp.ButtonCount);

            Debug.Print("  Flyout Type: " + fogrp.FlyoutType);

            Debug.Print("  SmallMainIcon: " + fogrp.SmallMainIcon);

            Debug.Print("  LargeMainIcon: " + fogrp.LargeMainIcon);

            Debug.Print("  SmallIconList: " + fogrp.SmallIconList);

            Debug.Print("  LargeIconList: " + fogrp.LargeIconList);

         

            DISABLE FLYOUTS*/

            foreach (int type in docTypes)

            {

                CommandTab cmdTab;

                cmdTab = iCmdMgr.GetCommandTab(type, Title);

                if (cmdTab != null & !getDataResult | ignorePrevious)//if tab exists, but we have ignored the registry info (or changed command group ID), re-create the tab.  Otherwise the ids won't matchup and the tab will be blank

                {

                    bool res = iCmdMgr.RemoveCommandTab(cmdTab);

                    cmdTab = null;

                }

                //if cmdTab is null, must be first load (possibly after reset), add the commands to the tabs

                if (cmdTab == null)

                {

                    cmdTab = iCmdMgr.AddCommandTab(type, Title);

                    CommandTabBox cmdBox = cmdTab.AddCommandTabBox();

                    int[] cmdIDs = new int[3];

                    int[] TextType = new int[3];

                    cmdIDs[0] = icmdGroup.get_CommandID(cmdIndex0);

                    TextType[0] = (int)swCommandTabButtonTextDisplay_e.swCommandTabButton_TextHorizontal;

                    cmdIDs[1] = icmdGroup.get_CommandID(cmdIndex1);

                    TextType[1] = (int)swCommandTabButtonTextDisplay_e.swCommandTabButton_TextHorizontal;

                    cmdIDs[2] = icmdGroup.ToolbarId;

                    TextType[2] = (int)swCommandTabButtonTextDisplay_e.swCommandTabButton_TextHorizontal; //| (int)swCommandTabButtonFlyoutStyle_e.swCommandTabButton_ActionFlyout;

                    bResult = cmdBox.AddCommands(cmdIDs, TextType);

                    /*THESE WERE FLYOUT SPECIFIC, WHICH ARE DISABLED

                    CommandTabBox cmdBox1 = cmdTab.AddCommandTabBox();

                    cmdIDs = new int[1];

                    TextType = new int[1];

                    cmdIDs[0] = iflyoutGroup.CmdID;

                    TextType[0] = (int)swCommandTabButtonTextDisplay_e.swCommandTabButton_TextBelow; | (int)swCommandTabButtonFlyoutStyle_e.swCommandTabButton_ActionFlyout;

                    bResult = cmdBox1.AddCommands(cmdIDs, TextType);

                    cmdTab.AddSeparator(cmdBox1, cmdIDs[0]);

                    THESE WERE FLYOUT SPECIFIC, WHICH ARE DISABLED*/

                }

            }

            thisAssembly = null;

        }