5 Replies Latest reply on Nov 6, 2017 11:19 AM by Adrian Luparu

    auto rotate view based on max dimension of flat c#

    Adrian Luparu

      I have a macro that auto generate flats, add values from custom properties and auto dimension of flat for manufacturing process. On flat view the dimension are automated I can retrieve the dimensions list from this view, but I want to rotate it if dimension on Y is bigger than X.

      1) Do we have a function or method for specific dimensions placed horizontal or vertical to use it later as flag for rotation by comparing values.

      2) Another hypothesis is to retrieve the view dimensions and rotate it ??

      I hope I made my self clear.



      Thank you

        • Re: auto rotate view based on max dimension of flat c#
          John Alexander

          I used the following method for a cutsheet macro that I wrote a few months ago. I don't want to post the whole thing but you can get an idea of the approach from these snippets.


          I chose to use the bounding box of the model in the view rather than the view's border dimensions. The view's border dimensions will change when annotations are added to the view so it isn't a reliable reference. With the model's bounding box, you can make up some convention for which dimension should be larger (with respect to the drawing's coordinate system). i.e. "x" should be larger than "y". For any particular view, you can check if the bounding box, when rotated by the ModeltoViewTransform, satisfies your criteria. If it does not, you can rotate the view. If you put that check and the rotation in a loop, it will converge to the orientation that you desire.


          This is VBA but I think it is fairly readable and conveys the intent.

          Dim needs_rotate        As Boolean

          Dim prescribed_rotation As Integer 'lazy enumeration

          needs_rotate = True

          While needs_rotate

              prescribed_rotation = prescribeRotation(swView)

              If prescribed_rotation = -1 Then

                  needs_rotate = False

              ElseIf prescribed_rotation = 0 Then

                  Set swView = rotateViewX(swView)

              ElseIf prescribed_rotation = 1 Then

                  Set swView = rotateViewY(swView)


                  Set swView = rotateViewZ(swView)

              End If



          rotateViewX() rotates about the X axis in sheet coordinate by increments of 90 degrees. I wrote a similar routine for X,Y,Z. Z just rotates the view in place but X and Y have to create an unfolded view and then delete the parent to achieve the "rotation". The direction of this rotation is dependent on your graphical projection convention (1st vs 3rd angle). A cleaner way of achieving this would be to assign the model's standard views "Front", "Right", etc.

          Private Function rotateViewX(swView As SldWorks.View) As SldWorks.View

              'Project Up

              Dim swDraw      As SldWorks.DrawingDoc

              Dim newView     As SldWorks.View

              Dim position_x  As Double

              Dim position_y  As Double

              Dim attempts  As Integer


              Set swDraw = pDrawModel

              moveViewCenterToPosition swView, 0, 0

              position_x = 0#

              position_y = 1#

              Set newView = Nothing

              attempts = 0

              While newView Is Nothing 'this is pretty sloppy, sometimes it would fail to unfold

                  attempts = attempts + 1


                  swDraw.Extension.SelectByID2 swView.GetName2, "DRAWINGVIEW", 0, 0, 0, True, 0, Nothing, 0

                  Set newView = swDraw.CreateUnfoldedViewAt3(position_x, position_y, 0#, True)

                  If attempts > 20 Then

                      MsgBox ("Unfolding of Drawing View (" & swView.GetName2 & ") failed after " & attempts & " attempts." & Chr(13) & "Ending Macro")


                  End If



              deleteView swDraw, swView

              Set rotateViewX = newView

          End Function


          Prescriberotation just determines which axis the view needs to rotate about to achieve the convention that I made up (L >= H  >= W)


          Private Function prescribeRotation(swView As SldWorks.View) As Integer

              '-1: None

              ' 0: X

              ' 1: Y

              ' 2: Z


              Dim swBody          As SldWorks.Body2

              Dim swBodyVect      As SldWorks.MathVector

              Dim arrayData(2)    As Double

              Dim body_box        As Variant

              Dim x               As Double

              Dim y               As Double

              Dim z               As Double


              Set swBody = swView.bodies(0)

              body_box = swBody.GetBodyBox

              arrayData(0) = Abs(body_box(3) - body_box(0))

              arrayData(1) = Abs(body_box(4) - body_box(1))

              arrayData(2) = Abs(body_box(5) - body_box(2))

              Set swBodyVect = pMathUtil.CreateVector(arrayData)

              Set swBodyVect = swBodyVect.MultiplyTransform(swView.ModelToViewTransform)

              x = Abs(swBodyVect.arrayData(0))

              y = Abs(swBodyVect.arrayData(1))

              z = Abs(swBodyVect.arrayData(2))

              If getMaxComponent(x, y, z) = 0 Then

                  If y >= z Then

                      prescribeRotation = -1


                      prescribeRotation = 0

                  End If

              ElseIf getMaxComponent(x, y, z) = 1 Then

                  prescribeRotation = 2


                  prescribeRotation = 1

              End If

          End Function