Ilogic Warning

Ilogic Warning

corey_danielson
Advocate Advocate
502 Views
8 Replies
Message 1 of 9

Ilogic Warning

corey_danielson
Advocate
Advocate

The code I have works perfectly for all my sizes. Then I added 3/8 & 1/2. Is this something to do with no number in front, less than an inch?  Bolt_Length is my parameter. It works for 1 1/2", 2 1/2" etc. , but not 1/2" or 3/8"

 

 

oLength = BOLT_LENGTH
If oLength.Contains("/") Then
sArray = Split(BOLT_LENGTH, " ")
Dim Int As Integer = sArray(0)
Dim Fraction As String = sArray(1)
sFractionArray = Split(Fraction, "/")
Dim Dbl As Double = sFractionArray(0) / sFractionArray(1)
oLength = Int + Dbl '
End If

____________________________________________________________

 

This is the error I get; it is converting any fractional dimension greater than 1" just fine, no errors. 

Error in rule: HEX HEAD BOLT, in document: HEX HEAD BOLT STD.ipt

Conversion from string "3/8" to type 'Integer' is not valid.

 

Is there a simple fix to the code that would work?

 

Thank you

 

0 Likes
503 Views
8 Replies
Replies (8)
Message 2 of 9

ryan.rittenhouse
Advocate
Advocate

Your code as written splits on a space (" "), so if you give a fraction less than 1 with no space, that's where it's going off the rails. Change it to account for the possibility of not having a space in it and it should work. Something like this:

oLength = BOLT_LENGTH
If oLength.Contains("/") Then
	Dim wholeNumber As Integer = 0
	Dim fraction As String
	
	If BOLT_LENGTH.Contains(" ") Then
		sArray = Split(BOLT_LENGTH, " ")
		wholeNumber = CInt(sArray(0))
		fraction = sArray(1)
	Else
		fraction = BOLT_LENGTH
	End If
	
	sFractionArray = Split(fraction, "/")
	Dim fractionAsDecimal As Double = sFractionArray(0) / sFractionArray(1)
	oLength = wholeNumber + fractionAsDecimal
End If
If this solved your problem, or answered your question, please click Accept Solution.
0 Likes
Message 3 of 9

WCrihfield
Mentor
Mentor

Hi @corey_danielson.  I'm not sure what you intentions were with that block of code, but it looks like you are just converting a String containing a fraction into a Double type value.  If that is the case, then there is a much easier way to do it.  You can use the Inventor.UnitsOfMeasure object, which can be obtained from any Document object (Document.UnitsOfMeasure), or directly from the main Application object (Application.UnitsOfMeasure).  Only downside is units conversion may be necessary, due to 'database units' always being centimeters.  See example below:

Dim UOM As Inventor.UnitsOfMeasure = ThisApplication.UnitsOfMeasure
Dim sFraction As String = "3 7/16 in" 'with or without "in"
Dim dDouble As Double = UOM.GetValueFromExpression(sFraction, "in")
dDouble = UOM.ConvertUnits(dDouble, "cm", "in")
MsgBox("Fraction = " & sFraction & vbCrLf & _
"Double = " & dDouble.ToString, vbInformation, "Fraction String To Double")

If this solved your problem, or answered your question, please click ACCEPT SOLUTION .
Or, if this helped you, please click (LIKE or KUDOS) 👍.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 4 of 9

corey_danielson
Advocate
Advocate
I have tried these solutions; I am still not having any luck. I am getting the warning that 
Error on Line 37 : 'LENGTH' is not declared. It may be inaccessible due to its protection level.

I have all my bolts in here, I commented out all the rest of the code, just trying to get the 1/4-20 to work



'Dim UOM As Inventor.UnitsOfMeasure = ThisApplication.UnitsOfMeasure
'Dim sFraction As String = "3 7/16 in" 'with or without "in" 'Dim dDouble As Double = UOM.GetValueFromExpression(sFraction, "in") 'dDouble = UOM.ConvertUnits(dDouble, "cm", "in") '''MsgBox("Fraction = " & sFraction & vbCrLf & _ ''"Double = " & dDouble.ToString, vbInformation, "Fraction String To Double") '============================================================================= 'oLength = BOLT_LENGTH ' If oLength.Contains("/") Then ' sArray = Split(BOLT_LENGTH, " ") ' Dim wholeNumber As Integer = 0 = sArray(0) ' Dim Fraction As String = sArray(1) ' sFractionArray = Split(Fraction, "/") ' Dim fractionAsDecimal As Double = sFractionArray(0) / sFractionArray(1) ' oLength = wholeNumber + fractionAsDecimal ' End If '============================================================================= oLength = LENGTH If oLength.Contains("/") Then Dim wholeNumber As Integer = 0 Dim fraction As String If LENGTH.Contains(" ") Then sArray = Split(LENGTH, " ") wholeNumber = CInt(sArray(0)) fraction = sArray(1) Else fraction = LENGTH End If sFractionArray = Split(fraction, "/") Dim fractionAsDecimal As Double = sFractionArray(0) / sFractionArray(1) oLength = wholeNumber + fractionAsDecimal End If '======================================= BOLT GRADE =========================================== MultiValue.SetList("GRADE", "GR5", "GR8") If Parameter("GRADE") = "GR5" Then SLOTPATTERN = 3 ElseIf Parameter("GRADE") = "GR8" Then SLOTPATTERN = 5 End If '==================================================================================== 'LENGTH = oLength '=========================================== 1/4-20 PARTIAL THREAD ============================ If DescriptionBolt = "HEX BOLT 1/4-20" And Parameter("THREAD") = "PARTIAL" Then THREADLENGTH = .75 SLOTPATTERN = SLOTPATTERN HEADTHICKNESS = .15625 DIAMETER = .25 HEX = .4375 boltpartnumber = "HB-PT" & ".25-20" & "X" & (LENGTH) & (GRADE) BOLT_DESCRIPTION = DescriptionBolt & " X " & LENGTH & " " & "LONG" & " " & GRADE & " " & Color SLOT = .09375 RADIUS = .04 VERTICAL_RADIUS = .04 FROM_CENTER = .0625 Feature.IsActive("Thread3") = True Feature.ThreadDesignation("Thread3") = "1/4-20 UNC" If LENGTH < 1 Then MessageBox.Show("HEX BOLT 1/4-20 must be 1 inch or greater.Changing to 1 inch", "AO Parameter Police") BOLT_LENGTH = 1 Else If LENGTH >= 1 Then BOLT_LENGTH = LENGTH End If End If

 

0 Likes
Message 5 of 9

WCrihfield
Mentor
Mentor

Hi @corey_danielson.  If the "LENGTH" being used in your rule is supposed to be representing a real Parameter in your document, then can you show us an image of it in the Parameters dialog.  It looks like you are attempting to use it in your code in two different ways (as a String, and as a Double), but it can not be used both ways.  We need to figure out if its Units (in the Parameters dialog, in the Units column) is set to "Text", or "in".

For example, look at the following screenshot image from the Parameters dialog:

WCrihfield_1-1731937724769.png

The Parameter named "NumParam1" has its 'Units/Type' set to "in" (inches), so it will have a numerical value, as seen in the 'Nominal Value' column.

The Parameter named "TextParam1" has its 'Units/Type' set to "Text", so it is just a String (text), and does not actually have a numerical value in the 'Nominal Value' column.

Which 'Units/Type' is your Parameter named "LENGTH' set to?  ...Or is it not a 'real' parameter, and only a local variable within the rule?

If it is set to "in" in the 'Units/Type' column, then it will have a true numerical value, and you can not use:

oLength = LENGTH
If oLength.Contains("/") Then

...because that .Contains() method is used on a String or a collection of some sort, not on a Double.  And you can not do 'math' operations directly with a String, and can not check if a String is LessThan (<) or GreaterThan (>) a numerical value (Double).

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 6 of 9

corey_danielson
Advocate
Advocate
Looks like length is set up correctly. Do you see something wrong?

'=============================================== For CHANGING Decimal Into FRACTIONS ===========
oLength = LENGTH If oLength.Contains("/") Then Dim wholeNumber As Integer = 0 Dim fraction As String If oLength.Contains(" ") Then sArray = Split(oLength, " ") wholeNumber = CInt(sArray(0)) fraction = sArray(1) Else fraction = oLength End If sFractionArray = Split(fraction, "/") Dim fractionAsDecimal As Double = sFractionArray(0) / sFractionArray(1) oLength = wholeNumber + fractionAsDecimal End If '======================================= BOLT GRADE =========================================== MultiValue.SetList("GRADE", "GR5", "GR8") If Parameter("GRADE") = "GR5" Then SLOTPATTERN = 3 Else If Parameter("GRADE") = "GR8" Then SLOTPATTERN = 5 End If '=========================================== 1/4-20 PARTIAL THREAD ============================ If DescriptionBolt = "HEX BOLT 1/4-20" And Parameter("THREAD") = "PARTIAL" Then LENGTH = oLength THREADLENGTH = .75 SLOTPATTERN = SLOTPATTERN HEADTHICKNESS = .15625 DIAMETER = .25 HEX = .4375 boltpartnumber = "HB-PT" & ".25-20" & "X" & (LENGTH) & (GRADE) BOLT_DESCRIPTION = DescriptionBolt & " X " & LENGTH & " " & "LONG" & " " & GRADE & " " & Color SLOT = .09375 RADIUS = .04 VERTICAL_RADIUS = .04 FROM_CENTER = .0625 Feature.IsActive("Thread3") = True Feature.ThreadDesignation("Thread3") = "1/4-20 UNC" If oLength < 1 Then MessageBox.Show("HEX BOLT 1/4-20 must be 1 inch or greater.Changing to 1 inch", "AO Parameter Police") LENGTH = 1 Else If oLength >= 1 Then LENGTH = LENGTH End If End If '========================================== 1/4-20 FULL THREAD ================================ If DescriptionBolt = "HEX BOLT 1/4-20" And Parameter("THREAD") = "FULL" Then LENGTH = oLength THREADLENGTH = LENGTH SLOTPATTERN = SLOTPATTERN HEADTHICKNESS = .15625 DIAMETER = .25 HEX = .4375 boltpartnumber = "HB-FT" & ".25-20" & "X" & (LENGTH) & (GRADE) BOLT_DESCRIPTION = DescriptionBolt & " X " & LENGTH & " " & "LONG" & " " & GRADE & " " & Color SLOT = .09375 RADIUS = .04 VERTICAL_RADIUS = .04 FROM_CENTER = .0625 Feature.IsActive("Thread3") = True Feature.ThreadDesignation("Thread3") = "1/4-20 UNC" If oLength < 1 Then MessageBox.Show("HEX BOLT 1/4-20 must be 1 inch or greater.Changing to 1 inch", "AO Parameter Police") LENGTH = 1 Else If oLength >= 1 Then LENGTH = LENGTH End If End If
0 Likes
Message 7 of 9

WCrihfield
Mentor
Mentor

OK.  The one picture shows that the Units of "LENGTH" is "in", so it has a numeric value, and represents a Double (not a String).

If that is the case, your whole first block of code will cause errors, because it starts with this line of code:

oLength = LENGTH

...because your 'oLength' variable has not been 'declared' (using Dim keyword), and you have not specified its Type, and you are setting its value from 'LENGTH' (a Parameter with Double type value).  That line of code is setting the Type of your 'oLength' variable to be Double, the same Type as 'LENGTH' parameter's value.

 

Then trying to use 'oLength.Containts()' method will throw an error, because it is not available for a Double Type variable, only for a String type variable, or a variable representing certain Types of collections.  A Double is a pure numeric value, with no 'characters' or 'text' in it.

Even if you specifically declared a variable like:

Dim sLengthString As String = CStr(LENGTH)

...the 'sLengthString' variable would still only contain the 'decimal' representation of its value, not the original fraction text, so it still will not have the "/" character in it.  Only the Parameter.Expression property of a real Parameter Type object will have a String type value containing the 'fraction text'.  When you have one of those blue, unquoted parameter names in an internal iLogic rule, that does not represent a 'real' Parameter object directly, but a its Value directly.  And when it is a numeric parameter, it is usually understood as a DoubleForEquals Type of value (very similar to Double, but treated as if it has fewer decimal places, for easier value comparisons).

 

I can't just completely copy your posted code, then paste it locally into a rule, then fix it for you, then paste it back, because on my end, all those blue, unquoted parameter names will be meaningless, and can even cause other errors, such as the 'Hex' term being misunderstood for the Conversion.Hex() method, but without any 'inputs'.

If you really need to get the fraction text of that parameter, then you can try doing that like this instead:

Dim sLengthFraction As String = Parameter.Param("LENGTH").Expression

Then you can use your 'Contains()' methods on that 'sLengthFraction' variable, if needed.  All the other places later in your code, where you are doing 'Math' operations with the oLength variable, or the 'LENGTH' directly, can stay the same.  But where you are using Contains method, that needs to originate from the String Type variable representing the actual fraction.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 8 of 9

corey_danielson
Advocate
Advocate

Thank you for looking into this. I feel I am missing something minor. I will include the warning I get now, and some code. 

 

Dim sLengthFraction As Double = Parameter.Param(LENGTH).Expression
oLength = LENGTH
If oLength.Contains(" 1 ") Then
	Dim wholeNumber As Integer = .0
	Dim fraction As String
	If oLength.Contains("  ") Then
		sArray = Split(oLength, "  ")
		wholeNumber = CInt(sArray(0))
		fraction = sArray(1)
	Else
		fraction = oLength
	End If
	sFractionArray = Split(fraction, " / ")
	Dim fractionAsDecimal As Double = sFractionArray(0) / sFractionArray(1)
	oLength = wholeNumber + fractionAsDecimal

End If

'======================================= BOLT GRADE ===========================================

MultiValue.SetList("GRADE", "GR5", "GR8")

If Parameter("GRADE") = "GR5" Then
	SLOTPATTERN = 3
Else If Parameter("GRADE") = "GR8" Then
	SLOTPATTERN = 5
End If

'=========================================== 1/4-20 PARTIAL THREAD ============================

If DescriptionBolt = "HEX BOLT 1/4-20" And
	Parameter("THREAD") = "PARTIAL" Then
	LENGTH = oLength
	THREADLENGTH = .75
	SLOTPATTERN = SLOTPATTERN
	HEADTHICKNESS = .15625
	DIAMETER = .25
	HEX = .4375
	boltpartnumber = "HB-PT" & ".25-20" & "X" & (LENGTH) & (GRADE)
	BOLT_DESCRIPTION = DescriptionBolt & " X " & LENGTH & " " & "LONG" & " " & GRADE & " " & Color
	SLOT = .09375
	RADIUS = .04
	VERTICAL_RADIUS = .04
	FROM_CENTER = .0625
	Feature.IsActive("Thread3") = True
	Feature.ThreadDesignation("Thread3") = "1/4-20 UNC"
If oLength < 1 Then
	MessageBox.Show("HEX BOLT 1/4-20 must be 1 inch or greater.Changing to 1 inch", "AO Parameter Police")
	LENGTH = 1
Else If oLength >= 1 Then
	LENGTH = LENGTH
End If
End If

'==========================================  1/4-20 FULL THREAD  ================================

If DescriptionBolt = "HEX BOLT 1/4-20" And
	Parameter("THREAD") = "FULL" Then
	LENGTH = oLength
	THREADLENGTH = LENGTH
	SLOTPATTERN = SLOTPATTERN
	HEADTHICKNESS = .15625
	DIAMETER = .25
	HEX = .4375
	boltpartnumber = "HB-FT" & ".25-20" & "X" & (LENGTH) & (GRADE)
	BOLT_DESCRIPTION = DescriptionBolt & " X " & LENGTH & " " & "LONG" & " " & GRADE & " " & Color
	SLOT = .09375
	RADIUS = .04
	VERTICAL_RADIUS = .04
	FROM_CENTER = .0625
	Feature.IsActive("Thread3") = True
	Feature.ThreadDesignation("Thread3") = "1/4-20 UNC"
If oLength < 1 Then
	MessageBox.Show("HEX BOLT 1/4-20 must be 1 inch or greater.Changing to 1 inch", "AO Parameter Police")
	LENGTH = 1
	Else If oLength >= 1 Then
	LENGTH = LENGTH
End If
End If

'=========================================  1/4-28 PARTIAL THREAD  =============================

If DescriptionBolt = "HEX BOLT 1/4-28" And
	Parameter("THREAD") = "PARTIAL" Then
	LENGTH = oLength
	THREADLENGTH = .75
	SLOTPATTERN = SLOTPATTERN
	HEADTHICKNESS = .15625
	DIAMETER = .25
	HEX = .4375
	boltpartnumber = "HB-PT" & ".25-28" & "X" & (LENGTH) & (GRADE)
	BOLT_DESCRIPTION = DescriptionBolt & " X " & LENGTH & " " & "LONG" & " " & GRADE & " " & Color
	SLOT = .09375
	RADIUS = .04
	VERTICAL_RADIUS = .04
	FROM_CENTER = .0625
	Feature.IsActive("Thread3") = True
	Feature.ThreadDesignation("Thread3") = "1/4-28 UNF"
If oLength < 1 Then
	MessageBox.Show("HEX BOLT 1/4-28 must be 1 inch or greater.Changing to 1 inch", "AO Parameter Police")
	LENGTH = 1
	Else If oLength >= 1 Then
	LENGTH = oLength
End If
End If

'==========================================  1/4-28 FULL THREAD  ============================

If DescriptionBolt = "HEX BOLT 1/4-28" And
	Parameter("THREAD") = "FULL" Then
	LENGTH = oLength
	THREADLENGTH = LENGTH
	SLOTPATTERN = SLOTPATTERN
	HEADTHICKNESS = .15625
	DIAMETER = .25
	HEX = .4375
	boltpartnumber = "HB-FT" & ".25-28" & "X" & (LENGTH) & (GRADE)
	BOLT_DESCRIPTION = DescriptionBolt & " X " & LENGTH & " " & "LONG" & " " & GRADE & " " & Color
	SLOT = .09375
	RADIUS = .04
	VERTICAL_RADIUS = .04
	FROM_CENTER = .0625
	Feature.IsActive("Thread3") = True
	Feature.ThreadDesignation("Thread3") = "1/4-28 UNF"
If oLength < 1 Then
	MessageBox.Show("HEX BOLT 1/4-28 must be 1 inch or greater.Changing to 1 inch", "AO Parameter Police")
	LENGTH = 1
	Else If oLength >= 1 Then
	LENGTH = oLength
End If
End If



This is the last part of my code:
	iProperties.Value("Project", "Description") = BOLT_DESCRIPTION
	iProperties.Value("Project", "Part Number") = boltpartnumber
	
	RuleParametersOutput()
	
	trigger = iTrigger0

iLogicVb.UpdateWhenDone = True


 

0 Likes
Message 9 of 9

WCrihfield
Mentor
Mentor

Please notice the difference between your first line of code here, and the last line of code I posted in my last reply.  I had the name of the parameter within quotation marks, because it is the 'name' of the Parameter, not the 'Value' of the parameter, that we need to be specifying there.  Your first line of code should be edited to add the quotation marks around the name of the Parameter within the () symbols, just like my sample line of code has them.  That line of code is getting the actual Parameter object itself, by its name, then getting the value of its Expression property, which is always a String, and will always be exactly the same as what you see in the 'Equation' column of the Parameters dialog for that specific Parameter.

 

So, even if you had a parameter with no fraction text and no equation that needs to be evaluated to get to a numerical value, but just a simple numerical value (like "1.25 in") in the 'equation' column of a Parameter, that Parameter.Expression value (a String) will still contain not only the numerical digits portion, but also the space after them, then the units specifier text ("in") after that space.  Because of that, it can cause errors if you try to directly convert the String "1.25 in" to a Double.  The space and units specifier text do not directly translate into numerical digits, so the Type converter does not know what to do with them, and will throw an error.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes