Hello All,
So i have a macro that, among other things, sets the gauge table and bend table of a sheet metal part. It was working wonderfully(or so I thought) until this morning. I haven't changed anything in it for at least 3 weeks and I dont have any clue as to how to fix it because I don't know what is wrong. It runs through just fine right up until the code where it is supposed to set the bend table. The little excel table pops up in solidworks and then everything freezes. I tried making a new part and it works just fine, it seems to happen on older parts only(I think). If anyone could take a look at it, it would be very much appreciated as I am seriously stumped.
Thanks,
Trevor Wunn
Public Const STOCK_XREF As String = "J:\CP LIBRARY\CP-KRAUSE MATERIAL REFERENCE.xlsx"
Public Const CP_STEEL As String = "J:\Toolbox\Templates\Drawing Standards\Ga. Tables\CP-STEEL.xls"
Public Const CP_BEND As String = "J:\Toolbox\Templates\Drawing Standards\Ga. Tables\CP-BEND DEDUCTIONS.xls"
Public swApp As SldWorks.SldWorks
Public swModel As SldWorks.ModelDoc2
Public swFeat As SldWorks.feature
Public swSubFeat As SldWorks.feature
Public swSelMgr As SldWorks.SelectionMgr
Public kStock As String
Public cpStock As String
Public cpGauge As String
Public cpRadius As String
Public customPropertyMgr As SldWorks.CustomPropertyManager
Public boolStatus As Boolean
Public component As SldWorks.Component2
Option Explicit
Public gaugeTable As String
Sub Main()
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
Set swSelMgr = swModel.SelectionManager
Set swFeat = swModel.FirstFeature
Set customPropertyMgr = swModel.Extension.CustomPropertyManager("")
'get kstocknumber
kStock = swModel.GetCustomInfoValue("", "kstockNumber")
'check to make sure the stock number is correct
If IsNumeric(Right(kStock, 1)) = False Then
MsgBox ("Double Check the Stock Number")
End
End If
'figure out what stock number/material needs to be used
If swModel.GetType = 1 Then
' Stock_Convert
cpStock = swModel.GetCustomInfoValue("", "MaterialPartNo")
Gauge_Table
'Select Base Flange
Do While Not swFeat Is Nothing
Select Case swFeat.GetTypeName
Case "SheetMetal"
Process_SheetMetal swApp, swModel, swFeat
Case "SMBaseFlange"
Process_SMBaseFlange swApp, swModel, swFeat
Case "EdgeFlange"
Process_EdgeFlange swApp, swModel, swFeat
End Select
Set swFeat = swFeat.GetNextFeature
Loop
Else
MsgBox "Can only be used on parts, not assemblies"
End
End If
End Sub
Function Stock_Convert()
Dim myExcelApp As Object
Dim xlSheet As Variant
'Open excel workbook
Set myExcelApp = CreateObject("Excel.Application")
myExcelApp.Visible = False
myExcelApp.WorkBooks.Open FileName:=STOCK_XREF
'get cpStock from .xls file
Set xlSheet = myExcelApp.ActiveSheet
Dim y As Double
y = 1
Do While xlSheet.Cells(y, 1) <> kStock
If xlSheet.Cells(y, 1) <> kStock Then
y = y + 1
End If
Loop
cpStock = xlSheet.Cells(y, 2)
myExcelApp.WorkBooks.Close
End Function
Function Gauge_Table()
Dim myExcelApp As Object
Dim xlSheet As Variant
Dim y As Double
'Figure out which gauge to use
Set myExcelApp = CreateObject("Excel.Application")
myExcelApp.Visible = False
myExcelApp.WorkBooks.Open FileName:=CP_STEEL
Set xlSheet = myExcelApp.ActiveSheet
y = 1
Do While xlSheet.Cells(y, 4) <> cpStock
If xlSheet.Cells(y, 4) <> cpStock Then
y = y + 1
End If
Loop
cpGauge = xlSheet.Cells(y, 1)
myExcelApp.WorkBooks.Close
End Function
Sub Process_SMBaseFlange _
( _
swApp As SldWorks.SldWorks, _
swModel As SldWorks.ModelDoc2, _
swFeat As SldWorks.feature _
)
'set gauge table parameters
Debug.Print " +" & swFeat.Name & " [" & swFeat.GetTypeName & "]"
Dim swBaseFlange As SldWorks.BaseFlangeFeatureData
Set swBaseFlange = swFeat.GetDefinition()
swBaseFlange.UseGaugeTable = True
swBaseFlange.GaugeTablePath = CP_STEEL
swBaseFlange.ThicknessTableName = cpGauge
boolStatus = swFeat.ModifyDefinition(swBaseFlange, swModel, component)
Debug.Print " Gauge Table = " & swBaseFlange.GaugeTablePath
Debug.Print " Thickness = " & swBaseFlange.Thickness / 0.0254
Debug.Print " ThicknessName = " & swBaseFlange.ThicknessTableName
Debug.Print " Used Gauge Table = " & swBaseFlange.UseGaugeTable
End Sub
'set sheet metal properties
Sub Process_SheetMetal _
( _
swApp As SldWorks.SldWorks, _
swModel As SldWorks.ModelDoc2, _
swFeat As SldWorks.feature _
)
Dim swSheetMetal As SldWorks.SheetMetalFeatureData
Set swSheetMetal = swFeat.GetDefinition
Dim swCustBend As SldWorks.customBendallowance
Set swCustBend = swSheetMetal.GetCustomBendAllowance
Process_CustomBendAllowance swApp, swModel, swCustBend, swSheetMetal, swFeat
End Sub
'set sheet metal bend allowances
Sub Process_CustomBendAllowance _
( _
swApp As SldWorks.SldWorks, _
swModel As SldWorks.ModelDoc2, _
swCustBend As SldWorks.customBendallowance, _
swSheetMetal As SldWorks.SheetMetalFeatureData, _
swFeat As SldWorks.feature _
)
swCustBend.BendTableFile = CP_BEND
swCustBend.Type = 1
Debug.Print "Bend Allowance = " & swCustBend.BendAllowance
Debug.Print "Bend Deduction = " & swCustBend.BendDeduction
Debug.Print "Kfactor = " & swCustBend.KFactor
Debug.Print " Bend type = " & swCustBend.Type
Debug.Print " BendTable File = " & swCustBend.BendTableFile
Call swSheetMetal.SetCustomBendAllowance(swCustBend)
boolStatus = swFeat.ModifyDefinition(swSheetMetal, swModel, component)
End Sub
Sub Process_EdgeFlange _
( _
swApp As SldWorks.SldWorks, _
swModel As SldWorks.ModelDoc2, _
swFeat As SldWorks.feature _
)
Dim swEdgeFlange As SldWorks.EdgeFlangeFeatureData
Set swEdgeFlange = swFeat.GetDefinition()
Dim swCustBend As customBendallowance
Set swCustBend = swEdgeFlange.GetCustomBendAllowance
swEdgeFlange.UseDefaultBendRadius = True
swCustBend.BendTableFile = CP_BEND
swCustBend.Type = 1
Call swEdgeFlange.SetCustomBendAllowance(swCustBend)
boolStatus = swFeat.ModifyDefinition(swEdgeFlange, swModel, component)
End Sub
SolidworksApi macros