Site icon BLUE BYTE SYSTEMS INC.

SOLIDWORKS Macro to Apply Custom Color and Width to Drawing Edges

Description

This tool gives SOLIDWORKS engineers detailed visual control of their technical documents. The SolidWorks macro to apply custom color and width to drawing edges applies customized line properties (both color and width) to selected SOLIDWORKS drawing edges. This provides a visual distinction to certain edges, lines, or hidden elements of a view, because the macro immediately is able to adjust its properties based on previously defined properties.

This is very helpful when referring to certain critical dimensions, selecting particular features that need inspection, or even following unconventional drawing specifications that require emphasis on specific geometry through visual representation. The macro uses checks to ensure the active document is a drawing and that an edge has been selected before executing, to reduce run-time errors.

System Requirements

Pre-requisites

Results

Steps to Implement the Macro

VBA Macro Code

' Disclaimer:
' The code provided should be used at your own risk.  
' Blue Byte Systems Inc. assumes no responsibility for any issues or damages that may arise from using or modifying this code.  
' For more information, visit [Blue Byte Systems Inc.](https://bluebyte.biz).

Option Explicit

Sub main()

    ' Declare SolidWorks application object and initialize it
    Dim swApp As SldWorks.SldWorks
    Set swApp = Application.SldWorks
    
    ' Get the currently active document
    Dim swModel As SldWorks.ModelDoc2
    Set swModel = swApp.ActiveDoc

    ' Check if there is an active document
    If Not swModel Is Nothing Then
        ' Check if the active document is a drawing
        If swModel.GetType = swDocDRAWING Then
            ' Declare drawing document object and selection manager object
            Dim swDraw As SldWorks.DrawingDoc
            Set swDraw = swModel
            Dim swSelMgr As SldWorks.SelectionMgr
            Set swSelMgr = swModel.SelectionManager

            ' Check if any edge is selected in the drawing
            If swSelMgr.GetSelectedObjectCount2(-1) > 0 Then
                
                ' Specify custom line properties
                Dim edgeWidth As Double
                Dim edgeColor As Integer
                edgeWidth = 0.0007          ' Line width in meters (e.g., 0.0007m = 0.7mm)
                edgeColor = 255             ' Color in RGB (e.g., 255 = Red)

                ' Apply custom line width and color to the selected edge(s)
                swDraw.SetLineWidthCustom (edgeWidth)
                swDraw.SetLineColor edgeColor

                ' Notify the user of successful application
                MsgBox "Line width and color applied successfully to the selected edge(s).", vbInformation, "Success"
            Else
                ' Error message if no edge is selected
                MsgBox "Please select at least one edge in the drawing to apply the properties.", vbExclamation, "No Edge Selected"
            End If
        Else
            ' Error message if the active document is not a drawing
            MsgBox "The active document is not a drawing. Please open a drawing and select an edge.", vbExclamation, "Invalid Document Type"
        End If
    Else
        ' Error message if no document is open
        MsgBox "No active document found. Please open a drawing and select an edge.", vbExclamation, "No Active Document"
    End If

End Sub

Macro

You can download the macro from here.

Need help customizing the macro?

Our team of experts at Blue Byte Systems Inc. are experienced in the development of custom SOLIDWORKS macros and add-ins.

Exit mobile version