1 Reply Latest reply on Oct 13, 2016 2:19 PM by Peter Brinkhuis

    How to create line in view with API

    Yong Ning

      Follow code  create line have error.




      Private Sub ll5()

          Dim swApp As SldWorks.SldWorks, swModel As ModelDoc2

               Set swApp = Application.SldWorks

               Set swModel = swApp.ActiveDoc

          Dim swDraw As DrawingDoc

               Set swDraw = swModel

          Dim SwSheet As Sheet

               Set SwSheet = swDraw.GetCurrentSheet

          Dim SwSelMgr As SelectionMgr

               Set SwSelMgr = swModel.SelectionManager

          Dim SwView As View, SwView1 As View

               Set SwView = SwSelMgr.GetSelectedObject5(1)


          Dim Var, vPos, oScale

              oScale = SwSheet.GetProperties(3)


              Var = SwView.GetOutline

              vPos = SwView.Position

          Dim x1 As Double, y1 As Double, x2 As Double, y2 As Double

              x2 = 0 'Var(0) '+ (Var(2) - Var(0)) / 2

              y2 = Abs(vPos(1) - Var(3)) * oScale

              x1 = x2

              y1 = -y2

              swDraw.CreateLine2 x1, y1, 0, x2, y2, 0 ', True

              'Set swView = swDraw.CreateSectionViewAt4(0.2, y2, 0, "A", 2, ExcludedComponents)

              Set SwView1 = swDraw.CreateSectionViewAt4(0.15, 0, 0, "A", 16, ExcludedComponents)


      End Sub







        • Re: How to create line in view with API
          Peter Brinkhuis

          At first I thought the line "Dim Var, vPos, oScale" was the problem because no variable type is selected, but then I remembered it would then fall back to the default of variant. So I decided to run the code.


          The error that I get is the text: "Variable not defined" and the variable ExcludedComponents is selected. That error seems pretty clear to me


          Now I do have the line "Option explicit" as a default on all of my macros (go to your VBA editor settings to turn it on), because not defining variables is just sloppy programming. This time it even causes weird errors. That's why I urge you do turn it on by default.


          When I remove the line Option explicit, the macro runs and it does create a section view. It just halts at the stop line, but you can manually continue the macro after that.