Private Function nest2(X As Integer, Y As Integer, a As Integer, b As Integer) As Long
Dim najvec As Long 'most parts calculated
Dim kosi As Long 'number of parts from current loop
Dim vmesni As Long 'an intermitten value for parts to make the calculation easier
Dim zacetni As Long 'a second intermitten value for easier calculation
najvec = 0 'set most to 0
If (a = 0) Then 'check if any of the values = 0. If yes, returns 0.
Exit Function
End If
If (b = 0) Then
Exit Function
End If
If (X = 0) Then
Exit Function
End If
If (Y = 0) Then
Exit Function
End If
If a <= X And b <= Y Then 'if the plate width and legth are smaller than the part, then stop the calculation as a bigger part can not be nested on to a smaller plate
For i = 1 To (X \ a) 'loop though all possible rows of one orientation
zacetni = i * (Y \ b) 'calculates the number of parts for one orientation
vmesni = ((X - (i * a)) \ b) * (Y \ a) 'calculate the number of parts for the other orientation with the width reduced by the parts from the first orientation
kosi = zacetni + vmesni 'number of larts = number of part for one orientation and number of parts for the other orientation on the remainder
If kosi > najvec Then 'check if new number of parts is bigger than the max number and change accordingly
najvec = kosi
End If
Next i
End If
nest2 = najvec 'return the biggest possible outcome
End Function
Function nest(pŠirina As Integer, pDolžina As Integer, kŠirina As Integer, kDolžina As Integer) As Long 'pŠirina, pDolžina ,eans pWidth and pLength same for k. P stands for plate while k stands for kos(part in english)
Dim kosi As Long
Dim kosi2 As Long
nest = nest2(pŠirina, pDolžina, kŠirina, kDolžina) 'check p-width and p-length with k-width and k-length
kosi2 = nest2(pDolžina, pŠirina, kŠirina, kDolžina) 'reverse the p direction and compare to k again
If nest > kosi2 Then 'return the bigger of the 2 possible variations
Else
nest = kosi2
End If
End Function