frmPrint.vb 16.7 KB
Newer Older
leaky114's avatar
leaky114 已提交
1
Imports System.Windows.Forms
leaky114's avatar
leaky114 已提交
2 3 4 5 6 7 8 9 10
Imports Inventor
Imports System
Imports System.IO
Imports Microsoft
Imports Microsoft.VisualBasic
Imports System.Collections.ObjectModel
Imports stdole
Imports System.Drawing

leaky114's avatar
leaky114 已提交
11
Public Class frmPrint
leaky114's avatar
leaky114 已提交
12 13

    '批量打印开始
leaky114's avatar
leaky114 已提交
14 15
    Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
        If lvwFileListView.Items.Count = 0 Then
leaky114's avatar
leaky114 已提交
16 17 18 19 20
            MsgBox("未添加工程图文件。", MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, "批量另存为")
            Exit Sub
        End If

        Dim sPrinterName As String = ""
leaky114's avatar
leaky114 已提交
21
        sPrinterName = cmbPrinter.Text
leaky114's avatar
leaky114 已提交
22

leaky114's avatar
leaky114 已提交
23 24 25 26 27
        'For i = 0 To lvwFileListView.Items.Count - 1

        For Each olistviewitem As ListViewItem In lvwFileListView.Items
            'lvwFileListView.Items(i).Selected = True
            olistviewitem.Selected = True
leaky114's avatar
leaky114 已提交
28 29 30
            '打开文件
            Dim InvDocFullFileName As String  '工程图全文件名

leaky114's avatar
leaky114 已提交
31
            InvDocFullFileName = olistviewitem.Text
leaky114's avatar
leaky114 已提交
32 33 34 35 36 37 38 39 40

            If IsFileExsts(InvDocFullFileName) = False Then   '跳过不存在的文件
                GoTo 999
            End If

            'If InStr(InvDocFullFileName, ContentCenterFiles) > 0 Then    '跳过零件库文件
            '    GoTo 999
            'End If

leaky114's avatar
leaky114 已提交
41
            Dim oInventorDrawingDocument As Inventor.DrawingDocument
leaky114's avatar
leaky114 已提交
42
            '打开工程图
leaky114's avatar
leaky114 已提交
43
            oInventorDrawingDocument = ThisApplication.Documents.Open(InvDocFullFileName, True)
leaky114's avatar
leaky114 已提交
44

leaky114's avatar
leaky114 已提交
45
            '保存文件
leaky114's avatar
leaky114 已提交
46
            If chkSave.Checked = True Then
leaky114's avatar
leaky114 已提交
47 48
                oInventorDrawingDocument.Save2(True)
            End If
leaky114's avatar
leaky114 已提交
49 50

            '设置签字
leaky114's avatar
leaky114 已提交
51 52
            Dim Print_Day As String
            Print_Day = Today.Year & "." & Today.Month & "." & Today.Day
leaky114's avatar
leaky114 已提交
53
            If chkSign.CheckState = CheckState.Checked Then
leaky114's avatar
leaky114 已提交
54
                SetSign(oInventorDrawingDocument, EngineerName, Print_Day, False)
leaky114's avatar
leaky114 已提交
55
            End If
leaky114's avatar
leaky114 已提交
56 57

            '打印文件
leaky114's avatar
leaky114 已提交
58
            PrintDrawing(oInventorDrawingDocument, sPrinterName)
leaky114's avatar
leaky114 已提交
59

leaky114's avatar
leaky114 已提交
60
            '关闭文件
leaky114's avatar
leaky114 已提交
61
            If chkClose.Checked = True Then
leaky114's avatar
leaky114 已提交
62
                oInventorDrawingDocument.Close(True)
leaky114's avatar
leaky114 已提交
63
            End If
leaky114's avatar
leaky114 已提交
64 65 66 67 68

999:
        Next
        'MsgBox("批量打印工程图完成", MsgBoxStyle.Information + MsgBoxStyle.OkOnly, "批量打印")

leaky114's avatar
leaky114 已提交
69
        lvwFileListView.Items.Clear()
leaky114's avatar
leaky114 已提交
70 71 72 73 74
        SetStatusBarText("批量打印工程图完成")

    End Sub

    '关闭
leaky114's avatar
leaky114 已提交
75 76
    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        lvwFileListView.Items.Clear()
leaky114's avatar
leaky114 已提交
77 78 79 80 81
        Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
        Me.Close()
    End Sub

    '添加文件
leaky114's avatar
leaky114 已提交
82
    Private Sub btnAddFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddFile.Click
leaky114's avatar
leaky114 已提交
83 84 85

        Dim NewOpenFileDialog As New OpenFileDialog

leaky114's avatar
leaky114 已提交
86
        lblsuggest.Visible = False
leaky114's avatar
leaky114 已提交
87 88 89 90 91 92 93 94

        With NewOpenFileDialog
            .Title = "打开"
            .FileName = ""
            .Filter = "AutoDesk Inventor 工程图文件(*.idw)|*.idw" '添加过滤文件
            .Multiselect = True '多开文件打开
            If .ShowDialog = Windows.Forms.DialogResult.OK Then '如果打开窗口OK
                If .FileName <> "" Then '如果有选中文件
leaky114's avatar
leaky114 已提交
95 96 97 98
                    For Each IdwFullFileName As String In .FileNames

                        AddItemToListView(lvwFileListView, IdwFullFileName)

leaky114's avatar
leaky114 已提交
99 100 101 102 103
                    Next
                End If
            Else
                Exit Sub
            End If
leaky114's avatar
leaky114 已提交
104 105 106

            Me.Text = "批量打印  (共" & lvwFileListView.Items.Count & "张)"

leaky114's avatar
leaky114 已提交
107 108 109 110
        End With
    End Sub

    '清空文件列表
leaky114's avatar
leaky114 已提交
111 112
    Private Sub btnClearList_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClearList.Click
        lvwFileListView.Items.Clear()
leaky114's avatar
leaky114 已提交
113
        Me.Text = "批量打印"
leaky114's avatar
leaky114 已提交
114 115 116
    End Sub

    '添加文件夹
leaky114's avatar
leaky114 已提交
117
    Private Sub btnAddFolder_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddFolder.Click
leaky114's avatar
leaky114 已提交
118 119 120
        Dim destinationFolder As String = Nothing
        Dim inf As FileAttributes
        Dim Present_Folder As String = Nothing
leaky114's avatar
leaky114 已提交
121
        Dim oFolderBrowserDialog As New FolderBrowserDialog
leaky114's avatar
leaky114 已提交
122

leaky114's avatar
leaky114 已提交
123
        lblsuggest.Visible = False
leaky114's avatar
leaky114 已提交
124

leaky114's avatar
leaky114 已提交
125
        With oFolderBrowserDialog
leaky114's avatar
leaky114 已提交
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
            .ShowNewFolderButton = False
            .Description = "添加文件夹"
            .RootFolder = System.Environment.SpecialFolder.Desktop
            If .ShowDialog() = Windows.Forms.DialogResult.OK Then
                destinationFolder = .SelectedPath
            Else
                Exit Sub
            End If
        End With

        '是否为文件夹,在其后添加 \,得到父文件夹
        inf = FileSystem.GetAttr(destinationFolder)

        If inf = FileAttributes.Directory Then
            destinationFolder = destinationFolder + "\"
            Present_Folder = My.Computer.FileSystem.GetParentPath(destinationFolder)
        End If

leaky114's avatar
leaky114 已提交
144
        GetAllFile(Present_Folder, destinationFolder, lvwFileListView)
leaky114's avatar
leaky114 已提交
145

leaky114's avatar
leaky114 已提交
146
        Me.Text = "批量打印  (共" & lvwFileListView.Items.Count & "张)"
leaky114's avatar
leaky114 已提交
147 148 149
    End Sub

    '打印文档,打印机名称
leaky114's avatar
leaky114 已提交
150
    Private Sub PrintDrawing(ByVal oInventorDrawingDocument As Inventor.DrawingDocument, ByVal sPrinterName As String)
leaky114's avatar
leaky114 已提交
151 152 153 154 155

        ' Set a reference to the print manager object of the active document.
        ' This will fail if a drawing document is not active.
        Dim oPrintMgr As DrawingPrintManager

leaky114's avatar
leaky114 已提交
156
        oPrintMgr = oInventorDrawingDocument.PrintManager
leaky114's avatar
leaky114 已提交
157 158 159 160 161

        With oPrintMgr
            ' Get the name of the printer that will be used.
            .Printer = sPrinterName

leaky114's avatar
21.12.3  
leaky114 已提交
162
            '所有颜色打印为黑色
leaky114's avatar
leaky114 已提交
163
            If chkBlack.Checked = True Then
leaky114's avatar
leaky114 已提交
164 165 166 167 168 169 170 171
                .AllColorsAsBlack = True
            Else
                .AllColorsAsBlack = False
            End If

            .ColorMode = PrintColorModeEnum.kPrintDefaultColorMode

            '份数
leaky114's avatar
leaky114 已提交
172
            .NumberOfCopies = nudCopies.Value
leaky114's avatar
leaky114 已提交
173 174 175 176 177 178 179 180 181 182

            ' Set to print using portrait orientation.
            .Orientation = PrintOrientationEnum.kDefaultOrientation

            '最佳比例
            .ScaleMode = PrintScaleModeEnum.kPrintBestFitScale

            '设置为默认纸张大小

            ' 如果是打印到打印机,修正为A3
leaky114's avatar
leaky114 已提交
183
            If chkPaperA3.Checked = True Then
leaky114's avatar
leaky114 已提交
184
                Select Case oInventorDrawingDocument.ActiveSheet.Size
leaky114's avatar
leaky114 已提交
185 186 187 188 189 190 191 192 193 194 195 196 197
                    Case DrawingSheetSizeEnum.kA4DrawingSheetSize
                        .PaperSize = PaperSizeEnum.kPaperSizeA4
                    Case DrawingSheetSizeEnum.kA3DrawingSheetSize
                        .PaperSize = PaperSizeEnum.kPaperSizeA3
                    Case DrawingSheetSizeEnum.kA2DrawingSheetSize
                        .PaperSize = PaperSizeEnum.kPaperSizeA3
                    Case DrawingSheetSizeEnum.kA1DrawingSheetSize
                        .PaperSize = PaperSizeEnum.kPaperSizeA3
                    Case DrawingSheetSizeEnum.kA0DrawingSheetSize
                        .PaperSize = PaperSizeEnum.kPaperSizeA3
                End Select
            Else
                '设置为默认纸张大小
leaky114's avatar
leaky114 已提交
198
                Select Case oInventorDrawingDocument.ActiveSheet.Size
leaky114's avatar
leaky114 已提交
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
                    Case DrawingSheetSizeEnum.kA4DrawingSheetSize
                        .PaperSize = PaperSizeEnum.kPaperSizeA4
                    Case DrawingSheetSizeEnum.kA3DrawingSheetSize
                        .PaperSize = PaperSizeEnum.kPaperSizeA3
                    Case DrawingSheetSizeEnum.kA2DrawingSheetSize
                        .PaperSize = PaperSizeEnum.kPaperSizeA2
                    Case DrawingSheetSizeEnum.kA1DrawingSheetSize
                        .PaperSize = PaperSizeEnum.kPaperSizeA1
                    Case DrawingSheetSizeEnum.kA0DrawingSheetSize
                        .PaperSize = PaperSizeEnum.kPaperSizeA0
                End Select

            End If

            '最佳比例
            .ScaleMode = PrintScaleModeEnum.kPrintBestFitScale

            '设置方向
leaky114's avatar
leaky114 已提交
217
            Select Case oInventorDrawingDocument.ActiveSheet.Orientation
leaky114's avatar
leaky114 已提交
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
                Case PageOrientationTypeEnum.kLandscapePageOrientation
                    .Orientation = PrintOrientationEnum.kLandscapeOrientation
                Case PageOrientationTypeEnum.kPortraitPageOrientation
                    .Orientation = PrintOrientationEnum.kPortraitOrientation
            End Select

            ' Set to print all sheets.
            .PrintRange = PrintRangeEnum.kPrintAllSheets

            ' 打印
            .SubmitPrint()

        End With
    End Sub

leaky114's avatar
leaky114 已提交
233
    Private Sub frmPrint_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
leaky114's avatar
leaky114 已提交
234 235 236 237 238
        Dim prtdoc As New Printing.PrintDocument
        Dim strDefaultPrinter As String = prtdoc.PrinterSettings.PrinterName

        Dim strPrinter As String
        For Each strPrinter In Printing.PrinterSettings.InstalledPrinters
leaky114's avatar
leaky114 已提交
239
            cmbPrinter.Items.Add(strPrinter)
leaky114's avatar
leaky114 已提交
240
            If strPrinter = strDefaultPrinter Then
leaky114's avatar
leaky114 已提交
241
                cmbPrinter.SelectedIndex = cmbPrinter.Items.IndexOf(strPrinter)
leaky114's avatar
leaky114 已提交
242 243 244 245
            End If
        Next strPrinter
    End Sub

leaky114's avatar
leaky114 已提交
246
    Private Sub btnLoadAsm_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoadAsm.Click
leaky114's avatar
leaky114 已提交
247
        Dim oOpenFileDialog As New OpenFileDialog
leaky114's avatar
leaky114 已提交
248 249
        Dim AssDoc As AssemblyDocument = Nothing

leaky114's avatar
leaky114 已提交
250
        lblsuggest.Visible = False
leaky114's avatar
leaky114 已提交
251

leaky114's avatar
leaky114 已提交
252
        With oOpenFileDialog
leaky114's avatar
leaky114 已提交
253 254 255 256 257 258 259 260 261 262 263 264 265 266
            .Title = "打开"
            .Filter = "AutoDesk Inventor 部件(*.iam)|*.iam" '添加过滤文件
            .Multiselect = False  '多开文件打开
            .FileName = ""
            If .ShowDialog = Windows.Forms.DialogResult.OK Then '如果打开窗口OK
                If .FileName <> "" Then '如果有选中文件
                    AssDoc = ThisApplication.Documents.Open(.FileName)
                End If
            Else
                Exit Sub
            End If
        End With


leaky114's avatar
leaky114 已提交
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
        '        ' 获取所有引用文档
        '        Dim oAllReferencedDocuments As DocumentsEnumerator
        '        oAllReferencedDocuments = AssDoc.AllReferencedDocuments

        '        ' 遍历这些文档

        '        For Each ReferencedDocument As Inventor.Document In oAllReferencedDocuments

        '            Dim FullFileName As String
        '            FullFileName = ReferencedDocument.FullDocumentName

        '            Dim IdwFullFileName As String
        '            IdwFullFileName = GetNewExtensionFileName(FullFileName, ".idw")

        '            If IsFileExsts(IdwFullFileName) = False Then   '跳过不存在的文件
        '                GoTo 999
        '            End If
leaky114's avatar
leaky114 已提交
284

leaky114's avatar
leaky114 已提交
285 286 287
        '            If InStr(FullFileName, ContentCenterFiles) > 0 Then    '跳过零件库文件
        '                GoTo 999
        '            End If
leaky114's avatar
leaky114 已提交
288

leaky114's avatar
leaky114 已提交
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 337 338 339 340 341 342 343 344 345
        '            lvwFileListView.Items.Add(IdwFullFileName)

        '999:
        '        Next


        '===================================
        '基于bom结构化数据,可跳过参考的文件
        ' Set a reference to the BOM
        Dim oBOM As BOM
        oBOM = AssDoc.ComponentDefinition.BOM
        oBOM.StructuredViewEnabled = True

        'Set a reference to the "Structured" BOMView
        Dim oBOMView As BOMView

        Me.Cursor = Cursors.WaitCursor

        lvwFileListView.BeginUpdate()

        '获取结构化的bom页面
        For Each oBOMView In oBOM.BOMViews
            If oBOMView.ViewType = BOMViewTypeEnum.kStructuredBOMViewType Then
                '遍历这个bom页面
                QueryBOMRowToLoadFile(oBOMView.BOMRows, lvwFileListView)
            End If
        Next

        lvwFileListView.EndUpdate()
        Me.Cursor = Cursors.Default
        Me.Text = "批量打印  (共" & lvwFileListView.Items.Count & "张)"
    End Sub

    Private Sub QueryBOMRowToLoadFile(ByVal oBOMRows As BOMRowsEnumerator, olistiview As ListView)
        Dim i As Integer

        Dim iStepCount As Short
        iStepCount = oBOMRows.Count

        'Create a new ProgressBar object.
        'Dim oProgressBar As Inventor.ProgressBar

        'oProgressBar = ThisApplication.CreateProgressBar(False, iStepCount, "当前文件: ")

        For i = 1 To oBOMRows.Count
            ' Get the current row.
            Dim oBOMRow As BOMRow
            oBOMRow = oBOMRows.Item(i)

            Dim oFullFileName As String
            oFullFileName = oBOMRow.ReferencedFileDescriptor.FullFileName

            '测试文件
            Debug.Print(oFullFileName)

            ' Set the message for the progress bar
            'oProgressBar.Message = oFullFileName
leaky114's avatar
leaky114 已提交
346 347

            Dim IdwFullFileName As String
leaky114's avatar
leaky114 已提交
348
            IdwFullFileName = GetNewExtensionFileName(oFullFileName, ".idw")
leaky114's avatar
leaky114 已提交
349 350 351 352 353

            If IsFileExsts(IdwFullFileName) = False Then   '跳过不存在的文件
                GoTo 999
            End If

leaky114's avatar
leaky114 已提交
354
            If InStr(oFullFileName, ContentCenterFiles) > 0 Then    '跳过零件库文件
leaky114's avatar
leaky114 已提交
355 356 357
                GoTo 999
            End If

leaky114's avatar
leaky114 已提交
358 359 360 361 362 363
            AddItemToListView(lvwFileListView, IdwFullFileName)

            '遍历下一级
            If (Not oBOMRow.ChildRows Is Nothing) Then
                Call QueryBOMRowToLoadFile(oBOMRow.ChildRows, olistiview)
            End If
leaky114's avatar
leaky114 已提交
364 365

999:
leaky114's avatar
leaky114 已提交
366
            'oProgressBar.UpdateProgress()
leaky114's avatar
leaky114 已提交
367 368
        Next

leaky114's avatar
leaky114 已提交
369
        'oProgressBar.Close()
leaky114's avatar
leaky114 已提交
370 371 372

    End Sub

leaky114's avatar
leaky114 已提交
373 374 375

    Private Sub btnRemove_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

leaky114's avatar
leaky114 已提交
376
    End Sub
377

leaky114's avatar
leaky114 已提交
378 379
  

leaky114's avatar
leaky114 已提交
380
    Private Sub btnLoadIdw_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoadIdw.Click
381
        Try
leaky114's avatar
leaky114 已提交
382
            lblsuggest.Visible = False
383 384 385 386 387 388 389

            SetStatusBarText()

            If IsInventorOpenDoc() = False Then
                Exit Sub
            End If

leaky114's avatar
leaky114 已提交
390 391
            For Each oInventorDocument As Inventor.Document In ThisApplication.Documents
                If oInventorDocument.DocumentType = DocumentTypeEnum.kDrawingDocumentObject Then
leaky114's avatar
leaky114 已提交
392 393 394

                    AddItemToListView(lvwFileListView, oInventorDocument.FullDocumentName)

395 396
                End If
            Next
leaky114's avatar
leaky114 已提交
397
            Me.Text = "批量打印  (共" & lvwFileListView.Items.Count & "张)"
398 399 400 401
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
leaky114's avatar
leaky114 已提交
402

leaky114's avatar
leaky114 已提交
403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484
   
    '移除
    Private Sub tsmiRemove_Click(sender As System.Object, e As System.EventArgs) Handles tsmiRemove.Click
        ListViewDel(lvwFileListView)
        Me.Text = "批量打印  (共" & lvwFileListView.Items.Count & "张)"
    End Sub


    '筛选移除
    Private Sub tsmiRemoveFilter_Click(sender As System.Object, e As System.EventArgs) Handles tsmiRemoveFilter.Click
        Dim strFilter As String

        Dim frmInputBox As New frmInputBox
999:
        With frmInputBox
            .txtInPut.Text = ""
            .Text = "筛选文件"
            .lblDescribe.Text = "按图号输入筛选字段。"
            .StartPosition = FormStartPosition.CenterScreen
            .ShowDialog()
        End With

        If (frmInputBox.DialogResult = Windows.Forms.DialogResult.OK) And (frmInputBox.txtInPut.Text <> "") Then
            strFilter = frmInputBox.txtInPut.Text
        Else
            Exit Sub
        End If

        For Each oListViewItem As ListViewItem In lvwFileListView.Items
            Dim InvDocFullFileName As String  '工程图全文件名
            InvDocFullFileName = oListViewItem.Text

            Dim InvDocFullFileONlyName As String
            InvDocFullFileONlyName = GetFileNameInfo(InvDocFullFileName).ONlyName

            If InStr(InvDocFullFileONlyName, strFilter) <> 0 Then
                oListViewItem.Remove()
            End If

        Next
        Me.Text = "批量打印  (共" & lvwFileListView.Items.Count & "张)"
    End Sub


    '筛选保留
    Private Sub tsmiSaveFilter_Click(sender As System.Object, e As System.EventArgs) Handles tsmiSaveFilter.Click
        Dim strFilter As String

        Dim frmInputBox As New frmInputBox
999:
        With frmInputBox
            .txtInPut.Text = ""
            .Text = "筛选文件"
            .lblDescribe.Text = "按图号输入筛选字段。"
            .StartPosition = FormStartPosition.CenterScreen
            .ShowDialog()
        End With

        If (frmInputBox.DialogResult = Windows.Forms.DialogResult.OK) And (frmInputBox.txtInPut.Text <> "") Then
            strFilter = frmInputBox.txtInPut.Text
        Else
            Exit Sub
        End If

        For Each oListViewItem As ListViewItem In lvwFileListView.Items
            Dim InvDocFullFileName As String  '工程图全文件名
            InvDocFullFileName = oListViewItem.Text

            Dim InvDocFullFileONlyName As String
            InvDocFullFileONlyName = GetFileNameInfo(InvDocFullFileName).ONlyName

            If InStr(InvDocFullFileONlyName, strFilter) = 0 Then
                oListViewItem.Remove()
            End If

        Next

        Me.Text = "批量打印  (共" & lvwFileListView.Items.Count & "张)"
    End Sub



leaky114's avatar
21.12.3  
leaky114 已提交
485
End Class