Hatches with gap tolerance from script

Hi all,

I am trying to take a rectangle and create internal hatches with a gap tolerance based on an array of points (Created from the rectangle).

Dynamo will take the rectangle (Polyline) and create an array of points within the rectangle. The idea is to then use the traceboundary method or similar on each of these points to test for all objects in the rectangle and then create a hatch from the boundary objects, and if there is a gap, then based on an input tolerance, will ignore it and hatch correctly (Like the hatch command with a tolerance set either with hpgaptol or via the UI)

So far, I have created the script that will create the boundary lines and hatches in modelspace. This works fine for any closed objects like a closed polyline or circle, however if there is an open polyline it will not close it. I have tried a few different methods but I’m not sure if its possible to do this, or at least not with traceboundary. For context, when I recieve a masterplan, i need to hatch all permeable areas for drainage calculations, but often the areas arent closed. I thought that gap tolerance with internal pick hatches would be the best solution. If there is a better way without using this method, please let me know!

For the code used, I am not an expert in Python scripting and only know what i’ve picked up whilst using dynamo, so I have used ChatGPT to assist in writing a decent bit of code, see below:

import clr

# Add Assemblies for AutoCAD
clr.AddReference('AcMgd')
clr.AddReference('AcCoreMgd')
clr.AddReference('AcDbMgd')

# Import references from AutoCAD
from Autodesk.AutoCAD.ApplicationServices import *
from Autodesk.AutoCAD.DatabaseServices import *
from Autodesk.AutoCAD.EditorInput import *
from Autodesk.AutoCAD.Geometry import *

# The inputs to this node will be stored as a list in the IN variables.
dataEnteringNode = IN

# Extract input points
points = IN[0]

def create_hatches_from_boundaries(points):
    # Initialize an empty list to store hatch objects
    hatch_objects = []

    # Get the active document
    doc = Application.DocumentManager.MdiActiveDocument

    # Get the current value of the hpgaptol system variable
    gap_tolerance = float(Application.GetSystemVariable("hpgaptol"))

    # Get the editor
    editor = doc.Editor

    # Start a transaction
    with doc.TransactionManager.StartTransaction() as t:
        # Open the ModelSpace for writing
        bt = t.GetObject(doc.Database.BlockTableId, OpenMode.ForRead)
        btr = t.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite)

        # Get the LayerTableId
        layer_table_id = doc.Database.LayerTableId

        # Loop through each point
        for point in points:
            # Use TraceBoundary method to create boundary objects
            boundary_objs = editor.TraceBoundary(point, True)

            # Loop through each boundary object
            for boundary_obj in boundary_objs:
                # Append the boundary object to the ModelSpace
                boundary_object_id = btr.AppendEntity(boundary_obj)
                t.AddNewlyCreatedDBObject(boundary_obj, True)

                # Set the layer for the boundary object
                boundary_obj.LayerId = layer_table_id["_Temp"]

                # Create a hatch object
                hatch = Hatch()
                hatch.SetHatchPattern(HatchPatternType.PreDefined, "SOLID")

                # Create a collection for the boundary objects
                boundary_object_ids = ObjectIdCollection()
                boundary_object_ids.Add(boundary_object_id)

                # Append the boundary loop to the hatch
                hatch.AppendLoop(HatchLoopTypes.Outermost, boundary_object_ids)

                # Evaluate the hatch
                hatch.EvaluateHatch(True)

                # Set the layer for the hatch object
                hatch.LayerId = layer_table_id["HatchAreas"]

                # Add the hatch object to the list
                hatch_objects.append(hatch)

                # Append the hatch object to the ModelSpace
                hatch_id = btr.AppendEntity(hatch)
                t.AddNewlyCreatedDBObject(hatch, True)

        # Commit the transaction
        t.Commit()

    return hatch_objects

# Call the function with the input points
hatch_objects = create_hatches_from_boundaries(points)

# Output the hatch objects
OUT = hatch_objects

Once created, I use dynamo nodes to handle the objects on the differant layers to remove any duplications and remove the boundary lines, leaving just the hatches so there arent several areas on top of each other.