Extracting overall length/width of an irregular shape of a plate using iLogic

Extracting overall length/width of an irregular shape of a plate using iLogic

eugene.morales
Enthusiast Enthusiast
2,014 Views
12 Replies
Message 1 of 13

Extracting overall length/width of an irregular shape of a plate using iLogic

eugene.morales
Enthusiast
Enthusiast

I've seen a lot of ilogic codes that can extract the extremities of a square/rectangular shape of a plate but if it's an  irregular shape the outcome is bigger. I've attached a sample file with the actual O/A size of 700mm x 1258.5, if I use one of the codes it will give me the wrong information about the part. I would like to ask the experts if they could help me with this matter.

0 Likes
Accepted solutions (1)
2,015 Views
12 Replies
Replies (12)
Message 2 of 13

SharkDesign
Mentor
Mentor

You're best off in the customisation forum.

 

https://forums.autodesk.com/t5/inventor-customization/bd-p/120

 

 

  Inventor Certified Professional
0 Likes
Message 3 of 13

WCrihfield
Mentor
Mentor

Hi @eugene.morales .

Here is something that can do what you want pretty accurately.  It takes advantage of the fairly new OrientedMinimumRangeBox property of the SurfaceBody object.  Of course if you are dealing with a flat plate, and all you wanted was 2D surface specifications, you could just inspect the Face, or something like that.

 

Sub Main
	Dim oPDoc As PartDocument = ThisDoc.Document
	Dim oPDef As PartComponentDefinition = oPDoc.ComponentDefinition
	Dim oOBox As OrientedBox = oPDef.SurfaceBodies.Item(1).OrientedMinimumRangeBox
	Dim oDim1 As Double = ConvertDBToMM(oOBox.DirectionOne.Length)
	Dim oDim2 As Double = ConvertDBToMM(oOBox.DirectionTwo.Length)
	Dim oDim3 As Double = ConvertDBToMM(oOBox.DirectionThree.Length)
		
	Dim oDims() As Double = {oDim1, oDim2, oDim3}
	Dim oThickness As Double = MinOfMany(oDims)
	Dim oLength As Double = MaxOfMany(oDims)
	Dim oWidth As Double
	'figure out which dim is left, or if two or more are equal
	Dim oWidthIdentified As Boolean
	For Each oDim In oDims
		If oDim <> oThickness And oDim <> oLength Then
			oWidth = oDim
			oWidthIdentified = True
			Exit For
		ElseIf oDim = oLength And oDim = oThickness Then
			'minimum dim and maximum dim are same, so middle dim will also be the same
			oWidth = oLength
			oWidthIdentified = True
		End If
	Next
	
	If oWidthIdentified Then
		MsgBox("Thickness = " & oThickness & vbCrLf & _
		"Width = " & oWidth & vbCrLf & _
		"Length = " & oLength, , "")
	Else
		MsgBox("Size = " & oDim1 & " x " & oDim2 & " x " & oDim3, , "")
	End If
End Sub

Function ConvertDBToMM(oInputValue As Double) As Double
	Return ThisApplication.UnitsOfMeasure.ConvertUnits(oInputValue, UnitsTypeEnum.kDatabaseLengthUnits, UnitsTypeEnum.kMillimeterLengthUnits)
End Function

 

 

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

If you want and have time, I would appreciate your Vote(s) for My IDEAS 💡or you can Explore My CONTRIBUTIONS

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 4 of 13

eugene.morales
Enthusiast
Enthusiast

Hi WCrihfield,

 

Thank you for that quick response with my query. It certainly work well but is there a way that I can create a custom parameter that I can use it to populate my BOM (Bill of Materials).

0 Likes
Message 5 of 13

WCrihfield
Mentor
Mentor

This edit should either create or update a text type user parameter named "Material Size" with the result size information.  You can change that parameter name if you want.  You may also want to limit the decimal places of accuracy or round the results before writing them to the parameter, to suit your preference.  If you only want width and length written to the parameter, it will usually do that.  But when the width variable isn't established, it may fall back to writing all 3 values to the parameter.  You could probably just add more checks in there to better determine which of the 3 are the middle value or if the width and length are the same, but different than the thickness.

Anyways, here's the updated iLogic code:

Sub Main
	Dim oPDoc As PartDocument = ThisDoc.Document
	Dim oPDef As PartComponentDefinition = oPDoc.ComponentDefinition
	Dim oOBox As OrientedBox = oPDef.SurfaceBodies.Item(1).OrientedMinimumRangeBox
	Dim oDim1 As Double = ConvertDBToMM(oOBox.DirectionOne.Length)
	Dim oDim2 As Double = ConvertDBToMM(oOBox.DirectionTwo.Length)
	Dim oDim3 As Double = ConvertDBToMM(oOBox.DirectionThree.Length)
		
	Dim oDims() As Double = {oDim1, oDim2, oDim3}
	Dim oThickness As Double = MinOfMany(oDims)
	Dim oLength As Double = MaxOfMany(oDims)
	Dim oWidth As Double
	'figure out which dim is left, or if two or more are equal
	Dim oWidthIdentified As Boolean
	For Each oDim In oDims
		If oDim <> oThickness And oDim <> oLength Then
			oWidth = oDim
			oWidthIdentified = True
			Exit For
		ElseIf oDim = oLength And oDim = oThickness Then
			'minimum dim and maximum dim are same, so middle dim will also be the same
			oWidth = oLength
			oWidthIdentified = True
		End If
	Next
	
	'write results to custom Parameter
	Dim oUParams As UserParameters = oPDef.Parameters.UserParameters
	Dim oUParam As UserParameter
	'get or create the user parameter
	Try
		oUParam = oUParams.Item("Material Size")
	Catch
		'it wasn't found, so create it, and just give it an empty (space) answer for now
		oUParam = oUParams.AddByValue("Material Size", " ", UnitsTypeEnum.kTextUnits)
	End Try
		
	If oWidthIdentified Then
		oUParam.Value = oWidth & " x " & oLength
'		MsgBox("Thickness = " & oThickness & vbCrLf & _
'		"Width = " & oWidth & vbCrLf & _
'		"Length = " & oLength, , "")
	Else
		oUParam.Value = oDim1 & " x " & oDim2 & " x " & oDim3
'		MsgBox("Size = " & oDim1 & " x " & oDim2 & " x " & oDim3, , "")
	End If
End Sub

Function ConvertDBToMM(oInputValue As Double) As Double
	Return ThisApplication.UnitsOfMeasure.ConvertUnits(oInputValue, UnitsTypeEnum.kDatabaseLengthUnits, UnitsTypeEnum.kMillimeterLengthUnits)
End Function

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

If you want and have time, I would appreciate your Vote(s) for My IDEAS 💡or you can Explore My CONTRIBUTIONS

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 6 of 13

eugene.morales
Enthusiast
Enthusiast
Hi WCrihfield,

I've copied and paste the rule into the part but I'm experiencing error saying "Unspecified error (Exception from HRESULT: 0x80004005 (E_FAIL)).
0 Likes
Message 7 of 13

rhasell
Advisor
Advisor

Hi

 

Remove the space in the User parameter of "Material Size"

There are two instances to change

 

Change it to "MaterialSize"

 

Screenshot 2021-07-15 122104.jpg

Reg
2026.1
Message 8 of 13

WCrihfield
Mentor
Mentor

Thanks for catching that little detail@rhasell.  That's the kind of stuff that happens when you get in too much of a hurry within multiple projects and skip retesting code after making changes.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 9 of 13

rhasell
Advisor
Advisor

No worries mate.

 

Glad to help.

 

Reg
2026.1
0 Likes
Message 10 of 13

eugene.morales
Enthusiast
Enthusiast

Thank you for reviewing and applying the right coding. it also works a treat now but is there a possibility that I can create a separate parameter for length and width.

0 Likes
Message 11 of 13

rhasell
Advisor
Advisor
Accepted solution

Hi

I have two separate rules. I do not have the time at the moment to make a new rule for you, so I will attach them both as is.

I have two additional parameters, DIMa and DIMb, for use with sketches, if these are populated they take priority (If detected, they are used instead).

 

Create Default Parameters:

'Create Default user Parameters

Dim oPartDoc as PartDocument = ThisDoc.Document
Dim userParams As UserParameters = oPartDoc.ComponentDefinition.Parameters.UserParameters
Dim newParam As UserParameter ' Placeholder
Dim oFormat As CustomPropertyFormat

'LENGTH
Try
	oParam = oPartDoc.ComponentDefinition.Parameters("LENGTH")
Catch 	'If the parameter was not found, then create a new one.
	newParam = userParams.AddByExpression("LENGTH", 0, "mm") ' Create the Parameter as per above
	newParam.ExposedAsProperty = True 'Flag for Export
	oFormat = newParam.CustomPropertyFormat 'For some reason or other this line is needed to enable the following formatting
	oFormat.PropertyType = Inventor.CustomPropertyTypeEnum.kTextPropertyType
	oFormat.Precision = Inventor.CustomPropertyPrecisionEnum.kZeroDecimalPlacePrecision 'Set one decimal place
	'oFormat.Units="mm" 'Units
	oFormat.ShowUnitsString = False
	oFormat.ShowLeadingZeros = False
	oFormat.ShowTrailingZeros = False
End Try

'WIDTH
Try
	oParam = oPartDoc.ComponentDefinition.Parameters("WIDTH")
Catch 	'If the parameter was not found, then create a new one.
	newParam = userParams.AddByExpression("WIDTH", 0, "mm") ' Create the Parameter as per above
	newParam.ExposedAsProperty = True 'Flag for Export
	oFormat = newParam.CustomPropertyFormat 'For some reason or other this line is needed to enable the following formatting
	oFormat.PropertyType = Inventor.CustomPropertyTypeEnum.kTextPropertyType
	oFormat.Precision = Inventor.CustomPropertyPrecisionEnum.kZeroDecimalPlacePrecision 'Set one decimal place
	'oFormat.Units="mm" 'Units
	oFormat.ShowUnitsString = False
	oFormat.ShowLeadingZeros = False
	oFormat.ShowTrailingZeros = False

End Try
'HEIGHT
Try
	oParam = oPartDoc.ComponentDefinition.Parameters("HEIGHT")
Catch 	'If the parameter was not found, then create a new one.
	newParam = userParams.AddByExpression("HEIGHT", 0, "mm") ' Create the Parameter as per above
	newParam.ExposedAsProperty = True 'Flag for Export
	oFormat = newParam.CustomPropertyFormat 'For some reason or other this line is needed to enable the following formatting
	oFormat.PropertyType = Inventor.CustomPropertyTypeEnum.kTextPropertyType
	oFormat.Precision = Inventor.CustomPropertyPrecisionEnum.kZeroDecimalPlacePrecision 'Set one decimal place
	'oFormat.Units="mm" 'Units
	oFormat.ShowUnitsString = False
	oFormat.ShowLeadingZeros = False
	oFormat.ShowTrailingZeros = False

End Try
'OD
Try
	oParam = oPartDoc.ComponentDefinition.Parameters("OD")
Catch 	'If the parameter was not found, then create a new one.
	newParam = userParams.AddByExpression("OD", 0, "mm") ' Create the Parameter as per above
	newParam.ExposedAsProperty = True 'Flag for Export
	oFormat = newParam.CustomPropertyFormat 'For some reason or other this line is needed to enable the following formatting
	oFormat.PropertyType = Inventor.CustomPropertyTypeEnum.kTextPropertyType
	oFormat.Precision = Inventor.CustomPropertyPrecisionEnum.kZeroDecimalPlacePrecision 'Set one decimal place
	'oFormat.Units="mm" 'Units
	oFormat.ShowUnitsString = False
	oFormat.ShowLeadingZeros = False
	oFormat.ShowTrailingZeros = False
End Try
'THICK
Try
	oParam = oPartDoc.ComponentDefinition.Parameters("THICK")
Catch 	'If the parameter was not found, then create a new one.
	newParam = userParams.AddByExpression("THICK", 0, "mm") ' Create the Parameter as per above
	newParam.ExposedAsProperty = True 'Flag for Export
	oFormat = newParam.CustomPropertyFormat 'For some reason or other this line is needed to enable the following formatting
	oFormat.PropertyType = Inventor.CustomPropertyTypeEnum.kTextPropertyType
	oFormat.Precision = Inventor.CustomPropertyPrecisionEnum.kZeroDecimalPlacePrecision 'Set one decimal place
	'oFormat.Units="mm" 'Units
	oFormat.ShowUnitsString = False
	oFormat.ShowLeadingZeros = False
	oFormat.ShowTrailingZeros = False
End Try

'Corner Rounds
Try
	oParam = oPartDoc.ComponentDefinition.Parameters("RO")
Catch 	'If the parameter was not found, then create a new one.
	newParam = userParams.AddByExpression("RO", "THICK * 2.5ul", "mm") ' Create the Parameter as per above
	newParam = userParams.AddByExpression("RI", "THICK * 1.5ul", "mm") ' Create the Parameter as per above
End Try

'DIMA
Try
	oParam = oPartDoc.ComponentDefinition.Parameters("DIMA")
Catch 	'If the parameter was not found, then create a new one.
	oNewParam = userParams.AddByExpression("DIMA", 0, "mm") ' Create the Parameter as per above
End Try

'DIMB
Try
	oParam = oPartDoc.ComponentDefinition.Parameters("DIMB")
Catch 'If the parameter was not found, then create a new one.
	oNewParam = userParams.AddByExpression("DIMB", 0, "mm") ' Create the Parameter as per above
End Try

 

 

My Part Extent Rule, it is slightly different to the code posted above:

Imports System.Collections.Generic
'Set Part Extents

Sub Main ()
oDoc = ThisDoc.Document

' ---------------------------------------------------
'Check to see if this part is a sheet-metal part.
If oDoc.ComponentDefinition.Type = 150995200 Then
Call sm ()

ElseIf Parameter("DIMA") <>0
Call DIMA ()

ElseIf Parameter("DIMA") = 0
Call NRML ()

ElseIf iProperties.Value("Custom", "TYPE")="PLATE RND"
Call oRound()

End If
End Sub

'-------------------------------------------------------------------
Sub sm()
	If Parameter("DIMA") <> 0
		MessageBox.Show("Sheetmetal Part, pass to DIMA", "Re-Routing")
Call DIMA()
Return
End If
Dim oPart As PartDocument
'Dim oAssy As AssemblyDocument
oPart = ThisDoc.Document
oDoc = ThisDoc.Document

'oDoc = ThisDoc.Document
 '   If oDoc.ComponentDefinition.Type = 150995200 Then
    MessageBox.Show("This rule will be run in a SheetMetal part...", "iLogic")
    
	'Turn off work features so they dont calculate
		oPart.ObjectVisibility.AllWorkFeatures = False
		InventorVb.DocumentUpdate()
		'
		Dim sizes As New List(Of Double)
		'
		sizes.Add(SheetMetal.FlatExtentsLength)
		sizes.Add(SheetMetal.FlatExtentsWidth)
		sizes.Sort()
		Parameter("HEIGHT") = sizes(1)
		Parameter("WIDTH") = sizes(0)
		iLogicVb.UpdateWhenDone = True
	'Turn back on work features - optional
		oPart.ObjectVisibility.AllWorkFeatures = True 
		
'			End If
'		End Select
'-------------------------------------------------------------------
'End If
Call oFin()
End Sub

'-------------------------------------------------------------------
Sub NRML ()
Dim oPart As PartDocument
'Dim oAssy As AssemblyDocument
oPart = ThisDoc.Document
oDoc = ThisDoc.Document
Dim partDoc As PartDocument = ThisDoc.Document

'*** - NEW - ***
'Author: DRoam
'https://forums.autodesk.com/t5/inventor-customization/getting-accurate-rangebox-xyz-values/td-p/8332760
'Get Part Extents, utilizing new features in Invetor 2020
' Get surface body to measure (assume it's the first body).
Dim body1 As SurfaceBody = partDoc.ComponentDefinition.SurfaceBodies.Item(1)

' Get the oriented minimum range box of the body.
' NOTE: "OrientedMinimumRangeBox" was added in Inventor 2020.3/2021.
Dim minBox As OrientedBox = body1.OrientedMinimumRangeBox

' Get length of each side of minimum range box.
Dim dir1 As Double = minBox.DirectionOne.Length
Dim dir2 As Double = minBox.DirectionTwo.Length
Dim dir3 As Double = minBox.DirectionThree.Length

' Convert lengths to document's length units.
Dim uom As UnitsOfMeasure = partDoc.UnitsOfMeasure

dir1 = uom.ConvertUnits(dir1, "cm", uom.LengthUnits)
dir2 = uom.ConvertUnits(dir2, "cm", uom.LengthUnits)
dir3 = uom.ConvertUnits(dir3, "cm", uom.LengthUnits)

' Sort lengths from smallest to largest.
Dim lengths As New List(Of Double) From {dir1, dir2, dir3 }
lengths.Sort

Dim minLength As Double = lengths(0)
Dim midLength As Double = lengths(1)
Dim maxLength As Double = lengths(2)
Parameter("HEIGHT") = lengths(2)
Parameter("WIDTH") = lengths(1)
'Parameter("THICK") = lengths(0)
Call oFin()
End Sub

'-------------------------------------------------------------------
Sub oRound ()
Dim oPart As PartDocument
oPart = ThisDoc.Document
oDoc = ThisDoc.Document

		'Turn off work features so they don't calculate
		oPart.ObjectVisibility.AllWorkFeatures = False
		InventorVb.DocumentUpdate()

Dim sizes As New List(Of Double)

sizes.Add(Measure.ExtentsLength)
sizes.Add(Measure.ExtentsWidth)
sizes.Add(Measure.ExtentsHeight)
sizes.Sort()
Parameter("OD") = sizes(2)
Call oFin()
End Sub

'-------------------------------------------------------------------
Sub DIMA ()
Dim oPart As PartDocument
'Dim oAssy As AssemblyDocument
oPart = ThisDoc.Document
oDoc = ThisDoc.Document
		'Turn off work features so they don't calculate
		oPart.ObjectVisibility.AllWorkFeatures = False
		InventorVb.DocumentUpdate()
Dim sizes As New List(Of Double)
'REV3 addition 30/03/2016
		'It allows me to populate the DIMA and DIMB fields without affecting the width and height boxes.
		MessageBox.Show("dim 'A&B' Detected " , "GDI iLogic")
			sizes.Add(Parameter("DIMA"))
			sizes.Add(Parameter("DIMB"))
			sizes.Sort()
			If Parameter("DIMA")=sizes(1)
			Parameter("HEIGHT") = "DIMA"
			Parameter("WIDTH") = "DIMB"
			Else
			Parameter("HEIGHT") = "DIMB"
			Parameter("WIDTH") = "DIMA"
			End If
iLogicVb.UpdateWhenDone = True
Call oFin()
End Sub

'-------------------------------------------------------------------
Sub oFin ()
oPart = ThisDoc.Document
'iLogicVb.UpdateWhenDone = True
'Turn back on work features - optional
oPart.ObjectVisibility.AllWorkFeatures = True 
iLogicVb.UpdateWhenDone = True
End Sub
''End If
'-------------------------------------------------------------------

 

 

Reg
2026.1
Message 12 of 13

eugene.morales
Enthusiast
Enthusiast

@rhasell thank you again for the time. I do want to skip creating a sketch and adding dimension in every part just to get the exact Length and Width. The aim is to automate the part created and it will increase my productivity in working with big projects. 

0 Likes
Message 13 of 13

rhasell
Advisor
Advisor

Hi

 

By default a sketch is not required, I just had to explain the purposes of the extra two parameters. If the the two properties  are used, they take priority, otherwise the process is automated.

 

The sketch method work better for skew items and certain library parts, as the sketch automatically updates for "Save as" and the parts are revised, without the need to re-run the code.

Reg
2026.1
0 Likes