private CustomizationSection BuildMenu( XmlDocument kojtoCadMenuDefinition, Dictionary <CommandMethodAttribute, MethodInfo> commandsAndMethodsDictionary) { // rebuild the new cui // Create a customization section for our partial menu CustomizationSection kojtoCui = new CustomizationSection(); kojtoCui.MenuGroupName = Settings.Default.appName; // add a menu group var kojtoCadMacroGroup = new MacroGroup(kojtoCui.MenuGroupName, kojtoCui.MenuGroup); foreach (XmlNode toolBarDefinition in kojtoCadMenuDefinition.DocumentElement.ChildNodes) { var toolBar = new Toolbar(toolBarDefinition.Attributes["name"].Value, kojtoCui.MenuGroup); toolBar.ToolbarOrient = ToolbarOrient.right; toolBar.ToolbarVisible = ToolbarVisible.show; foreach (XmlNode buttonDefinition in toolBarDefinition) { foreach (KeyValuePair <CommandMethodAttribute, MethodInfo> commandAndMethod in commandsAndMethodsDictionary) { CommandMethodAttribute commandMethodAttribute = commandAndMethod.Key; MethodInfo method = commandAndMethod.Value; if (method.Name.Length <= "Start".Length) { continue; } string methodName = method.Name.Substring(0, method.Name.Length - "Start".Length); if (!methodName.Equals(buttonDefinition.Attributes["methodName"].Value, StringComparison.InvariantCultureIgnoreCase)) { continue; } MenuMacro menuMacro = new MenuMacro( kojtoCadMacroGroup, buttonDefinition.InnerText, commandMethodAttribute.GlobalName, method.Name); string smallImage = ""; string mediumImage = ""; ; try { smallImage = IconsManager.GetIconFile( commandAndMethod.Value.Name.Replace("Start", ""), IconSize.Small); mediumImage = IconsManager.GetIconFile( commandAndMethod.Value.Name.Replace("Start", ""), IconSize.Medium); menuMacro.macro.SmallImage = smallImage; menuMacro.macro.LargeImage = mediumImage; // The var toolBarButton is not assigned to anything because it needs only to be instantiated in order appear in the toolbar var toolBarButton = new ToolbarButton( menuMacro.ElementID, buttonDefinition.InnerText, toolBar, -1); } catch (System.Exception exception) { Logger.Error(Resources.ErrorCreatingToolbarButton, exception); } } } } return(kojtoCui); }
private void PopulateToolbarItems() { var cuiFile = DirectoriesAndFiles.BcadCuiFile; var cuiFileName = new FileInfo(cuiFile).Name.Replace(new FileInfo(cuiFile).Extension, string.Empty); var app = Application.AcadApplication as AcadApplication; if (app == null) { MessageBox.Show("App was null!", "E R R O R", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } var menuGroups = app.MenuGroups; var myGroup = menuGroups.GetMenuGroupByName(cuiFileName) ?? menuGroups.Load(cuiFile); var toolbars = myGroup.Toolbars; var commandMethods = GetCommandMethodsFromCurrentAssembly().Where(x => x.Value.Name.Length >= "Start".Length).ToArray(); var documentElement = MenuSchemaProvider.GetMenuSchemaFile().DocumentElement; if (documentElement != null) { var toolbarDefinitions = documentElement.ChildNodes; foreach (XmlNode toolBarDefinition in toolbarDefinitions) { if (toolBarDefinition.Attributes == null) { MessageBox.Show("XML file - toolbar name!", "E R R O R", MessageBoxButtons.OK, MessageBoxIcon.Error); continue; } var toolbarName = toolBarDefinition.Attributes["name"].Value; var toolbar = toolbars.GetToolbarByName(toolbarName); if (toolbar == null) { toolbar = toolbars.Add(toolbarName); toolbar.Dock(AcToolbarDockStatus.acToolbarDockRight); } foreach (XmlNode buttonDefinition in toolBarDefinition) { if (buttonDefinition.Attributes == null) { MessageBox.Show("XML file - method name!", "E R R O R", MessageBoxButtons.OK, MessageBoxIcon.Error); continue; } var buttonName = buttonDefinition.Attributes["methodName"].Value; var toolbarItem = toolbar.GetToolbarItemByName(buttonName); if (toolbarItem == null) { var buttonMethod = commandMethods.FirstOrDefault( x => x.Value.Name.Substring(0, x.Value.Name.Length - "Start".Length) .Equals(buttonName, StringComparison.InvariantCultureIgnoreCase)); toolbarItem = toolbar.AddToolbarButton(-1, buttonName, buttonDefinition.InnerText, buttonMethod.Key.GlobalName); var smallIcon = IconsManager.GetIconFile(buttonMethod.Value.Name.Replace("Start", ""), IconSize.Small); var largeIcon = IconsManager.GetIconFile(buttonMethod.Value.Name.Replace("Start", ""), IconSize.Large); if (!File.Exists(smallIcon) || !File.Exists(largeIcon)) { MessageBox.Show("Icon not found!", "E R R O R", MessageBoxButtons.OK, MessageBoxIcon.Error); continue; } toolbarItem.SetBitmaps(smallIcon, largeIcon); } } } } myGroup.Save(AcMenuFileType.acMenuFileSource); }