US$0.00
0

SOLIDWORKS Macro for Automated Mate Toggling and Assembly Constraint Testing

Description

  • The macro targets a user-defined mate set to “Angle 0 Deg.”
  • The macro toggles the mate’s suppression status (unsuppressing and immediately repressing).
SOLIDWORKS Macro for Automated Mate Toggling and An image divided into two side-by-side sections labeled "Before" and "After," showing a SOLIDWORKS software interface. The "Before" section displays a 3D model of a rectangular base with two red cylindrical components attached, viewed in an isometric motion study. The feature tree on the left lists "Control Panel" and "Valve Assembly" with constraints like "Angle 0 Deg" and "Concentric." The "After" section shows the same model with an additional red cylindrical component added to the right side, and the feature tree updated to reflect the new assembly and constraints. Both sections include toolbars and a 3D view with axes indicators.

System Requirements

  • SolidWorks Version: SolidWorks 2014 or newer
  • Operating System: Windows 7 or later

Pre-Requisites

  • This macro must be executed from within an active SOLIDWORKS assembly document that contains the indicated target mate called “Angle 0 Deg.”. (The code can be changed to target any of the mates.)

Results

  • The assigned mate, “Angle 0 Deg,” is programmatically toggled between suppressed and unsuppressed states. 
  • The automation cycle checks to see if the assembly constraints are stable without permanently changing any constraints.
  • Optional: The code contains an additional feature to maximize the view of the assembly during the operation for visual feedback, which can be turned on easily by the user.

Steps to Set Up the Macro

  1. Open the SOLIDWORKS Assembly
  • Check that the Active Document in SOLIDWORKS is the Assembly file that contains the specific mate that you will be toggling.
  1. Access and Load the VBA Macro
  • Access the VBA Editor: Open the VBA editor in SolidWorks by pressing (Alt + F11).
  • Load the Code: Insert a new Module (Insert > Module) and then paste the provided VBA Code from the SOLIDWORKS macro for automated mate toggling into the module window.
  1. Run the Macro
  • Run the macro from within the VBA Editor itself (Run > Run Sub/UserForm) or save the macro file (.swp) and run the macro from the SOLIDWORKS menu (Tools > Macro > Run) or as a custom button on a custom Toolbar.

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

' Declare variables for the SolidWorks application and active document
Dim swApp As Object               ' SolidWorks application object
Dim Part As Object                ' Active document object (part or assembly)
Dim boolstatus As Boolean         ' Boolean to capture operation success
Dim longstatus As Long, longwarnings As Long ' Longs to capture detailed operation statuses

Sub main()
    ' Initialize the SolidWorks application object
    Set swApp = Application.SldWorks
    
    ' Get the currently active document
    Set Part = swApp.ActiveDoc

    ' Optional: Declare and set the active model view
    Dim myModelView As Object       ' Object for controlling the model view
    Set myModelView = Part.ActiveView
    
    ' Optional: Maximize the SolidWorks window (uncomment to enable functionality)
    ' myModelView.FrameState = swWindowState_e.swWindowMaximized
    
    ' Step 1: Select the mate named "Angle 0 Deg"
    boolstatus = Part.Extension.SelectByID2("Angle 0 Deg", "MATE", 0, 0, 0, False, 0, Nothing, 0)
    
    ' Step 2: Unsuppress the selected mate to activate it
    Part.EditUnsuppress2
    
    ' Step 3: Clear the current selection to reset the environment
    Part.ClearSelection2 True
    
    ' Step 4: Re-select the mate named "Angle 0 Deg"
    boolstatus = Part.Extension.SelectByID2("Angle 0 Deg", "MATE", 0, 0, 0, False, 0, Nothing, 0)
    
    ' Step 5: Suppress the selected mate to deactivate it
    Part.EditSuppress2
    
    ' Step 6: Clear the selection again to clean up the selection state
    Part.ClearSelection2 True
End Sub

Macro

You can download the macro fromĀ here.

Need to customize the macro?

Contact us today to help create a more powerful, custom solution for your specific constraint testing experience.

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
    ×