2 Replies Latest reply on Mar 22, 2018 7:25 PM by Michael Spens

    Align drawing dimensions macro

    Allam Yacoub

      Complete newbie, when it comes to Solidworks API. I have used recorded solidworks macro before.

      Is it possible to arrange the dimensions within the top drawing views to be identical to the dimensions in the bottom view.

      I have shown dimensions names just as a reference.

       

      Note:

      1. Top views are auto arranged.

      2. Bottom view have been manually moved after using auto arrange.

        • Re: Align drawing dimensions macro
          Allam Yacoub

          After going through similar posts this is what I have and it seems to work.

          Alignment is based on swAlignDimensionType.

           

          Dim swApp As Object

           

          Dim Part As Object

          Dim boolstatus As Boolean

          Dim longstatus As Long, longwarnings As Long

           

          Sub main()

           

          Set swApp = _Application.SldWorks

           

          Set Part = swApp.ActiveDoc

          boolstatus = Part.Extension.SelectByID2("RD7@Drawing View2", "DIMENSION", 0, 0, 0, True, 0, Nothing, 0)

          boolstatus = Part.Extension.SelectByID2("RD6@Drawing View2", "DIMENSION", 0, 0, 0, True, 0, Nothing, 0)

          boolstatus = Part.Extension.SelectByID2("RD5@Drawing View2", "DIMENSION", 0, 0, 0, True, 0, Nothing, 0)

          boolstatus = Part.Extension.SelectByID2("RD4@Drawing View2", "DIMENSION", 0, 0, 0, True, 0, Nothing, 0)

          boolstatus = Part.Extension.SelectByID2("RD3@Drawing View2", "DIMENSION", 0, 0, 0, True, 0, Nothing, 0)

          boolstatus = Part.Extension.SelectByID2("RD2@Drawing View2", "DIMENSION", 0, 0, 0, True, 0, Nothing, 0)

          boolstatus = Part.Extension.SelectByID2("RD1@Drawing View2", "DIMENSION", 0, 0, 0, True, 0, Nothing, 0)

          boolstatus = Part.Extension.SelectByID2("RD6@Drawing View1", "DIMENSION", 0, 0, 0, True, 0, Nothing, 0)

          boolstatus = Part.Extension.SelectByID2("RD1@Drawing View1", "DIMENSION", 0, 0, 0, True, 0, Nothing, 0)

          boolstatus = Part.Extension.SelectByID2("RD4@Drawing View1", "DIMENSION", 0, 0, 0, True, 0, Nothing, 0)

          boolstatus = Part.Extension.SelectByID2("RD2@Drawing View1", "DIMENSION", 0, 0, 0, True, 0, Nothing, 0)

          boolstatus = Part.Extension.SelectByID2("RD9@Drawing View1", "DIMENSION", 0, 0, 0, True, 0, Nothing, 0)

          boolstatus = Part.Extension.SelectByID2("RD8@Drawing View1", "DIMENSION", 0, 0, 0, True, 0, Nothing, 0)

          boolstatus = Part.Extension.SelectByID2("RD7@Drawing View1", "DIMENSION", 0, 0, 0, True, 0, Nothing, 0)

          boolstatus = Part.Extension.SelectByID2("RD5@Drawing View1", "DIMENSION", 0, 0, 0, True, 0, Nothing, 0)

          boolstatus = Part.Extension.SelectByID2("RD3@Drawing View1", "DIMENSION", 0, 0, 0, True, 0, Nothing, 0)

          boolstatus = Part.Extension.AlignDimensions(0, 0.06)

          Part.SetPickMode

          Part.ClearSelection2 True

          boolstatus = Part.ActivateView("Drawing View1")

          boolstatus = Part.Extension.SelectByID2("RD2@Drawing View1", "DIMENSION", 0, 0, 0, True, 0, Nothing, 0)

          boolstatus = Part.Extension.SelectByID2("RD4@Drawing View1", "DIMENSION", 0, 0, 0, True, 0, Nothing, 0)

          boolstatus = Part.Extension.SelectByID2("RD6@Drawing View1", "DIMENSION", 0, 0, 0, True, 0, Nothing, 0)

          boolstatus = Part.Extension.AlignDimensions(2, 0.06)

          Part.SetPickMode

          Part.ClearSelection2 True

          boolstatus = Part.ActivateSheet("Sheet1")

          boolstatus = Part.ActivateView("Drawing View1")

          boolstatus = Part.Extension.SelectByID2("RD5@Drawing View1", "DIMENSION", 0, 0, 0, True, 0, Nothing, 0)

          boolstatus = Part.Extension.SelectByID2("RD3@Drawing View1", "DIMENSION", 0, 0, 0, True, 0, Nothing, 0)

          boolstatus = Part.Extension.SelectByID2("RD1@Drawing View1", "DIMENSION", 0, 0, 0, True, 0, Nothing, 0)

          boolstatus = Part.Extension.SelectByID2("RD4@Drawing View1", "DIMENSION", 0, 0, 0, True, 0, Nothing, 0)

          boolstatus = Part.Extension.SelectByID2("RD2@Drawing View1", "DIMENSION", 0, 0, 0, True, 0, Nothing, 0)

          boolstatus = Part.Extension.AlignDimensions(2, 0.06)

          Part.SetPickMode

          Part.ClearSelection2 True

          boolstatus = Part.ActivateSheet("Sheet1")

          boolstatus = Part.ActivateView("Drawing View2")

          boolstatus = Part.Extension.SelectByID2("RD6@Drawing View2", "DIMENSION", 0, 0, 0, True, 0, Nothing, 0)

          boolstatus = Part.Extension.SelectByID2("RD7@Drawing View2", "DIMENSION", 0, 0, 0, True, 0, Nothing, 0)

          boolstatus = Part.Extension.AlignDimensions(2, 0.06)

          Part.SetPickMode

          Part.ClearSelection2 True

          boolstatus = Part.ActivateSheet("Sheet1")

          End Sub

            • Re: Align drawing dimensions macro
              Michael Spens

              Hello Allam,

              The more challenging part would be determining which dimensions should align between the views.  Here is some quick sample code that will traverse all dimensions on a drawing sheet, then print their name and location to the Immediate window.  It uses the DisplayDimension DisplayData to show if the dimension is vertical or horizontal.

               

              Dim swApp As Object

              Dim Part As Object

              Dim boolstatus As Boolean

              Dim longstatus As Long, longwarnings As Long

               

              Sub main()

              Set swApp = Application.SldWorks

              Set Part = swApp.ActiveDoc

              Dim draw As SldWorks.DrawingDoc

              Set draw = Part

              Dim v As View

              Set v = draw.GetFirstView 'sheet

              Set v = v.GetNextView 'first drawing view

               

              'loop through all views

              Do While Not v Is Nothing

              Debug.Print "View: " & v.Name

                  'get all dimensions

                  Dim c As Integer

                  c = v.GetAnnotationCount

                  Dim allAnnotations As Variant

                  allAnnotations = v.GetAnnotations

                  'Stop

                  Dim i As Integer

                  For i = 0 To c - 1

                      Dim ann As Annotation

                      Set ann = allAnnotations(i)

                      If ann.GetType = swDisplayDimension Then 'found a dimension

                          Dim swDim As DisplayDimension

                          Set swDim = ann.GetSpecificAnnotation

                          Dim swDimType As Long

                          swDimType = swDim.Type2 'get the dimension type

                          Dim swDimName As String

                          swDimName = swDim.GetNameForSelection 'get the dimension name

                          Debug.Print swDimName

                          Dim pos() As Double

                          pos = ann.GetPosition 'get it's position on the sheet

                          Dim posX As Double

                          Dim posY As Double

                          posX = pos(0)

                          posY = pos(1)

                          Select Case swDimType

                              Case swHorLinearDimension

                                  Debug.Print vbTab & "Horizontal Dimension"

                              Case swVertLinearDimension

                                  Debug.Print vbTab & "Vertical Dimension"

                              Case swRadialDimension

                                  Debug.Print vbTab & "Radial Dimension"

                              Case swLinearDimension

                                  Debug.Print vbTab & "Linear Dimension"

                                  Dim isHorizontal As Long

                                  isHorizontal = GetDimensionDirection(swDim)

                                  If isHorizontal = 1 Then 'horizontal

                                      Debug.Print vbTab & "Horizontal Dimension"

                                  ElseIf isHorizontal = 2 Then 'vertical

                                      Debug.Print vbTab & "Vertical Dimension"

                                  End If

                              Case Else

                                  Debug.Print vbTab & "Other Dimension"

                          End Select

                          Debug.Print vbTab & "xloc = " & posX

                          Debug.Print vbTab & "yloc = " & posY

                          Debug.Print

                         

                          'to select the dimension

                          'and add it to the selection set

                          Dim res As Boolean

                          res = ann.Select(True)

                      End If

                  Next i

                  Set v = v.GetNextView

              Loop

              Stop 'look at the Immediate window for dimension names, views and locations

              End Sub

               

               

              Private Function GetDimensionDirection(swDisplayDim As DisplayDimension) As Long

                  Dim swDisplayData As DisplayData

                  Set swDisplayData = swDisplayDim.GetDisplayData

                  Dim dimDir() As Double

                  dimDir = swDisplayData.GetArrowHeadAtIndex2(1)

                  If Abs(dimDir(3)) = 1 Then

                      GetDimensionDirection = 1 'horizontal

                  ElseIf Abs(dimDir(4)) = 1 Then

                      GetDimensionDirection = 2 'vertical

                  Else

                      GetDimensionDirection = 0 'unknown

                  End If

              End Function