Application.DocumentManager.MdiActiveDocument.SendStringToExecute( \ + flName + \, false, false, false); } } }
(2)编译,并在AutoCAD利用NETLOAD加载dll。
4.6 创建工具条
(1)输入完整程序如下:
using System;
using System.Collections.Generic; using System.Linq; using System.Text;
using System.Threading.Tasks;
using Autodesk.AutoCAD.DatabaseServices; using Autodesk.AutoCAD.Customization; using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.ApplicationServices; using Autodesk.AutoCAD.Runtime; using System.IO; using System.Data;
[assembly: CommandClass(typeof(AddToolbar.ToolBar))] namespace AddToolbar {
public class ToolBar {
Database m_db = HostApplicationServices.WorkingDatabase; CustomizationSection pMainCS = null; CustomizationSection[] partials; public int BlockCount = 0;
10
public string fname = null;
Editor pEd = Application.DocumentManager.MdiActiveDocument.Editor; string mainCuiFile; public ToolBar() {
mainCuiFile = (string)Application.GetSystemVariable(\); mainCuiFile += \;
pMainCS = new CustomizationSection(mainCuiFile);
partials = new CustomizationSection[pMainCS.PartialCuiFiles.Count]; foreach (string fileName in pMainCS.PartialCuiFiles) {
if (File.Exists(fileName)) { } } } #region
[CommandMethod(\)] public void addToolbar() {
Toolbar newTb = new Toolbar(\公路边桩绘制工具\, pMainCS.MenuGroup); newTb.ToolbarOrient = ToolbarOrient.floating; newTb.ToolbarVisible = ToolbarVisible.show; newTb.Description = \公路边桩绘制工具\;
MacroGroup oMacroGroup = new MacroGroup(\, pMainCS.MenuGroup); MenuMacro oMenuMacro1 = new MenuMacro(oMacroGroup,\, \, \, MacroType.Any);
oMenuMacro1.macro.SmallImage = @\; oMenuMacro1.macro.LargeImage = @\; ToolbarButton tbBtn = new ToolbarButton(newTb, -1); tbBtn = new ToolbarButton(newTb, -1); tbBtn.Name = \起始里程设置\;
tbBtn.MacroID = oMenuMacro1.ElementID;
oMenuMacro1 = new MenuMacro(oMacroGroup,\, \, \, MacroType.Any); tbBtn = new ToolbarButton(newTb, -1);
oMenuMacro1.macro.SmallImage = @\; oMenuMacro1.macro.LargeImage = @\; tbBtn.Name = \任意点设置边桩\;
tbBtn.MacroID = oMenuMacro1.ElementID;
oMenuMacro1 = new MenuMacro(oMacroGroup, \, \, \, MacroType.Any); tbBtn = new ToolbarButton(newTb, -1);
oMenuMacro1.macro.SmallImage = @\; oMenuMacro1.macro.LargeImage = @\;
11
tbBtn.Name = \已知里程设置边桩\;
tbBtn.MacroID = oMenuMacro1.ElementID;//ModifyBZ
oMenuMacro1 = new MenuMacro(oMacroGroup, \, \, \, MacroType.Any); tbBtn = new ToolbarButton(newTb, -1);
oMenuMacro1.macro.SmallImage = @\; oMenuMacro1.macro.LargeImage = @\; tbBtn.Name = \修改边桩\;
tbBtn.MacroID = oMenuMacro1.ElementID;
oMenuMacro1 = new MenuMacro(oMacroGroup, \, \, \, MacroType.Any); tbBtn = new ToolbarButton(newTb, -1);
oMenuMacro1.macro.SmallImage = @\; oMenuMacro1.macro.LargeImage = @\; tbBtn.Name = \属性查看\;//OutputBZ
tbBtn.MacroID = oMenuMacro1.ElementID;//DelMidPl oMenuMacro1 = new MenuMacro(oMacroGroup,\, \, \, MacroType.Any); tbBtn = new ToolbarButton(newTb, -1);
oMenuMacro1.macro.SmallImage = @\; oMenuMacro1.macro.LargeImage = @\; tbBtn.Name = \删除所选中线\;//OutputBZ
tbBtn.MacroID = oMenuMacro1.ElementID;//DelMidPl oMenuMacro1 = new MenuMacro(oMacroGroup, \, \, \, MacroType.Any); tbBtn = new ToolbarButton(newTb, -1);
oMenuMacro1.macro.SmallImage = @\; oMenuMacro1.macro.LargeImage = @\; tbBtn.Name = \导出边桩信息\;//OutputBZ tbBtn.MacroID = oMenuMacro1.ElementID; foreach (Workspace wk in pMainCS.Workspaces) {
WorkspaceToolbar wkTb = new WorkspaceToolbar(wk, newTb); wk.WorkspaceToolbars.Add(wkTb); wkTb.Display = 1; }
saveCui(); }
#endregion
[CommandMethod(\)] public void saveCui() {
if (pMainCS.IsModified) pMainCS.Save();
12
string flName = pMainCS.CUIFileBaseName; Application.SetSystemVariable(\, 0);
Application.DocumentManager.MdiActiveDocument.SendStringToExecute( \ + flName + \, false, false, false);
Application.DocumentManager.MdiActiveDocument.SendStringToExecute( \ + flName + \, false, false, false); }
[CommandMethod(\)] public void remToolbar() {
Toolbar tbr = pMainCS.MenuGroup.Toolbars.FindToolbarWithName(\公路桩号设置\); if (tbr != null) {
foreach (Workspace wk in pMainCS.Workspaces) {
WorkspaceToolbar wkTb = wk.WorkspaceToolbars.FindWorkspaceToolbar( tbr.ElementID, tbr.Parent.Name); if (wkTb != null)
wk.WorkspaceToolbars.Remove(wkTb); }
pMainCS.MenuGroup.Toolbars.Remove(tbr); } }
[CommandMethod(\)] public void callForAllChanges() {
addToolbar(); saveCui(); } } }
(2)编译,并在AutoCAD利用NETLOAD加载dll。
4.7 创建有模态和无模态对话框
(1)定义窗体 MyForm,选择 1 个 TextBox 和 3 个 Button 控件。 窗体主要代码如下:
using System;
using System.Collections.Generic; using System.ComponentModel;
13
using System.Data; using System.Drawing; using System.Linq; using System.Text;
using System.Threading.Tasks; using System.Windows.Forms; using Autodesk.AutoCAD.Geometry; using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.ApplicationServices; namespace 对话框 {
public partial class MyForm : Form {
public MyForm() {
InitializeComponent(); }
private void button1_Click(object sender, EventArgs e) {
Editor ed = Autodesk.AutoCAD.ApplicationServices.Application. DocumentManager.MdiActiveDocument.Editor;
using (EditorUserInteraction edUsrInt = ed.StartUserInteraction(this)) {
Point3d pt = GetPoint(\选择点:\);
this.InputText.Text = \ + pt.X.ToString() + \ \ + pt.Y.ToString() + \ + pt.Z.ToString() + \; edUsrInt.End(); this.Focus(); } }
public Point3d GetPoint(string word) {
Document doc = Autodesk.AutoCAD.ApplicationServices.Application. DocumentManager.MdiActiveDocument; Editor ed = doc.Editor;
PromptPointResult pt = ed.GetPoint(word); if (pt.Status == PromptStatus.OK) {
return (Point3d)pt.Value; } else {
return new Point3d(); }
14