123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using Autodesk.AutoCAD.ApplicationServices;
- using Autodesk.AutoCAD.DatabaseServices;
- using Autodesk.AutoCAD.EditorInput;
- using Autodesk.AutoCAD.Runtime;
- using Autodesk.AutoCAD.Colors;
- using DotNetARX2014;
- using Autodesk.AutoCAD.Geometry;
- namespace CadStudy
- {
- public class Cmd
- {
- //Demo1:CmdLine直线
- [CommandMethod("CmdLine")]
- public void CmdLine()
- {
- Point3d startPoint = new Point3d(0, 0, 0); //声明起始点
- Point3d endPoint = new Point3d(100, 200, 0); //声明终止点
- Line line = new Line(startPoint, endPoint); //声明直线
- line.Color = Color.FromRgb(255, 0, 0); //设置颜色
- //line.Layer = "3"; //设置图层
- line.LineWeight = LineWeight.LineWeight050; //设置线宽
- //开启事务
- Database db = HostApplicationServices.WorkingDatabase;
- 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();
- }
- }
- //Demo2:CmdCir圆
- [CommandMethod("CmdCir")]
- public void CmdCir()
- {
- Circle circle = new Circle();//声明圆
- circle.Center = new Point3d(10, 20, 0); //声明圆心
- circle.Radius = 10; //声明半径
- //调用方法
- AddEntity(circle);
- }
- //Demo3:CmdArc圆弧
- [CommandMethod("CmdArc")]
- public void CmdArc()
- {
- //声明圆弧
- Arc arc = new Arc()
- {
- Center = new Point3d(50, 50, 0), //设置圆弧的圆心
- Radius = 10, //设置半径
- StartAngle = 0, //设置起始的弧度
- EndAngle = Math.PI / 2, //设置终点的弧度
- };
- AddEntity(arc);
- }
- //Demo4:AddPolyline多段线
- [CommandMethod("AddPolyline")]
- public void AddPolyline()
- {
- Polyline polyline = new Polyline();//声明多段线
- //AddVertexAt:给多段线加拐点
- //第一个参数表示在第几个序列加点
- //第二个参数表示这个点的坐标
- //第三个参数表示这个点的凸起
- //第四个参数表示线的初始宽度
- //第五个参数表示线的终止宽度
- polyline.AddVertexAt(0, new Point2d(0, 0), 0, 0, 0);
- polyline.AddVertexAt(1, new Point2d(10, 10), 0, 0, 0);
- polyline.AddVertexAt(2, new Point2d(20, 0), 0, 0, 0);
- //重新设置拐点的坐标
- //polyline.SetPointAt(1, new Point2d(20, 30));
- //移除拐点
- //polyline.RemoveVertexAt(1);
- //NumberOfVertices:多段线所有的拐点数
- for (int i = 0; i < polyline.NumberOfVertices; i++)
- {
- Point2d point2D = polyline.GetPoint2dAt(i);//获取每一个点
- }
- polyline.Closed = false; //是否闭合
- AddEntity(polyline);
- }
- //Demo5:SelecetDemo选择集
- [CommandMethod("SelectDemo")]
- public void SelectDemo()
- {
- List<Entity> entities = SelectEntities();
- }
- //Demo6:单行文字
- [CommandMethod("AddText")]
- public void AddText()
- {
- DBText text = new DBText
- {
- TextString = "单行文字", //文字内容
- Position = new Point3d(10, 10, 0), //文字坐标
- Height = 3, //文字高度
- WidthFactor = 0.8, //宽度因子
- //Rotation = Math.PI / 4, //旋转角度
- //IsMirroredInX = true, //在X轴方向上镜像
- //IsMirroredInY = true, //在Y轴方向上镜像
- //HorizontalMode = TextHorizontalMode.TextMid,
- //AlignmentPoint = new Point3d(20, 20, 0),
- //HorizontalMode = TextHorizontalMode.TextAlign,
- //AlignmentPoint = new Point3d(20, 20, 0),
- };
- List<TextStyleTableRecord> textStyleTableRecords = GetTextStyle();
- //判断是否有样式名为123
- //if表达式的意思是集合中有任意一个的样式等于123就返回true
- if (textStyleTableRecords.Any(x => x.Name == "123"))
- {
- //将第一个匹配的元素找出来
- text.TextStyleId = textStyleTableRecords.Find(x => x.Name == "123").ObjectId;
- }
- AddEntity(text);
- }
- //将公共方法提取出来
- private void AddEntity(Entity entity)
- {
- //开启事务
- Database db = HostApplicationServices.WorkingDatabase;
- 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(entity);
- trans.AddNewlyCreatedDBObject(entity, true);
- trans.Commit();
- }
- }
- //获取文字样式的方法
- public List<TextStyleTableRecord> GetTextStyle()
- {
- List<TextStyleTableRecord> result = new List<TextStyleTableRecord>();
- //获取数据库
- Database db = HostApplicationServices.WorkingDatabase;
- //打开事务
- using (Transaction trans = db.TransactionManager.StartTransaction())
- {
- //获取字体样式的样式表
- TextStyleTable tt = (TextStyleTable)trans.GetObject(db.TextStyleTableId,OpenMode.ForRead);
- //遍历样式表
- foreach (var item in tt)
- {
- //获取到每一个字体样式的记录
- TextStyleTableRecord ttr = (TextStyleTableRecord)trans.GetObject(item, OpenMode.ForRead);
- result.Add(ttr);
- }
- }
- return result;
- }
- //获取选择集的公共方法
- private List<Entity> SelectEntities()
- {
- Editor acDocEd = Application.DocumentManager.MdiActiveDocument.Editor;
- //获取PickFirst选择集
- PromptSelectionResult acSSPrompt;
- SelectionSet acSSet;
- //清空PickFirst选择集
- ObjectId[] idarrayEmpty = new ObjectId[0];
- acDocEd.SetImpliedSelection(idarrayEmpty);
- //请求从图形区域选择对象
- acSSPrompt = acDocEd.GetSelection();
- //如果提示状态O看,表示已选择对象
- if (acSSPrompt.Status == PromptStatus.OK)
- {
- acSSet = acSSPrompt.Value;
- List<ObjectId> objectIds = acSSet.GetObjectIds().ToList();
- Database database = HostApplicationServices.WorkingDatabase;
- List<Entity> entities = new List<Entity>();
- using (Transaction tran = database.TransactionManager.StartTransaction())
- {
- foreach (var item in objectIds)
- {
- Entity entity = (Entity)item.GetObject(OpenMode.ForRead);
- entities.Add(entity);
- }
- }
- return entities;
- }
- else
- {
- Application.ShowAlertDialog("Number of objects selected:0");
- return null;
- }
- }
- }
- }
|