frmInventoryCoding.vb 8.7 KB
Newer Older
leaky114's avatar
leaky114 已提交
1 2 3 4 5 6
Imports System.Windows.Forms
Imports Inventor.SelectionFilterEnum
Imports Inventor.SelectTypeEnum
Imports Inventor.DocumentTypeEnum
Imports Inventor
Imports Microsoft.Office.Interop
7
Imports stdole
leaky114's avatar
leaky114 已提交
8

leaky114's avatar
leaky114 已提交
9
Public Class frmInventoryCoding
leaky114's avatar
leaky114 已提交
10

leaky114's avatar
leaky114 已提交
11 12 13
    Private Sub btnLoadFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoadFile.Click

        btnLoadFile.Enabled = False
leaky114's avatar
leaky114 已提交
14

leaky114's avatar
leaky114 已提交
15
        lvwFileList.Items.Clear()
leaky114's avatar
leaky114 已提交
16

leaky114's avatar
leaky114 已提交
17 18
        SetStatusBarText()

leaky114's avatar
leaky114 已提交
19 20 21 22
        If IsInventorOpenDoc() = False Then
            Exit Sub
        End If

leaky114's avatar
leaky114 已提交
23
        Dim oAssemblyDocument As AssemblyDocument
leaky114's avatar
leaky114 已提交
24

leaky114's avatar
leaky114 已提交
25
        oAssemblyDocument = ThisApplication.ActiveDocument
leaky114's avatar
leaky114 已提交
26

leaky114's avatar
leaky114 已提交
27
        If oAssemblyDocument.DocumentType <> kAssemblyDocumentObject Then
leaky114's avatar
leaky114 已提交
28 29 30 31
            MsgBox("该功能仅适用于部件")
            Exit Sub
        End If

leaky114's avatar
22.2.25  
leaky114 已提交
32 33 34 35 36 37 38 39 40 41
        '==============================================================================================
        '基于bom结构化数据,可跳过参考的文件
        ' Set a reference to the BOM
        Dim oBOM As BOM
        oBOM = oAssemblyDocument.ComponentDefinition.BOM
        oBOM.StructuredViewEnabled = True

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

leaky114's avatar
leaky114 已提交
42 43
        lvwFileList.BeginUpdate()

leaky114's avatar
22.2.25  
leaky114 已提交
44 45 46 47 48 49 50
        '获取结构化的bom页面
        For Each oBOMView In oBOM.BOMViews
            If oBOMView.ViewType = BOMViewTypeEnum.kStructuredBOMViewType Then
                '遍历这个bom页面
                QueryBOMRowToLoadiPro(oBOMView.BOMRows, lvwFileList)
            End If
        Next
leaky114's avatar
leaky114 已提交
51 52

        lvwFileList.EndUpdate()
leaky114's avatar
22.2.25  
leaky114 已提交
53
        '==============================================================================================
leaky114's avatar
leaky114 已提交
54

leaky114's avatar
22.2.25  
leaky114 已提交
55 56 57 58 59 60 61 62 63 64

        '' 获取所有引用文档
        'Dim oAllReferencedDocuments As DocumentsEnumerator
        'oAllReferencedDocuments = oAssemblyDocument.AllReferencedDocuments

        'With prgProcess
        '    .Minimum = 0
        '    .Maximum = oAllReferencedDocuments.Count
        '    .Value = 0
        'End With
leaky114's avatar
leaky114 已提交
65 66


leaky114's avatar
21.12.3  
leaky114 已提交
67
        ' 遍历这些文档
leaky114's avatar
leaky114 已提交
68

leaky114's avatar
22.2.25  
leaky114 已提交
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
        'For Each ReferencedDocument As Document In oAllReferencedDocuments
        '    Debug.Print(ReferencedDocument.DisplayName)
        '    Dim oStockNumPartName As StockNumPartName
        '    oStockNumPartName = GetPropitems(ReferencedDocument)

        '    Dim LVI As ListViewItem
        '    LVI = lvwFileList.Items.Add(oStockNumPartName.StockNum)
        '    LVI.SubItems.Add(oStockNumPartName.PartName)
        '    LVI.SubItems.Add(oStockNumPartName.PartNum)
        '    LVI.SubItems.Add(ReferencedDocument.FullDocumentName)

        '    prgProcess.Value = prgProcess.Value + 1
        'Next

        btnLoadFile.Enabled = True

    End Sub

    Public Sub QueryBOMRowToLoadiPro(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

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

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

            Dim oInventorDocument As Inventor.Document
            oInventorDocument = ThisApplication.Documents.Open(oFullFileName, False)  '打开文件,不显示

leaky114's avatar
leaky114 已提交
123

leaky114's avatar
leaky114 已提交
124
            Dim oStockNumPartName As StockNumPartName
leaky114's avatar
22.2.25  
leaky114 已提交
125
            oStockNumPartName = GetPropitems(oInventorDocument)
leaky114's avatar
leaky114 已提交
126 127

            Dim LVI As ListViewItem
leaky114's avatar
leaky114 已提交
128
            LVI = lvwFileList.Items.Add(oStockNumPartName.StockNum)
leaky114's avatar
leaky114 已提交
129
            LVI.SubItems.Add(oStockNumPartName.PartName)
leaky114's avatar
leaky114 已提交
130
            LVI.SubItems.Add(oStockNumPartName.PartNum)
leaky114's avatar
22.2.25  
leaky114 已提交
131 132 133
            LVI.SubItems.Add(oFullFileName)
            oInventorDocument.Close(True)
           
leaky114's avatar
leaky114 已提交
134

leaky114's avatar
22.2.25  
leaky114 已提交
135 136 137 138 139 140 141
            '遍历下一级
            If (Not oBOMRow.ChildRows Is Nothing) Then
                Call QueryBOMRowToLoadiPro(oBOMRow.ChildRows, olistiview)
            End If

999:
            'oProgressBar.UpdateProgress()
leaky114's avatar
leaky114 已提交
142 143
        Next

leaky114's avatar
22.2.25  
leaky114 已提交
144
        'oProgressBar.Close()
leaky114's avatar
leaky114 已提交
145

leaky114's avatar
leaky114 已提交
146 147
    End Sub

leaky114's avatar
leaky114 已提交
148
    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
leaky114's avatar
leaky114 已提交
149 150 151
        Me.Close()
    End Sub

leaky114's avatar
leaky114 已提交
152
    Private Sub btnSearchCoding_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearchCoding.Click
leaky114's avatar
leaky114 已提交
153

leaky114's avatar
leaky114 已提交
154 155
        On Error Resume Next
        'PartNum = FindSrtingInSheet(Excel_File_Name, StochNum, Sheet_Name, Table_Array, Col_Index_Num, 0)
leaky114's avatar
leaky114 已提交
156
        btnSearchCoding.Enabled = False
leaky114's avatar
leaky114 已提交
157

leaky114's avatar
leaky114 已提交
158 159
        Dim excelApp As Excel.Application
        excelApp = New Excel.Application
leaky114's avatar
leaky114 已提交
160

leaky114's avatar
leaky114 已提交
161
        'excelApp.Visible = True
leaky114's avatar
leaky114 已提交
162
        'Excel_File_Name = "E:\软件\Invenotr\Inventor编程\InventorAddIn\code\bin\最新物料编码.xls"
leaky114's avatar
leaky114 已提交
163

leaky114's avatar
leaky114 已提交
164
        Dim wb As Excel.Workbook = excelApp.Workbooks.Open(Excel_File_Name, 0, True)
leaky114's avatar
leaky114 已提交
165
        Dim sht As Excel.Worksheet = Nothing
leaky114's avatar
leaky114 已提交
166

leaky114's avatar
leaky114 已提交
167
        Dim userange As Excel.Range = Nothing
leaky114's avatar
leaky114 已提交
168

leaky114's avatar
leaky114 已提交
169 170 171
        For Each sht In wb.Sheets
            'sht = wb.Sheets(Sheet_Name)
            'sht = wb.Sheets("物料")
leaky114's avatar
leaky114 已提交
172

leaky114's avatar
leaky114 已提交
173
            Dim Table_Array(10) As String
leaky114's avatar
leaky114 已提交
174

leaky114's avatar
leaky114 已提交
175
            Table_Array = Split("A,C,D,E", ",")
leaky114's avatar
leaky114 已提交
176

leaky114's avatar
leaky114 已提交
177
            Dim MatchRow As Double   '寻找到的行
leaky114's avatar
leaky114 已提交
178

leaky114's avatar
leaky114 已提交
179
            With prgProcess
leaky114's avatar
leaky114 已提交
180
                .Minimum = 0
leaky114's avatar
leaky114 已提交
181
                .Maximum = lvwFileList.Items.Count
leaky114's avatar
leaky114 已提交
182 183 184
                .Value = 0
            End With

leaky114's avatar
leaky114 已提交
185
            For Each LVI As ListViewItem In lvwFileList.Items
leaky114's avatar
leaky114 已提交
186 187 188 189
                For Each a In Table_Array
                    userange = sht.Range(a & ":" & a)
                    MatchRow = 0
                    MatchRow = excelApp.WorksheetFunction.Match(LVI.Text, userange, 0)
leaky114's avatar
leaky114 已提交
190

leaky114's avatar
leaky114 已提交
191
                    If MatchRow <> 0.0 Then
leaky114's avatar
leaky114 已提交
192

leaky114's avatar
leaky114 已提交
193 194
                        '当前值
                        Dim NowRangeValue As String = LVI.SubItems(2).Text
leaky114's avatar
leaky114 已提交
195

leaky114's avatar
leaky114 已提交
196 197
                        Dim FindRange As String  '寻找的单元
                        Dim FindRangeValue As String    '寻找的值
leaky114's avatar
leaky114 已提交
198

leaky114's avatar
leaky114 已提交
199 200 201
                        'FindRange = "B" & MatchRow
                        FindRange = Col_Index_Num & MatchRow
                        FindRangeValue = sht.Range(FindRange).Value
leaky114's avatar
leaky114 已提交
202

leaky114's avatar
leaky114 已提交
203 204 205 206 207 208
                        If NowRangeValue <> FindRangeValue Then
                            LVI.SubItems(2).Text = FindRangeValue
                            LVI.UseItemStyleForSubItems = False
                            LVI.SubItems(2).ForeColor = Drawing.Color.Red
                            Exit For
                        End If
leaky114's avatar
leaky114 已提交
209
                    End If
leaky114's avatar
leaky114 已提交
210
                Next
leaky114's avatar
leaky114 已提交
211

leaky114's avatar
leaky114 已提交
212
                prgProcess.Value = prgProcess.Value + 1
leaky114's avatar
leaky114 已提交
213
            Next
leaky114's avatar
leaky114 已提交
214 215 216 217 218
        Next
        '关闭文件
        wb.Close()
        ' 8.退出Excel程序
        excelApp.Quit()
leaky114's avatar
leaky114 已提交
219

leaky114's avatar
leaky114 已提交
220 221 222 223 224 225
        '9.释放资源
        System.Runtime.InteropServices.Marshal.ReleaseComObject(userange)
        System.Runtime.InteropServices.Marshal.ReleaseComObject(sht)
        System.Runtime.InteropServices.Marshal.ReleaseComObject(wb)
        System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApp)

leaky114's avatar
leaky114 已提交
226
        btnSearchCoding.Enabled = True
leaky114's avatar
leaky114 已提交
227 228
    End Sub

leaky114's avatar
leaky114 已提交
229
    Private Sub btnWriteCoding_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnWriteCoding.Click
leaky114's avatar
leaky114 已提交
230 231 232 233

        Dim oInventorDocument As Inventor.Document
        Dim FullDocumentName As String
        Dim oCoding As String
leaky114's avatar
leaky114 已提交
234

leaky114's avatar
leaky114 已提交
235
        btnWriteCoding.Enabled = False
leaky114's avatar
leaky114 已提交
236

leaky114's avatar
leaky114 已提交
237
        With prgProcess
leaky114's avatar
leaky114 已提交
238
            .Minimum = 0
leaky114's avatar
leaky114 已提交
239
            .Maximum = lvwFileList.Items.Count
leaky114's avatar
leaky114 已提交
240 241
            .Value = 0
        End With
leaky114's avatar
leaky114 已提交
242

leaky114's avatar
leaky114 已提交
243
        For Each LVI As ListViewItem In lvwFileList.Items
leaky114's avatar
leaky114 已提交
244 245 246 247 248

            If LVI.SubItems(2).ForeColor = Drawing.Color.Red Then
                LVI.SubItems(2).ForeColor = Drawing.Color.Black  '写入后变为黑色
                oCoding = LVI.SubItems(2).Text
                FullDocumentName = LVI.SubItems(3).Text
leaky114's avatar
leaky114 已提交
249 250
                oInventorDocument = ThisApplication.Documents.Open(FullDocumentName, False)
                SetPropitem(oInventorDocument, "成本中心", oCoding)
leaky114's avatar
leaky114 已提交
251
            End If
leaky114's avatar
leaky114 已提交
252

leaky114's avatar
leaky114 已提交
253
            prgProcess.Value = prgProcess.Value + 1
leaky114's avatar
leaky114 已提交
254 255
        Next

leaky114's avatar
leaky114 已提交
256
        btnWriteCoding.Enabled = True
leaky114's avatar
leaky114 已提交
257
    End Sub
leaky114's avatar
leaky114 已提交
258

leaky114's avatar
22.2.25  
leaky114 已提交
259 260 261
    Private Sub lvwFileList_MouseDoubleClick(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles lvwFileList.MouseDoubleClick
        ThisApplication.Documents.Open(lvwFileList.SelectedItems(0).SubItems(3).Text)
    End Sub
leaky114's avatar
leaky114 已提交
262

leaky114's avatar
leaky114 已提交
263
End Class