17 Replies Latest reply on May 16, 2018 2:08 PM by Dave Paul

    Part View Rotate Macro

    Dave Paul

      Good Afternoon All,

      I'm looking for a function / method that will rotate a part view, in a part model, so the user can save a named view.  I can rotate the view.  The issue that I'm having is calculating the angle without drawing a new sketch.  Drag might be cool too.

      What it needs to do is to calculate the angle between the red line and the green line and rotate the view so that the hole, on the red line, is at 0° (green line).  This is just to save a named view for a drawing.  All thoughts are appreciated.



        • Re: Part View Rotate Macro
          Fatih Mehmet Ozcan

          This macro goes normal to front plane then rotates view in Z axis with default increment. I couldn't find anyway to input rotate degree. If anyone knows how to do this please share


          Dim swApp As Object

          Sub main()


          Set swApp = Application.SldWorks


          Dim boolstatus As Long

          Dim status As Long

          Dim swmodel As SldWorks.ModelDoc2

          Set swmodel = swApp.ActiveDoc


          boolstatus = swmodel.Extension.SelectByID2("Front Plane", "PLANE", 0, 0, 0, False, 0, Nothing, 0)

          swmodel.Extension.RunCommand swCommands_e.swCommands_NormalTo, “”



          End Sub

          • Re: Part View Rotate Macro
            Jacob Corder

            Dim swApp As SldWorks.SldWorks

            Sub main()

            'Dim Pi As Double

            Pi = 3.14159265359

            Set swApp = Application.SldWorks

            Dim Vectdir1() As Double

            Dim VectDir2() As Double

            ReDim Vectdir1(2)

            ReDim VectDir2(2)

            'To fill these out you need 2 3d points

            Dim StartPoint1(2) As Double

            Dim StartPoint2(2) As Double

            Dim EndPoint2(2) As Double

            Dim EndPoint1(2) As Double

            EndPoint1(1) = -1

            EndPoint2(1) = 1

            'Get Some reference to fill these out

            Dim I As Integer

            For I = 0 To 2

                 Vectdir1(I) = EndPoint1(I) - StartPoint1(I)

                 VectDir2(I) = EndPoint2(I) - StartPoint2(I)




            'Now that we have the vector magnatude defined we first need to get the Dot Product

            Dim DotProd As Double

            DotProd = (Vectdir1(0) * VectDir2(0)) + (Vectdir1(1) * VectDir2(1)) + (Vectdir1(2) * VectDir2(2))

            Dim AngleBetween As Double

            If DotProd <> 0 Then

                 Dim VectMult As Double

                 VectMult = Length(Vectdir1(0), Vectdir1(1), Vectdir1(2)) * Length(VectDir2(0), VectDir2(1), VectDir2(2))

                 If VectMult = 0 Then

                      AngleBetween = 0


                    'NEED FORMULA FOR VBA AND ARCCOS

                      AngleBetween = Acos(DotProd / VectMult)       

                 End If


                 If (Vectdir1(0) <> 0 Or Vectdir1(1) <> 0 Or Vectdir1(1) <> 0) And (VectDir2(0) <> 0 Or VectDir2(1) <> 0 Or VectDir2(2) <> 0) Then

                    AngleBetween = Pi / 2


                    AngleBetween = 0

                 End If

            End If

            Debug.Print "angle between = " & ((AngleBetween * 180) / Pi)

            End Sub




            as you see above in bold. you will need to find a way to calculate ACos or Cos ^-1

            • Re: Part View Rotate Macro
              Jordan Martins

              I have a similar question to this and believe you guys may be able to help create a macro function for it.


              When I put a drawing onto the page sometimes it isn't in the correct orientation. I already have a rotate 90 degree clockwise macro function, however it only seems to want to rotate the first drawing on the page and not the one that's selected (front or side views). It only rotates 90 degrees globally and not native to the drawing selected so sometimes doesn't rotate at all!


              I don't have any knowledge in coding so none of the above pieces make any sense to me


              If someone could help me produce a macro function for this, my workflow would definitely become much more effective!

                • Re: Part View Rotate Macro
                  Jordan Martins

                  This is the function Im using currently



                  Set swApp = Application.SldWorks



                  Set Part = swApp.ActiveDoc

                  boolstatus = Part.ActivateView("Drawing View1")

                  boolstatus = Part.Extension.SelectByRay(0.129143015056029, 0.103495081900875, -9499.9875, 0, 0, -1, 8.52786885245901E-04, 1, False, 0, 0)

                  boolstatus = Part.Extension.SelectByID2("Drawing View1", "DRAWINGVIEW", 0, 0, 0, False, 0, Nothing, 0)

                  boolstatus = Part.DrawingViewRotate(1.5707963267949)

                  End Sub

                    • Re: Part View Rotate Macro
                      Dave Paul



                      I came up with a macro a while ago that rotates the part and creates a named view "DrawingXX".  The XX is the next sequential number.  The routine counts the user's LMB clicks and issues commands based on the click number.  It ain't pretty, but it seems to work.  You can zoom in and out while the macro is running, just use the mouse wheel.


                      The first click is the face of the part that will be used in the drawing view.

                      Second click is the rotation point of the part.  The center of the part.

                      The third click is the vertex, center, point, etc. that will be the 0° mark of the part in the drawing.

                      The named view will be created after the part rotates.