/// <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 void SetRectangleWidth(this Database db, ObjectId id, double newWidth) { if (db.IsRectangle(id)) { RectangleData data = db.GetRectangleData(id); int baseIndex = data.BasePointIndex; Point3d basePoint = data.BasePoint; bool clockWise = data.IsClockWise; double width = data.Width; double height = data.Height; 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; btr.UpgradeOpen(); pline.SetPointAt(baseIndex, new Point2d(basePoint.X - newWidth / 2, basePoint.Y)); if (clockWise) { pline.SetPointAt((baseIndex + 1) % 4, new Point2d(basePoint.X - newWidth / 2, basePoint.Y + height)); pline.SetPointAt((baseIndex + 2) % 4, new Point2d(basePoint.X + width + newWidth / 2, basePoint.Y + height)); pline.SetPointAt((baseIndex + 3) % 4, new Point2d(basePoint.X + width + newWidth / 2, basePoint.Y)); } else { pline.SetPointAt((baseIndex + 1) % 4, new Point2d(basePoint.X + width + newWidth / 2, basePoint.Y)); pline.SetPointAt((baseIndex + 2) % 4, new Point2d(basePoint.X + width + newWidth / 2, basePoint.Y + height)); pline.SetPointAt((baseIndex + 3) % 4, new Point2d(basePoint.X - newWidth / 2, basePoint.Y + height)); } btr.DowngradeOpen(); } trans.Commit(); } } }