Hatch to Drawn Polyline : e.Message = "eInvalidInput"

Hatch to Drawn Polyline : e.Message = "eInvalidInput"

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

Hatch to Drawn Polyline : e.Message = "eInvalidInput"

manohar2375
Advocate
Advocate

Hi All, 

Kindly suggest on it.

 

I have drawn a Polyline using below statement:
Acad.Application.DocumentManager.MdiActiveDocument.SendStringToExecute("PL ", true, false, true);

Got polyline object as below:
ObjectId plobj = Autodesk.AutoCAD.Internal.Utils.EntLast();

Sending to it other method as below:

CommandWrapper.HatchPolyLine(plobj);

 

Here i am trying to hatch to drawn above polyline using below code but i am getting error message 'e.Message = "eInvalidInput"' at 'hatch.AppendLoop(HatchLoopTypes.Default, objectIdColletion);'


Kindly suggest where i did mistake in whole code.

 

public static void HatchPolyLine(ObjectId plobj){
try{
if (plobj != null){
Polyline pline = null;
Document document = Application.DocumentManager.MdiActiveDocument;
using (Transaction transaction = document.Database.TransactionManager.StartTransaction()){
pline = (Polyline)transaction.GetObject(plobj, OpenMode.ForWrite);
using (BlockTable blockTable = CADDBUtil.GetBlockTable(transaction))
using (BlockTableRecord modelSpace = CADDBUtil.GetModelSpace(transaction, blockTable))
using (Hatch hatch = new Hatch())
{
modelSpace.UpgradeOpen();

ObjectIdCollection objectIdColletion = new ObjectIdCollection();
objectIdColletion.Add(pline.Id);

hatch.SetHatchPattern(HatchPatternType.PreDefined, "SOLID");
modelSpace.AppendEntity(hatch);
transaction.AddNewlyCreatedDBObject(hatch, true);
hatch.AppendLoop(HatchLoopTypes.Default, objectIdColletion);
hatch.EvaluateHatch(true);

pline.Color = hatch.Color;
}
transaction.Commit();
}
}
}
catch (Exception e)
{
throw new Exception(e.Message);
}
}

0 Likes
Accepted solutions (1)
1,671 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

Thank you so much for your valuable support, its working fine.

 

0 Likes