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

            }

        }
    }
}

你可能感兴趣的:(AutoCAD.NET,API2018,.net,microsoft,c#)