Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Imports System.Net.Sockets
- Imports System
- Imports System.Runtime.InteropServices
- Imports System.Security.Principal
- Imports System.Security.Permissions
- Imports System.IO
- Public Class Form1
- Dim jobpath As String = ""
- Dim jobini As String = ""
- Dim povinipath As String = ""
- Dim currentframe As String = ""
- Dim running As Boolean = False
- Dim framez As Integer = 1
- Dim widthz As Integer = 320
- Dim heightz As Integer = 240
- Dim framedone As Boolean = False
- Dim outputimage As String
- Dim outputimagepath As String
- Dim maxframe As Integer
- Dim sourcepath As String = ""
- Dim framestart As TimeSpan
- Dim lastini As String
- Dim copytry As Boolean = False
- Dim networkpath As String = ""
- Dim server As String
- Dim publisher As New UdpClient()
- Dim subscriber As New UdpClient()
- Dim mycomputername As String = Environment.MachineName
- Dim mycomputerconnections() As Net.NetworkInformation.NetworkInterface
- Dim MCSub As New UdpClient(10100)
- 'Set Job Path
- Private Sub TBName_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TBName.TextChanged
- jobpath = "C:\POVStorage\" & TBName.Text
- End Sub
- 'Render Start
- Private Sub renderstart(ByVal rootdir As String)
- If running = False Then
- LBLStatus.Text = "Copying"
- If IO.Directory.Exists(jobpath) = False Then
- 'DirectoryCopy(rootdir, jobpath, True)
- IO.Directory.CreateDirectory(jobpath)
- My.Computer.FileSystem.CopyDirectory(rootdir, jobpath, FileIO.UIOption.AllDialogs)
- End If
- Dim filelist() As String
- filelist = IO.Directory.GetFiles(jobpath)
- maxframez()
- Dim ini As String = "naaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaame"
- For i = 0 To filelist.Count - 1
- Dim fName As String = filelist(i).Substring(jobpath.Length + 1)
- If fName.Contains("ini") And fName.Count < ini.Count Then
- ini = fName
- End If
- Next
- ini = IO.Path.Combine(jobpath, ini)
- jobini = ini
- Dim filelines() As String
- GC.Collect()
- filelines = IO.File.ReadAllLines(ini)
- GC.Collect()
- For i = 0 To filelines.Count - 1
- If filelines(i).Contains("Subset_Start_Frame") Then
- filelines(i) = "Subset_Start_Frame=" & framez
- End If
- If filelines(i).Contains("Subset_End_Frame") Then
- filelines(i) = "Subset_End_Frame=" & framez
- End If
- If filelines(i).Contains("Width") Then
- filelines(i) = "Width=" & widthz
- End If
- If filelines(i).Contains("Height") Then
- filelines(i) = "Height=" & heightz
- End If
- Next
- GC.Collect()
- IO.File.WriteAllLines(ini, filelines)
- GC.Collect()
- framestart = My.Computer.Clock.LocalTime.TimeOfDay
- Dim maxstring As Integer = maxframe.ToString.Length
- outputimage = jobini.Substring(0, jobini.Length - 4)
- outputimagepath = outputimage & framez.ToString().PadLeft(maxstring, "0") & ".png"
- If IO.File.Exists(outputimagepath) = True Then
- IO.File.Delete(outputimagepath)
- End If
- running = True
- Process.Start(My.Settings.POVDir, "/RENDER " & ini)
- lastini = ini
- End If
- TimerPOV.Enabled = True
- End Sub
- 'Find EXE, setup initial POV
- Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
- OFDPovEngine.ShowDialog()
- End Sub
- Private Sub OpenFileDialog1_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles OFDPovEngine.FileOk
- My.Settings.POVDir = OFDPovEngine.FileName
- Dim mydocz() As String = IO.Directory.GetDirectories(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments))
- Dim povdir As String = ""
- For Each Dir As String In mydocz
- If Dir.ToLower.Contains("pov-ray") Then
- povdir = Dir
- End If
- Next
- If povdir = "" Then
- MsgBox("Unable to Find 'pvengine.ini' Please manually find")
- OFDPovINI.ShowDialog()
- Return
- End If
- Dim povvz() As String = IO.Directory.GetDirectories(povdir)
- Dim povv As String = ""
- For Each Dir As String In povvz
- If Dir.ToLower.Contains("v") And Dir.Contains(".") Then
- povv = Dir
- End If
- Next
- If povv = "" Then
- MsgBox("Unable to Find 'pvengine.ini' Please manually find")
- OFDPovINI.ShowDialog()
- Return
- End If
- Dim povini As String = IO.Path.Combine(povv & "\ini")
- Dim poviniz() As String = IO.Directory.GetFiles(povini)
- For Each file As String In poviniz
- If file.Contains("pvengine.ini") And file.Substring(file.LastIndexOf("\") + 1).Length = 12 Then
- povinipath = file
- End If
- Next
- If povinipath = "" Then
- MsgBox("Unable to Find 'pvengine.ini' Please manually find")
- OFDPovINI.ShowDialog()
- Return
- End If
- INIsetup()
- End Sub
- Private Sub OFDPovINI_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles OFDPovINI.FileOk
- povinipath = OFDPovINI.FileName
- INIsetup()
- End Sub
- Private Sub INIsetup()
- Dim filelines() As String
- GC.Collect()
- filelines = IO.File.ReadAllLines(povinipath)
- GC.Collect()
- Dim filelinez As New List(Of String)
- For i = 0 To filelines.Count - 1
- filelinez.Add(filelines(i))
- Next
- Dim anyoneinstance As Boolean = False
- Dim anyoneinstanceset As Boolean = False
- For i = 0 To filelinez.Count - 1
- If filelinez(i).Contains("[Permitted Input Paths]") Then
- filelinez(i + 1) = "1=c:\"
- End If
- If filelinez(i).Contains("[Permitted Output Paths]") Then
- filelinez(i + 1) = "1=c:\"
- End If
- If filelinez(i).Contains("HideNewUserHelp") Then
- filelinez(i) = "HideNewUserHelp=1"
- End If
- If filelinez(i).Contains("RenderCompleteSound") Then
- filelinez(i) = "RenderCompleteSound=1"
- End If
- If filelinez(i).Contains("OutputFileLocation") Then
- filelinez(i) = "OutputFileLocation=1"
- End If
- If filelinez(i).Contains("RenderwinClose") Then
- filelinez(i) = "RenderwinClose=1"
- End If
- If filelinez(i).Contains("ParserErrorSound") Then
- filelinez(i) = "ParserErrorSound=1"
- End If
- If filelinez(i).Contains("OneInstanceSet") Then
- filelinez(i) = "OneInstanceSet=1"
- anyoneinstanceset = True
- End If
- If filelinez(i).Contains("OneInstance=") Then
- filelinez(i) = "OneInstance=1"
- anyoneinstance = True
- End If
- If filelinez(i).Contains("ItsAboutTime") Then
- filelinez(i) = "ItsAboutTime=1500000000"
- End If
- Next
- If anyoneinstance = False Then
- filelinez.Add("OneInstance=1")
- End If
- If anyoneinstanceset = False Then
- filelinez.Add("OneInstanceSet=1")
- End If
- Dim filelined(filelinez.Count - 1) As String
- For i = 0 To filelinez.Count - 1
- filelined(i) = filelinez(i)
- Next
- GC.Collect()
- IO.File.WriteAllLines(povinipath, filelined)
- GC.Collect()
- 'If MsgBox("Windows 7?", MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
- ' Process.Start(Environment.SystemDirectory & "\netsh.exe", "advfirewall firewall add rule name=""allow PVD"" dir=in program=""" & IO.Directory.GetCurrentDirectory.ToString & "\POVRay Distribute Client.exe" & """ security=authnoencap action=allow")
- ' Process.Start(Environment.SystemDirectory & "\netsh.exe", "advfirewall firewall add rule name=""allow PVD"" dir=out program=""" & IO.Directory.GetCurrentDirectory.ToString & "\POVRay Distribute Client.exe" & """ security=authnoencap action=allow")
- 'Else
- ' MsgBox("Then auto-connect may not work unless you open ports manually")
- 'End If
- End Sub
- 'Pic Complete Monitor
- Private Sub TimerPOV_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TimerPOV.Tick
- Dim elapsedtime As TimeSpan = My.Computer.Clock.LocalTime.TimeOfDay - framestart
- If elapsedtime.TotalMinutes > 1 Then
- framestart = My.Computer.Clock.LocalTime.TimeOfDay
- Process.Start(My.Settings.POVDir, "/RENDER " & lastini)
- End If
- Dim maxstring As Integer = maxframe.ToString.Length
- outputimage = jobini.Substring(0, jobini.Length - 4)
- outputimagepath = outputimage & framez.ToString().PadLeft(maxstring, "0") & ".png"
- If IO.File.Exists(outputimagepath) = True And FileInUse(outputimagepath) = False Then
- LBLStatus.Text = "Ready"
- If running = True Then
- Threading.Thread.Sleep(2000)
- framedone = True
- End If
- running = False
- TimerPOV.Enabled = False
- Else
- LBLStatus.Text = "Running"
- running = True
- End If
- End Sub
- Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
- My.Settings.Save()
- End Sub
- 'Form Load
- Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
- mycomputerconnections = Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces
- TBHostName.Text = mycomputername
- '
- publisher.Client.Blocking = False
- publisher.ExclusiveAddressUse = False
- subscriber.Client.ReceiveTimeout = 100
- subscriber.Client.Blocking = False
- subscriber.ExclusiveAddressUse = False
- MCSub.Client.ReceiveTimeout = 100
- MCSub.Client.Blocking = False
- MCSub.JoinMulticastGroup(Net.IPAddress.Parse("239.80.8.5"), 4)
- MCSub.Client.MulticastLoopback = True
- MCSub.Client.EnableBroadcast = True
- If My.Settings.POVDir Is Nothing Or My.Settings.POVDir = "" Then
- MsgBox("POV Directory Not Set, please point to POV Engine.")
- OFDPovEngine.ShowDialog()
- End If
- For i = 0 To mycomputerconnections.Length - 1
- CBConnections.Items.Add(mycomputerconnections(i).Name)
- Next
- CBConnections.SelectedIndex = My.Settings.Connection
- TimerMCSub.Enabled = True
- End Sub
- 'Max Frame
- Private Sub maxframez()
- Dim rootdir As String = jobpath
- Dim filelist() As String = IO.Directory.GetFiles(rootdir)
- Dim ini As String = "naaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaame"
- For i = 0 To filelist.Count - 1
- Dim fName As String = filelist(i).Substring(rootdir.Length + 1)
- If fName.Contains("ini") And fName.Count < ini.Count Then
- ini = fName
- End If
- Next
- ini = IO.Path.Combine(rootdir, ini)
- Dim filelines() As String
- GC.Collect()
- filelines = IO.File.ReadAllLines(ini)
- GC.Collect()
- For i = 0 To filelines.Count - 1
- If filelines(i).Contains("Final_Frame") Then
- maxframe = filelines(i).Substring(12)
- End If
- Next
- End Sub
- 'UDP Monitor
- Private Sub TimerUDP_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TimerUDP.Tick
- Dim stringz As String = ""
- Dim Receive As String = ""
- Try
- Dim ep As System.Net.IPEndPoint = New System.Net.IPEndPoint(System.Net.IPAddress.Any, 0)
- Dim rcvbytes() As Byte = subscriber.Receive(ep)
- Receive = System.Text.Encoding.ASCII.GetString(rcvbytes)
- Catch ex As Exception
- End Try
- If Receive <> "" Then
- If Receive.Contains("%PVD%N") = True Then
- stringz = "%PVD%C"
- My.Settings.Server = Receive.Substring(6)
- LBLStatus.Text = "Ready"
- End If
- If Receive.Contains("%PVD%J") Then
- Dim reclines() As String = Receive.Split("%")
- Dim Jobname As String = reclines(2).Substring(1)
- networkpath = reclines(3).Substring(1)
- widthz = reclines(4).Substring(1)
- heightz = reclines(5).Substring(1)
- framez = reclines(6).Substring(1)
- stringz = "%PVD%J" & framez
- If networkpath <> "" Then
- sourcepath = networkpath & "\" & Jobname
- Else
- sourcepath = "\\" & My.Settings.Server & "\POVShare\" & Jobname
- End If
- TBName.Text = Jobname
- TBFrame.Text = framez
- renderstart(sourcepath)
- End If
- End If
- If stringz = "" And framedone = True Then
- framedone = False
- If IO.Directory.Exists(sourcepath & "Images") = False Then
- IO.Directory.CreateDirectory(sourcepath & "Images")
- End If
- Dim destination As String = sourcepath & "Images" & outputimagepath.Substring(outputimagepath.LastIndexOf("\"))
- stringz = "%PVD%F" & framez & "%L" & destination
- If copytry = False Then
- Try
- TBFiles.Text = destination
- IO.File.Copy(outputimagepath, destination, True)
- Catch ex As Exception
- copytry = True
- TBFiles.BackColor = Color.Black
- TBFiles.ForeColor = Color.Red
- TBFiles.Text = jobpath
- End Try
- End If
- End If
- If stringz <> "" Then
- Try
- Dim sendbytes() As Byte = System.Text.Encoding.ASCII.GetBytes(stringz)
- publisher.Send(sendbytes, sendbytes.Length)
- Catch ex As Exception
- End Try
- End If
- End Sub
- 'Connect
- Private Sub PBConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PBConnect.Click
- If TBPortPub.Text Is Nothing Or TBPortPub.Text = "" Then
- MsgBox("PORTA needed.")
- Return
- End If
- If TBPortSub.Text Is Nothing Or TBPortSub.Text = "" Then
- MsgBox("PORTB needed.")
- Return
- End If
- Connect()
- End Sub
- Private Sub Connect()
- Dim pinger As New Net.NetworkInformation.Ping 'Does it ping?
- Try
- Dim pingreply As Net.NetworkInformation.PingReply = pinger.Send(My.Settings.Server, 1000)
- If pingreply.Status <> Net.NetworkInformation.IPStatus.Success Then
- MsgBox("Unable to Ping, verify your IP and PLC addresses are correct", MsgBoxStyle.Exclamation)
- Return
- End If
- Catch ex As Exception
- MsgBox("Ping Error on, verify your IP and PLC addresses are correct", MsgBoxStyle.Exclamation) 'Ping fail so hard that you gotta have further info??
- Return
- End Try
- publisher.Client.Dispose()
- publisher = New UdpClient()
- publisher.Client.Blocking = False
- publisher.ExclusiveAddressUse = False
- subscriber.Client.Dispose()
- subscriber = New UdpClient()
- subscriber.Client.ReceiveTimeout = 100
- subscriber.Client.Blocking = False
- subscriber.ExclusiveAddressUse = False
- subscriber.Client.Bind(New Net.IPEndPoint(Net.IPAddress.Any, TBPortPub.Text))
- publisher.Connect(My.Settings.Server, TBPortSub.Text)
- TimerUDP.Enabled = True
- LBLStatus.Text = "Waiting for Connection"
- End Sub
- 'Directory Copy
- Private Shared Sub DirectoryCopy( _
- ByVal sourceDirName As String, _
- ByVal destDirName As String, _
- ByVal copySubDirs As Boolean)
- Dim dir As DirectoryInfo = New DirectoryInfo(sourceDirName)
- Dim dirs As DirectoryInfo() = dir.GetDirectories()
- If Not dir.Exists Then
- Throw New DirectoryNotFoundException( _
- "Source directory does not exist or could not be found: " _
- + sourceDirName)
- End If
- If Not Directory.Exists(destDirName) Then
- Directory.CreateDirectory(destDirName)
- End If
- Dim files As FileInfo() = dir.GetFiles()
- For Each file In files
- Dim temppath As String = Path.Combine(destDirName, file.Name)
- file.CopyTo(temppath, True)
- Next file
- If copySubDirs Then
- For Each subdir In dirs
- Dim temppath As String = Path.Combine(destDirName, subdir.Name)
- DirectoryCopy(subdir.FullName, temppath, copySubDirs)
- Next subdir
- End If
- End Sub
- 'MCSub
- Private Sub TimerMCSub_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TimerMCSub.Tick
- Dim ep As Net.IPEndPoint = New Net.IPEndPoint(Net.IPAddress.Parse("239.80.8.5"), 10100)
- Try
- Dim rcvbytes() As Byte = MCSub.Receive(ep)
- Dim MCSubRcv As String = System.Text.Encoding.ASCII.GetString(rcvbytes)
- If MCSubRcv.Contains("%PVD%MCS") Then
- Dim rcvlines() As String = MCSubRcv.Split("%")
- TBServer.Text = rcvlines(3)
- TBPortPub.Text = rcvlines(4)
- TBPortSub.Text = rcvlines(5)
- TimerMCSub.Enabled = False
- Try
- Dim stringz As String = "%PVD%MCP%" & mycomputername & "%" & TBDescription.Text
- Dim sendbytes() As Byte = System.Text.Encoding.ASCII.GetBytes(stringz)
- MCSub.Send(sendbytes, sendbytes.Length, ep)
- Catch ex As Exception
- End Try
- Connect()
- End If
- Catch ex As Exception
- End Try
- End Sub
- 'Connection Selection
- Private Sub CBConnections_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CBConnections.SelectedIndexChanged
- For i = 0 To mycomputerconnections(CBConnections.SelectedIndex).GetIPProperties.UnicastAddresses.Count - 1
- If mycomputerconnections(CBConnections.SelectedIndex).GetIPProperties.UnicastAddresses(i).Address.AddressFamily = Net.Sockets.AddressFamily.InterNetwork Then
- Dim bytez() As Byte = mycomputerconnections(CBConnections.SelectedIndex).GetIPProperties.UnicastAddresses(i).Address.GetAddressBytes
- Dim AddressString As String = bytez(0) & "." & bytez(1) & "." & bytez(2) & "." & bytez(3)
- Dim ipep As New Net.IPEndPoint(Net.IPAddress.Parse(AddressString), 10100)
- Try
- MCSub.Close()
- MCSub = New Net.Sockets.UdpClient(Net.Sockets.AddressFamily.InterNetwork)
- MCSub.Client.Blocking = False
- MCSub.Client.ReceiveTimeout = 100
- MCSub.ExclusiveAddressUse = False
- MCSub.Client.MulticastLoopback = True
- MCSub.EnableBroadcast = True
- MCSub.Client.Bind(ipep)
- MCSub.JoinMulticastGroup(Net.IPAddress.Parse("239.80.8.5"), 4)
- LBConnectStatus.ForeColor = Color.LimeGreen
- LBConnectStatus.Text = "√"
- My.Settings.Connection = CBConnections.SelectedIndex
- My.Settings.Save()
- Catch ex As Exception
- LBConnectStatus.ForeColor = Color.Red
- LBConnectStatus.Text = "X"
- End Try
- End If
- Next
- End Sub
- Public Function FileInUse(ByVal sFile As String) As Boolean
- Dim thisFileInUse As Boolean = False
- If System.IO.File.Exists(sFile) Then
- Try
- Using f As New IO.FileStream(sFile, FileMode.Open, FileAccess.ReadWrite, FileShare.None)
- thisFileInUse = False
- End Using
- Catch
- thisFileInUse = True
- End Try
- End If
- Return thisFileInUse
- End Function
- End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement