Community
Inventor Forum
Welcome to Autodesk’s Inventor Forums. Share your knowledge, ask questions, and explore popular Inventor topics.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

iLogic parameter searching

11 REPLIES 11
SOLVED
Reply
Message 1 of 12
nsappZS6WZ
312 Views, 11 Replies

iLogic parameter searching

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

11 REPLIES 11
Message 2 of 12
blandb
in reply to: nsappZS6WZ

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? 

Autodesk Certified Professional
Message 3 of 12
nsappZS6WZ
in reply to: blandb

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.

Message 4 of 12
Frederick_Law
in reply to: nsappZS6WZ

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
Message 5 of 12
nsappZS6WZ
in reply to: Frederick_Law

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")
Message 6 of 12
Frederick_Law
in reply to: nsappZS6WZ

Main is everything not SumMangoMass.

Main is the program that run and call SumMangoMass.

 

https://learn.microsoft.com/en-us/office/vba/language/concepts/getting-started/calling-sub-and-funct...

Message 7 of 12
nsappZS6WZ
in reply to: Frederick_Law

Would you mind typing out what you mean in my code? That might help me a bit, I'm unfamiliar with anything VB.

Message 8 of 12
nsappZS6WZ
in reply to: nsappZS6WZ

Oh, I see your link now. I'll see what I can figure out from it.
Message 9 of 12
nsappZS6WZ
in reply to: Frederick_Law

Just saw your link - I'll check it out and get back to you, thanks.

Message 10 of 12
nsappZS6WZ
in reply to: nsappZS6WZ

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
Message 11 of 12
Frederick_Law
in reply to: nsappZS6WZ

Message 12 of 12
nsappZS6WZ
in reply to: Frederick_Law

Was able to figure it out! Thank you so much for your help.

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report