📄 form1.vb
字号:
Imports System.Data
Imports System.Data.OleDb
Imports System.Math
Public Class Form1
Dim connectionstring As String
Dim strselect As String
Dim mydataset As New DataSet()
Dim dbConnection As OleDbConnection
Dim dataAdapter As OleDbDataAdapter
Dim aprocess As CProcess
Dim processArray As New ArrayList '定义工序数组,存入所有工序及其信息
Dim partsArray As New ArrayList '将工件名存入数组
Dim machinesArray As New ArrayList '将设备名存入数组
Dim amachine As CMachine
Public bmp As Bitmap
Dim table As New DataTable
Dim flag As Boolean = False
Public count As Integer = 0
Public count1 As Integer
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.DataGridView2.Hide()
connectionstring = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & Application.StartupPath & "\mydatabase.mdb"
dbConnection = New OleDbConnection(connectionstring)
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Call ReadMachinesName()
Call ReadPartsName()
Call SetProcess(processArray)
processArray.Sort(New CompareByTime()) '给所有工序根据相对剩余时间从大到小进行排序
Call StartScheduling() '初步调度
Call AdjustScheduling() '调节调度
Call DrawGanTe() '生成甘特图
End Sub
Sub AdjustScheduling() '调节排序
Dim preapro As CProcess '该设备上的前一道加工工序
Dim nextapro As CProcess '该设备上的后一道加工工序
Dim max As Double
For j As Integer = 0 To machinesArray.Count - 1
amachine = machinesArray.Item(j)
For k As Integer = 1 To amachine.schedulingArray.Count - 2
aprocess = amachine.schedulingArray.Item(k)
If aprocess.timeVoid > 0 Then '从头开始查找空闲时间大于0的工序
preapro = amachine.schedulingArray.Item(k - 1) '得到设备上前一道工序,和后一道工序
nextapro = amachine.schedulingArray.Item(k + 1)
'若nextapro.preProcess.timeFinish < aprocess.timeStart且
'max{preapro..timeFinish,nextapro.preProcess.timeFinish}+
'nextapro.timeProcess(+aprocess.timeProcess <= aprocess.nextProcess.timeStart)
'交换aprocess和nextapro在该设备上的加工顺序
If nextapro.preProcess.timeFinish < aprocess.timeStart Then
max = preapro.timeFinish
If max < nextapro.preProcess.timeFinish Then
max = nextapro.preProcess.timeFinish
End If
If max + nextapro.timeProcess + aprocess.timeProcess <= aprocess.nextProcess.timeStart Then
nextapro.timeStart = max
nextapro.timeFinish = max + nextapro.timeProcess
max = nextapro.timeFinish
If max < aprocess.preProcess.timeFinish Then
max = aprocess.preProcess.timeFinish
End If
aprocess.timeStart = max
aprocess.timeFinish = aprocess.timeStart + aprocess.timeProcess
amachine.schedulingArray.Item(k) = nextapro
amachine.schedulingArray.Item(k + 1) = aprocess
For n As Integer = k + 2 To amachine.schedulingArray.Count - 1
nextapro = amachine.schedulingArray.Item(n)
max = aprocess.timeFinish
If max < nextapro.preProcess.timeFinish Then
max = nextapro.preProcess.timeFinish
End If
nextapro.timeStart = max
nextapro.timeVoid = max - aprocess.timeFinish
nextapro.timeFinish = nextapro.timeStart + nextapro.timeProcess
aprocess = nextapro
'flag += 1
Next
End If
End If
End If
Next
Next
End Sub
Sub DrawGanTe() '生成甘特图
Dim bmphig As Integer
Dim bmpwid As Integer
Dim eff As Integer
Dim wid As Integer
wid = Int(CType(machinesArray.Item(0), CMachine).Lk)
For i As Integer = 1 To machinesArray.Count - 1
If wid < Int(CType(machinesArray.Item(i), CMachine).Lk) Then
wid = Int(CType(machinesArray.Item(i), CMachine).Lk)
End If
Next
If wid < 800 Then
eff = 1500 / wid
ElseIf wid > 1500 Then
eff = wid / 1500
Else
eff = 5
End If
bmpwid = wid * eff + 100
bmphig = machinesArray.Count * 20 + 50
bmp = New Bitmap(bmpwid, bmphig)
Dim gbmp As Graphics = Graphics.FromImage(bmp)
Dim rect As Rectangle
For j As Integer = 0 To machinesArray.Count - 1
amachine = machinesArray.Item(j)
For k As Integer = 0 To amachine.schedulingArray.Count - 1
aprocess = amachine.schedulingArray.Item(k)
rect = New Rectangle(30 + aprocess.timeStart * eff, 20 + 20 * j, (aprocess.timeFinish - aprocess.timeStart) * eff, 15)
gbmp.DrawRectangle(Pens.Blue, rect)
Dim brush As New SolidBrush(Color.Black)
gbmp.DrawString(amachine.nameMachine, New Font("Times New Roman", 8), brush, 5, 20 + 20 * j)
Dim num As Integer = Integer.Parse(aprocess.namePart)
brush.Color = Color.FromArgb(80, (num * 194) Mod (255), (num * 111) Mod (255), (num * 92) Mod (255))
gbmp.FillRectangle(brush, rect)
gbmp.DrawString(aprocess.timeStart, New Font("Times New Roman", 8), Brushes.Black, 30 + aprocess.timeStart * eff - 5, 20 + 20 * j + 10)
gbmp.DrawString(aprocess.timeFinish, New Font("Times New Roman", 8), Brushes.Black, 30 + aprocess.timeFinish * eff - 5, 20 + 20 * j + 10)
gbmp.DrawString(aprocess.nameProcess, New Font("Times New Roman", 8), Brushes.Red, 30 + (aprocess.timeStart + aprocess.timeProcess / 2) * eff, 20 + 20 * j - 2)
Next
Next
Me.Hide()
Form2.PictureBox1.Image = bmp
Form2.Show()
End Sub
Sub StartScheduling() '进行调度
Dim tempArray As New ArrayList
Dim sortArray As New ArrayList
Dim count As Integer = processArray.Count - 1
Dim flag As Integer = 0 '标志位,初始为0,当碰到第一个工序的相对剩余时间为0后,改变成1
For i As Integer = 0 To count
aprocess = processArray.Item(i)
'将相对剩余时间都为0的工序按照其前道工序结束时间进行排序,其前道工序结束的时间越早,其相对剩余时间值越大
If flag = 0 And aprocess.timeRelation = 0 Then
For j As Integer = count To i Step -1
aprocess = processArray.Item(j)
sortArray.Add(aprocess)
processArray.Remove(aprocess)
Next
sortArray.Sort(New CompareByPerFinishTime()) '对所有相对剩余时间为0的工序进行排序
For k As Integer = 0 To sortArray.Count - 1
processArray.Add(sortArray.Item(k))
Next
aprocess = processArray.Item(i)
flag = 1
End If
'---------------------
tempArray = aprocess.validmachineArray.Clone()
If tempArray.Count > 1 Then
tempArray.Sort(New CompareByMachine()) '对设备组按照Lk的从小到大的顺序进行排序
End If
amachine = tempArray.Item(0) '取Lk最小的设备
If aprocess.preProcess.timeFinish > amachine.Lk Then '若该工序的上道工序的完工时间大于该设备停止加工的时间(该设备上产生了空余时间)
aprocess.timeStart = aprocess.preProcess.timeFinish
aprocess.timeVoid = aprocess.preProcess.timeFinish - amachine.Lk
aprocess.timeFinish = aprocess.timeStart + aprocess.timeProcess
GoTo add
Else '若该工序的上道工序完成时间小于该设备停止加工的时间
aprocess.timeStart = amachine.Lk
aprocess.timeFinish = amachine.Lk + aprocess.timeProcess
GoTo add
End If
add: amachine.Lk = aprocess.timeFinish
Dim alk As Double = amachine.Lk
amachine.LkArray.Add(alk)
amachine.addProcess(aprocess)
tempArray.Clear()
Next
End Sub
Sub SetProcess(ByVal array As ArrayList) '从表中读取每个工序的信息,计算每个工序的相对剩余时间,并将工序存入到工序数组
If count1 = 1 Then
mydataset = New DataSet()
For i As Integer = 0 To partsArray.Count - 1 '根据工件号,将不同工件的工序分成几张表
strselect = "select * from processinfo where 工件号= '" & partsArray.Item(i).ToString() & "' order by 工序号 asc"
dataAdapter = New OleDbDataAdapter(strselect, dbConnection)
dataAdapter.Fill(mydataset, i.ToString)
Dim sum As Double = 0
Dim tempArray As New ArrayList
Dim aArray As New ArrayList
Dim tempApro As CProcess
Dim flag As Integer = 0
For Each arow As DataRow In mydataset.Tables(i).Rows '计算某张表中的工序的相对剩余时间,并将工序存入工序数组
If flag = 0 Then
aprocess = New CProcess(arow(0), arow(1), arow(2), arow(3), arow(5))
aprocess.preProcess = New CProcess()
Else
tempApro = New CProcess(arow(0), arow(1), arow(2), arow(3), arow(5))
tempApro.preProcess = aprocess '将前一道工序与后一道工序相连
aprocess.nextProcess = tempApro
aprocess = tempApro
End If
flag = 1
'将可用设备添加到工序中
For Each str As String In aprocess.validmachineArray
For Each amach As CMachine In machinesArray
If amach.nameMachine = str Then
aArray.Add(amach)
End If
Next
Next
aprocess.validmachineArray.Clear()
aprocess.validmachineArray = aArray.Clone()
aArray.Clear()
'------------
sum = sum + aprocess.timeProcess
tempArray.Add(aprocess)
array.Add(aprocess)
Next
aprocess = tempArray.Item(0)
aprocess.timeRelation = Round(aprocess.timeProcess / sum, 2)
For j As Integer = 1 To tempArray.Count - 1
aprocess = tempArray.Item(j)
tempApro = tempArray.Item(j - 1)
aprocess.timeRelation = tempApro.timeRelation + aprocess.timeProcess / sum
Next
For k As Integer = 0 To tempArray.Count - 1
aprocess = tempArray.Item(k)
aprocess.timeRelation = Round(1 - aprocess.timeRelation, 2)
Next
Next
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
ElseIf count1 = 2 Then
mydataset = New DataSet()
For i As Integer = 0 To partsArray.Count - 1 '根据工件号,将不同工件的工序分成几张表
strselect = "select * from processinfo66 where 工件号= '" & partsArray.Item(i).ToString() & "' order by 工序号 asc"
dataAdapter = New OleDbDataAdapter(strselect, dbConnection)
dataAdapter.Fill(mydataset, i.ToString)
Dim sum As Double = 0
Dim tempArray As New ArrayList
Dim aArray As New ArrayList
Dim tempApro As CProcess
Dim flag As Integer = 0
For Each arow As DataRow In mydataset.Tables(i).Rows '计算某张表中的工序的相对剩余时间,并将工序存入工序数组
If flag = 0 Then
aprocess = New CProcess(arow(0), arow(1), arow(2), arow(3), arow(5))
aprocess.preProcess = New CProcess()
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -