Through the Interface: Manipulating the draw order of a newly created AutoCAD hatch using .NET

May 2015

Sun Mon Tue Wed Thu Fri Sat
          1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30


« Closing all open AutoCAD documents from a .NET application | Main | Adding XData to AutoCAD entities using .NET »

March 30, 2007

Manipulating the draw order of a newly created AutoCAD hatch using .NET

This question came in from Limin as a comment in this previous post:

Is there .Net interface for autocad command "DrawOrder"? For example: after solid-hatch a polyline, need to reorder polyline and hatch, how to make it happen using .NET?

I've put together some C# code to demonstrate how to do this:

using Autodesk.AutoCAD.ApplicationServices;

using Autodesk.AutoCAD.DatabaseServices;

using Autodesk.AutoCAD.EditorInput;

using Autodesk.AutoCAD.Runtime;

namespace HatchDrawOrder


  public class Commands



    static public void HatchDrawOrder()


      Document doc =


      Editor ed = doc.Editor;

      // Ask the user to select a hatch boundary

      PromptEntityOptions opt =

        new PromptEntityOptions(

          "\nSelect boundary: "


      opt.SetRejectMessage("\nObject must be a curve.");

      opt.AddAllowedClass(typeof(Curve), false);

      PromptEntityResult res =


      if (res.Status == PromptStatus.OK)


        Transaction tr =


        using (tr)


          // Check the entity is a closed curve

          DBObject obj =





          Curve cur = obj as Curve;

          if (cur != null && cur.Closed == false)


            ed.WriteMessage("\nLoop must be a closed curve.");




            // Add the hatch to the model space

            BlockTable bt =





            BlockTableRecord btr =





            Hatch hat = new Hatch();






            ObjectId hatId = btr.AppendEntity(hat);

            tr.AddNewlyCreatedDBObject(hat, true);

            // Add the hatch loop and complete the hatch

            ObjectIdCollection ids =

              new ObjectIdCollection();


            hat.Associative = true;






            // Change the draworder of both hatch and loop



            DrawOrderTable dot =













The code to manipulate the draw order of these two entities is very simple - we have to open the DrawOrderTable for the space containing the entities we care about, and use it to change the order in which the entities are drawn. In this case I chose to send them both to the back of the draw order stack, but you might also manipulate their draw order relative to other entities in the drawing.

On a personal note, my family and I have arrived safely in Beijing - we're all a bit jet-lagged, but very happy to be here. The journey was thankfully uneventful, and with two young children that's really as much as you can hope for. :-)


TrackBack URL for this entry:

Listed below are links to weblogs that reference Manipulating the draw order of a newly created AutoCAD hatch using .NET:

blog comments powered by Disqus


10 Random Posts