2

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?

Shahgee
  • 3,303
  • 8
  • 49
  • 81

4 Answers4

2

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;
CAD Developer
  • 1,532
  • 2
  • 21
  • 27
2

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.

CAD Developer
  • 1,532
  • 2
  • 21
  • 27
2
  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();
        }
    }
BouRHooD
  • 200
  • 1
  • 14
0
    [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);

        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())
            {
                Entity acEnt = tr.GetObject(id, OpenMode.ForRead) as Entity;

                Circle acCir = acEnt as Circle;
                Point3d cntrPnt = acCir.Center;

                // Open the Block table for read
                BlockTable acBlkTbl = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;

                // Open the Block table record Model space for write
                BlockTableRecord acBlkTblRec = tr.GetObject(acBlkTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;

                // Create the hatch object and append it to the block table record
                using (Hatch acHatch = new Hatch())
                {
                    acBlkTblRec.AppendEntity(acHatch);
                    tr.AddNewlyCreatedDBObject(acHatch, true);

                    ObjectIdCollection ids = new ObjectIdCollection();
                    ids.Add(id);
                    // 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, ids);

                    acHatch.EvaluateHatch(true);
                    acHatch.HatchStyle = HatchStyle.Outer;
                    ObjectId hatchId = acHatch.ObjectId;
                }
            }
        }
    }