Limit Distance Mate

Programming and macros
User avatar
mattpeneguy
Posts: 1380
Joined: Tue Mar 09, 2021 11:14 am
Answers: 4
x 2487
x 1888

Limit Distance Mate

Unread post by mattpeneguy »

I saw a question about how to set the Limit Distance Mate distances in a macro:

Code: Select all

    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    Set swAssy = swModel
    Set swSelMgr = swModel.SelectionManager
   

    Set swMateData = swAssy.CreateMateData(5) 'Distance mate
   

    Debug.Print "Type of mate as defined in swMateType_e: " & swMateData.TypeName
   

    Set swDistMateData = swMateData
    swDistMateData.MateAlignment = SwConst.swMateAlign_e.swMateAlignALIGNED
    swDistMateData.FlipDimension = False
    swDistMateData.Distance = 0.0254
    swDistMateData.MaximumDistance = 0.0254
    swDistMateData.MinimumDistance = 0.01
This came from the help at https://help.solidworks.com/2018/Englis ... ple_VB.htm
User avatar
josh
Posts: 265
Joined: Thu Mar 11, 2021 1:05 pm
Answers: 11
x 19
x 456

Re: Limit Distance Mate

Unread post by josh »

Actually, (if I read it right), the question was how to set those values for a specific configuration. I looked a little, but I didn't find a way to specify configs for that. The information does seem to be stored in the dimension somehow, because when you select said dimension is shows the upper and lower limits. I'm wondering if it's in the tolerances somehow.
User avatar
josh
Posts: 265
Joined: Thu Mar 11, 2021 1:05 pm
Answers: 11
x 19
x 456

Re: Limit Distance Mate

Unread post by josh »

This is how you do it:

Code: Select all

Dim swApp As Object
Sub main()

Set swApp = Application.SldWorks
Dim swDim As Dimension
Dim swTol As SldWorks.DimensionTolerance
Set swDim = swApp.ActiveDoc.Parameter("D1@LimitDistance1") 'You gotta figure out how to get the pointer to your dimension
Set swTol = swDim.Tolerance

Dim DesiredMaxValue As Double
Dim DesiredMinValue As Double
DesiredMaxValue = 0.05 'Meters
DesiredMinValue = 0

Debug.Print swTol.Type
Dim theVal As Double
swTol.GetMaxValue2 theVal
Debug.Print "Current max: " & theVal + swDim.SystemValue
swTol.GetMinValue2 theVal
Debug.Print "Current min: " & swDim.SystemValue + theVal
Debug.Print swTol.SetValues2(DesiredMinValue - swDim.SystemValue, DesiredMaxValue - swDim.SystemValue, swSetValue_InThisConfiguration, "")

End Sub
You should be able to set this per configuration using swSetValue_InSpecifiedConfigurations in the SetValues2 line. However, I was unable to get this to work at all. So you'll have to actually activate each config you want to change it in.

Also, the values you have to set are plus and minus from the current value of the dimension, wherever it's sitting in its range. So, for example, if you're currently sitting at 32mm, and you want a range of 30 to 40, you need to use -2 for the lower value and 8 for the upper. That's what the calculations using swDim.SystemValue are for.

Another caveat: if you are changing the range such that the current value now lies outside the new range (like you're at 32 and you want a range from 40 to 50), you will also need to set the dimension value to a number inside that range. Otherwise, you'll get a rebuild error.
User avatar
mattpeneguy
Posts: 1380
Joined: Tue Mar 09, 2021 11:14 am
Answers: 4
x 2487
x 1888

Re: Limit Distance Mate

Unread post by mattpeneguy »

josh wrote: Sat May 08, 2021 8:09 pm This is how you do it:

Code: Select all

Dim swApp As Object
Sub main()

Set swApp = Application.SldWorks
Dim swDim As Dimension
Dim swTol As SldWorks.DimensionTolerance
Set swDim = swApp.ActiveDoc.Parameter("D1@LimitDistance1") 'You gotta figure out how to get the pointer to your dimension
Set swTol = swDim.Tolerance

Dim DesiredMaxValue As Double
Dim DesiredMinValue As Double
DesiredMaxValue = 0.05 'Meters
DesiredMinValue = 0

Debug.Print swTol.Type
Dim theVal As Double
swTol.GetMaxValue2 theVal
Debug.Print "Current max: " & theVal + swDim.SystemValue
swTol.GetMinValue2 theVal
Debug.Print "Current min: " & swDim.SystemValue + theVal
Debug.Print swTol.SetValues2(DesiredMinValue - swDim.SystemValue, DesiredMaxValue - swDim.SystemValue, swSetValue_InThisConfiguration, "")

End Sub
You should be able to set this per configuration using swSetValue_InSpecifiedConfigurations in the SetValues2 line. However, I was unable to get this to work at all. So you'll have to actually activate each config you want to change it in.

Also, the values you have to set are plus and minus from the current value of the dimension, wherever it's sitting in its range. So, for example, if you're currently sitting at 32mm, and you want a range of 30 to 40, you need to use -2 for the lower value and 8 for the upper. That's what the calculations using swDim.SystemValue are for.

Another caveat: if you are changing the range such that the current value now lies outside the new range (like you're at 32 and you want a range from 40 to 50), you will also need to set the dimension value to a number inside that range. Otherwise, you'll get a rebuild error.
Thanks for clarifying @josh, I missed the mark on this one.
Post Reply