forked from Codered741/iLogic
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBatch Export Drawing DXF.iLogicVB
More file actions
337 lines (227 loc) · 9.47 KB
/
Copy pathBatch Export Drawing DXF.iLogicVB
File metadata and controls
337 lines (227 loc) · 9.47 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
336
Imports System.IO
Imports System.IO.File
Sub Main()
Dim usrTimeAck = MessageBox.Show("This rule will find all drawings of parts referenced in the BOM and Export them as ACAD DXFs to your desktop. " _
& vbLf & "This process can take some time to run, and cannot be stopped once started. ", "Time Warning", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning)
If usrTimeAck = vbCancel Then
Exit Sub
End If
Dim VaultAddin As Inventor.ApplicationAddIn = ThisApplication.ApplicationAddIns.ItemById("{48B682BC-42E6-4953-84C5-3D253B52E77B}")
VaultAddin.Deactivate()
Try
Dim oDoc as Document = ThisApplication.ActiveDocument
Dim FileNameMinLong as Integer = 21
'check if active document is an assembly
If isAsm(oDoc) = False
MsgBox("Please run from an Assembly")
Return
End If
'write to iLogic Log
SharedVariable("LogVar") = "Batch Export Drawing DXF"
iLogicVB.RunExternalRule("Write SV to Log.iLogicVB")
Dim oAppend As System.IO.StreamWriter
noWrite = True
Dim lstDrawings as New List(of String)
Dim BOMDocs As New List(of String)
'get list of part numbers from Structured BOM
GetDocsFromBOM(ThisApplication.ActiveDocument, BOMDocs)
Dim oProgressBar as Inventor.ProgressBar
' Dim DocCount = oRefDocs.Count
oProgressBar = ThisApplication.CreateProgressBar(False, BOMDocs.Count, "Finding Drawings...")
'Attempt to find a drawing file of the same name as the assembly doc
RefDwgFullFileName = FindDrawingFilePN(oDoc)
oProgressBar.Message = "Checking for drawing of " & oDoc.FullFileName
oProgressBar.UpdateProgress
If RefDwgFullFileName.Length > FileNameMinLong Then
lstDrawings.Add(RefDwgFullFileName)
End If
'Dim bDoc as Document
For Each BOMDoc as String In BOMDocs
bDoc = ThisApplication.Documents.ItemByName(BOMDoc) 'convert full file name to document object
oProgressBar.Message = "Checking for drawing of " & BOMDoc 'oRefDoc.FullFileName
oProgressBar.UpdateProgress
'Attempt to find a drawing file of the same name as the model doc
RefDwgFullFileName = FindDrawingFilePN(bDoc)
If RefDwgFullFileName.Length > FileNameMinLong Then
lstDrawings.Add(RefDwgFullFileName)
End If
Next
oProgressBar.Close
ExportDwgs(oDoc.FullFileName, lstDrawings)
' CopyPostInfo(oDoc)
Catch Ex as Exception
MsgBox("Main error: " & Ex.Message)
Finally
VaultAddin.Activate
End Try
End Sub 'Main
Private Sub ExportDwgs(strFilePath as String, lstDwgsPrint as List(of String))
'Dim PDFCreatorQueue As Queue = New Queue
'Dim job As Object
Dim fullPath As String
Dim oProgressBar As Inventor.ProgressBar = ThisApplication.CreateProgressBar(False, lstDwgsPrint.Count, "Exporting Drawings...")
Try
fullPath = DocumentFileName(strFilePath) 'Path.Combine(Path.GetTempPath, Path.GetTempFileName)
savepath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Desktop) & "\" & IO.Path.GetFileNameWithoutExtension(strFilePath) & "\" & Now.ToString("yyyyMMddHHmm") & "\"
System.IO.Directory.CreateDirectory(savepath)
'MsgBox("Initializing PDFCreator queue...")
'PDFCreatorQueue.Initialize()
'setup options for open
Dim oOptions as NameValueMap = ThisApplication.TransientObjects.CreateNameValueMap
oOptions.Value("DeferUpdates") = True
oOptions.Value("FastOpen") = True
'MsgBox("Printing windows test page...")
For Each DwgName as String In lstDwgsPrint
oProgressBar.Message = "Exporting drawing file: " & DwgName
oProgressBar.UpdateProgress
DWG = ThisApplication.Documents.OpenWithOptions(DwgName, oOptions, True)
ExportDXF(DWG, savepath)
'DWG.SaveAs(savepath & IO.Path.GetFileNameWithoutExtension(DwgName) & ".dxf", True)
'DwgPrint("PDFCreator")
DWG.Close(True)
Next
Process.Start("explorer.exe", "/select," & savepath)
Catch Ex As Exception
MessageBox.Show("ExportDwgs Error: " & Ex.Message)
Finally
oProgressBar.Close
End Try
End Sub
Sub ExportDXF(oDoc as DrawingDocument, Path as String)
'setup translator addin options
Dim DXFAddIn As TranslatorAddIn = ThisApplication.ApplicationAddIns.ItemById("{C24E3AC4-122E-11D5-8E91-0010B541CD80}")
Dim oContext As TranslationContext = ThisApplication.TransientObjects.CreateTranslationContext
oContext.Type = kFileBrowseIOMechanism
Dim oOptions As NameValueMap = ThisApplication.TransientObjects.CreateNameValueMap
Dim oDataMedium As DataMedium = ThisApplication.TransientObjects.CreateDataMedium
Dim strIniFile As String = "C:\_vaultWIP\Designs\Templates\Library\iLogic\dxf_export.ini"
Dim oSheets as Sheets = oDoc.Sheets
Dim oSheet as Sheet '= oSheets.Item(1)
'Iterate through all sheets, exporting and attaching each sheet individually
For Each oSheet in oSheets
oSheet.Activate
'Get sheet number
oCurrentSheet = Mid(oDoc.ActiveSheet.Name, InStr(1, oDoc.ActiveSheet.Name, ":")+1)
'get filename w/o extension
FileName = Path & IO.Path.GetFileNameWithoutExtension(oDoc.FullFileName)
'Append sheet number if more than one sheet
If oSheets.Count > 1 Then
FileNameDXF = FileName & " p" & oCurrentSheet & ".dxf"
Else
FileNameDXF = FileName & ".dxf"
End If
'Writes all configured options to translator addin
If DXFAddIn.HasSaveCopyAsOptions(oDoc, oContext, oOptions) Then
oOptions.Value("Export_Acad_IniFile") = strIniFile
End If
'sets DXF filename to Translator addin
oDataMedium.FileName = FileNameDXF
'Check if we have write access to file, error if not
Try
DXFAddin.SaveCopyAs(oDoc, oContext, oOptions, oDataMedium) 'set all options
Catch Ex As Exception
MessageBox.Show("WHAT DID I JUST SAY!!!! You do not have permission to write to this file, likely because you DIDNT CHECK IT OUT!!!! " & vbLF & Ex.Message, "Permission Denied", MessageBoxButtons.OK, MessageBoxIcon.Error)
Exit Sub
End Try
Next
End Sub 'ExportDXF
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
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
For Each oChildRow as BOMRow in oRow.ChildRows
GetBOMRowDocFile(oChildRow, BOMDocs)
Next
End If
End Sub
Function FindDrawingFilePN(PartOrAssemblyDoc As Document) As String
Dim fullFilenamePN As String
fullFilenamePN = PartOrAssemblyDoc.fullFilename
' Extract the path from the full filename.
Dim path As String = ThisApplication.DesignProjectManager.ActiveDesignProject.WorkspacePath
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 'FindDrawingFilePNN
Private Sub DwgPrint(PrinterName as String)
Dim oPrintMgr As DrawingPrintManager = ThisApplication.ActiveDocument.PrintManager
' Get the name of the printer that will be used.
oPrintMgr.Printer = PrinterName
' Set to print in color.
oPrintMgr.ColorMode = 13313 'kPrintColorPalette
' Set to print one copies.
oPrintMgr.NumberOfCopies = 1
' Set to print using landscape orientation.
oPrintMgr.Orientation = 13570
'13570 'kLandscapeOrientation
'13569 'kPortraitOrientation
' Set the paper size.
oPrintMgr.PaperSize = 14338
'14338 'kPaperSize11x17
' Set to print all sheets.
oPrintMgr.PrintRange = 14082 'kPrintAllSheets
' Set to print full scale.
oPrintMgr.ScaleMode = 13825 'kPrintFullScale
' Submit the print.
oPrintMgr.SubmitPrint
End Sub 'DwgPrint
Private Sub printTestFile()
Dim ShellObj As Object
ShellObj = CreateObject("Shell.Application")
ShellObj.ShellExecute("RUNDLL32.exe", "PRINTUI.DLL,PrintUIEntry /k /n ""PDFCreator""", "", "open", 1)
End Sub
Function DocumentFileName(Doc As String) As String
DocumentFileName = Left(Doc, Doc.Length - 4)
'MsgBox(DocumentFileName)
End Function 'DocumentFileName
Function isAsm(ThisDoc As Document) As Boolean
Debug.Print (ThisDoc.DocumentType)
If ThisDoc.DocumentType = kAssemblyDocumentObject Then
isAsm = True
Else
isAsm = False
End If
End Function 'isAsm