Customize AutoCAD crosshatch patterns

31 Dec, 1999 By: Bill Fane

Once upon a time, in a CADALYST column not far away (November 1999), Captain LearnCurve described how to create custom linetype patterns. This stirred up enough interest that he felt it was worth looking at customizing AutoCAD crosshatch patterns. AutoCAD comes with an extensive library of crosshatch patterns that range from the utilitarian and even mundane ANSI and ISO standard patterns to the out-and-out weird, such as Escher. Even with all of those, you may not find a particular pattern to suit your needs. Most of the original patterns are intended to represent cross-sections of a solid object such as a machine element, but many users also use hatching to represent surface patterns. Architects in particular often make good use of hatching to represent shingles, tiles, parquet flooring, and so on.

As with almost all aspects of AutoCAD, you can customize hatch patterns.

The Bhatch and Hatch commands let you generate simple user-defined patterns on the fly, but those patterns are so simple, they are hardly worth the effort. You can specify the angle, the spacing, and double hatching, which produces a second set of lines at right angles to the first, regardless of the angle of the first set. The lines take on the linetype of the current layer when AutoCAD produces the hatch.

How to hatch
Hatch patterns are stored as standard ASCII text files that you can create or open with Notepad. The basic structure of a hatch pattern definition is simple. It contains a header line and one or more line descriptors.

The header line always starts with an asterisk (*) followed by the pattern name. If desired, the name is then followed by a comma and a description. The description can contain commas. A typical header line thus looks like:
*ansi31,ANSI Iron, Brick, Stone masonry
One or more line descriptions follow the header. AutoCAD always creates hatch patterns from straight-line segments. If you want a curved pattern, you must produce it from a series of short, straight line segments.

A simple hatch pattern can contain one or more line specifications. A line descriptor is always in the format of a series of numbers, separated by commas, that specify the angle, the x-origin, the y-origin, delta-x, and delta-y. It may also contain optional dash descriptors. A typical line descriptor looks like:
45, 0, 0, 0, .125
This is the line descriptor for the standard ANSI31 pattern. AutoCAD ignores the spaces.
• The first number is the angle at which AutoCAD draws the lines. This explains why the Bhatch command often produces an angled pattern even though you didn’t specify an angle—some lines are already defined as angled within your pattern definition.
• The next two zeros specify the x- and y-coordinates of the starting point of the first line drawn.
• The third zero indicates that succeeding lines do not shift in the direction of the line.
• The last number indicates that succeeding lines are spaced 0.125 units apart normal to the lines.
Note that the last two items are relative to any angle specification for the lines and are not relative to the x- and y-coordinate system. No dash specifications follow the spacing definition in this descriptor, so the pattern consists of continuous lines.

You specify noncontinuous lines by adding dash definitions at the end of the line descriptors, each separated by a comma. The dash definitions are simple and look remarkably like the definitions for noncontinuous linetypes in September 1999’s Learning Curve. All you need to do is specify a length for each segment. Negative lengths indicate pen-up to create spaces, and positive values indicate pen-down to draw line segments. Lengths of 0 and 20 both produce a dot.

We can turn our sample continuous line into a simple dashed one just by adding two extra fields to the end of the line descriptor:
45, 0, 0, 0, .125, .25, -.125
This generates a dashed line that consists of a repeating pattern of 0.25-unit-long lines separated by 0.125-unit-long spaces. AutoCAD allows you to append up to six dash specifications to the line specification. For example:
0, 0, 0, 0, .125, .25, -.125, 0, -.125, .125, -.125
produces a hatch pattern made up of horizontal lines that contain a looping repetition of the dash pattern specification. The pattern looks something like this:
__ . _ __ . _ __ . _ __ . _ __ . _ __ . _ __ . _
__ . _ __ . _ __ . _ __ . _ __ . _ __ . _ __ . _
__ . _ __ . _ __ . _ __ . _ __ . _ __ . _ __ . _
__ . _ __ . _ __ . _ __ . _ __ . _ __ . _ __ . _

No rule says so, but dash patterns should contain an even number of specifications that alternate between pen-down (positive length) and pen-up (negative length).

A new slant
But what if you don’t want all the line segments to align vertically? No problem—just supply a nonzero value for the fourth item in the line specification. This causes each succeeding line to shift toward the left. For example:
0, 0, 0, .125, .125, .25, -.125, 0, -.125, .125, -.125
lll__ . _ __ . _ __ . _ __ . _ __ . _ __ . _ __ . _
ll__ . _ __ . _ __ . _ __ . _ __ . _ __ . _ __ . _
l__ . _ __ . _ __ . _ __ . _ __ . _ __ . _ __ . _
__ . _ __ . _ __ . _ __ . _ __ . _ __ . _ __ . _

Negative values produce a shift in the opposite direction.

This is a good time to bring up another significant point. Hatch patterns consist of line segments, so they take on the linetype of the current layer. You should almost always specify a continuous linetype for the layer on which hatching occurs, because any noncontinuous linetype interacts with any dashed lines specified within the hatch pattern, leading to bizarre results.

Why use a simple pattern?
When I started to play with complex hatch pattern definitions, I expected to find something that worked like an array, where all I had to do was define one copy of the pattern, then specify a repetition spacing for multiple copies. Unfortunately, complex hatching in AutoCAD works on a completely different principle.

To help understand how a hatch description functions, watch AutoCAD as it generates a complex pattern on an old,
s-l-o-w computer. Take a look at the hatch definition for the standard Net3 pattern:
*NET3, Network pattern 0-60-120
0, 0, 0, 0, .125
60, 0, 0, 0, .125
120, 0, 0, 0, .125

Figure 1 shows the steps that AutoCAD uses to hatch a selected area. First, it hatches the area with horizontal (red) line segments spaced .125 apart using the first definition line. The second definition line adds all the 60° (green) line segments, and then the final specification draws the 120° (blue) lines. The resulting pattern is a network of equilateral triangles, as shown in black.

Figure 1. AutoCAD generates a hatch pattern as a series of parallel lines, first the horizontal (red) ones, then the left (green) ones, and finally the right (blue) ones, to produce the full black pattern.

A pattern definition for a pattern made up of anything other than parallel, identical lines contains multiple line specifications following the header line.

AutoCAD places no limit on the number of line definitions contained within a hatch specification, so all those really complex hatch patterns are created by carefully specifying a series of dashed lines at different angles and appropriate offsets.

Individually, they are just dashed lines, but collectively the line angles, spacings, and dash patterns combine to create the desired effect. For example, the code below produces a series of 0.125-unit triangles that are 0.125 apart (figure 2).
*Lrncrv120, Triangles
0, 0, 0, .125, .21650635, .125, -.125
60, 0, 0, .125, .21650635, .125, -.125
120, .125, 0, .125, .21650635, .125, -.125

The first line specification has an angle of zero, so it produces the red horizontal line segments that make up the bottoms of the triangles. The second line specification produces the green 60° lines that make up the left sides of the triangles, and the third line specification adds the blue 120° right side lines. In a normal drawing, all the lines would be on the same layer so they would have the same color. The net result is shown by the black triangle pattern.

Figure 2. A complex, repeating pattern is generated as a series of colinear straight-line segments, first the horizontal (red) ones, then the left (green) ones, and finally the right (blue) ones. The line segments fit together to produce the full repeating (black) pattern.

In figure 3, the two black dimensions are the common ones that apply to the last two numbers of each specification line. The remaining dimensions are color-coded, and you can easily relate them back to their corresponding definition lines.

Figure 3. These dimension values in a hatch definition file produced the triangle pattern in figure 2.

The concept behind creating a complex hatch pattern is not difficult, but it can be tedious and frustrating. You’ll usually find it best to draw your pattern out in different colors, one per line specification, and then dimension it as I did in figure 3. This makes it much easier to decide which values are required where.
To put things in perspective, AutoCAD’s Escher hatch pattern requires twenty-one line specifications!

Like most programming languages, AutoCAD ignores anything that follows a semicolon. You can use them to add comments to the file, or to remark out some definition lines while you fine-tune others.

Store them
Now that you know how to create line definition patterns, you should know where to store them. As you develop a pattern, store it in its own file and follow these simple rules:
• The filename must be the same as the pattern name from the header line, minus the asterisk, and it must have the extension PAT.
• You must store it in a directory located in AutoCAD’s search path, such as C:\Program files\…\Support, where \…\ indicates your AutoCAD release.
• You must press <Enter> at the end of the last line in the file.

When you want to use your new pattern, simply start the Bhatch dialog box, change the type from Predefined to Custom, type in the name of your pattern, and away you go.

Once the pattern works properly, you can cut and paste it into the standard acad.pat file. This is the master file that holds AutoCAD’s predefined patterns. Now your custom definition is a regular, predefined one.

The gnarly bits
If you create a custom pattern definition, it must be present, either within its own file or as an addition to acad.pat, whenever you create or edit a hatch region that uses it. If you don’t include a copy of your specification file when you pass a copy of your drawings to others, they won’t be able to edit your hatchings or create new hatched regions that match yours. They can plot and do regular AutoCAD edits.

The next gnarly bit is that you can’t easily modify the Bhatch dialog box to display a preview swatch of your custom pattern, and you can’t browse for external filenames. You must know the pattern name. You might want to print out a hard copy of named test swatches or generate your own custom image menu. Finally, when you edit a hatch pattern definition, existing instances of it in your drawing won’t update automatically. You must manually edit each one individually.

And now for something completely different
Always wear a proper, approved crash helmet when riding a motorcycle, even if it’s not legally required in your jurisdiction. My son’s helmet was beaten to a pulp and can never be worn again—but he sustained absolutely no injuries other than a broken leg. He’ll recover completely in a couple of months.

More News and Resources from Cadalyst Partners

For Mold Designers! Cadalyst has an area of our site focused on technologies and resources specific to the mold design professional. Sponsored by Siemens NX.  Visit the Equipped Mold Designer here!

For Architects! Cadalyst has an area of our site focused on technologies and resources specific to the building design professional. Sponsored by HP.  Visit the Equipped Architect here!