Imports System.Runtime.InteropServices
Public Class RecognizeAndCreatePatterns
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim objApplication As SolidEdgeFramework.Application = Nothing
        Dim objDoc As SolidEdgePart.SheetMetalDocument = Nothing
        Dim objHoles As SolidEdgePart.Holes
        Dim objHole(0 To 100) As SolidEdgePart.Hole
        Dim objPatterns As SolidEdgePart.Patterns
        Dim objRecognizedPattern(0 To 100) As Object
        Dim objPattern As SolidEdgePart.Pattern
        Dim objModel As Object
        Dim numCreatedPatterns As Integer
        Dim numHoles As Integer
        Dim objType As Type = Nothing
        Try
            ' Create/get the application with specific settings
            objApplication = Marshal.GetActiveObject("SolidEdge.Application")
            If objApplication Is Nothing Then
                ' Get the type from the Solid Edge ProgID
                objType = Type.GetTypeFromProgID("SolidEdge.Application")
                ' Start Solid Edge
                objApplication = Activator.CreateInstance(objType)
                ' Make Solid Edge visible
                objApplication.Visible = True
            End If
            'Get reference to active document
            objDoc = objApplication.ActiveDocument()
            If Err().Number <> 0 Or objDoc Is Nothing Then
                MsgBox("Could not open document")
            End If
            objModel = objDoc.Models.Item(1)
            objPatterns = objModel.Patterns
            If objPatterns Is Nothing Then
                MsgBox("Failed to get the Patterns collection")
                Exit Sub
            End If
            objHoles = objModel.Holes()
            If objHoles Is Nothing Then
                MsgBox("Failed to get the Holes collection")
                Exit Sub
            End If
            numHoles = objHoles.Count()
            If numHoles <= 0 Then
                MsgBox("Failed to get the Holes")
                Exit Sub
            End If
            For value As Integer = 1 To numHoles
                objHole(value - 1) = objHoles.Item(value)
            Next
            objPatterns.RecognizeAndCreatePatterns(numHoles, objHole, numCreatedPatterns, objRecognizedPattern)
            If numCreatedPatterns <= 0 Then
                MsgBox("Failed to Recognize Patterns")
            End If
            For value As Integer = 0 To (numCreatedPatterns - 1)
                If objRecognizedPattern(value) Is Nothing Then
                    MsgBox("Failed to Recognize Patterns")
                End If
                objPattern = objRecognizedPattern(value)
                numCreatedPatterns = objPattern.NumberOfOccurrences()
            Next
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub
End Class