您的位置:网站首页 > Solidworks

获取SolidWorks文件的BOM信息

时间:2010-11-13 11:31:34 来源:未知

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

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

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

001 ''' <summary>
002    ''' 获取BOM视图
003    ''' </summary>
004    ''' <param name="path1">文件路径</param>
005    ''' <param name="dt"></param>
#p#分页标题#e#
006    ''' <remarks></remarks>
007    Public Sub GetBom(ByVal path1 As String, ByVal dt As DataTable)
008  
009        dt.Columns.Add("母件图号")
010  
011        dt.Columns.Add("级别")
012  
013        Dim edmBOMView As IEdmBomView                                   '声明IedmBomView对象,用来获取BOM视图
014  
015        Dim edmBomManager As IEdmBomMgr                                 '定义IEdmBomMgr对象,用来操作BOM
#p#分页标题#e#
016  
017        file8 = vault.GetFileFromPath(path1)                            '根据文件路径来获取文件
018  
019        Dim edmBomArray() As EdmBomLayout                               '定义BOM的样式
020  
021        edmBomManager = vault.CreateUtility(EdmUtility.EdmUtil_BomMgr)  '创建IEdmBomMgr实例
022  
023        edmBomManager.GetBomLayouts(edmBomArray)                        '获取BOM的样式
024  
025        For i As Integer = 0 To UBound(edmBomArray)                     '根据BOM的样式来获取BOM视图
#p#分页标题#e#
026  
027            edmBOMView = file8.GetComputedBOM(edmBomArray(i).mlLayoutID, _
028               file8.CurrentVersion, "default", -1)
029  
030        Next
031  
032        Dim bomRows As Array = Array.CreateInstance(GetType(EdmBomCell), 1) '定义承装BOM行的数组
033  
034        edmBOMView.GetRows(bomRows)                                         '从视图中获取BOM的所有行
035        Dim bomClunms As Array = Array.CreateInstance(GetType(EdmBomColumn), 1) '定义承装BOM列的数组
#p#分页标题#e#
036  
037        edmBOMView.GetColumns(bomClunms)                                    '获取BOM的所有列
038  
039        Dim column As EdmBomColumn                                          '定义BOM列类型的变量
040  
041        For k As Integer = 0 To bomClunms.Length - 1                        '循环列数组来获取列名,添加到DataTable中
042  
043            column = bomClunms.GetValue(k)
044  
045            dt.Columns.Add(column.mbsCaption, GetType(String))
#p#分页标题#e#
046  
047        Next
048  
049        Dim j As Integer = bomRows.GetLength(bomRows.Rank - 1)              '获取BOM行数
050  
051        Dim cell As IEdmBomCell                                             '定义行变量
052  
053        Dim hjPath As String
054  
055        Dim level, fisLevel As Integer
#p#分页标题#e#
056  
057        Dim r As Integer = 1
058  
059        For Each cell In bomRows                                           '循环行,并把行数据添加到DataTable中
060            Dim dr As DataRow = dt.NewRow()
061  
062            Dim pa As String = cell.GetPathName()
063  
064            dt.AcceptChanges()
065  
#p#分页标题#e#
066            For j = 0 To bomClunms.Length - 1
067  
068                Dim column1 As EdmBomColumn = bomClunms.GetValue(j)
069  
070                Dim b As Integer = cell.GetTreeLevel()
071  
072                level = cell.GetTreeLevel + 1
073  
074                Dim value As New Object
075  
076                hjPath = cell.GetPathName()
#p#分页标题#e#
077  
078                Dim poComputedValue As New Object
079  
080                Dim config As String
081  
082                cell.GetVar(column1.mlVariableID, column1.meType, value, poComputedValue, config, True)
083  
084                dr("级别") = b + 1
085  
086                dr(j + 2) = value.ToString()
087  
088            Next
#p#分页标题#e#
089  
090            dt.Rows.Add(dr)
091  
092            If dr("是否自制焊件") = "焊件" Then
093  
094                GetRef(hjPath, level, dr("图号"), dr("零件类型"))
095  
096            End If
097  
098            r += 1
099        Next
100        AdjustTable(dt)                                          '对DataTable进行处理 填充DataTable中的母件图号列
#p#分页标题#e#
101  
102  
103    End Sub
104    ''' <summary>
105    ''' 输出格式
106    ''' </summary>
107    ''' <param name="dt"></param>
108    ''' <remarks></remarks>
109    Private Sub AdjustTable(ByVal dt As DataTable)
110  
111        For i As Integer = 0 To dt.Rows.Count - 1
#p#分页标题#e#
112  
113            Try
114                If i = 0 Then
115  
116                    Continue For
117  
118                End If
119  
120                If dt.Rows(i)("级别") - dt.Rows(i - 1)("级别") = 0 Then
121  
122                    dt.Rows(i)("母件图号") = dt.Rows(i - 1)("母件图号")
#p#分页标题#e#
123  
124                ElseIf dt.Rows(i)("级别") - dt.Rows(i - 1)("级别") > 0 Then
125  
126                    dt.Rows(i)("母件图号") = dt.Rows(i - 1)("图号").ToString
127  
128                Else
129  
130                    dt.Rows(i)("母件图号") = GetPre(i, dt)
131  
132                End If
133  
134            Catch ex As Exception
#p#分页标题#e#
135                'MsgBox(ex.Message.ToString)
136            End Try
137  
138        Next
139    End Sub
140    ''' <summary>
141    ''' 调整输出格式
142    ''' </summary>
143    ''' <param name="i"></param>
144    ''' <param name="dt"></param>
145    ''' <returns></returns>
146    ''' <remarks></remarks>
#p#分页标题#e#
147    Private Function GetPre(ByVal i As Integer, ByVal dt As DataTable) As String
148  
149        Dim strSre As String = dt(0)("图号").ToString
150  
151        For j As Integer = i - 1 To 0 Step -1
152  
153            Try
154                If dt.Rows(j)("级别") = dt.Rows(i)("级别") Then
#p#分页标题#e#
155  
156                    strSre = dt.Rows(j)("母件图号").ToString
157  
158                    Exit For
159  
160                ElseIf dt.Rows(j)("级别") < dt.Rows(i)("级别") Then
161  
162                    strSre = dt.Rows(j)("图号").ToString
163  
164                    Exit For
165  
166                End If
#p#分页标题#e#
167            Catch ex As Exception
168                'MsgBox(ex.Message.ToString)
169            End Try
170              
171  
172        Next
173  
174        Return strSre
175  
176    End Function