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(, , );
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(, , ), 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(); }
[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 = ; i < ; i++)
{
DBPoint acPoint = new DBPoint(new Point3d(*i, , )); 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 = ;
acCurDb.Pdsize = ; // 保存新对象到数据库中 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(, , ),
new Point3d(, , ),
new Point3d(, , ),
new Point3d(, , )); 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(, , ),
new Point3d(, , ),
new Point3d(, , ),
new Point3d(, , )); 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(, , ),
new Point3d(, , )); 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();
} }
}
}

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

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

    众所周知,对AutoCAD进行二次开发用到的主要工具有:ObjectArx,VBA,VLisp.但它们的优缺点是显而易见的:ObjectArx功能强大,编程效率高,但它的缺点是编程者必须掌握VC++, ...

  2. AutoCAD .NET二次开发(一)

    其他话不多说,直接进入主题,既然是二次开发,当然是用CAD平台已经封装好了很多类,我们需要熟悉和使用它们.常用的AutoCAD .NET API的四个主要DLL文件是: 名称 作用 备注 AcDbMg ...

  3. AutoCAD.NET二次开发:创建自定义菜单(AcCui)

    从CAD2007之后,Autodesk提供了一个新的程序集AcCui.dll,使用这个程序集,我们可以方便地做一些界面方面的操作,比如创建自定义菜单. 下面介绍一下菜单的创建过程: 1.在项目中添加引 ...

  4. AutoCAD .NET二次开发(四)

    在CAD中,属性信息一般是以注记的形式存在,但当属性数据内容较多时,显示就成了问题.扩展属性(Xdata)可以解决这一问题,比如南方Cass中就利用了这一点.我们经常用Lisp来读取操作扩展属性. 查 ...

  5. AutoCAD .NET二次开发(三)

    在ArcGIS中,锁是一个经常遇到的东西,在打开一个该当时要锁定,编辑一个文档是再次锁定.要深入理解这个,要学习一下进程与线程.在CAD.NET中,也有Lock与Unlock. 获取一个文档,在进行处 ...

  6. AutoCAD.NET二次开发:扩展数据之XData

    结果缓存——ResultBuffer 结果缓存即 Autodesk.AutoCAD.DatabaseServices.ResultBuffer 类型,使用 ResultBuffer 对象时需要提供一个 ...

  7. AutoCAD.NET二次开发:创建自定义菜单的两种方法比较

    目前我已经掌握的创建CAD菜单方法有两种: COM方式: http://www.cnblogs.com/bomb12138/p/3607929.html CUI方式: http://www.cnblo ...

  8. AutoCAD.NET二次开发:创建自定义菜单(COM)

    当我们要在CAD中创建自定菜单时,可以引用COM组件来实现. 下面是实现方式: 1.新建类库项目,并引用CAD目录(我这里用的是CAD2008)下的acdbmgd.dll.acmgd.dll,并将引用 ...

  9. AutoCAD DevTV-AUTOCAD二次开发资源合集

    Webcast Language Date AutoCAD .Net - Session 2 English 13-Sep-12 AutoCAD .Net - Session 1 English 6- ...

随机推荐

  1. [NOIP2013]华容道 题解(搜索)

    [NOIP2013]华容道 [题目描述] 这道题根据小时候玩华容道不靠谱的经验还以为是并查集,果断扑街.考后想想也是,数据这么小一定有他的道理. 首先由于是最小步数,所以BFS没跑了.那么我们大可把这 ...

  2. SpringCloud-Alibaba-Sentinel(1)初探

    Sentinel 是什么? Sentinel 具有以下特征: 丰富的应用场景:Sentinel 承接了阿里巴巴近 10 年的双十一大促流量的核心场景,例如秒杀(即突发流量控制在系统容量可以承受的范围) ...

  3. 【原创】一个shell脚本记录(实现rsync生产文件批量迁移功能)

    #!/bin/bash #Date:2018-01-08 #Author:xxxxxx #Function:xxxxxx #Change:2018-01-17 # #设置忽略CTRL+C信号 trap ...

  4. java调用新浪接口根据Ip查询所属地区

    import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import ...

  5. 【CYH-02】NOIp考砸后虐题赛:函数:题解

    这道题貌似只有@AKEE 大佬A掉,恭喜! 还有因为c++中支持两个参数数量不同的相同名称的函数调用,所以当时就没改成两个函数,这里表示抱歉. 这道题可直接用指针+hash一下,然后就模拟即可. 代码 ...

  6. springboot简单入门笔记

    一.Spring Boot 入门 1.Spring Boot 简介 简化Spring应用开发的一个框架: 整个Spring技术栈的一个大整合: J2EE开发的一站式解决方案: 2.微服务 2014,m ...

  7. Linux/Ubuntu正确卸载LXDE

    第一步: sudo apt-get remove lxde 第二步 sudo apt autoremove lxde

  8. python基础练习 斐波那契数列

    转载于知乎刘奕聪的方法 一 f = [1, 1]print([f.append((f[-1] + f[-2])) or f.pop(0) for i in range(100)]) ///  f.ap ...

  9. LiteDB源码解析系列(2)数据库页详解

    在这一篇里,我将用图文的方式展示LiteDB中页的结构及作用,内容都是原创,在描述的过程中有不准确的地方烦请指出. 1.LiteDB页的技术工作原理 LiteDB虽然是单个文件类型的数据库,但是数据库 ...

  10. JedisClient操作redis 单机版和集群版

    一.在pom文件中添加依赖 <!-- https://mvnrepository.com/artifact/redis.clients/jedis --> <dependency&g ...