﻿Imports System.Text
Public Class Form1
    Dim dwRet As Integer
    Dim ArcStartDir As Short
    Dim MotionType As Short
    Dim szComment As String
    Dim bRet As Boolean
    Dim Id As Short
    Dim AxisNo As Short
    Dim BankNum As Short
    Dim ErrorString As New StringBuilder("", 256)  'Error String

    Private Sub cboBankNum_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboBankNum.SelectedIndexChanged

        BankNum = cboBankNum.SelectedIndex + 1

    End Sub

    Private Sub cboTargetAxis_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboTargetAxis.SelectedIndexChanged

        AxisNo = cboTargetAxis.SelectedIndex + 1
        txtTargetAxis.Text = AxisNo + 1

    End Sub

    Private Sub btnComEnd_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnComEnd.Click

        SmcWExit(Id)
        End

    End Sub

    Private Sub btnComMove_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnComMove.Click

        Dim End_Y As Integer
        Dim End_X As Integer
        Dim Center_Y As Integer
        Dim Center_X As Integer
        Dim ArcSpeed As Double
        '----------------------------------
        ' Set parameters to Driver
        '----------------------------------
        bRet = SetBankParam()
        If bRet = False Then
            Exit Sub
        End If

        '---------------
        ' Ready motion
        '---------------
        dwRet = SmcWSetBankReady(Id, AxisNo, MotionType)
        If dwRet <> 0 Then
            SmcWGetErrorString(dwRet, ErrorString)
            lblComment.Text = "SmcWSetBankReady = " & dwRet & " : " & ErrorString.ToString
            Exit Sub
        End If

        dwRet = SmcWGetBankArcPoint(Id, AxisNo, 1, ArcSpeed, Center_X, Center_Y, End_X, End_Y)
        If dwRet <> 0 Then
            SmcWGetErrorString(dwRet, ErrorString)
            lblComment.Text = "SmcWGetBankArcPoint = " & dwRet & " : " & ErrorString.ToString
            Exit Sub
        End If

        txtArcSpeed.Text = ArcSpeed

        '---------------
        ' Start Motion
        '---------------
        dwRet = SmcWMotionStart(Id, AxisNo)
        If dwRet <> 0 Then
            SmcWGetErrorString(dwRet, ErrorString)
            lblComment.Text = "SmcWMotionStart = " & dwRet & " : " & ErrorString.ToString
            Exit Sub
        End If

        lblComment.Text = "OK "

    End Sub

    Private Sub btnComStop_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnComStop.Click

        dwRet = SmcWMotionStop(Id, AxisNo)
        If dwRet <> 0 Then
            SmcWGetErrorString(dwRet, ErrorString)
            lblComment.Text = "SmcWMotionStop = " & dwRet & " : " & ErrorString.ToString
            Exit Sub
        End If

        lblComment.Text = "OK "

    End Sub

    Private Sub rdoDirCcw_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles rdoDirCcw.CheckedChanged

        ArcStartDir = CSMC_CCW

    End Sub

    Private Sub rdoDirCw_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles rdoDirCw.CheckedChanged

        ArcStartDir = CSMC_CW

    End Sub

    Private Sub BankArc_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim n As Short
        ' Centering
        Me.Left = (Screen.PrimaryScreen.Bounds.Width - Me.Width) / 2
        Me.Top = (Screen.PrimaryScreen.Bounds.Height - Me.Height) / 2
        '-------------
        ' Initialize
        '-------------
        AxisNo = 1
        txtArcSpeed.Text = CStr(10000)
        rdoOptionSingle.Checked = True
        rdoDirCw.Checked = True

        ' AxisNo ComboBox
        cboTargetAxis.Items.Clear()
        For n = 1 To 8
            cboTargetAxis.Items.Add(Str(n))
        Next

        ' ComboBox Initialise
        cboTargetAxis.SelectedIndex = AxisNo - 1

        BankNum = 4
        ' BankNum ComboBox
        cboBankNum.Items.Clear()
        For n = 1 To 4
            cboBankNum.Items.Add(Str(n))
        Next
        ' ComboBox Initialise
        cboBankNum.SelectedIndex = BankNum - 1
        dwRet = SmcWInit(txtDeviceName.Text, Id)
        If dwRet <> 0 Then
            SmcWGetErrorString(dwRet, ErrorString)
            lblComment.Text = "SmcWInit = " & dwRet & " : " & ErrorString.ToString
            Exit Sub
        End If

        lblComment.Text = "OK "

    End Sub

    Function SetBankParam() As Boolean

        Dim InterAxis As Short

        Dim End_Y As Integer
        Dim End_X As Integer
        Dim Center_Y As Integer
        Dim Center_X As Integer
        Dim Coordinates As Short
        Dim ArcSpeed As Double
        Dim Reserved As Short
        Dim BankNo As Short
        Dim AutoEndPointPull As Short
        Dim InterType As Short
        '----------------------------
        ' Set Bank Number to Driver
        '----------------------------
        dwRet = SmcWSetBankNumber(Id, AxisNo, BankNum)
        If dwRet <> 0 Then
            SmcWGetErrorString(dwRet, ErrorString)
            lblComment.Text = "SmcWSetBankNumber = " & dwRet & " : " & ErrorString.ToString
            SetBankParam = False
            Exit Function
        End If

        '----------------------------
        ' Set Interpolation to Driver
        '----------------------------
        InterAxis = (2 ^ (AxisNo - 1)) Or (2 ^ AxisNo)

        'Circular arc interpolation
        InterType = 3

        'Automatically enter an end point pull in operation at the end of arc interpolation operation.
        AutoEndPointPull = 1

        For BankNo = 1 To BankNum
            dwRet = SmcWSetBankInterpolation(Id, AxisNo, BankNo, InterType, InterAxis, Reserved)
            If dwRet <> 0 Then
                SmcWGetErrorString(dwRet, ErrorString)
                lblComment.Text = "SmcWSetBankInterpolation = " & dwRet & " : " & ErrorString.ToString
                SetBankParam = False
                Exit Function
            End If

            dwRet = SmcWSetBankArcParam(Id, AxisNo, BankNo, ArcStartDir, AutoEndPointPull)
            If dwRet <> 0 Then
                SmcWGetErrorString(dwRet, ErrorString)
                lblComment.Text = "SmcWSetBankArcParam = " & dwRet & " : " & ErrorString.ToString
                SetBankParam = False
                Exit Function
            End If
        Next

        '----------------------------
        ' Set Arc Point to Driver
        '----------------------------
        Try
            ArcSpeed = txtArcSpeed.Text
        Catch ex As Exception
            ArcSpeed = 0
        End Try
        Coordinates = 5000

        Center_X = Coordinates
        Center_Y = 0
        If ArcStartDir = CSMC_CW Then
            End_X = Coordinates
            End_Y = Coordinates
        Else
            End_X = Coordinates
            End_Y = (-1) * Coordinates
        End If
        dwRet = SmcWSetBankArcPoint(Id, AxisNo, 1, ArcSpeed, Center_X, Center_Y, End_X, End_Y)
        If dwRet <> 0 Then
            SmcWGetErrorString(dwRet, ErrorString)
            lblComment.Text = "SmcWSetBankArcPoint = " & dwRet & " : " & ErrorString.ToString
            SetBankParam = False
            Exit Function
        End If

        If ArcStartDir = CSMC_CW Then
            Center_X = 0
            Center_Y = (-1) * Coordinates
            End_X = Coordinates
            End_Y = (-1) * Coordinates
        Else
            Center_X = 0
            Center_Y = Coordinates
            End_X = Coordinates
            End_Y = Coordinates
        End If
        dwRet = SmcWSetBankArcPoint(Id, AxisNo, 2, ArcSpeed, Center_X, Center_Y, End_X, End_Y)
        If dwRet <> 0 Then
            SmcWGetErrorString(dwRet, ErrorString)
            lblComment.Text = "SmcWSetBankArcPoint = " & dwRet & " : " & ErrorString.ToString
            SetBankParam = False
            Exit Function
        End If

        Center_X = (-1) * Coordinates
        Center_Y = 0
        If ArcStartDir = CSMC_CW Then
            End_X = (-1) * Coordinates
            End_Y = (-1) * Coordinates
        Else
            End_X = (-1) * Coordinates
            End_Y = Coordinates
        End If
        dwRet = SmcWSetBankArcPoint(Id, AxisNo, 3, ArcSpeed, Center_X, Center_Y, End_X, End_Y)
        If dwRet <> 0 Then
            SmcWGetErrorString(dwRet, ErrorString)
            lblComment.Text = "SmcWSetBankArcPoint = " & dwRet & " : " & ErrorString.ToString
            SetBankParam = False
            Exit Function
        End If

        If ArcStartDir = CSMC_CW Then
            Center_X = 0
            Center_Y = Coordinates
            End_X = (-1) * Coordinates
            End_Y = Coordinates
        Else
            Center_X = 0
            Center_Y = (-1) * Coordinates
            End_X = (-1) * Coordinates
            End_Y = (-1) * Coordinates
        End If
        dwRet = SmcWSetBankArcPoint(Id, AxisNo, 4, ArcSpeed, Center_X, Center_Y, End_X, End_Y)
        If dwRet <> 0 Then
            SmcWGetErrorString(dwRet, ErrorString)
            lblComment.Text = "SmcWSetBankArcPoint = " & dwRet & " : " & ErrorString.ToString
            SetBankParam = False
            Exit Function
        End If

        SetBankParam = True

    End Function

    Private Sub rdoOptionLoop_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles rdoOptionLoop.CheckedChanged

        MotionType = CSMC_LOOP

        lblComment.Text = "OK "

    End Sub

    Private Sub rdoOptionSingle_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles rdoOptionSingle.CheckedChanged

        MotionType = CSMC_SINGLE

        lblComment.Text = "OK "

    End Sub

    Private Sub txtDeviceName_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtDeviceName.TextChanged
        dwRet = SmcWInit(txtDeviceName.Text, Id)
        If dwRet <> 0 Then
            SmcWGetErrorString(dwRet, ErrorString)
            lblComment.Text = "SmcWInit = " & dwRet & " : " & ErrorString.ToString
            Exit Sub
        End If

        lblComment.Text = "OK "

    End Sub
End Class
