3 Replies Latest reply on Aug 30, 2016 1:58 PM by Amen Allah Jlili

    Macro Help

    Taylor Reed

      I am working on a research project and this code will work with break points but once they are removed it doesnt work! I have lowered the issue to it not selecting the proper plane, so i created a plane parallel to the surface i need to draw on and still not working right. i have the multiple selections commented out and have tried multiple different methods and it hates me. everything work up until the "build sides"

       

       

      ' ******************************************************************************

      ' C:\Users\HOH118\AppData\Local\Temp\swx5496\Macro1.swb - macro recorded on 08/03/16 by HOH118

      ' ******************************************************************************

      Dim swApp As Object

       

      Dim Part As Object

      Dim boolstatus As Boolean

      Dim longstatus As Long, longwarnings As Long

       

      Sub main()

       

      Set swApp = _

      Application.SldWorks

      'load Part

      Set Part = swApp.OpenDoc6("C:\Users\HOH118\Desktop\semi_1.SLDPRT", 1, 0, "", longstatus, longwarnings)

      swApp.ActivateDoc2 "semi", False, longstatus

      Set Part = swApp.ActiveDoc

      Set Part = swApp.ActiveDoc

      Dim myModelView As Object

      'TOP

      'Tables/Variables

             Dim H As Double, A As Double, L1 As Double, L2 As Double, T As Double, TL As Double, x As Double, TLs As Double

             Dim n As Integer, i As Integer

      'Gap

              A = 0.01 'InputBox("How big of a gap?(keep smaller than quarter the length(0.1 for sides, .07 for top)")

      ' length of bump

              L1 = 0.01 'InputBox("Specify L1(front)")

              L2 = 0.01 'InputBox("Specify L2(rear)")

              H = 0.01 'InputBox("specify Height")

      ' Thickness of bump

              T = 0.01 'InputBox("Specify Thickness")

      ' lenght of platform

              TL = 0.28

              TLs = 0.4

      ' number of bumps

              n = 3 'InputBox("How many extrusions do you want?")

      ' spacing

              x = ((TL - n * T - 2 * A) / (n - 1))

      'Build

              Part.ShowNamedView2 "*Front", 1

              Part.SketchManager.InsertSketch True

              boolstatus = Part.Extension.SelectByID2("", "FACE", 2.5354686515633, 0.196199939706639, 0.139999999999986, False, 0, Nothing, 0)

              Part.ClearSelection2 True

              Dim skSegment As Object

              Set skSegment = Part.SketchManager.CreateLine(2.62256, 0.39624, 0#, 2.62256 - L1, 0.39624, 0#)

              Set skSegment = Part.SketchManager.CreateLine(2.62256 - L1, 0.39624, 0#, 2.62256 - L1 - L2, 0.39624, 0#)

              Part.SetPickMode

              Part.ClearSelection2 True

              Set skSegment = Part.SketchManager.CreateLine(2.62256 - L1, 0.39624, 0#, 2.62256 - L1, 0.39624 + H, 0#)

              Part.SetPickMode

              Part.ClearSelection2 True

              Set skSegment = Part.SketchManager.CreateTangentArc(2.62256 - L1, 0.39624 + H, 0#, 2.62256, 0.39624, 0#, 4)

              Part.ClearSelection2 True

              Set skSegment = Part.SketchManager.CreateTangentArc(2.62256 - L1, 0.39624 + H, 0#, 2.62256 - L1 - L2, 0.39624, 0#, 2)

              Part.ClearSelection2 True

              Part.SketchManager.InsertSketch True

              boolstatus = Part.Extension.SelectByID2("sketch44", "SKETCHREGION", 2.60921172422904, 0.39942814122772, 0, True, 4, Nothing, 0)

              boolstatus = Part.Extension.SelectByID2("sketch44", "SKETCHREGION", 2.61887488731883, 0.39894498307323, 0, True, 4, Nothing, 0)

              Part.ClearSelection2 True

              boolstatus = Part.Extension.SelectByID2("sketch44", "SKETCH", 2.60921172422904, 0.39942814122772, 0, False, 4, Nothing, 0)

              Part.SelectionManager.EnableContourSelection = True

              boolstatus = Part.Extension.SelectByID2("sketch44", "SKETCHREGION", 2.60921172422904, 0.39942814122772, 0, True, 4, Nothing, 0)

              boolstatus = Part.Extension.SelectByID2("sketch44", "SKETCHREGION", 2.61887488731883, 0.39894498307323, 0, True, 4, Nothing, 0)

              Dim myFeature As Object

              Set myFeature = Part.FeatureManager.FeatureExtrusion2(True, False, True, 0, 0, T, T, False, False, False, False, 1.74532925199433E-02, 1.74532925199433E-02, False, False, False, False, False, True, True, 0, 0, False)

              Part.SelectionManager.EnableContourSelection = False

              Set Part = swApp.ActiveDoc

      'translate

              Part.ClearSelection2 True

              boolstatus = Part.Extension.SelectByID2("Boss-Extrude18", "SOLIDBODY", 2.62043554987201, 0.404421032129638, 0.132200255518736, True, 0, Nothing, 0)

              Part.ClearSelection2 True

              boolstatus = Part.Extension.SelectByID2("Boss-Extrude18", "SOLIDBODY", 2.62043554987201, 0.404421032129638, 0.132200255518736, False, 1, Nothing, 0)

              Set myFeature = Part.FeatureManager.InsertMoveCopyBody2(0, 0, -A, 0, 2.61005990677298, 0.402340560138932, 0.115, 0, 0, 0, False, 1)

      'replicate

                boolstatus = Part.Extension.SelectByID2("", "FACE", 2.62145504830096, 0.400809257677547, 0.122485083596739, False, 0, Nothing, 0)

                Part.ActivateSelectedFeature

                boolstatus = Part.Extension.SelectByID2("Line1@Sketch41", "EXTSKETCHSEGMENT", -2.46073845709474E-02, 0.39624, 0, True, 0, Nothing, 0)

                Part.ClearSelection2 True

                boolstatus = Part.Extension.SelectByID2("Boss-Extrude18", "BODYFEATURE", 2.62145504830096, 0.400809257677547, 0.122485083596739, False, 4, Nothing, 0)

                boolstatus = Part.Extension.SelectByID2("Line1@Sketch41", "EXTSKETCHSEGMENT", -2.46073845709474E-02, 0.39624, 0, True, 1, Nothing, 0)

                Set myFeature = Part.FeatureManager.FeatureLinearPattern4(n, x + T, 0, 0, False, False, "NULL", "NULL", True, False, False, False, False, False, True, True, False, False, 0, 0)

      'Sides

      'Tables/Variables

             Dim Hs As Double, a_s As Double, L1s As Double, L2s As Double, Ts As Double, xs As Double

             Dim ns As Integer

      'Gap

              a_s = 0.01 'InputBox("How big of a gap?(keep smaller than quarter the length(0.1 for sides, .07 for top)")

      ' length of bump

              L1s = 0.01 ' InputBox("Specify L1(front)")

              L2s = 0.01 'InputBox("Specify L2(rear)")

              Hs = 0.01 'InputBox("specify Height")

      ' Thickness of bump

              Ts = 0.01 'InputBox("Specify Thickness")

      ' number of bumps

              ns = 3 'InputBox("How many extrusions do you want?")

      ' spacing

             x = ((TLs - ns * Ts - 2 * a_s) / (ns - 1))

      'Build Sides

              Set Part = swApp.ActiveDoc

              Part.SketchManager.InsertSketch True

              'boolstatus = Part.Extension.SelectByID2("", "FACE", 2.65, 0.396239999999978, 0, False, 0, Nothing, 0)

              'boolstatus = Part.Extension.SelectByID2("Plane7", "PLANE", 0, 0, 0, False, 0, Nothing, 0)

              boolstatus = Part.Extension.SelectByID2("", "FACE", 2#, 0.39624, 0, False, 0, Nothing, 0)

              Part.ClearSelection2 True

              Set skSegment = Part.SketchManager.CreateLine(2.62256, -0.14, 0#, 2.62256 - L1s, -0.14, 0#)

              Part.SetPickMode

              Part.ClearSelection2 True

              Set skSegment = Part.SketchManager.CreateLine(2.62256 - L1s, -0.14, 0#, 2.62256 - L1s, -0.14 - Hs, 0#)

              Part.SetPickMode

              Part.ClearSelection2 True

              Set skSegment = Part.SketchManager.CreateLine(2.62256 - L1s, -0.14, 0#, 2.62256 - L1s - L2s, -0.14, 0#)

              Part.SetPickMode

              Part.ClearSelection2 True

              Set skSegment = Part.SketchManager.CreateTangentArc(2.62256 - L1s, -0.14 - Hs, 0#, 2.62256, -0.14, 0#, 2)

              Part.ClearSelection2 True

              Set skSegment = Part.SketchManager.CreateTangentArc(2.62256 - L1s - L2s, -0.14, 0#, 2.62256 - L1s, -0.14 - Hs, 0#, 2)

              Part.ClearSelection2 True

              Part.SketchManager.InsertSketch True

              boolstatus = Part.Extension.SelectByID2("Sketch45", "SKETCHREGION", 2.60777103006834, 0.157285249999984, 0.145508834727256, True, 4, Nothing, 0)

              boolstatus = Part.Extension.SelectByID2("Sketch45", "SKETCHREGION", 2.61648817213429, 0.157285249999984, 0.145218263325058, True, 4, Nothing, 0)

              Part.ClearSelection2 True

              boolstatus = Part.Extension.SelectByID2("Sketch45", "SKETCH", 2.60777103006834, 0.157285249999984, 0.145508834727256, False, 4, Nothing, 0)

              Part.SelectionManager.EnableContourSelection = True

              boolstatus = Part.Extension.SelectByID2("Sketch45", "SKETCHREGION", 2.60777103006834, 0.157285249999984, 0.145508834727256, True, 4, Nothing, 0)

              boolstatus = Part.Extension.SelectByID2("Sketch45", "SKETCHREGION", 2.61648817213429, 0.157285249999984, 0.145218263325058, True, 4, Nothing, 0)

              Set myFeature = Part.FeatureManager.FeatureExtrusion2(True, False, True, 0, 0, 0.01, 0.01, False, False, False, False, 1.74532925199433E-02, 1.74532925199433E-02, False, False, False, False, True, True, True, 0, 0, False)

              Part.SelectionManager.EnableContourSelection = False

       

      End Sub

        • Re: Macro Help
          Taylor Reed

                 This is the specific line of code where it screws up, I have tried this more than a hundred times please help.

                   'boolstatus = Part.Extension.SelectByID2("", "FACE", 2.65, 0.396239999999978, 0, False, 0, Nothing, 0)

                  'boolstatus = Part.Extension.SelectByID2("Plane7", "PLANE", 0, 0, 0, False, 0, Nothing, 0)

                  boolstatus = Part.Extension.SelectByID2("", "FACE", 2#, 0.39624, 0, False, 0, Nothing, 0)

          • Re: Macro Help
            Peter Brinkhuis

            What is the goal of the macro? What part of it is essential for us to debug?

             

            You can post readable code when you use the advanced editor when placing a comment. The bottom right buttons says insert, I'm trying this section as raw HTML. Ok that didn't work.

             

            What I notice from your code already:

            • It's obviously recorded. This means there are usually a lot of unnecessary steps and very long numbers in there. The line Set Part = swApp.ActiveDoc is even in there twice. Usually most clearselections can be removed.
            • Is this the cleanest recording that you have? It seems there are even more unnecessary steps in there than normal
            • Try adding Option Explicit at the top to make sure all of your variables are defined
            • It's better to dimension swApp as SldWorks.SldWorks and Part as SldWorks.Modeldoc2 or partdoc to make sure you can only assign the correct properties to them. This will also make adding code easier because it will autocomplete your lines with the available functions.
            • Dimension myModelView as a view instead of an object, the feature as a feature