获取SolidWorks文件的BOM信息

今天是个好日子,心情比较沉闷,对自己将来的何去何从又迷茫了。

今天来把上一个月所弄的东西来和大家分享一下。希望大家都来指出不好的地方。

前段时间做了一个读取产品BOM信息的程序。刚开始觉得挺困难,后来慢慢的研究,最终还是解决了。不多说了,看看代码就知道了。写的有点乱(最闹心的地方)

 ''' <summary>

    ''' 获取BOM视图

    ''' </summary>

    ''' <param name="path1">文件路径</param>

    ''' <param name="dt"></param>

    ''' <remarks></remarks>

    Public Sub GetBom(ByVal path1 As String, ByVal dt As DataTable)



        dt.Columns.Add("母件图号")



        dt.Columns.Add("级别")



        Dim edmBOMView As IEdmBomView                                   '声明IedmBomView对象,用来获取BOM视图



        Dim edmBomManager As IEdmBomMgr                                 '定义IEdmBomMgr对象,用来操作BOM



        file8 = vault.GetFileFromPath(path1)                            '根据文件路径来获取文件



        Dim edmBomArray() As EdmBomLayout                               '定义BOM的样式



        edmBomManager = vault.CreateUtility(EdmUtility.EdmUtil_BomMgr)  '创建IEdmBomMgr实例



        edmBomManager.GetBomLayouts(edmBomArray)                        '获取BOM的样式



        For i As Integer = 0 To UBound(edmBomArray)                     '根据BOM的样式来获取BOM视图



            edmBOMView = file8.GetComputedBOM(edmBomArray(i).mlLayoutID, _

               file8.CurrentVersion, "default", -1)



        Next



        Dim bomRows As Array = Array.CreateInstance(GetType(EdmBomCell), 1) '定义承装BOM行的数组



        edmBOMView.GetRows(bomRows)                                         '从视图中获取BOM的所有行

        Dim bomClunms As Array = Array.CreateInstance(GetType(EdmBomColumn), 1) '定义承装BOM列的数组



        edmBOMView.GetColumns(bomClunms)                                    '获取BOM的所有列



        Dim column As EdmBomColumn                                          '定义BOM列类型的变量



        For k As Integer = 0 To bomClunms.Length - 1                        '循环列数组来获取列名,添加到DataTable中



            column = bomClunms.GetValue(k)



            dt.Columns.Add(column.mbsCaption, GetType(String))



        Next



        Dim j As Integer = bomRows.GetLength(bomRows.Rank - 1)              '获取BOM行数



        Dim cell As IEdmBomCell                                             '定义行变量



        Dim hjPath As String



        Dim level, fisLevel As Integer



        Dim r As Integer = 1



        For Each cell In bomRows                                           '循环行,并把行数据添加到DataTable中

            Dim dr As DataRow = dt.NewRow()



            Dim pa As String = cell.GetPathName()



            dt.AcceptChanges()



            For j = 0 To bomClunms.Length - 1



                Dim column1 As EdmBomColumn = bomClunms.GetValue(j)



                Dim b As Integer = cell.GetTreeLevel()



                level = cell.GetTreeLevel + 1



                Dim value As New Object



                hjPath = cell.GetPathName()



                Dim poComputedValue As New Object



                Dim config As String



                cell.GetVar(column1.mlVariableID, column1.meType, value, poComputedValue, config, True)



                dr("级别") = b + 1



                dr(j + 2) = value.ToString()



            Next



            dt.Rows.Add(dr)



            If dr("是否自制焊件") = "焊件" Then



                GetRef(hjPath, level, dr("图号"), dr("零件类型"))



            End If



            r += 1

        Next

        AdjustTable(dt)                                          '对DataTable进行处理 填充DataTable中的母件图号列





    End Sub

    ''' <summary>

    ''' 输出格式

    ''' </summary>

    ''' <param name="dt"></param>

    ''' <remarks></remarks>

    Private Sub AdjustTable(ByVal dt As DataTable)



        For i As Integer = 0 To dt.Rows.Count - 1



            Try

                If i = 0 Then



                    Continue For



                End If



                If dt.Rows(i)("级别") - dt.Rows(i - 1)("级别") = 0 Then



                    dt.Rows(i)("母件图号") = dt.Rows(i - 1)("母件图号")



                ElseIf dt.Rows(i)("级别") - dt.Rows(i - 1)("级别") > 0 Then



                    dt.Rows(i)("母件图号") = dt.Rows(i - 1)("图号").ToString



                Else



                    dt.Rows(i)("母件图号") = GetPre(i, dt)



                End If



            Catch ex As Exception

                'MsgBox(ex.Message.ToString)

            End Try



        Next

    End Sub

    ''' <summary>

    ''' 调整输出格式

    ''' </summary>

    ''' <param name="i"></param>

    ''' <param name="dt"></param>

    ''' <returns></returns>

    ''' <remarks></remarks>

    Private Function GetPre(ByVal i As Integer, ByVal dt As DataTable) As String



        Dim strSre As String = dt(0)("图号").ToString



        For j As Integer = i - 1 To 0 Step -1



            Try

                If dt.Rows(j)("级别") = dt.Rows(i)("级别") Then



                    strSre = dt.Rows(j)("母件图号").ToString



                    Exit For



                ElseIf dt.Rows(j)("级别") < dt.Rows(i)("级别") Then



                    strSre = dt.Rows(j)("图号").ToString



                    Exit For



                End If

            Catch ex As Exception

                'MsgBox(ex.Message.ToString)

            End Try

            



        Next



        Return strSre



    End Function

你可能感兴趣的:(bom)