Search code examples
c#autocadautocad-plugin

Hatching list of selected circles in Autocad


I'm currently working with Autocad plugin development and I want to select circles and the selected circles turn into hatches using C#. Should I make a list of hatches that contains all the selected circles or what? I've tried this but I'm getting runtime exception at AppendLoop() Here's the code:

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





            }
        }
    }

Solution

  • You can use a selection (with a selection filter) to get the circles, and then, iterate through the selected circles to create hatches.

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