Getting the list of hatch patterns available in the current AutoCAD drawing using .NET

This post was inspired by an email I saw from Philippe Leefsma, from DevTech EMEA, which made use of the Autodesk.AutoCAD.Windows.Data namespace – something I didn't even know existed.

The specific example Philippe showed was how to access the list of hatch patterns available in the current drawing without iterating through the relevant dictionary in the named objects dictionary.

Here's Philippe's C# code:

using Autodesk.AutoCAD.ApplicationServices;

using Autodesk.AutoCAD.EditorInput;

using Autodesk.AutoCAD.Runtime;

using Autodesk.AutoCAD.Windows.Data;

 

public class DataStuff

{

  [CommandMethod("HPS")]

  static public void GetHatchPatterns()

  {

    Editor ed =

      Application.DocumentManager.MdiActiveDocument.Editor;

    foreach (

      string str in HatchPatterns.Instance.AllPatterns)

      ed.WriteMessage("\n" + str);

  }

}

When we run it, we see the following output at the command-line:

Command: HPS

SOLID

ANGLE

ANSI31

ANSI32

ANSI33

ANSI34

ANSI35

ANSI36

ANSI37

ANSI38

AR-B816

AR-B816C

AR-B88

AR-BRELM

AR-BRSTD

AR-CONC

AR-HBONE

AR-PARQ1

AR-RROOF

AR-RSHKE

AR-SAND

BOX

BRASS

BRICK

BRSTONE

CLAY

CORK

CROSS

DASH

DOLMIT

DOTS

EARTH

ESCHER

FLEX

GOST_GLASS

GOST_GROUND

GOST_WOOD

GR_LINEAR

GR_CYLIN

GR_INVCYL

GR_SPHER

GR_HEMISP

GR_CURVED

GR_INVSPH

GR_INVHEM

GR_INVCUR

GRASS

GRATE

GRAVEL

HEX

HONEY

HOUND

INSUL

ACAD_ISO02W100

ACAD_ISO03W100

ACAD_ISO04W100

ACAD_ISO05W100

ACAD_ISO06W100

ACAD_ISO07W100

ACAD_ISO08W100

ACAD_ISO09W100

ACAD_ISO10W100

ACAD_ISO11W100

ACAD_ISO12W100

ACAD_ISO13W100

ACAD_ISO14W100

ACAD_ISO15W100

LINE

MUDST

NET

NET3

PLAST

PLASTI

SACNCR

SQUARE

STARS

STEEL

SWAMP

TRANS

TRIANG

ZIGZAG

USER

I was curious about this namespace and so asked around to find out more about its origins. Apparently it was introduced to provide simplified access to data that needs to be displayed in the AutoCAD user interface: we expose this data through Autodesk.AutoCAD.Windows.Data to make it simpler to implement the ribbon without having to iterate database objects there directly. What's nice is the data changes when the connected database does: whether relevant items are added to, edited in or removed from the database, or the user changes the currently active drawing in the editor, the data made accessible is adjusted accordingly.

This capability is known internally as the BOL or Bindable Object Layer. Cool stuff.

At this stage, I didn't find a way to access other data from this namespace: HatchPatterns is the only class with a static Instance property that provides access to it. There is a very interesting DataBindings class, that appears to provide access to the data in the rest of the namespace, but I so far have been unable to find a way to instantiate (or retrieve an existing instance of it).

12 responses to “Getting the list of hatch patterns available in the current AutoCAD drawing using .NET”

  1. Hi Kean,

    Fenton Webb makes use of the Autodesk.AutoCAD.Windows.Data namespace and UIDataBindings in a webcast from July 09, ACAD_Using_WPF_in_your_applications_14July2009.

    Cheers, Mark

  2. Hi Mark,

    Excellent - thanks for the pointer. I'll go ahead and review this, to see whether there's something for a follow-up post.

    Cheers,

    Kean

  3. Juergen.Becker@CAD-Becker.de Avatar
    Juergen.Becker@CAD-Becker.de

    Hi Kean,
    this sample doesnt work for me, because there is an error with the foreach-loop with variables from Typ System.Collections.ObjectModel.ObservableCollection.
    Is there another way to get all the names?
    Thanks for help.
    Regards Jürgen

  4. Hi Jürgen,

    Which versions of .NET and AutoCAD are you targeting? foreach should just work on ObservableCollections...

    Regards,

    Kean

  5. Juergen.Becker@CAD-Becker.de Avatar
    Juergen.Becker@CAD-Becker.de

    Hi Kean,
    I'm targeting .Net 3.5 (Managed(v2.0, v1.1, v1.0)).
    What I think is that I cannot go through the ObservableCollections with foreach.
    Maybe its works with .Net 4.
    Regards Jürgen

  6. Hi Jürgen,

    You're probably right... it seems not that hard to add, though:

    stackoverflow.com/questions/2519416/how-do-i-execute-a-foreach-lambda-expression-on-observablecollectiont

    Regards,

    Kean

  7. Juergen.Becker@CAD-Becker.de Avatar
    Juergen.Becker@CAD-Becker.de

    Hi Kean,

    I've figured it out.
    I have to reference WindowsBase. Now it works.
    The ADN-Team helps. Many thanks to them and to you.

    Regards Jürgen

  8. Autocad hatch patterns Avatar
    Autocad hatch patterns

    Is it possible to add custom hatch patterns to the hatch library? How should I add one if that is possible?I think it is very hard to add through auto cad patterns.Thanks.

  9. I can't tell whether this is a spam comment or not. Is this a genuine question? Either way, I've removed the URL you've used.

    If it is genuine, please ask it on the appropriate online forum.

    Kean

  10. Hi Kean,

    I know this post is over a year old and I apologize for asking such a question here, but do you know how I could get all hatch pattern names in .NET using the ObjectArx 2010 API? Your code only works for ObjectArx 2013. I searched the ObjectArx 2010 docs but didn't find anything.. does that mean I have to parse the acad.pat file to get the information?

    I'd greatly appreciate any help on this. Thanks in advance.

    Best Regards

    Christian

  11. It is written this way because all of the patterns are not resident in the database.

  12. NICE WEBSITE. NICE CONTENT KEEP IT UP.
    But I am confused about this.
    How to use Autocad Hatch ?

Leave a Reply to Kean Walmsley Cancel reply

Your email address will not be published. Required fields are marked *