1. stp file is not generated
2. document is not closed
I made some edit to the swb file to resolve the issue. See following code
I try running it as swb directly and it is working properly.
Code: Select all
' ******************************************************************************
'Purpose: Automation for 3D ellipse-approximation model construction in SolidWorks
'******************************************************************************
Option Explicit '***
Dim swApp As SldWorks.SldWorks '***
Dim Part As SldWorks.ModelDoc2 '***
Dim boolstatus As Boolean
Dim longstatus As Long, longwarnings As Long
Dim swSheetWidth As Double
Dim swPart As SldWorks.PartDoc '***
Dim myModelView As SldWorks.ModelView '***
Dim swSheetHeight As Double
Dim skSegment As SldWorks.SketchSegment '***
Dim swFeat As SldWorks.Feature
Dim swFeatMgr As SldWorks.FeatureManager
Dim swFeatData As Object
Dim myFeature As Object
Dim partTitle As String 'NEW var
Sub main()
Set swApp = Application.SldWorks
' New Document
swSheetWidth = 0
swSheetHeight = 0
Set Part = swApp.NewDocument("C:\ProgramData\SolidWorks\SOLIDWORKS 2020\templates\Part.prtdot", 0, swSheetWidth, swSheetHeight) '[*]
Set Part = swApp.ActiveDoc
Set swPart = Part
'swApp.ActivateDoc2 "Part10", False, longstatus '***, not required, consider remove?
Set Part = swApp.ActiveDoc
Set myModelView = Part.ActiveView
myModelView.FrameState = swWindowState_e.swWindowMaximized
'insert ext X curve
Part.InsertCurveFileBegin
Part.InsertCurveFilePoint 0.011316, 0#, 0#
Part.InsertCurveFilePoint 0.011343, 0#, 0.00585
Part.InsertCurveFilePoint 0.011366, 0#, 0.0117
Part.InsertCurveFilePoint 0.01139, 0#, 0.01755
Part.InsertCurveFilePoint 0.011407, 0#, 0.0234
Part.InsertCurveFilePoint 0.011406, 0#, 0.02769
Part.InsertCurveFilePoint 0.011406, 0#, 0.030186
Part.InsertCurveFilePoint 0.011394, 0#, 0.032526
Part.InsertCurveFilePoint 0.011379, 0#, 0.034788
Part.InsertCurveFilePoint 0.01137, 0#, 0.036894
Part.InsertCurveFilePoint 0.011362, 0#, 0.038844
Part.InsertCurveFilePoint 0.011369, 0#, 0.040716
Part.InsertCurveFilePoint 0.011378, 0#, 0.042432
Part.InsertCurveFilePoint 0.011394, 0#, 0.044148
Part.InsertCurveFilePoint 0.011392, 0#, 0.04563
Part.InsertCurveFilePoint 0.011418, 0#, 0.047112
Part.InsertCurveFilePoint 0.011439, 0#, 0.048516
Part.InsertCurveFilePoint 0.011465, 0#, 0.049764
Part.InsertCurveFilePoint 0.011487, 0#, 0.051012
Part.InsertCurveFilePoint 0.011546, 0#, 0.052104
Part.InsertCurveFilePoint 0.011531, 0#, 0.053196
Part.InsertCurveFilePoint 0.011491, 0#, 0.05421
Part.InsertCurveFilePoint 0.011448, 0#, 0.055146
Part.InsertCurveFilePoint 0.01145, 0#, 0.056082
Part.InsertCurveFilePoint 0.011514, 0#, 0.057018
Part.InsertCurveFilePoint 0.011542, 0#, 0.058032
Part.InsertCurveFilePoint 0.010607, 0#, 0.059124
Part.InsertCurveFilePoint 0.009938, 0#, 0.060216
Part.InsertCurveFilePoint 0.009883, 0#, 0.061464
Part.InsertCurveFilePoint 0.009859, 0#, 0.062712
Part.InsertCurveFilePoint 0.009839, 0#, 0.064116
Part.InsertCurveFilePoint 0.00991, 0#, 0.065598
Part.InsertCurveFilePoint 0.010107, 0#, 0.06708
Part.InsertCurveFilePoint 0.010225, 0#, 0.068796
Part.InsertCurveFilePoint 0.010287, 0#, 0.070512
Part.InsertCurveFilePoint 0.010333, 0#, 0.072384
Part.InsertCurveFilePoint 0.010392, 0#, 0.074334
Part.InsertCurveFilePoint 0.010435, 0#, 0.07644
Part.InsertCurveFilePoint 0.010486, 0#, 0.078702
Part.InsertCurveFilePoint 0.010544, 0#, 0.081042
Part.InsertCurveFilePoint 0.010595, 0#, 0.083538
Part.InsertCurveFilePoint 0.010641, 0#, 0.087828
Part.InsertCurveFilePoint 0.010683, 0#, 0.093678
Part.InsertCurveFilePoint 0.010722, 0#, 0.099528
Part.InsertCurveFilePoint 0.010769, 0#, 0.105378
Part.InsertCurveFilePoint 0.010829, 0#, 0.111228
Part.InsertCurveFileEnd
'insert ext Y curve
Part.InsertCurveFileBegin
Part.InsertCurveFilePoint 0#, 0.008753, 0#
Part.InsertCurveFilePoint 0#, 0.008753, 0.00585
Part.InsertCurveFilePoint 0#, 0.008762, 0.0117
Part.InsertCurveFilePoint 0#, 0.00876, 0.01755
Part.InsertCurveFilePoint 0#, 0.008793, 0.0234
Part.InsertCurveFilePoint 0#, 0.008831, 0.02769
Part.InsertCurveFilePoint 0#, 0.008842, 0.030186
Part.InsertCurveFilePoint 0#, 0.008849, 0.032526
Part.InsertCurveFilePoint 0#, 0.008853, 0.034788
Part.InsertCurveFilePoint 0#, 0.008843, 0.036894
Part.InsertCurveFilePoint 0#, 0.008831, 0.038844
Part.InsertCurveFilePoint 0#, 0.0088, 0.040716
Part.InsertCurveFilePoint 0#, 0.008759, 0.042432
Part.InsertCurveFilePoint 0#, 0.00871, 0.044148
Part.InsertCurveFilePoint 0#, 0.008658, 0.04563
Part.InsertCurveFilePoint 0#, 0.008606, 0.047112
Part.InsertCurveFilePoint 0#, 0.008552, 0.048516
Part.InsertCurveFilePoint 0#, 0.00852, 0.049764
Part.InsertCurveFilePoint 0#, 0.008469, 0.051012
Part.InsertCurveFilePoint 0#, 0.008435, 0.052104
Part.InsertCurveFilePoint 0#, 0.008396, 0.053196
Part.InsertCurveFilePoint 0#, 0.008412, 0.05421
Part.InsertCurveFilePoint 0#, 0.008506, 0.055146
Part.InsertCurveFilePoint 0#, 0.008624, 0.056082
Part.InsertCurveFilePoint 0#, 0.008747, 0.057018
Part.InsertCurveFilePoint 0#, 0.008739, 0.058032
Part.InsertCurveFilePoint 0#, 0.008114, 0.059124
Part.InsertCurveFilePoint 0#, 0.008076, 0.060216
Part.InsertCurveFilePoint 0#, 0.008193, 0.061464
Part.InsertCurveFilePoint 0#, 0.00836, 0.062712
Part.InsertCurveFilePoint 0#, 0.008533, 0.064116
Part.InsertCurveFilePoint 0#, 0.008545, 0.065598
Part.InsertCurveFilePoint 0#, 0.008577, 0.06708
Part.InsertCurveFilePoint 0#, 0.008565, 0.068796
Part.InsertCurveFilePoint 0#, 0.00852, 0.070512
Part.InsertCurveFilePoint 0#, 0.008482, 0.072384
Part.InsertCurveFilePoint 0#, 0.008446, 0.074334
Part.InsertCurveFilePoint 0#, 0.008432, 0.07644
Part.InsertCurveFilePoint 0#, 0.008442, 0.078702
Part.InsertCurveFilePoint 0#, 0.008462, 0.081042
Part.InsertCurveFilePoint 0#, 0.008485, 0.083538
Part.InsertCurveFilePoint 0#, 0.008537, 0.087828
Part.InsertCurveFilePoint 0#, 0.008584, 0.093678
Part.InsertCurveFilePoint 0#, 0.008641, 0.099528
Part.InsertCurveFilePoint 0#, 0.008677, 0.105378
Part.InsertCurveFilePoint 0#, 0.008703, 0.111228
Part.InsertCurveFileEnd
'insert int X curve
Part.InsertCurveFileBegin
Part.InsertCurveFilePoint 0.010316, 0#, 0#
Part.InsertCurveFilePoint 0.010335, 0#, 0.00585
Part.InsertCurveFilePoint 0.010332, 0#, 0.0117
Part.InsertCurveFilePoint 0.010384, 0#, 0.01755
Part.InsertCurveFilePoint 0.010383, 0#, 0.0234
Part.InsertCurveFilePoint 0.010397, 0#, 0.02769
Part.InsertCurveFilePoint 0.010402, 0#, 0.030186
Part.InsertCurveFilePoint 0.010374, 0#, 0.032526
Part.InsertCurveFilePoint 0.010344, 0#, 0.034788
Part.InsertCurveFilePoint 0.010337, 0#, 0.036894
Part.InsertCurveFilePoint 0.010322, 0#, 0.038844
Part.InsertCurveFilePoint 0.010323, 0#, 0.040716
Part.InsertCurveFilePoint 0.010337, 0#, 0.042432
Part.InsertCurveFilePoint 0.010355, 0#, 0.044148
Part.InsertCurveFilePoint 0.010347, 0#, 0.04563
Part.InsertCurveFilePoint 0.010356, 0#, 0.047112
Part.InsertCurveFilePoint 0.010373, 0#, 0.048516
Part.InsertCurveFilePoint 0.010375, 0#, 0.049764
Part.InsertCurveFilePoint 0.010349, 0#, 0.051012
Part.InsertCurveFilePoint 0.010309, 0#, 0.052104
Part.InsertCurveFilePoint 0.010108, 0#, 0.053196
Part.InsertCurveFilePoint 0.008828, 0#, 0.05421
Part.InsertCurveFilePoint 0.00603, 0#, 0.055146
Part.InsertCurveFilePoint 0.006486, 0#, 0.056082
Part.InsertCurveFilePoint 0.007642, 0#, 0.057018
Part.InsertCurveFilePoint 0.008398, 0#, 0.058032
Part.InsertCurveFilePoint 0.008556, 0#, 0.059124
Part.InsertCurveFilePoint 0.008318, 0#, 0.060216
Part.InsertCurveFilePoint 0.008332, 0#, 0.061464
Part.InsertCurveFilePoint 0.008265, 0#, 0.062712
Part.InsertCurveFilePoint 0.007884, 0#, 0.064116
Part.InsertCurveFilePoint 0.007966, 0#, 0.065598
Part.InsertCurveFilePoint 0.008359, 0#, 0.06708
Part.InsertCurveFilePoint 0.008777, 0#, 0.068796
Part.InsertCurveFilePoint 0.009023, 0#, 0.070512
Part.InsertCurveFilePoint 0.009188, 0#, 0.072384
Part.InsertCurveFilePoint 0.00933, 0#, 0.074334
Part.InsertCurveFilePoint 0.009431, 0#, 0.07644
Part.InsertCurveFilePoint 0.009506, 0#, 0.078702
Part.InsertCurveFilePoint 0.009589, 0#, 0.081042
Part.InsertCurveFilePoint 0.009654, 0#, 0.083538
Part.InsertCurveFilePoint 0.009737, 0#, 0.087828
Part.InsertCurveFilePoint 0.009799, 0#, 0.093678
Part.InsertCurveFilePoint 0.009873, 0#, 0.099528
Part.InsertCurveFilePoint 0.009938, 0#, 0.105378
Part.InsertCurveFilePoint 0.009995, 0#, 0.111228
Part.InsertCurveFileEnd
'insert int Y curve
Part.InsertCurveFileBegin
Part.InsertCurveFilePoint 0#, 0.007753, 0#
Part.InsertCurveFilePoint 0#, 0.007745, 0.00585
Part.InsertCurveFilePoint 0#, 0.007728, 0.0117
Part.InsertCurveFilePoint 0#, 0.007754, 0.01755
Part.InsertCurveFilePoint 0#, 0.007769, 0.0234
Part.InsertCurveFilePoint 0#, 0.007822, 0.02769
Part.InsertCurveFilePoint 0#, 0.007838, 0.030186
Part.InsertCurveFilePoint 0#, 0.007829, 0.032526
Part.InsertCurveFilePoint 0#, 0.007818, 0.034788
Part.InsertCurveFilePoint 0#, 0.00781, 0.036894
Part.InsertCurveFilePoint 0#, 0.00779, 0.038844
Part.InsertCurveFilePoint 0#, 0.007754, 0.040716
Part.InsertCurveFilePoint 0#, 0.007718, 0.042432
Part.InsertCurveFilePoint 0#, 0.007671, 0.044148
Part.InsertCurveFilePoint 0#, 0.007614, 0.04563
Part.InsertCurveFilePoint 0#, 0.007544, 0.047112
Part.InsertCurveFilePoint 0#, 0.007486, 0.048516
Part.InsertCurveFilePoint 0#, 0.00743, 0.049764
Part.InsertCurveFilePoint 0#, 0.007331, 0.051012
Part.InsertCurveFilePoint 0#, 0.007197, 0.052104
Part.InsertCurveFilePoint 0#, 0.006974, 0.053196
Part.InsertCurveFilePoint 0#, 0.00575, 0.05421
Part.InsertCurveFilePoint 0#, 0.003089, 0.055146
Part.InsertCurveFilePoint 0#, 0.00366, 0.056082
Part.InsertCurveFilePoint 0#, 0.004875, 0.057018
Part.InsertCurveFilePoint 0#, 0.005594, 0.058032
Part.InsertCurveFilePoint 0#, 0.006063, 0.059124
Part.InsertCurveFilePoint 0#, 0.006456, 0.060216
Part.InsertCurveFilePoint 0#, 0.006641, 0.061464
Part.InsertCurveFilePoint 0#, 0.006766, 0.062712
Part.InsertCurveFilePoint 0#, 0.006578, 0.064116
Part.InsertCurveFilePoint 0#, 0.006602, 0.065598
Part.InsertCurveFilePoint 0#, 0.006829, 0.06708
Part.InsertCurveFilePoint 0#, 0.007118, 0.068796
Part.InsertCurveFilePoint 0#, 0.007256, 0.070512
Part.InsertCurveFilePoint 0#, 0.007337, 0.072384
Part.InsertCurveFilePoint 0#, 0.007385, 0.074334
Part.InsertCurveFilePoint 0#, 0.007428, 0.07644
Part.InsertCurveFilePoint 0#, 0.007463, 0.078702
Part.InsertCurveFilePoint 0#, 0.007507, 0.081042
Part.InsertCurveFilePoint 0#, 0.007545, 0.083538
Part.InsertCurveFilePoint 0#, 0.007633, 0.087828
Part.InsertCurveFilePoint 0#, 0.0077, 0.093678
Part.InsertCurveFilePoint 0#, 0.007792, 0.099528
Part.InsertCurveFilePoint 0#, 0.007847, 0.105378
Part.InsertCurveFilePoint 0#, 0.00787, 0.111228
Part.InsertCurveFileEnd
'insert the centerline
Part.InsertCurveFileBegin
Part.InsertCurveFilePoint 0#, 0#, 0#
Part.InsertCurveFilePoint 0#, 0#, 0.00585
Part.InsertCurveFilePoint 0#, 0#, 0.0117
Part.InsertCurveFilePoint 0#, 0#, 0.01755
Part.InsertCurveFilePoint 0#, 0#, 0.0234
Part.InsertCurveFilePoint 0#, 0#, 0.02769
Part.InsertCurveFilePoint 0#, 0#, 0.030186
Part.InsertCurveFilePoint 0#, 0#, 0.032526
Part.InsertCurveFilePoint 0#, 0#, 0.034788
Part.InsertCurveFilePoint 0#, 0#, 0.036894
Part.InsertCurveFilePoint 0#, 0#, 0.038844
Part.InsertCurveFilePoint 0#, 0#, 0.040716
Part.InsertCurveFilePoint 0#, 0#, 0.042432
Part.InsertCurveFilePoint 0#, 0#, 0.044148
Part.InsertCurveFilePoint 0#, 0#, 0.04563
Part.InsertCurveFilePoint 0#, 0#, 0.047112
Part.InsertCurveFilePoint 0#, 0#, 0.048516
Part.InsertCurveFilePoint 0#, 0#, 0.049764
Part.InsertCurveFilePoint 0#, 0#, 0.051012
Part.InsertCurveFilePoint 0#, 0#, 0.052104
Part.InsertCurveFilePoint 0#, 0#, 0.053196
Part.InsertCurveFilePoint 0#, 0#, 0.05421
Part.InsertCurveFilePoint 0#, 0#, 0.055146
Part.InsertCurveFilePoint 0#, 0#, 0.056082
Part.InsertCurveFilePoint 0#, 0#, 0.057018
Part.InsertCurveFilePoint 0#, 0#, 0.058032
Part.InsertCurveFilePoint 0#, 0#, 0.059124
Part.InsertCurveFilePoint 0#, 0#, 0.060216
Part.InsertCurveFilePoint 0#, 0#, 0.061464
Part.InsertCurveFilePoint 0#, 0#, 0.062712
Part.InsertCurveFilePoint 0#, 0#, 0.064116
Part.InsertCurveFilePoint 0#, 0#, 0.065598
Part.InsertCurveFilePoint 0#, 0#, 0.06708
Part.InsertCurveFilePoint 0#, 0#, 0.068796
Part.InsertCurveFilePoint 0#, 0#, 0.070512
Part.InsertCurveFilePoint 0#, 0#, 0.072384
Part.InsertCurveFilePoint 0#, 0#, 0.074334
Part.InsertCurveFilePoint 0#, 0#, 0.07644
Part.InsertCurveFilePoint 0#, 0#, 0.078702
Part.InsertCurveFilePoint 0#, 0#, 0.081042
Part.InsertCurveFilePoint 0#, 0#, 0.083538
Part.InsertCurveFilePoint 0#, 0#, 0.087828
Part.InsertCurveFilePoint 0#, 0#, 0.093678
Part.InsertCurveFilePoint 0#, 0#, 0.099528
Part.InsertCurveFilePoint 0#, 0#, 0.105378
Part.InsertCurveFilePoint 0#, 0#, 0.111228
Part.InsertCurveFileEnd
'Make a sketch
Part.Extension.SelectByID2 "Front Plane", "PLANE", 0, 0, 0, False, 0, Nothing, 0
Part.SketchManager.InsertSketch True
Part.ClearSelection2 True
'Create ellipse #1
Set skSegment = Part.SketchManager.CreateEllipse(1.61913708406892E-02, 8.17764199252039E-03, 0, 1.73929649008303E-02, 8.17764199252039E-03, 0, 1.61913708406892E-02, 8.77843902259095E-03, 0)
'Make pieces between the ellipse, and ext_x, ext_y, and centerline
Part.Extension.SelectByID2 "Point3", "SKETCHPOINT", Empty, Empty, Empty, False, 0, Nothing, 0
Part.Extension.SelectByID2 "Curve1", "REFERENCECURVES", 0, 0, 0, True, 0, Nothing, 0
Part.SketchAddConstraints "sgATPIERCE"
Part.ClearSelection2 True
Part.Extension.SelectByID2 "Point4", "SKETCHPOINT", Empty, Empty, Empty, False, 0, Nothing, 0
Part.Extension.SelectByID2 "Curve2", "REFERENCECURVES", 0, 0, 0, True, 0, Nothing, 0
Part.SketchAddConstraints "sgATPIERCE"
Part.ClearSelection2 True
Part.Extension.SelectByID2 "Point2", "SKETCHPOINT", Empty, Empty, Empty, False, 0, Nothing, 0
Part.Extension.SelectByID2 "Curve5", "REFERENCECURVES", 0, 0, 0, True, 0, Nothing, 0
Part.SketchAddConstraints "sgATPIERCE"
Part.ClearSelection2 True
Part.Extension.SelectByID2 "Sketch1", "SKETCH", Empty, Empty, Empty, True, 0, Nothing, 0
Part.Extension.SelectByID2 "Curve5", "REFERENCECURVES", 0, 0, 0, True, 0, Nothing, 0
Part.Extension.SelectByID2 "Curve1", "REFERENCECURVES", 0, 0, 0, True, 0, Nothing, 0
Part.Extension.SelectByID2 "Curve2", "REFERENCECURVES", 0, 0, 0, True, 0, Nothing, 0
Part.ClearSelection2 True
Part.Extension.SelectByID2 "Sketch1", "SKETCH", Empty, Empty, Empty, False, 1, Nothing, 0
Part.Extension.SelectByID2 "Curve5", "REFERENCECURVES", 0, 0, 0, True, 4, Nothing, 0
Part.Extension.SelectByID2 "Curve1", "REFERENCECURVES", 0, 0, 0, True, 2, Nothing, 0
Part.Extension.SelectByID2 "Curve2", "REFERENCECURVES", 0, 0, 0, True, 2, Nothing, 0
Set swFeatMgr = Part.FeatureManager
Set swFeatData = swFeatMgr.CreateDefinition(swFeatureNameID_e.swFmSweep)
swFeatData.AdvancedSmoothing = False
swFeatData.AlignWithEndFaces = 0
swFeatData.AutoSelect = True
swFeatData.D1ReverseTwistDir = False
swFeatData.Direction = 0
swFeatData.EndTangencyType = 0
swFeatData.FeatureScope = True
swFeatData.MaintainTangency = False
swFeatData.Merge = True
swFeatData.MergeSmoothFaces = True
swFeatData.PathAlignmentType = 0
swFeatData.StartTangencyType = 0
swFeatData.ThinFeature = False
swFeatData.ThinWallType = 0
swFeatData.TwistControlType = 0
swFeatData.SetTwistAngle 0
swFeatData.SetWallThickness True, 0
Set swFeat = swFeatMgr.CreateFeature(swFeatData)
'Make a sketch
Part.Extension.SelectByID2 "Front Plane", "PLANE", 0, 0, 0, False, 0, Nothing, 0
Part.SketchManager.InsertSketch True
Part.ClearSelection2 True
'Create ellipse #2
Set skSegment = Part.SketchManager.CreateEllipse(2.84511792584823E-02, 0, 0, 3.40285520757959E-02, 0, 0, 2.84511792584823E-02, 2.85476685014174E-03, 0)
'Make pieces between the ellipse, and ext_x, ext_y, and centerline
Part.Extension.SelectByID2 "Point3", "SKETCHPOINT", Empty, Empty, Empty, False, 0, Nothing, 0
Part.Extension.SelectByID2 "Curve3", "REFERENCECURVES", 0, 0, 0, True, 0, Nothing, 0
Part.SketchAddConstraints "sgATPIERCE"
Part.ClearSelection2 True
Part.Extension.SelectByID2 "Point4", "SKETCHPOINT", Empty, Empty, Empty, False, 0, Nothing, 0
Part.Extension.SelectByID2 "Curve4", "REFERENCECURVES", 0, 0, 0, True, 0, Nothing, 0
Part.SketchAddConstraints "sgATPIERCE"
Part.ClearSelection2 True
Part.Extension.SelectByID2 "Point2", "SKETCHPOINT", Empty, Empty, Empty, False, 0, Nothing, 0
Part.Extension.SelectByID2 "Curve5", "REFERENCECURVES", 0, 0, 0, True, 0, Nothing, 0
Part.SketchAddConstraints "sgATPIERCE"
Part.ClearSelection2 True
Part.Extension.SelectByID2 "Sketch2", "SKETCH", Empty, Empty, Empty, True, 0, Nothing, 0
Part.Extension.SelectByID2 "Curve5", "REFERENCECURVES", 0, 0, 0, True, 0, Nothing, 0
Part.Extension.SelectByID2 "Curve3", "REFERENCECURVES", 0, 0, 0, True, 0, Nothing, 0
Part.Extension.SelectByID2 "Curve4", "REFERENCECURVES", 0, 0, 0, True, 0, Nothing, 0
Part.ClearSelection2 True
Part.Extension.SelectByID2 "Sketch2", "SKETCH", Empty, Empty, Empty, False, 1, Nothing, 0
Part.Extension.SelectByID2 "Curve5", "REFERENCECURVES", 0, 0, 0, True, 4, Nothing, 0
Part.Extension.SelectByID2 "Curve3", "REFERENCECURVES", 0, 0, 0, True, 2, Nothing, 0
Part.Extension.SelectByID2 "Curve4", "REFERENCECURVES", 0, 0, 0, True, 2, Nothing, 0
Set swFeatMgr = Part.FeatureManager
Set swFeatData = swFeatMgr.CreateDefinition(swFeatureNameID_e.swFmSweep)
swFeatData.AdvancedSmoothing = False
swFeatData.AlignWithEndFaces = 0
swFeatData.AutoSelect = True
swFeatData.D1ReverseTwistDir = False
swFeatData.Direction = 0
swFeatData.EndTangencyType = 0
swFeatData.FeatureScope = True
swFeatData.MaintainTangency = False
swFeatData.Merge = True
swFeatData.MergeSmoothFaces = True
swFeatData.PathAlignmentType = 0
swFeatData.StartTangencyType = 0
swFeatData.ThinFeature = False
swFeatData.ThinWallType = 0
swFeatData.TwistControlType = 0
swFeatData.SetTwistAngle 0
swFeatData.SetWallThickness True, 0
Set swFeat = swFeatMgr.CreateFeature(swFeatData)
Part.ClearSelection2 True
Part.Extension.SelectByID2 "Sweep2", "SOLIDBODY", 0, 0, 0, True, 0, Nothing, 0
Part.ClearSelection2 True
Part.Extension.SelectByID2 "Sweep2", "SOLIDBODY", 0, 0, 0, False, 1, Nothing, 0
'Copy the pith body
Set myFeature = Part.FeatureManager.InsertMoveCopyBody2(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, True, 1)
Part.Extension.SelectByID2 "Sweep1", "SOLIDBODY", 0, 0, 0, True, 0, Nothing, 0
Part.Extension.SelectByID2 "Sweep2", "SOLIDBODY", 0, 0, 0, True, 0, Nothing, 0
Part.ClearSelection2 True
'Get the rind body by subtration in Combine feature
Part.Extension.SelectByID2 "Body-Move/Copy1", "BODYFEATURE", 0, 0, 0, False, 0, Nothing, 0
Part.Extension.SelectByID2 "Sweep1", "SOLIDBODY", 0, 0, 0, True, 1, Nothing, 0
Part.Extension.SelectByID2 "Sweep2", "SOLIDBODY", 0, 0, 0, True, 2, Nothing, 0
Set myFeature = Part.FeatureManager.InsertCombineFeature(15902, Nothing, Nothing)
'Save As
longstatus = Part.SaveAs3("C:\Nan-Wei Desktop\Stalk #423 (refined)\Stalk_423.SLDPRT", 0, 0) '[*]
'Save As
longstatus = Part.SaveAs3("C:\Nan-Wei Desktop\Stalk #423 (refined)\Stalk_423.step", 0, 2) '***, change stp to step '[*]
Part.ClearSelection2 True
partTitle = Part.GetTitle '*** added this line to get title to closedoc
'Close Document
Set swPart = Nothing
Set Part = Nothing
swApp.CloseDoc partTitle '*** update syntax to use part title
msgbox "Macro Ended" '*** Added to indicate marco ended
End Sub
One thing to highlight is that i noticed you are hard-coding your path/file name as well as the template.
1. User do not have the template file or SOLIDWORKS 2020
2. User do not have C:\Nan-Wei Desktop\ folder
1. Update the code to use default template specified by user in user system setting by using GetUserPreferenceStringValue method. See
2. Make the Save path "dynnamic", eg: Save it to the same folder as the macro by using GetCurrentMacroPathFolder See