天天看点

CAD二次开发入门——Hello World以及画一个圆

cad二次开发的准备工作:需要在cad的安装文件中加载红色框中的dll文件。

CAD二次开发入门——Hello World以及画一个圆

然后就是一堆Using:

CAD二次开发入门——Hello World以及画一个圆

接着注意版本的问题,因为visual studio2015用的是新的程序集,所以最好cad也用比较高的版本,我这里直接用的2016版的cad。

然后就是一个在cad中加载生成的dll文件的问题。这里用下面的这个方法:

http://jingyan.baidu.com/article/d8072ac4930316ec94cefd67.html

using System;
using System.Linq;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;
using System.Windows.Forms;
using System.Reflection;

namespace myNetLoad
{
    public class MyClass
    {
        //本程序在AutoCAD的快捷命令是"NL"
        [CommandMethod("NL")]
        public void myLoad()
        {
            //AutoCAD命令栏
            Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
            //调用Windows.Forms选择一个文件
            OpenFileDialog fileDialog = new OpenFileDialog();
            //判断确认按钮
            if (fileDialog.ShowDialog() == DialogResult.OK)
            {
                //选择的文件路径
                string file_dir = fileDialog.FileName;
                //在AutoCAD命令栏输出选择的文件路径
                ed.WriteMessage("文件路径:" + file_dir);
                //打开文件,将文件以二进制方式复制到内存,自动关闭文件
                byte[] buffer = System.IO.File.ReadAllBytes(file_dir);
                //加载内存中的文件
                Assembly assembly = Assembly.Load(buffer);
            }
        }
    }
}
           

注意最后覆盖新的dll文件的时候注意用新的程序集,即链接方法的最后一步。

接下来开始我们的正文——Hello World以及画一个圆。

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

using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.Windows;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Colors;
using Autodesk.AutoCAD.EditorInput;

using System.Collections;
using AcadApp = Autodesk.AutoCAD.ApplicationServices.Application;
using System.Reflection;

namespace cad开发
{

    public class Class1
    {
        [CommandMethod("HelloWorld")]
        public void HelloWorld()
        {
            Editor ed = AcadApp.DocumentManager.MdiActiveDocument.Editor;
            ed.WriteMessage("Hello World222");
        }
        [CommandMethod("test")]
        public void createCircle()
        { //首先声明我们要使用的对象
            Circle circle; //这个是我们要加入到模型空间的圆
            BlockTableRecord btr;//要加入圆,我们必须打开模型空间
            BlockTable bt; //要打开模型空间,我们必须通过块表(BlockTable)来访问它

            //我们使用一个名为‘Transaction’的对象,把函数中有关数据库的操作封装起来
            Transaction trans;
            //使用 TransactionManager 的 StartTransaction()成员来开始事务处理
            trans = HostApplicationServices.WorkingDatabase.TransactionManager.StartTransaction();
            //现在创建圆……请仔细看这些参数——注意创建 Point3d 对象的‘New’和 Vector3d 的静态成员 ZAxis
            circle = new Circle(new Point3d(, , ), Vector3d.ZAxis, );
            bt = (BlockTable)trans.GetObject(HostApplicationServices.WorkingDatabase.BlockTableId, OpenMode.ForRead);
            //使用当前的空间 Id 来获取块表记录——注意我们是打开它用来写入
            btr = (BlockTableRecord)trans.GetObject(HostApplicationServices.WorkingDatabase.CurrentSpaceId, OpenMode.ForWrite);
            //现在使用 btr 对象来加入圆
            btr.AppendEntity(circle);
            trans.AddNewlyCreatedDBObject(circle, true); //并确定事务处理知道要加入圆!
                                                         //一旦完成以上操作,我们就提交事务处理,这样以上所做的改变就被保存了……
            trans.Commit();
            //…然后销毁事务处理,因为我们已经完成了相关的操作(事务处理不是数据库驻留对象,可以销毁)
            trans.Dispose();
        }
    }
}
           

在cad中先用NETLOAD命令加载myNetLoad.dll,然后在命令框中加载NL命令,然后再把”cad开发.dll”加载进去。然后运行HelloWorld命令,命令框中直接出现Hello World。然后在命令框中输入test,cad里就直接画了一个圆。

CAD二次开发入门——Hello World以及画一个圆

继续阅读