Option Strict Off
Option Explicit On
Imports System.Runtime.InteropServices
'Imports System.Reflection
Imports Microsoft.Win32

Namespace DINTable
    ' solidifying the interface guid
    <Guid("60A8082C-3AD7-4dea-BB8D-0C4CFD0E7CBA"), _
        InterfaceType(ComInterfaceType.InterfaceIsIDispatch)> _
       Public Interface _DINTable
        <DispId(1)> Function PZLFunctionArgs() As Integer
        <DispId(2)> Function PZL(ByRef Feature As Object, ByRef Thickness As Double, ByRef Radius As Double, ByRef SweepAngle As Double, ByRef Material As String, ByRef NF As Double) As Double

    End Interface

    ' solidifying the clsID
    <Guid("4581135B-2531-4c0d-BA8D-62A60B0712D1"), ClassInterface(ClassInterfaceType.None), _
    ProgId("DIN.PZL")> Public Class PZL
        Implements _DINTable

        '<System.Runtime.InteropServices.ProgId("PZL_NET.PZL")> Public Class PZL
        '****************************************************************
        '*      PZL OLE Server Class Example for SE-Sheet Metal         *
        '*                                                              *
        '* This example uses the DIN Standard Bend Equation to          *
        '* determine the length of the bend in the flat.                *
        '*                                                              *
        '* DINBEQ.PZL                                                   *
        '****************************************************************

        Public Sub New()
            'NOTE: In this example there are no default values to set.
        End Sub

        Protected Overrides Sub Finalize()
            'NOTE: In this example there are no cleanup operations to perform.
        End Sub

        Public Function PZLFunctionArgs() As Integer Implements _DINTable.PZLFunctionArgs
            PZLFunctionArgs = 0
        End Function


        Public Function PZL(ByRef Feature As Object, ByRef Thickness As Double, ByRef Radius As Double, ByRef Angle As Double, ByRef Material As String, ByRef NF As Double) As Double Implements _DINTable.PZL
            'The function arguments and units are defined as follows:
            'Feature - The SE-Sheet Metal feature that is being created/updated or flattened.
            'Thickness- Material thickness in meters. Defined in tools options
            'Radius- Bend radius in meters. Defined by the feature.
            'Angle- Sweep angle of the bend in radians. Defined by the feature
            'NF- Neutral factor value (No units). Define in tools options and on the feature.
            'PZL - Return value of this function. Must be in meters.
            '      NOTE: The PZL value that you return to SE-Sheet Metal must be >= 0.000

            Dim k As Double 'Correction factor
            Dim dErrFix As Double

            'Fix for division error bug when calling program from C++
            On Error Resume Next
            dErrFix = 1.0# / 2.0#

            On Error GoTo Handle_Error

            'Compute correction factor
            If Radius <= 0.00001 Then 'Check for Zero bend radius. NOTE: Radius is in meters
                'The factor below is for example purposes only. Modify the value or formula
                'as needed to support your required values for zero bend radii.
                k = 1
            ElseIf (Radius / Thickness) > 5 Then
                k = 1
            Else
                k = 0.65 + 0.5 * System.Math.Log(Radius / Thickness) / System.Math.Log(10)
            End If

            'Since argument values are in meters, radians etc. and we need to return meters to SE-Sheet Metal no
            'unit conversion is required
            PZL = (Radius + Thickness * k / 2) * Angle
            Exit Function

Handle_Error:
            Err.Clear()
            PZL = (Radius + Thickness * 0.33) * Angle

        End Function

#Region "REGISTRATION"

        ' Code to register/unregister the additional add-in data was derived from Jason Newell (jasonnewell.net)

        ' When Regasm is run on this project, either through IDE or command window, these functions will be called.

        ' Setup the required Solid Edge registry values for an addin that were not automatically
        ' added due to the guid, progid and com visible attributes.
        <ComRegisterFunctionAttribute()> _
        Public Shared Sub RegisterFunction(ByVal t As Type)

            Dim Key As RegistryKey = Registry.ClassesRoot.CreateSubKey("CLSID\{" + t.GUID.ToString() + "}")
            '   {40FC6ED5-2438-11CF-A3DB-080036F12502} = automation object
            If Not (Key Is Nothing) Then
                Key.CreateSubKey("Implemented Categories\" & "{40FC6ED5-2438-11CF-A3DB-080036F12502}")
                Key.Close()
            End If
        End Sub

        ' Remove any registry values specifically added above.
        <ComUnregisterFunctionAttribute()> _
        Public Shared Sub UnregisterFunction(ByVal t As Type)
            Registry.ClassesRoot.DeleteSubKeyTree("CLSID\{" + t.GUID.ToString() + "}")
        End Sub
#End Region
    End Class

End Namespace
