4 Replies Latest reply on Jan 13, 2015 10:07 AM by Has Bal

    View Poisition issue

    Hersh Sridhar Iyer

      Hi,

       

      I am querying view locations in order to modify it at a later point. The API documentation says that the Position Property - "

      Gets and sets the X and Y location of the drawing view origin with respect to the drawing sheet origin."

      When I run the macro provided with the api to query for view locations, I get negative co-ordinates too. Is this possible? Is the sheet origin not at the lower Left corner?

       

      Also, when I set the location of a view to (0,0), it is set way below the drawing and not at the lower left.

       

      Please advise.

       

      Thanks,

      Hersh

        • Re: View Poisition issue
          Hersh Sridhar Iyer

          Here is the code I use to set the position to (0,0) for the second view.

          The result of doing so is seen in the attached drawing.

           

          Dim swApp As Object

          Option Explicit

           

          Sub main()

           

          Set swApp = Application.SldWorks

           

           

              Dim swModel                     As SldWorks.ModelDoc2

           

              Dim swDraw                      As SldWorks.DrawingDoc

           

              Dim swView                      As SldWorks.View

           

              Dim vPos()                      As Variant

           

              Dim nNumView                    As Long

           

              Dim bRet                        As Boolean

           

              Set swApp = CreateObject("SldWorks.Application")

           

              Set swModel = swApp.ActiveDoc

           

              Set swDraw = swModel

           

              nNumView = 0

           

              Set swView = swDraw.GetFirstView

           

              Do While Not swView Is Nothing

           

                  ReDim Preserve vOutline(nNumView)

           

                  ReDim Preserve vPos(nNumView)

           

                  vOutline(nNumView) = swView.GetOutline

           

                  vPos(nNumView) = swView.Position

           

                  Debug.Print "View = " + swView.GetName2

           

                  Debug.Print "  Pos = (" & vPos(nNumView)(0) & ", " & vPos(nNumView)(1) & ") mm"

           

                  If nNumView = 2 Then

                      vPos(0) = 0

                      vPos(1) = 0

                      swView.Position = vPos

                      Exit Do

                  End If

           

                  nNumView = nNumView + 1

           

                   Set swView = swView.GetNextView

           

              Loop

           

          End Sub

            • Re: View Poisition issue
              Daniel Seaman

              Hello Hersh,

              The problem you are seeing with then drawing you posted to the forum is that the Position of the detail view is outside of its Outline by some distance.

               

              If we calculate the centre of the Outline and compare it to the Position it becomes clear why the detail view is moved off the sheet:

               

              Drawing View1X or X MinY or Y minX maxY max
              Position0.4019370.358226
              Outline0.2963070.2525960.5075670.463856
              Outline Centre0.4019370.358226

               

              Detail View BX or X MinY or Y minX maxY max
              Position0.7129460.487150
              Outline0.3363950.1112110.3821950.157010
              Outline Centre0.3592950.134110

               

              Notice that the Position of DrawingView1 is located at the centre of its Outline while the Position of Detail View B is above and right of the outline.

               

              This is because the Position of Detail View B is in the same location relative to the geometry shown as the Position of is parent view once you take the different scales into account (20:5).

               

              To compensate for this you need calculate the offset from the view Position to the Outline centre and apply that to your new position.

               

              Applying this to your macro code would result in something like this:

              ...

              Sub main()

                  ....

                      If nNumView = 2 Then
                          ' Calculate centre of outline
                          Dim dViewCentre(0 To 1) As Double
                          dViewCentre(0) = vOutline(2)(0) + (vOutline(2)(2) - vOutline(2)(0)) / 2
                          dViewCentre(1) = vOutline(2)(1) + (vOutline(2)(3) - vOutline(2)(1)) / 2
                 
                          ' Calculate offset from position to centre
                          Dim dPositionOffset(0 To 1) As Double
                          dPositionOffset(0) = vPos(2)(0) - dViewCentre(0)
                          dPositionOffset(1) = vPos(2)(1) - dViewCentre(1)
                         
                          ' Define the location you want the view centred on.
                          Dim requiredViewCentre(0 To 1) As Double
                          requiredViewCentre(0) = 0  ' X distance from bottom left in metres
                          requiredViewCentre(1) = 0 ' Y distance from bottom left in metres
                         
                          ' Apply the offset to determine the new position
                          Dim dNewPos(0 To 1) As Double
                          dNewPos(0) = requiredViewCentre(0) + dPositionOffset(0)
                          dNewPos(1) = requiredViewCentre(1) + dPositionOffset(1)
                          swView.Position = dNewPos

                          Exit Do

                      End If

                  ...

              End Sub

               

              Best regards,

              Dan Seaman

              SW API Support