using System; using System.Collections.Generic; using System.Text; using System.Diagnostics; using System.Runtime.InteropServices; using Autodesk.AutoCAD.Interop; using Autodesk.AutoCAD.Interop.Common; using Autodesk.AutoCAD.ApplicationServices; using Autodesk.AutoCAD.Runtime; namespace CSharpCOM { publicclass Class1 { public Class1() { } [CommandMethod("AM")] publicstaticvoid AddMenuCom() { AcadApplication app = (AcadApplication)Marshal.GetActiveObject("AutoCAD.Application.17"); AcadMenuBar menuBar = app.MenuBar; AcadMenuGroup menuGroup = app.MenuGroups.Item(0); AcadPopupMenus menus = menuGroup.Menus; AcadPopupMenu mymenu = menus.Add("MyMenu"); mymenu.AddMenuItem(0, "Hello", "hello"); mymenu.AddSeparator(1); mymenu.AddMenuItem(2, "Hello2", "hello"); AcadPopupMenu ext = mymenu.AddSubMenu(3, "Ext"); ext.AddMenuItem(0, "Hello", "hello"); ext.AddSeparator(1); ext.AddMenuItem(2, "Hello2", "hello"); mymenu.InsertInMenuBar(menuBar.Count -2); } [CommandMethod("hello")] publicstaticvoid Hello() { AcadApplication app = (AcadApplication)Marshal.GetActiveObject("AutoCAD.Application.17"); app.ActiveDocument.Utility.Prompt("Hello/n"); } } }
四 CSharp基于ACUI.DLL接口的
// Copyright 2005-2007 by Autodesk, Inc. // //Permission to use, copy, modify, and distribute this software in //object code form for any purpose and without fee is hereby granted, //provided that the above copyright notice appears in all copies and //that both that copyright notice and the limited warranty and //restricted rights notice below appear in all supporting //documentation. // //AUTODESK PROVIDES THIS PROGRAM "AS IS" AND WITH ALL FAULTS. //AUTODESK SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTY OF //MERCHANTABILITY OR FITNESS FOR A PARTICULAR USE. AUTODESK, INC. using System; using System.IO; using System.Runtime.InteropServices; using System.Text; using Autodesk.AutoCAD.Runtime; using Autodesk.AutoCAD.ApplicationServices; using Autodesk.AutoCAD.EditorInput; using Autodesk.AutoCAD.DatabaseServices; using Autodesk.AutoCAD.Customization; namespace CuiSamp { /**////<summary> /// Summary description for Class1. ///</summary> publicclass CuiSamp { members#region members // All Cui files (main/partial/enterprise) have to be loaded into an object of class // CustomizationSection // cs - main AutoCAD CUI file CustomizationSection cs; CustomizationSection entCs; CustomizationSection[] partials; int numPartialFiles; YesNoIgnoreToggle yes = YesNoIgnoreToggle.yes; YesNoIgnoreToggle no = YesNoIgnoreToggle.no; // True when enterprise CUI file is loaded successfully bool entCsLoaded; // ed - access to the AutoCAD Command Line // Allows us to write messages or Issue Commands in the interface Editor ed = Application.DocumentManager.MdiActiveDocument.Editor; #endregion //Default Constructor public CuiSamp() { // retrieve the location of, and open the ACAD Main CUI File string mainCuiFile = (string)Application.GetSystemVariable("MENUNAME"); mainCuiFile +=".cui"; cs =new CustomizationSection(mainCuiFile); string entCuiFile = (string)Application.GetSystemVariable("ENTERPRISEMENU"); if (entCuiFile.Equals(".")) entCsLoaded =false; else { entCs =new CustomizationSection(entCuiFile); entCsLoaded =true; } // Code for loading all partial CUI's listed in the main CUI file partials =new CustomizationSection[cs.PartialCuiFiles.Count]; int i =0; foreach (string fileName in cs.PartialCuiFiles) { if (File.Exists(fileName)) { partials[i] =new CustomizationSection(fileName); i++; } } numPartialFiles = i; } // Command: savecui // This Command saves all open CUI Files that have been modified [CommandMethod("savecui")] publicvoid saveCui() { // Save all Changes made to the CUI file in this session. // If changes were made to the Main CUI file - save it // If changes were made to teh Partial CUI files need to save them too if (cs.IsModified) cs.Save(); for (int i =0; i < numPartialFiles; i++) { if (partials[i].IsModified) partials[i].Save(); } if (entCsLoaded && entCs.IsModified) entCs.Save(); // Here we unload and reload the main CUI file so the changes to the CUI file could take effect immediately. //string flName = cs.CUIFileBaseName; //Application.SetSystemVariable("FILEDIA", 0); //Application.DocumentManager.MdiActiveDocument.SendStringToExecute("cuiunload " + flName + " ", false, false, false); //Application.DocumentManager.MdiActiveDocument.SendStringToExecute("cuiload " + flName + " filedia 1 ", false, false, false); //Application.SetSystemVariable("FILEDIA", 1); } // Command: addmenu // This Command adds a new menu to all workspaces called Custom Menu, which contains 2 sub items // The Menu is first added to the Main CUI File and then added to all it's workspaces. [CommandMethod("addmenu")] publicvoid addMenu() { if (cs.MenuGroup.PopMenus.IsNameFree("CustomMenu")) { System.Collections.Specialized.StringCollection pmAliases =new System.Collections.Specialized.StringCollection(); pmAliases.Add("POP12"); PopMenu pm =new PopMenu("CustomMenu", pmAliases, "CustomMenu", cs.MenuGroup); addItemsToPM(pm); addMenu2Workspaces(pm); } else ed.WriteMessage("CustomMenu already Exists/n"); } // Add new Items to a PopMenu privatevoid addItemsToPM(PopMenu pm) { PopMenuItem pmi =new PopMenuItem(pm, -1); pmi.MacroID ="ID_AUGI"; pmi.Name ="Autodesk User Group International"; pmi =new PopMenuItem(pm, -1); pmi =new PopMenuItem(pm, -1); pmi.MacroID ="ID_CustomSafe"; pmi.Name ="Online Developer Center"; } // Add the menu to all the workspaces privatevoid addMenu2Workspaces(PopMenu pm) { foreach (Workspace wk in cs.Workspaces) { WorkspacePopMenu wkpm =new WorkspacePopMenu(wk, pm); wkpm.Display =1; } } // Command: remmenu // This Command deletes the menu added above from the Main CUI File and any // workspaces that it was added to. [CommandMethod("remmenu")] publicvoid remMenu() { // Find Index of the desired MenuItem // Remove it from all Workspaces that it exists in // Omitting this step leaves nasty left-overs in the Workspace files // Remove it from the Cui files' Menu List PopMenu pm = cs.MenuGroup.PopMenus.FindPopWithAlias("POP12"); if (pm !=null) { foreach (Workspace wk in cs.Workspaces) { WorkspacePopMenu wkPm = wk.WorkspacePopMenus.FindWorkspacePopMenu(pm.ElementID, pm.Parent.Name); if (wkPm !=null) wk.WorkspacePopMenus.Remove(wkPm); } cs.MenuGroup.PopMenus.Remove(pm); // Deletes the Menu from ACAD Menu Group } } // Command: addtoolbar // Creates a new toolbar called "New Toolbar", and adds it to all workspaces. // This toolbar contains a Toolbar control for named view, button for drawing // a pline, and a flyout that uses the "Draw" tool bar. [CommandMethod("addtoolbar")] publicvoid addToolbar() { Toolbar newTb =new Toolbar("New Toolbar", cs.MenuGroup); newTb.ToolbarOrient = ToolbarOrient.floating; newTb.ToolbarVisible = ToolbarVisible.show; ToolbarControl tbCtrl =new ToolbarControl(ControlType.NamedViewControl, newTb, -1); ToolbarButton tbBtn =new ToolbarButton(newTb, -1); tbBtn.Name ="PolyLine"; tbBtn.MacroID ="ID_Pline"; ToolbarFlyout tbFlyout =new ToolbarFlyout(newTb, -1); tbFlyout.ToolbarReference ="DRAW"; foreach (Workspace wk in cs.Workspaces) { WorkspaceToolbar wkTb =new WorkspaceToolbar(wk, newTb); wk.WorkspaceToolbars.Add(wkTb); wkTb.Display =1; } } // Command: remtoolbar // This Command removes the toolbar added above from the Main CUI File and any // workspaces that it was added to. [CommandMethod("remtoolbar")] publicvoid remToolbar() { Toolbar tbr = cs.MenuGroup.Toolbars.FindToolbarWithName("New Toolbar"); if (tbr !=null) { foreach (Workspace wk in cs.Workspaces) { WorkspaceToolbar wkTb = wk.WorkspaceToolbars.FindWorkspaceToolbar(tbr.ElementID, tbr.Parent.Name); if (wkTb !=null) wk.WorkspaceToolbars.Remove(wkTb); } cs.MenuGroup.Toolbars.Remove(tbr); // Deletes the toolbar from ACAD Menu Group } } // Command: cuiall // Issuing this command will run the methods to make all changes to the UI // This will add the custom menu, toolbar, and shortcut, as well as // dock the Properties palette on the right side. [CommandMethod("addcui")] publicvoid AddMenuAndToobar() { addMenu(); addToolbar(); saveCui(); } [CommandMethod("remcui")] publicvoid RemMenuAndToolbar() { remMenu(); remToolbar(); saveCui(); } } }