My Polyline Hatch code has terrible error and does not work.

My Polyline Hatch code has terrible error and does not work.

MarkJamesRogolino
Advocate Advocate
1,468 Views
7 Replies
Message 1 of 8

My Polyline Hatch code has terrible error and does not work.

MarkJamesRogolino
Advocate
Advocate

Hello all the thankful persons.

Now I am making polyline hatch application but it does not work and error.

I am stunned because of this problem.

Followings are my error image and code.

I hope someone can help me. Thanks in advance. 

errorimage.jpg

public static void Hatch()
        {
            var documentManager = Application.DocumentManager;
            var currentDocument = documentManager.MdiActiveDocument;
            var db = currentDocument.Database;
            var ed = currentDocument.Editor;
            using (var tr = db.TransactionManager.StartOpenCloseTransaction())
            {
                var blockTable = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
                foreach (ObjectId btrId in blockTable)
                {
                    var btr = (BlockTableRecord)tr.GetObject(btrId, OpenMode.ForWrite);
                    var PlineCls = RXObject.GetClass(typeof(Polyline));
                    if (btr.IsLayout)
                    {
                        
                        foreach (ObjectId id in btr)
                        {
                            if (id.ObjectClass == PlineCls)
                            {
                                var pline = (Polyline)tr.GetObject(id, OpenMode.ForRead);
                                string curlay = pline.Layer;
                                if (IsreqHatch(curlay) == true)
                                {
                                    ObjectIdCollection acObjIdColl = new ObjectIdCollection();
                                    acObjIdColl.Add(pline.ObjectId);
                                    Hatch acHatch = new Hatch();
                                    btr.AppendEntity(acHatch);
                                    tr.AddNewlyCreatedDBObject(acHatch, true);
                                    acHatch.SetDatabaseDefaults();
                                    acHatch.SetHatchPattern(HatchPatternType.PreDefined, "ANSI31");
                                    acHatch.Associative = true;
                                    acHatch.HatchStyle = HatchStyle.Ignore;

                                    acHatch.AppendLoop(HatchLoopTypes.Outermost, acObjIdColl);
                                    acHatch.EvaluateHatch(true);
                                }
                            }
                        }                        
                    }
                }
                tr.Commit();
            }
        }
0 Likes
Accepted solutions (1)
1,469 Views
7 Replies
Replies (7)
Message 2 of 8

_gile
Consultant
Consultant

Hi,

You have to set the hatch pattern before adding it to the Database.

One more time, build a little method to hatch a single polyline so that you can test-it and debug it by selection a single polyline with a simple test command instead of running your method which iterates through each entity of each layout.

        private static void HacthPolyline(Polyline pline, BlockTableRecord btr, Transaction tr)
        {
            var ids = new ObjectIdCollection();
            ids.Add(pline.Id);
            var hatch = new Hatch { Layer = pline.Layer };
            hatch.SetHatchPattern(HatchPatternType.PreDefined, "ANSI31");
            btr.AppendEntity(hatch);
            tr.AddNewlyCreatedDBObject(hatch, true);
            hatch.Associative = true;
            hatch.AppendLoop(HatchLoopTypes.Default, ids);
            hatch.EvaluateHatch(true);
        }

 

And, when you're satified with this method, you can integrate it into your main method:

        public static void Hatch()
        {
            var documentManager = Application.DocumentManager;
            var currentDocument = documentManager.MdiActiveDocument;
            var db = currentDocument.Database;
            using (var tr = db.TransactionManager.StartOpenCloseTransaction())
            {
                var blockTable = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
                var PlineCls = RXObject.GetClass(typeof(Polyline));
                foreach (ObjectId btrId in blockTable)
                {
                    var btr = (BlockTableRecord)tr.GetObject(btrId, OpenMode.ForWrite);
                    if (btr.IsLayout)
                    {
                        foreach (ObjectId id in btr)
                        {
                            if (id.ObjectClass == PlineCls)
                            {
                                var pline = (Polyline)tr.GetObject(id, OpenMode.ForRead);
                                if (IsreqHatch(pline.Layer))
                                    HacthPolyline(pline, btr, tr);
                            }
                        }
                    }
                }
                tr.Commit();
            }
        }


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 3 of 8

MarkJamesRogolino
Advocate
Advocate
well done. Thanks, gile. but is it possible to manage spacing?
0 Likes
Message 4 of 8

_gile
Consultant
Consultant
Accepted solution

You can set the Hatch poperties when you create the new instance:

var hatch = new Hatch { Layer = pline.Layer, PatternScale = 2.0 };

or

var hatch = new Hatch();
hatch.Layer = pline.Layer;
hatch.PatternScale = 2.0;


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 5 of 8

MarkJamesRogolino
Advocate
Advocate
You are very very talented person.
0 Likes
Message 6 of 8

kerry_w_brown
Mentor
Mentor

and very generous !

 


// Called Kerry or kdub in my other life.

Everything will work just as you expect it to, unless your expectations are incorrect. ~ kdub
Sometimes the question is more important than the answer. ~ kdub

NZST UTC+12 : class keyThumper<T> : Lazy<T>;      another  Swamper
0 Likes
Message 7 of 8

MarkJamesRogolino
Advocate
Advocate

Hi, _gile. I am currently getting this error.

goldhorsemillion_0-1661392737227.png

could you tell me why this error occurs?

I hope your answer.

0 Likes
Message 8 of 8

_gile
Consultant
Consultant

What if you try to hatch this polyline with the native command ?



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes