Problem creating slide stress

Hello,

I have a macro that inserts parts and creates constraints, it works great when I want to create slide constraints with a two-sided parallel slider, but not with a round slider. By doing it by hand it is not a problem, so to see where the problem comes from I created a simplified model with a round slider and a parallel and two corresponding slide constraints created by hand.

When I want to recreate a new constraint with the one previously created by hand, everything works fine, but not when I create it from scratch.

After investigation, it seems that the WidthMateFeatureData object used to create a new constraint only accepts 2 faces by default (when created with createMateData(11)) and " refuses " its modification with a single-sided slide.

Conversely, when it is created by hand with a single-sided slide, it " refuses " to be modified into a two-sided slide.
Do you know how to get the WidthMateFeatureData object to accept a round slider from scratch? (TabSelection attribute of size 1)
Or a trick to be able to create a width constraint with a round slider?

To be more precise, here is a piece of my commented code

'swCircularMateData : objet WidthMateFeatureData d’une contrainte glissière avec un coulisseau rond (créée à la main)
'swOldMateData : objet WidthMateFeatureData d’une contrainte glissière avec un coulisseau à deux faces parallèles (créée à la main)

Set swTestMateData = swCircularMateData 'récupère l'objet WidthMateFeatureData créé à la main
swAssembly.CreateMate swTestMateData 'crée la contrainte sans problème

Set swTestMateData = swAssembly.CreateMateData(11) 'crée un objet WidthMateFeatureData vide
'Recopie l'intégralité des attributs de l'objet
swTestMateData.ConstraintType = swCircularMateData.ConstraintType
swTestMateData.DistanceFromEnd = swCircularMateData.DistanceFromEnd
swTestMateData.FlipDimension = swCircularMateData.FlipDimension
swTestMateData.PercentDistanceFromEnd = swCircularMateData.PercentDistanceFromEnd
swTestMateData.TabSelection = swCircularMateData.TabSelection
swTestMateData.WidthSelection = swCircularMateData.WidthSelection

swAssembly.CreateMate swTestMateData 'la contrainte ne se crée pas
vTest = swTestMateData.TabSelection 'en inspectant l'attribut TabSelection, il s'avère être vide

swTestMateData.TabSelection = swParalleleMateData.TabSelection 'recopie l'attribut TabSelection de la contrainte avec le coulisseau parallele
swAssembly.CreateMate swTestMateData 'crée la contrainte sans problème

Hello @Léo_RHODES1 , and welcome to this forum...

Indeed, the " CreateMate " function stubbornly refuses to create a " Slide " constraint with a slider with only a cylindrical surface.
If there is a solution, it is well hidden... :joy:

On the other hand, the " AddMate5 " function, declared obsolete because it is older, does the job.

The code below works on my basic example.

Option Explicit
    
' Génération d'une contrainte d'assemblage de type glissière, entre deux faces planes d'une pièce
' et une face cylindrique d'une autre.
' La fonction de l'API utilisée est AddMate5, obsolète, mais qui a le mérite de fonctionner...
    
    Dim swApp As SldWorks.SldWorks
    Dim swModel As ModelDoc2
    Dim swAssemb As AssemblyDoc
    Dim swSelMgr As SelectionMgr

Sub Main()
    
    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    Set swAssemb = swModel
    Set swSelMgr = swModel.SelectionManager

    ' Sélection des faces dans l'ordre: 2 plans parallèles d'une pièce, un cylindre d'une autre
    Dim faceWidth(1) As Face2
    Dim faceTab(0) As Face2
    
    Set faceWidth(0) = swSelMgr.GetSelectedObject6(1, -1)
    Set faceWidth(1) = swSelMgr.GetSelectedObject6(2, -1)
    Set faceTab(0) = swSelMgr.GetSelectedObject6(3, -1)
    
    ' Application des Marks exigés par AddMate5
    swModel.ClearSelection2 True
    Dim swSelData As SldWorks.SelectData
    Set swSelData = swSelMgr.CreateSelectData
    
    swSelData.Mark = 1
    faceWidth(0).Select4 True, swSelData
    faceWidth(1).Select4 True, swSelData
    swSelData.Mark = 16
    faceTab(0).Select4 True, swSelData

    ' Création de la contrainte
    Dim swMate As SldWorks.Mate2
    Dim errorStatus As Long
    
    ' Le paramètre 0 après swMateWIDTH correspond à l'option "Centré"
    Set swMate = swAssemb.AddMate5(swMateWIDTH, 0, False, 0, 0, 0, 0, 0, 0, 0, 0, False, False, 0, errorStatus)

    If swMate Is Nothing Then
        MsgBox "Erreur AddMate5. Code : " & errorStatus, vbCritical
    Else
        MsgBox "Contrainte créée avec AddMate5."
        swModel.ClearSelection2 True
    End If
End Sub
4 Likes

Thanks for the answer! I'll make the changes on Monday morning and keep you informed of the result!

1 Like

I've integrated this into my code and it seems to work well! To be seen in use!

Thank you, you have removed a beautiful thorn in my side!