Description
This VBA macro automates selecting and arranging dimensions in SOLIDWORKS drawings. It loops through views, selects display dimensions, and auto-arranges them.
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
- Make sure the SOLIDWORKS drawing you want to align dimensions in is open and active.
Load the Macro File (.swp)
- Download the provided
.swpmacro file, or - Copy the code (from
Option ExplicittoEnd Sub) and save it as.swpusing 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
.swpfile - Click Run. The macro will align dimensions automatically
VBA Macro Code
Visual Basic
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
' ******************************************************************** ' 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.
