Draw Polyline with Hatch : getting error 'eInvalidInput'

Draw Polyline with Hatch : getting error 'eInvalidInput'

manohar2375
Advocate Advocate
1,392 Views
2 Replies
Message 1 of 3

Draw Polyline with Hatch : getting error 'eInvalidInput'

manohar2375
Advocate
Advocate

Hello Experts,

I am trying to draw polyline with hatch, here Polyline is working as expected but hatching is NOT happening and am getting error 'eInvalidInput' at 'hatch.AppendLoop(HatchLoopTypes.Default, ids);' i tried ids.Add(pline.ObjectId); and ids.Add(pline.Id); but no luck, Could you please help me where i did mistake then will learn accordingly, below is my some of the code.

Thanks in Advance.

 

Polyline pline = new Polyline(pts.Count);
pline.Normal = normal;
foreach (Point3d pt in pts)
{
Point3d transformedPt = pt.TransformBy(ucs);
pline.AddVertexAt(pline.NumberOfVertices, plane.ParameterOf(transformedPt), 0, 0, 0);
}
var ids = new ObjectIdCollection();
ids.Add(pline.ObjectId);
// Now let's add the polyline to the modelspace
Transaction tr = db.TransactionManager.StartTransaction();
using (tr)
{
BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
BlockTableRecord btr = (BlockTableRecord)tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
ObjectId plineId = btr.AppendEntity(pline);
tr.AddNewlyCreatedDBObject(pline, true);

var hatch = new Hatch();
hatch.SetHatchPattern(HatchPatternType.PreDefined, "SOLID");
//hatch.LayerId = hatchlayerId;
btr.AppendEntity(hatch);
tr.AddNewlyCreatedDBObject(hatch, true);
hatch.Associative = true;
hatch.AppendLoop(HatchLoopTypes.Default, ids);
hatch.EvaluateHatch(true);
tr.Commit();
ed.WriteMessage("\nPolyline entity is: " + plineId.ToString());
}

 

 

 

 

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

_gile
Consultant
Consultant
Accepted solution

Hi,

 

Are you sure the polyline is closed?

 

public static void HatchPolyLine(ObjectId plineId)
{
    try
    {
        if (plineId.IsNull)
            throw new ArgumentNullException("plineId");

        if (plineId.ObjectClass != RXObject.GetClass(typeof(Polyline)))
            throw new Autodesk.AutoCAD.Runtime.Exception(ErrorStatus.IllegalEntityType);

        var ids = new ObjectIdCollection();
        ids.Add(plineId);

        using (var tr = plineId.Database.TransactionManager.StartTransaction())
        {
            var pline = (Polyline)tr.GetObject(plineId, OpenMode.ForRead);
            if (!(pline.Closed || pline.GetPoint2dAt(0).IsEqualTo(pline.GetPoint2dAt(pline.NumberOfVertices - 1))))
                throw new InvalidOperationException("Opened polyline.");

            var owner = (BlockTableRecord)tr.GetObject(pline.OwnerId, OpenMode.ForWrite);
            var hatch = new Hatch();
            hatch.SetHatchPattern(HatchPatternType.PreDefined, "SOLID");
            owner.AppendEntity(hatch);
            tr.AddNewlyCreatedDBObject(hatch, true);
            hatch.Associative = true;
            hatch.AppendLoop(HatchLoopTypes.Default, ids);
            hatch.EvaluateHatch(true);
            tr.Commit();
        }
    }
    catch(System.Exception ex)
    {
        var ed = Application.DocumentManager.MdiActiveDocument.Editor;
        ed.WriteMessage($"{ex.Message}\n{ex.StackTrace}");
    }
}


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 3 of 3

manohar2375
Advocate
Advocate

Thanks for your valuable support.

Yes, i missed to close polyline, after closing polyline able hatch and working fine.

 

Again, thank you so much.

 

 

0 Likes