/// <summary> /// 获取矩形属性 /// </summary> public static RectangleData GetRectangleData(this Database db, ObjectId id) { RectangleData data = new RectangleData(); if (db.IsRectangle(id)) { PLineData plinedata = db.GetPLineData(id); for (int i = 0; i < 4; i++) { Vector3d vec1 = plinedata.Vectors[i]; Vector3d vec2 = plinedata.Vectors[(i + 1) % 4]; if (vec1 == new Vector3d(1, 0, 0) && vec2 == new Vector3d(0, 1, 0)) { data.IsClockWise = false; data.BasePointIndex = i; data.BasePoint = plinedata.VertexPoints[i]; } else if (vec1 == new Vector3d(0, 1, 0) && vec2 == new Vector3d(1, 0, 0)) { data.IsClockWise = true; data.BasePointIndex = i; data.BasePoint = plinedata.VertexPoints[i]; } } Point3d p0 = plinedata.VertexPoints[0]; Point3d p2 = plinedata.VertexPoints[2]; data.Width = Math.Abs(p2.X - p0.X); data.Height = Math.Abs(p2.Y - p0.Y); } return(data); }
/// <summary> /// 判断多段线是否为矩形 /// </summary> public static bool IsRectangle(this Database db, ObjectId id) { bool flag = false; using (Transaction trans = db.TransactionManager.StartTransaction()) { // 获取图形对象 Entity ent = trans.GetObject(id, OpenMode.ForWrite) as Entity; if (ent.GetType() == typeof(Polyline)) { db.PLinePurge(id); PLineData plinedata = db.GetPLineData(id); if (plinedata.VertexCount == 4) { if (plinedata.Vectors[0].DotProduct(plinedata.Vectors[1]).ToString() == "0" && plinedata.Vectors[1].DotProduct(plinedata.Vectors[2]).ToString() == "0" && plinedata.Vectors[2].DotProduct(plinedata.Vectors[3]).ToString() == "0") { flag = true; } } } trans.Commit(); } return(flag); }
/// <summary> /// 获取多段线属性 /// </summary> public static PLineData GetPLineData(this Database db, ObjectId Id) { PLineData data = new PLineData(); using (Transaction trans = db.TransactionManager.StartTransaction()) { Entity ent = trans.GetObject(Id, OpenMode.ForRead) as Entity; data.PLineId = Id; try { Polyline pline = ent as Polyline; data.StartPoint = pline.StartPoint; data.EndPoint = pline.EndPoint; data.Length = pline.Length; data.VertexCount = pline.NumberOfVertices; data.IsClosed = pline.Closed; data.VertexPoints = new Point3d[data.VertexCount]; data.Vectors = new Vector3d[data.VertexCount]; for (int i = 0; i < data.VertexCount; i++) { data.VertexPoints[i] = pline.GetPoint3dAt(i); data.Vectors[i] = pline.GetFirstDerivative(pline.GetPoint3dAt(i)).GetNormal(); } } catch (Autodesk.AutoCAD.Runtime.Exception e) { throw e; } trans.Commit(); } return(data); }
/// <summary> /// 删除多段线中的多余点并将应闭合的多段线闭合 /// </summary> public static void PLinePurge(this Database db, ObjectId id) { using (Transaction trans = db.TransactionManager.StartTransaction()) { // 打开块表 BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead); // 打开块表记录 BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForRead); // 获取图形对象 Entity ent = trans.GetObject(id, OpenMode.ForWrite) as Entity; if (ent.GetType() == typeof(Polyline)) { Polyline pline = ent as Polyline; PLineData plinedata = db.GetPLineData(id); /// 删除多段线中的多余点 if (plinedata.VertexCount > 2) { // 为避免下标出现问题, 从下标最大处开始遍历 for (int i = plinedata.VertexCount - 2; i > 0; i--) { if (plinedata.Vectors[i - 1] == plinedata.Vectors[i]) { btr.UpgradeOpen(); pline.RemoveVertexAt(i); btr.DowngradeOpen(); } } } // 重新读取 plinedata = db.GetPLineData(id); if (plinedata.VertexPoints[0] == plinedata.VertexPoints[plinedata.VertexCount - 1]) { btr.UpgradeOpen(); pline.Closed = true; pline.RemoveVertexAt(plinedata.VertexCount - 1); btr.DowngradeOpen(); } } trans.Commit(); } }