-
Notifications
You must be signed in to change notification settings - Fork 1
/
NXZipV2.vb
230 lines (195 loc) · 8.58 KB
/
NXZipV2.vb
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
'This is a modification of the original file from the community.
'Added parameters to allow locating master model drawings in the same folder matching a specific format.
'Added parameters to exclude folder locations from inclusion
'Only minimal testing has been done.
Option Strict Off
Imports System
Imports System.IO
Imports System.Windows.Forms
Imports System.Environment
Imports System.Collections
Imports NXOpen
Imports NXOpen.UF
Imports System.Text.RegularExpressions
Module NXJournal
Dim theSession As Session = Session.GetSession()
Dim theUFSession As UFSession = UFSession.GetUFSession()
Dim regexMasterModel As Regex
Dim skipfolders() As String
Sub Echo(ByVal output As String)
theSession.ListingWindow.Open()
theSession.ListingWindow.WriteLine(output)
theSession.LogFile.WriteLine(output)
End Sub
Function GetComponentFullPath(ByVal comp As Assemblies.Component) As String
Dim partName As String = ""
Dim refsetName As String = ""
Dim instanceName As String = ""
Dim origin(2) As Double
Dim csysMatrix(8) As Double
Dim transform(3, 3) As Double
theUFSession.Assem.AskComponentData(comp.Tag, partName, refsetName,
instanceName, origin, csysMatrix, transform)
Return partName
End Function
Sub GetComponentsFullPaths(ByVal thisComp As Assemblies.Component, ByVal parts As ArrayList)
Dim thisPath As String = GetComponentFullPath(thisComp)
If Not parts.Contains(thisPath) Then parts.Add(thisPath)
For Each child As Assemblies.Component In thisComp.GetChildren()
GetComponentsFullPaths(child, parts)
Next
End Sub
Sub Main(ByVal args As String())
Dim workPart As BasePart = theSession.Parts.Work
Dim masterModelRegex As String = "" '"[BASENAME]_dwg\d.prt"
Dim processSessionParts As Boolean = False
'Do we have a regex for the master drawing?
If args(0).Contains("[BASENAME]") Then
masterModelRegex = args(0)
Echo("Maste Model RegEx=" & masterModelRegex)
End If
'Do we have a parameter?
If args.Length > 1 AndAlso (args(1) = "SESSIONPARTS") Then
processSessionParts = True
Echo("Processing all session parts...")
End If
'Do we have excludes?
If args.Length > 2 Then
skipfolders = args(2).Split("|")
End If
If workPart Is Nothing Then
If args.Length = 0 Then
Echo("Part file argument expected or work part required")
Return
End If
theSession.Parts.LoadOptions.ComponentsToLoad =
LoadOptions.LoadComponents.None
theSession.Parts.LoadOptions.SetInterpartData(True,
LoadOptions.Parent.All)
theSession.Parts.LoadOptions.UsePartialLoading = True
Dim partLoadStatus1 As PartLoadStatus = Nothing
workPart = theSession.Parts.OpenBaseDisplay(args(0), partLoadStatus1)
If workPart Is Nothing Then Return
End If
Dim paths As ArrayList = New ArrayList
paths.Add(workPart.FullPath)
CheckForMasterModel(workPart.FullPath, masterModelRegex, paths)
Dim root As Assemblies.Component = workPart.ComponentAssembly.RootComponent
If root IsNot Nothing Then
GetComponentsFullPaths(root, paths)
End If
If processSessionParts Then
For Each aPart As BasePart In theSession.Parts
If Not paths.Contains(aPart.FullPath) Then
paths.Add(aPart.FullPath)
CheckForMasterModel(aPart.FullPath, masterModelRegex, paths)
End If
Next
End If
Dim tmp_dir As String = "C:\TEMP"
theUFSession.UF.TranslateVariable("UGII_TMP_DIR", tmp_dir) ' See PR 6271239!
Dim listFile As String = tmp_dir & "\" & workPart.Leaf & ".txt"
Echo("List file is: " & listFile)
Dim outFile As IO.StreamWriter = My.Computer.FileSystem.OpenTextFileWriter(listFile, False)
For Each aPath As String In paths
If Not ExcludedPath(aPath) Then
If File.Exists(aPath) Then
Echo("Adding: " & aPath)
outFile.WriteLine(aPath)
Else
Echo("Not Found: " & aPath)
End If
End If
Next
outFile.Close()
Dim outputFolder As String = AskUserOutputPath(tmp_dir)
Dim archive As String = outputFolder & "\" & workPart.Leaf & ".7z"
Dim path7z As String = Nothing
If File.Exists("c:\APPS\7-Zip\7z.exe") Then
path7z = "c:\APPS\7-Zip\7z"
ElseIf File.Exists("C:\Program Files (x86)\7-Zip\7z.exe") Then
path7z = "C:\Program Files (x86)\7-Zip\7z"
ElseIf File.Exists("C:\Program Files\7-Zip\7z.exe") Then
path7z = "C:\Program Files\7-Zip\7z"
Else
Echo("Cannot find 7-Zip\7z.exe")
Return
End If
Dim command As String = """" & path7z & """ a """ & archive & """ @""" & listFile & """"
Echo(command)
Shell(command, , True)
If File.Exists(archive) Then
Echo("Created: " & archive)
Else
Echo("Failed : " & archive)
End If
' The list file is not deleted in case it needs to be editted and reused
'File.Delete(listFile)
End Sub
Private Function ExcludedPath(filename As String) As Boolean
If skipfolders Is Nothing Then Return False
For Each exclude As String In skipfolders
If filename.StartsWith(exclude, StringComparison.InvariantCultureIgnoreCase) Then
Return True
End If
Next
Return False
End Function
Private Function CheckForMasterModel(ByVal fullpath As String, ByVal match As String, ByRef paths As ArrayList) As Integer
' Process the list of files found in the directory.
'"[BASENAME]_dwg\d.prt"
If match.Contains("[BASENAME]") Then
Dim containingFolder As String = Path.GetDirectoryName(fullpath)
Dim baseName As String = Path.GetFileNameWithoutExtension(fullpath)
Dim folderFiles As String() = Directory.GetFiles(containingFolder, "*.prt")
Dim exactMatch As String = match.Replace("[BASENAME]", baseName)
For Each fl As String In folderFiles
If Regex.IsMatch(fl, match, RegexOptions.IgnoreCase) Then
If Not paths.Contains(fl) Then
paths.Add(fl)
Echo("Master!!!: " & fl)
End If
End If
Next
End If
End Function
Function AskUserOutputPath(strDefaultOutputFolder As String) As String
Dim strLastPath As String = ""
Dim strOutputPath As String = ""
'Key will show up in HKEY_CURRENT_USER\Software\VB and VBA Program Settings
Try
'Get the last path used from the registry
strLastPath = GetSetting("NX journal", "NX Zip", "ExportPath")
'msgbox("Last Path: " & strLastPath)
Catch e As ArgumentException
Catch e As Exception
MsgBox(e.GetType.ToString)
Finally
End Try
Dim FolderBrowserDialog1 As New FolderBrowserDialog
' Then use the following code to create the Dialog window
' Change the .SelectedPath property to the default location
With FolderBrowserDialog1
' Desktop is the root folder in the dialog.
.RootFolder = Environment.SpecialFolder.Desktop
' Select the strDefaultOutputFolder directory on entry.
If Directory.Exists(strLastPath) Then
.SelectedPath = strLastPath
Else
.SelectedPath = strDefaultOutputFolder
End If
' Prompt the user with a custom message.
.Description = "Select the directory to export .7 zip file"
If .ShowDialog = DialogResult.OK Then
' Display the selected folder if the user clicked on the OK button.
AskUserOutputPath = .SelectedPath
' save the output folder path in the registry for use on next run
SaveSetting("NX journal", "NX Zip", "ExportPath", .SelectedPath)
Else
'user pressed 'cancel', exit the subroutine
AskUserOutputPath = Nothing
'exit sub
End If
End With
End Function
End Module