VBA- Unable to change tolerance precision

VBA- Unable to change tolerance precision

Anonymous
Not applicable
1,920 Views
4 Replies
Message 1 of 5

VBA- Unable to change tolerance precision

Anonymous
Not applicable

All the values I put in to set my tolerance are being rounded to three decimal places even though the value is four place and I am setting the dimension precision to four place. 

 

For Example:  I have a hole of diameter 2.0466.  It is to have a tolerance of +.0005/-.0000.  When tolerance.setToDeviation is called, the .0005 is strangely forced to .001 and is set as this in the part file.  The precision value had already been set to 4 at this point.

 

All suggestions and help is greatly appreciated!

 

I am using the following code in Inventor 2010:

 

    'Create hole feature
    Call oCompDef.Features.HoleFeatures.AddDrilledByThroughAllExtent(oLinearPlacementDef, dDiameter, kExtentDirection)
    
    'Get hole feature just created
    Dim oHole As HoleFeature
    Set oHole = oCompDef.Features.HoleFeatures.Item(oCompDef.Features.HoleFeatures.Count) ' need most recent index
        
    'Get the parameter controlling its diameter
    Dim oDiamParam As Parameter
    Set oDiamParam = oHole.HoleDiameter
    

   'Set Precision
    oDiamParam.Precision = dPrecision
    
    'Set the tolerance
    If (sToleranceType = "Deviation") Then
        Call oDiamParam.Tolerance.SetToDeviation(dUpperTol, dLowerTol)
    ElseIf (sToleranceType = "Limits") Then
        Call oDiamParam.Tolerance.SetToLimits(kLimitsStackedTolerance, dUpperTol / 2, dLowerTol / 2)
    End If
    
    oDoc.Rebuild

 

 

0 Likes
1,921 Views
4 Replies
Replies (4)
Message 2 of 5

Anonymous
Not applicable

Update: I've traced the code and this is what I have found.

(The values from the previous post are in inches as they are the end result values)]

 

Values at run:

dUpperTol = .00127 (which is the centimeter equivalent of .0005 inches so that is good)

dLowerTol = 0

 

Before Tolerance.SetToDeviation is run:

The values in oDiam.Tolerance.Upper = 0 as well as .Lower = 0

 

After that line of code is run:

oDiam.Tolerance.Upper = 2.53999999999976E-03 (which equals .001 inches when converted from centimeters)

oDiam.Tolerance.Lower = 0

 

It appears to be rounding  within the setToDeviation line as if there is a precision property built in that is set to three-place inch decimal.

 

Any ideas?  I really need to get this figured out ASAP, thanks!

 

0 Likes
Message 3 of 5

Anonymous
Not applicable

did you ever find a solution to this?

 

0 Likes
Message 4 of 5

Vladimir.Ananyev
Alumni
Alumni

It is good idea to define also LengthDisplayPrecision property in document settings     oDoc.UnitsOfMeasure.LengthDisplayPrecision
This value is used for round operation.

Private Sub test_Tolerance()

  Dim oDoc As PartDocument
  Set oDoc = ThisApplication.ActiveDocument
  Dim oDef As PartComponentDefinition
  Set oDef = oDoc.ComponentDefinition
  
  'Reference to the first hole feature
  Dim oHole As HoleFeature
  Set oHole = oDef.Features.HoleFeatures.Item(1)
  'Get the parameter controlling hole diameter
  Dim oDiamParam As Parameter
  Set oDiamParam = oHole.HoleDiameter
  
  'inputs for deviation
  Dim InputUpper As Double, InputLower As Double
  InputUpper = 0.0005
  InputLower = -0.003
    
  Debug.Print
  Debug.Print "Input Upper = "; InputUpper, _
              "Input Lower = "; InputLower
    
  Dim iDocPrecision As Integer
  
  For iDocPrecision = 1 To 5
    'change document settings
    oDoc.UnitsOfMeasure.LengthDisplayPrecision = iDocPrecision
    'reference to the Tolerance object
    Dim oTol As Tolerance
    Set oTol = oDiamParam.Tolerance
    'Set the tolerance
    Call oTol.SetToDeviation(InputUpper, InputLower)
    'print current result
    Debug.Print "Doc Precision = "; iDocPrecision; "   "; _
      "Result Upper = "; _
       FormatNumber(oDiamParam.Tolerance.Upper, 8), _
      "Result Lower = "; _
       FormatNumber(oDiamParam.Tolerance.Lower, 8)
  Next iDocPrecision
  
  Debug.Print
  Beep
End Sub

Input Upper =  0,0005       Input Lower = -0,003 
Doc Precision =  1    Result Upper = 0,00000000         Result Lower = 0,00000000
Doc Precision =  2    Result Upper = 0,00000000         Result Lower = 0,00000000
Doc Precision =  3    Result Upper = 0,00000000         Result Lower = -0,00254000
Doc Precision =  4    Result Upper = 0,00050800         Result Lower = -0,00304800
Doc Precision =  5    Result Upper = 0,00050800         Result Lower = -0,00299720

Length units in this part document:   in.   Internal length units in Inventor API - cm.

Cheers,


Vladimir Ananyev
Developer Technical Services
Autodesk Developer Network

0 Likes
Message 5 of 5

forbillian
Advocate
Advocate

I am not sure if this helps at all, but I I referred to your postings to try to solve a tolerance precision issue I was having

& couldn't find any solution until after some forum exchanges found this simple solution.

 

My issue was I wanted to select several dimension in my drawing and add tolerance deviation on them

but couldn't alter the precision on the selected Dimensions.  All my forum searches suggested I change the Dimension Style but I will post the solution below.

 

 

'deviation example

' Set a reference to the select set of the active document.
Dim oDoc As DrawingDocument
oDoc = ThisApplication.ActiveDocument

' Find all selected occurrences and add them to an ObjectCollection.
Dim oDrawingDims As DrawingDimension
Dim oSSet As SelectSet = oDoc.SelectSet

'Loop through all dimensions'For Each oDrawingDims In oDoc.ActiveSheet.DrawingDimensions
For Each oDrawingDims In oSSet'set Tolerance Method
oDrawingDims.Tolerance.SetToDeviation( 0.5, -0.5)
'THIS LINE BELOW IS WHAT I NEEDED oDrawingDims.TolerancePrecision = 0 'THIS LINE ABOVE IS WHAT I NEEDED
Next

Credit goes to  danilo.veljkovic who responded quickly to all my questions & led me to the answer. Thankyou Danilo

0 Likes