Autocad 中选定圆的孵化列表

Hatching list of selected circles in Autocad

我目前正在使用 Autocad 插件开发,我想使用 C# select 圆圈和 selected 圆圈变成舱口。我应该制作一个包含所有 selected 圆圈的舱口列表还是什么? 我已经尝试过了,但是我在 AppendLoop() 处遇到了运行时异常 这是代码:

public void SelectCirclesToHatch(int n)
    {
        var doc = Application.DocumentManager.MdiActiveDocument;
        if (doc == null)

            return;

        var acCurDb = doc.Database;
        var edt = doc.Editor;
        using (DocumentLock docLock = doc.LockDocument())
        {
            using (var acTrans = doc.TransactionManager.StartTransaction())
            {
                var acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead) as BlockTable;
                var acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;

                ObjectIdCollection acObjIdColl = new ObjectIdCollection();

                // list of circles
                List<Circle> circles = new List<Circle>();



                var peo2 = new PromptEntityOptions("\nSelect the Circle");
                peo2.SetRejectMessage("\nMust be a Circle.");
                peo2.AddAllowedClass(typeof(Circle), false);
                // select circles
                for (int i = 0; i < n; i++)
                {
                    var per2 = edt.GetEntity(peo2);
                    if (per2.Status != PromptStatus.OK)
                        return;

                    var cId = per2.ObjectId;

                    var br = acTrans.GetObject(cId, OpenMode.ForRead) as Circle;
                    if (br != null)
                    {
                        circles.Add(br);
                    }

                    // Adds the circle to an object id array 
                    acObjIdColl.Add(br.ObjectId);


                }
                using (Hatch acHatch = new Hatch())
                {
                    acBlkTblRec.AppendEntity(acHatch);
                    acTrans.AddNewlyCreatedDBObject(acHatch, true);

                    // Set the properties of the hatch object
                    // Associative must be set after the hatch object is appended to the 
                    // block table record and before AppendLoop
                    acHatch.SetHatchPattern(HatchPatternType.PreDefined, "ANSI31");
                    acHatch.Associative = true;
                    acHatch.AppendLoop(HatchLoopTypes.Outermost, acObjIdColl);
                    acHatch.EvaluateHatch(true);
                }





            }
        }
    }

您可以使用选择(带有选择过滤器)来获取圆,然后循环访问选定的圆以创建影线。

[CommandMethod("TEST")]
public static void SelectCirclesToHatch()
{
    var doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
    var db = doc.Database;
    var ed = doc.Editor;
    var filter = new SelectionFilter(new[] { new TypedValue(0, "CIRCLE") });
    var selection = ed.GetSelection(filter);
    if (selection.Status != PromptStatus.OK)
        return;
    using (var tr = db.TransactionManager.StartTransaction())
    {
        var curSpace = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
        foreach (var id in selection.Value.GetObjectIds())
        {
            var ids = new ObjectIdCollection(new[] { id });
            using (var hatch = new Hatch())
            {
                curSpace.AppendEntity(hatch);
                tr.AddNewlyCreatedDBObject(hatch, true);
                hatch.SetHatchPattern(HatchPatternType.PreDefined, "ANSI31");
                hatch.Associative = true;
                hatch.AppendLoop(HatchLoopTypes.Outermost, ids);
                hatch.EvaluateHatch(true);
            }
        }
        tr.Commit();
    }
}