23 | 02 | 2017

Cutting Optimizer (raw material calculation)

The program code below can be used for calculating the amount of Raw Material Lengths (extrusions, poles, beams etc) needed to cut a certain cutting list (in one dimension)


Cutting Raw Material Length Calculation

'Screenshot of the program'

The code:

Imports System.ComponentModel

Public Class Form1

    '*** Code from VisibleVisual.com ********
    '1. Cuts are made form the longest to the shortest length
    '2. With each cut left over scrap material will be stored in DataGridViewScrap
    '3. Before each cut the code looks in DataGridViewScrap for the shortest length that can be used to make the cut.
    '4. If scrap material is used left over scrap will be stored again in DataGridViewScrap.
    '5. If there is no length available for the cut DataGridViewScrap then new lengths will be used. 
    '6. If part of the scrap material is available the code will use what is available and adds new lengths to finish the job.

    ''The code can be used for materials that come in standard Raw material lengths like: steel beams, wooden pols, extrusion material, U-profiles etc.

    Function CalculateCuts(ByVal MaxLength As Double) As Integer
        CalculateCuts = 0
        Dim CutLength As Double
        Dim TotalLengtsNeeded As Integer
        Dim NoLengthsNeeded As Integer
        Dim ScrapLenth As Double
        Dim NoOfScrap As Integer
        Dim CutfromScrap As Boolean
        Dim CutfromScrap2 As Boolean
        Dim AmountOutOneLength As Integer
        Dim NewLengthsNeeded As Integer

        DataGridViewScrap.Rows.Clear() 'Clear Scrap 
        DataGridViewCuts.Sort(DataGridViewCuts.Columns("Length"), ListSortDirection.Ascending) 'Sort longest cut first

        If DataGridViewCuts.Rows.Count = 0 Then MsgBox("No data available. Load sample data first.") : Exit Function
        For Each RowNeededCut As DataGridViewRow In DataGridViewCuts.Rows

            CutLength = RowNeededCut.Cells("Length").Value
            NoLengthsNeeded = RowNeededCut.Cells("NoLengths").Value

            CutfromScrap = False

            'First loop through scrap material to see if the cut length exists
            With DataGridViewScrap
                For Each rowScrap As DataGridViewRow In DataGridViewScrap.Rows
                    ScrapLenth = rowScrap.Cells("LengthScrap").Value        'Length of scrap   

                    If ScrapLenth >= CutLength Then                         'Is scrap long enough
                        CutfromScrap = True
                        NoOfScrap = rowScrap.Cells("NoLengths").Value 'Aantal scrap available

                        If NoOfScrap >= NoLengthsNeeded Then
                            'Íf all can be cut from scrap then
                            rowScrap.Cells("NoLengths").Value = NoOfScrap - NoLengthsNeeded  'Reset NoOfScrap

                            If ScrapLenth - CutLength > 0 Then  'Çheck if there is scrap material
                                .Rows(.Rows.Count - 1).Cells("NoLengths").Value = NoLengthsNeeded
                                .Rows(.Rows.Count - 1).Cells("Length").Value = ScrapLenth - CutLength
                                .Sort(.Columns("LengthScrap"), ListSortDirection.Ascending)
                            End If
                            'if only a part can be cut from scrap
                            .Rows(.Rows.Count - 1).Cells("NoLengths").Value = NoOfScrap
                            .Rows(.Rows.Count - 1).Cells("Length").Value = ScrapLenth - CutLength

                            NoLengthsNeeded = NoLengthsNeeded - NoOfScrap
                            .Sort(.Columns("LengthScrap"), ListSortDirection.Ascending)
                            GoTo recalculate
                        End If

                        .Sort(.Columns("Length"), ListSortDirection.Ascending)
                        Exit For
                    End If
                Next rowScrap

                If CutfromScrap = False Then     'if not found in scrap

                    AmountOutOneLength = Math.Truncate(MaxLength / CutLength)
                    NewLengthsNeeded = NoLengthsNeeded / AmountOutOneLength

                    CutfromScrap2 = False
                    For Each rowScrap2 As DataGridViewRow In DataGridViewScrap.Rows
                        If rowScrap2.Cells("LengthScrap").Value = MaxLength - (CutLength * AmountOutOneLength) Then
                            rowScrap2.Cells("NoLengthsScrap").Value = rowScrap2.Cells("NoLengthsScrap").Value + NewLengthsNeeded
                            CutfromScrap2 = True
                            Exit For
                        End If
                    Next rowScrap2

                    If CutfromScrap2 = False Then
                        If MaxLength - (CutLength * AmountOutOneLength) > 0 Then 'Çheck if there is scrap material
                            .Rows(.Rows.Count - 1).Cells("NoLengthsScrap").Value = NewLengthsNeeded
                            .Rows(.Rows.Count - 1).Cells("LengthScrap").Value = MaxLength - (CutLength * AmountOutOneLength)
                        End If
                    End If
                    TotalLengtsNeeded = TotalLengtsNeeded + NewLengthsNeeded

                End If
                .Sort(.Columns("LengthScrap"), ListSortDirection.Ascending)
            End With

        Next RowNeededCut

        CalculateCuts = (TotalLengtsNeeded)

    End Function

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim MaxLength As Double = 5800

        MsgBox(CalculateCuts(MaxLength) & " lengths are needed to complete the cutting list.")
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        'Sample data
        With DataGridViewCuts

            .Rows.Add(100, CDbl("2560,5"))
            .Rows.Add(3, CDbl("500,5"))
            .Rows.Add(50, CDbl("900"))
            .Rows.Add(5, CDbl("5800"))
            .Rows.Add(36, CDbl("650"))
            .Rows.Add(25, CDbl("50"))
        End With

    End Sub

End Class

To download the complete program login to see the link below.




Sign up now and upload your code to the website.

Help us to continue.....
Articles View Hits
Latest Articles