Hello!
I'm trying to use a simple iLogic Rule for a very specific purpose - I have an upper-level assembly with subassemblies and these subassemblies include Frame Generator structural parts. I've customized these structural parts to have a "Mango_mass" parameter (lbmass units) that calculates the mass of a part by multiplying the generated length (G_L) of the frame with a linear density custom parameter that I've included as well (Mango_density, lbmass/ft).
What I'm wanting to do is, from the top-level assembly, cycle through each lower-level assembly and part in the whole model tree, test if that component has a parameter called "Mango_mass", and if it does, add it to a running tally. I simply want the total mass of the structure.
I am not familiar with iLogic at all, and I believe this is the only use I'll ever have for it, so I figure it's not worth the time learning the language for just this task. So I used ChatGPT 🙂 It's gotten most of the way, but it's having a bit of trouble with a couple of lines in the code. Here's what it's given me:
' Initialize a variable to store the total mass Dim totalMass As Double = 0 ' Subroutine to iterate through all components and sum the "Mango_mass" parameter Sub SumMangoMass(oCompDef) ' Loop through each occurrence in the assembly For Each oOccurrence As ComponentOccurrence In oCompDef.Occurrences Dim oSubCompDef As ComponentDefinition = oOccurrence.Definition ' Check if the occurrence is an assembly If TypeOf oSubCompDef Is AssemblyComponentDefinition Then ' Recursively call this subroutine for subassemblies SumMangoMass(oSubCompDef) Else ' Check if the part has a "Mango_mass" parameter Try ' Try to get the parameter Dim oParameter As Parameter = oSubCompDef.Parameters.Item("Mango_mass") ' Check if the parameter is numeric If oParameter.ParameterType = ParameterTypeEnum.kModelParameter AndAlso oParameter.Units = UnitsTypeEnum.kPoundMassUnits Then ' Add the parameter value to the total mass totalMass += oParameter.Value End If Catch ex As Exception ' Ignore exceptions for missing parameters End Try End If Next End Sub ' Main execution ' Access the top-level assembly Dim oDoc As AssemblyDocument = ThisApplication.ActiveDocument Dim oCompDef As AssemblyComponentDefinition = oDoc.ComponentDefinition ' Call the subroutine to sum the Mango_mass parameter Call SumMangoMass(oCompDef) ' Output the total mass MessageBox.Show("Total Mango Mass: " & totalMass & " lbmass", "Mass Calculation")
When this code is pasted into Inventor, there's an error line below "Call" in the line "Call SumMangoMass(oCompDef)". The error says "Class member declaration expected."
I'm hoping this is a simple fix. (Please don't spend a lot of time here, it's not a big deal if I can't get it to work.) If you think of something, I'd love a suggestion!
Thanks a lot!
Nate
Solved! Go to Solution.
Solved by Frederick_Law. Go to Solution.
Just out of curiosity, why are you calculating the mass when it is already there? Each steel shape will have the weight based on the material it is set to. Maybe I am missing something?
Hi!
Just for the sake of accuracy. Inventor calculates mass based on a density and a volume, but it always overshoots for our purposes. I have pounds-per-foot values that will be quite a bit more accurate than Inventor's default. Like I said, it's not a big deal if we can't get the code to work because we can always just use Inventor's calculation and give a margin of error.
You should be able to add mass up in part list.
Your code declare Sub inside Main sub without declaring the Main sub.
It should look like this:
Sub Main ()
End Sub
Sub SumMangoMass(oCompDef)
End Sub
Thanks for the response!
I'm still a bit confused. Is the main sub empty?
Here's what I've got.
' Initialize a variable to store the total mass Dim totalMass As Double = 0
Sub Main()
End Sub
' Subroutine to iterate through all components and sum the "Mango_mass" parameter Sub SumMangoMass(oCompDef) ' Loop through each occurrence in the assembly For Each oOccurrence As ComponentOccurrence In oCompDef.Occurrences Dim oSubCompDef As ComponentDefinition = oOccurrence.Definition ' Check if the occurrence is an assembly If TypeOf oSubCompDef Is AssemblyComponentDefinition Then ' Recursively call this subroutine for subassemblies SumMangoMass(oSubCompDef) Else ' Check if the part has a "Mango_mass" parameter Try ' Try to get the parameter Dim oParameter As Parameter = oSubCompDef.Parameters.Item("Mango_mass") ' Check if the parameter is numeric If oParameter.ParameterType = ParameterTypeEnum.kModelParameter AndAlso oParameter.Units = UnitsTypeEnum.kPoundMassUnits Then ' Add the parameter value to the total mass totalMass += oParameter.Value End If Catch ex As Exception ' Ignore exceptions for missing parameters End Try End If Next End Sub ' Main execution ' Access the top-level assembly Dim oDoc As AssemblyDocument = ThisApplication.ActiveDocument Dim oCompDef As AssemblyComponentDefinition = oDoc.ComponentDefinition ' Call the subroutine to sum the Mango_mass parameter Call SumMangoMass(oCompDef) ' Output the total mass MessageBox.Show("Total Mango Mass: " & totalMass & " lbmass", "Mass Calculation")
Main is everything not SumMangoMass.
Main is the program that run and call SumMangoMass.
Would you mind typing out what you mean in my code? That might help me a bit, I'm unfamiliar with anything VB.
Okay! Making some progress, that link did help @Frederick_Law, thank you. The code no longer shows errors, and it lets me run, but for some reason it's outputting a mass of 0 and I'm not sure why. The parameters in the frame members are named correctly, is the code missing something?
Sub Main() Dim totalMass As Double = 0 ' Initialize totalMass in Main ' Access the top-level assembly Dim oDoc As AssemblyDocument = ThisApplication.ActiveDocument Dim oCompDef As AssemblyComponentDefinition = oDoc.ComponentDefinition ' Call the subroutine and get the total mass totalMass = SumMangoMass(oCompDef, totalMass) ' Output the total mass MessageBox.Show("Total Mango Mass: " & totalMass & " lbmass", "Mass Calculation") End Sub ' Subroutine to iterate through all components and sum the "Mango_mass" parameter Function SumMangoMass(oCompDef As AssemblyComponentDefinition, totalMass As Double) As Double ' Loop through each occurrence in the assembly For Each oOccurrence As ComponentOccurrence In oCompDef.Occurrences Dim oSubCompDef As ComponentDefinition = oOccurrence.Definition ' Check if the occurrence is an assembly If TypeOf oSubCompDef Is AssemblyComponentDefinition Then ' Recursively call this subroutine for subassemblies totalMass = SumMangoMass(oSubCompDef, totalMass) Else ' Check if the part has a "Mango_mass" parameter Try ' Try to get the parameter Dim oParameter As Parameter = oSubCompDef.Parameters.Item("Mango_mass") ' Check if the parameter is numeric If oParameter.ParameterType = ParameterTypeEnum.kModelParameter AndAlso oParameter.Units = UnitsTypeEnum.kLbMassMassUnits Then ' Add the parameter value to the total mass totalMass += oParameter.Value End If Catch ex As Exception ' Ignore exceptions for missing parameters End Try End If Next ' Return the updated total mass Return totalMass End Function
You can use Log to see what's happening inside the program.
https://help.autodesk.com/view/INVNTOR/2023/ENU/?guid=GUID-BE996AA3-DE49-4765-86A7-129DD7B42A4A
Can't find what you're looking for? Ask the community or share your knowledge.