I have made an attempt at a solution for you. I'm not sure if it is exactly what you wanted, and it may need more 'tweaking', but there's quite a bit of work done for you in there, so it should be a pretty good start, if you need to further customize it.
I created a custom 'Enum' to control the expected input variable for what tolerance class you want to use.
I created a separate Sub routine do define all the tolerance and tolerance class settings in, to leave the main Sub a but cleaner, and you could even move more of the main Sub's code down into it if you wanted.
It assumes you are targeting the 'first' planar sketch in the part document, but you can change that however you want, if you need to specify a different sketch to target.
It is only targeting two variations of dimension constraints within the sketch, because there are many types, and not all are linear, just to avoid potential error for now, but you can expand which ones you want it to work with if you need to.
It is set to only work with dimensions that are 'driving' (not 'driven').
And it only tries to set a tolerance if the parameter being represented by that dimension is currently set to 'Default', so it won't bother ones that have a different type of tolerance already set.
Here's the code.
(Make sure you read through it before attempting to run it, and probably run it on a 'test' part first, just to be safe.)
Sub Main
If ThisApplication.ActiveDocumentType <> DocumentTypeEnum.kPartDocumentObject Then
MsgBox("A Part Document must be active for this rule to work. Exiting.",vbOKOnly+vbCritical, "WRONG DOCUMENT TYPE")
Exit Sub
End If
Dim oPDoc As PartDocument = ThisApplication.ActiveDocument
Dim oPDef As PartComponentDefinition = oPDoc.ComponentDefinition
Dim oSketch As PlanarSketch = oPDef.Sketches.Item(1)
For Each oDim As DimensionConstraint In oSketch.DimensionConstraints
'MsgBox("oDim.Type.ToString = " & oDim.Type.ToString, , "")
If oDim.Driven = False Then
If oDim.Type = ObjectTypeEnum.kTwoPointDistanceDimConstraintObject Or _
oDim.Type = ObjectTypeEnum.kTwoPointDistanceDimConstraintObject Then
If oDim.Parameter.Tolerance IsNot Nothing AndAlso oDim.Parameter.Tolerance.ToleranceType = ToleranceTypeEnum.kDefaultTolerance Then
'call sub here to set tolerance the way you want it
SetTolerance(oDim, ToleranceClassEnum.Fine)
End If
End If
End If
Next
End Sub
Public Sub SetTolerance(ByRef oDimC As DimensionConstraint, ByVal oTolClass As ToleranceClassEnum)
'just recreating the two variables here to enable 'intellisense' below
Dim oDim As DimensionConstraint = oDimC
Dim oTC As ToleranceClassEnum = oTolClass
Dim oVal As Double = oDim.Parameter.Value
Dim oTol As Tolerance = oDim.Parameter.Tolerance
'define the rules of the tolerances by tolerance class
If oTC = ToleranceClassEnum.Fine Then
If oVal > .5 AndAlso oVal <= 3 Then
oTol.SetToSymmetric(.05)
ElseIf oVal > 3 AndAlso oVal <= 6 Then
oTol.SetToSymmetric(.05)
ElseIf oVal > 6 AndAlso oVal <= 30 Then
oTol.SetToSymmetric(.1)
ElseIf oVal > 30 AndAlso oVal <= 120 Then
oTol.SetToSymmetric(.15)
ElseIf oVal > 120 AndAlso oVal <= 400 Then
oTol.SetToSymmetric(.2)
ElseIf oVal > 400 AndAlso oVal <= 1000 Then
oTol.SetToSymmetric(.3)
ElseIf oVal > 1000 AndAlso oVal <= 2000 Then
oTol.SetToSymmetric(.5)
ElseIf oVal > 2000 AndAlso oVal <= 4000 Then
'do nothing, out of range
End If
ElseIf oTC = ToleranceClassEnum.Medium Then
If oVal > .5 AndAlso oVal <= 3 Then
oTol.SetToSymmetric(.1)
ElseIf oVal > 3 AndAlso oVal <= 6 Then
oTol.SetToSymmetric(.1)
ElseIf oVal > 6 AndAlso oVal <= 30 Then
oTol.SetToSymmetric(.2)
ElseIf oVal > 30 AndAlso oVal <= 120 Then
oTol.SetToSymmetric(.3)
ElseIf oVal > 120 AndAlso oVal <= 400 Then
oTol.SetToSymmetric(.5)
ElseIf oVal > 400 AndAlso oVal <= 1000 Then
oTol.SetToSymmetric(.8)
ElseIf oVal > 1000 AndAlso oVal <= 2000 Then
oTol.SetToSymmetric(1.2)
ElseIf oVal > 2000 AndAlso oVal <= 4000 Then
oTol.SetToSymmetric(2.0)
End If
ElseIf oTC = ToleranceClassEnum.Coarse Then
If oVal > .5 AndAlso oVal <= 3 Then
oTol.SetToSymmetric(.2)
ElseIf oVal > 3 AndAlso oVal <= 6 Then
oTol.SetToSymmetric(.3)
ElseIf oVal > 6 AndAlso oVal <= 30 Then
oTol.SetToSymmetric(.5)
ElseIf oVal > 30 AndAlso oVal <= 120 Then
oTol.SetToSymmetric(.8)
ElseIf oVal > 120 AndAlso oVal <= 400 Then
oTol.SetToSymmetric(1.2)
ElseIf oVal > 400 AndAlso oVal <= 1000 Then
oTol.SetToSymmetric(2.0)
ElseIf oVal > 1000 AndAlso oVal <= 2000 Then
oTol.SetToSymmetric(3.0)
ElseIf oVal > 2000 AndAlso oVal <= 4000 Then
oTol.SetToSymmetric(4.0)
End If
ElseIf oTC = ToleranceClassEnum.VeryCoarse Then
If oVal > .5 AndAlso oVal <= 3 Then
'do nothing, out of range
ElseIf oVal > 3 AndAlso oVal <= 6 Then
oTol.SetToSymmetric(.5)
ElseIf oVal > 6 AndAlso oVal <= 30 Then
oTol.SetToSymmetric(1.0)
ElseIf oVal > 30 AndAlso oVal <= 120 Then
oTol.SetToSymmetric(1.5)
ElseIf oVal > 120 AndAlso oVal <= 400 Then
oTol.SetToSymmetric(2.5)
ElseIf oVal > 400 AndAlso oVal <= 1000 Then
oTol.SetToSymmetric(4.0)
ElseIf oVal > 1000 AndAlso oVal <= 2000 Then
oTol.SetToSymmetric(6.0)
ElseIf oVal > 2000 AndAlso oVal <= 4000 Then
oTol.SetToSymmetric(8.0)
End If
End If
End Sub
Public Enum ToleranceClassEnum
Fine
Medium
Coarse
VeryCoarse
End Enum
If this solved your problem, or answered your question, please click ACCEPT SOLUTION.
Or, if this helped you, please click (LIKE or KUDOS) 👍.
If you want and have time, I would appreciate your Vote(s) for My IDEAS 💡or you can Explore My CONTRIBUTIONS
Wesley Crihfield

(Not an Autodesk Employee)