AutoCAD C#二次开发

https://www.cnblogs.com/gisoracle/archive/2012/02/19/2357925.html

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;

namespace MyFirstProject
{
    public class Class1
    {
        [CommandMethod("HelloNet")]
        public void HelloNet()
        {
            Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
            ed.WriteMessage("使用NET开发AutoCAD 程序bygisoracle");
        }
        [CommandMethod("PickPoint")]
        public void PickPoint()
        {
            //获取Editor 对象
            Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
            PromptPointOptions promptPtOp = new PromptPointOptions("选择一个点:");
            //指定的基点,如果指定了该点,则在选择的时候绘制一条橡皮线。
            promptPtOp.BasePoint = new Autodesk.AutoCAD.Geometry.Point3d(0, 0, 0);
            PromptPointResult resPt;
            resPt = ed.GetPoint(promptPtOp);
            if (resPt.Status == PromptStatus.OK)
            {
                ed.WriteMessage(" 选择的点为: " + resPt.Value.ToString());
            }

        }
        [CommandMethod("createCircle")]
        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(10, 10, 0), Vector3d.ZAxis, 2);
            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();

        }
        [CommandMethod("SelectAPoint")]
        public void SelectAPoint()
        {
            //实例化一个 PromptPointOptions类用来设置提示字符串和其他的一些控制提示
            PromptPointOptions prPointOptions = new PromptPointOptions("Select a point");
            PromptPointResult prPointRes;
            // 实例化一个Editor类,使用GetPoint方法返回
            Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
            prPointRes = ed.GetPoint(prPointOptions);
            if (prPointRes.Status != PromptStatus.OK)
            {
                ed.WriteMessage("Error");
            }
            else
            {
                ed.WriteMessage("选择的点为:" + prPointRes.Value.ToString());
            }
        }
        [CommandMethod("getDistance")]
        public void GetDistance()
        {
            PromptDistanceOptions prDistOptions = new
            PromptDistanceOptions("计算两点距离,请选择第一个点:");
            PromptDoubleResult prDistRes;
            Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
            prDistRes = ed.GetDistance(prDistOptions);
            if (prDistRes.Status != PromptStatus.OK)
            {
                ed.WriteMessage("选择错误!");
            }
            else
            {
                ed.WriteMessage("两点的距离为:" + prDistRes.Value.ToString());
            }
        }
        [CommandMethod("AddPointAndSetPointStyle")]

        public static void AddPointAndSetPointStyle()
        {
            // 获得当前文档和数据库   Get the current document and database
            Document acDoc = Application.DocumentManager.MdiActiveDocument;
            Database acCurDb = acDoc.Database;

            // 启动一个事务  Start a transaction
            using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
            {
                // 以只读方式打开块表   Open the Block table for read
                BlockTable acBlkTbl;
                acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId,
                                             OpenMode.ForRead) as BlockTable;

                // 以写方式打开模型空间块表记录   Open the Block table record Model space for write
                BlockTableRecord acBlkTblRec;
                acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],
                                                OpenMode.ForWrite) as BlockTableRecord;

                // 在模型空间中创建一个坐标为(4,3,0)的点   Create a point at (4, 3, 0) in Model space
                for (int i = 0; i < 100; i++)
                {
                    DBPoint acPoint = new DBPoint(new Point3d(4*i, 3, 0));

                    acPoint.SetDatabaseDefaults();

                    // 添加新对象到块表记录和事务中   Add the new object to the block table record and the transaction
                    acBlkTblRec.AppendEntity(acPoint);
                    acTrans.AddNewlyCreatedDBObject(acPoint, true);
                }

                // 在图形中设置所有点对象的样式   Set the style for all point objects in the drawing
                acCurDb.Pdmode = 34;
                acCurDb.Pdsize = 1;

                // 保存新对象到数据库中   Save the new object to the database
                acTrans.Commit();
            }
        }
        [CommandMethod("Add2DSolid")]
        public static void Add2DSolid()
        {
            // 获得当前文档和数据库   Get the current document and database
            Document acDoc = Application.DocumentManager.MdiActiveDocument;
            Database acCurDb = acDoc.Database;

            // 启动一个事务  Start a transaction
            using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
            {
                // 以只读方式打开块表   Open the Block table for read
                BlockTable acBlkTbl;
                acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId,
                                             OpenMode.ForRead) as BlockTable;

                // 以写方式打开模型空间块表记录   Open the Block table record Model space for write
                BlockTableRecord acBlkTblRec;
                acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],
                                                OpenMode.ForWrite) as BlockTableRecord;

                // Create a quadrilateral (bow-tie) solid in Model space

                Solid ac2DSolidBow = new Solid(new Point3d(0, 0, 0),
                                               new Point3d(5, 0, 0),
                                               new Point3d(5, 8, 0),
                                               new Point3d(0, 8, 0));

                ac2DSolidBow.SetDatabaseDefaults();

                // 添加新对象到块表记录和事务中   Add the new object to the block table record and the transaction
                acBlkTblRec.AppendEntity(ac2DSolidBow);
                acTrans.AddNewlyCreatedDBObject(ac2DSolidBow, true);

                // Create a quadrilateral (square) solid in Model space
                Solid ac2DSolidSqr = new Solid(new Point3d(10, 0, 0),
                                               new Point3d(15, 0, 0),
                                               new Point3d(10, 8, 0),
                                               new Point3d(15, 8, 0));

                ac2DSolidSqr.SetDatabaseDefaults();

                // 添加新对象到块表记录和事务中   Add the new object to the block table record and the transaction
                acBlkTblRec.AppendEntity(ac2DSolidSqr);
                acTrans.AddNewlyCreatedDBObject(ac2DSolidSqr, true);

                // 保存新对象到数据库中   Save the new object to the database
                acTrans.Commit();
            }
        }

        [CommandMethod("AddLine")]
        public static void AddLine()
        {
            // 获得当前文档和数据库   Get the current document and database
            Document acDoc = Application.DocumentManager.MdiActiveDocument;
            Database acCurDb = acDoc.Database;

            // 启动一个事务  Start a transaction
            using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
            {
                // 以只读方式打开块表   Open the Block table for read
                BlockTable acBlkTbl;
                acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId,
                                             OpenMode.ForRead) as BlockTable;

                // 以写方式打开模型空间块表记录   Open the Block table record Model space for write
                BlockTableRecord acBlkTblRec;
                acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],
                                                OpenMode.ForWrite) as BlockTableRecord;

                // 创建一条起点为(5,5,0),终点为(12,3,0)的直线  Create a line that starts at 5,5 and ends at 12,3
                Line acLine = new Line(new Point3d(5, 5, 0),
                                       new Point3d(12, 3, 0));

                acLine.SetDatabaseDefaults();

                // 添加新对象到块表记录和事务中   Add the new object to the block table record and the transaction
                acBlkTblRec.AppendEntity(acLine);
                acTrans.AddNewlyCreatedDBObject(acLine, true);

                // 保存新对象到数据库中   Save the new object to the database
                acTrans.Commit();
            }

        }
    }
}

原文地址:https://www.cnblogs.com/belx/p/9256364.html

时间: 2024-08-15 09:09:45

AutoCAD C#二次开发的相关文章

利用C#进行AUTOCAD的二次开发

众所周知,对AutoCAD进行二次开发用到的主要工具有:ObjectArx,VBA,VLisp.但它们的优缺点是显而易见的:ObjectArx功能强大,编程效率高,但它的缺点是编程者必须掌握VC++,而这门语言非常的难学;VBA和VLisp虽然简单易上手,但它们对于开发大型的程序好象无能为力.那究竟有没有一种语言能结合它们的优点而尽量避免它们的缺点呢? 回答是肯定的,那就是微软新推出的21世纪编程语言C#.关于C#的详细介绍,大家可以参考有关的文章. C#是通过AutoCAD ActiveX 这

AutoCAD .NET二次开发(一)

其他话不多说,直接进入主题,既然是二次开发,当然是用CAD平台已经封装好了很多类,我们需要熟悉和使用它们.常用的AutoCAD .NET API的四个主要DLL文件是: 名称 作用 备注 AcDbMgd.dll 处理图形文件中存储的对象   AcMgd.dll 处理AutoCAD应用程序和用户接口   AcCui.dll 处理自定义文件   AcCoreMgd.dll 处理编辑器.发布与打印.定义AutoLISP命令和函数 AutoCAD 2014版 我们现在不必去深究这些类库的构造,这四个引用

AutoCAD .NET二次开发(四)

在CAD中,属性信息一般是以注记的形式存在,但当属性数据内容较多时,显示就成了问题.扩展属性(Xdata)可以解决这一问题,比如南方Cass中就利用了这一点.我们经常用Lisp来读取操作扩展属性. 查看实体属性信息: (entget(car(entsel))'("*")) 但我们为了方便与GIS交互,我们得在.NET中来读取.修改CAD文件中实体的扩展属性.本文先只讲解怎么读取扩展属性. 第一步,不管怎么样,都要先打开文件,获取文档,选择对象,获取实体.下面这段代码在测试中肯定会经常用

AutoCAD二次开发&mdash;&mdash;AutoCAD.NET API开发环境搭建

AutoCAD二次开发--AutoCAD.NET API开发环境搭建 AutoCAD二次开发--AutoCAD.NET API开发环境搭建 AutoCAD二次开发工具:1986年AutoLisp,1989年ADS,1990年DCL,1993年ADS-RX,1995年ObjectARX,1996年Active X Automation(COM),1997年VBA,1998年Visual Lisp,2006年.net API(DLL). 趋势和方向:AutoCAD.net API(AutoCAD20

.NET AutoCAD二次开发之路(一、基础篇)

学习AutoCAD二次开发已经有一段时间了,磕磕碰碰,十分的艰辛枯燥.但回想一下还是会有些小激动,嘿嘿!最近这段时间一直都有这么个想法,就是将我学习CAD二次开发的过程用文字的方式记录下来,形成系列,并定期更新.主要内容就是记录自己每天学习过程中所碰见的问题和感悟,并总结今天所学到的内容.目的一是激励自己坚持下去,看着自己一步一步的提高,多有成就感.还有就是和大家积极沟通,希望能够指正我的错误,减少我的弯路.再者就是望能给后面学习的人一点点借鉴经验,好吧其实还有点装B的心里.废话不多说就进入今天

AutoCAD二次开发

AutoCAD本身是功能强大.可配置性极高的软件,并且由于AutoCAD良好的软件架构,AutoCAD的二次开发能力也极强,几乎所有可见的软件功能都能在SDK内找到封装好的接口,由于AutoCAD SDK的向后兼容性极好,写出的程序几乎不用修改就可以适用于所有版本的AutoCAD. AutoCAD的第一代开发工具是AutoLISP,是AutoCAD定制的LISP语言,至今仍然是一个活跃的LISP分支,第二代开发工具是基于C语言的ADS,目前来看几乎被淘汰了,也是被AutoDesk公司标记为dep

.NET AutoCAD二次开发之路(一、直线篇)

我以前只有一点VB的基础,C语言也只是看过,学这个CAD二次开发,也是借工作之余研究下,我到现在已经从事机械行业5年了,中国的工业设计环境区别太大,各行各业的技术现状也不尽相同,但有一点是可以肯定的,那就是自动化,国外都在工业4.0了,我们还在互联网+,哎太out了,我也赶紧进修下,要不就淘汰了.所以我选择了CAD二次开发,主要还是跟专业接近,希望能越学越精吧,未来就让未来的我去面对! 今天主要总结下直线的创建,要想能在CAD中生成直线或者其他图形,都必须遵循几个步骤,那就是: 1.获取当前图形

C#/AutoCAD 2018/ObjectArx/二次开发添加圆对象的的例子(五)

1.创建一个图形对象的步骤如下见上一篇博客(三)2.添加删除实体的工具函数见上一篇博客(四) 3.添加圆的例子(完整源代码请加云幽学院免费课yunyun.ke.qq.com)   [CommandMethod("MKCircle")]        public void MKCircle()        {            //(1)获取当前活动图形数据库            Database db = HostApplicationServices.WorkingData

AutoCAD二次开发-使用ObjectARX向导创建应用程序(HelloWorld例子)

AutoCAD2007+vs2005 首先自己去网上搜索下载AutoCAD2007的ARX开发包. 解压后如下 打开后如下 classmap文件夹为C++类和.net类的框架图,是一个DWG文件. docs帮助文件 inc引用的头文件 lib引用的库文件 redistrib其他引用的库文件 samples例子 utils其他东西 打开utils 上面几个是头文件和库文件 ObjARXWiz这个文件夹是向导(2012以后的版本就没有了,要去官网单独下载,不和开发包在一起) 打开后如下 直接双击安装