Announcements
Attention for Customers without Multi-Factor Authentication or Single Sign-On - OTP Verification rolls out April 2025. Read all about it here.
WCrihfield
in reply to: jeslinakoruth

Hi @jeslinakoruth.  This is just a slightly modified version of your original code.  I added some additional feedback into it, and enclosed some areas of the code within a few Try...Catch...End Try statements, to help narrow down where errors are happening, and potentially avoid them.  I do not know if this will actually eliminate any errors you are currently seeing, but it may help you narrow down where the error is happening or further develop your code in a way to avoid the errors.

' Prompt the user for the part OD
Dim sPartOD As String = InputBox("Enter the Part OD:", "Part OD Input", "")
If sPartOD = "" Then Return 'exit rule if no value entered
Dim dPartOD As Double
If Double.TryParse(sPartOD, dPartOD) = False Then
	MsgBox("Could not convert input value to a Double!", vbCritical, "iLogic")
	Return
End If

'if you expect that an error could happen, but can not avoid it
'then you can enclose the code within a Try...Catch...End Try statement
'that will 'catch / handle' the error, allowing your code to continue, if needed
'the Catch side can be used for feedback, and/or to try an alternate code

Try
	' Set dimensions based on part OD
	If dPartOD > 0 And dPartOD <= 2.25 Then
		size = 3.5
		sizec = 3.5
		sizeb = 3.5
		sizet = 3.5
	ElseIf dPartOD > 2.25 And dPartOD <= 3.5 Then
		size = 4.75
		sizec = 4.75
		sizeb = 4.75
		sizet = 4.75
	ElseIf dPartOD > 3.5 And dPartOD <= 4.5 Then
		size = 6.0
		sizec = 6.0
		sizeb = 6.0
		sizet = 6.0
	ElseIf dPartOD > 4.5 And dPartOD <= 6.25 Then
		size = 7.75
		sizec = 7.75
		sizeb = 7.75
		sizet = 7.75
	ElseIf dPartOD > 6.25 And dPartOD <= 8.0 Then
		size = 9.75
		sizec = 9.75
		sizeb = 9.75
		sizet = 9.75
	ElseIf dPartOD > 10 And dPartOD <= 12 Then
		size = 14.0
		sizec = 14.0
		sizeb = 14.0
		sizet = 14.0
	End If
	Logger.Info("Checked 'PartOD' and set 'size' param value OK.")
Catch ex As Exception
	MsgBox("Error while checking 'PartOD' or setting value of 'size' parameter." _
	& vbCrLf & ex.Message, vbExclamation, "iLogic")
	Return
End Try

'apply any changes made to local variables representing parameters
'to the model document at this point
RuleParametersOutput()


Try
	' Set other dimensions based on the sizes
	If size = 3.5 Then
		zdiameter = 3.245
		zdiameterc = 3.245
		zdiameterb = 3.245
		zdiametert = 3.245
		maxpindiameter = 1.125
		maxpindiameterc = 1.125
		maxpindiameterb = 1.125
		maxpindiametert = 1.125
	ElseIf size = 4.75 Then
		zdiameter = 4.495
		zdiameterc = 4.495
		zdiameterb = 4.495
		zdiametert = 4.495
		maxpindiameter = 1.125
		maxpindiameterc = 1.125
		maxpindiameterb = 1.125
		maxpindiametert = 1.125
	ElseIf size = 6.0 Then
		zdiameter = 5.745
		zdiameterc = 5.745
		zdiameterb = 5.745
		zdiametert = 5.745
		maxpindiameter = 1.5
		maxpindiameterc = 1.5
		maxpindiameterb = 1.5
		maxpindiametert = 1.5
	ElseIf size = 7.75 Then
		zdiameter = 7.495
		zdiameterc = 7.495
		zdiameterb = 7.495
		zdiametert = 7.495
		maxpindiameter = 1.75
		maxpindiameterc = 1.75
		maxpindiameterb = 1.75
		maxpindiametert = 1.75
	ElseIf size = 9.75 Then
		zdiameter = 9.495
		zdiameterc = 9.495
		zdiameterb = 9.495
		zdiametert = 9.495
		maxpindiameter = 1.875
		maxpindiameterc = 1.875
		maxpindiameterb = 1.875
		maxpindiametert = 1.875
	ElseIf size = 14.0 Then
		zdiameter = 13.745
		zdiameterc = 13.745
		zdiameterb = 13.745
		zdiametert = 13.745
		maxpindiameter = 2.75
		maxpindiameterc = 2.75
		maxpindiameterb = 2.75
		maxpindiametert = 2.75
	End If
	Logger.Info("Checked 'size' and set value of 'zdiameter'/'maxpindiameter' params OK.")
Catch ex As Exception
	MsgBox("Error checking 'size' or setting value of 'zdiameter'/'maxpindiameter'." _
	& vbCrLf & ex.Message, vbExclamation, "iLogic")
	Return
End Try

'apply any changes made to local variables representing parameters
'to the model document at this point
RuleParametersOutput()

'get access to the document
Dim oDoc As Inventor.Document = ThisDoc.Document
'get access to its parameters
Dim oParams As Inventor.Parameters = Nothing
If (TypeOf oDoc Is AssemblyDocument) OrElse (TypeOf oDoc Is PartDocument) Then
	oParams = oDoc.ComponentDefinition.Parameters
ElseIf (TypeOf oDoc Is DrawingDocument) Then
	oParams = oDoc.Parameters
End If
If oParams Is Nothing Then Return 'exit rule, parameters collection could not be obtained

'if any of these parameters are not found, it will throw an error
'if the document is ReadOnly for some reason, then trying to change parameter values will throw error
'if the file is in the Content Center or a library folder, it will act like ReadOnly
Try
	oParams.Item("size").Value = size
	oParams.Item("sizec").Value = sizec
	oParams.Item("sizeb").Value = sizeb
	oParams.Item("sizet").Value = sizet
	oParams.Item("zdiameter").Value = zdiameter
	oParams.Item("zdiameterc").Value = zdiameterc
	oParams.Item("zdiameterb").Value = zdiameterb
	oParams.Item("zdiametert").Value = zdiametert
	oParams.Item("maxindiameter").Value = maxpindiameter
	oParams.Item("maxpindiameterc").Value = maxpindiameterc
	oParams.Item("maxpindiameterb").Value = maxpindiameterb
	oParams.Item("maxpinDiametert").Value = maxpindiametert
	Logger.Info("Set param values OK.")
Catch ex As Exception
	MsgBox("Error setting parameter values." & vbCrLf & _
	ex.Message, vbExclamation, "iLogic")
	Return
End Try

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)