I'm trying to do a completely spheric spring in my job. The cnc machines that I use do let you introduce the desired diameter of each coil and the amount of wire that you want to do it.
I can calculate the diameter of each coil manually, but I though that it was going to be the easy thing to create a little script in Autoit to do the job for me.
#include <GDIPlus.au3>
#include <WinAPI.au3>
#include <Constants.au3>
#include <WindowsConstants.au3>
#include <GUIConstants.au3>
#include <GUIConstantsEx.au3>
#include <StructureConstants.au3>
#include <Misc.au3>
#include <Array.au3>
#include <ComboConstants.au3>
#include <StaticConstants.au3>
#include <GuiEdit.au3>
Global Const $M_PI = 3.14159265358979323846
Global $iDecimal = 7
;I do declare the global variables with dummy data to prevent warnings during compilation
Global $possibleballs = 1
Global $finalballs = 1
Global $middlediameterfinal = 1
Global $wire = 1
$hWnd = GUICreate("Calculador Ø", 210, 210); the gui
;$Pic1 = GUICtrlCreatePic("", 20, 10, 298 / 3, 221 / 3)
$label1 = GUICtrlCreateLabel("Wire Ø", 10, 15, 75);an input to ask for the wire to be used.
GUICtrlSetFont(-1, 12)
$input1 = GUICtrlCreateInput("1", 98, 15, 50, 20)
$label2 = GUICtrlCreateLabel("Max Ø", 10, 50, 75); another input asking for the maximum outer diameter of the spring
GUICtrlSetFont(-1, 12)
$input2 = GUICtrlCreateInput("10", 98, 50, 50, 20)
$labelnt = GUICtrlCreateLabel("Nt", 10, 85, 75); total number of coils that we want (depending of the outer diameter you will be tied to use more or less)
GUICtrlSetFont(-1, 12)
$inputnt = GUICtrlCreateInput("10", 98, 85, 50, 20)
$button = GUICtrlCreateButton("CALCULATE", 10, 148 + 35, 90, 22); a button to calculate the maximum allowed coils with the desired wire and outer diameter)
$button2 = GUICtrlCreateButton("PROCEED", 110, 148 + 35, 90, 22); When we have already deerminated the desired coils and outer diameter we proceed to look for the diameter of each coil
$label3 = GUICtrlCreateLabel("", 10, 85 + 35, 190, 25);blank labels to be filled later with data
GUICtrlSetFont(-1, 12)
$label4 = GUICtrlCreateLabel("", 10, 115 + 35, 190, 25);blank labels to be filled later with data
GUICtrlSetFont(-1, 12)
GUIRegisterMsg($WM_COMMAND, "MY_WM_COMMAND"); based on forum search to allow decimal places inside the previous inputs
GUISetState()
While 1
$msg = GUIGetMsg()
Select
Case $msg = $GUI_EVENT_CLOSE
Exit
Case $msg = $button
$Maxdiameter = GUICtrlRead($input2)
$Maxradius = Number($Maxdiameter) / 2 ;with that we do know the radius of the spring
$wire = GUICtrlRead($input1)
$Middlediameter = $Maxdiameter - $wire ; I do calculate an imaginated sphere with the spring's diameter - the diameter of the wire, so every point
; of the new sphere will pass through the middle of the wire
calculate
()
Case $msg = $button2 ;when all the data has been confirmed the I read again the inputs and I do proceed to search the diameter of each coil
$Maxdiameter = GUICtrlRead($input2)
$Maxradius = Number($Maxdiameter) / 2
$wire = GUICtrlRead($input1)
$Middlediameter = $Maxdiameter - $wire
proceed
()
EndSelect
Sleep(20)
WEnd
Func calculate
()
$perimeter = $Middlediameter * $M_PI ; with this I know the lenght of the little sphere that will hold the wire
$possibleballs = $perimeter / $wire ;now we know how many times we can fit the wire inside this sphere
;ConsoleWrite ( $possibleballs & @CRLF)
GUICtrlSetData($label3, "nt = " & (Round($possibleballs / 2, 2))) ;and I do write it in the gui. Notice that I have divided the possible times that wire can
; be fitted in the new sphere because every coil of the spring is going to be formed by 2 times the wire
$finalballs = Round($possibleballs, 0); I round the number of possible balls of wire
;ConsoleWrite ( $finalballs & @CRLF)
$perimeterparcial = $finalballs * $wire ; and I do calculate what do exactly would be the diameter of the spring if you input more coils than the wire that fits
;in the max outer diameter
$middlediameterfinal = $perimeterparcial / $M_PI ; if we do keep the number of coils (even if they don't fit in the max outer diameter
;I do calculate the diamter that a perfect spheric spring would have with this number of coils
;ConsoleWrite ( $middlediameterfinal & @CRLF)
$Maxdiameterfinal = $middlediameterfinal + $wire ;the sring diameter would be the middle diameter of the new sphere plus the diameter of the wire
GUICtrlSetData($label4, "With nt =" & $finalballs / 2 & " Ø =" & Round($Maxdiameterfinal, 4))
;I do inform int he gui about what size would have the spring to be completely shperic with this number of coils
EndFunc ;==>calculate
Func proceed
()
$i = 1 ;a variable to increase during the loop to get the new position of every coil
$degreejump = 360 / (Number(GUICtrlRead($inputnt)) * 2); I calculate in how many degrees are we going to find the middle of each coil
;it is *2 because every coil is going to be formed by 2 wires (one on each side of the sphere)
ConsoleWrite("One wire ball every " & $degreejump & " degree" & @CRLF & @CRLF)
$firstdegree = $degreejump / 2 ;I do that to don't cut th first wire by the half like it is the 12 in a clock.
Do
$diameter0 = $middlediameterfinal * Cos($firstdegree) ; I look for the lenght of the c side of the triangle knowing that the hipotenuse = radius of the middle sphere * cos of the
;A angle (look to the drawing)
$diameter1 = ($diameter0 * 2) + $wire ;so now, knowing the lenght of the c side I multiplicate it by 2 and I add the wire diameter to get the diameter of the spring
;in that point
ConsoleWrite("The outer diameter of the " & $i & " coil is: " & $diameter1 & @CRLF)
$i = $i + 1
;$degreejump = $degreejump * $i
$firstdegree = $firstdegree + $degreejump
Until $firstdegree > 90
Exit
EndFunc ;==>proceed
Func MY
_WM
_COMMAND
($hWnd, $iMsg, $wParam, $lParam)
Local $iIDFrom = BitAND($wParam, 0xFFFF);LoWord
Local $iCode = BitShift($wParam, 16) ;HiWord
If $iIDFrom = $input1 And $iCode = $EN_CHANGE Then
$Read_Input = GUICtrlRead($input1)
If StringRegExp($Read_Input, '[^\d.-]|([{0-9,1}^\A-])[^\d.]') Then $Read_Input = StringRegExpReplace($Read_Input, '[^\d.-]|([{0-9,1}^\A-])[^\d.]', '\1')
$Point1 = StringInStr($Read_Input, ".", 0)
$Point2 = StringInStr($Read_Input, ".", 0, 2)
If $Point2 <> 0 Then $Read_Input = StringLeft($Read_Input, $Point2 - 1)
If $Point1 <> 0 Then $Read_Input = StringLeft($Read_Input, $Point1 + $iDecimal)
GUICtrlSetData($input1, $Read_Input)
EndIf
If $iIDFrom = $input2 And $iCode = $EN_CHANGE Then
$Read_Input2 = GUICtrlRead($input2)
If StringRegExp($Read_Input2, '[^\d.-]|([{0-9,1}^\A-])[^\d.]') Then $Read_Input2 = StringRegExpReplace($Read_Input2, '[^\d.-]|([{0-9,1}^\A-])[^\d.]', '\1')
$Point11 = StringInStr($Read_Input2, ".", 0)
$Point22 = StringInStr($Read_Input2, ".", 0, 2)
If $Point22 <> 0 Then $Read_Input2 = StringLeft($Read_Input2, $Point22 - 1)
If $Point11 <> 0 Then $Read_Input2 = StringLeft($Read_Input2, $Point11 + $iDecimal)
GUICtrlSetData($input2, $Read_Input2)
EndIf
If $iIDFrom = $inputnt And $iCode = $EN_CHANGE Then
$Read_Inputnt = GUICtrlRead($inputnt)
If StringRegExp($Read_Inputnt, '[^\d.-]|([{0-9,1}^\A-])[^\d.]') Then $Read_Inputnt = StringRegExpReplace($Read_Inputnt, '[^\d.-]|([{0-9,1}^\A-])[^\d.]', '\1')
$Point11 = StringInStr($Read_Inputnt, ".", 0)
$Pointntnt = StringInStr($Read_Inputnt, ".", 0, 2)
If $Pointntnt <> 0 Then $Read_Inputnt = StringLeft($Read_Inputnt, $Pointntnt - 1)
If $Point11 <> 0 Then $Read_Inputnt = StringLeft($Read_Inputnt, $Point11 + $iDecimal)
GUICtrlSetData($inputnt, $Read_Inputnt)
EndIf
EndFunc ;==>MY_WM_COMMAND
My goal is to fill an array with the needed diameters in each coil (I can program the machine increasing 1 coil while it is increasing the diameter to the next calculated point).
My problem is that I don't know how to correctly do the looping to get the lenght of the c side of the triangle everytime that I pass from one coil to another.
Thanks a lot for all your help.