Announcements
Attention for Customers without Multi-Factor Authentication or Single Sign-On - OTP Verification rolls out April 2025. Read all about it here.
tdswanson
1147 Views, 2 Replies

iLogic - Assign Preset Hole Tolerance from a Table

Hey everyone:

 

Some of you have helped me in the past with a piece of iLogic code that I was working on to assign a preset tolerance to holes from the part modeling environment.  I'm having some trouble with the code, which I'll post below.

 

The idea is to select some or all holes in the feature list, then run this iLogic rule and have it assign a hole tolerance to the feature based on its size.

 

I'll also upload an IPT file that I'm using as my example.  It has four holes in it.

 

If I select all four holes, the rule works great.

 

If I select no holes, it barks at me and quits, which is great.

 

If I select Hole 1, it adds the tolerance to only that hole, which is great.  

 

If I select Hole 1 and Hole 3, I get an error, "Error in rule: Class B Hole Tolerance, in document: iLogic Class B Hole Tolerance.ipt

Object reference not set to an instance of an object."  This is obviously a problem.

 

If I pick all features, I get a big long error.  This I don't understand as the code should be skipping the extrusion feature since it's not a hole.  Here it is:

"Error in rule: Class B Hole Tolerance, in document: iLogic Class B Hole Tolerance.ipt

Unable to cast COM object of type 'System.__ComObject' to interface type 'Inventor.HoleFeature'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{ABA7FFC5-E604-498E-B1B1-B829D4E059EC}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE))."

 

Can anyone help me trouble shoot the errors?

 

Thanks!

 

 

 

 

Dim oDoc As Document
	oDoc = ThisApplication.ActiveEditDocument
Dim i As Integer
	i = 1

Dim FeatureCount As Integer
Dim HoleModCount As Integer
Dim Proceed As Integer
If ThisDoc.Document.SelectSet.count <1 Then ' Verify the total number of selected features
	q = MessageBox.Show("You must select at least one hole.", "iLogic Error", MessageBoxButtons.OK, MessageBoxIcon.asterisk, MessageBoxDefaultButton.Button1)
	Exit Sub
End If

Dim OriginalUnits as Integer
OriginalUnits = oDoc.unitsofmeasure.LengthUnits ' Get the current units of measure

If oDoc.unitsofmeasure.LengthUnits <> 11272 Then oDoc.unitsofmeasure.LengthUnits = 11272 'Set to inches

FeatureCount = ThisDoc.Document.SelectSet.count ' Store the total number of selected features
x = MessageBox.Show(FeatureCount & " Holes in the Current Selection.  ", "My iLogic Dialog", MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1)
HoleModCount = 0 ' Zero the counter for modified holes

'For Each CurrentHole As HoleFeature In oDoc.ComponentDefinition.Features.HoleFeatures ' Run on ALL Holes
For Each CurrentHole As HoleFeature In ThisDoc.Document.SelectSet ' Run on SELECTED Holes
'For Each CurrentHole In ThisDoc.Document.SelectSet ' Run on SELECTED Features
	'If CurrentHole.Suppressed = False Then Proceed = 0 'Run command on suppressed features
	If CurrentHole.Type = ObjectTypeEnum.kHoleFeatureObject Then ' Check to make sure this feature is a hole
		Proceed = 1 ' Green light
		If CurrentHole.Tapped = True Then Proceed = 0 ' Cancel if a tapped hole
		If Proceed = 1 Then
			'x = MessageBox.Show(CurrentHole.HoleType, "My iLogic Dialog", MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1)
			Select Case CurrentHole.HoleType
				'kCounterBoreHole 21507 Hole Is counterbored. 
				'kCounterSinkHole 21506 Hole Is countersunk. 
				'kDrilledHole 21505 Hole Is drilled (no countersink Or counterboring). 
				'kSpotFaceHole 21508 Hole Is spotfaced. 
				
				Case 21505 'Hole Is drilled (no countersink Or counterboring)
					Dim oDef As PartComponentDefinition
					oDef = oDoc.ComponentDefinition
					Dim oHole(i) As HoleFeature
					oHole(i) = oDef.Features.HoleFeatures.Item(i)
					Dim oDiamParam(i) As Parameter
					oDiamParam(i) = oHole(i).HoleDiameter
					Dim CurrDiam
					CurrDiam = oDiamParam(i).Value / 2.54
					'x = MessageBox.Show(CurrDiam, "My iLogic Dialog", MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1)
					Dim oTol(i) As Tolerance
					oTol(i) = oDiamParam(i).Tolerance
					If CurrDiam < 0.062 Then oTol(i).SetToDeviation("0.003 in", "-0.001 in")
					If CurrDiam > 0.06201 And CurrDiam <= 0.250 Then oTol(i).SetToDeviation("0.010 in", "-0.004 in")
					If CurrDiam > 0.25001 And CurrDiam <= 0.500 Then oTol(i).SetToDeviation("0.012 in", "-0.004 in")
					If CurrDiam > 0.50001 And CurrDiam <= 0.750 Then oTol(i).SetToDeviation("0.016 in", "-0.006 in")
					If CurrDiam > 0.75001 And CurrDiam <= 1.000 Then oTol(i).SetToDeviation("0.020 in", "-0.006 in")
					If CurrDiam > 1.00000 Then oTol(i).SetToDeviation("0.020 in", "-0.010 in")
					HoleModCount = HoleModCount + 1
					InventorVb.DocumentUpdate() ' Update Doc
					oDoc.Update()
					
				Case 21507 'Hole Is counterbored
					'Do the Through Hole Work First
					Dim oDef As PartComponentDefinition
					oDef = oDoc.ComponentDefinition
					Dim oHole(i) As HoleFeature
					oHole(i) = oDef.Features.HoleFeatures.Item(i)
					Dim oDiamParam(i) As Parameter
					oDiamParam(i) = oHole(i).HoleDiameter
					Dim CurrDiam
					CurrDiam = oDiamParam(i).Value / 2.54
					'q = MessageBox.Show(CurrDiam, "My iLogic Dialog", MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1)
					Dim oTol(i) As Tolerance
					oTol(i) = oDiamParam(i).Tolerance
					If CurrDiam < 0.062 Then oTol(i).SetToDeviation("0.003 in", "-0.001 in")
					If CurrDiam > 0.06201 And CurrDiam <= 0.250 Then oTol(i).SetToDeviation("0.010 in", "-0.004 in")
					If CurrDiam > 0.25001 And CurrDiam <= 0.500 Then oTol(i).SetToDeviation("0.012 in", "-0.004 in")
					If CurrDiam > 0.50001 And CurrDiam <= 0.750 Then oTol(i).SetToDeviation("0.016 in", "-0.006 in")
					If CurrDiam > 0.75001 And CurrDiam <= 1.000 Then oTol(i).SetToDeviation("0.020 in", "-0.006 in")
					If CurrDiam > 1.00000 Then oTol(i).SetToDeviation("0.020 in", "-0.010 in")
					'Set a tolerance for the counterbore depth
					Dim oCBDepParam(i) As Parameter
					oCBDepParam(i) = oHole(i).CBoreDepth
					Dim CurrCBoreDep
					CurrCBoreDep = oCBDepParam(i).Value
					Dim oCboreDepTol(i) As tolerance
					oCboreDepTol(i) = oCBDepParam(i).Tolerance
					oCboreDepTol(i).settodeviation("0.010 in", "0 in")
					
					InventorVb.DocumentUpdate() ' Update Doc
					oDoc.Update()
					
					'For the Countebore
					Dim oDiamCboreParam(i) As Parameter
					oDiamCboreParam(i) = oHole(i).CboreDiameter
					Dim CurrDiamCbore
					CurrDiamCbore = oDiamCboreParam(i).Value / 2.54
					'x = MessageBox.Show(CurrDiamCbore, "My iLogic Dialog", MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1)
					Dim oTolCbore(i) As Tolerance
					oTolCbore(i) = oDiamCboreParam(i).Tolerance
					If CurrDiamCbore < 0.062 Then oTolCbore(i).SetToDeviation("0.003 in", "-0.001 in")
					If CurrDiamCbore > 0.06201 And CurrDiamCbore <= 0.250 Then oTolCbore(i).SetToDeviation("0.010 in", "-0.004 in")
					If CurrDiamCbore > 0.25001 And CurrDiamCbore <= 0.500 Then oTolCbore(i).SetToDeviation("0.012 in", "-0.004 in")
					If CurrDiamCbore > 0.50001 And CurrDiamCbore <= 0.750 Then oTolCbore(i).SetToDeviation("0.016 in", "-0.006 in")
					If CurrDiamCbore > 0.75001 And CurrDiamCbore <= 1.000 Then oTolCbore(i).SetToDeviation("0.020 in", "-0.006 in")
					If CurrDiamCbore > 1.00000 Then oTolCbore(i).SetToDeviation("0.020 in", "-0.010 in")
					
					HoleModCount = HoleModCount + 1
					
				Case 21506 ' Hole Is countersunk
					Dim oDef As PartComponentDefinition
					oDef = oDoc.ComponentDefinition
					Dim oHole(i) As HoleFeature
					oHole(i) = oDef.Features.HoleFeatures.Item(i)
					Dim oDiamParam(i) As Parameter
					oDiamParam(i) = oHole(i).HoleDiameter
					Dim CurrDiam
					CurrDiam = oDiamParam(i).Value / 2.54
					'x = MessageBox.Show(CurrDiam, "My iLogic Dialog", MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1)
					Dim oTol(i) As Tolerance
					oTol(i) = oDiamParam(i).Tolerance
					If CurrDiam < 0.062 Then oTol(i).SetToDeviation("0.003 in", "-0.001 in")
					If CurrDiam > 0.06201 And CurrDiam <= 0.250 Then oTol(i).SetToDeviation("0.010 in", "-0.004 in")
					If CurrDiam > 0.25001 And CurrDiam <= 0.500 Then oTol(i).SetToDeviation("0.012 in", "-0.004 in")
					If CurrDiam > 0.50001 And CurrDiam <= 0.750 Then oTol(i).SetToDeviation("0.016 in", "-0.006 in")
					If CurrDiam > 0.75001 And CurrDiam <= 1.000 Then oTol(i).SetToDeviation("0.020 in", "-0.006 in")
					If CurrDiam > 1.00000 Then oTol(i).SetToDeviation("0.020 in", "-0.010 in")
					HoleModCount = HoleModCount + 1
					InventorVb.DocumentUpdate() ' Update Doc
					oDoc.Update()
				
			End Select
		End If
	Else 
		Proceed = 0 ' Red light if the feature isn't a hole
	End If
	
	i = i + 1
	
Next CurrentHole ' Next Feature

ThisDoc.Document.Rebuild()
ThisApplication.CommandManager.ControlDefinitions.Item("AppUpdateMassPropertiesCmd").Execute

q = MessageBox.Show(HoleModCount & " hole(s) were updated", "iLogic Complete!", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1)


oDoc.unitsofmeasure.LengthUnits = OriginalUnits
InventorVb.DocumentUpdate() ' Update Doc
oDoc.Update()
Tags (2)