-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathMissing Drawings.iLogicVB
More file actions
335 lines (238 loc) · 9.51 KB
/
Missing Drawings.iLogicVB
File metadata and controls
335 lines (238 loc) · 9.51 KB
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
'AUTHOR: Cody Redding
'CONTACT: Codered741@gmail.com
'LAST MODIFIED: 20170721
'STATUS: In Progress
'LICENSE: Free to redistribute without authorization
'FUNCTION: Create a report of all "Make" parts that are missing drawings
Imports System.IO
Sub Main()
SharedVariable("LogVar") = "Missing Drawings"
iLogicVb.RunExternalRule("Write SV to Log.iLogicVB")
MissingDrawings()
End Sub
Sub MissingDrawings()
'check that the active document is an assembly file
If ThisApplication.ActiveDocument.DocumentType <> kAssemblyDocumentObject Then
MessageBox.Show("Please run this rule from the assembly file.", "iLogic")
Exit Sub
End If
'declarations
Dim oAsmDoc As AssemblyDocument 'define the active document as an assembly file
oAsmDoc = ThisApplication.ActiveDocument
Dim oRefDocs As DocumentsEnumerator = oAsmDoc.AllReferencedDocuments 'look at the files referenced by the assembly
Dim oRefDoc As Document
Dim oFile As String
oFile = ThisDoc.PathAndFileName(False) & ".txt"
Dim oAppend As System.IO.StreamWriter
oAppend = IO.File.AppendText(oFile)
Dim bFoundFile As Boolean = False
Dim PrtsMissingDwgs As New List(Of String)
Dim PrtsMissingMB As New List(Of String)
'get user input
RUsure = MessageBox.Show("This operation will search for drawing files that do NOT exist in your Vault workspace. " _
& vbLf & " Drawings may exist in the Vault, but will not be counted in this operation. See a Vault Admin for assistance. " _
& vbLf & " " _
& vbLf & "Are you sure you want to create this report for all of the assembly components?" _
& vbLf & "This operation could take some time.", "Find Missing Drawings ", MessageBoxButtons.YesNo)
' If RUsure = vbNo Then
' Exit Sub
' End If
oAppend.WriteLine ("Report created - " & DateString & ":" & TimeString) '& " by " & ThisApplication.UserName)
'oAppend.Flush()
dwgPathName = FindDrawingFilePN(oAsmDoc)
If dwgPathName = "" Then
PrtsMissingDwgs.Add(oAsmDoc.FullDocumentName)
bFoundFile = True
Else
'oAppend.WriteLine("FOUND : " & dwgPathName & " FOR : " & oRefDoc.FullDocumentName)
End If
Dim BOMDocs As New List(of String)
'get list of part numbers from Structured BOM
GetDocsFromBOM(oAsmDoc, BOMDocs)
For Each BOMDoc as String In BOMDocs
bDoc = ThisApplication.Documents.ItemByName(BOMDoc) 'convert full file name to document object
'Attempt to find a drawing file of the same name as the model doc
RefDwgFullFileName = FindDrawingFilePN(bDoc)
If bDoc.DocumentType = kPartDocumentObject OrElse bDoc.DocumentType = kAssemblyDocumentObject Then
' Dim props as PropertySet
props = bDoc.PropertySets.Item("Inventor User Defined Properties")
Try
'Test if the MakeBuy property exists
MBProp = props.Item("MakeBuy")
If MBProp.Value = "Make" Then
'check to see that the model has a drawing in the project workspace
dwgPathName = FindDrawingFilePN(bDoc)
If dwgPathName = "" Then
'add windows search for duplicates
PrtsMissingDwgs.Add(bDoc.FullDocumentName)
bFoundFile = True
End If
End If
Catch
PrtsMissingMB.Add(bDoc.FullDocumentName)
bFoundFile = True
End Try
End If
Next
'work the the drawing files for the referenced models
'this expects that the model has a drawing of the same name
' For Each oRefDoc In oRefDocs
' If oRefDoc.DocumentType = kPartDocumentObject OrElse oRefDoc.DocumentType = kAssemblyDocumentObject Then
' ' Dim props as PropertySet
' props = oRefDoc.PropertySets.Item("Inventor User Defined Properties")
' Try
' MBProp = props.Item("MakeBuy")
' If MBProp.Value = "Make" Then
' 'check to see that the model has a drawing in the project workspace
' dwgPathName = FindDrawingFile(oRefDoc)
' If dwgPathName = "" Then
' PrtsMissingDwgs.Add(oRefDoc.FullDocumentName)
' bFoundFile = True
' Else
' 'oAppend.WriteLine("FOUND : " & dwgPathName & " FOR : " & oRefDoc.FullDocumentName)
' End If
' End If
' Catch
' 'oAppend.WriteLine("MAKE-BUY PROPERTY NOT FOUND : " & oRefDoc.FullDocumentName)
' PrtsMissingMB.Add(oRefDoc.FullDocumentName)
' bFoundFile = True
' End Try
' End If
' Next
'- - - - - - - - - - - - -
If bFoundFile = True Then
PrtsMissingDwgs.Sort()
For Each doc In PrtsMissingDwgs
oAppend.WriteLine("MISSING DRAWING FOR : " & doc)
Next
PrtsMissingMB.Sort()
For Each doc In PrtsMissingMB
oAppend.WriteLine("MAKE-BUY PROPERTY NOT FOUND : " & doc)
Next
oAppend.WriteLine("---End of Report---")
oAppend.Flush()
oAppend.Close()
ThisDoc.Launch(oFile)
If PrtsMissingDwgs.Count > 0 Then
OpenSParts = MessageBox.Show("Would you like to open all the parts that are missing drawings?", "Open Parts?", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
If OpenSParts = vbYes Then
For Each doc In PrtsMissingDwgs
ThisApplication.Documents.Open(doc)
ThisApplication.CommandManager.ControlDefinitions.Item("AppZoomAllCmd").Execute
Next
End If
End If
If PrtsMissingMB.Count > 0 Then
OpenSParts = MessageBox.Show("Would you like to open all the parts that are missing Smart Part Properties?", "Open Parts?", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
If OpenSParts = vbYes Then
For Each doc In PrtsMissingMB
ThisApplication.Documents.Open(doc)
ThisApplication.CommandManager.ControlDefinitions.Item("AppZoomAllCmd").Execute
Next
End If
End If
Else
MessageBox.Show("No parts are missing drawings", "Good Job!")
oAppend.WriteLine ("Nothing to Report")
oAppend.WriteLine("---End of Report---")
oAppend.Flush()
oAppend.Close()
End If
End Sub
Function FindDrawingFile(PartOrAssemblyDoc As Document)
Dim fullFilename As String
fullFilename = PartOrAssemblyDoc.fullFilename
' Extract the path from the full filename.
Dim path As String
path = ThisApplication.DesignProjectManager.ActiveDesignProject.WorkspacePath
' Extract the filename from the full filename.
Dim filename As String
filename = Right$(fullFilename, Len(fullFilename) - InStrRev(fullFilename, "\"))
' Replace the extension with "dwg"
filename = Left$(filename, InStrRev(filename, ".")) & "dwg"
' Find if the drawing exists.
Dim drawingFilename As String
drawingFilename = ThisApplication.DesignProjectManager.ResolveFile(path, filename)
' Check the result.
If drawingFilename = "" Then
' Try again with idw extension.
filename = Left$(filename, InStrRev(filename, ".")) & "idw"
' Find if the drawing exists.
drawingFilename = ThisApplication.DesignProjectManager.ResolveFile(path, filename)
' Return the result.
If drawingFilename <> "" Then
FindDrawingFile = drawingFilename
Else
FindDrawingFile = ""
End If
Else
' Return the result.
FindDrawingFile = drawingFilename
End If
End Function
Sub GetDocsFromBOM(oDoc as Document, ByRef BOMDocs as List(of String))
If oDoc.DocumentType <> kAssemblyDocumentObject Then
MsgBox("This rule can only be run from an assembly")
Exit Sub
End If
Dim oDocBOM As BOM = oDoc.ComponentDefinition.BOM
oDocBOM.StructuredViewEnabled = True
oDocBOM.StructuredViewFirstLevelOnly = False
oDocBOM.PartsOnlyViewEnabled = False
Dim oBOMView as BOMView = oDocBOM.BOMViews.Item("Structured")
Dim oBOMRows As BOMRowsEnumerator = oBOMView.BOMRows
Dim oCompDef As ComponentDefinition
Dim oPNProp as Inventor.Property
For Each oRow as BOMRow in oBOMRows
GetBOMRowDocFile(oRow, BOMDocs)
Next
'BOMDocs.Sort()
End Sub
Sub GetBOMRowDocFile(oRow as BOMRow, ByRef BOMDocs as List(of String))
Dim oCompDef As ComponentDefinition
'Add the part number of the current row, for parts that have children
oCompDef = oRow.ComponentDefinitions.Item(1)
If oCompDef.Type = 100675072 Then 'exclude Virtual Components
'do nothing
Else
DocName = oCompDef.Document.FullFileName
If Not BOMDocs.Contains(DocName) Then
BOMDocs.Add(DocName)
End If
End If
If Not oRow.ChildRows Is Nothing Then
If Not BOMDocs.Contains(DocName) Then
BOMDocs.Add(DocName)
End If
For Each oChildRow as BOMRow in oRow.ChildRows
GetBOMRowDocFile(oChildRow, BOMDocs)
Next
End If
End Sub
Function FindDrawingFilePN(PartOrAssemblyDoc As Document) As String 'find drawing (dwg or idw) of Model Document by Part number. Drawing file name should match part number.
Dim fullFilenamePN As String
fullFilenamePN = PartOrAssemblyDoc.fullFilename
' Extract the path from the full filename.
Dim path As String = ThisApplication.DesignProjectManager.ActiveDesignProject.WorkspacePath
'path = Left$(fullFilenamePN, InStrRev(fullFilenamePN, "\"))
Dim iProps as PropertySet = PartOrAssemblyDoc.PropertySets.Item("Design Tracking Properties")
Dim pn = iProps.Item("Part Number")
Dim filename As String = pn.Value
' Find if the drawing exists.
Dim drawingFilename As String
drawingFilename = ThisApplication.DesignProjectManager.ResolveFile(path, filename & ".dwg")
' Check the result.
If drawingFilename = "" Then
' Find if the drawing exists.
drawingFilename = ThisApplication.DesignProjectManager.ResolveFile(path, filename & ".idw")
' Return the result.
If drawingFilename <> "" Then
Return drawingFilename
Else
Return ""
End If
Else
' Return the result.
Return drawingFilename
End If
End Function 'FindDrawingFilePN