CAD二次开发实验报告(2)

2020-04-14 18:26

using Autodesk.AutoCAD.EditorInput;

[assembly: CommandClass(typeof(HelloWorld.Hello))] namespace HelloWorld {

///

/// Summary description for rdsCommands. ///

public class Hello {

public Hello() { //

// TODO: Add constructor logic here // }

// Define Command \ [CommandMethod(\)]

static public void HW() // This method can have any name {

// Put your command code here

Editor pEd = Application.DocumentManager.MdiActiveDocument.Editor; pEd.WriteMessage(\); } } }

→编译,并在AutoCAD加载DLL

5

→输入命令“HW”,查看运行结果

4.4 创建上下文菜单 (1)加载命名空间

? using System;

? using Autodesk.AutoCAD.Runtime; ? using Autodesk.AutoCAD.Windows; ? using Autodesk.AutoCAD.EditorInput; (2)输入完成程序如下

using System;

using Autodesk.AutoCAD.Windows; using Autodesk.AutoCAD.Runtime; using Autodesk.AutoCAD.EditorInput;

[assembly: CommandClass(typeof(MyMenu.MyMenu))] namespace MyMenu {

public class MyMenu : IExtensionApplication {

ContextMenuExtension MyMenu; //初始化菜单

public void Initialize() {

AddMenu(); }

//释放资源,移除菜单 public void Terminate() {

Remove(); }

//添加菜单

public void AddMenu() { try {

MyMenu = new ContextMenuExtension();

6

MyMenu.Title = \;

Autodesk.AutoCAD.Windows.MenuItem menuItem;

menuItem = new Autodesk.AutoCAD.Windows.MenuItem(\); menuItem.Click += new EventHandler(CallbackOnClick); MyMenu.MenuItems.Add(menuItem);

Autodesk.AutoCAD.ApplicationServices.Application. AddDefaultContextMenuExtension(MyMenu); }

catch (System.Exception ex){} }

//移除菜单

public void Remove() { try {

if (MyMenu != null) {

Autodesk.AutoCAD.ApplicationServices.Application. RemoveDefaultContextMenuExtension(MyMenu); } }

catch (System.Exception ex){} }

public void CallbackOnClick(object sender, EventArgs e){} } }

(3)编译,并在AutoCAD利用NETLOAD加载dll。

4.5 创建弹出式下拉菜单 (1)输入完整程序如下:

using System;

using System.Collections.Generic; using System.Linq; using System.Text;

using System.Threading.Tasks; using System.IO;

using Autodesk.AutoCAD.Customization; using Autodesk.AutoCAD.ApplicationServices;

7

using Autodesk.AutoCAD.Runtime; namespace TurndownMenu {

public class TurndownMenu {

CustomizationSection[] partials; CustomizationSection cs; CustomizationSection entCs; int numPartialFiles; bool entCsLoaded; [CommandMethod(\)] public void Ad_Cui_Load() {

this.init_Cui(); this.add_Menu(); this.save_Cui(); }

private void init_Cui() {

string mainCuiFile = (string)Application.GetSystemVariable(\); mainCuiFile += \;

cs = new CustomizationSection(mainCuiFile); string entCuiFile = (string)Application. GetSystemVariable(\); if (entCuiFile.Equals(\)) entCsLoaded = false; else

{ entCs = new CustomizationSection(entCuiFile); entCsLoaded = true; }

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; }

private void add_Menu() {

8

if (cs.MenuGroup.PopMenus.IsNameFree(\测试菜单\)) {

MacroGroup mg = new MacroGroup(cs.MenuGroupName, cs.MenuGroup); MenuMacro m1 = new MenuMacro(mg, \, \,

\);

MenuMacro m2 = new MenuMacro(mg, \, \,

\);

MenuMacro m3 = new MenuMacro(mg, \, \,

\);

System.Collections.Specialized.StringCollection pmAliases = new System.Collections.Specialized.StringCollection(); pmAliases.Add(\);

PopMenu pm = new PopMenu(\测试菜单\, pmAliases, \测试菜单\,

cs.MenuGroup);

PopMenuItem pmi = new PopMenuItem(m1, \测试菜单\, pm, -1); pmi = new PopMenuItem(m2, \测试菜单\, pm, -1); pmi = new PopMenuItem(pm, -1);

pmi = new PopMenuItem(m3, \菜单项\, pm, -1); addMenu2Workspaces(pm);

foreach (Workspace wk in cs.Workspaces) {

WorkspacePopMenu wkpm = new WorkspacePopMenu(wk, pm); wkpm.Display = 1; } } else { } }

private void addMenu2Workspaces(PopMenu pm) {}

private void save_Cui() {

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();

string flName = cs.CUIFileBaseName;

Application.SetSystemVariable(\, 0);

Application.DocumentManager.MdiActiveDocument.SendStringToExecute( \ + flName + \, false, false, false);

9


CAD二次开发实验报告(2).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:(全国通用版)2019版高考数学一轮复习 第二单元 函数的概念及其

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: