How to write a Rule that will create a custom iproperty that contains math from Assy components?

How to write a Rule that will create a custom iproperty that contains math from Assy components?

chris
Advisor Advisor
1,506 Views
11 Replies
Message 1 of 12

How to write a Rule that will create a custom iproperty that contains math from Assy components?

chris
Advisor
Advisor

I'm look for some help with a rule that will create a custom property called "Total Length", but the property will be a total length of some components in an assy. 

 

Example: Beam Length + Bottom Plate Thk + top Plate Thk

 

The Component parameter names that will make this up are:

Beam Component:    Length

Bottom Plate:            Thk

Top Plate:                   Thk

 

**edit**  Is it possible to get it formatted to be in Feet/Inches (fraction to 1/16), and to complicate things further, can the total decimal inches in () show up a couple spaces behind the fraction answer?

 

I can already get the decimal length with this code (see below), but I really would like have it as a custom iProp and have the dual display of the length

iProperties.Value("Project", "Description") = Parameter("08443MFD.AS180.01.01:1", "Length") + Parameter("08443MFD.AS180.01.02:2", "Thk") + Parameter("08443MFD.AS180.01.03:1", "Thk"

 

0 Likes
1,507 Views
11 Replies
Replies (11)
Message 2 of 12

chris
Advisor
Advisor

I know I can do this with a sketch at the assembly level, but I'm trying to avoid that so the template is a little more... eloquent.

 

0 Likes
Message 3 of 12

Cadkunde.nl
Collaborator
Collaborator
Class iRule()
	Sub Main()
		Dim oMainDoc As AssemblyDocument = ThisDoc.Document

		Dim List_strComponents As New List(Of String)
		Dim List_strParams As New List(Of String)

		List_strComponents.Add("08443MFD.AS180.01.01:1")
		List_strComponents.Add("name2")
		List_strComponents.Add("name3")

		List_strParams.Add("Length")
		List_strParams.Add("Thk")
		List_strParams.Add("Thk")
		
		'Uncomment this if you want to make a userparameter named Total_Length on assembly level, then export it to custom iproperty
		'CreateParamTotalLength(oMainDoc)
		
		Dim dblTotalValue As Double = 0
		
		For i = 0 To List_strComponents.Count - 1
			Dim oOcc As Inventor.ComponentOccurrence
			Try
				oOcc = oMainDoc.ComponentDefinition.Occurrences.ItemByName(List_strComponents(i))
			Catch
				MsgBox("unable to find occurrence List_strComponents(i)")
				Continue For
			End Try

			Dim occDoc As Inventor.Document = oOcc.Definition.Document

			Dim ModelDef As Inventor.ComponentDefinition = Nothing
			If occDoc.DocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then
				ModelDef = CType(occDoc, Inventor.AssemblyDocument).ComponentDefinition
			ElseIf occDoc.DocumentType = DocumentTypeEnum.kPartDocumentObject Then
				ModelDef = CType(occDoc, Inventor.PartDocument).ComponentDefinition
			End If

			Dim uom As UnitsOfMeasure = occDoc.UnitsOfMeasure
			Dim intPrecision As Integer = 3
			Dim strInputUnit As String = "cm" 'base unit of inventor
			Dim strOutputUnit As String = "inch"
			Dim dblValue As Double = (uom.ConvertUnits(ModelDef.Parameters.UserParameters.Item(List_strParams(i)).value, strInputUnit, strOutputUnit))
			dblValue = Math.Round(dblValue, intPrecision)
			dblTotalValue = dblTotalValue + dblValue
		Next
			'comment this part if you uncommented CreateParamTotalLength(oMainDoc)
			Try
				oMainDoc.PropertySets.Item("Inventor User Defined Properties").Item("Total_Length").Value = dblTotalValue
			Catch
				oMainDoc.PropertySets.Item("Inventor User Defined Properties").Add(dblTotalValue, "Total_Length")
			End Try
	End Sub
'Use this if you want to go make a userparameter on assembly level first Sub CreateParamTotalLength(oMainDoc As AssemblyDocument) Dim oParam As Inventor.UserParameter = Nothing Try oParam = oMainDoc.ComponentDefinition.Parameters.UserParameters.Item("Total_Length") Catch oParam = oMainDoc.ComponentDefinition.Parameters.UserParameters.AddByValue("Total_Length", 0, UnitsTypeEnum.kInchLengthUnits) End Try Dim oFormat As CustomPropertyFormat If Not oParam.ExposedAsProperty = True Then oParam.ExposedAsProperty = True cFormat = oParam.CustomPropertyFormat cFormat.Precision = CustomPropertyPrecisionEnum.kThreeDecimalPlacesPrecision cFormat.ShowTrailingZeros = True cFormat.ShowUnitsString = False End If End Sub End Class
0 Likes
Message 4 of 12

Cadkunde.nl
Collaborator
Collaborator

I have 0 experience with inch/feet and how fractions look like.

So this code might need some editing towards how many precision you want and type of units.

But I set up the code for you to easily edit those parts

 

In this code i added 3 items to list_strComponents and 3 items to list_strParams.

 

so it looks at first items for occurrence "08443MFD.bla.bla" and then looks for parameter "Length"

Then it looks at second item "Name2" and "Thk" etc etc

You are free to add and remove items. just remember that item1 of strComponents is linked to item1 of strParams

 

Also you might want to create a userparameter on the top assembly first, then you need to read the comments what to do

 

Hope this answers what you are looking for

 

An alternative solution is:

Go to parameter list in top assembly, Link--> ipt --> add the parameter you want

Then you could make a parameter "Total_Length" and link the parameters with an equation: Thickness + Thk_1 + Thk_2

This will be always updated and maybe even better than using ilogic

 

 

 

 

 

0 Likes
Message 5 of 12

chris
Advisor
Advisor

@Cadkunde.nl  Thanks a lot!!! I run the rule and it worked great for the decimal inches... that will get me started.

However, if anyone else knows how to add in the fractional option that would be great.

0 Likes
Message 6 of 12

Cadkunde.nl
Collaborator
Collaborator

Perhaps you can give me a screenshots. Or a part with fractions set

0 Likes
Message 7 of 12

AlwaysFriend
Observer
Observer

To create a custom iProperty that contains the total length of some components in an assembly, you can use iLogic in Autodesk Inventor. Here's how you can create a rule that will set the value of a custom iProperty called "Total Length" based on the length of a beam component, the thickness of a bottom plate, and the thickness of a top plate:

  1. Open the assembly that you want to add the custom iProperty to.
  2. Open the iLogic Editor by going to the Manage tab and clicking on "iLogic Editor" in the iLogic panel.
  3. Create a new rule by clicking on the "New Rule" button in the iLogic Editor.
  4. Name the rule "Set Total Length" and add the following code:

 

 

 

' Get the length of the beam component
Dim beamLength As Double = Parameter("Beam Component Name", "Length")

' Get the thickness of the bottom plate
Dim bottomPlateThk As Double = Parameter("Bottom Plate Name", "Thk")

' Get the thickness of the top plate
Dim topPlateThk As Double = Parameter("Top Plate Name", "Thk")

' Calculate the total length
Dim totalLength As Double = beamLength + bottomPlateThk + topPlateThk

' Set the value of the custom iProperty
iProperties.Value("Custom", "Total Length") = totalLength

 

 

Replace "Beam Component Name", "Bottom Plate Name", and "Top Plate Name" with the names of the components in your assembly that contain the length and thickness parameters.

 

  5.Save the rule and close the iLogic Editor.

 

Now, every time you run the "Set Total Length" rule, it will calculate the total length of the components and set the value of the custom iProperty "Total Length" to the calculated value.

To format the custom iProperty to display the length in feet and inches (fraction to 1/16), you can modify the rule as follows:

 

 

' Get the length of the beam component in inches
Dim beamLengthIn As Double = Parameter("Beam Component Name", "Length")

' Get the thickness of the bottom plate in inches
Dim bottomPlateThkIn As Double = Parameter("Bottom Plate Name", "Thk")

' Get the thickness of the top plate in inches
Dim topPlateThkIn As Double = Parameter("Top Plate Name", "Thk")

' Calculate the total length in inches
Dim totalLengthIn As Double = beamLengthIn + bottomPlateThkIn + topPlateThkIn

' Convert total length from inches to feet and inches
Dim totalFeet As Integer = Math.Floor(totalLengthIn / 12)
Dim totalInches As Double = totalLengthIn - (totalFeet * 12)
Dim totalInchesRound As Double = Math.Round(totalInches * 16) / 16

' Set the value of the custom iProperty
iProperties.Value("Custom", "Total Length") = totalFeet.ToString() & "'-" & totalInchesRound.ToString("0.0''")

 

 

This code first calculates the total length in inches and then converts it to feet and inches. The total inches are rounded to the nearest 1/16th and displayed with a precision of one decimal place. The resulting value is formatted as feet-inches, and the dual display of the total length is shown.

 

0 Likes
Message 8 of 12

chris
Advisor
Advisor

@AlwaysFriend  First off, thank you for the rule(s), the first image is what I get when I run your first rule, the second image is when I run the conversion rule, what I was hoping for  on the conversion part was a final value that looked like this:   Total Length  4'-1 1/2   (49.5")

I'm not sure how to get it to go to fractional inches

 

chris_0-1679619016262.pngchris_1-1679619059433.png

 

 

 

0 Likes
Message 9 of 12

chris
Advisor
Advisor

@AlwaysFriend Here's an example of one I did where I ran the Beam and plates as a single multibody part, (this of course makes it easy, because I have access to the total length in parameters and I can create two user parameters and format each, one for decimal inches and one for feet fractions

 

I'm showing it on the beam as a "luxury" but the main purpose is in the Description

 

chris_0-1679619564117.png

chris_1-1679619623174.png

chris_0-1679619736740.png

 

 

 

 

0 Likes
Message 10 of 12

Cadkunde.nl
Collaborator
Collaborator

"To create a custom iProperty that contains the total length of some components in an assembly, you can use iLogic in Autodesk Inventor. Here's how you can create a rule that will set the value of a custom iProperty called "Total Length" based on the length of a beam component, the thickness of a bottom plate, and the thickness of a top plate"

 

Haha this is just too funny

This is how chatGPT answers questions. Reading your post, its just a full copy paste of chatGPT. This is first time on forum i want to give an anti-kudo

0 Likes
Message 11 of 12

Cadkunde.nl
Collaborator
Collaborator

 

Sorry I havent got time to check myself what I'm about to suggest.

 

I wrote my code in a way you can sepcify the unitType (Inch) and Precisiontype

 

Perhaps you can check a different precisiontype for fractional

 

Knipsel.PNG

My previous code had an alternative method to use the sub CreateParamTotalLength, if you commented and uncommented some parts.

 

Class iRule()
	Sub Main()
		Dim oMainDoc As AssemblyDocument = ThisDoc.Document

		Dim List_strComponents As New List(Of String)
		Dim List_strParams As New List(Of String)

		List_strComponents.Add("08443MFD.AS180.01.01:1")
		List_strComponents.Add("name2")
		List_strComponents.Add("name3")

		List_strParams.Add("Length")
		List_strParams.Add("Thk")
		List_strParams.Add("Thk")
		
		CreateParamTotalLength(oMainDoc)
		
		Dim dblTotalValue As Double = 0
		
		For i = 0 To List_strComponents.Count - 1
			Dim oOcc As Inventor.ComponentOccurrence
			Try
				oOcc = oMainDoc.ComponentDefinition.Occurrences.ItemByName(List_strComponents(i))
			Catch
				MsgBox("unable to find occurrence List_strComponents(i)")
				Continue For
			End Try

			Dim occDoc As Inventor.Document = oOcc.Definition.Document

			Dim ModelDef As Inventor.ComponentDefinition = Nothing
			If occDoc.DocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then
				ModelDef = CType(occDoc, Inventor.AssemblyDocument).ComponentDefinition
			ElseIf occDoc.DocumentType = DocumentTypeEnum.kPartDocumentObject Then
				ModelDef = CType(occDoc, Inventor.PartDocument).ComponentDefinition
			End If

			Dim uom As UnitsOfMeasure = occDoc.UnitsOfMeasure
			Dim intPrecision As Integer = 3
			Dim strInputUnit As String = "cm" 'base unit of inventor
			Dim strOutputUnit As String = "inch"
			Dim dblValue As Double = (uom.ConvertUnits(ModelDef.Parameters.UserParameters.Item(List_strParams(i)).value, strInputUnit, strOutputUnit))
			dblValue = Math.Round(dblValue, intPrecision)
			dblTotalValue = dblTotalValue + dblValue
		Next

	End Sub

	Sub CreateParamTotalLength(oMainDoc As AssemblyDocument)
		Dim oParam As Inventor.UserParameter = Nothing
		Try
			oParam = oMainDoc.ComponentDefinition.Parameters.UserParameters.Item("Total_Length")
		Catch
			oParam = oMainDoc.ComponentDefinition.Parameters.UserParameters.AddByValue("Total_Length", 0, UnitsTypeEnum.kInchLengthUnits)
		End Try

		Dim oFormat As CustomPropertyFormat
		If Not oParam.ExposedAsProperty = True Then
			oParam.ExposedAsProperty = True
			cFormat = oParam.CustomPropertyFormat
			cFormat.Precision = CustomPropertyPrecisionEnum.kEighthsFractionalLengthPrecision
			cFormat.ShowTrailingZeros = True
			cFormat.ShowUnitsString = False
		End If
	End Sub
End Class

0 Likes
Message 12 of 12

chris
Advisor
Advisor

So he got that iLogic from Chat GPT.... not bad...