Improved the algorithm to find cut the frames in the best manor

This commit is contained in:
Anton 2021-04-08 13:18:44 +02:00
parent 16f2c82142
commit df4726889a
2 changed files with 120 additions and 9 deletions

Binary file not shown.

View File

@ -251,29 +251,124 @@ Public Class Individual_Frame_3D
While frameLengths.Count > 0 While frameLengths.Count > 0
Dim frameCutList As New Dictionary(Of String, Integer) Dim frameCutList As New Dictionary(Of String, Integer)
frameCutList.Add(frameLengths.ElementAt(0).Key, frameLengths.ElementAt(0).Value) 'Fixa att vi hela tiden tar den längsta och testar mot de andra Dim longestFrame As Integer = 0
frameLengths.Remove(frameLengths.ElementAt(0).Key) Dim longestFrameKey As String = ""
For i = 0 To frameLengths.Count - 1
If frameLengths.ElementAt(i).Value > longestFrame Then
longestFrame = frameLengths.ElementAt(i).Value
longestFrameKey = frameLengths.ElementAt(i).Key
End If
Next
frameCutList.Add(longestFrameKey, longestFrame)
frameLengths.Remove(longestFrameKey)
For i = 0 To 100 For i = 0 To 1000
Dim lengthRemaining As Integer = 2000 Dim lengthRemaining As Integer = 2000
For j = 0 To frameCutList.Count - 1 For j = 0 To frameCutList.Count - 1
lengthRemaining -= frameCutList.ElementAt(j).Value lengthRemaining -= frameCutList.ElementAt(j).Value
Next Next
Dim restLength As Integer = 2000 Dim restLength As Integer = 2000
Dim frameID As String = "" Dim frameID As New List(Of String)
For j = 0 To frameLengths.Count - 1 For j = 0 To frameLengths.Count - 1
If lengthRemaining - frameLengths.ElementAt(j).Value > 0 AndAlso lengthRemaining - frameLengths.ElementAt(j).Value < restLength Then If lengthRemaining - frameLengths.ElementAt(j).Value >= 0 AndAlso lengthRemaining - frameLengths.ElementAt(j).Value < restLength Then
restLength = lengthRemaining - frameLengths.ElementAt(j).Value restLength = lengthRemaining - frameLengths.ElementAt(j).Value
frameID = frameLengths.ElementAt(j).Key frameID.Clear()
frameID.Add(frameLengths.ElementAt(j).Key)
End If End If
Next Next
If frameID = "" Then If frameID.Count = 0 Then
Exit For Exit For
Else Else
frameCutList.Add(frameID, frameLengths(frameID)) Dim frameCombDS As New DataSet
frameLengths.Remove(frameID) For l = 2 To frameLengths.Count
Dim frameCombDT As New DataTable
Dim numOfValues As Integer = l
Dim numOfComb As Integer = Calculate_Factorial(frameLengths.Count) / (Calculate_Factorial(numOfValues) * Calculate_Factorial(frameLengths.Count - numOfValues))
For j = 1 To numOfValues
frameCombDT.Columns.Add("Enum " & j, GetType(Integer))
Next
For j = 1 To numOfValues
frameCombDT.Columns.Add("ID " & j, GetType(Integer))
Next
frameCombDT.Columns.Add("Combined Length", GetType(Integer))
Dim frameCombDR As DataRow = frameCombDT.NewRow
Dim combLength As Integer = 0
For j = 1 To numOfValues
frameCombDR("Enum " & j) = j
frameCombDR("ID " & j) = CInt(frameLengths.ElementAt(j - 1).Key.Split(" ")(1))
combLength += frameLengths.ElementAt(j - 1).Value
Next
frameCombDR("Combined Length") = combLength
frameCombDT.Rows.Add(frameCombDR)
For j = 1 To numOfComb - 1
frameCombDR = frameCombDT.NewRow
If frameCombDT.Rows(j - 1)("Enum " & numOfValues) < frameLengths.Count Then
combLength = 0
For k = 1 To numOfValues
If k = numOfValues Then
frameCombDR("Enum " & k) = frameCombDT.Rows(j - 1)("Enum " & k) + 1
frameCombDR("ID " & k) = CInt(frameLengths.ElementAt(frameCombDR("Enum " & k) - 1).Key.Split(" ")(1))
Else
frameCombDR("Enum " & k) = frameCombDT.Rows(j - 1)("Enum " & k)
frameCombDR("ID " & k) = frameCombDT.Rows(j - 1)("ID " & k)
End If
combLength += frameLengths.ElementAt(frameCombDR("Enum " & k) - 1).Value
Next
frameCombDR("Combined Length") = combLength
Else
Dim frameEnum As Integer
For k = numOfValues To 1 Step -1
If frameCombDT.Rows(j - 1)("Enum " & k) < frameLengths.Count - (numOfValues - k) Then
frameEnum = k
End If
Next
combLength = 0
For k = 1 To numOfValues
If k = frameEnum Then
frameCombDR("Enum " & k) = frameCombDT.Rows(j - 1)("Enum " & k) + 1
frameCombDR("ID " & k) = CInt(frameLengths.ElementAt(frameCombDR("Enum " & k) - 1).Key.Split(" ")(1))
ElseIf k > frameEnum Then
frameCombDR("Enum " & k) = frameCombDR("Enum " & k - 1) + 1
frameCombDR("ID " & k) = CInt(frameLengths.ElementAt(frameCombDR("Enum " & k) - 1).Key.Split(" ")(1))
Else
frameCombDR("Enum " & k) = frameCombDT.Rows(j - 1)("Enum " & k)
frameCombDR("ID " & k) = frameCombDT.Rows(j - 1)("ID " & k)
End If
combLength += frameLengths.ElementAt(frameCombDR("Enum " & k) - 1).Value
Next
frameCombDR("Combined Length") = combLength
End If
frameCombDT.Rows.Add(frameCombDR)
Next
frameCombDS.Tables.Add(frameCombDT)
Next
For j = 0 To frameCombDS.Tables.Count - 1
For Each DR As DataRow In frameCombDS.Tables(j).Rows
If lengthRemaining - DR("Combined Length") >= 0 AndAlso lengthRemaining - DR("Combined Length") < restLength Then
restLength = lengthRemaining - DR("Combined Length")
frameID.Clear()
For k = 1 To j + 2
frameID.Add("Frame " & DR("ID " & k))
Next
End If
Next
Next
For j = 0 To frameID.Count - 1
frameCutList.Add(frameID.ElementAt(j), frameLengths(frameID.ElementAt(j)))
frameLengths.Remove(frameID.ElementAt(j))
Next
End If End If
Next Next
@ -289,6 +384,22 @@ Public Class Individual_Frame_3D
Return cuttingList Return cuttingList
End Function End Function
Private Shared Function Calculate_Factorial(num As Integer)
Dim result As Integer
If num = 0 Then
result = 1
Else
result = num
num -= 1
While num > 1
result = result * num
num -= 1
End While
End If
Return result
End Function
Public Shared Sub Generate_End_Frame_Pieces() Public Shared Sub Generate_End_Frame_Pieces()
Dim swApp As SldWorks.SldWorks Dim swApp As SldWorks.SldWorks
swApp = CType(System.Runtime.InteropServices.Marshal.GetActiveObject("SldWorks.Application"), SldWorks.SldWorks) swApp = CType(System.Runtime.InteropServices.Marshal.GetActiveObject("SldWorks.Application"), SldWorks.SldWorks)