c#, Autocad, Displaying the properties of hatch

Viewed 42

I am writing an AutoCAD plugin using c# and need to display the property of hatch in the middle of the hatched object (e.g., the center of a circle). I have two problems in code:

  1. How can I access center of circle?

  2. How can I get property of hatch?

I am getting error on this line in autocad.

acText.TextString = hatch.Area.ToString(); // Area of hatch

Below is the code mainly taken from Stackflow.

 [CommandMethod("DisplyArea")]
        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);
            int vr = 1;
            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);
                        DBText acText = new DBText();

// It needs to be CIRCLE.CENTER, but how can I access that??????

                        acText.Position = new Point3d(2,2,0);     
         
                        acText.TextString = hatch.Area.ToString(); // Area of hatch
                        acText.Height = 0.5;
                        curSpace.AppendEntity(acText);
                        tr.AddNewlyCreatedDBObject(acText, true);
                    }
                }
                tr.Commit();
            }
        }

Any solution?

3 Answers

You can easy get center of the circle by cast Id to entity like this:

Circle c = tr.GetObject(id, OpenMode.ForRead) as Circle;
acText.Position = c.Center;

About problem with hatch area I think first You need to define hatch

using (var hatch = new Hatch())
{
     
     hatch.SetHatchPattern(HatchPatternType.PreDefined, "ANSI31");
     hatch.Associative = true;
     hatch.AppendLoop(HatchLoopTypes.External, ids);
     hatch.EvaluateHatch(true);

Then add it to space;

curSpace.AppendEntity(hatch);
tr.AddNewlyCreatedDBObject(hatch, true);

Finally read area

acText.TextString = hatch.Area.ToString()

You first add hatch to space, while it has not geometry yet.

  1. How can I access center of circle?
    Answer:
    Circle oCircle = tr.GetObject(id, OpenMode.ForRead) as Circle; var _centerPosition = oCircle.Center;

  2. How can I get property of hatch?
    Answer:
    you need to change
    from hatch.AppendLoop(HatchLoopTypes.Outermost, ids);
    to hatch.AppendLoop(HatchLoopTypes.External, ids);

I changed your code and it works for me

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);
        int vr = 1;
        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())
            {
                try
                {
                    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.External, ids);
                        hatch.EvaluateHatch(true);
                        hatch.HatchStyle = HatchStyle.Normal;

                        Circle oCircle = tr.GetObject(id, OpenMode.ForRead) as Circle;
                        var _centerPosition = oCircle.Center;

                        DBText acText = new DBText();
                        acText.Position = _centerPosition;

                        acText.TextString = hatch.Area.ToString(); // Area of hatch
                        acText.Height = 0.5;
                        curSpace.AppendEntity(acText);
                        tr.AddNewlyCreatedDBObject(acText, true);
                    }
                }
                catch { }
            }
            tr.Commit();
        }
    }
Related