AutoCAD .NET - Creating hatch from polyline

AutoCAD .NET - Creating hatch from polyline

Anonymous
Not applicable
4,990 Views
2 Replies
Message 1 of 3

AutoCAD .NET - Creating hatch from polyline

Anonymous
Not applicable

I've been working on an .NET project. I was tasked with creating a method to draw a hatch from a custom polyline. Below I've attempted to create a function that will take a few points from the user and then connect the last dot to the first, enclosing the polyline.

However, at the line of acHatch.AppendLoop, I receive an exception that says:

    Autodesk.AutoCAD.Runtime.Exception: 'eInvalidInput'

I've been trying to figure why this might not work. Using the same code to draw an enclosed polyline and then using the hatch command manually seems to work, for example.

    public static void DrawArea()
        {
            Document acDoc = Application.DocumentManager.MdiActiveDocument;
            Database acCurDb = acDoc.Database;

            PromptPointResult ptRes;
            Point2dCollection colPt = new Point2dCollection();
            PromptPointOptions ptOpts = new PromptPointOptions("");
            ptOpts.Keywords.Add("Stop");

            using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
            {
                BlockTable acBt;
                acBt = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead) as BlockTable;

                BlockTableRecord acBtr;
                acBtr = acTrans.GetObject(acBt[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;

                ptOpts.Message = "\nSpecify point: ";
                ptRes = acDoc.Editor.GetPoint(ptOpts);
                colPt.Add(new Point2d(ptRes.Value.X, ptRes.Value.Y));

                int cnt = 0;

                while (cnt < 3)
                {
                    ptOpts.UseBasePoint = true;
                    ptOpts.BasePoint = ptRes.Value;

                    ptRes = acDoc.Editor.GetPoint(ptOpts);
                    colPt.Add(new Point2d(ptRes.Value.X, ptRes.Value.Y));

                    if (ptRes.StringResult == "Stop") break;
                    cnt++;
                }

                using (Polyline acPoly = new Polyline())
                {

                    int i = 0;
                    foreach (Point2d point in colPt)
                    {
                        acPoly.AddVertexAt(i, point, 0, 0, 0);
                    }

                    int last = colPt.Count;

                    acPoly.AddVertexAt(i, colPt[0], 0, 0, 0);

                    ObjectId oid = new ObjectId();
                    oid = acBtr.AppendEntity(acPoly);
                    //acTrans.AddNewlyCreatedDBObject(acPoly, true);

                    acPoly.Closed = true;

                    ObjectIdCollection oidCol = new ObjectIdCollection();
                    oidCol.Add(oid);

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

                        acHatch.SetHatchPattern(HatchPatternType.PreDefined, "ANSI31");
                        acHatch.Associative = true;
                        acHatch.AppendLoop(HatchLoopTypes.Polyline, oidCol);
                        acHatch.EvaluateHatch(true);

                        using (DBText acLabel = new DBText())
                        {
                            acLabel.TextString = acHatch.Area.ToString();
                            acLabel.Position = new Point3d(colPt[3].X + 15, colPt[3].Y, 0);
                            acLabel.Height = 25;

                            acBtr.AppendEntity(acLabel);
                            acTrans.AddNewlyCreatedDBObject(acLabel, true);
                        }
                    }
                }

                acTrans.Commit();
            }
        }

0 Likes
Accepted solutions (1)
4,991 Views
2 Replies
Replies (2)
Message 2 of 3

Paulio
Advocate
Advocate
Accepted solution

Have you tried:

 acHatch.AppendLoop(HatchLoopTypes.External, oidCol);

Message 3 of 3

Anonymous
Not applicable

Well, it works. Thanks a lot.

0 Likes