US$0.00
0

SOLIDWORKS Macro to Align All Dimensions Automatically

Description

This VBA macro automates selecting and arranging dimensions in SOLIDWORKS drawings. It loops through views, selects display dimensions, and auto-arranges them.

SOLIDWORKS Macro to Align All Dimensions Automatically and

System Requirements

To run this VBA macro, ensure that your system meets the following requirements:

  • SOLIDWORKS Version: SOLIDWORKS 2018 or later
  • VBA Environment: Pre-installed with SOLIDWORKS (Access via Tools > Macro > New or Edit)
  • Operating System: Windows 7, 8, 10, or later.

Prerequisites

In order for the macro to run as intended, the following condition must be satisfied:

  • There is an active drawing document (.SLDDRW) open, and it is the active document in SOLIDWORKS.

Steps to Set Up the Macro

Open Your Drawing

Load the Macro File (.swp)

  • Download the provided .swp macro file, or
  • Copy the code (from Option Explicit to End Sub) and save it as .swp using VBA Editor or any text editor.
  • Remember where you save it.

This SolidWorks macro to align all dimensions automatically ensures your dimension layout stays clean and readable without manual adjustments.

Run the Macro

  • Go to Tools > Macro > Run
  • Select the .swp file
  • Click Run. The macro will align dimensions automatically

VBA Macro Code

' ********************************************************************
' DISCLAIMER: 
' This code is provided as-is with no warranty or liability by 
' Blue Byte Systems Inc. The company assumes no responsibility for 
' any issues arising from the use of this code in production.
' ********************************************************************

Option Explicit

Public swApp As SldWorks.SldWorks
Public swModel As SldWorks.ModelDoc2
Public swDraw As SldWorks.DrawingDoc
Public swView As SldWorks.View
Public swDispDim As SldWorks.DisplayDimension
Public swAnn As SldWorks.Annotation

Sub main()

    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    
    ' Ensure the active document is a drawing
    If swModel.GetType <> swDocDRAWING Then
        MsgBox "This macro only works on drawings!", vbCritical, "Error"
        Exit Sub
    End If
    
    Set swDraw = swModel
    Set swView = swDraw.GetFirstView
    swModel.ClearSelection2 True
    
    ' Loop through all views and select display dimensions
    Do While Not swView Is Nothing
        Set swDispDim = swView.GetFirstDisplayDimension5
        Do While Not swDispDim Is Nothing
            Set swAnn = swDispDim.GetAnnotation
            swAnn.Select True
            Set swDispDim = swDispDim.GetNext5
        Loop
        Set swView = swView.GetNextView
    Loop
    
    ' Auto-arrange dimensions
    swModel.Extension.AlignDimensions swAlignDimensionType_AutoArrange, 0.06
    swModel.ClearSelection2 True
    
End Sub

Macro

You can download the macro from here.

Customization

Need to modify the macro to meet specific requirements or integrate it with other processes? We provide custom macro development tailored to your needs. Contact us.

Author

Amen Jlili

Amen Jlili is the founder and technical director of Blue Byte Systems Inc., a software company in Vancouver, Canada, specializing in automating SOLIDWORKS and PDM. With over a decade of experience, he has authored several courses and open-source frameworks related to the SOLIDWORKS API. His leadership ensures that Blue Byte Systems prioritizes customer satisfaction and delivers high-quality software and CAD design solutions.
0
    0
    Your Cart
    Your cart is emptyReturn to Shop
    ×