Community
Inventor Programming - iLogic, Macros, AddIns & Apprentice
Inventor iLogic, Macros, AddIns & Apprentice Forum. Share your knowledge, ask questions, and explore popular Inventor topics related to programming, creating add-ins, macros, working with the API or creating iLogic tools.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

New to Inventor

3 REPLIES 3
Reply
Message 1 of 4
jeslinakoruth
114 Views, 3 Replies

New to Inventor

My code either gives me catastrophic error or says my values are not double. Any tips?

 

' Prompt the user for the part OD
Dim partOD As Double
partOD = CDbl(InputBox("Enter the Part OD:", "Part OD Input"))

' Set dimensions based on part OD
If partOD > 0 And partOD <= 2.25 Then
    size = 3.5
    sizec = 3.5
    sizeb = 3.5
    sizet = 3.5
ElseIf partOD > 2.25 And partOD <= 3.5 Then
    size = 4.75
    sizec = 4.75
    sizeb = 4.75
    sizet = 4.75
ElseIf partOD > 3.5 And partOD <= 4.5 Then
    size = 6.0
    sizec = 6.0
    sizeb = 6.0
    sizet = 6.0
ElseIf partOD > 4.5 And partOD <= 6.25 Then
    size = 7.75
    sizec = 7.75
    sizeb = 7.75
    sizet = 7.75
ElseIf partOD > 6.25 And partOD <= 8.0 Then
    size = 9.75
    sizec = 9.75
    sizeb = 9.75
    sizet = 9.75
ElseIf partOD > 10 And partOD <= 12 Then
    size = 14.0
    sizec = 14.0
    sizeb = 14.0
    sizet = 14.0
End If

' 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
ThisApplication.ActiveDocument.ComponentDefinition.Parameters.Item("size").Value = size 
ThisApplication.ActiveDocument.ComponentDefinition.Parameters.Item("sizec").Value = sizec 
ThisApplication.ActiveDocument.ComponentDefinition.Parameters.Item("sizeb").Value = sizeb 
ThisApplication.ActiveDocument.ComponentDefinition.Parameters.Item("sizet").Value = sizet 
ThisApplication.ActiveDocument.ComponentDefinition.Parameters.Item("zdiameter").Value = zdiameter 
ThisApplication.ActiveDocument.ComponentDefinition.Parameters.Item("zdiameterc").Value = zdiameterc 
ThisApplication.ActiveDocument.ComponentDefinition.Parameters.Item("zdiameterb").Value = zdiameterb 
ThisApplication.ActiveDocument.ComponentDefinition.Parameters.Item("zdiametert").Value = zdiametert 
ThisApplication.ActiveDocument.ComponentDefinition.Parameters.Item("maxindiameter").Value = maxpindiameter 
ThisApplication.ActiveDocument.ComponentDefinition.Parameters.Item("maxpindiameterc").Value = maxpindiameterc 
ThisApplication.ActiveDocument.ComponentDefinition.Parameters.Item("maxpindiameterb").Value = maxpindiameterb 
ThisApplication.ActiveDocument.ComponentDefinition.Parameters.Item("maxpinDiametert").Value = maxpindiametert 
 
3 REPLIES 3
Message 2 of 4
WCrihfield
in reply to: jeslinakoruth

Hi @jeslinakoruth.  It is difficult to diagnose problems with your code without being familiar with what all the custom terms being used within it represent.  Since the code is just black & white text in a code window (the way we usually like to see it) we can not tell if some of them may be blue, unquoted names of parameters, and directly recognized as representing local parameters...or just terms being used as local variables within the code, and do not actually directly represent parameters.  We also do not know what types of parameters they are, or what types of values they may have.  At first, working with parameters can be a bit tricky, due to the differences in units and automatic (or not) unit conversions that happen.

 

Is 'partOD' only a local variable, as it looks like, or is it also the exact spelling and capitalization of a parameter in the same model that the iLogic rule is saved within?  Same for 'size', 'sizec', 'sizeb', 'sizet', 'zdiameter', zdiameterc' , 'zdiameterb', 'zdiametert', and so on.  If any of those terms are just local variables, then they should be 'declared' (using the Dim keyword), and should have their Type specified where they are declared, such as:

Dim size As Double

...if that is just a local variable, and not a blue, unquoted parameter name.  Also, keep in mind that when working with blue, unquoted parameter names, or when working with the Parameter("ParamName") type iLogic snippets, the 'value' will be in 'document units' (the value specified within the Document Settings as the default length/distance unit), not necessarily in the parameter's own units (if they are different units).  However, when working accessing the actual Parameter object by Inventor API code (such as what you are doing in the last 12 or so lines of code), then the value will always be in 'database units' (always centimeters for length/distance, no matter what the document units or parameter units are).

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 3 of 4
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)

Message 4 of 4


@jeslinakoruth wrote:

My code either gives me catastrophic error or says my values are not double. Any tips?


That go nothing to do with being new in Inventor.

That's programmer error.

VB complaining about value not double means you assigning wrong variables.

 

What's the "catastrophic error"?

Which values are not double?

 

My crystal ball having trouble connecting to your computer.

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Technology Administrators


Autodesk Design & Make Report