-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAddTimeRemaining.ps1
147 lines (103 loc) · 4.14 KB
/
AddTimeRemaining.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
[CmdletBinding()]
param
(
$InputFilePath
)
# Validate parameter
if ($InputFilePath -eq "")
{
Write-Host "This script needs to be given a G code file to work with. One way to do this is by dragging & dropping a G code file onto the launcher script (AddTimeRemaining.cmd)."
}
elseif (!(Test-Path -Type Leaf $InputFilePath))
{
Write-Host "Couldn't find a file called: $($InputFilePath)"
}
else
{
# Inspect the file indicated, and make sure we have enough disk space (G code files can be quite large!)
$FileInfo = [System.IO.FileInfo]::new($InputFilePath)
$DriveInfo = [System.IO.DriveInfo]::new($FileInfo.Directory.Root)
if ($DriveInfo.TotalFreeSpace -lt ($FileInfo.Length * 1.2))
{
Write-Host "Cannot process this file because there isn't enough free disk space to write out the updated file"
}
else
{
# Open the original (unmodified) file
$Stream = $FileInfo.Open("Open")
$TotalPrintTime = 0
# Jump near the end of the file because we only need to find the last "time elapsed" comment
if ($Stream.Length -gt 100000) { $Stream.Position = $Stream.Length - 100000 }
# Attach a reader to the stream that will leave it open when disposed
$Reader = [System.IO.StreamReader]::new($Stream, [System.Text.Encoding]::UTF8, $true, 131072, $true)
# Scan the file for the largest "time elapsed", which should latch onto the final one that indicates total print time.
Write-Host -NoNewLine "Scanning file..."
$LineCount = 0
while ($true)
{
$Line = $Reader.ReadLine()
if ($Line -eq $null) { break }
$LineCount++
if ($LineCount % 10000 -eq 0) { Write-Host -NoNewLine "." }
if ($Line.StartsWith(";TIME_ELAPSED:"))
{
$Elapsed = [decimal]$Line.Substring(14)
if ($Elapsed -gt $TotalPrintTime) { $TotalPrintTime = $Elapsed }
}
}
Write-Host ""
if ($TotalPrintTime -eq 0)
{
Write-Host "Error: Unable to find print time estimates in this file (maybe it's not a G-code file?)"
}
else
{
Write-Host "Found total print time: $($TotalPrintTime) seconds"
$TotalPrintTimeSpan = [System.TimeSpan]::FromSeconds($TotalPrintTime)
Write-Host "=> $($TotalPrintTimeSpan)"
# Go back to the start of the file, this time to process it
$Stream.Position = 0
$Reader = [System.IO.StreamReader]::new($Stream)
# Find a temporary filename to which to write the updated file
$ContainingDirectory = $FileInfo.Directory.FullName
while ($true)
{
$NewTempName = [System.IO.Path]::Combine($ContainingDirectory, [Guid]::NewGuid().ToString("n"))
if (!(Test-Path $NewTempName)) { break }
}
$Writer = [System.IO.StreamWriter]::new($NewTempName)
# Process the contents
Write-Host -NoNewLine "Processing file..."
$AddedCommands = 0
$LineCount = 0
while ($true)
{
$Line = $Reader.ReadLine()
if ($Line -eq $null) { break }
$LineCount++
if ($LineCount % 10000 -eq 0) { Write-Host -NoNewLine "." }
$Writer.WriteLine($Line)
if ($Line.StartsWith(";TIME_ELAPSED:"))
{
$Elapsed = [decimal]$Line.Substring(14)
$Remaining = $TotalPrintTime - $Elapsed
$Writer.WriteLine("M73 R{0:########0}", $Remaining)
$AddedCommands++
}
}
Write-Host ""
# Rename the files, so that the original is moved out of the way and the new temporary file takes its name
$Reader.Close()
$Writer.Close()
$TargetName = $FileInfo.FullName
$OriginalFileName = $FileInfo.Name
$OriginalFileName = [System.IO.Path]::GetFileNameWithoutExtension($OriginalFileName) + "-ORIGINAL" + [System.IO.Path]::GetExtension($OriginalFileName)
$OriginalFileName = [System.IO.Path]::Combine($ContainingDirectory, $OriginalFileName)
$NewFileInfo = [System.IO.FileInfo]::new($NewTempName)
Write-Host "Added $($AddedCommands) commands, file size increased by $($NewFileInfo.Length - $FileInfo.Length) from $($FileInfo.Length) to $($NewFileInfo.Length) bytes"
$FileInfo.MoveTo($OriginalFileName)
$NewFileInfo.MoveTo($TargetName)
}
}
}
Pause