Hatch Entity (HatchEnt)
Hatch Entity (HatchEnt)
HatchEnt represents a hatch entity in CAD, used to fill closed regions with patterns or solid colors.
Overview
Hatches can use predefined patterns, solid colors, or gradients. A hatch requires one or more closed boundaries to define the fill region. Boundaries are defined through the Edge and Edges classes, supporting polyline, circle, and ellipse types.
Constructor
import { HatchEnt } from 'vjcad';
// Create hatch entity
const hatch = new HatchEnt();
// Set pattern
hatch.patternName = 'ANSI31';
hatch.patternScale = 1.0;
hatch.patternAngle = 0;
// Call setDefaults after setting boundaries
hatch.setLoops(edges);
hatch.setDefaults();Properties
| Property | Type | Description |
|---|---|---|
patternName | string | Hatch pattern name |
patternScale | number | Pattern scale |
patternAngle | number | Pattern rotation angle (radians) |
patternCross | boolean | Whether it is a cross pattern |
loops | Edges | Boundary loop collection |
Boundary Types (EdgeType)
| Type | Description |
|---|---|
EdgeType.Polyline | Polyline boundary |
EdgeType.Circle | Circular boundary |
EdgeType.Ellipse | Ellipse boundary |
Common Patterns
| Pattern Name | Description |
|---|---|
SOLID | Solid fill |
ANSI31 | 45° diagonal lines |
ANSI32 | Double diagonal lines |
ANSI37 | Cross lines |
JIS_LC_20 | JIS pattern |
PAT Pattern Format Description
The PAT format is the AutoCAD standard hatch pattern definition format. Each pattern consists of multiple definition lines:
Header Line
*pattern_name,pattern_description- Starts with
* - Pattern name and description separated by comma
Line Definition
angle,X_origin,Y_origin,deltaX,deltaY[,dash_definition...]| Parameter | Description |
|---|---|
| angle | Line drawing direction (degrees), 0=horizontal right, 90=vertical up |
| X_origin | X coordinate of first line start |
| Y_origin | Y coordinate of first line start |
| deltaX | Offset along line direction (for dashed line types) |
| deltaY | Offset perpendicular to line direction (line spacing) |
| dash_definition | Optional dash pattern: positive=solid segment, negative=gap, 0=dot |
Format Examples
Simple 45° diagonal:
45,0,0,0,1- Angle 45°, start (0,0), deltaY=1 (line spacing), continuous solid line
Horizontal line with dashes:
0,0,0,4,4,2,-1,0.5,-0.5- Horizontal line, start (0,0), deltaX=4, deltaY=4
- Line type: solid 2, gap 1, dot, gap 0.5
Custom Patterns
Register Custom Pattern
import { Engine } from 'vjcad';
// Pattern definition string (PAT format)
const patString = `*Grass or swamp,Grass or swamp
0,1.2,0.08,4,4,0.6,-0.12,0.16,-0.12,0.6,-2.4
0,1,0.2,4,4,0.4,-0.3,0.6,-0.3,0.4,-2
90,0.8,2.6,4,4,0.36,-3.64
90,2,0.2,4,4,0.4,-3.6`;
// Parse PAT format string
function parsePatternString(patString) {
const lines = patString.trim().split('\n');
const headerMatch = lines[0].match(/^\*([^,]+),(.*)$/);
if (!headerMatch) throw new Error(`Invalid pattern header`);
const name = headerMatch[1].trim();
const description = headerMatch[2].trim();
const patternLines = [];
for (let i = 1; i < lines.length; i++) {
const line = lines[i].trim();
if (!line || line.startsWith(';')) continue;
const parts = line.split(',').map(p => parseFloat(p.trim()));
patternLines.push({
angle: parts[0],
xOrigin: parts[1],
yOrigin: parts[2],
deltaX: parts[3],
deltaY: parts[4],
dashes: parts.slice(5)
});
}
return { name, description, lines: patternLines };
}
// Register to PatternManager
const patternDef = parsePatternString(patString);
Engine.patternManager.registerPattern(patternDef, "custom");
// Use custom pattern
const hatch = new HatchEnt();
hatch.patternName = "Grass or swamp";
hatch.patternScale = 10;Auto-calculate Scale
Use the calculateHatchPatternScale function to automatically calculate an appropriate hatch scale based on boundary size, avoiding fills that are too dense or too sparse.
import { Engine, HatchEnt, PolylineEnt, Edge, Edges, EdgeType, calculateHatchPatternScale } from 'vjcad';
// Create boundary
const boundary = new PolylineEnt();
boundary.setPoints([[0, 0], [200, 0], [200, 150], [0, 150]]);
boundary.isClosed = true;
boundary.setDefaults();
// Calculate boundary size
const bbox = boundary.boundingBox();
const boundsSize = Math.max(bbox.maxX - bbox.minX, bbox.maxY - bbox.minY);
// Auto-calculate scale
const patternName = "ANSI31";
const patternDef = Engine.patternManager.getPattern(patternName);
const autoScale = calculateHatchPatternScale(boundsSize, patternName, patternDef);
// Create hatch
const hatch = new HatchEnt();
hatch.patternName = patternName;
hatch.patternScale = autoScale; // Use auto-calculated scale
const edges = new Edges();
const edge = new Edge();
edge.edgeType = EdgeType.Polyline;
edge.bulgePoints = boundary.bulgePoints.clone();
edges.add(edge);
hatch.setLoops(edges);
hatch.setDefaults();
Engine.addEntities([boundary, hatch]);calculateHatchPatternScale Parameters
| Parameter | Type | Description |
|---|---|---|
boundsSize | number | Maximum boundary dimension (larger of width/height) |
patternName | string | Pattern name, "SOLID" returns 1 directly |
patternDef | object | Pattern definition (optional, fetched from PatternManager if not provided) |
getPatternUnitSize Function
Get the approximate size of the pattern repeat unit for custom scale calculation:
import { getPatternUnitSize } from 'vjcad';
const patternDef = Engine.patternManager.getPattern("ANSI31");
const unitSize = getPatternUnitSize(patternDef);
console.log(`Pattern unit size: ${unitSize}`);Examples
Polyline Boundary Hatch
import { Engine, HatchEnt, PolylineEnt, Edge, Edges, EdgeType } from 'vjcad';
// Create polyline boundary
const polyline = new PolylineEnt();
polyline.setPoints([
[0, 0],
[60, 0],
[60, 50],
[30, 70],
[0, 50]
]);
polyline.isClosed = true;
polyline.setDefaults();
// Create hatch
const hatch = new HatchEnt();
hatch.patternName = 'ANSI31';
hatch.patternScale = 2;
// Create boundary from polyline
const edges = new Edges();
const edge = new Edge();
edge.edgeType = EdgeType.Polyline;
edge.bulgePoints = polyline.bulgePoints.clone();
edges.add(edge);
hatch.setLoops(edges);
hatch.setDefaults();
hatch.color = 1;
Engine.addEntities([polyline, hatch]);Circular Boundary Hatch
import { Engine, HatchEnt, CircleEnt, Edge, Edges, EdgeType } from 'vjcad';
// Create circular boundary
const circle = new CircleEnt([120, 35], 30);
circle.setDefaults();
// Create solid hatch
const hatch = new HatchEnt();
hatch.patternName = 'SOLID';
// Create boundary from circle (setLoops automatically handles regenBulgePoints and closure)
const edges = new Edges();
const edge = new Edge();
edge.edgeType = EdgeType.Circle;
edge.center = circle.center.clone();
edge.radius = circle.radius;
edges.add(edge);
hatch.setLoops(edges);
hatch.setDefaults();
hatch.color = 3;
Engine.addEntities([circle, hatch]);Hatch with Hole (Multiple Boundaries)
import { Engine, HatchEnt, PolylineEnt, CircleEnt, Edge, Edges, EdgeType } from 'vjcad';
// Outer boundary (rectangle)
const outerRect = new PolylineEnt();
outerRect.setPoints([
[180, 0],
[260, 0],
[260, 70],
[180, 70]
]);
outerRect.isClosed = true;
outerRect.setDefaults();
// Inner boundary (circular hole)
const innerCircle = new CircleEnt([220, 35], 15);
innerCircle.setDefaults();
// Create hatch
const hatch = new HatchEnt();
hatch.patternName = 'ANSI32';
hatch.patternScale = 2;
// Put all boundaries in the same Edges object
const allEdges = new Edges();
// Outer boundary
const outerEdge = new Edge();
outerEdge.edgeType = EdgeType.Polyline;
outerEdge.bulgePoints = outerRect.bulgePoints.clone();
allEdges.add(outerEdge);
// Inner boundary (hole)
const innerEdge = new Edge();
innerEdge.edgeType = EdgeType.Circle;
innerEdge.center = innerCircle.center.clone();
innerEdge.radius = innerCircle.radius;
allEdges.add(innerEdge);
hatch.setLoops(allEdges);
hatch.setDefaults();
hatch.color = 5;
Engine.addEntities([outerRect, innerCircle, hatch]);Methods
setLoops(edges: Edges)
Set boundary loops. This method automatically:
- Calls
regenBulgePoints()forCircleandEllipsetype boundaries to generate bulge points - Sets the hatch to closed state
hatch.setLoops(edges);setDefaults()
Set default properties (layer, color, etc.).
regenLoop()
Regenerate hatch loops. Call when boundaries have changed.