博客
关于我
用C#编写AutoCAD中的直线
阅读量:769 次
发布时间:2019-03-22

本文共 1851 字,大约阅读时间需要 6 分钟。

好的,现在我将按照您的要求优化内容:


在Visual Studio 2005中使用C#开发AutoCAD插件Step-by-Step指南


一、创建项目

  • 打开Visual Studio 2005

    打开VS2005,选择菜单栏的 Visual C# > 新建项目。

  • 选择项目类型

    在新项目窗口中,选择 类库 类型,给项目命名为 Line2,点击下一步即可。


  • 二、配置项目

  • 添加必要的DLL引用

    在项目中添加需要的三个DLL文件:

    • acdbmgd.dll 和 acdmgd.dll

      这些文件是AutoCAD托管封装类,可以从AutoCAD的安装文件中找到。确保它们的属性设置为 复制到本地:.off。

    • DotNetARX.dll

      这是一个支持.NET开发AutoCAD程序的类库,建议使用DotNetARX6.0版本。从网上下载后,将其添加到项目中。

  • 禁用LoaderLock

    点击菜单栏的 调试 > 异常 > .managed debugging assistants,取消勾选 LoaderLock。

  • 启用外部程序

    • 在项目属性中,选择 调试 标签。
    • 勾选 启用外部程序,输入路径:C:\Program Files\AutoCAD 2006\acad.exe。

  • 三、编写代码

  • 创建代码文件

    • 在 VS 2005 中的项目中右键点击 Line2.cs,选择新建,添加 Line2.cs 文件。
    • 输入以下代码:
    using Autodesk.AutoCAD.DatabaseServices;using Autodesk.AutoCAD.Geometry;namespace Line2{    public class Line2    {        [CommandMethod("FirstLine")]        public static void FirstLine()        {            // 获取当前工作数据库            Database db = HostApplicationServices.WorkingDatabase;            // 定义直线起点和终点            Point3d startPoint = new Point3d(500, 500, 0);            Point3d endPoint = new Point3d(700, 500, 0);            // 创建直线对象            Line line = new Line(startPoint, endPoint);            // 开始事务处理            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);                // 将直线添加到块表记录中                btr.AppendEntity(line);                trans.AddNewlyCreatedDBObject(line, true);                trans.Commit(); // 提交事务            }        }    }}

  • 四、调试与测试

  • 使用F5键启动调试

    点击F5键,程序运行 。注意在AutoCAD命令行窗口输入:

    netload

    然后选择 Line2.dll(通常位于Debug目录下)。

  • 运行自定义命令

    • 在命令行输入 FirstLine,AutoCAD将画出一条直线。

  • 完成!

    以上步骤将帮助您成功在AutoCAD中使用C#开发插件。如果遇到问题,请检查DLL文件路径及AutoCAD版本兼容性。

    转载地址:http://pkgwk.baihongyu.com/

    你可能感兴趣的文章
    Python3:setup.py:完成所有任务的PIP安装(Build_ext+Install)
    查看>>
    python4.0 gil_Python 并发编程系列--1、浅谈GIL历史及对多线程的影响
    查看>>
    python60行绘图程序_天底下最简单的QT画图板,就一个类,60行代码
    查看>>
    Python: AES加密与解密
    查看>>
    Python: Range() 最大尺寸;动态的还是静态的?
    查看>>
    Python: Windows下用 multiprocessing 的深坑
    查看>>
    python:Could not find platform independent libraries <prefix>解决方法
    查看>>
    Python:datetime tzinfo 时区名称文档
    查看>>
    Python:deepcopy(list) vs new_list = old_list[:]
    查看>>
    Python:Google API - 从消息中获取 mimeTypes
    查看>>
    Python:Google API - 从消息中获取 mimeTypes
    查看>>
    Python:subprocess.call,stdout to file,stderr to file,在屏幕上实时显示stderr
    查看>>
    Python:TypeError:无法连接“str“和“int“对象
    查看>>
    Python:XPath 在 ElementTree 中不可用
    查看>>
    Python:从 FTP 服务器下载文件
    查看>>
    python:从列表中创建元组列表
    查看>>
    Python:从嵌套列表中删除重复项
    查看>>
    Python:从间隔映射到值
    查看>>
    Python:使用 shell=False 的子进程调用不起作用
    查看>>
    Python:修改 PATH 环境变量的平台无关方式
    查看>>