I want to automatically add X and Y dimensions to hatched areas in a SolidWorks drawing. The workflow should be simple for the user: they open the drawing, click a button in my add-in, and all hatched regions get their width (X) and height (Y) dimensions automatically.
Is it possible to detect the boundaries of hatches and insert these dimensions programmatically using the SolidWorks API? If yes, what would be the recommended approach?

You can access the Hatches and their Sketches, if that is helpful.
I only know VBA, but I am sure you can transfer it to C# for your purposes.
'by John R. Sitek
'30-DEC-2024
'This macro loops views to get Sketches within Hatches
Option Explicit
Sub Main()
Dim swApplication As SldWorks.SldWorks
Dim swDrawing As SldWorks.DrawingDoc
Dim swSketchHatch As SldWorks.SketchHatch
Dim swSelectData As SldWorks.SelectData
Dim swSketch As SldWorks.Sketch
Dim vbViews As Variant
Dim vbSheet As Variant
Dim vbView As Variant
Dim vbSketchHatches As Variant
Dim vbSketchHatch As Variant
Set swApplication = Application.SldWorks
Set swDrawing = swApplication.ActiveDoc
'If nothing is open
If swApplication.GetDocumentCount = 0 Then
MsgBox "Please open a drawing file first, then rerun the macro."
Exit Sub
End If
'If the file is not a part
If swDrawing.GetType <> swDocDRAWING Then
MsgBox "This macro is for drawings only."
Exit Sub
End If
'If the file has not been saved
If swDrawing.GetPathName = "" Then
MsgBox "Please save the file first, then rerun the macro."
Exit Sub
End If
'Get views
vbViews = swDrawing.GetViews
'Loops sheets and views
For Each vbSheet In vbViews
For Each vbView In vbSheet
'If not a drawing sheet
If vbView.Type <> swDrawingSheet Then
vbSketchHatches = vbView.GetSketch.GetSketchHatches
For Each vbSketchHatch In vbSketchHatches
Set swSketch = vbSketchHatch.GetSketch
Debug.Print swSketch.GetLineCount2(swCrossHatchInclude)
Next vbSketchHatch
End If
Next vbView
Next vbSheet
End Sub
"Hatch" objects don't have attributes that are available for referencing. You can't associate your dimensions to the objects unless they are "exposed".
A quick example in VBA of adding dimensions to Hatch Sketch Points.
'by John R. Sitek
'31-DEC-2025
'This macro loops views to get Sketches within Hatches to add dimensions
Option Explicit
Sub Main()
Dim swApplication As SldWorks.SldWorks
Dim swDrawing As SldWorks.DrawingDoc
Dim swView As SldWorks.View
Dim swSelectData As SldWorks.SelectData
Dim vbViews As Variant, vbSheet As Variant, vbView As Variant, vbSketchHatches As Variant, vbSketchHatch As Variant, vbSketchPoints As Variant ', vbSketchPoint As Variant
Set swApplication = Application.SldWorks
Set swDrawing = swApplication.ActiveDoc
swDrawing.ClearSelection2 True
'If nothing is open
If swApplication.GetDocumentCount = 0 Then
MsgBox "Please open a drawing file first, then rerun the macro."
Exit Sub
End If
'If the file is not a part
If swDrawing.GetType <> swDocDRAWING Then
MsgBox "This macro is for drawings only."
Exit Sub
End If
'If the file has not been saved
If swDrawing.GetPathName = "" Then
MsgBox "Please save the file first, then rerun the macro."
Exit Sub
End If
'Get views
vbViews = swDrawing.GetViews
'Loops sheets and views
For Each vbSheet In vbViews
For Each vbView In vbSheet
'If not a drawing sheet
If vbView.Type <> swDrawingSheet Then
Set swView = vbView
vbSketchHatches = swView.GetSketch.GetSketchHatches
'Quick dimension hatch size
For Each vbSketchHatch In vbSketchHatches
vbSketchPoints = vbSketchHatch.GetSketch.GetSketchPoints2
vbSketchPoints(0).Select4 True, swSelectData
vbSketchPoints(1).Select4 True, swSelectData
swDrawing.AddDimension2 swView.Position(0), swView.Position(1), 0
vbSketchPoints(1).Select4 True, swSelectData
vbSketchPoints(2).Select4 True, swSelectData
swDrawing.AddDimension2 swView.Position(0), swView.Position(1), 0
Next vbSketchHatch
End If
Next vbView
Next vbSheet
'Clear selections
swDrawing.ClearSelection2 True
End Sub