Hatch does not work

Hatch does not work

wesbird
Collaborator Collaborator
542 Views
5 Replies
Message 1 of 6

Hatch does not work

wesbird
Collaborator
Collaborator

hi: 

 

I have code to hatch 100+ LWPOLYLINE, for some reason, some of them not working. I check the code, I got error message of 

eInvalidInput

at line of 

oHatch.AppendLoop(HatchLoopTypes.Default, ObjIds);

 

I already tried HatchLoopTypes.External, same result. 

 

for testing, I create a function: pick the pline, then call the function, got the same error.

if I manually pick the same pline, use AutoCAD HATCH command, it works.

 

what's could be wrong? 

 

I really appreciate your help.

 

 

 

 

 

 

 

private static Entity DrawHatch(Entity ent, Color color)
        {
            string HatchPattern = "SOLID";
            Transparency HatchTransparency = new Transparency(255);
            ObjectIdCollection ObjIds = new ObjectIdCollection();
            ObjIds.Add(ent.ObjectId);

            try
            {
                ent.UpgradeOpen();

                Hatch oHatch = new Hatch();

                oHatch.SetDatabaseDefaults();

                oHatch.SetHatchPattern(HatchPatternType.PreDefined, HatchPattern);

                // color
                oHatch.Color = color;

                // transparency 
                oHatch.Transparency = HatchTransparency;

                // Outermost
                oHatch.AppendLoop(HatchLoopTypes.Default, ObjIds);

                // evaluate hatch 
                oHatch.EvaluateHatch(true);

                return oHatch;
            }
            catch (Exception caught)
            {
                BPLog.Fatal($"ent={ent.Handle}", caught);
                return null;
            }
        }
Windows 10 64 bit, AutoCAD (ACA, Map) 2023
0 Likes
543 Views
5 Replies
Replies (5)
Message 2 of 6

hng_kaijianG7MAN
Community Visitor
Community Visitor

@wesbird 

 

Hi there, you need to append to block table record before editing the properties of the hatch.

 

Please refer: Solved: AutoCAD .NET - Creating hatch from polyline - Autodesk Community - AutoCAD

 

 

0 Likes
Message 3 of 6

wesbird
Collaborator
Collaborator

Thank you, @hng_kaijianG7MAN,

 

Sorry, my code is not complete. I do have code for  appending to block table record

also, I tested with this code from gile.  still the same error. 

I just don't understand, why the same code work for some pline but not the other, why the same pline code not working but HATCH command works. 

and the test pline with problem does not have issue like open, self intersect. 

here code from gile

 

 

[CommandMethod("JGC.UI.Test", nameof(JGCTest_Hatch2), CommandFlags.Modal)]
        public static void JGCTest_Hatch2()
        {
            var oid = JTISelect.GetEntityX<Polyline>("\nPick a polyline:", "");
            if (oid.IsNull)
            {
                return;
            }

            HatchPolyLine(oid);
        }

        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.ForWrite);
                    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}");
            }
        }

 

 

 

 

Windows 10 64 bit, AutoCAD (ACA, Map) 2023
0 Likes
Message 4 of 6

_gile
Consultant
Consultant

Hi,

Is the selected pline correctly closed?



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 5 of 6

wesbird
Collaborator
Collaborator

Thank you, gile, 

 

Yes, it's closed. and HATCH command works just fine. 

Windows 10 64 bit, AutoCAD (ACA, Map) 2023
0 Likes
Message 6 of 6

shubhamraut221195
Enthusiast
Enthusiast

Most probably your entity is not closed entity. if you draw circle and fill it with hatch it won't show such error but if you do the same with line, it will cause eInvalidInput error at AppendLoop()

0 Likes