在Autocad中对选定圆的阴影线列表

Waseememe

我目前正在从事Autocad插件开发,我想选择圈子,然后使用C#将选中的圈子变成阴影线。我是否应该列出包含所有选定圆圈的阴影线列表?我已经尝试过了,但是在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);
                }





            }
        }
    }
gileCAD

您可以使用选区(带有选区过滤器)来获取圆,然后迭代选定的圆以创建剖面线。

[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();
    }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

TOP 榜单

热门标签

归档