4 Replies Latest reply on Mar 30, 2017 1:36 PM by Peter Brinkhuis

    Error with cut feature in VB Macro

    Ahmed Eldyasty

      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

        • Re: Error with cut feature in VB Macro
          Peter Brinkhuis

          The macro is clearly recorded my the macro recorder. You could remove 80% of it and it would do the same, minus the zooming. I suggest you do that to make it more readable.

           

          What should the code do? Switch from a drawing to a part, extrude a donut and add an extuded cut to it? What's the error?

           

          I removed all of the zooming actions and ran it without problems. Ran it once again with your code and also runs flawless. Here's the cleaned-up version, although you can still get rid of a lot more probably:

           

          Imports SolidWorks.Interop.sldworks

          Imports SolidWorks.Interop.swconst

          Imports System.Runtime.InteropServices

          Imports System

           

          Partial Class SolidWorksMacro

           

              Sub main()

           

                  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)

           

                  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

           

          End Class