Hatch Example

This example demonstrates creating hatches with layer-based colors. It shows how to define a hatch by creating a boundary and applying a pattern.

using CadSDK;
using CadSDK.Elements;
using CadSDK.Enums;
using CadSDK.Geometry;
using System.Numerics;

// Create a drawing session
var session = new DrawingSession
{
    ExportFormat = ExportFormat.DWG,
    CadVersion = CadVersion.Cad2018
};

// Define layers
var boundaryLayer = new Layer { Name = "TestLayer_HatchBoundary", ColorARGB = AciColors.YellowGreen };
var hatchLayer = new Layer { Name = "TestLayer_Hatch", ColorARGB = AciColors.DeepRed2 };
session.AddLayer(boundaryLayer);
session.AddLayer(hatchLayer);

// Hatch using layer colors
var boundaryPolyline1 = new Polyline(
    vertices:
    [
        new Vector3(200, 200, 0),
        new Vector3(300, 100, 0),
        new Vector3(450, 280, 0),
        new Vector3(300, 250, 0),
        new Vector3(200, 250, 0),
        new Vector3(200, 200, 0)
    ])
{
    LayerName = boundaryLayer.Name
};
session.AddElement(boundaryPolyline1);

var hatch1 = new Hatch(
    pattern: HatchPattern.ANSI37)
{
    LayerName = hatchLayer.Name
};
hatch1.BoundaryElements.Add(boundaryPolyline1);
session.AddElement(hatch1);

// Export the drawing
// You can signup and get your free API key
string apiKey = "your-api-key"; // Get your free API key from the dashboard
string outputPath = "drawing.dwg";
await session.ExportAndSaveAsync(apiKey, outputPath);
# Coming soon...

Key Points

  • Hatches consist of:
    • One or more closed boundary elements (BoundaryElements)
    • A pattern type (Pattern)
  • Boundary elements can be any closed geometry (polylines in this example).
  • The hatch will inherit styling from its respective layer.