MACRO LIBRARY

Automate mating with mate references

Free public access. No account or subscription required.

Files

Automate mating with mate references

Code

'Demonstrates how mate reference entities are accessed, which could be used as the basis for _
    a macro that programmatically mates components.
    
'Mate automation using mate references should be used only if the models already have _
    mate references. Otherwise it is recommended that users simply name edges. Indeed _
    it might even be easiest to write a tool that converts mate references to entity _
    names in a part before programmatically mating with entity names.
    
'Preconditions: assembly is open containing components with mate references
'Results: entities are selected
    
'Written by Keith Rice
'CADSharp LLC
'www.cadsharp.com
    
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swAssy As SldWorks.AssemblyDoc
Dim swComp As SldWorks.Component2
Dim swCompModel As SldWorks.ModelDoc2
Dim swCompPart As SldWorks.PartDoc
Dim swMateRef As SldWorks.MateReference
Dim swEnt As SldWorks.Entity
Dim swFeat As SldWorks.Feature
Dim swSubFeat As SldWorks.Feature
Dim vComps As Variant
Dim i As Integer
Dim j As Integer
    
Sub main()
    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    Set swAssy = swModel
    vComps = swAssy.GetComponents(False)
    For i = 0 To UBound(vComps)
        Set swComp = vComps(i)
        Debug.Print "Component name: " & swComp.Name2
        Set swFeat = swComp.FirstFeature
        While Not swFeat Is Nothing
            If swFeat.GetTypeName2 = "PosGroupFolder" Then
                Set swMateRef = swFeat.GetSpecificFeature2
                Debug.Print "    Mate reference name: " & swMateRef.Name
                Debug.Print "        Entity count: " & swMateRef.ReferenceEntityCount
                For j = 0 To swMateRef.ReferenceEntityCount - 1
                    If swMateRef.ReferenceEntityType(j) = 1 Then Debug.Print "        Primary entity type: Edge"
                    If swMateRef.ReferenceEntityType(j) = 2 Then Debug.Print "        Secondary entity type: Face"
                    If swMateRef.ReferenceEntityType(j) = 3 Then Debug.Print "        Tertiary entity type: Vertex"
                    Set swEnt = swMateRef.ReferenceEntity2(j)
                    swEnt.Select4 True, Nothing
                Next j
            End If
            Set swFeat = swFeat.GetNextFeature
        Wend
        Debug.Print ""
    Next i
End Sub
    
'Notes: mate references are not sub-features, they are top-level features of type _
    "PosGroupFolder".
    
'Even if mate references are present in a component, the components still have to be mated _
    using IAssemblyDoc::AddMate3. This basically means that using mate references for mate _
    automation should only be done if mate references are already present and there isn't _
    an easy way to programmatically find the entities needed for mating using geometry and _
    topology, or named entities.