CAD二次开发C#代码,给定容差删除多段线、二维多段线、三维多段线上的重复点,CAD2010,VS2012
using System;using System.Collections;using System.Collections.Generic;using System.Linq;using System.Text;using Autodesk.AutoCAD.Runtime;using Autodesk.AutoCAD.EditorInput;using Autodesk.AutoCAD.ApplicationServices;using Autodesk.AutoCAD.DatabaseServices;using Autodesk.AutoCAD.Geometry;[assembly:CommandClass(typeof(CheckDwgData.BasicCheck))]namespace CheckDwgData{ public class BasicCheck { /// <summary> /// 删除重复点 /// </summary> /// <param name="acadDoc">当前文档</param> /// <param name="cur">曲线</param> /// <param name="dblDis">容差</param> public void DeleteDuplicatePoints(Document acadDoc, Curve cur, Double dblDis) { #region Editor ed = acadDoc.Editor; Database db=acadDoc.Database; Point2d pt2d1 = new Point2d(); Point2d pt2d2 = new Point2d(); Point3d pt3d1 = new Point3d(); Point3d pt3d2 = new Point3d(); Tolerance tol = new Tolerance(dblDis, dblDis); int intIndex = 0; switch (cur.GetType().ToString()) { case "Autodesk.AutoCAD.DatabaseServices.Polyline": using (Transaction tr = db.TransactionManager.StartTransaction()) { Polyline poly = cur as Polyline; while (intIndex < poly.NumberOfVertices) { if (intIndex != poly.NumberOfVertices-1) { pt2d1 = poly.GetPoint2dAt(intIndex); pt2d2 = poly.GetPoint2dAt(intIndex 1); } else if (poly.Closed == true) { pt2d1 = poly.GetPoint2dAt(intIndex); pt2d2 = poly.GetPoint2dAt(0); } if (pt2d1.IsEqualTo(pt2d2,tol)) { poly = (Polyline)tr.GetObject(poly.ObjectId, OpenMode.ForWrite); poly.RemoveVertexAt(intIndex); intIndex--; } intIndex ; } tr.Commit(); } break; case "Autodesk.AutoCAD.DatabaseServices.Polyline2d": using (Transaction tr = db.TransactionManager.StartTransaction()) { Polyline2d poly2d = cur as Polyline2d; IEnumerator it= poly2d.GetEnumerator(); it.MoveNext(); Vertex2d ver2dFir = (Vertex2d)tr.GetObject((ObjectId)it.Current, OpenMode.ForWrite); Vertex2d ver2dTmp = ver2dFir; while(it.MoveNext()) { Vertex2d ver2d = (Vertex2d)tr.GetObject((ObjectId)it.Current, OpenMode.ForWrite); pt3d1 = ver2dTmp.Position; pt3d2 = ver2d.Position; if (pt3d1.IsEqualTo(pt3d2, tol)) { ver2dTmp.Erase(); } ver2dTmp = ver2d; } if (poly2d.Closed == true) {pt3d1 = ver2dTmp.Position;pt3d2 = ver2dFir.Position;if (pt3d1.IsEqualTo(pt3d2, tol)){ ver2dTmp.Erase();} } tr.Commit(); } break; case "Autodesk.AutoCAD.DatabaseServices.Polyline3d": using (Transaction tr = db.TransactionManager.StartTransaction()) { Polyline3d poly3d = cur as Polyline3d; IEnumerator it= poly3d.GetEnumerator(); it.MoveNext(); PolylineVertex3d ver3dFir = tr.GetObject((ObjectId)it.Current, OpenMode.ForWrite) as PolylineVertex3d; PolylineVertex3d ver3dTmp = ver3dFir; while(it.MoveNext()) { PolylineVertex3d ver3d =tr.GetObject((ObjectId)it.Current, OpenMode.ForWrite) as PolylineVertex3d; pt3d1 = ver3dTmp.Position; pt3d2 = ver3d.Position; if (pt3d1.IsEqualTo(pt3d2, tol)) { ver3dTmp.Erase(); } ver3dTmp = ver3d; } if (poly3d.Closed == true) {pt3d1 = ver3dTmp.Position;pt3d2 = ver3dFir.Position;if (pt3d1.IsEqualTo(pt3d2, tol)){ ver3dTmp.Erase();} } tr.Commit(); } break; } #endregion } [CommandMethod("Test")] public void Test() { Document acadDoc = Application.DocumentManager.MdiActiveDocument; Editor ed = acadDoc.Editor; Entity ent = Entity("选择实体"); ed.WriteMessage(ent.GetType().ToString()); DeleteDuplicatePoints(acadDoc, (Curve)ent, 0.5); } /// <summary> /// 选择单个实体 /// </summary> /// <param name="strMsg"></param> /// <returns></returns> public static Entity Entity(string strMsg) { Document doc = Application.DocumentManager.MdiActiveDocument; Editor ed = doc.Editor; Database db = doc.Database; Entity entity = null; PromptEntityResult ent = ed.GetEntity(strMsg); if (ent.Status == PromptStatus.OK) { using (Transaction tr = db.TransactionManager.StartTransaction()) { entity = (Entity)tr.GetObject(ent.ObjectId, OpenMode.ForWrite, true); tr.Commit(); } } return entity; } }}
评论