记录一下CAD二次开发的一些简单实例。
1、helloworld
2、画一个圆
3、画一条直线
4、取得图层下的所有对象id
5、得到当前图层的所有object的id
6、取得当前所有图层名称
7、添加图层

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
 {
 /// <summary>
 /// Hello World!!!
 /// </summary>
 [CommandMethod("HelloWorld")]
 public void HelloWorld()
 {
 Editor ed = AcadApp.DocumentManager.MdiActiveDocument.Editor;
 ed.WriteMessage("Hello World222");
 }
 /// <summary>
 /// 画一个圆
 /// </summary>
 [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(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();
 }
 /// <summary>
 /// 选择一个点,并显示坐标;选择两个点,并显示距离
 /// </summary>
 [CommandMethod("selectPoint")]
 public void SelectPoint()
 {
 PromptPointOptions prPointOptions = new PromptPointOptions("Select a point");
 PromptPointResult prPointRes;
 Editor ed = AcadApp.DocumentManager.MdiActiveDocument.Editor;
 prPointRes = ed.GetPoint(prPointOptions);
 if (prPointRes.Status != PromptStatus.OK)
 {
 ed.WriteMessage("Error");
 }
 else
 ed.WriteMessage("You selected point: " + prPointRes.Value.ToString());
 PromptDistanceOptions prDistOptions = new PromptDistanceOptions("Find distance, select first point:");
 PromptDoubleResult prDistRes;
 prDistRes = ed.GetDistance(prDistOptions);
 if (prDistRes.Status != PromptStatus.OK)
 {
 ed.WriteMessage("Error");
 }
 else
 {
 ed.WriteMessage("The distance is: " + prDistRes.Value.ToString());
 }
 }

 [CommandMethod("CreateDivision")]
 public void CreateDivision()
 {
 Database db = HostApplicationServices.WorkingDatabase;
 Transaction trans = db.TransactionManager.StartTransaction();

 try
 {
 //首先,获取 NOD……
 DBDictionary NOD = (DBDictionary)trans.GetObject(db.NamedObjectsDictionaryId, OpenMode.ForWrite);
 //定义一个公司级别的字典
 DBDictionary acmeDict;
 try
 {
 //如果 ACME_DIVISION 不存在,则转到 catch 块,这里什么也不做
 acmeDict = (DBDictionary)trans.GetObject(NOD.GetAt("ACME_DIVISION"), OpenMode.ForRead);
 }
 catch
 {
 //如果 ACME_DIVISION 不存在,则创建它并把它加入到 NOD 中……
 acmeDict = new DBDictionary();
 NOD.SetAt("ACME_DIVISION", acmeDict);
 trans.AddNewlyCreatedDBObject(acmeDict, true);
 }
 trans.Commit();
 }
 finally
 {
 trans.Dispose();
 }
 }
 /// <summary>
 /// 画一条直线
 /// </summary>
 [CommandMethod("FirstLine")]
 public static void FirstLine()
 {
 //获取当前活动图形数据库 HostApplicationServices.WorkingDatabase;
 Database db = HostApplicationServices.WorkingDatabase;
 //定义一个指向当前数据库的事务处理,以添加直线
 using (Transaction trans = db.TransactionManager.StartTransaction())
 {
 BlockTable acBlkTbl;
 acBlkTbl = trans.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable; //以读方式打开块表.
 //以写方式打开模型空间块表记录.
 BlockTableRecord acBlkTblRec;
 acBlkTblRec = trans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
 Point3d startPoint = new Point3d(0, 100, 0); //直线起点
 Point3d endPoint = new Point3d(100, 100, 0); //直线终点
 Line line = new Line(startPoint, endPoint); //新建一直线对象
 //将图形对象的信息添加到块表记录中,并返回ObjectId对象.
 acBlkTblRec.AppendEntity(line);
 trans.AddNewlyCreatedDBObject(line, true); //把对象添加到事务处理中.
 trans.Commit(); //提交事务处理
 }
 }

 #region "取得图层下的所有对象id"
 /// <summary>
 /// 取得图层下的所有对象id
 /// </summary>
 /// <param name="name">图层名称</param>
 /// <returns>id集合</returns>
 public ObjectIdCollection GetObjectIdsAtLayer(string name)
 {
 ObjectIdCollection ids = new ObjectIdCollection();
 PromptSelectionResult ProSset = null;
 TypedValue[] filList = new TypedValue[1] { new TypedValue((int)DxfCode.LayerName, name) };
 SelectionFilter sfilter = new SelectionFilter(filList);
 Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
 ProSset = ed.SelectAll(sfilter);
 if (ProSset.Status == PromptStatus.OK)
 {

 SelectionSet sst = ProSset.Value;
 ObjectId[] oids = sst.GetObjectIds();

 for (int i = 0; i < oids.Length; i++)
 {

 ids.Add(oids[i]);
 }
 }
 return ids;
 }
 #endregion
 /// <summary>
 /// 得到当前图层的所有几何的id
 /// </summary>
 [CommandMethod("GetAllId")]
 public void GetAllId()
 {
 Editor ed = AcadApp.DocumentManager.MdiActiveDocument.Editor;

 ObjectIdCollection ids = GetObjectIdsAtLayer("0");
 string str = "";
 Database db = HostApplicationServices.WorkingDatabase;
 //定义一个指向当前数据库的事务处理,以添加直线
 using (Transaction trans = db.TransactionManager.StartTransaction())
 {
 BlockTable acBlkTbl;
 acBlkTbl = trans.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable; //以读方式打开块表.
 //以写方式打开模型空间块表记录.
 BlockTableRecord acBlkTblRec;
 acBlkTblRec = trans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
 foreach (ObjectId item in ids)
 {
 Entity entity = (Entity)item.GetObject(OpenMode.ForRead, false, false);
 switch (entity.GetRXClass().Name)
 {
 case "AcDbText":
 DBText dbtext = entity as DBText;

 string strtext = dbtext.TextString;
 str += strtext + 'n';
 break;
 }
 }
 ed.WriteMessage(str);
 trans.Commit(); //提交事务处理
 } 
 }
 #region "取得当前所有图层名称"
 /// <summary>
 /// 取得当前所有图层名称
 /// </summary>
 /// <returns>图层名称集合</returns>
 [CommandMethod("GetAllLayer")]
 public ArrayList GetLayerName()
 {
 ArrayList layers = new ArrayList();
 using (Database db = HostApplicationServices.WorkingDatabase)
 {
 using (Transaction trans = db.TransactionManager.StartTransaction())
 {
 using (LayerTable lt = (LayerTable)trans.GetObject(db.LayerTableId, OpenMode.ForRead))
 {
 foreach (ObjectId id in lt)
 {
 LayerTableRecord ltr = (LayerTableRecord)trans.GetObject(id, OpenMode.ForRead);
 layers.Add(ltr.Name);
 }
 }
 trans.Commit();
 }
 }
 return layers;
 }
 #endregion

 #region 添加图层
 /// <summary>
 /// 添加图层
 /// </summary>
 /// <param name="layName"></param>
 /// <param name="layColor"></param>
 private ObjectId AddLayer(string layName, short layColor)
 {
 ObjectId oidReturn = new ObjectId(); ;
 using (Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.LockDocument())
 {
 Database database = HostApplicationServices.WorkingDatabase;
 Editor editor = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
 using (Transaction transaction = database.TransactionManager.StartTransaction())
 {
 try
 {
 ObjectId id;
 LayerTable table = transaction.GetObject(database.LayerTableId, OpenMode.ForWrite) as LayerTable;
 LayerTableRecord record = new LayerTableRecord();
 if (table.Has(layName))
 {
 id = table.Id;
 record = transaction.GetObject(id, OpenMode.ForWrite) as LayerTableRecord;
 record.IsOff = false;
 record.IsLocked = false;
 if (id != database.Clayer)
 {
 record.IsFrozen = false;
 }
 }
 else
 {
 record.Name = layName;
 id = table.Add(record);
 transaction.AddNewlyCreatedDBObject(record, true);
 }
 record.Color = Autodesk.AutoCAD.Colors.Color.FromColorIndex(ColorMethod.ByColor, layColor);
 database.Clayer = id;
 oidReturn = id;
 transaction.Commit();
 }
 catch (Autodesk.AutoCAD.Runtime.Exception exception)
 {
 editor.WriteMessage("Error in addLayer2(): " + exception.Message);
 }
 }
 return oidReturn;
 }
 }
 #endregion
 }
}
0

评论0

请先
2021版《广东省公路工程施工标准化指南》粤交基2021239号
2021版《广东省公路工程施工标准化指南》粤交基2021239号
8分钟前 有人购买 去瞅瞅看

社交账号快速登录

微信扫一扫关注
扫码关注后会自动登录网站