iLogic form to enter only Upper and lower limit of a dmension

iLogic form to enter only Upper and lower limit of a dmension

sbalasubramanianJJDFU
Enthusiast Enthusiast
682 Views
9 Replies
Message 1 of 10

iLogic form to enter only Upper and lower limit of a dmension

sbalasubramanianJJDFU
Enthusiast
Enthusiast

Hello,

 

I am trying to create an iLogic form to just to enter upper and lower limit of a dimension in a sketch and let inventor automatically generate the nominal value based on this. Can someone guide me in the right direction? It doesnt necessarily be using iLogic, open to any alternative.

Attached screenshot. Thanks in advance.

0 Likes
Accepted solutions (1)
683 Views
9 Replies
Replies (9)
Message 2 of 10

j_weber
Mentor
Mentor

Hi, 

I see a problem with an iLogic form because you can only set values using user-defined properties.

 

So first, you must create properties, one upper and one lower tolerance, and then write them to the properties. 

Perhaps a VBA form is a better way. There, it isn't necessary to write properties. 

 

 




Jörg Weber
CAD Systemtechniker für AutoCAD, Inventor, Vault





0 Likes
Message 3 of 10

sbalasubramanianJJDFU
Enthusiast
Enthusiast

I wrote this following iLogic code, but its rounding off the value and not changing tolerance type to -limits stacked 

Hope someone can help

'

' Prompt the user to select a dimension
Dim oDoc As PartDocument = ThisApplication.ActiveDocument
Dim oCompDef As ComponentDefinition = oDoc.ComponentDefinition

' Use a general selection method to pick a parameter
Dim oParameter As Parameter
Try
' Pick a parameter from the model
oParameter = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kAllEntitiesFilter, "Select a parameter")
Catch ex As Exception
MsgBox("Selection was canceled or invalid. Please select a valid parameter.")
Exit Sub
End Try

' Prompt user to enter upper and lower limits, with error handling for decimal input
Dim upperLimitStr As String = InputBox("Enter the upper limit for the dimension:", "Upper Limit")
Dim lowerLimitStr As String = InputBox("Enter the lower limit for the dimension:", "Lower Limit")

Dim upperLimit As Double
Dim lowerLimit As Double

' Convert string inputs to double, ensuring proper decimal handling
If Not Double.TryParse(upperLimitStr, System.Globalization.NumberStyles.Any, System.Globalization.CultureInfo.InvariantCulture, upperLimit) Then
MsgBox("Invalid input for upper limit. Please enter a valid decimal number.", vbOKOnly + vbExclamation)
Exit Sub
End If

If Not Double.TryParse(lowerLimitStr, System.Globalization.NumberStyles.Any, System.Globalization.CultureInfo.InvariantCulture, lowerLimit) Then
MsgBox("Invalid input for lower limit. Please enter a valid decimal number.", vbOKOnly + vbExclamation)
Exit Sub
End If

' Calculate the median value without rounding
Dim medianValue As Double = (upperLimit + lowerLimit) / 2

' Set the parameter to the median value
oParameter.Value = medianValue

' Inform the user of the new dimension value with six decimal places precision
MsgBox("The nominal dimension has been set to the median value: " & Format(medianValue, "0.######"))

0 Likes
Message 4 of 10

WCrihfield
Mentor
Mentor

Hi @sbalasubramanianJJDFU.  I'm not sure how you will 'Pick' a parameter object, but you might be able to pick a dimensional sketch constraint (DimensionConstraint), then get the parameter associated with that constraint.  Once you have a reference to that parameter object though, you would need to include some additional lines of code near the end of your rule.  I will include some links below to the Inventor API objects involved, to help you along.

Parameter (object)

Parameter.Tolerance (property)

Tolerance (object)

Tolerance.SetToLimits (method)

ToleranceTypeEnum (Enum needed for SetToLimits method)

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 5 of 10

jnowel
Advocate
Advocate
Accepted solution

Following what @WCrihfield mentioned above.
Below code should work as you intended.

 

Sub Main

	' Prompt the user to select a dimension
	Dim oDoc As PartDocument = ThisApplication.ActiveDocument
	Dim oCompDef As ComponentDefinition = oDoc.ComponentDefinition
	Dim oParameter As Inventor.Parameter
	
	Try
		' Pick a dimension constraint from the model
		Dim oDimConstraint As DimensionConstraint
		oDimConstraint = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kSketchDimConstraintFilter, "Select a Dimension Constraint")

		If oDimConstraint.Driven = True Then
			MsgBox("Selection was canceled or invalid. Please select a valid (non-driven) Dimension Constraint.")
			Exit Sub
		End If
		
		oParameter = oDimConstraint.Parameter
			
	Catch ex As Exception
		MsgBox("Selection was canceled or invalid. Please select a valid (non-driven) Dimension Constraint.")
		Exit Sub
	End Try
	
	
	' Prompt user to enter upper and lower limits, with error handling for decimal input
	Dim upperLimitStr As String = InputBox("Enter the upper limit for the dimension:", "Upper Limit")
	Dim lowerLimitStr As String = InputBox("Enter the lower limit for the dimension:", "Lower Limit")
	
	Dim upperLimit As Double
	Dim lowerLimit As Double
	
	' Convert string inputs to double, ensuring proper decimal handling
	If Not Double.TryParse(upperLimitStr, System.Globalization.NumberStyles.Any, System.Globalization.CultureInfo.InvariantCulture, upperLimit) Then
		MsgBox("Invalid input for upper limit. Please enter a valid decimal number.", vbOKOnly + vbExclamation)
		Exit Sub
	End If
	
	If Not Double.TryParse(lowerLimitStr, System.Globalization.NumberStyles.Any, System.Globalization.CultureInfo.InvariantCulture, lowerLimit) Then
		MsgBox("Invalid input for lower limit. Please enter a valid decimal number.", vbOKOnly + vbExclamation)
		Exit Sub
	End If
	
	' Calculate the median value without rounding
	'this assumes the entereed values in the previous input are of the same unit as per parameter.units
	Dim medianValue As Double = (upperLimit + lowerLimit) / 2
	
	' Set the parameter to the median value
	oParameter.Expression = medianValue & oParameter.Units
	oParameter.Tolerance.SetToLimits(kLimitsStackedTolerance, (upperLimit - medianValue) & oParameter.Units, (lowerLimit - medianValue) & oParameter.Units)
	
	' Inform the user of the new dimension value with six decimal places precision
	MsgBox("The nominal dimension has been set to the median value: " & Round(medianValue,5) & oParameter.Units)
	
	oDoc.Update

End Sub
0 Likes
Message 6 of 10

sbalasubramanianJJDFU
Enthusiast
Enthusiast

Thanks.. perfect solution

0 Likes
Message 7 of 10

sbalasubramanianJJDFU
Enthusiast
Enthusiast

Hi @jnowel , just one small feedback, the dimension seems updating. theres some error. In the screen recorded video, we can see that upper limit 5.500 is entered and lower limit 5.499 , the median value shows correctly as 5.4995 but once executed,  its showing as 5.501/5.499 ( this is wrong) and also it is exiting the sketch mode, whereas I want to run that iLogic for other dimensions. Kindly help tweak a little. thanks!

 

0 Likes
Message 8 of 10

jnowel
Advocate
Advocate

Hi @sbalasubramanianJJDFU, can you share the file you are working on?
Have you edited the above code in some way as I cannot replicate the issue you are experiencing?

 

0 Likes
Message 9 of 10

sbalasubramanianJJDFU
Enthusiast
Enthusiast

Hi @jnowel , I found out the reason why this was off. This tolerance difference occurs when the document unit settings precision is set at 3 decimal places. When I change it to 4 or 5 decimal places, its working perfectly.  Hope you can tweak the code to set the precision based on user input decimal values instead of rounding off based on document settings to avoid this error. Thanks!

0 Likes
Message 10 of 10

jnowel
Advocate
Advocate

try this, updated the Parameter.Precision based on your input

Sub Main

	' Prompt the user to select a dimension
	Dim oDoc As PartDocument = ThisApplication.ActiveDocument
	Dim oCompDef As ComponentDefinition = oDoc.ComponentDefinition
	Dim oParameter As Inventor.Parameter
	
	
	Dim oDimConstraint As DimensionConstraint
	Try
		' Pick a dimension constraint from the model
		oDimConstraint = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kSketchDimConstraintFilter, "Select a Dimension Constraint")

		If oDimConstraint.Driven = True Then
			MsgBox("Selection was canceled or invalid. Please select a valid (non-driven) Dimension Constraint.")
			Exit Sub
		End If
		
		oParameter = oDimConstraint.Parameter
			
	Catch ex As Exception
		MsgBox("Selection was canceled or invalid. Please select a valid (non-driven) Dimension Constraint.")
		Exit Sub
	End Try
	
	
	' Prompt user to enter upper and lower limits, with error handling for decimal input
	Dim upperLimitStr As String = InputBox("Enter the upper limit for the dimension:", "Upper Limit")
	Dim lowerLimitStr As String = InputBox("Enter the lower limit for the dimension:", "Lower Limit")
	
	Dim upperLimit As Double
	Dim lowerLimit As Double
	
	' Convert string inputs to double, ensuring proper decimal handling
	If Not Double.TryParse(upperLimitStr, System.Globalization.NumberStyles.Any, System.Globalization.CultureInfo.InvariantCulture, upperLimit) Then
		MsgBox("Invalid input for upper limit. Please enter a valid decimal number.", vbOKOnly + vbExclamation)
		Exit Sub
	End If
	
	If Not Double.TryParse(lowerLimitStr, System.Globalization.NumberStyles.Any, System.Globalization.CultureInfo.InvariantCulture, lowerLimit) Then
		MsgBox("Invalid input for lower limit. Please enter a valid decimal number.", vbOKOnly + vbExclamation)
		Exit Sub
	End If
		
	'get max decimal places of input limits
	'assumes period "." is used as decimal point
	
	Dim sUpperDecimals As String
		Try
			sUpperDecimals = Split(CStr(upperLimit), ".")(1) 'this errors if you have whole number only
		Catch
			sUpperDecimals = String.Empty
		End Try
		
	Dim sLowerDecimals As String
		Try
			sLowerDecimals = Split(CStr(lowerLimit), ".")(1) 'this errors if you have whole number only
		Catch
			sLowerDecimals = String.Empty
		End Try
	
	Dim iDecPlaces As Integer = Math.Min(Math.Max(Len(sUpperDecimals), Len(sLowerDecimals)), 8) 'max 8 decimal places
	
	' Calculate the median value without rounding
	'this assumes the entereed values in the previous input are of the same unit as per parameter.units
	Dim medianValue As Double = (upperLimit + lowerLimit) / 2
	
	' Set the parameter to the median value
	oParameter.Expression = medianValue & oParameter.Units
	oParameter.Precision = Math.Max(iDecPlaces, 3) 'changes the parameter precision to max decimal places, minimum 3
	oParameter.Tolerance.SetToLimits(kLimitsStackedTolerance, (upperLimit - medianValue) & oParameter.Units, (lowerLimit - medianValue) & oParameter.Units)

	' Inform the user of the new dimension value with six decimal places precision
	MsgBox("The nominal dimension has been set to the median value: " & Round(medianValue,iDecPlaces+1) & " " & oParameter.Units) 'added space
	
	oDoc.Update

End Sub
0 Likes