主要步骤:
一、获取一个块的id:
其中oId就是了。
二、通过次oId获取块引用blkRef:
三、通过它获取所有相同的块引用的id集合:
四、通过步骤三的集合得到所有的块引用得到集合listBr:
五、遍历listBr,创建DBText编号 ,确定DBText在图中插入的位置,加入到集合listDBText:
在做第五步骤前,先要获取TextStyleId:
最后通过扩展方法加入到模型空间:listDBText.ToSpace();
ToSpace方法截图:
需要注意的地方:一定要是块定义,在使用dbText.AlignmentPoint之前一定要写排序方式这里用的是:dbText.HorizontalMode = TextHorizontalMode.TextMid;
完整代码:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using Autodesk.AutoCAD.ApplicationServices; using Autodesk.AutoCAD.EditorInput; using Autodesk.AutoCAD.DatabaseServices; using Autodesk.AutoCAD.Runtime; using Autodesk.AutoCAD.Geometry; namespace MulitySortNum { public class SortNum { private Document doc = Application.DocumentManager.MdiActiveDocument; private Database db = Application.DocumentManager.MdiActiveDocument.Database; private Editor ed = Application.DocumentManager.MdiActiveDocument.Editor; private string str = "LL"; public int index = 1; [CommandMethod("NumSort1")] public void NumSort1() { index = 1; var propEnt = new PromptEntityOptions("请选择要编号的一个块\n"); var propRes = ed.GetEntity(propEnt); if (propRes.Status != PromptStatus.OK) { return; } var oId = propRes.ObjectId; ObjectIdCollection objIds = null; List<DBText> listDBText = new List<DBText>(); using (Transaction trans = db.TransactionManager.StartTransaction()) { var blkRef = trans.GetObject(oId, OpenMode.ForRead) as BlockReference; if (blkRef == null) { Application.ShowAlertDialog("请选择块定义"); return; } var recId = blkRef.BlockTableRecord; var blkTblRec = trans.GetObject(recId, OpenMode.ForRead) as BlockTableRecord; objIds = blkTblRec.GetBlockReferenceIds(true, false); var txtStlTbl = trans.GetObject(db.TextStyleTableId, OpenMode.ForRead) as TextStyleTable; var txtstyleId = txtStlTbl["Standard"]; List<BlockReference> listBr = new List<BlockReference>(); foreach (ObjectId objectId in objIds) { var blkTempRef = trans.GetObject(objectId, OpenMode.ForRead) as BlockReference; listBr.Add(blkTempRef); } listBr.OrderByDescending(b => b.Position.Y).ToList().ForEach(blkTempRef => { DBText dbText = new DBText(); dbText.TextString = str + "_" + index++; dbText.TextStyleId = txtstyleId; var pointMin = blkTempRef.Bounds.Value.MinPoint; var pointMax = blkTempRef.Bounds.Value.MaxPoint; dbText.HorizontalMode = TextHorizontalMode.TextMid; dbText.AlignmentPoint = pointMin + Vector3d.YAxis * 2 + Vector3d.XAxis * Math.Abs(pointMax.X - pointMin.X) / 2; listDBText.Add(dbText); }); trans.Commit(); } listDBText.ToSpace(); } } }
原文地址:https://www.cnblogs.com/HelloQLQ/p/11823870.html
时间: 2024-10-12 01:25:15