Lokgard
Mechanical
- Jul 31, 2012
- 1
Hi
I have a macro running on a BOM table, when I run the macro on small assemblies it works with no problems. But as soon as I have a large assembly (over 50 parts) solidworks stop working and has to be terminated (no error message, just a window popping up saying "Close program" or "Look for solutions on the web"). I am using a Component2 command (ComponentReference). It dies as soon as I enter the Else statement below.. I would appreciate all the help I could get, thanks once again! I am using SW 2012 sp4
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swBomTbl As SldWorks.BomTableAnnotation
Dim swBalloonOpt As SldWorks.BalloonOptions
Dim TableAnnotation As SldWorks.TableAnnotation
Dim DrawComponent As SldWorks.DrawingComponent
Dim SelMgr, objApp As Object
Sub main()
Dim I As Long
Dim NumOfObj, SelObjType As Long
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
Set SelMgr = swModel.SelectionManager
NumOfObj = SelMgr.GetSelectedObjectCount
Debug.Print "Number of selected Objects: " & NumOfObj
For I = 1 To NumOfObj
SelObjType = SelMgr.GetSelectedObjectType2(I)
Debug.Print "Type of object selected: " & SelObjType
Select Case SelObjType
Case 98
Set swBomTbl = SelMgr.GetSelectedObject5(I)
Set TableAnnotation = SelMgr.GetSelectedObject5(I)
Debug.Print "BOM Table Selected"
Case 12
' DrawComponent = SelMgr.GetSelectedObject5(I)
Debug.Print "Drawing View Selected"
End Select
Next I
CompRef swBomTbl, TableAnnotation
'AddBalloon DrawComponent
swModel.ForceRebuild3 True
End Sub
Sub CompRef(swBomTbl As SldWorks.BomTableAnnotation, TableAnnotation As SldWorks.TableAnnotation)
Dim Row As Long
Dim WRow As Long
Dim K As Long
Dim MergedCell As Boolean
Dim RowCount, CompCount, ColCount As Long
Dim Component As Component2
Dim CellText As String
RowCount = TableAnnotation.TotalRowCount
ColCount = TableAnnotation.TotalColumnCount
For K = 2 To RowCount - 1
Debug.Print K
Row = K
WRow = K
MergedCell = TableAnnotation.IsCellMerged(Row, 0, WRow, ColCount - 1)
Debug.Print MergedCell
If MergedCell Then
Else
CompCount = swBomTbl.GetComponentsCount(K)
Set Component = swBomTbl.IGetComponents(K, CompCount)
CellText = TableAnnotation.Text(K, 0)
Select Case CellText
Case "--"
MsgBox "Unable to find BOM No. at Row: " & (K + 1)
Case ""
TableAnnotation.DeleteRow (K)
Case Else
Component.ComponentReference = CellText
End Select
End If
Next K
End Sub
//Mattias
I have a macro running on a BOM table, when I run the macro on small assemblies it works with no problems. But as soon as I have a large assembly (over 50 parts) solidworks stop working and has to be terminated (no error message, just a window popping up saying "Close program" or "Look for solutions on the web"). I am using a Component2 command (ComponentReference). It dies as soon as I enter the Else statement below.. I would appreciate all the help I could get, thanks once again! I am using SW 2012 sp4
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swBomTbl As SldWorks.BomTableAnnotation
Dim swBalloonOpt As SldWorks.BalloonOptions
Dim TableAnnotation As SldWorks.TableAnnotation
Dim DrawComponent As SldWorks.DrawingComponent
Dim SelMgr, objApp As Object
Sub main()
Dim I As Long
Dim NumOfObj, SelObjType As Long
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
Set SelMgr = swModel.SelectionManager
NumOfObj = SelMgr.GetSelectedObjectCount
Debug.Print "Number of selected Objects: " & NumOfObj
For I = 1 To NumOfObj
SelObjType = SelMgr.GetSelectedObjectType2(I)
Debug.Print "Type of object selected: " & SelObjType
Select Case SelObjType
Case 98
Set swBomTbl = SelMgr.GetSelectedObject5(I)
Set TableAnnotation = SelMgr.GetSelectedObject5(I)
Debug.Print "BOM Table Selected"
Case 12
' DrawComponent = SelMgr.GetSelectedObject5(I)
Debug.Print "Drawing View Selected"
End Select
Next I
CompRef swBomTbl, TableAnnotation
'AddBalloon DrawComponent
swModel.ForceRebuild3 True
End Sub
Sub CompRef(swBomTbl As SldWorks.BomTableAnnotation, TableAnnotation As SldWorks.TableAnnotation)
Dim Row As Long
Dim WRow As Long
Dim K As Long
Dim MergedCell As Boolean
Dim RowCount, CompCount, ColCount As Long
Dim Component As Component2
Dim CellText As String
RowCount = TableAnnotation.TotalRowCount
ColCount = TableAnnotation.TotalColumnCount
For K = 2 To RowCount - 1
Debug.Print K
Row = K
WRow = K
MergedCell = TableAnnotation.IsCellMerged(Row, 0, WRow, ColCount - 1)
Debug.Print MergedCell
If MergedCell Then
Else
CompCount = swBomTbl.GetComponentsCount(K)
Set Component = swBomTbl.IGetComponents(K, CompCount)
CellText = TableAnnotation.Text(K, 0)
Select Case CellText
Case "--"
MsgBox "Unable to find BOM No. at Row: " & (K + 1)
Case ""
TableAnnotation.DeleteRow (K)
Case Else
Component.ComponentReference = CellText
End Select
End If
Next K
End Sub
//Mattias