AnsweredAssumed Answered

Error with cut feature in VB Macro

Question asked by Ahmed Eldyasty on Mar 29, 2017
Latest reply on Mar 30, 2017 by Peter Brinkhuis

Hello

i tried to make cut through a cylinder but when i run the code from API SW VSTA VB Macro it stop and gives me an error with cut feature

here is the code

Dim swDoc As ModelDoc2 = Nothing

            Dim swPart As PartDoc = Nothing

            Dim swDrawing As DrawingDoc = Nothing

            Dim swAssembly As AssemblyDoc = Nothing

            Dim boolstatus As Boolean = false

            Dim longstatus As Integer = 0

            Dim longwarnings As Integer = 0

            '

            'New Document

            Dim swSheetWidth As Double

            swSheetWidth = 0

            Dim swSheetHeight As Double

            swSheetHeight = 0

            swDoc = CType(swApp.NewDocument("C:\ProgramData\SolidWorks\SOLIDWORKS 2017\templates\Part.prtdot", 0, swSheetWidth, swSheetHeight),ModelDoc2)

            swPart = swDoc

            swApp.ActivateDoc2("Part1", false, longstatus)

            swDoc = CType(swApp.ActiveDoc,ModelDoc2)

            Dim myModelView As ModelView = Nothing

            myModelView = CType(swDoc.ActiveView,ModelView)

            myModelView.FrameState = CType(swWindowState_e.swWindowMaximized,Integer)

            boolstatus = swDoc.Extension.SelectByID2("Front Plane", "PLANE", 0, 0, 0, false, 0, Nothing, 0)

            swDoc.SketchManager.InsertSketch(true)

            swDoc.ClearSelection2(true)

            Dim skSegment As SketchSegment = Nothing

            skSegment = CType(swDoc.SketchManager.CreateCircle(0, 0, 0, 0.049045, 0.007529, 0),SketchSegment)

            swDoc.ClearSelection2(true)

            skSegment = CType(swDoc.SketchManager.CreateCircle(0, 0, 0, 0.032266, 0.007529, 0),SketchSegment)

            '

            'Named View

            swDoc.ShowNamedView2("*Trimetric", 8)

            swDoc.ViewZoomtofit2()

            '

            'Zoom In/Out (MouseWheel)

            Dim swModelView As ModelView = Nothing

            swModelView = CType(swDoc.ActiveView,ModelView)

            swModelView.Scale2 = 1.1038054555528047

            Dim swTranslation(2) As Double

            swTranslation(0) = 0.045236861391939921

            swTranslation(1) = -0.00075604615086462235

            swTranslation(2) = 0.0012582720965941959

        Dim swTranslationVar As Object

            swTranslationVar = swTranslation

            Dim swMathUtils As Object = Nothing

            swMathUtils = swApp.GetMathUtility()

            Dim swTranslationVector As Object = Nothing

            swTranslationVector = swMathUtils.CreateVector(swTranslationVar)

            swModelView.Translation3 = swTranslationVector

            '

            'Zoom In/Out (MouseWheel)

            swModelView = CType(swDoc.ActiveView,ModelView)

            swModelView.Scale2 = 0.91983787962733743

            swTranslation = New Double(2) {}

            swTranslation(0) = 0.045365963728255923

            swTranslation(1) = -0.000630038459053852

            swTranslation(2) = 0.0010485600804951633

            swTranslationVar = swTranslation

            swMathUtils = swApp.GetMathUtility()

            swTranslationVector = swMathUtils.CreateVector(swTranslationVar)

            swModelView.Translation3 = swTranslationVector

            '

            'Zoom In/Out (MouseWheel)

            swModelView = CType(swDoc.ActiveView,ModelView)

            swModelView.Scale2 = 0.76653156635611441

            swTranslation = New Double(2) {}

            swTranslation(0) = 0.045473549008519305

            swTranslation(1) = -0.0005250320492115433

            swTranslation(2) = 0.00087380006707930282

            swTranslationVar = swTranslation

            swMathUtils = swApp.GetMathUtility()

            swTranslationVector = swMathUtils.CreateVector(swTranslationVar)

            swModelView.Translation3 = swTranslationVector

            '

            'Zoom In/Out (MouseWheel)

            swModelView = CType(swDoc.ActiveView,ModelView)

            swModelView.Scale2 = 0.638776305296762

            swTranslation = New Double(2) {}

            swTranslation(0) = 0.045563203408738762

            swTranslation(1) = -0.00043752670767627867

            swTranslation(2) = 0.00072816672256608567

            swTranslationVar = swTranslation

            swMathUtils = swApp.GetMathUtility()

            swTranslationVector = swMathUtils.CreateVector(swTranslationVar)

            swModelView.Translation3 = swTranslationVector

            '

            'Zoom In/Out (MouseWheel)

            swModelView = CType(swDoc.ActiveView,ModelView)

            swModelView.Scale2 = 0.53231358774730164

            swTranslation = New Double(2) {}

            swTranslation(0) = 0.045637915408921662

            swTranslation(1) = -0.00036460558973023968

            swTranslation(2) = 0.00060680560213840476

            swTranslationVar = swTranslation

            swMathUtils = swApp.GetMathUtility()

            swTranslationVector = swMathUtils.CreateVector(swTranslationVar)

            swModelView.Translation3 = swTranslationVector

            '

            'Zoom In/Out (MouseWheel)

            swModelView = CType(swDoc.ActiveView,ModelView)

            swModelView.Scale2 = 0.44359465645608476

            swTranslation = New Double(2) {}

            swTranslation(0) = 0.045700175409074034

            swTranslation(1) = -0.00030383799144187011

            swTranslation(2) = 0.00050567133511533724

            swTranslationVar = swTranslation

            swMathUtils = swApp.GetMathUtility()

            swTranslationVector = swMathUtils.CreateVector(swTranslationVar)

            swModelView.Translation3 = swTranslationVector

            '

            'Zoom In/Out (MouseWheel)

            swModelView = CType(swDoc.ActiveView,ModelView)

            swModelView.Scale2 = 0.534451393320584

            swTranslation = New Double(2) {}

            swTranslation(0) = 0.079006461583123008

            swTranslation(1) = 0.011208225509027191

            swTranslation(2) = 0.00060924257242811735

            swTranslationVar = swTranslation

            swMathUtils = swApp.GetMathUtility()

            swTranslationVector = swMathUtils.CreateVector(swTranslationVar)

            swModelView.Translation3 = swTranslationVector

            '

            'Zoom In/Out (MouseWheel)

            swModelView = CType(swDoc.ActiveView,ModelView)

            swModelView.Scale2 = 0.64391734135010126

            swTranslation = New Double(2) {}

            swTranslation(0) = 0.11913451721450734

            swTranslation(1) = 0.0250781815336887

            swTranslation(2) = 0.00073402719569652685

            swTranslationVar = swTranslation

            swMathUtils = swApp.GetMathUtility()

            swTranslationVector = swMathUtils.CreateVector(swTranslationVar)

            swModelView.Translation3 = swTranslationVector

            '

            'Zoom In/Out (MouseWheel)

            swModelView = CType(swDoc.ActiveView,ModelView)

            swModelView.Scale2 = 0.77580402572301366

            swTranslation = New Double(2) {}

            swTranslation(0) = 0.12108418032954664

            swTranslation(1) = 0.035676140599545943

            swTranslation(2) = 0.00088437011529702051

            swTranslationVar = swTranslation

            swMathUtils = swApp.GetMathUtility()

            swTranslationVector = swMathUtils.CreateVector(swTranslationVar)

            swModelView.Translation3 = swTranslationVector

            Dim myFeature As Feature = Nothing

            myFeature = CType(swDoc.FeatureManager.FeatureExtrusion2(true, false, false, 0, 0, 0.23000000000000007, 0.01, false, false, false, false, 0.017453292519943334, 0.017453292519943334, false, false, false, false, true, true, true, 0, 0, false),Feature)

            swDoc.ISelectionManager.EnableContourSelection = false

            boolstatus = swDoc.Extension.SelectByID2("Top Plane", "PLANE", 0, 0, 0, false, 0, Nothing, 0)

            swDoc.SketchManager.InsertSketch(true)

            swDoc.ClearSelection2(true)

            skSegment = CType(swDoc.SketchManager.CreateCircle(0, -0.108635, 0, 0.028008, -0.108125, 0),SketchSegment)

            myFeature = CType(swDoc.FeatureManager.FeatureCut4(false, false, false, 9, 1, 0.23000000000000007, 0.23000000000000007, false, false, false, false, 0.017453292519943334, 0.017453292519943334, false, false, false, false, false, true, true, true, true, false, 0, 0, false, false),Feature)

            swDoc.ISelectionManager.EnableContourSelection = false

            '

            'Named View

            swDoc.ShowNamedView2("*Isometric", 7)

            swDoc.ViewZoomtofit2()

    End Sub

 

 

    ''' <summary>

    ''' The SldWorks swApp variable is pre-assigned for you.

    ''' </summary>

    Public swApp As SldWorks

Outcomes