赞
踩
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; //运行时 using Autodesk.AutoCAD.Runtime; //数据库 using Autodesk.AutoCAD.DatabaseServices; //应用程序服务 using Autodesk.AutoCAD.ApplicationServices; //几何 using Autodesk.AutoCAD.Geometry; using Autodesk.AutoCAD.Interop; using Autodesk.AutoCAD.Interop.Common; //编辑输入 using Autodesk.AutoCAD.EditorInput; namespace AutoDemo19 { public class Class1 { //合并选择集 [CommandMethod("MergeSS")] public static void MergeSS() { //当前文档编辑器 Editor ed = Application.DocumentManager.MdiActiveDocument.Editor; //选择图形对象 PromptSelectionResult psr; //获得选择结果 psr = ed.GetSelection(); SelectionSet ss; //对象集合 ObjectIdCollection oiC = new ObjectIdCollection(); //是否选中图形 if (psr.Status == PromptStatus.OK) { //获得选择集 ss = psr.Value; //选择图形放入集合 oiC = new ObjectIdCollection(ss.GetObjectIds()); } psr = ed.GetSelection(); SelectionSet ss2; //是否有选中图形 if (psr.Status == PromptStatus.OK) { //选中结果 ss2 = psr.Value; //当前选中的个数是否为0 if (oiC.Count == 0) { oiC = new ObjectIdCollection(ss2.GetObjectIds()); } else { foreach (ObjectId oi in ss2.GetObjectIds()) { oiC.Add(oi); } } } Application.ShowAlertDialog("合并后的选择集的图形个数是:" + oiC.Count.ToString()); } //设置过滤器条件 选择线是红色 0层 [CommandMethod("FilterS")] public static void FilterS() { //或者编辑框 Editor ed = Application.DocumentManager.MdiActiveDocument.Editor; //声明数组 过滤器 TypedValue[] tv = new TypedValue[3]; //过滤条件 //颜色为红色 tv.SetValue(new TypedValue((int)DxfCode.Color, 1), 0); //图形类型为直线 tv.SetValue(new TypedValue((int)DxfCode.Start, "LINE"), 1); //图层为0 tv.SetValue(new TypedValue((int)DxfCode.LayerName, "0"), 2); //选择器 SelectionFilter sf = new SelectionFilter(tv); //选择的结果 PromptSelectionResult psr = ed.SelectAll(sf); //判断是否被选中 if (psr.Status == PromptStatus.OK) { //选中的value SelectionSet ss = psr.Value; //选中的个数 Application.ShowAlertDialog("被选中的图形的个数是:" + ss.Count.ToString()); } else Application.ShowAlertDialog("没有图形被选中!!"); } //在过滤中使用关系运行 [CommandMethod("GXYS")] public static void GXYS() { //编辑框 Editor ed = Application.DocumentManager.MdiActiveDocument.Editor; //声明数组 过滤器 TypedValue[] tv = new TypedValue[3]; //过滤条件 //图形类型为圆 tv.SetValue(new TypedValue((int)DxfCode.Start, "CIRCLE"), 0); tv.SetValue(new TypedValue((int)DxfCode.Operator, ">="), 1); Point3d p = new Point3d(0, 0, 0); //圆心坐标值大于0 tv.SetValue(new TypedValue(10,p), 2); //选择器 SelectionFilter sf = new SelectionFilter(tv); //选择的结果 PromptSelectionResult psr = ed.SelectAll(sf); //判断是否被选中 if (psr.Status == PromptStatus.OK) { //选中的value SelectionSet ss = psr.Value; //选中的个数 Application.ShowAlertDialog("被选中的图形的个数是:" + ss.Count.ToString()); } else Application.ShowAlertDialog("没有图形被选中!!"); } //删除未使用的图层 [CommandMethod("SCTC")] public static void SCTC() { //获取当前文档 Document acDoc = Application.DocumentManager.MdiActiveDocument; //获取数据库 Database acDB = acDoc.Database; //事务管理器 using (Transaction acTran = acDB.TransactionManager.StartTransaction()) { //获取图层 LayerTable acLT = acTran.GetObject(acDB.LayerTableId, OpenMode.ForRead) as LayerTable; acDoc.Editor.WriteMessage(acLT.ToString()); //集合 ObjectIdCollection oic = new ObjectIdCollection(); foreach (ObjectId oi in acLT) { oic.Add(oi); acDoc.Editor.WriteMessage("\n第一次循环"+oi.ToString()); } //清除 查询那些图层没有画图 则可以成清除对象 acDB.Purge(oic); foreach (ObjectId oi in oic) { acDoc.Editor.WriteMessage("\n第二次循环" + oi.ToString()); //图层对象 SymbolTableRecord str = acTran.GetObject(oi, OpenMode.ForWrite) as SymbolTableRecord; try { //实际删除 str.Erase(); } catch(Autodesk.AutoCAD.Runtime.Exception ex) { Application.ShowAlertDialog(ex.Message); } } acTran.Commit(); } } //复制图形对象 [CommandMethod("CopyC")] public static void CopyC() { //获取当前文档 Document acDoc = Application.DocumentManager.MdiActiveDocument; //获取数据库 Database acDB = acDoc.Database; //事务管理器 using (Transaction acTran = acDB.TransactionManager.StartTransaction()) { //块表 BlockTable acbt = acTran.GetObject(acDB.BlockTableId, OpenMode.ForRead) as BlockTable; //块表记录 打开模型空间 BlockTableRecord acbtr = acTran.GetObject(acbt[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord; using (Circle c = new Circle()) { //定义园 c.Center = new Point3d(0, 0, 0); //半径 c.Radius = 10; //颜色 c.ColorIndex = 3; acbtr.AppendEntity(c); acTran.AddNewlyCreatedDBObject(c, true); //拷贝 using (Circle c2 = c.Clone() as Circle) { //定义园 c2.Center = new Point3d(0, 0, 0); //半径 c2.Radius = 5; //颜色 c2.ColorIndex = 3; acbtr.AppendEntity(c2); acTran.AddNewlyCreatedDBObject(c2, true); } } acTran.Commit(); } } //偏移多段线 [CommandMethod("PYDX")] public static void PYDX() { //获取当前文档 Document acDoc = Application.DocumentManager.MdiActiveDocument; //获取数据库 Database acDB = acDoc.Database; //事务管理器 using (Transaction acTran = acDB.TransactionManager.StartTransaction()) { //块表 BlockTable acbt = acTran.GetObject(acDB.BlockTableId, OpenMode.ForRead) as BlockTable; //块表记录 打开模型空间 BlockTableRecord acbtr = acTran.GetObject(acbt[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord; Point2d p2d1 = new Point2d(0, 0); Point2d p2d2 = new Point2d(10, 0); Point2d p2d3 = new Point2d(10, 5); Point2d p2d4 = new Point2d(20, 10); //声明多段线 Polyline acpl = new Polyline(); // 五个参数 索引 定点坐标 顶点突出值 顶点起始宽度 和 高度 acpl.AddVertexAt(0, p2d1, 0, 0, 0); acpl.AddVertexAt(1, p2d2, 0, 0, 0); acpl.AddVertexAt(2, p2d3, 0, 0, 0); acpl.AddVertexAt(3, p2d4, 0, 0, 0); acbtr.AppendEntity(acpl); acTran.AddNewlyCreatedDBObject(acpl, true); //像左边偏移 DBObjectCollection dc = acpl.GetOffsetCurves(-1); foreach (Entity oid in dc) { oid.ColorIndex = 3; acbtr.AppendEntity(oid); acTran.AddNewlyCreatedDBObject(oid, true); } acTran.Commit(); } } //旋转多段线 [CommandMethod("XZDX")] public static void XZDX() { //获取当前文档 Document acDoc = Application.DocumentManager.MdiActiveDocument; //获取数据库 Database acDB = acDoc.Database; //事务管理器 using (Transaction acTran = acDB.TransactionManager.StartTransaction()) { //块表 BlockTable acbt = acTran.GetObject(acDB.BlockTableId, OpenMode.ForRead) as BlockTable; //块表记录 打开模型空间 BlockTableRecord acbtr = acTran.GetObject(acbt[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord; Point2d p2d1 = new Point2d(0, 0); Point2d p2d2 = new Point2d(10, 0); Point2d p2d3 = new Point2d(10, 5); Point2d p2d4 = new Point2d(20, 10); //声明多段线 Polyline acpl = new Polyline(); // 五个参数 索引 定点坐标 顶点突出值 顶点起始宽度 和 高度 acpl.AddVertexAt(0, p2d1, 0, 0, 0); acpl.AddVertexAt(1, p2d2, 0, 0, 0); acpl.AddVertexAt(2, p2d3, 0, 0, 0); acpl.AddVertexAt(3, p2d4, 0, 0, 0); acbtr.AppendEntity(acpl); acTran.AddNewlyCreatedDBObject(acpl, true); Polyline acpl2 = acpl.Clone() as Polyline; acpl2.ColorIndex = 3; //获取当前文档的坐标系 定义三维矩阵 Matrix3d m3d = acDoc.Editor.CurrentUserCoordinateSystem; //定义三维坐标系 == 三维矩阵的坐标系 CoordinateSystem3d cs3 = m3d.CoordinateSystem3d; //三维矩阵 == 定义旋转方法 (旋转角度 旋转轴(Z轴) 旋转的基点) Matrix3d mat = Matrix3d.Rotation(1.57, cs3.Zaxis, new Point3d(0, 0, 0)); //实现旋转操作 acpl2.TransformBy(mat); acbtr.AppendEntity(acpl2); acTran.AddNewlyCreatedDBObject(acpl2, true); acTran.Commit(); } } } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。