AutoCAD.NET API2018二次开发第二十章

程序事件,删除事件,添加文档集合事件,删除文档集合事件,文档事件,删除文档事件,使用图形对象事件,创建工具栏,注册表注册程序

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
//运行时
using Autodesk.AutoCAD.Runtime;
//数据库
using Autodesk.AutoCAD.DatabaseServices;
//应用程序服务
using Autodesk.AutoCAD.ApplicationServices;
//几何
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.PlottingServices;
using Autodesk.AutoCAD.Customization;
using System.Reflection;
using Microsoft.Win32;

namespace AutoDemo24
{
    public class Class1
    {
        //用用程序事件
        [CommandMethod("AddAppEvent")]

        public static void AddAppEvent()
        {
            Application.BeginDoubleClick += Application_BeginDoubleClick;
        }
        //双击
        private static void Application_BeginDoubleClick(object sender, BeginDoubleClickEventArgs e)
        {
            Application.ShowAlertDialog("鼠标双击事件");
        }

        //删除事件
        [CommandMethod("RemAppEvent")]

        public static void RemAppEvent()
        {
            Application.BeginDoubleClick -= Application_BeginDoubleClick;
        }

        //添加文档集合事件
        [CommandMethod("AddDCEvent")]

        public static void AddDCEvent()
        {
            Application.DocumentManager.DocumentCreated += DocumentManager_DocumentCreated;
        }

        private static void DocumentManager_DocumentCreated(object sender, DocumentCollectionEventArgs e)
        {
            Application.ShowAlertDialog("正在创建文档");
        }
        //删除文档事件
        [CommandMethod("RemDCEvent")]

        public static void RemDCEvent()
        {
            Application.DocumentManager.DocumentCreated -= DocumentManager_DocumentCreated;
        }

        Document doc = Application.DocumentManager.MdiActiveDocument;
        //文档事件
        [CommandMethod("AddDocEvent")]

        public void AddDocEvent()
        {
            doc.BeginDocumentClose += Doc_BeginDocumentClose;

        }

        private void Doc_BeginDocumentClose(object sender, DocumentBeginCloseEventArgs e)
        {
            Application.ShowAlertDialog("文档正在关闭");
        }

        [CommandMethod("RemDocEvent")]
        //删除
        public void RemDocEvent()
        {
            doc.BeginDocumentClose -= Doc_BeginDocumentClose;
        }



        //使用图形对象事件

        Polyline Pl = null;
        //添加
        [CommandMethod("AddPlEvent")]

        public void AddPlEvent()
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;

            Database db = doc.Database;

            using (Transaction tran = db.TransactionManager.StartTransaction())
            {
                BlockTable bt = tran.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;

                BlockTableRecord btr = tran.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;

                Pl = new Polyline();

                Pl.AddVertexAt(0, new Point2d(0, 0), 0, 0, 0);

                Pl.AddVertexAt(1, new Point2d(5, 5), 0, 0, 0);

                Pl.AddVertexAt(2, new Point2d(10, 5), 0, 0, 0);

                Pl.AddVertexAt(3, new Point2d(10, 0), 0, 0, 0);

                Pl.Closed = true;

                btr.AppendEntity(Pl);

                tran.AddNewlyCreatedDBObject(Pl, true);

                Pl.Modified += Pl_Modified;

                tran.Commit();
            }
        }

        private void Pl_Modified(object sender, EventArgs e)
        {
            Application.ShowAlertDialog(Pl.ToString() + "多边形的面积是:" + Pl.Area);
        }

        //移除
        [CommandMethod("RemPlEvent")]

        public void RemPlEvent()
        {
            if (Pl != null)
            {
                Document doc = Application.DocumentManager.MdiActiveDocument;

                Database db = doc.Database;

                using (Transaction tran = db.TransactionManager.StartTransaction())
                {
                    Pl = tran.GetObject(Pl.ObjectId, OpenMode.ForRead) as Polyline;

                    if (Pl.IsWriteEnabled == false)
                    {
                        tran.GetObject(Pl.ObjectId, OpenMode.ForWrite);
                    }

                    Pl.Modified -= Pl_Modified;

                    Pl = null;
                }

            }

        }

        //创建工具栏
        [CommandMethod("AddTb")]
        public static void AddTb()
        {
            string cuiPath = (string)Application.GetSystemVariable("MENUNAME");

            cuiPath += ".cuix";

            CustomizationSection cs = new CustomizationSection(cuiPath);

            Toolbar Tb = new Toolbar("测试", cs.MenuGroup);

            Tb.ToolbarOrient = ToolbarOrient.floating;

            Tb.ToolbarVisible = ToolbarVisible.show;

            Tb.XCoordinate = 200;
            Tb.YCoordinate = 300;

            ToolbarButton tbb = new ToolbarButton(Tb, -1);

            tbb.Name = "LINE";
            tbb.MacroID = "ID_Line";

            foreach (Workspace ws in cs.Workspaces)
            {
                WorkspaceToolbar wt = new WorkspaceToolbar(ws, Tb);

                ws.WorkspaceToolbars.Add(wt);

                wt.Display = 1;
            }

            cs.Save();

            Application.LoadMainMenu(cuiPath);

        }

        //注册表
        [CommandMethod("ZCCX")]

        public static void ZCCX()
        {
            string prodKey = HostApplicationServices.Current.UserRegistryProductRootKey;

            string AppName = "MyApp";

            Microsoft.Win32.RegistryKey regProKey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(prodKey);

            Microsoft.Win32.RegistryKey regAppKey = regProKey.OpenSubKey("Applications", true);

            string[] subKeys = regAppKey.GetSubKeyNames();

            foreach (string subkey in subKeys)
            {
                if (subkey.Equals(AppName))
                {
                    regAppKey.Close();

                    return;
                }
            }

            string path = Assembly.GetExecutingAssembly().Location;

            Microsoft.Win32.RegistryKey AppKey = regAppKey.CreateSubKey(AppName);

            AppKey.SetValue("DESCRIPTION", AppName, RegistryValueKind.String);

            AppKey.SetValue("LOADCTRLS", 14, RegistryValueKind.DWord);

            AppKey.SetValue("LOADER", path, RegistryValueKind.String);

            AppKey.SetValue("MANAGED", 1, RegistryValueKind.DWord);

            regAppKey.Close();
        }

        //多段线
        [CommandMethod("AddPolyLine")]

        public static void AddPolyLine()
        {
            Document acDoc = Application.DocumentManager.MdiActiveDocument;

            Database acDb = acDoc.Database;

            using (Transaction acTran = acDb.TransactionManager.StartTransaction())
            {
                BlockTable acBT = acTran.GetObject(acDb.BlockTableId, OpenMode.ForRead) as BlockTable;

                BlockTableRecord acBTR = acTran.GetObject(acBT[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;

                Point2d p2d1 = new Point2d(0, 0);

                Point2d p2d2 = new Point2d(10, 10);

                Point2d p2d3 = new Point2d(20, 30);

                Polyline acPL = new Polyline();

                acPL.AddVertexAt(0, p2d1, 0, 0, 0);

                acPL.AddVertexAt(1, p2d2, 0, 0, 0);

                acPL.AddVertexAt(2, p2d3, 0, 0, 0);

                acPL.ColorIndex = 1;

                acBTR.AppendEntity(acPL);

                acTran.AddNewlyCreatedDBObject(acPL, true);

                acTran.Commit();

            }

        }
    }
}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
AutoCAD .NET API(应用程序编程接口)是用于在AutoCAD软件中定制和扩展功能的编程工具。它提供了一组功能强大的类和方法,使开发人员能够通过编程方式访问和操作AutoCAD的各种对象和功能。 要下载AutoCAD .NET API,可以按照以下步骤进行操作: 1. 打开Autodesk官方网站,进入AutoCAD的下载页面。 2. 在页面上,找到AutoCAD .NET API的下载选项。通常可以在“附加工具”或“开发者工具”部分中找到。 3. 单击下载链接,选择适用于您的操作系统版本的AutoCAD .NET API软件包。 4. 点击下载按钮,等待下载完成。 在下载完成后,您可以按照以下步骤安装AutoCAD .NET API: 1. 打开下载文件的位置,双击运行安装程序。 2. 遵循安装程序的指示,选择您想要安装的文件夹和配置选项。 3. 完成安装后,您可以在指定的文件夹中找到安装文件和示例代码。 安装完成后,您可以开始使用AutoCAD .NET API来开发自定义功能。您可以使用.NET编程语言(如C#或VB.NET)来编写代码,并使用AutoCAD .NET API中的类和方法来访问和操作AutoCAD中的对象,如图形、图层、块等。您还可以创建自定义命令、工具栏、菜单和对话框,以及执行各种操作,如绘制、修改、查询和导出CAD数据。 总之,要下载AutoCAD .NET API,只需前往官方网站下载页面,并按照指示进行下载和安装即可。安装完成后,您可以使用AutoCAD .NET API进行自定义开发和定制AutoCAD软件的功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值