MACRO LIBRARY

Edit the alignment of a mate

Free public access. No account or subscription required.

Files

Edit the alignment of a mate Associated model(s)

Code

'This macro demonstrates how the alignment of a mate is changed
    
'Written by Keith Rice
'CADSharp LLC
'www.cadsharp.com
    
'Preconditions: An assembly with two components, Part1 and Part2, are mated with a _
    coincident mate (files may be downloaded from www.cadsharp.com)
    
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swAssy As SldWorks.AssemblyDoc
Dim swComp As SldWorks.Component2
Dim swBody As SldWorks.Body2
Dim vBodies As Variant
Dim swFace As SldWorks.Face2
Dim vFaces As Variant
Dim swEnt1 As SldWorks.Entity
Dim swEnt2 As SldWorks.Entity
Dim swEnt3 As SldWorks.Entity
Dim swEnt4 As SldWorks.Entity
Dim swSelMgr As SldWorks.SelectionMgr
Dim swSelData As SldWorks.SelectData
Dim i As Integer
    
Sub main()
    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    Set swAssy = swModel
    Set swSelMgr = swModel.SelectionManager
    Set swSelData = swSelMgr.CreateSelectData
    
    'Select first face and mark with 1
    Set swComp = swAssy.GetComponentByName("Part1-1")
    vBodies = swComp.GetBodies3(swSolidBody, Empty)
    Set swBody = vBodies(0)
    vFaces = swBody.GetFaces
    For i = 0 To UBound(vFaces)
        Set swFace = vFaces(i)
        Set swEnt1 = swFace
        If swModel.GetEntityName(swEnt1) = "1" Then Exit For
    Next i
    swSelData.Mark = 1
    swEnt1.Select4 False, swSelData
    
    'Get second face and mark with 1
    Set swComp = swAssy.GetComponentByName("Part2-1")
    vBodies = swComp.GetBodies3(swSolidBody, Empty)
    Set swBody = vBodies(0)
    vFaces = swBody.GetFaces
    For i = 0 To UBound(vFaces)
        Set swFace = vFaces(i)
        Set swEnt2 = swFace
        If swModel.GetEntityName(swEnt2) = "2" Then Exit For
    Next i
    swSelData.Mark = 1
    swEnt2.Select4 True, swSelData
    
    'This will search assy feature manager tree for last coincident mate and select it
    Dim swFeat As SldWorks.Feature
    Dim swSubFeat As SldWorks.Feature
    Dim swMateFeat As SldWorks.Feature
    For i = 0 To swModel.GetFeatureCount - 1
        Set swFeat = swModel.FeatureByPositionReverse(i)
        If swFeat.GetTypeName = "MateGroup" Then
            Set swSubFeat = swFeat.GetFirstSubFeature
            While Not swSubFeat Is Nothing
                If swSubFeat.GetTypeName = "MateCoincident" Then Set swMateFeat = swSubFeat
                Set swSubFeat = swSubFeat.GetNextSubFeature
            Wend
            Exit For
        End If
    Next i
    swMateFeat.Select2 True, Empty
    
    'Edit mate alignment
    Dim lngErrors As Long
    swAssy.EditMate2 swMateCOINCIDENT, swMateAlignALIGNED, False, Empty, Empty, Empty, _
        Empty, Empty, Empty, Empty, Empty, lngErrors
    Debug.Print lngErrors
End Sub