3 Replies Latest reply on Aug 5, 2018 2:49 AM by Sebastien Kitten

    Arrange views in drawing

    Sebastien Kitten

      Hi,

      I'm definitively losing my time since several weeks in manually arranging views in my drawing. I tried to write a macro but the view.position function doesn't seem to work. I found the SPR 682112 issue that sounds like my problem, but didn't succeed to find an alternate solution. Here is my code, does anyone can try it on any sheet with multiple views and check the result ? I'm running on SW2016 but can upgrade if necessary.

      Before macro :

      2018-06-25_11h41_27.png

      after running the macro :

      2018-06-25_11h42_01.png

      The vpos array is really weird.

      And if anyone has a solution ...

       

      Option Explicit

       

      Dim swApp As SldWorks.SldWorks

      Dim swDraw As SldWorks.DrawingDoc

      Dim swView As SldWorks.View

      Dim nextView As SldWorks.View

      Dim bool As Boolean

      Dim i As Long

      Dim vPos As Variant

      Dim vOutline As Variant

      Dim vNewPos As Variant

      Dim vNewOutline As Variant

      Dim larg_col As Double

      Dim marge As Double

      Dim first_body As Boolean

       

      Sub arrange_views() '(swModel As SldWorks.ModelDoc2)

          Set swApp = Application.SldWorks

          Dim swModel As SldWorks.ModelDoc2

          Set swModel = swApp.ActiveDoc

        

          Set swDraw = swModel

          first_body = True

          marge = 0.02

          Set swView = swDraw.GetFirstView

          Set swView = swView.GetNextView

        

          Do While Not swView Is Nothing

              vNewPos = swView.Position

              vNewOutline = swView.GetOutline

              Debug.Print swView.Name & vbCrLf & "       position INITIALE " & " : min : (" & Round(vNewOutline(0) * 1000, 2) & " ; " & Round(vNewOutline(1) * 1000, 2) & ") | MAX : (" & Round(vNewOutline(2) * 1000, 2) & " ; " & Round(vNewOutline(3) * 1000, 2) & ")"

              If vNewOutline(2) - vNewOutline(0) < vNewOutline(3) - vNewOutline(1) Then 'if height > width then rotate

                  bool = swModel.Extension.SelectByID2(swView.Name, "DRAWINGVIEW", 0, 0, 0, False, 0, Nothing, 0)

                  bool = swModel.DrawingViewRotate(90 / 57.3) 'rotation de 90° (conversion degres/radians)

                  swModel.GraphicsRedraw2

                  vNewOutline = swView.GetOutline

                  Debug.Print "APRES ROTATION : min : (" & vNewOutline(0) & " ; " & vNewOutline(1) & ") | MAX : (" & vNewOutline(2) & " ; " & vNewOutline(3) & ")"

              End If

              If first_body = True Then 'initialisation pour la première vue

                  vOutline = swView.GetOutline

                  vOutline(1) = 0.277

                  larg_col = vOutline(2)

              End If

              vNewPos(0) = marge + Abs(vNewOutline(2) - vNewOutline(0)) / 2 ' X centre de la vue à plat

              vNewPos(1) = vOutline(1) - Abs(vNewOutline(3) - vNewOutline(1)) / 2 'Y centre de la vue à plat

              swView.Position = vNewPos

              swDraw.SuppressView

              swDraw.UnsuppressView

              vNewOutline = swView.GetOutline

            

              If vNewOutline(1) < 0.05 Then ' si la vue déborde en dessous de 50mm, déplacement dans la colonne suivante

                  marge = larg_col

                  vNewPos(0) = marge + (vNewOutline(2) - vNewOutline(0)) / 2 ' X centre de la vue à plat

                  vNewPos(1) = 0.277 - (vNewOutline(3) - vNewOutline(1)) / 2 'Y centre de la vue à plat

                  swView.Position = vNewPos

                  vNewOutline = swView.GetOutline

              End If

            

              If larg_col < vNewOutline(2) Then larg_col = vNewOutline(2)

              vPos = swView.Position

              vOutline = swView.GetOutline

        

              first_body = False

              Debug.Print "       position FINALE : min : (" & Round(vNewOutline(0) * 1000, 2) & " ; " & Round(vNewOutline(1) * 1000, 2) & ") | MAX : (" & Round(vNewOutline(2) * 1000, 2) & " ; " & Round(vNewOutline(3) * 1000, 2) & ")"

            

          Set swView = swView.GetNextView

          Loop

      End Sub

       

      Thx

        • Re: Arrange views in drawing
          Fifi Riri

          Salut,

          From what I gather from your code, you're trying to place the views from bottom to top and left to right

          So here is a code that do that:

           

          Option Explicit

          Dim swApp As SldWorks.SldWorks

          Dim swModel As SldWorks.ModelDoc2

          Dim swDraw As SldWorks.DrawingDoc

          Dim swView As SldWorks.View

          Dim bool As Boolean

          Dim vPos As Variant

          Dim vOutline As Variant

          Dim marge As Double

          Dim NextPosX As Double, NextPosY As Double, WidestView As Double

          Dim swSheet As SldWorks.Sheet

          Dim SheetOutline As Variant

            

          Sub arrange_views()

              Set swApp = Application.SldWorks

              Set swModel = swApp.ActiveDoc

              Set swDraw = swModel

              Set swView = swDraw.GetFirstView

              Set swView = swView.GetNextView

              Set swSheet = swDraw.GetCurrentSheet

              SheetOutline = swSheet.GetProperties

           

              marge = 0.02

              NextPosX = marge

              NextPosY = marge

           

              Do While Not swView Is Nothing

                  vPos = swView.Position

                  vOutline = swView.GetOutline

           

                  If vOutline(2) - vOutline(0) < vOutline(3) - vOutline(1) Then 'if height > width then rotate

                      bool = swModel.Extension.SelectByID2(swView.Name, "DRAWINGVIEW", 0, 0, 0, False, 0, Nothing, 0)

                      bool = swModel.DrawingViewRotate(90 / 57.3) 'rotation de 90° (conversion degres/radians)

                      swModel.GraphicsRedraw2

                      vOutline = swView.GetOutline

                  End If

           

                  ' vérifie si il y a la place de placer cette vue dans la colonne

                  If NextPosY + vOutline(3) - vOutline(1) > SheetOutline(6) - marge Then

                      ' sinon place la vue dans la prochaine colonne

                      NextPosX = WidestView

                      NextPosY = marge

                  End If

           

                  ' positionne la vue en fonction de son contour

                  vPos(0) = NextPosX + vPos(0) - vOutline(0)

                  vPos(1) = NextPosY + vPos(1) - vOutline(1)

                  swView.Position = vPos

           

                  ' détermine le départ de la prochaine vue

                  WidestView = NextPosX + vOutline(2) - vOutline(0)

                  NextPosY = NextPosY + vOutline(3) - vOutline(1)

           

                  Set swView = swView.GetNextView

              Loop

          End Sub

            • Re: Arrange views in drawing
              Sebastien Kitten

              Hi, sorry for my very late reply, I was busy on another project. And thank you for your help. Unfortunately, your modifications didn't improve my results : here is a screenshot of the final result I get.

              2018-08-04_23h40_46.png

              Does this macro works for you ? If so, which version are you using ?

              Thanks again.

            • Re: Arrange views in drawing
              Sebastien Kitten

              Ok, I get it : problem was due to view alignment that was active between some of them. I fixed this with a simple supplementary line of code :

                      bool = swView.AlignWithView(swNoViewAlignment, BaseView)

              Topic closed, thanks Fifi Riri