I have a macro that is supposed to calculate the volume of my model at different levels. I'm able to set the plane originally however when I try to move the plane again the program just crashes completely.
Is there something I need to release or reset?
Here is my full code:
Dim swApp As SldWorks.SldWorks
Dim swModel As ModelDoc2
Dim swModelDocExt As ModelDocExtension
Dim swPart As PartDoc
Dim swFeat As SldWorks.Feature
Dim swMassProp As MassProperty
Dim swSelMgr As SelectionMgr
Dim swRefPlane As RefPlaneFeatureData
Dim Feature As SldWorks.Feature
Dim boolstatus As Boolean
Dim i As Long
Dim val As Double
Dim level As Long
Option Explicit
Sub main()
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
Set swModelDocExt = swModel.Extension
Set swPart = swModel
Set swMassProp = swModelDocExt.CreateMassProperty
Set swSelMgr = swModel.SelectionManager
boolstatus = swModelDocExt.SelectByID2("Plane5", "PLANE", 0, 0, 0, False, 0, Nothing, swSelectOptionDefault)
Set Feature = swSelMgr.GetSelectedObject5(1)
Set swRefPlane = Feature.GetDefinition
swRefPlane.AccessSelections swPart, Nothing
swRefPlane.Distance = 0# 'Set original distance to 0
Debug.Print "Original offset distance: " & swRefPlane.Distance
Feature.ModifyDefinition swRefPlane, swPart, Nothing
swRefPlane.ReleaseSelectionAccess
For i = level To 279
swRefPlane.AccessSelections swPart, Nothing
swRefPlane.Distance = swRefPlane.Distance + 0.00635 'Adding 1/4"
Debug.Print "Modified offset distance: " & swRefPlane.Distance
Feature.ModifyDefinition swRefPlane, swPart, Nothing
swRefPlane.ReleaseSelectionAccess
val = swMassProp.Volume
Debug.Print "Volume: " & val; "m3"
Next i
End Sub
SolidworksApi/macros