Quesney

First Autoit script

5 posts in this topic

#1 ·  Posted

Below is my first script in AutoIt looking for critique on the code. I am learning through reverse engineering of example codes, I am use to VBA so some of the functions and verbiage has me confused here.

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Icon=C:\Program Files (x86)\AutoIt3\Icons\MyAutoIt3_Green.ico
#AutoIt3Wrapper_Res_Comment=Created for use at Ariel Corporation
#AutoIt3Wrapper_Res_Description=Identify the allocated length of a finished part
#AutoIt3Wrapper_Res_Fileversion=0.0.0.1
#AutoIt3Wrapper_Res_Fileversion_AutoIncrement=y
#AutoIt3Wrapper_Res_LegalCopyright=2017 Ariel Corporation
#AutoIt3Wrapper_Res_Language=1033
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
#cs ----------------------------------------------------------------------------

 AutoIt Version: 3.3.14.2
 Author:         David Chaney

 Script Function:
    Identify the allocated length of a finished part

#ce ----------------------------------------------------------------------------

; Script Start - Add your code below here
#include <GUIConstantsEx.au3>
#include <ColorConstantS.au3>
#include <MsgBoxConstants.au3>

RadioRequest()

Func RadioRequest()

    GUICreate("Allocated Length Calculator", 350, 150) ; Creates a dialog box
    GUISetBkColor($COLOR_WHITE) ; will change background color
    Local $idButton1 = GUICtrlCreateButton("Criteria", 10, 120, 100, 20)
    Local $idRadio1 = GUICtrlCreateRadio("Less than 4in diameter and less than 2in long", 10, 10, 250, 20)
    Local $idRadio2 = GUICtrlCreateRadio("Less than 4in diameter and greater than 2in long", 10, 30, 250, 20)
    Local $idRadio3 = GUICtrlCreateRadio("Between 4in to 12in diameter and less than 2in long", 10, 50, 275, 20)
    Local $idRadio4 = GUICtrlCreateRadio("Between 4in to 12in diameter and greater 2in long", 10, 70, 250, 20)
    Local $idRadio5 = GUICtrlCreateRadio("Greater than 12in diameter", 10, 90, 250, 20)

    GUISetState(@SW_SHOW)

    Local $idMsg
    ; Loop until the user exits.
    While 1
        $idMsg = GUIGetMsg()
        Select
            Case $idMsg = $GUI_EVENT_CLOSE
                ExitLoop
             Case $idMsg = $idRadio1 And BitAND(GUICtrlRead($idRadio1), $GUI_CHECKED) = $GUI_CHECKED
                GUISetState(@SW_HIDE)
                Calc_Radio1()
                Exit
             Case $idMsg = $idRadio2 And BitAND(GUICtrlRead($idRadio2), $GUI_CHECKED) = $GUI_CHECKED
                GUISetState(@SW_HIDE)
                Calc_Radio2()
                Exit
             Case $idMsg = $idRadio3 And BitAND(GUICtrlRead($idRadio3), $GUI_CHECKED) = $GUI_CHECKED
                GUISetState(@SW_HIDE)
                Calc_Radio3()
                Exit
             Case $idMsg = $idRadio4 And BitAND(GUICtrlRead($idRadio4), $GUI_CHECKED) = $GUI_CHECKED
                GUISetState(@SW_HIDE)
                Calc_Radio4()
                Exit
             Case $idMsg = $idRadio5 And BitAND(GUICtrlRead($idRadio5), $GUI_CHECKED) = $GUI_CHECKED
                GUISetState(@SW_HIDE)
                Calc_Radio5()
                Exit
             Case $idMsg = $idButton1
                MsgBox($MB_SYSTEMMODAL, "Formula Criteria", "Example: 6.5"" Diameter and 22.931"" Finished length" & @CRLF & @CRLF & _
                                                            "1) Add 0.25"" to barstock for machining" & @CRLF & _
                                                            "2) Add 0.08"" for saw blade width" & @CRLF & _
                                                            "     A) 22.931"" + 0.25"" + 0.08"" = 23.261""" & @CRLF & @CRLF & _
                                                            "3) Barstock is generally 144"" long" & @CRLF & _
                                                            "     A) 144"" / 23.261"" = 6.190 (6 parts per 144"")" & @CRLF & _
                                                            "     B) 6 * 23.261"" = 139.566"" (Not enough stock for 7 parts)" & @CRLF & _
                                                            "     C) 144"" / 6 = 24"" Allocated Length")
        EndSelect
    WEnd
 EndFunc

Func Calc_Radio1()
    ; Places the input box
    Local $sAnswer = InputBox("Finished Length", "Enter finished part length" & @CRLF & @CRLF &  "NOTE:" & @CRLF & "Part should have a " & _
                                                 "diameter less than 4"" and finished length less than 2""", "", "")

    ; Display the result.
    MsgBox($MB_SYSTEMMODAL, "Allocated Length", "For a part with a diameter less than 4"" and a finished length of " & $sAnswer & """ the " & _
                            "allocated length is "  & Round(144/floor(142/($sAnswer+0.125+0.08)),4) & """")
 EndFunc

Func Calc_Radio2()
    ; Places the input box
    Local $sAnswer = InputBox("Finished Length", "Enter finished part length" & @CRLF & @CRLF &  "NOTE:" & @CRLF & "Part should have a " & _
                              "diameter less than 4"" and finished length greater than 2""", "", "")

    ; Display the result.
    MsgBox($MB_SYSTEMMODAL, "Allocated Length", "For a part with a diameter less than 4"" and a finished length of " & $sAnswer & """ the " & _
                                                "allocated length is " & Round(144/floor(144/($sAnswer+0.125+0.08)),4) & """")
 EndFunc

Func Calc_Radio3()
    ; Places the input box
    Local $sAnswer = InputBox("Finished Length", "Enter finished part length" & @CRLF & @CRLF &  "NOTE:" & @CRLF & "Part should have a " & _
                              "diameter between 4"" and 12"" and finished length less than 2""", "", "")

    ; Display the result.
    MsgBox($MB_SYSTEMMODAL, "Allocated Length", "For a part with a diameter between 4"" and 12"" and a finished length of " & $sAnswer & _
                                                """ the allocated length is "& Round(144/floor(142/($sAnswer+0.25+0.08)),4) & """")
 EndFunc

Func Calc_Radio4()
    ; Places the input box
    Local $sAnswer = InputBox("Finished Length", "Enter finished part length" & @CRLF & @CRLF &  "NOTE:" & @CRLF & "Part should have a " & _
                                                 "diameter between 4"" and 12"" and finished length greater than 2""", "", "")

    ; Display the result.
    MsgBox($MB_SYSTEMMODAL, "Allocated Length", "For a part with a diameter between 4"" and 12"" and a finished length of " & $sAnswer & _
                                                """ the allocated length is " & Round(144/floor(144/($sAnswer+0.25+0.08)),4) & """")
 EndFunc

Func Calc_Radio5()
    ; Places the input box
    Local $sAnswer = InputBox("Finished Length", "Enter finished part length" & @CRLF & @CRLF &  "NOTE:" & @CRLF & "Part should have a " & _
                                                 "diameter greater than 12""", "", "")

    ; Display the result.
    MsgBox($MB_SYSTEMMODAL, "Allocated Length", "For a part with a diameter greater then 12"" and a finished length of " & $sAnswer & _
                                                """ the allocated length is " & Round(144/floor(144/($sAnswer+0.375+0.08)),4) & """")
EndFunc

 


“Courage is being scared to death, but saddling up anyway”John Wayne

Share this post


Link to post
Share on other sites



#2 ·  Posted

Firstly, welcome to the forums and well done learning and borrowing from examples.  That is how I started (looking at examples and tweaking them to see what impacts/results would happen).  

I have a couple of suggestions/critiques for ya.

For one thing, I see you trigger the next part of your script (inputbox) after selecting a radio button.  In your select statement, to evaluate the result from GUIGetMsg, I notice you check which control and the state of the control.  Checking the state is pretty erroneous in my opinion; just Case = $idRadio# would be sufficient.  Now, if you want to reduce user error, you might want to consider another button (OK, Apply, what-have-you) which then checks to see which radio button is checked...

Secondly, this is more of an efficient code writing practice...you build a function for each radio button case.  There is a lot of redundant code, particularly in the messages and structure.  I do see that there is slight variation on the calculations which is probably the most important part to you.  I would recommend trying to make a single function which parameters to alter the input and msg specifications as necessary.  This style of coding is dubbed modular.  The intent is to eleiminate as much redundant code as feasible and adapt it to work with the necessary use cases.  Here is a post I made to describe modular coding; might be worth a quick read.  My recommendation would be create a function to handle the input, but depending on which control "activates/calls" it, set a parameter that will determine the messages and calculations appropriate.

Keep up the scripting. ;)

1 person likes this

Share this post


Link to post
Share on other sites

#3 ·  Posted

Thanks a lot for the advice, looking into it all right now. Just like VBA so many different ways to accomplish the same results


“Courage is being scared to death, but saddling up anyway”John Wayne

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Here is another method using an Array, still requires some work though:

#include <Array.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <ComboConstants.au3>
#include <StaticConstants.au3>

Global $iCalculate = 0
Global $aCombo[6][3]
    $aCombo[0][0] = 'Less than 4in diameter and less than 2in long'
    $aCombo[0][1] = 'Part should have a diameter less than 4" and finished length less than 2"'
    $aCombo[0][2] = 'Round(144/floor(142/($iCalculate + 0.125 + 0.08)), 4)'
    $aCombo[1][0] = 'Less than 4" diameter and greater than 2" long'
    $aCombo[1][1] = 'Part should have a diameter less than 4" and finished length greater than 2"'
    $aCombo[1][2] = 'Round(144/floor(144/($iCalculate + 0.125 + 0.08)), 4)'
    $aCombo[2][0] = 'Between 4" to 12" diameter and less than 2" long'
    $aCombo[2][1] = 'Part should have a diameter between 4" and 12" and finished length less than 2"'
    $aCombo[2][2] = 'Round(144/floor(142/($iCalculate + 0.25 + 0.08)), 4)'
    $aCombo[3][0] = 'Between 4" to 12" diameter and greater 2" long'
    $aCombo[3][1] = 'Part should have a diameter between 4" and 12" and finished length greater than 2"'
    $aCombo[3][2] = 'Round(144/floor(144/($iCalculate + 0.25 + 0.08)), 4)'
    $aCombo[4][0] = 'Greater than 12in diameter'
    $aCombo[4][1] = 'Part should have a diameter greater than 12"'
    $aCombo[4][2] = 'Round(144/floor(144/($iCalculate + 0.375 + 0.08)), 4)'

RadioRequest()

Func RadioRequest()
    Local $sCombo, $sComboSelect = $aCombo[0][0]

    GUICreate("Allocated Length Calculator", 270, 225) ; Creates a dialog box
    GUISetBkColor(0xFFFFFF) ; will change background color
    Local $idCombo = GUICtrlCreateCombo("", 10, 10, 250, 25, $CBS_DROPDOWNLIST)
        GUICtrlSetData($idCombo, _ArrayToString($aCombo, "|", -1, -1, "|", 0, 0), $aCombo[0][0])
    GUICtrlCreateLabel("Enter finished part length:", 10, 40, 140, 21, $SS_CENTERIMAGE)
        Local $idPartLength = GUICtrlCreateInput("", 155, 40, 105, 21)
    GUICtrlCreateLabel("Answer:", 10, 65, 140, 21, $SS_CENTERIMAGE)
        Local $idAnswer = GUICtrlCreateInput("", 155, 65, 105, 21, $ES_READONLY)
    Local $idCalculate = GUICtrlCreateButton("Calculate", 10, 90, 250, 30)
    Local $idGroup = GUICtrlCreateGroup('Notes:', 5, 125, 260, 60)
    Local $idLabel = GUICtrlCreateLabel('', 10, 145, 250, 30)
        GUICtrlSetData($idLabel, $aCombo[0][1])
        GUICtrlSetBkColor(-1, $GUI_BKCOLOR_TRANSPARENT)
    Local $idButton1 = GUICtrlCreateButton("Criteria", 5, 195, 100, 20)

    GUISetState(@SW_SHOW)

    Local $idMsg
    ; Loop until the user exits.
    While 1
        $idMsg = GUIGetMsg()
        Switch $idMsg
            Case $GUI_EVENT_CLOSE
                ExitLoop
            Case $idCombo
                $sCombo = GUICtrlRead($idCombo)
                If $sCombo <> $sComboSelect Then
                    $sComboSelect = $sCombo
                    Switch $sCombo
                        Case $aCombo[0][0]
                            GUICtrlSetData($idLabel, $aCombo[0][1])
                        Case $aCombo[1][0]
                            GUICtrlSetData($idLabel, $aCombo[1][1])
                        Case $aCombo[2][0]
                            GUICtrlSetData($idLabel, $aCombo[2][1])
                        Case $aCombo[3][0]
                            GUICtrlSetData($idLabel, $aCombo[3][1])
                        Case $aCombo[4][0]
                            GUICtrlSetData($idLabel, $aCombo[4][1])
                        Case $aCombo[5][0]
                            GUICtrlSetData($idLabel, $aCombo[5][1])
                        Case Else
                    EndSwitch
                EndIf
            Case $idCalculate
                $iCalculate = Number(GUICtrlRead($idPartLength))
                $sCombo = GUICtrlRead($idCombo)
                Switch $sCombo
                    Case $aCombo[0][0]
                        GUICtrlSetData($idAnswer, Execute($aCombo[0][2]))
                    Case $aCombo[1][0]
                        GUICtrlSetData($idAnswer, Execute($aCombo[1][2]))
                    Case $aCombo[2][0]
                        GUICtrlSetData($idAnswer, Execute($aCombo[2][2]))
                    Case $aCombo[3][0]
                        GUICtrlSetData($idAnswer, Execute($aCombo[3][2]))
                    Case $aCombo[4][0]
                        GUICtrlSetData($idAnswer, Execute($aCombo[4][2]))
                    Case $aCombo[5][0]
                        GUICtrlSetData($idAnswer, Execute($aCombo[5][2]))
                    Case Else
                EndSwitch
            Case $idButton1
                MsgBox(48, "Formula Criteria", "Example: 6.5"" Diameter and 22.931"" Finished length" & @CRLF & @CRLF & _
                                                            "1) Add 0.25"" to barstock for machining" & @CRLF & _
                                                            "2) Add 0.08"" for saw blade width" & @CRLF & _
                                                            "     A) 22.931"" + 0.25"" + 0.08"" = 23.261""" & @CRLF & @CRLF & _
                                                            "3) Barstock is generally 144"" long" & @CRLF & _
                                                            "     A) 144"" / 23.261"" = 6.190 (6 parts per 144"")" & @CRLF & _
                                                            "     B) 6 * 23.261"" = 139.566"" (Not enough stock for 7 parts)" & @CRLF & _
                                                            "     C) 144"" / 6 = 24"" Allocated Length")
        EndSwitch
    WEnd
 EndFunc

Edit:

Removed miscellaneous code used for debugging
Added $sComboSelect = $sCombo to detect changes in ComboBox

Edited by Subz

Share this post


Link to post
Share on other sites

#5 ·  Posted

26 minutes ago, Subz said:

Here is another method using an Array, still requires some work though:

#include <Array.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <ComboConstants.au3>
#include <StaticConstants.au3>

Global $iCalculate = 0
Global $aCombo[6][4]
    $aCombo[0][0] = 'Less than 4in diameter and less than 2in long'
    $aCombo[0][1] = 'Part should have a diameter less than 4" and finished length less than 2"'
    $aCombo[0][2] = 'Round(144/floor(142/($iCalculate + 0.125 + 0.08)), 4)'
    $aCombo[1][0] = 'Less than 4" diameter and greater than 2" long'
    $aCombo[1][1] = 'Part should have a diameter less than 4" and finished length greater than 2"'
    $aCombo[1][2] = 'Round(144/floor(144/($iCalculate + 0.125 + 0.08)), 4)'
    $aCombo[2][0] = 'Between 4" to 12" diameter and less than 2" long'
    $aCombo[2][1] = 'Part should have a diameter between 4" and 12" and finished length less than 2"'
    $aCombo[2][2] = 'Round(144/floor(142/($iCalculate + 0.25 + 0.08)), 4)'
    $aCombo[3][0] = 'Between 4" to 12" diameter and greater 2" long'
    $aCombo[3][1] = 'Part should have a diameter between 4" and 12" and finished length greater than 2"'
    $aCombo[3][2] = 'Round(144/floor(144/($iCalculate + 0.25 + 0.08)), 4)'
    $aCombo[4][0] = 'Greater than 12in diameter'
    $aCombo[4][1] = 'Part should have a diameter greater than 12"'
    $aCombo[4][2] = 'Round(144/floor(144/($iCalculate + 0.375 + 0.08)), 4)'

RadioRequest()

Func RadioRequest()
    Local $sCombo, $sComboSelect = $aCombo[0][0]

    GUICreate("Allocated Length Calculator", 270, 225) ; Creates a dialog box
    GUISetBkColor(0xFFFFFF) ; will change background color
    Local $idCombo = GUICtrlCreateCombo("", 10, 10, 250, 25, $CBS_DROPDOWNLIST)
        GUICtrlSetData($idCombo, _ArrayToString($aCombo, "|", -1, -1, "|", 0, 0), $aCombo[0][0])
    GUICtrlCreateLabel("Enter finished part length:", 10, 40, 140, 21, $SS_CENTERIMAGE)
        Local $idPartLength = GUICtrlCreateInput("", 155, 40, 105, 21)
    GUICtrlCreateLabel("Answer:", 10, 65, 140, 21, $SS_CENTERIMAGE)
        Local $idAnswer = GUICtrlCreateInput("", 155, 65, 105, 21, $ES_READONLY)
    Local $idCalculate = GUICtrlCreateButton("Calculate", 10, 90, 250, 30)
    Local $idGroup = GUICtrlCreateGroup('Notes:', 5, 125, 260, 60)
    Local $idLabel = GUICtrlCreateLabel('', 10, 145, 250, 30)
        GUICtrlSetData($idLabel, $aCombo[0][1])
        GUICtrlSetBkColor(-1, $GUI_BKCOLOR_TRANSPARENT)
    Local $idButton1 = GUICtrlCreateButton("Criteria", 5, 195, 100, 20)

    GUISetState(@SW_SHOW)

    Local $idMsg
    ; Loop until the user exits.
    While 1
        $idMsg = GUIGetMsg()
        Switch $idMsg
            Case $GUI_EVENT_CLOSE
                ExitLoop
            Case $idCombo
                $sCombo = GUICtrlRead($idCombo)
                If $sCombo <> $sComboSelect Then
                    Switch $sCombo
                        Case $aCombo[0][0]
                            GUICtrlSetData($idLabel, $aCombo[0][1])
                        Case $aCombo[1][0]
                            GUICtrlSetData($idLabel, $aCombo[1][1])
                        Case $aCombo[2][0]
                            GUICtrlSetData($idLabel, $aCombo[2][1])
                        Case $aCombo[3][0]
                            GUICtrlSetData($idLabel, $aCombo[3][1])
                        Case $aCombo[4][0]
                            GUICtrlSetData($idLabel, $aCombo[4][1])
                        Case $aCombo[5][0]
                            GUICtrlSetData($idLabel, $aCombo[5][1])
                        Case Else
                    EndSwitch
                EndIf
            Case $idCalculate
                $iCalculate = Number(GUICtrlRead($idPartLength))
                ConsoleWrite($iCalculate & @CRLF)
                $sCombo = GUICtrlRead($idCombo)
                Switch $sCombo
                    Case $aCombo[0][0]
                        GUICtrlSetData($idAnswer, Execute($aCombo[0][2]))
                    Case $aCombo[1][0]
                        GUICtrlSetData($idAnswer, Execute($aCombo[1][2]))
                    Case $aCombo[2][0]
                        GUICtrlSetData($idAnswer, Execute($aCombo[2][2]))
                    Case $aCombo[3][0]
                        GUICtrlSetData($idAnswer, Execute($aCombo[3][2]))
                    Case $aCombo[4][0]
                        GUICtrlSetData($idAnswer, Execute($aCombo[4][2]))
                    Case $aCombo[5][0]
                        GUICtrlSetData($idAnswer, Execute($aCombo[5][2]))
                    Case Else
                EndSwitch
            Case $idButton1
                MsgBox(48, "Formula Criteria", "Example: 6.5"" Diameter and 22.931"" Finished length" & @CRLF & @CRLF & _
                                                            "1) Add 0.25"" to barstock for machining" & @CRLF & _
                                                            "2) Add 0.08"" for saw blade width" & @CRLF & _
                                                            "     A) 22.931"" + 0.25"" + 0.08"" = 23.261""" & @CRLF & @CRLF & _
                                                            "3) Barstock is generally 144"" long" & @CRLF & _
                                                            "     A) 144"" / 23.261"" = 6.190 (6 parts per 144"")" & @CRLF & _
                                                            "     B) 6 * 23.261"" = 139.566"" (Not enough stock for 7 parts)" & @CRLF & _
                                                            "     C) 144"" / 6 = 24"" Allocated Length")
        EndSwitch
    WEnd
 EndFunc

 

That looks crazy, lol way above my level thus far, but thank you as well... another method for me to review and learn :p) So far loving the site and the program...


“Courage is being scared to death, but saddling up anyway”John Wayne

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now