/// <summary>
        /// Creates the command group based on it's current children
        /// NOTE: Once created, parent command manager must remove and re-create the group
        /// This group cannot be re-used after creating, any edits will not take place
        /// </summary>
        /// <param name="manager">The command manager that is our owner</param>
        public void Create(CommandManager manager)
        {
            if (mCreated)
            {
                throw new SolidDnaException(SolidDnaErrors.CreateError(
                                                SolidDnaErrorTypeCode.SolidWorksCommandManager,
                                                SolidDnaErrorCode.SolidWorksCommandGroupReActivateError,
                                                Localization.GetString("ErrorSolidWorksCommandGroupReCreateError")));
            }

            #region Set Icons

            //
            // Set the icons
            //
            // NOTE: The order in which you specify the icons must be the same for this property and MainIconList.
            //
            //       For example, if you specify an array of paths to
            //       20 x 20 pixels, 32 x 32 pixels, and 40 x 40 pixels icons for this property
            //       then you must specify an array of paths to
            //       20 x 20 pixels, 32 x 32 pixels, and 40 x 40 pixels icons for MainIconList.</remarks>
            //

            // Set all icon lists
            var icons = GetIconListPaths();

            // 2016+ support
            mBaseObject.IconList = icons.ToArray();

            // <2016 support
            if (icons.Count > 0)
            {
                // Largest icon for this one
                mBaseObject.LargeIconList = icons.Last();

                // The list of icons
                mBaseObject.MainIconList = icons.ToArray();

                // Smallest icon for this one
                mBaseObject.SmallIconList = icons.First();
            }

            #endregion

            #region Add Items

            // Add items
            Items?.ForEach(item => AddCommandItem(item));

            #endregion

            // Activate the command group
            mCreated = mBaseObject.Activate();

            // Get command Ids
            Items?.ForEach(item => item.CommandId = mBaseObject.CommandID[item.UniqueId]);

            #region Command Tab

            // Add to parts tab
            var list = Items.Where(f => f.TabView != CommandManagerItemTabView.None && f.VisibleForParts).ToList();
            if (list?.Count > 0)
            {
                AddItemsToTab(ModelType.Part, manager, list);
            }

            // Add to assembly tab
            list = Items.Where(f => f.TabView != CommandManagerItemTabView.None && f.VisibleForAssemblies).ToList();
            if (list?.Count > 0)
            {
                AddItemsToTab(ModelType.Assembly, manager, list);
            }

            // Add to drawing tab
            list = Items.Where(f => f.TabView != CommandManagerItemTabView.None && f.VisibleForDrawings).ToList();
            if (list?.Count > 0)
            {
                AddItemsToTab(ModelType.Drawing, manager, list);
            }

            #endregion

            // If we failed to create, throw
            if (!mCreated)
            {
                throw new SolidDnaException(SolidDnaErrors.CreateError(
                                                SolidDnaErrorTypeCode.SolidWorksCommandManager,
                                                SolidDnaErrorCode.SolidWorksCommandGroupActivateError,
                                                Localization.GetString("ErrorSolidWorksCommandGroupActivateError")));
            }
        }
示例#2
0
        /// <summary>
        /// Creates a command group
        /// </summary>
        /// <param name="title">The title</param>
        /// <param name="items">The command items to add</param>
        /// <param name="flyoutItems">The flyout command items that contain a list of other commands</param>
        /// <param name="tooltip">The tool tip</param>
        /// <param name="hint">The hint</param>
        /// <param name="position">Position of the CommandGroup in the CommandManager for all document templates.
        /// NOTE: Specify 0 to add the CommandGroup to the beginning of the CommandManager, or specify -1 to add it to the end of the CommandManager.
        /// NOTE: You can also use ICommandGroup::MenuPosition to control the position of the CommandGroup in specific document templates.</param>
        /// <param name="ignorePreviousVersion">True to remove all previously saved customization and toolbar information before creating a new CommandGroup, false to not.
        ///     Call CommandManager.GetGroupDataFromRegistry before calling this method to determine how to set IgnorePreviousVersion.
        ///     Set IgnorePreviousVersion to true to prevent SOLIDWORKS from saving the current toolbar setting to the registry, even if there is no previous version.</param>
        /// <param name="hasMenu">Whether the CommandGroup should appear in the Tools dropdown menu.</param>
        /// <param name="hasToolbar">Whether the CommandGroup should appear in the Command Manager and as a separate toolbar.</param>
        /// <param name="addDropdownBoxForParts">If true, adds a command box to the toolbar for parts that has a dropdown of all commands that are part of this group. The tooltip of the command group is used as the name.</param>
        /// <param name="addDropdownBoxForAssemblies">If true, adds a command box to the toolbar for assemblies that has a dropdown of all commands that are part of this group. The tooltip of the command group is used as the name.</param>
        /// <param name="addDropdownBoxForDrawings">If true, adds a command box to the toolbar for drawings that has a dropdown of all commands that are part of this group. The tooltip of the command group is used as the name.</param>
        /// <returns></returns>
        private CommandManagerGroup CreateCommandGroup(
            string title,
            List <CommandManagerItem> items,
            List <CommandManagerFlyout> flyoutItems,
            string tooltip                   = "",
            string hint                      = "",
            int position                     = -1,
            bool ignorePreviousVersion       = true,
            bool hasMenu                     = true,
            bool hasToolbar                  = true,
            bool addDropdownBoxForParts      = false,
            bool addDropdownBoxForAssemblies = false,
            bool addDropdownBoxForDrawings   = false)
        {
            // NOTE: We may need to look carefully at this Id if things get removed and re-added based on this SolidWorks note:
            //
            //       If you change the definition of an existing CommandGroup (i.e., add or remove toolbar buttons), you must assign a
            //       new unique user-defined UserID to that CommandGroup. You must perform this action to avoid conflicts with any
            //       previously existing CommandGroups and to allow for backward and forward compatibility of the CommandGroups in your application.
            //

            // Get the next Id
            var id = mCommandGroups.Count == 0 ? 1 : mCommandGroups.Max(f => f.UserId) + 1;

            // Store error code
            var errors = -1;

            // Attempt to create the command group
            var unsafeCommandGroup = BaseObject.CreateCommandGroup2(id, title, tooltip, hint, position, ignorePreviousVersion, ref errors);

            // Check for errors
            if (errors != (int)swCreateCommandGroupErrors.swCreateCommandGroup_Success)
            {
                // Get enum name
                var errorEnumString = ((swCreateCommandGroupErrors)errors).ToString();

                // Throw error
                throw new SolidDnaException(SolidDnaErrors.CreateError(
                                                SolidDnaErrorTypeCode.SolidWorksCommandManager,
                                                SolidDnaErrorCode.SolidWorksCommandGroupCreateError,
                                                Localization.GetString("ErrorSolidWorksCommandGroupAddError") + $". {errorEnumString}"));
            }

            // Otherwise we got the command group
            var group = new CommandManagerGroup(
                unsafeCommandGroup,
                items,
                flyoutItems,
                id,
                title,
                tooltip,
                hint,
                hasMenu,
                hasToolbar,
                addDropdownBoxForParts,
                addDropdownBoxForAssemblies,
                addDropdownBoxForDrawings);

            // Return it
            return(group);
        }