US$0.00
0

SOLIDWORKS Macro to Add a Parallel Mate Between Two Selections

Add a parallel mate between two selected faces, planes or edges in a SOLIDWORKS assembly.
SOLIDWORKS Macro to Add a Parallel Mate Between Two Selections
Picture of Amen Jlili

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.

Description

  • Adds a Parallel mate between the two entities you selected in the active assembly.
  • Rebuilds the assembly afterwards.
  • Assign it to a shortcut to mate faster than through the Mate PropertyManager.

System Requirements

  • SOLIDWORKS Version: SOLIDWORKS 2018 or newer
  • Operating System: Windows 10 or later

Pre-Requisites

  • The active document must be an assembly.
  • Select exactly two faces, planes or edges before running the macro.

Results

  • A new Parallel mate appears in the Mates folder.
  • If the mate cannot be added, a message explains why.

Steps to Set Up the Macro

  1. Open SOLIDWORKS and the document described in the pre-requisites.
  1. Create the macro
  • Go to Tools > Macro > New, give the .swp file a name and save it. The VBA editor opens.
  • Replace everything in the module with the code below.
  1. Run the macro
  • Press F5 in the VBA editor, or use Tools > Macro > Run.
  • For daily use, assign it to a toolbar button or keyboard shortcut via Tools > Customize.

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 https://bluebyte.biz
' Reviewed and updated by Blue Byte Systems Inc.

Option Explicit

' Adds a Parallel mate between the two entities you selected in the active
' assembly (faces, planes or edges), then rebuilds.

Sub main()
    Dim swApp As SldWorks.SldWorks
    Dim swModel As SldWorks.ModelDoc2
    Dim swAssy As SldWorks.AssemblyDoc
    Dim swMate As SldWorks.Feature
    Dim status As Long

    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc

    If swModel Is Nothing Then
        MsgBox "Open an assembly before running this macro.", vbExclamation
        Exit Sub
    End If
    If swModel.GetType <> swDocASSEMBLY Then
        MsgBox "The active document must be an assembly.", vbExclamation
        Exit Sub
    End If
    If swModel.SelectionManager.GetSelectedObjectCount2(-1) <> 2 Then
        MsgBox "Select exactly two faces, planes or edges, then run the macro.", vbExclamation
        Exit Sub
    End If

    Set swAssy = swModel
    Set swMate = swAssy.AddMate5(swMatePARALLEL, swMateAlignCLOSEST, False, _
                                 0, 0, 0, 0, 0, 0, 0, 0, False, False, 0, status)

    If swMate Is Nothing Or status <> swAddMateError_NoError Then
        MsgBox "The parallel mate could not be added (error " & status & ")." & vbCrLf & _
               "Check that both selections are planar or linear.", vbCritical
        Exit Sub
    End If

    swModel.ClearSelection2 True
    swModel.EditRebuild3
End Sub

Code Review Notes

We reviewed this macro before publishing it here. Changes from the original version:

  • The original reported every successful mate as a failure because it checked for the wrong status code. It now uses the correct success value.
  • Uses the current AddMate5 API and checks the document type and selection count first.

Need to customize the macro?

Contact us and we will adapt this macro to your workflow, or build a complete SOLIDWORKS or PDM automation for your team.

Need this adapted to your workflow?

We build custom SOLIDWORKS macros, add-ins and PDM automation.

Picture of Amen Jlili

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.

Leave a Comment

0
0
Your Cart
Your cart is emptyReturn to Shop