Quantcast
Channel: VBForums
Viewing all articles
Browse latest Browse all 15019

Capture X & Y coordinates in an array and pass to a public sub for reuse

$
0
0
Hi. I am using Visual Studio Community 2017. I've created a Windows Form application written in Visual Basic.
I want to have users input dimensions of a rectangle. I then create CNC mill code to cut the profile of the rectangle - This works great.
I then want to plot the outside profile of the rectangle. I created an array called xy(10,1) to capture the X & Y coordinates. The array indexes are loaded after writing each line of the cnc code to a text file. I then want to transfer this array to a public sub where I can read the array when a button is selected.
I would prefer to read the array without transferring to a public sub. Maybe I should make the "Private Sub BtnRPPostCode_Click" a Public sub?
I get the red squiggly line under the array name "xypos" in the following line of code, Me.PublicizePlotPts(xypos).
The PublicizePlotPts is a Public sub that I want to pass the array to that I can read later.
This is probably not the correct way to do this, any help would be great.
Apologies in advance if I messed up how to capture the code in the post.
Thank you,
Ophelia.

Code:

Imports System.IO
Imports System.Runtime.Serialization
Imports Microsoft.Office.Interop


Public Class Rectangular_Profile

    Private Sub BtnClearValues_Click(sender As Object, e As EventArgs) Handles BtnClearValues.Click
        'Clears Input Values and Moves focus to "Length(X)" entry box

        TxtXLen.Text = ""
        TxtYWdth.Text = ""
        TxtZDepth.Text = ""
        TxtXStrtPt.Text = ""
        TxtYStrtPt.Text = ""
        TxtCrnrRad.Text = ""
        TxtCutrDia.Text = ""
        TxtCutrRpm.Text = ""
        TxtFdRate.Text = ""
        TxtTlNum.Text = ""
        TxtLdOnOffRad.Text = ""
        TxtXLen.Select()

    End Sub

    Private Sub BtnCancel_Click(sender As Object, e As EventArgs) Handles BtnCancel.Click
        'btnExit exits the form and closes the application
        Me.Close()
        'Application.Exit()
    End Sub

    Private Sub BtnOK_Click(sender As Object, e As EventArgs) Handles BtnOK.Click
        'If isValidDate() Then
        ' Me.SaveData()
        'End If
        Me.Close()
    End Sub

    Private Sub BtnRPPostCode_Click(sender As Object, e As EventArgs) Handles BtnRPPostCode.Click
        'Check for Empty Input Boxes
        Dim EmptyTextBoxFound As Boolean = False 'Boolean flag for empty textbox
        Dim EmptyTextBoxName As String = ""
        For Each ctl As Control In Me.Controls
            If TypeOf ctl Is TextBox And ctl.Text.Length = 0 Then
                EmptyTextBoxName = ctl.Name
                EmptyTextBoxFound = True
                'Exit For
                If EmptyTextBoxFound = True Then
                    'Output Warning that a Value is Missing
                    MessageBox.Show("An input Box is Empty", "ENTRY ERROR")
                    ctl.Select()
                    Exit Sub
                End If
            End If
        Next

        'Define an array to capture X & Y coordinates
        Dim xy(,) As Decimal = New Decimal(10, 1) {}
        Dim xypos(,) As Decimal = New Decimal(10, 1) {}


        'Define Variables to Store Input Values, "rp" = Rectangular Profile
        Dim rpXLength As Decimal = CDec(TxtXLen.Text)
        Dim rpYWidth As Decimal = CDec(TxtYWdth.Text)
        Dim rpZDepth As Decimal = CDec(TxtZDepth.Text)
        'Dim rpZDepth As Decimal = Convert.ToDecimal(txtZDepth.Text)
        Dim rpXStrt As Decimal = CDec(TxtXStrtPt.Text)
        Dim rpYStrt As Decimal = CDec(TxtYStrtPt.Text)
        Dim rpCrnrRad As Decimal = CDec(TxtCrnrRad.Text)
        Dim rpCutrDia As Decimal = CDec(TxtCutrDia.Text)
        Dim rpCutrRPM As Integer = CInt(TxtCutrRpm.Text)
        Dim rpFdRate As Decimal = CDec(TxtFdRate.Text)
        Dim rpldRadOnOff As Decimal = CDec(TxtLdOnOffRad.Text)
        Dim rpTlNum As Integer = CInt(TxtTlNum.Text)

        'Calculate X & Y Start Point
        Dim rpXStrtPt As Decimal = 0 + rpXStrt - (rpCutrDia / 2) - rpldRadOnOff
        Dim rpYStrtPt As Decimal = 0 + rpYStrt + (rpCutrDia / 2) + 0.1
        rpZDepth = 0 + rpZDepth

        'Convert some decimal values back to Strings in correct format
        '---- Empty as of 3/21/21 ----

        'Open File Path and Create File for Output.
        'Exceptions and Error Handling To Be Added Later
        Dim path As String = lblF2DirNm.Text & "\" & lblF2FileNm.Text & ".txt"
        Dim textout As New StreamWriter(
                      New FileStream(path, FileMode.Create, FileAccess.Write))

        textout.WriteLine("%")
        textout.WriteLine("O" & lblF2ProgNum.Text)
        textout.WriteLine("(" & path & ", " & DateTime.Now & ")")
        textout.WriteLine("T" & rpTlNum & "M6")
        textout.WriteLine("S" & rpCutrRPM & "M3")
        textout.WriteLine("G90G54G0X" & rpXStrtPt & "Y" & rpYStrtPt)
        xy(0, 0) = rpXStrtPt
        xy(0, 1) = rpYStrtPt
        textout.WriteLine("G43Z0.1H" & rpTlNum)
        textout.WriteLine("G1Z" & rpZDepth & "F" & rpFdRate)
        textout.WriteLine("G41Y" & (0 + rpYStrt + (rpCutrDia / 2)) & "D" & rpTlNum)
        xy(1, 0) = rpXStrt
        xy(1, 1) = (0 + rpYStrt + (rpCutrDia / 2))
        textout.WriteLine("X" & (0 + rpXStrt + rpXLength - rpCrnrRad))
        xy(2, 0) = (0 + rpXStrt + rpXLength - rpCrnrRad)
        xy(2, 1) = xy(1, 1)
        textout.WriteLine("G2X" & (0 + rpXStrt + rpXLength + (rpCutrDia / 2)) &
                          "Y" & (0 + rpYStrt - rpCrnrRad) & "J-" & (rpCrnrRad + (rpCutrDia / 2)))
        xy(3, 0) = (0 + rpXStrt + rpXLength + (rpCutrDia / 2))
        xy(3, 1) = (0 + rpYStrt - rpCrnrRad)
        textout.WriteLine("G1Y" & (0 + rpYStrt + rpYWidth + rpCrnrRad))
        xy(4, 0) = xy(3, 0)
        xy(4, 1) = (0 + rpYStrt + rpYWidth + rpCrnrRad)
        textout.WriteLine("G2X" & (0 + rpXStrt + rpXLength - rpCrnrRad) &
                          "Y" & (0 + rpYStrt + rpYWidth - (rpCutrDia / 2)) & "I-" & (rpCrnrRad + (rpCutrDia / 2)))
        xy(5, 0) = (0 + rpXStrt + rpXLength - rpCrnrRad)
        xy(5, 1) = (0 + rpYStrt + rpYWidth - (rpCutrDia / 2))
        textout.WriteLine("G1X" & (0 + rpXStrt + rpCrnrRad))
        xy(6, 0) = (0 + rpXStrt + rpCrnrRad)
        xy(6, 1) = xy(5, 1)
        textout.WriteLine("G2X" & (0 + rpXStrt - (rpCutrDia / 2)) &
                          "Y" & (0 + rpYStrt + rpYWidth + rpCrnrRad) & "J" & (rpCrnrRad + (rpCutrDia / 2)))
        xy(7, 0) = (0 + rpXStrt - (rpCutrDia / 2))
        xy(7, 1) = (0 + rpYStrt + rpYWidth + rpCrnrRad)
        textout.WriteLine("G1Y" & (0 + rpYStrt - rpCrnrRad))
        xy(8, 0) = xy(7, 0)
        xy(8, 1) = (0 + rpYStrt - rpCrnrRad)
        textout.WriteLine("G2X" & (0 + rpXStrt + rpCrnrRad) &
                          "Y" & (0 + rpYStrt + (rpCutrDia / 2)) & "I-" & (rpCrnrRad + (rpCutrDia / 2)))
        xy(9, 0) = (0 + rpXStrt + rpCrnrRad)
        xy(9, 1) = (0 + rpYStrt + (rpCutrDia / 2))
        textout.WriteLine("G3X" & (0 + rpXStrt + rpCrnrRad + rpldRadOnOff) &
                          "Y" & (0 + rpYStrt + (rpCutrDia / 2) + rpldRadOnOff) & "J" & rpldRadOnOff)
        xy(10, 0) = (0 + rpXStrt + rpCrnrRad + rpldRadOnOff)
        xy(10, 1) = (0 + rpYStrt + (rpCutrDia / 2) + rpldRadOnOff)
        textout.WriteLine("G40G0Z0.1M9")
        textout.WriteLine("G91G28G0Z0M5")
        textout.WriteLine("G28X0Y0")
        textout.WriteLine("M30")
        textout.WriteLine("%")

        textout.Close()

        'Copy array xy to array xypos
        Array.Copy(xy, xypos, xy.Length)
        Me.PublicizePlotPts(xypos)
    End Sub

    Public Sub PublicizePlotPts(ByRef xypos() As Decimal)
        For i As Integer = 0 To xypos.Length - 1
            xypos(i) = i
        Next
    End Sub

    Private Sub Rectangular_Profile_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        lblF2DirNm.Text = StartScreen.lblFileDir.Text
        lblF2FileNm.Text = StartScreen.txtFileName.Text
        lblF2OpNum.Text = StartScreen.txtOpNum.Text
        lblF2ProgNum.Text = StartScreen.txtProgNum.Text
    End Sub

    Private Sub BtnToolLib_Click(sender As Object, e As EventArgs) Handles BtnToolLib.Click
        'Opens Tool_Library.xlsx to get tool info.
        'Need to be able to load Tl_Num & Tl_Dia values from Excel spreadsheet
        Process.Start("Excel", "C:\Users\Jay\source\repos\GCodeGenerator\StartScreen\bin\Debug\Tool_Library.xlsx")

    End Sub

    Private Sub TxtXLen_Keypress(sender As Object, e As EventArgs) Handles TxtXLen.KeyPress
        DecimalOnly(e)
    End Sub

    Private Sub TxtYWdth_Keypress(sender As Object, e As EventArgs) Handles TxtYWdth.KeyPress
        DecimalOnly(e)
    End Sub

    Private Sub TxtZDepth_Keypress(sender As Object, e As EventArgs) Handles TxtZDepth.KeyPress
        DecimalOnly(e)
    End Sub

    Private Sub TxtXStrtPt_Keypress(sender As Object, e As EventArgs) Handles TxtXStrtPt.KeyPress
        DecimalOnly(e)
    End Sub

    Private Sub TxtYStrtPt_Keypress(sender As Object, e As EventArgs) Handles TxtYStrtPt.KeyPress
        DecimalOnly(e)
    End Sub

    Private Sub TxtCrnrRad_Keypress(sender As Object, e As EventArgs) Handles TxtCrnrRad.KeyPress
        DecimalOnly(e)
    End Sub

    Private Sub TxtCutrDia_Keypress(sender As Object, e As EventArgs) Handles TxtCutrDia.KeyPress
        DecimalOnly(e)
    End Sub

    Private Sub TxtFdRate_Keypress(sender As Object, e As EventArgs) Handles TxtFdRate.KeyPress
        DecimalOnly(e)
    End Sub

    Private Sub TxtLdOnOffRad_Keypress(sender As Object, e As EventArgs) Handles TxtLdOnOffRad.KeyPress
        DecimalOnly(e)
    End Sub

    Public Sub DecimalOnly(ByVal e As System.Windows.Forms.KeyPressEventArgs)
        'MsgBox("Test Keypress")
        If (Asc(e.KeyChar)) >= 48 And (Asc(e.KeyChar)) <= 57 Or
          (Asc(e.KeyChar)) = 46 Or
          (Asc(e.KeyChar)) = 8 Or
          (Asc(e.KeyChar)) = 45 Then
            '(Asc(e.KeyChar)) >= 48 And (Asc(e.KeyChar)) <= 57 // Allow numbers 0 to 9
            '(Asc(e.KeyChar)) = 46 // Allow Decimal point
            '(Asc(e.KeyChar)) = 8 // Allow BackSpace
            '(Asc(e.KeyChar)) = 45 // Allow Negative sign (-)
        Else
            e.Handled = True
            MsgBox("Allow Numbers, Decimals and BKSP Only !!!")
        End If
    End Sub

    Private Sub TxtCutrRpm_Keypress(sender As Object, e As EventArgs) Handles TxtCutrRpm.KeyPress
        IntegerOnly(e)
    End Sub

    Private Sub TxtTlNum_Keypress(sender As Object, e As EventArgs) Handles TxtTlNum.KeyPress
        IntegerOnly(e)
    End Sub

    Public Sub IntegerOnly(ByVal e As System.Windows.Forms.KeyPressEventArgs)
        'MsgBox("Test Keypress")
        If (Asc(e.KeyChar)) >= 48 And (Asc(e.KeyChar)) <= 57 Or (Asc(e.KeyChar)) = 8 Then
            '(Asc(e.KeyChar)) >= 48 And (Asc(e.KeyChar)) <= 57 // Allow numbers 0 to 9
            '(Asc(e.KeyChar)) = 8 // Allow BackSpace
        Else
            e.Handled = True
            MsgBox("Allow Numbers and BKSP Only !!!")
        End If
    End Sub

    Private Sub BtnRPPlot_Click(sender As Object, e As EventArgs) Handles BtnRPPlot.Click
        'Open drawing window
        GraphicWindow.Show()
    End Sub
End Class


Viewing all articles
Browse latest Browse all 15019

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>