using System; using System.Text; using Autodesk.AutoCAD.ApplicationServices; using Autodesk.AutoCAD.DatabaseServices; using Autodesk.AutoCAD.EditorInput; using Autodesk.AutoCAD.Geometry; using Autodesk.AutoCAD.Runtime; namespace TextAddCircle { public class Class1 { [CommandMethod("TextAddCircle")] public void TextAddCircle() { Database db = HostApplicationServices.WorkingDatabase; Editor ed = Application.DocumentManager.MdiActiveDocument.Editor; using (Transaction tr = db.TransactionManager.StartTransaction()) { ed.WriteMessage("/n作者: 小萝卜头 QQ:95818608 2010年04月19日"); ed.WriteMessage("/n给包含某个字符串的文本加圆"); // GetString PromptResult pStrRes = ed.GetString("/n输入字符串<任意>: "); // if (pStrRes.Status == PromptStatus.OK) { // SelectionFilter(tv) TypedValue数组 TypedValue[] tv = new TypedValue[2] { new TypedValue(0, "TEXT"), new TypedValue(1, "*" + pStrRes.StringResult + "*") }; PromptSelectionResult pSelRes = ed.GetSelection(new SelectionFilter(tv)); // if (pSelRes.Status == PromptStatus.OK) { // 取得选择集 SelectionSet ss = pSelRes.Value; // 读块表、写当前空间的块记录表 BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead); BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite); // 遍历选择集 foreach (SelectedObject obj in ss) { // GetObject DBText ent = tr.GetObject(obj.ObjectId, OpenMode.ForRead) as DBText; // GeometricExtents Point3d pt1 = ent.GeometricExtents.MinPoint; Point3d pt2 = ent.GeometricExtents.MaxPoint; Point3d center = new Point3d((0.5 * (pt1.X + pt2.X)), (0.5 * (pt1.Y + pt2.Y)), (0.5 * (pt1.Z + pt2.Z))); double radius = 0.55 * pt1.DistanceTo(pt2); // 生成圆 Circle circle = new Circle(center, Vector3d.ZAxis, radius); btr.AppendEntity(circle); tr.AddNewlyCreatedDBObject(circle, true); // 亮显 ent.Highlight(); } // 提交 tr.Commit(); } } } } } }