1 Reply Latest reply on Oct 30, 2014 1:22 PM by John Kangas

    Insert Auto Arrange Dimensions Macro?

    Zak Craig

      Hi everyone,


      I'm currently working with a macro that is going to automatically generate drawings for me based on configurations, but the dimensions are giving me a hard time. Since the parts in the configurations vary widely in size, the dimensions in the drawings are flying all over the place and in many cases off of the sheet.


      I want to insert an Auto Arrange Dimensions command in my macro, but I can't seem to find a way to do it. I'm new to macros and the one that I'm using was found on the forums here somewhere.


      Here's the macro:


      Dim swApp As SldWorks.SldWorks

      Dim swModel As ModelDoc2

      Dim swDrawing As DrawingDoc

      Dim configMgr As ConfigurationManager

      Dim configs() As String

      Dim lErrors As Long, lWarnings As Long


      Sub main()


      ' MAKE SURE YOU CHANGE THE swDocASSEMBLY 4 lines below to swDocPART if you are opening a part not assembly

      Dim modelLocation As String: modelLocation = "C:\Users\user\Desktop\Macro Testing\Ball Nose.sldprt"

      Dim drawingLocation As String: drawingLocation = "C:\Users\user\Desktop\Macro Testing\Drawings\Ball Nose.SLDDRW"

      Dim outputLocation As String: outputLocation = "C:\Users\user\Desktop\Macro Testing\Drawings\Output\"


      Set swApp = Application.SldWorks


      ' Start by opening the files and just presume they open fine else this is a pointless excercise anyway

      Set swModel = swApp.OpenDoc6(modelLocation, swDocPART, 0, "", lErrors, lWarnings)

      Set swDrawing = swApp.OpenDoc6(drawingLocation, swDocDRAWING, 0, "", lErrors, lWarnings)


      ' Get all configuration names for this document

      configs = swModel.GetConfigurationNames()


      ' Loop them all

      For i = 0 To swModel.GetConfigurationCount() - 1

          ' Activate configuration

          If swModel.ShowConfiguration2(configs(i)) Then

              ' Successfully activated configuration so update model

            '   swModel.ForceRebuild3 True


              ' Now make sure we have the drawing active and update that

              swApp.ActivateDoc2 drawingLocation, True, lErrors


              ' Loop all views and re-reference them

              Dim view As view

              Set view = swDrawing.GetFirstView

              ' Skip first view which is the sheet


              Set view = view.GetNextView


              While Not view Is Nothing

                  view.ReferencedConfiguration = configs(i)


                  view.UseSheetScale = False

                  view.ScaleDecimal = 2  ' 2 = 2:1   0.2 = 1:5 etc... so set it here however you want based on config name which is in configs(i)

                  Set view = view.GetNextView



              ' Update sheet

              swModel.ForceRebuild3 True



              ' And save out the drawing

              swDrawing.Extension.SaveAs outputLocation & configs(i) & ".pdf", 0, 0, Nothing, lErrors, lWarnings


          End If




      End Sub


      Where and how could I insert an Auto Arrange Dimensions command?

        • Re: Insert Auto Arrange Dimensions Macro?
          John Kangas

          Your best bet is to do all of your layout and scaling for the views before dealing with the dimension callouts. The Auto Arrange command is pretty straightforward.



          Dim DocExtension As ModelDocExtension

          Set DocExtension = swModelDocExt

               'Auto Arrange only works on selected dimension callouts

                'This is selecting everything just for demonstration, modify as necessary


              'The following is the function you're looking for!

          status = swModelDocExt.AlignDimensions(swAlignDimensionType_e.swAlignDimensionType_AutoArrange, 0.001)

               'It's good etiquette to drop the programatically selected items before exiting the macro

          swModel.ClearSelection2 (True)



          If you're handling layout and scale dynamically, I'd be interested in how you're doing that! I've done this before, but my solution isn't exactly the most elegant macro that's ever been written...