27 | 08 | 2016

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.



Listview to String and String to Listview

The function below are able to store listview content into a string. With the function ImportListViewfromString the string can be used tot populate a listview again.



 Function ImportListViewfromString(ByVal ListViewString As String, ByVal LSTview As ListView) As Boolean
        On Error GoTo errorhandler
        ImportListViewfromString = False
        'This function populates a listview with content from a string

        Dim ItemLines As New TextBox
        ItemLines.Text = ListViewString

        For Each line As String In ItemLines.Lines
            Dim a1() As String = line.Split("|") 'Change | if a different delimiter is used
            Dim Item As New ListViewItem(a1(0))
            For i = 1 To UBound(a1)
            Next i
            LSTview.Items.AddRange(New ListViewItem() {Item})

        ImportListViewfromString = True
        Exit Function
    End Function

    Function ExportListViewtoString(ByVal LSTview As ListView) As String
        'This function populates a string with content from a listview
        On Error GoTo errorhandler

        Dim String1 As String = ""
        For Each ListViewitem1 As ListViewItem In LSTview.Items
            If String1 <> "" Then String1 = String1 & vbCrLf
            String1 = String1 & ListViewitem1.Text & "|" 'Change | if a different delimiter is used

            For i = 1 To ListViewitem1.SubItems.Count - 1
                String1 = String1 & ListViewitem1.SubItems(i).Text & "|"

        ExportListViewtoString = (String1)
        Exit Function
    End Function

Search within Datatable

TO search for a value within a datatable use the code below:

Dim Tbl as DataTable =  '(Specify datatable here)

Dim SearchString As String = "TypeSearchStringHere"   'String to search for within datatable
Dim rowfound As DataRow =TBL.Rows.Find(SearchString )

If rowfound IsNot Nothing Then
    MsgBox(rowfound (1).ToString())  'Resturns the first value of the found row(s). 
    MsgBox("A row with value " & SearchString  & " has not be found")
End If

Datagridview Select Row on Right Click Mouse

The code below selects the row (where the mouse cursor is on) when the right mouse button is used.  

DataGridView1.Rows(DataGridHistory.HitTest(e.Location.X, e.Location.Y).RowIndex).Selected = True

Scroll to\Select Datagridview Last Row

'Scroll to the last row.
Me.DataGridView1.FirstDisplayedScrollingRowIndex = Me.DataGridView1.RowCount - 1
'Select the last row.
Me.DataGridView1.Rows(Me.DataGridView1.RowCount - 1).Selected = True

Inventor Names of Ribbons and RibbonTab

Below is a list of Ribbon and Ribbontab names as used in Inventor 2014

      Get Started - id_GetStarted
            Launch - id_Panel_Launch
            New Features - id_Panel_GetStartedWhatsNew
            Learn about Inventor - id_Panel_LearnAbout
            Community - id_Panel_GetStartedInvolve
      Tools - id_TabTools
            Options - id_PanelP_ToolsOptions
            Content Center - id_PanelZ_ToolsContentCenter
            iLogic - iLogic.RibbonPanel
            Web - id_PanelP_ToolsWebTools

Read more: Inventor Names of Ribbons and RibbonTab

Checkif filename has illegal characters

The function below checks if a filename is valid or if a filename has illegal characters. This is a usefull check before saving a file.

 Function IsFilenameValid(ByVal name As String) As Boolean

        Dim i As Integer
        Dim dn, fn As String

        i = InStrRev(name, "\") : dn = Mid(name, 1, i) : fn = Mid(name, i + 1)
        If name Is Nothing Or Trim(fn) = "" Then
            Return False
            For Each badchar As Char In Path.GetInvalidFileNameChars
                If InStr(fn, badchar) > 0 Then
                    Return False
                End If
        End If

        If dn <> "" Then
            If InStr(dn, "\\") > 0 Then
                Return False
            End If
            For Each badChar As Char In Path.GetInvalidPathChars
                If InStr(dn, badChar) > 0 Then
                    Return False
                End If
            If Not System.IO.Directory.Exists(dn) Then
                    Return False
                End Try
            End If
        End If
        Return True
    End Function

VB error numbers and descriptions Overview

Use errorhandling like the function below. In case of an error within the code the code will jump to errorhandler: and display an error number & description. 

    Sub CheckErrorCode()
        '**** Code from VisibleVisual.com ******
        On Error GoTo errorhandler

        'your code

        Exit Sub
        MsgBox(Err.Number & "-" & Err.Description)
    End Sub

 A complete list of error numbers and descriptions is shown below.

Read more: VB error numbers and descriptions Overview


Sign up now and upload your code to the website.

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