zoukankan      html  css  js  c++  java
  • 【AutoCAD.NET】创建一个类库DLL文件(DotNetARX)写入一个工具类(Tools)以简化代码的编写

    01.创建一个类库(名为DotNetARX.dll)引用所需CAD开发的dll,设置赋值本地为false;创建静态类和静态方法(不用实例化,直接通过类名进行调用);函数说明用///

    (向当前模型空间中添加实体图元)代码如下:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Autodesk.AutoCAD.DatabaseServices;
    using Autodesk.AutoCAD.Runtime;
    using Autodesk.AutoCAD.ApplicationServices;
    namespace DotNetARX
    {
        public static class Tools
        {
            /// <summary>
            /// 将实体图元添加到模型空间
            /// </summary>
            /// <param name="ent"></param>实体图元
            /// <returns>返回添加到模型空间中的实体ObjectId</returns>
            public static ObjectId AddToModelSpace(Entity ent)
            {
                ObjectId entId;//用于返回添加到模型空间中的实体ObjectId
                //定义一个指向当前数据库的事务处理,以添加实体
                Document acDoc = Application.DocumentManager.MdiActiveDocument;
                Database db = acDoc.Database;
                using (Transaction trans = db.TransactionManager.StartTransaction())
                {
                    BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);//以读的方式打开块表
                    //以写方式打开模型空间块表记录
                    BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
                    entId = btr.AppendEntity(ent);//将图形对象的信息添加到块表记录中
                    trans.AddNewlyCreatedDBObject(ent, true);//把对象添加到事务处理中
                    trans.Commit();//提交事务处理
                }
                return entId;
            }
        }
    }

    02.使用静态类中的静态方法,下面是通过创建一条直线来示例:

    先引用开发所需的CAD的dll文件设置复制本地为false;添加引用自己所创建的静态类库DotNetARX.dll;添加命名空间,采用类名.方法名的方式引用

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Autodesk.AutoCAD.DatabaseServices;
    using Autodesk.AutoCAD.Runtime;
    using Autodesk.AutoCAD.ApplicationServices;
    using Autodesk.AutoCAD.Geometry;
    using DotNetARX;//添加静态类的命名空间
    
    namespace Test
    {
        public static class class1
        {
            [CommandMethod("HLine")] //注册一个命令
            public static void HLine()
            {
                
                Line acline = new Line(new Point3d(0, 0, 0), new Point3d(10000, 1000, 0));
                Tools.AddToModelSpace(acline);//***引用静态类中的方法***
            }
        }
    }

    03.创建一个程序集,新建扩展方法,采用 [对象.方法]的模式调用

    namespace DotNetARX
    {
        public static class EntTools
        {
            /// <summary>
            /// 将实体图元添加到模型空间
            /// </summary>
            /// <param name="ent"></param>实体图元
            /// <returns>返回添加到模型空间中的实体ObjectId</returns>
            public static ObjectId AddToModelSpace(this Database db, Entity ent)
            {
                ObjectId entId;//用于返回添加到模型空间中的实体ObjectId
                //定义一个指向当前数据库的事务处理,以添加实体
                // Document acDoc = Application.DocumentManager.MdiActiveDocument;
                // Database db = acDoc.Database;
                using (Transaction trans = db.TransactionManager.StartTransaction())
                {
                    BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);//以读的方式打开块表
                    //以写方式打开模型空间块表记录
                    BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
                    entId = btr.AppendEntity(ent);//将图形对象的信息添加到块表记录中
                    trans.AddNewlyCreatedDBObject(ent, true);//把对象添加到事务处理中
                    trans.Commit();//提交事务处理
                }
                return entId;
            }
        }
    }

    04.调用03中的程序集内的方法

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Autodesk.AutoCAD.DatabaseServices;
    using Autodesk.AutoCAD.Geometry;
    using Autodesk.AutoCAD.Runtime;
    using DotNetARX;//添加对程序集空间的引用
    
    namespace testAddLine
    {
        public class Class1
        {
            [CommandMethod("TestAddLine")]
            public void TestAddLine()
            {
                Point3d startPoint = new Point3d(0,0,0);
                Point3d endPoint = new Point3d(1000,1000,0);
                Line newLine = new Line(startPoint,endPoint);
                Database db = HostApplicationServices.WorkingDatabase;
                
                //调用程序集函数
                db.AddToModelSpace(newLine);
            }
        }
    }

    <<the end>>

  • 相关阅读:
    ntohs, ntohl, htons,htonl的比较和详解
    转 linux socket的select函数例子
    转 结构体中字节对齐问题(转载)
    C语言中volatile关键字的作用
    转 字符串模式匹配算法——BM、Horspool、Sunday、KMP、KR、AC算法
    转 常见hash算法的原理
    转 从头到尾彻底解析Hash表算法
    [CEOI2020 D2T1 / CF1403A] 权力药水The Potion of Great Power 题解
    [CEOI2020 D1T3 / CF1402C] 星际迷航Star Trek 题解
    [CEOI2020 D1T2 / CF1402B] 道路Roads 题解
  • 原文地址:https://www.cnblogs.com/Helchan/p/4463956.html
Copyright © 2011-2022 走看看