Multi-Mate function

I'm trying to create a macro in VBA that allows all the selected planes to be mated parallel.  I would like to use a multi-mate.  I'm using AddMate3, I've tried AddMate, AddMate2 and AddMate3.  see code below.  I get error 0, unknown error.  Thank you.  PS, all planes are pre-selected.

Dim swApp As Object

Dim Part As Object

Dim boolstatus As Boolean

Dim longstatus As Long, longwarnings As Long

Sub main()

Set swApp = Application.SldWorks

Set Part = swApp.ActiveDoc

Dim myMate As Object

Set myMate = Part.AddMate3(3, 0, False, 0, 0, 0, 0, 0, 0, 0, 0, False, longstatus)

MsgBox longstatus

Part.ClearSelection2 True

End Sub

SolidworksApi macros
Answers 3
CR
CR 2009-07-22 Best Answer

I don't know if the function "MultiMate" exist between APIs , but I've solved with a (rustic) cycle between the selected objects ...


Dim Assy As SldWorks.AssemblyDoc
Dim boolstatus As Boolean
Dim longstatus As Long, longwarnings As Long


Sub main()
Dim swApp                   As SldWorks.SldWorks
Dim swModel                 As SldWorks.ModelDoc2
Dim swSelMgr                As SldWorks.SelectionMgr


Dim SelObj(5) As Object ' Max 5 object


Dim retval  As Boolean
Dim Index As Integer

On main_ERR GoTo main_ERR

Set swApp = CreateObject("SldWorks.Application")
Set swModel = swApp.ActiveDoc
Set swSelMgr = swModel.SelectionManager
   
    For Index = 0 To swSelMgr.GetSelectedObjectCount - 1
        Set SelObj(Index) = swSelMgr.GetSelectedObject(Index + 1)
    Next
   
    For Index = 1 To swSelMgr.GetSelectedObjectCount - 1
     swModel.ClearSelection
       
        ' Select the First plane
        retval = SelObj(0).Select2(False, 0)
        ' Select the next (index) object
        retval = SelObj(Index).Select2(True, 0)
        ' apply the mate
        Set myMate = swModel.AddMate2(swMatePARALLEL, 0, False, 0, 0, 0, 0, 0, 0, 0, 0, longstatus)
        If longstatus > swAddMateError_NoError Then MsgBox DescErrorStatus(longstatus)
    Next
swModel.EditRebuild3
Exit Sub
main_ERR:
    MsgBox Err.Description
End Sub


Function DescErrorStatus(ErrorS) As String
Select Case ErrorS
    Case swAddMateError_ErrorUknown
        DescErrorStatus = "Unknown error occurred"
    Case swAddMateError_NoError
        DescErrorStatus = "Success, no error"
    Case swAddMateError_IncorrectMateType
        DescErrorStatus = "Unknown mate type or mate type not present in swMateType_e"
    Case swAddMateError_IncorrectAlignment
        DescErrorStatus = "Unknown mate alignment or mate alignmnet is not present in swMateAlign_e"
    Case swAddMateError_IncorrectSelections
        DescErrorStatus = "Incorrect selections for mate"
    Case swAddMateError_OverDefinedAssembly
        DescErrorStatus = "Mate is over-defining the assembly"
    Case swAddMateError_IncorrectGearRatios
        DescErrorStatus = "Mate gear ratios are invalid"
End Select
End Function

SF
SF 2009-07-22
Perfect, I was thinking about a more brute force approach like this, but was hoping for the other way, since it works, I'll run with it, THANK YOU!
SF
SF 2009-07-22
Perfect, I was thinking about a more brute force approach like this, but was hoping for the other way, since it works, I'll run with it, THANK YOU!